5b2235736019c7264c5d41810918cdbd04ee3df0
[bluesky.git] / bluesky / crypto.c
1 /* Blue Sky: File Systems in the Cloud
2  *
3  * Copyright (C) 2009  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * TODO: Licensing
7  */
8
9 #include <stdint.h>
10 #include <assert.h>
11 #include <errno.h>
12 #include <pthread.h>
13 #include <glib.h>
14 #include <string.h>
15 #include <gcrypt.h>
16
17 #include "bluesky-private.h"
18
19 /* Cryptographic operations.  The rest of the BlueSky code merely calls into
20  * the functions in this file, so this is the only point where we interface
21  * with an external cryptographic library. */
22
23 /* TODO: We ought to switch to an authenticated encryption mode like EAX. */
24
25 GCRY_THREAD_OPTION_PTHREAD_IMPL;
26
27 void bluesky_crypt_init()
28 {
29     gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
30
31     if (gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P))
32         return;
33
34     if (!gcry_check_version(GCRYPT_VERSION))
35         g_error("libgcrypt version mismatch\n");
36
37     gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
38     gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
39 }
40
41 /* Return cryptographically-strong random data. */
42 void bluesky_crypt_random_bytes(guchar *buf, gint len)
43 {
44     gcry_randomize(buf, len, GCRY_STRONG_RANDOM);
45 }
46
47 /* Hash a string down to an encryption key. */
48 void bluesky_crypt_hash_key(const char *keystr, uint8_t *out)
49 {
50     guint8 raw_csum[32];
51     gsize csum_len = sizeof(raw_csum);
52
53     assert(CRYPTO_KEY_SIZE == 16);
54
55     GChecksum *csum = g_checksum_new(G_CHECKSUM_SHA256);
56     g_checksum_update(csum, (const guchar *)keystr, strlen(keystr));
57     g_checksum_get_digest(csum, raw_csum, &csum_len);
58     g_checksum_free(csum);
59
60     memcpy(out, raw_csum, CRYPTO_KEY_SIZE);
61 }
62
63 /* Encrypt a data block. */
64 BlueSkyRCStr *bluesky_crypt_encrypt(BlueSkyRCStr *in, const uint8_t *key)
65 {
66     gcry_error_t status;
67     gcry_cipher_hd_t handle;
68
69     status = gcry_cipher_open(&handle, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CTR,
70                               0);
71     if (status) {
72         g_error("gcrypt error setting up encryption: %s\n",
73                 gcry_strerror(status));
74     }
75
76     uint8_t *out = g_malloc0(in->len + CRYPTO_BLOCK_SIZE);
77
78     gcry_cipher_setkey(handle, key, CRYPTO_KEY_SIZE);
79     if (status) {
80         g_error("gcrypt error setting key: %s\n",
81                 gcry_strerror(status));
82     }
83
84     bluesky_crypt_random_bytes(out, CRYPTO_BLOCK_SIZE);
85     status = gcry_cipher_setctr(handle, out, CRYPTO_BLOCK_SIZE);
86     if (status) {
87         g_error("gcrypt error setting IV: %s\n",
88                 gcry_strerror(status));
89     }
90
91     status = gcry_cipher_encrypt(handle, out + CRYPTO_BLOCK_SIZE, in->len,
92                                  in->data, in->len);
93     if (status) {
94         g_error("gcrypt error encrypting: %s\n",
95                 gcry_strerror(status));
96     }
97
98     gcry_cipher_close(handle);
99
100     return bluesky_string_new(out, in->len + CRYPTO_BLOCK_SIZE);
101 }
102
103 /* Decrypt a data block. */
104 BlueSkyRCStr *bluesky_crypt_decrypt(BlueSkyRCStr *in, const uint8_t *key)
105 {
106     gcry_error_t status;
107     gcry_cipher_hd_t handle;
108
109     g_return_val_if_fail(in->len > CRYPTO_BLOCK_SIZE, NULL);
110
111     status = gcry_cipher_open(&handle, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CTR,
112                               0);
113     if (status) {
114         g_error("gcrypt error setting up encryption: %s\n",
115                 gcry_strerror(status));
116     }
117
118     uint8_t *out = g_malloc0(in->len - CRYPTO_BLOCK_SIZE);
119
120     gcry_cipher_setkey(handle, key, CRYPTO_KEY_SIZE);
121     if (status) {
122         g_error("gcrypt error setting key: %s\n",
123                 gcry_strerror(status));
124     }
125
126     status = gcry_cipher_setctr(handle, in->data, CRYPTO_BLOCK_SIZE);
127     if (status) {
128         g_error("gcrypt error setting IV: %s\n",
129                 gcry_strerror(status));
130     }
131
132     status = gcry_cipher_decrypt(handle, out, in->len - CRYPTO_BLOCK_SIZE,
133                                  in->data + CRYPTO_BLOCK_SIZE,
134                                  in->len - CRYPTO_BLOCK_SIZE);
135     if (status) {
136         g_error("gcrypt error decrypting: %s\n",
137                 gcry_strerror(status));
138     }
139
140     gcry_cipher_close(handle);
141
142     return bluesky_string_new(out, in->len - CRYPTO_BLOCK_SIZE);
143 }