aes.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 <openssl/aes.h>
  16. #include <openssl/evp.h>
  17. #include <openssl/engine.h>
  18. #if OPENSSL_VERSION_NUMBER >= 0x1000001fL
  19. /* See comments about which counter mode implementation to use below. */
  20. #include <openssl/modes.h>
  21. #define USE_OPENSSL_CTR
  22. #endif
  23. #include "compat.h"
  24. #include "aes.h"
  25. #include "util.h"
  26. #include "torlog.h"
  27. #ifdef ANDROID
  28. /* Android's OpenSSL seems to have removed all of its Engine support. */
  29. #define DISABLE_ENGINES
  30. #endif
  31. /* We have 2 strategies for getting AES: Via OpenSSL's AES_encrypt function,
  32. * via OpenSSL's EVP_EncryptUpdate function.
  33. *
  34. * If there's any hardware acceleration in play, we want to be using EVP_* so
  35. * we can get it. Otherwise, we'll want AES_*, which seems to be about 5%
  36. * faster than indirecting through the EVP layer.
  37. */
  38. /* We have 2 strategies for counter mode: use our own, or use OpenSSL's.
  39. *
  40. * Here we have a counter mode that's faster than the one shipping with
  41. * OpenSSL pre-1.0 (by about 10%!). But OpenSSL 1.0.0 added a counter mode
  42. * implementation faster than the one here (by about 7%). So we pick which
  43. * one to used based on the Openssl version above. (OpenSSL 1.0.0a fixed a
  44. * critical bug in that counter mode implementation, so we actually require
  45. * that one.)
  46. */
  47. /*======================================================================*/
  48. /* Interface to AES code, and counter implementation */
  49. /** Implements an AES counter-mode cipher. */
  50. struct aes_cnt_cipher {
  51. /** This next element (however it's defined) is the AES key. */
  52. union {
  53. EVP_CIPHER_CTX evp;
  54. AES_KEY aes;
  55. } key;
  56. #if !defined(WORDS_BIGENDIAN) && !defined(USE_OPENSSL_CTR)
  57. #define USING_COUNTER_VARS
  58. /** These four values, together, implement a 128-bit counter, with
  59. * counter0 as the low-order word and counter3 as the high-order word. */
  60. uint32_t counter3;
  61. uint32_t counter2;
  62. uint32_t counter1;
  63. uint32_t counter0;
  64. #endif
  65. union {
  66. /** The counter, in big-endian order, as bytes. */
  67. uint8_t buf[16];
  68. /** The counter, in big-endian order, as big-endian words. Note that
  69. * on big-endian platforms, this is redundant with counter3...0,
  70. * so we just use these values instead. */
  71. uint32_t buf32[4];
  72. } ctr_buf;
  73. /** The encrypted value of ctr_buf. */
  74. uint8_t buf[16];
  75. /** Our current stream position within buf. */
  76. #ifdef USE_OPENSSL_CTR
  77. unsigned int pos;
  78. #else
  79. uint8_t pos;
  80. #endif
  81. /** True iff we're using the evp implementation of this cipher. */
  82. uint8_t using_evp;
  83. };
  84. /** True if we should prefer the EVP implementation for AES, either because
  85. * we're testing it or because we have hardware acceleration configured */
  86. static int should_use_EVP = 0;
  87. /** Check whether we should use the EVP interface for AES. If <b>force_val</b>
  88. * is nonnegative, we use use EVP iff it is true. Otherwise, we use EVP
  89. * if there is an engine enabled for aes-ecb. */
  90. int
  91. evaluate_evp_for_aes(int force_val)
  92. {
  93. ENGINE *e;
  94. if (force_val >= 0) {
  95. should_use_EVP = force_val;
  96. return 0;
  97. }
  98. #ifdef DISABLE_ENGINES
  99. should_use_EVP = 0;
  100. #else
  101. e = ENGINE_get_cipher_engine(NID_aes_128_ecb);
  102. if (e) {
  103. log_notice(LD_CRYPTO, "AES engine \"%s\" found; using EVP_* functions.",
  104. ENGINE_get_name(e));
  105. should_use_EVP = 1;
  106. } else {
  107. log_notice(LD_CRYPTO, "No AES engine found; using AES_* functions.");
  108. should_use_EVP = 0;
  109. }
  110. #endif
  111. return 0;
  112. }
  113. #ifndef USE_OPENSSL_CTR
  114. #if !defined(USING_COUNTER_VARS)
  115. #define COUNTER(c, n) ((c)->ctr_buf.buf32[3-(n)])
  116. #else
  117. #define COUNTER(c, n) ((c)->counter ## n)
  118. #endif
  119. /**
  120. * Helper function: set <b>cipher</b>'s internal buffer to the encrypted
  121. * value of the current counter.
  122. */
  123. static INLINE void
  124. _aes_fill_buf(aes_cnt_cipher_t *cipher)
  125. {
  126. /* We don't currently use OpenSSL's counter mode implementation because:
  127. * 1) some versions have known bugs
  128. * 2) its attitude towards IVs is not our own
  129. * 3) changing the counter position was not trivial, last time I looked.
  130. * None of these issues are insurmountable in principle.
  131. */
  132. if (cipher->using_evp) {
  133. int outl=16, inl=16;
  134. EVP_EncryptUpdate(&cipher->key.evp, cipher->buf, &outl,
  135. cipher->ctr_buf.buf, inl);
  136. } else {
  137. AES_encrypt(cipher->ctr_buf.buf, cipher->buf, &cipher->key.aes);
  138. }
  139. }
  140. #endif
  141. /**
  142. * Return a newly allocated counter-mode AES128 cipher implementation.
  143. */
  144. aes_cnt_cipher_t*
  145. aes_new_cipher(void)
  146. {
  147. aes_cnt_cipher_t* result = tor_malloc_zero(sizeof(aes_cnt_cipher_t));
  148. return result;
  149. }
  150. /** Set the key of <b>cipher</b> to <b>key</b>, which is
  151. * <b>key_bits</b> bits long (must be 128, 192, or 256). Also resets
  152. * the counter to 0.
  153. */
  154. void
  155. aes_set_key(aes_cnt_cipher_t *cipher, const char *key, int key_bits)
  156. {
  157. if (should_use_EVP) {
  158. const EVP_CIPHER *c;
  159. switch (key_bits) {
  160. case 128: c = EVP_aes_128_ecb(); break;
  161. case 192: c = EVP_aes_192_ecb(); break;
  162. case 256: c = EVP_aes_256_ecb(); break;
  163. default: tor_assert(0);
  164. }
  165. EVP_EncryptInit(&cipher->key.evp, c, (const unsigned char*)key, NULL);
  166. cipher->using_evp = 1;
  167. } else {
  168. AES_set_encrypt_key((const unsigned char *)key, key_bits, &cipher->key.aes);
  169. cipher->using_evp = 0;
  170. }
  171. #ifdef USING_COUNTER_VARS
  172. cipher->counter0 = 0;
  173. cipher->counter1 = 0;
  174. cipher->counter2 = 0;
  175. cipher->counter3 = 0;
  176. #endif
  177. memset(cipher->ctr_buf.buf, 0, sizeof(cipher->ctr_buf.buf));
  178. cipher->pos = 0;
  179. #ifdef USE_OPENSSL_CTR
  180. memset(cipher->buf, 0, sizeof(cipher->buf));
  181. #else
  182. _aes_fill_buf(cipher);
  183. #endif
  184. }
  185. /** Release storage held by <b>cipher</b>
  186. */
  187. void
  188. aes_free_cipher(aes_cnt_cipher_t *cipher)
  189. {
  190. if (!cipher)
  191. return;
  192. if (cipher->using_evp) {
  193. EVP_CIPHER_CTX_cleanup(&cipher->key.evp);
  194. }
  195. memset(cipher, 0, sizeof(aes_cnt_cipher_t));
  196. tor_free(cipher);
  197. }
  198. #if defined(USING_COUNTER_VARS)
  199. #define UPDATE_CTR_BUF(c, n) STMT_BEGIN \
  200. (c)->ctr_buf.buf32[3-(n)] = htonl((c)->counter ## n); \
  201. STMT_END
  202. #else
  203. #define UPDATE_CTR_BUF(c, n)
  204. #endif
  205. #ifdef USE_OPENSSL_CTR
  206. /* Helper function to use EVP with openssl's counter-mode wrapper. */
  207. static void evp_block128_fn(const uint8_t in[16],
  208. uint8_t out[16],
  209. const void *key)
  210. {
  211. EVP_CIPHER_CTX *ctx = (void*)key;
  212. int inl=16, outl=16;
  213. EVP_EncryptUpdate(ctx, out, &outl, in, inl);
  214. }
  215. #endif
  216. /** Encrypt <b>len</b> bytes from <b>input</b>, storing the result in
  217. * <b>output</b>. Uses the key in <b>cipher</b>, and advances the counter
  218. * by <b>len</b> bytes as it encrypts.
  219. */
  220. void
  221. aes_crypt(aes_cnt_cipher_t *cipher, const char *input, size_t len,
  222. char *output)
  223. {
  224. #ifdef USE_OPENSSL_CTR
  225. if (cipher->using_evp) {
  226. /* In openssl 1.0.0, there's an if'd out EVP_aes_128_ctr in evp.h. If
  227. * it weren't disabled, it might be better just to use that.
  228. */
  229. CRYPTO_ctr128_encrypt((const unsigned char *)input,
  230. (unsigned char *)output,
  231. len,
  232. &cipher->key.evp,
  233. cipher->ctr_buf.buf,
  234. cipher->buf,
  235. &cipher->pos,
  236. evp_block128_fn);
  237. } else {
  238. AES_ctr128_encrypt((const unsigned char *)input,
  239. (unsigned char *)output,
  240. len,
  241. &cipher->key.aes,
  242. cipher->ctr_buf.buf,
  243. cipher->buf,
  244. &cipher->pos);
  245. }
  246. #else
  247. int c = cipher->pos;
  248. if (PREDICT_UNLIKELY(!len)) return;
  249. while (1) {
  250. do {
  251. if (len-- == 0) { cipher->pos = c; return; }
  252. *(output++) = *(input++) ^ cipher->buf[c];
  253. } while (++c != 16);
  254. cipher->pos = c = 0;
  255. if (PREDICT_UNLIKELY(! ++COUNTER(cipher, 0))) {
  256. if (PREDICT_UNLIKELY(! ++COUNTER(cipher, 1))) {
  257. if (PREDICT_UNLIKELY(! ++COUNTER(cipher, 2))) {
  258. ++COUNTER(cipher, 3);
  259. UPDATE_CTR_BUF(cipher, 3);
  260. }
  261. UPDATE_CTR_BUF(cipher, 2);
  262. }
  263. UPDATE_CTR_BUF(cipher, 1);
  264. }
  265. UPDATE_CTR_BUF(cipher, 0);
  266. _aes_fill_buf(cipher);
  267. }
  268. #endif
  269. }
  270. /** Encrypt <b>len</b> bytes from <b>input</b>, storing the results in place.
  271. * Uses the key in <b>cipher</b>, and advances the counter by <b>len</b> bytes
  272. * as it encrypts.
  273. */
  274. void
  275. aes_crypt_inplace(aes_cnt_cipher_t *cipher, char *data, size_t len)
  276. {
  277. #ifdef USE_OPENSSL_CTR
  278. aes_crypt(cipher, data, len, data);
  279. #else
  280. int c = cipher->pos;
  281. if (PREDICT_UNLIKELY(!len)) return;
  282. while (1) {
  283. do {
  284. if (len-- == 0) { cipher->pos = c; return; }
  285. *(data++) ^= cipher->buf[c];
  286. } while (++c != 16);
  287. cipher->pos = c = 0;
  288. if (PREDICT_UNLIKELY(! ++COUNTER(cipher, 0))) {
  289. if (PREDICT_UNLIKELY(! ++COUNTER(cipher, 1))) {
  290. if (PREDICT_UNLIKELY(! ++COUNTER(cipher, 2))) {
  291. ++COUNTER(cipher, 3);
  292. UPDATE_CTR_BUF(cipher, 3);
  293. }
  294. UPDATE_CTR_BUF(cipher, 2);
  295. }
  296. UPDATE_CTR_BUF(cipher, 1);
  297. }
  298. UPDATE_CTR_BUF(cipher, 0);
  299. _aes_fill_buf(cipher);
  300. }
  301. #endif
  302. }
  303. /** Reset the 128-bit counter of <b>cipher</b> to the 16-bit big-endian value
  304. * in <b>iv</b>. */
  305. void
  306. aes_set_iv(aes_cnt_cipher_t *cipher, const char *iv)
  307. {
  308. #ifdef USING_COUNTER_VARS
  309. cipher->counter3 = ntohl(get_uint32(iv));
  310. cipher->counter2 = ntohl(get_uint32(iv+4));
  311. cipher->counter1 = ntohl(get_uint32(iv+8));
  312. cipher->counter0 = ntohl(get_uint32(iv+12));
  313. #endif
  314. cipher->pos = 0;
  315. memcpy(cipher->ctr_buf.buf, iv, 16);
  316. #ifndef USE_OPENSSL_CTR
  317. _aes_fill_buf(cipher);
  318. #endif
  319. }