crypto_file.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #include <stddef.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "crypto_aes.h"
  6. #include "crypto_aesctr.h"
  7. #include "crypto_entropy.h"
  8. #include "crypto_verify_bytes.h"
  9. #include "sysendian.h"
  10. #include "warnp.h"
  11. #include "crypto_internal.h"
  12. #include "rwhashtab.h"
  13. #include "crypto.h"
  14. struct crypto_file_aes_key {
  15. struct crypto_aes_key * key; /* Expanded key. */
  16. uint64_t nonce; /* Nonce, used only for encryption. */
  17. uint8_t key_encrypted[256]; /* AES key encrypted with encr_pub. */
  18. };
  19. static struct crypto_file_aes_key * encr_aes;
  20. static RWHASHTAB * decr_aes_cache;
  21. static int keygen(void);
  22. static void crypto_file_atexit(void);
  23. static int callback_free_rwhashtab_aes_key(void * rec, void * cookie);
  24. /**
  25. * crypto_file_init_keys(void):
  26. * Initialize the keys cached by crypto_file.
  27. */
  28. int
  29. crypto_file_init_keys(void)
  30. {
  31. /* We don't have an encryption key. */
  32. encr_aes = NULL;
  33. /* Create encrypted key -> AES key mapping table. */
  34. if ((decr_aes_cache =
  35. rwhashtab_init(offsetof(struct crypto_file_aes_key, key_encrypted),
  36. 256)) == NULL)
  37. goto err0;
  38. if (atexit(crypto_file_atexit)) {
  39. warnp("Could not initialize atexit");
  40. goto err1;
  41. }
  42. /* Success! */
  43. return (0);
  44. err1:
  45. rwhashtab_free(decr_aes_cache);
  46. err0:
  47. /* Failure! */
  48. return (-1);
  49. }
  50. static int
  51. callback_free_rwhashtab_aes_key(void * rec, void * cookie)
  52. {
  53. struct crypto_file_aes_key * key = rec;
  54. (void)cookie; /* UNUSED */
  55. /* Free expanded AES key and encrypted key lookup record. */
  56. crypto_aes_key_free(key->key);
  57. free(key);
  58. /* Success! */
  59. return (0);
  60. }
  61. /**
  62. * crypto_file_atexit(void):
  63. * Free the keys cached by crypto_file.
  64. */
  65. static void
  66. crypto_file_atexit(void)
  67. {
  68. /* Iterate through encrypted key -> AES key table freeing records. */
  69. rwhashtab_foreach(decr_aes_cache, callback_free_rwhashtab_aes_key,
  70. NULL);
  71. /* Free decryption cache. */
  72. rwhashtab_free(decr_aes_cache);
  73. /* Free encryption key, if we have one. */
  74. if (encr_aes != NULL) {
  75. crypto_aes_key_free(encr_aes->key);
  76. free(encr_aes);
  77. }
  78. }
  79. /* Generate encr_aes. */
  80. static int
  81. keygen(void)
  82. {
  83. uint8_t aeskey[32];
  84. /* Allocate memory. */
  85. if ((encr_aes = malloc(sizeof(struct crypto_file_aes_key))) == NULL)
  86. goto err0;
  87. /* Generate random key. */
  88. if (crypto_entropy_read(aeskey, 32))
  89. goto err1;
  90. /* Expand the key. */
  91. if ((encr_aes->key = crypto_aes_key_expand(aeskey, 32)) == NULL)
  92. goto err1;
  93. /* We start with a nonce of zero. */
  94. encr_aes->nonce = 0;
  95. /* RSA encrypt the key. */
  96. if (crypto_rsa_encrypt(CRYPTO_KEY_ENCR_PUB, aeskey, 32,
  97. encr_aes->key_encrypted, 256))
  98. goto err2;
  99. /* Success! */
  100. return (0);
  101. err2:
  102. crypto_aes_key_free(encr_aes->key);
  103. err1:
  104. free(encr_aes);
  105. encr_aes = NULL;
  106. err0:
  107. /* Failure! */
  108. return (-1);
  109. }
  110. /**
  111. * crypto_file_enc(buf, len, filebuf):
  112. * Encrypt the buffer ${buf} of length ${len}, placing the result (including
  113. * encryption header and authentication trailer) into ${filebuf}.
  114. */
  115. int
  116. crypto_file_enc(const uint8_t * buf, size_t len, uint8_t * filebuf)
  117. {
  118. struct crypto_aesctr * stream;
  119. /* If we don't have a session AES key yet, generate one. */
  120. if ((encr_aes == NULL) && keygen())
  121. goto err0;
  122. /* Copy encrypted key into header. */
  123. memcpy(filebuf, encr_aes->key_encrypted, 256);
  124. /* Store nonce. */
  125. be64enc(filebuf + 256, encr_aes->nonce);
  126. /* Encrypt the data. */
  127. if ((stream =
  128. crypto_aesctr_init(encr_aes->key, encr_aes->nonce++)) == NULL)
  129. goto err0;
  130. crypto_aesctr_stream(stream, buf, filebuf + CRYPTO_FILE_HLEN, len);
  131. crypto_aesctr_free(stream);
  132. /* Compute HMAC. */
  133. if (crypto_hash_data(CRYPTO_KEY_HMAC_FILE_WRITE, filebuf,
  134. CRYPTO_FILE_HLEN + len, filebuf + CRYPTO_FILE_HLEN + len))
  135. goto err0;
  136. /* Success! */
  137. return (0);
  138. err0:
  139. /* Failure! */
  140. return (-1);
  141. }
  142. /**
  143. * crypto_file_dec(filebuf, len, buf):
  144. * Decrypt the buffer ${filebuf}, removing the encryption header and
  145. * authentication trailer, and place the result into ${buf} of length ${len}.
  146. */
  147. int
  148. crypto_file_dec(const uint8_t * filebuf, size_t len, uint8_t * buf)
  149. {
  150. uint8_t hash[CRYPTO_FILE_TLEN];
  151. struct crypto_file_aes_key * key;
  152. struct crypto_aesctr * stream;
  153. uint64_t nonce;
  154. /*
  155. * The AES key is 32 bytes, but the buffer is larger in order
  156. * to properly detect and handle bogus encrypted keys (i.e., if
  157. * more than 32 bytes were encrypted).
  158. */
  159. uint8_t aeskey[256];
  160. size_t aeskeybuflen = 256;
  161. /* Compute HMAC. */
  162. if (crypto_hash_data(CRYPTO_KEY_HMAC_FILE,
  163. filebuf, CRYPTO_FILE_HLEN + len, hash))
  164. goto err0;
  165. /* If the HMAC doesn't match, the file was corrupted. */
  166. if (crypto_verify_bytes(hash,
  167. &filebuf[CRYPTO_FILE_HLEN + len], CRYPTO_FILE_TLEN))
  168. goto bad0;
  169. /* Look up key in hash table. */
  170. key = rwhashtab_read(decr_aes_cache, filebuf);
  171. /* If it's not in the hash table, construct it the hard way. */
  172. if (key == NULL) {
  173. /* Allocate memory. */
  174. if ((key = malloc(sizeof(struct crypto_file_aes_key))) == NULL)
  175. goto err0;
  176. /* RSA decrypt key. */
  177. switch (crypto_rsa_decrypt(CRYPTO_KEY_ENCR_PRIV, filebuf,
  178. 256, aeskey, &aeskeybuflen)) {
  179. case -1:
  180. /* Something went wrong. */
  181. goto err1;
  182. case 0:
  183. /* Decrypted. Is the key the correct length? */
  184. if (aeskeybuflen == 32)
  185. break;
  186. /* FALLTHROUGH */
  187. case 1:
  188. /* The ciphertext is corrupt. */
  189. goto bad1;
  190. }
  191. /* Expand the AES key. */
  192. if ((key->key = crypto_aes_key_expand(aeskey, 32)) == NULL)
  193. goto err1;
  194. /* Copy the encrypted AES key. */
  195. memcpy(key->key_encrypted, filebuf, 256);
  196. /* Insert the key into the hash table. */
  197. if (rwhashtab_insert(decr_aes_cache, key)) {
  198. warnp("error inserting key into aes cache");
  199. goto err2;
  200. }
  201. }
  202. /* Read the nonce. */
  203. nonce = be64dec(&filebuf[256]);
  204. /* Decrypt the data. */
  205. if ((stream = crypto_aesctr_init(key->key, nonce)) == NULL)
  206. goto err0;
  207. crypto_aesctr_stream(stream, &filebuf[CRYPTO_FILE_HLEN], buf, len);
  208. crypto_aesctr_free(stream);
  209. /* Success! */
  210. return (0);
  211. bad1:
  212. free(key);
  213. bad0:
  214. /* File is not authentic. */
  215. return (1);
  216. err2:
  217. crypto_aes_key_free(key->key);
  218. err1:
  219. free(key);
  220. err0:
  221. /* Failure! */
  222. return (-1);
  223. }