2a5bf6ebf0fb36bb2e548bcee2bbc22b94c1ac5c
[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 /* Compute an HMAC of a given data block with the given key.  The key and
64  * output should both as large as the hash output size. */
65 #define MD_ALGO GCRY_MD_SHA256
66 void bluesky_crypt_hmac(const char *buf, size_t bufsize,
67                         const uint8_t key[CRYPTO_HASH_SIZE],
68                         uint8_t output[CRYPTO_HASH_SIZE])
69 {
70     gcry_error_t status;
71     gcry_md_hd_t handle;
72
73     g_assert(gcry_md_get_algo_dlen(MD_ALGO) == CRYPTO_HASH_SIZE);
74
75     status = gcry_md_open(&handle, MD_ALGO, GCRY_MD_FLAG_HMAC);
76     if (status) {
77         g_error("gcrypt error setting up message digest: %s\n",
78                 gcry_strerror(status));
79         g_assert(FALSE);
80     }
81
82     status = gcry_md_setkey(handle, key, CRYPTO_HASH_SIZE);
83     if (status) {
84         g_error("gcrypt error setting HMAC key: %s\n",
85                 gcry_strerror(status));
86         g_assert(FALSE);
87     }
88
89     gcry_md_write(handle, buf, bufsize);
90
91     unsigned char *digest = gcry_md_read(handle, MD_ALGO);
92     memcpy(output, digest, CRYPTO_HASH_SIZE);
93
94     gcry_md_close(handle);
95 }
96
97 void bluesky_crypt_derive_keys(BlueSkyCryptKeys *keys, const gchar *master)
98 {
99     uint8_t outbuf[CRYPTO_HASH_SIZE];
100
101     const char *key_type = "ENCRYPT";
102     bluesky_crypt_hmac(key_type, strlen(key_type),
103                        (const uint8_t *)master, outbuf);
104     memcpy(keys->encryption_key, outbuf, sizeof(keys->encryption_key));
105
106     key_type = "AUTH";
107     bluesky_crypt_hmac(key_type, strlen(key_type),
108                        (const uint8_t *)master, outbuf);
109     memcpy(keys->authentication_key, outbuf, sizeof(keys->authentication_key));
110 }
111
112 /* A boolean: are blocks of the specified type encrypted in the BlueSky file
113  * system? */
114 gboolean bluesky_crypt_block_needs_encryption(uint8_t type)
115 {
116     type -= '0';
117
118     switch (type) {
119     case LOGTYPE_DATA:
120     case LOGTYPE_INODE:
121         return TRUE;
122     case LOGTYPE_INODE_MAP:
123     case LOGTYPE_CHECKPOINT:
124         return FALSE;
125     default:
126         g_warning("Unknown log item type in crypto layer: %d!\n",
127                   type);
128         return TRUE;
129     }
130 }
131
132 void bluesky_crypt_block_encrypt(gchar *cloud_block, size_t len,
133                                  BlueSkyCryptKeys *keys)
134 {
135     if (bluesky_options.disable_crypto)
136         return;
137
138     gcry_error_t status;
139     gcry_cipher_hd_t handle;
140
141     status = gcry_cipher_open(&handle, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CTR,
142                               0);
143     if (status) {
144         g_error("gcrypt error setting up encryption: %s\n",
145                 gcry_strerror(status));
146     }
147
148     struct cloudlog_header *header = (struct cloudlog_header *)cloud_block;
149     g_assert(memcmp(header->magic, CLOUDLOG_MAGIC, sizeof(header->magic)) == 0);
150
151     gcry_cipher_setkey(handle, keys->encryption_key, CRYPTO_KEY_SIZE);
152     if (status) {
153         g_error("gcrypt error setting key: %s\n",
154                 gcry_strerror(status));
155     }
156
157     gboolean encrypted = bluesky_crypt_block_needs_encryption(header->type);
158
159     if (encrypted) {
160         header->magic[3] ^= 0x10;
161
162         bluesky_crypt_random_bytes(header->crypt_iv, sizeof(header->crypt_iv));
163         status = gcry_cipher_setctr(handle, header->crypt_iv,
164                                     sizeof(header->crypt_iv));
165         if (status) {
166             g_error("gcrypt error setting IV: %s\n",
167                     gcry_strerror(status));
168         }
169
170         status = gcry_cipher_encrypt(handle,
171                                      cloud_block + sizeof(struct cloudlog_header),
172                                      GUINT32_FROM_LE(header->size1),
173                                      NULL, 0);
174         if (status) {
175             g_error("gcrypt error encrypting: %s\n",
176                     gcry_strerror(status));
177         }
178     } else {
179         memset(header->crypt_iv, 0, sizeof(header->crypt_iv));
180     }
181
182     bluesky_crypt_hmac((char *)&header->crypt_iv,
183                        cloud_block + len - (char *)&header->crypt_iv - GUINT32_FROM_LE(header->size3),
184                        keys->authentication_key,
185                        header->crypt_auth);
186
187     gcry_cipher_close(handle);
188 }
189
190 gboolean bluesky_crypt_block_decrypt(gchar *cloud_block, size_t len,
191                                      BlueSkyCryptKeys *keys,
192                                      gboolean allow_unauth)
193 {
194     gcry_error_t status;
195     uint8_t hmac_check[CRYPTO_HASH_SIZE];
196
197     gboolean encrypted = TRUE;
198
199     struct cloudlog_header *header = (struct cloudlog_header *)cloud_block;
200     if (memcmp(header->magic, CLOUDLOG_MAGIC,
201                     sizeof(header->magic)) == 0)
202         encrypted = FALSE;
203     else
204         g_assert(memcmp(header->magic, CLOUDLOG_MAGIC_ENCRYPTED,
205                         sizeof(header->magic)) == 0);
206
207     if (bluesky_options.disable_crypto) {
208         g_assert(encrypted == FALSE);
209         return TRUE;
210     }
211
212     if (encrypted != bluesky_crypt_block_needs_encryption(header->type)) {
213         g_warning("Encrypted status of item does not match expected!\n");
214     }
215
216     bluesky_crypt_hmac((char *)&header->crypt_iv,
217                        cloud_block + len - (char *)&header->crypt_iv - GUINT32_FROM_LE(header->size3),
218                        keys->authentication_key,
219                        hmac_check);
220     if (memcmp(hmac_check, header->crypt_auth, CRYPTO_HASH_SIZE) != 0) {
221         g_warning("Cloud block HMAC does not match!");
222         if (allow_unauth
223             && (header->type == LOGTYPE_INODE_MAP + '0'
224                 || header->type == LOGTYPE_CHECKPOINT + '0'))
225         {
226             g_warning("Allowing unauthenticated data from cleaner");
227         } else {
228             return FALSE;
229         }
230     }
231
232     if (encrypted) {
233         gcry_cipher_hd_t handle;
234         status = gcry_cipher_open(&handle, GCRY_CIPHER_AES,
235                                   GCRY_CIPHER_MODE_CTR, 0);
236         if (status) {
237             g_error("gcrypt error setting up encryption: %s\n",
238                     gcry_strerror(status));
239         }
240
241         gcry_cipher_setkey(handle, keys->encryption_key, CRYPTO_KEY_SIZE);
242         if (status) {
243             g_error("gcrypt error setting key: %s\n",
244                     gcry_strerror(status));
245         }
246
247         status = gcry_cipher_setctr(handle, header->crypt_iv,
248                                     sizeof(header->crypt_iv));
249         if (status) {
250             g_error("gcrypt error setting IV: %s\n",
251                     gcry_strerror(status));
252         }
253
254         status = gcry_cipher_decrypt(handle,
255                                      cloud_block + sizeof(struct cloudlog_header),
256                                      GUINT32_FROM_LE(header->size1),
257                                      NULL, 0);
258         if (status) {
259             g_error("gcrypt error decrypting: %s\n",
260                     gcry_strerror(status));
261         }
262         header->magic[3] ^= 0x10;
263         memset(header->crypt_iv, 0, sizeof(header->crypt_iv));
264
265         gcry_cipher_close(handle);
266     }
267
268     return TRUE;
269 }