Work to add inode serialization/deserialization routines.
[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 /* Cryptographic operations.  The rest of the BlueSky code merely calls into
19  * the functions in this file, so this is the only point where we interface
20  * with an external cryptographic library. */
21
22 GCRY_THREAD_OPTION_PTHREAD_IMPL;
23
24 void bluesky_crypt_init()
25 {
26     gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
27
28     if (gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P))
29         return;
30
31     g_print("libgcrypt not yet initialized, initializing...\n");
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     gcry_error_t status;
50     gcry_cipher_hd_t handle;
51
52     status = gcry_cipher_open(&handle, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CBC,
53                               GCRY_CIPHER_CBC_CTS);
54     if (status) {
55         g_error("gcrypt error setting up encryption: %s\n",
56                 gcry_strerror(status));
57     }
58
59     uint8_t *out = g_malloc0(in->len + CRYPTO_BLOCK_SIZE);
60
61     gcry_cipher_setkey(handle, key, CRYPTO_KEY_SIZE);
62     if (status) {
63         g_error("gcrypt error setting key: %s\n",
64                 gcry_strerror(status));
65     }
66
67     bluesky_crypt_random_bytes(out, CRYPTO_BLOCK_SIZE);
68     status = gcry_cipher_setiv(handle, out, CRYPTO_BLOCK_SIZE);
69     if (status) {
70         g_error("gcrypt error setting IV: %s\n",
71                 gcry_strerror(status));
72     }
73
74     status = gcry_cipher_encrypt(handle, out + CRYPTO_BLOCK_SIZE, in->len,
75                                  in->data, in->len);
76     if (status) {
77         g_error("gcrypt error encrypting: %s\n",
78                 gcry_strerror(status));
79     }
80
81     gcry_cipher_close(handle);
82
83     return bluesky_string_new(out, in->len + CRYPTO_BLOCK_SIZE);
84 }
85
86 /* Decrypt a data block. */
87 BlueSkyRCStr *bluesky_crypt_decrypt(BlueSkyRCStr *in, const uint8_t *key)
88 {
89     gcry_error_t status;
90     gcry_cipher_hd_t handle;
91
92     g_return_val_if_fail(in->len > CRYPTO_BLOCK_SIZE, NULL);
93
94     status = gcry_cipher_open(&handle, GCRY_CIPHER_AES, GCRY_CIPHER_MODE_CBC,
95                               GCRY_CIPHER_CBC_CTS);
96     if (status) {
97         g_error("gcrypt error setting up encryption: %s\n",
98                 gcry_strerror(status));
99     }
100
101     uint8_t *out = g_malloc0(in->len - CRYPTO_BLOCK_SIZE);
102
103     gcry_cipher_setkey(handle, key, CRYPTO_KEY_SIZE);
104     if (status) {
105         g_error("gcrypt error setting key: %s\n",
106                 gcry_strerror(status));
107     }
108
109     status = gcry_cipher_setiv(handle, in->data, CRYPTO_BLOCK_SIZE);
110     if (status) {
111         g_error("gcrypt error setting IV: %s\n",
112                 gcry_strerror(status));
113     }
114
115     status = gcry_cipher_decrypt(handle, out, in->len - CRYPTO_BLOCK_SIZE,
116                                  in->data + CRYPTO_BLOCK_SIZE,
117                                  in->len - CRYPTO_BLOCK_SIZE);
118     if (status) {
119         g_error("gcrypt error decrypting: %s\n",
120                 gcry_strerror(status));
121     }
122
123     gcry_cipher_close(handle);
124
125     return bluesky_string_new(out, in->len + CRYPTO_BLOCK_SIZE);
126 }