aes.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* Copyright (c) 2001, Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2011, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file aes.c
  8. * \brief Implements a counter-mode stream cipher on top of AES.
  9. **/
  10. #include "orconfig.h"
  11. #include <openssl/opensslv.h>
  12. #include <assert.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "compat.h"
  16. #include "aes.h"
  17. #include "util.h"
  18. #include "torlog.h"
  19. /* We have 2 strategies for getting AES: Via OpenSSL's AES_encrypt function,
  20. * via OpenSSL's EVP_EncryptUpdate function. */
  21. /** Defined iff we're using OpenSSL's AES functions for AES. */
  22. #undef USE_OPENSSL_AES
  23. /** Defined iff we're using OpenSSL's EVP code for AES. */
  24. #undef USE_OPENSSL_EVP
  25. /* Here we pick which to use, if none is force-defined above */
  26. #if (!defined(USE_OPENSSL_AES) && \
  27. !defined(USE_OPENSSL_EVP))
  28. #define USE_OPENSSL_EVP
  29. #endif
  30. /* Include OpenSSL headers as needed. */
  31. #ifdef USE_OPENSSL_AES
  32. # include <openssl/aes.h>
  33. #endif
  34. #ifdef USE_OPENSSL_EVP
  35. # include <openssl/evp.h>
  36. #endif
  37. /*======================================================================*/
  38. /* Interface to AES code, and counter implementation */
  39. /** Implements an AES counter-mode cipher. */
  40. struct aes_cnt_cipher {
  41. /** This next element (however it's defined) is the AES key. */
  42. #if defined(USE_OPENSSL_EVP)
  43. EVP_CIPHER_CTX key;
  44. #elif defined(USE_OPENSSL_AES)
  45. AES_KEY key;
  46. #endif
  47. #if !defined(WORDS_BIGENDIAN)
  48. #define USING_COUNTER_VARS
  49. /** These four values, together, implement a 128-bit counter, with
  50. * counter0 as the low-order word and counter3 as the high-order word. */
  51. uint32_t counter3;
  52. uint32_t counter2;
  53. uint32_t counter1;
  54. uint32_t counter0;
  55. #endif
  56. union {
  57. /** The counter, in big-endian order, as bytes. */
  58. uint8_t buf[16];
  59. /** The counter, in big-endian order, as big-endian words. Note that
  60. * on big-endian platforms, this is redundant with counter3...0,
  61. * so we just use these values instead. */
  62. uint32_t buf32[4];
  63. } ctr_buf;
  64. /** The encrypted value of ctr_buf. */
  65. uint8_t buf[16];
  66. /** Our current stream position within buf. */
  67. uint8_t pos;
  68. };
  69. #if !defined(USING_COUNTER_VARS)
  70. #define COUNTER(c, n) ((c)->ctr_buf.buf32[3-(n)])
  71. #else
  72. #define COUNTER(c, n) ((c)->counter ## n)
  73. #endif
  74. /**
  75. * Helper function: set <b>cipher</b>'s internal buffer to the encrypted
  76. * value of the current counter.
  77. */
  78. static INLINE void
  79. _aes_fill_buf(aes_cnt_cipher_t *cipher)
  80. {
  81. /* We don't currently use OpenSSL's counter mode implementation because:
  82. * 1) some versions have known bugs
  83. * 2) its attitude towards IVs is not our own
  84. * 3) changing the counter position was not trivial, last time I looked.
  85. * None of these issues are insurmountable in principle.
  86. */
  87. #if defined(USE_OPENSSL_EVP)
  88. {
  89. int outl=16, inl=16;
  90. EVP_EncryptUpdate(&cipher->key, cipher->buf, &outl,
  91. cipher->ctr_buf.buf, inl);
  92. }
  93. #elif defined(USE_OPENSSL_AES)
  94. AES_encrypt(cipher->ctr_buf.buf, cipher->buf, &cipher->key);
  95. #endif
  96. }
  97. /**
  98. * Return a newly allocated counter-mode AES128 cipher implementation.
  99. */
  100. aes_cnt_cipher_t*
  101. aes_new_cipher(void)
  102. {
  103. aes_cnt_cipher_t* result = tor_malloc_zero(sizeof(aes_cnt_cipher_t));
  104. return result;
  105. }
  106. /** Set the key of <b>cipher</b> to <b>key</b>, which is
  107. * <b>key_bits</b> bits long (must be 128, 192, or 256). Also resets
  108. * the counter to 0.
  109. */
  110. void
  111. aes_set_key(aes_cnt_cipher_t *cipher, const char *key, int key_bits)
  112. {
  113. #if defined(USE_OPENSSL_EVP)
  114. const EVP_CIPHER *c;
  115. switch (key_bits) {
  116. case 128: c = EVP_aes_128_ecb(); break;
  117. case 192: c = EVP_aes_192_ecb(); break;
  118. case 256: c = EVP_aes_256_ecb(); break;
  119. default: tor_assert(0);
  120. }
  121. EVP_EncryptInit(&cipher->key, c, (const unsigned char*)key, NULL);
  122. #elif defined(USE_OPENSSL_AES)
  123. AES_set_encrypt_key((const unsigned char *)key, key_bits, &(cipher->key));
  124. #endif
  125. #ifdef USING_COUNTER_VARS
  126. cipher->counter0 = 0;
  127. cipher->counter1 = 0;
  128. cipher->counter2 = 0;
  129. cipher->counter3 = 0;
  130. #endif
  131. memset(cipher->ctr_buf.buf, 0, sizeof(cipher->ctr_buf.buf));
  132. cipher->pos = 0;
  133. _aes_fill_buf(cipher);
  134. }
  135. /** Release storage held by <b>cipher</b>
  136. */
  137. void
  138. aes_free_cipher(aes_cnt_cipher_t *cipher)
  139. {
  140. if (!cipher)
  141. return;
  142. #ifdef USE_OPENSSL_EVP
  143. EVP_CIPHER_CTX_cleanup(&cipher->key);
  144. #endif
  145. memset(cipher, 0, sizeof(aes_cnt_cipher_t));
  146. tor_free(cipher);
  147. }
  148. #if defined(USING_COUNTER_VARS)
  149. #define UPDATE_CTR_BUF(c, n) STMT_BEGIN \
  150. (c)->ctr_buf.buf32[3-(n)] = htonl((c)->counter ## n); \
  151. STMT_END
  152. #else
  153. #define UPDATE_CTR_BUF(c, n)
  154. #endif
  155. /** Encrypt <b>len</b> bytes from <b>input</b>, storing the result in
  156. * <b>output</b>. Uses the key in <b>cipher</b>, and advances the counter
  157. * by <b>len</b> bytes as it encrypts.
  158. */
  159. void
  160. aes_crypt(aes_cnt_cipher_t *cipher, const char *input, size_t len,
  161. char *output)
  162. {
  163. /* This function alone is up to 5% of our runtime in some profiles; anything
  164. * we could do to make it faster would be great.
  165. *
  166. * Experimenting suggests that unrolling the inner loop into a switch
  167. * statement doesn't help. What does seem to help is making the input and
  168. * output buffers word aligned, and never crypting anything besides an
  169. * integer number of words at a time -- it shaves maybe 4-5% of the per-byte
  170. * encryption time measured by bench_aes. We can't do that with the current
  171. * Tor protocol, though: Tor really likes to crypt things in 509-byte
  172. * chunks.
  173. *
  174. * If we were really ambitous, we'd force len to be a multiple of the block
  175. * size, and shave maybe another 4-5% off.
  176. */
  177. int c = cipher->pos;
  178. if (PREDICT_UNLIKELY(!len)) return;
  179. while (1) {
  180. do {
  181. if (len-- == 0) { cipher->pos = c; return; }
  182. *(output++) = *(input++) ^ cipher->buf[c];
  183. } while (++c != 16);
  184. cipher->pos = c = 0;
  185. if (PREDICT_UNLIKELY(! ++COUNTER(cipher, 0))) {
  186. if (PREDICT_UNLIKELY(! ++COUNTER(cipher, 1))) {
  187. if (PREDICT_UNLIKELY(! ++COUNTER(cipher, 2))) {
  188. ++COUNTER(cipher, 3);
  189. UPDATE_CTR_BUF(cipher, 3);
  190. }
  191. UPDATE_CTR_BUF(cipher, 2);
  192. }
  193. UPDATE_CTR_BUF(cipher, 1);
  194. }
  195. UPDATE_CTR_BUF(cipher, 0);
  196. _aes_fill_buf(cipher);
  197. }
  198. }
  199. /** Encrypt <b>len</b> bytes from <b>input</b>, storing the results in place.
  200. * Uses the key in <b>cipher</b>, and advances the counter by <b>len</b> bytes
  201. * as it encrypts.
  202. */
  203. void
  204. aes_crypt_inplace(aes_cnt_cipher_t *cipher, char *data, size_t len)
  205. {
  206. /* XXXX This function is up to 5% of our runtime in some profiles;
  207. * we should look into unrolling some of the loops; taking advantage
  208. * of alignment, using a bigger buffer, and so on. Not till after 0.1.2.x,
  209. * though. */
  210. int c = cipher->pos;
  211. if (PREDICT_UNLIKELY(!len)) return;
  212. while (1) {
  213. do {
  214. if (len-- == 0) { cipher->pos = c; return; }
  215. *(data++) ^= cipher->buf[c];
  216. } while (++c != 16);
  217. cipher->pos = c = 0;
  218. if (PREDICT_UNLIKELY(! ++COUNTER(cipher, 0))) {
  219. if (PREDICT_UNLIKELY(! ++COUNTER(cipher, 1))) {
  220. if (PREDICT_UNLIKELY(! ++COUNTER(cipher, 2))) {
  221. ++COUNTER(cipher, 3);
  222. UPDATE_CTR_BUF(cipher, 3);
  223. }
  224. UPDATE_CTR_BUF(cipher, 2);
  225. }
  226. UPDATE_CTR_BUF(cipher, 1);
  227. }
  228. UPDATE_CTR_BUF(cipher, 0);
  229. _aes_fill_buf(cipher);
  230. }
  231. }
  232. /** Reset the 128-bit counter of <b>cipher</b> to the 16-bit big-endian value
  233. * in <b>iv</b>. */
  234. void
  235. aes_set_iv(aes_cnt_cipher_t *cipher, const char *iv)
  236. {
  237. #ifdef USING_COUNTER_VARS
  238. cipher->counter3 = ntohl(get_uint32(iv));
  239. cipher->counter2 = ntohl(get_uint32(iv+4));
  240. cipher->counter1 = ntohl(get_uint32(iv+8));
  241. cipher->counter0 = ntohl(get_uint32(iv+12));
  242. #endif
  243. cipher->pos = 0;
  244. memcpy(cipher->ctr_buf.buf, iv, 16);
  245. _aes_fill_buf(cipher);
  246. }