aes.c 9.7 KB

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