df0af666b0eef3c5a25d872fd0fee81b5a8df59c
[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 <errno.h>
11 #include <pthread.h>
12 #include <glib.h>
13 #include <string.h>
14 #include <gcrypt.h>
15
16 #include "bluesky.h"
17
18 static int DISABLE_CRYPTO = 1;
19
20 /* Cryptographic operations.  The rest of the BlueSky code merely calls into
21  * the functions in this file, so this is the only point where we interface
22  * with an external cryptographic library. */
23
24 GCRY_THREAD_OPTION_PTHREAD_IMPL;
25
26 void bluesky_crypt_init()
27 {
28     gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
29
30     if (gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P))
31         return;
32
33     g_print("libgcrypt not yet initialized, initializing...\n");
34
35     if (!gcry_check_version(GCRYPT_VERSION))
36         g_error("libgcrypt version mismatch\n");
37
38     gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
39     gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
40 }
41
42 /* Return cryptographically-strong random data. */
43 void bluesky_crypt_random_bytes(guchar *buf, gint len)
44 {
45     gcry_randomize(buf, len, GCRY_STRONG_RANDOM);
46 }
47
48 /* Encrypt a data block. */
49 BlueSkyRCStr *bluesky_crypt_encrypt(BlueSkyRCStr *in, const uint8_t *key)
50 {
51     if (DISABLE_CRYPTO) {
52         bluesky_string_ref(in);
53         return in;
54     }
55
56     gcry_error_t status;
57     gcry_cipher_hd_t handle;
58
59     status = gcry_cipher_open(&handle, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CBC,
60                               GCRY_CIPHER_CBC_CTS);
61     if (status) {
62         g_error("gcrypt error setting up encryption: %s\n",
63                 gcry_strerror(status));
64     }
65
66     uint8_t *out = g_malloc0(in->len + CRYPTO_BLOCK_SIZE);
67
68     gcry_cipher_setkey(handle, key, CRYPTO_KEY_SIZE);
69     if (status) {
70         g_error("gcrypt error setting key: %s\n",
71                 gcry_strerror(status));
72     }
73
74     bluesky_crypt_random_bytes(out, CRYPTO_BLOCK_SIZE);
75     status = gcry_cipher_setiv(handle, out, CRYPTO_BLOCK_SIZE);
76     if (status) {
77         g_error("gcrypt error setting IV: %s\n",
78                 gcry_strerror(status));
79     }
80
81     status = gcry_cipher_encrypt(handle, out + CRYPTO_BLOCK_SIZE, in->len,
82                                  in->data, in->len);
83     if (status) {
84         g_error("gcrypt error encrypting: %s\n",
85                 gcry_strerror(status));
86     }
87
88     gcry_cipher_close(handle);
89
90     return bluesky_string_new(out, in->len + CRYPTO_BLOCK_SIZE);
91 }
92
93 /* Decrypt a data block. */
94 BlueSkyRCStr *bluesky_crypt_decrypt(BlueSkyRCStr *in, const uint8_t *key)
95 {
96     if (DISABLE_CRYPTO) {
97         bluesky_string_ref(in);
98         return in;
99     }
100
101     gcry_error_t status;
102     gcry_cipher_hd_t handle;
103
104     g_return_val_if_fail(in->len > CRYPTO_BLOCK_SIZE, NULL);
105
106     status = gcry_cipher_open(&handle, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CBC,
107                               GCRY_CIPHER_CBC_CTS);
108     if (status) {
109         g_error("gcrypt error setting up encryption: %s\n",
110                 gcry_strerror(status));
111     }
112
113     uint8_t *out = g_malloc0(in->len - CRYPTO_BLOCK_SIZE);
114
115     gcry_cipher_setkey(handle, key, CRYPTO_KEY_SIZE);
116     if (status) {
117         g_error("gcrypt error setting key: %s\n",
118                 gcry_strerror(status));
119     }
120
121     status = gcry_cipher_setiv(handle, in->data, CRYPTO_BLOCK_SIZE);
122     if (status) {
123         g_error("gcrypt error setting IV: %s\n",
124                 gcry_strerror(status));
125     }
126
127     status = gcry_cipher_decrypt(handle, out, in->len - CRYPTO_BLOCK_SIZE,
128                                  in->data + CRYPTO_BLOCK_SIZE,
129                                  in->len - CRYPTO_BLOCK_SIZE);
130     if (status) {
131         g_error("gcrypt error decrypting: %s\n",
132                 gcry_strerror(status));
133     }
134
135     gcry_cipher_close(handle);
136
137     return bluesky_string_new(out, in->len + CRYPTO_BLOCK_SIZE);
138 }