Silence some compiler warnings.
[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 GCRY_THREAD_OPTION_PTHREAD_IMPL;
24
25 void bluesky_crypt_init()
26 {
27     gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
28
29     if (gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P))
30         return;
31
32     if (!gcry_check_version(GCRYPT_VERSION))
33         g_error("libgcrypt version mismatch\n");
34
35     gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
36     gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
37 }
38
39 /* Return cryptographically-strong random data. */
40 void bluesky_crypt_random_bytes(guchar *buf, gint len)
41 {
42     gcry_randomize(buf, len, GCRY_STRONG_RANDOM);
43 }
44
45 /* Hash a string down to an encryption key. */
46 void bluesky_crypt_hash_key(const char *keystr, uint8_t *out)
47 {
48     guint8 raw_csum[32];
49     gsize csum_len = sizeof(raw_csum);
50
51     assert(CRYPTO_KEY_SIZE == 16);
52
53     GChecksum *csum = g_checksum_new(G_CHECKSUM_SHA256);
54     g_checksum_update(csum, (const guchar *)keystr, strlen(keystr));
55     g_checksum_get_digest(csum, raw_csum, &csum_len);
56     g_checksum_free(csum);
57
58     memcpy(out, raw_csum, CRYPTO_KEY_SIZE);
59 }
60
61 /* Encrypt a data block. */
62 BlueSkyRCStr *bluesky_crypt_encrypt(BlueSkyRCStr *in, const uint8_t *key)
63 {
64     gcry_error_t status;
65     gcry_cipher_hd_t handle;
66
67     status = gcry_cipher_open(&handle, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CBC,
68                               GCRY_CIPHER_CBC_CTS);
69     if (status) {
70         g_error("gcrypt error setting up encryption: %s\n",
71                 gcry_strerror(status));
72     }
73
74     uint8_t *out = g_malloc0(in->len + CRYPTO_BLOCK_SIZE);
75
76     gcry_cipher_setkey(handle, key, CRYPTO_KEY_SIZE);
77     if (status) {
78         g_error("gcrypt error setting key: %s\n",
79                 gcry_strerror(status));
80     }
81
82     bluesky_crypt_random_bytes(out, CRYPTO_BLOCK_SIZE);
83     status = gcry_cipher_setiv(handle, out, CRYPTO_BLOCK_SIZE);
84     if (status) {
85         g_error("gcrypt error setting IV: %s\n",
86                 gcry_strerror(status));
87     }
88
89     status = gcry_cipher_encrypt(handle, out + CRYPTO_BLOCK_SIZE, in->len,
90                                  in->data, in->len);
91     if (status) {
92         g_error("gcrypt error encrypting: %s\n",
93                 gcry_strerror(status));
94     }
95
96     gcry_cipher_close(handle);
97
98     return bluesky_string_new(out, in->len + CRYPTO_BLOCK_SIZE);
99 }
100
101 /* Decrypt a data block. */
102 BlueSkyRCStr *bluesky_crypt_decrypt(BlueSkyRCStr *in, const uint8_t *key)
103 {
104     gcry_error_t status;
105     gcry_cipher_hd_t handle;
106
107     g_return_val_if_fail(in->len > CRYPTO_BLOCK_SIZE, NULL);
108
109     status = gcry_cipher_open(&handle, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CBC,
110                               GCRY_CIPHER_CBC_CTS);
111     if (status) {
112         g_error("gcrypt error setting up encryption: %s\n",
113                 gcry_strerror(status));
114     }
115
116     uint8_t *out = g_malloc0(in->len - CRYPTO_BLOCK_SIZE);
117
118     gcry_cipher_setkey(handle, key, CRYPTO_KEY_SIZE);
119     if (status) {
120         g_error("gcrypt error setting key: %s\n",
121                 gcry_strerror(status));
122     }
123
124     status = gcry_cipher_setiv(handle, in->data, CRYPTO_BLOCK_SIZE);
125     if (status) {
126         g_error("gcrypt error setting IV: %s\n",
127                 gcry_strerror(status));
128     }
129
130     status = gcry_cipher_decrypt(handle, out, in->len - CRYPTO_BLOCK_SIZE,
131                                  in->data + CRYPTO_BLOCK_SIZE,
132                                  in->len - CRYPTO_BLOCK_SIZE);
133     if (status) {
134         g_error("gcrypt error decrypting: %s\n",
135                 gcry_strerror(status));
136     }
137
138     gcry_cipher_close(handle);
139
140     return bluesky_string_new(out, in->len + CRYPTO_BLOCK_SIZE);
141 }