Miscellaneous fixes.
[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     if (!gcry_check_version(GCRYPT_VERSION))
34         g_error("libgcrypt version mismatch\n");
35
36     gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
37     gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
38 }
39
40 /* Return cryptographically-strong random data. */
41 void bluesky_crypt_random_bytes(guchar *buf, gint len)
42 {
43     gcry_randomize(buf, len, GCRY_STRONG_RANDOM);
44 }
45
46 /* Encrypt a data block. */
47 BlueSkyRCStr *bluesky_crypt_encrypt(BlueSkyRCStr *in, const uint8_t *key)
48 {
49     if (DISABLE_CRYPTO) {
50         bluesky_string_ref(in);
51         return in;
52     }
53
54     gcry_error_t status;
55     gcry_cipher_hd_t handle;
56
57     status = gcry_cipher_open(&handle, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CBC,
58                               GCRY_CIPHER_CBC_CTS);
59     if (status) {
60         g_error("gcrypt error setting up encryption: %s\n",
61                 gcry_strerror(status));
62     }
63
64     uint8_t *out = g_malloc0(in->len + CRYPTO_BLOCK_SIZE);
65
66     gcry_cipher_setkey(handle, key, CRYPTO_KEY_SIZE);
67     if (status) {
68         g_error("gcrypt error setting key: %s\n",
69                 gcry_strerror(status));
70     }
71
72     bluesky_crypt_random_bytes(out, CRYPTO_BLOCK_SIZE);
73     status = gcry_cipher_setiv(handle, out, CRYPTO_BLOCK_SIZE);
74     if (status) {
75         g_error("gcrypt error setting IV: %s\n",
76                 gcry_strerror(status));
77     }
78
79     status = gcry_cipher_encrypt(handle, out + CRYPTO_BLOCK_SIZE, in->len,
80                                  in->data, in->len);
81     if (status) {
82         g_error("gcrypt error encrypting: %s\n",
83                 gcry_strerror(status));
84     }
85
86     gcry_cipher_close(handle);
87
88     return bluesky_string_new(out, in->len + CRYPTO_BLOCK_SIZE);
89 }
90
91 /* Decrypt a data block. */
92 BlueSkyRCStr *bluesky_crypt_decrypt(BlueSkyRCStr *in, const uint8_t *key)
93 {
94     if (DISABLE_CRYPTO) {
95         bluesky_string_ref(in);
96         return in;
97     }
98
99     gcry_error_t status;
100     gcry_cipher_hd_t handle;
101
102     g_return_val_if_fail(in->len > CRYPTO_BLOCK_SIZE, NULL);
103
104     status = gcry_cipher_open(&handle, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CBC,
105                               GCRY_CIPHER_CBC_CTS);
106     if (status) {
107         g_error("gcrypt error setting up encryption: %s\n",
108                 gcry_strerror(status));
109     }
110
111     uint8_t *out = g_malloc0(in->len - CRYPTO_BLOCK_SIZE);
112
113     gcry_cipher_setkey(handle, key, CRYPTO_KEY_SIZE);
114     if (status) {
115         g_error("gcrypt error setting key: %s\n",
116                 gcry_strerror(status));
117     }
118
119     status = gcry_cipher_setiv(handle, in->data, CRYPTO_BLOCK_SIZE);
120     if (status) {
121         g_error("gcrypt error setting IV: %s\n",
122                 gcry_strerror(status));
123     }
124
125     status = gcry_cipher_decrypt(handle, out, in->len - CRYPTO_BLOCK_SIZE,
126                                  in->data + CRYPTO_BLOCK_SIZE,
127                                  in->len - CRYPTO_BLOCK_SIZE);
128     if (status) {
129         g_error("gcrypt error decrypting: %s\n",
130                 gcry_strerror(status));
131     }
132
133     gcry_cipher_close(handle);
134
135     return bluesky_string_new(out, in->len + CRYPTO_BLOCK_SIZE);
136 }