e_aes.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767
  1. /* ====================================================================
  2. * Copyright (c) 2001-2011 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@openssl.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ==================================================================== */
  48. #include <string.h>
  49. #include <openssl/aead.h>
  50. #include <openssl/aes.h>
  51. #include <openssl/cipher.h>
  52. #include <openssl/cpu.h>
  53. #include <openssl/err.h>
  54. #include <openssl/mem.h>
  55. #include <openssl/obj.h>
  56. #include <openssl/rand.h>
  57. #include <openssl/sha.h>
  58. #include "internal.h"
  59. #include "../internal.h"
  60. #include "../modes/internal.h"
  61. #if defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
  62. #include <openssl/arm_arch.h>
  63. #endif
  64. typedef struct {
  65. union {
  66. double align;
  67. AES_KEY ks;
  68. } ks;
  69. block128_f block;
  70. union {
  71. cbc128_f cbc;
  72. ctr128_f ctr;
  73. } stream;
  74. } EVP_AES_KEY;
  75. typedef struct {
  76. union {
  77. double align;
  78. AES_KEY ks;
  79. } ks; /* AES key schedule to use */
  80. int key_set; /* Set if key initialised */
  81. int iv_set; /* Set if an iv is set */
  82. GCM128_CONTEXT gcm;
  83. uint8_t *iv; /* Temporary IV store */
  84. int ivlen; /* IV length */
  85. int taglen;
  86. int iv_gen; /* It is OK to generate IVs */
  87. ctr128_f ctr;
  88. } EVP_AES_GCM_CTX;
  89. #if !defined(OPENSSL_NO_ASM) && \
  90. (defined(OPENSSL_X86_64) || defined(OPENSSL_X86))
  91. #define VPAES
  92. static char vpaes_capable(void) {
  93. return (OPENSSL_ia32cap_P[1] & (1 << (41 - 32))) != 0;
  94. }
  95. #if defined(OPENSSL_X86_64)
  96. #define BSAES
  97. static char bsaes_capable(void) {
  98. return vpaes_capable();
  99. }
  100. #endif
  101. #elif !defined(OPENSSL_NO_ASM) && \
  102. (defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64))
  103. #if defined(OPENSSL_ARM) && __ARM_MAX_ARCH__ >= 7
  104. #define BSAES
  105. static char bsaes_capable(void) {
  106. return CRYPTO_is_NEON_capable();
  107. }
  108. #endif
  109. #define HWAES
  110. static int hwaes_capable(void) {
  111. return CRYPTO_is_ARMv8_AES_capable();
  112. }
  113. int aes_v8_set_encrypt_key(const uint8_t *user_key, const int bits,
  114. AES_KEY *key);
  115. int aes_v8_set_decrypt_key(const uint8_t *user_key, const int bits,
  116. AES_KEY *key);
  117. void aes_v8_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
  118. void aes_v8_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
  119. void aes_v8_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
  120. const AES_KEY *key, uint8_t *ivec, const int enc);
  121. void aes_v8_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t len,
  122. const AES_KEY *key, const uint8_t ivec[16]);
  123. #endif /* OPENSSL_ARM */
  124. #if defined(BSAES)
  125. /* On platforms where BSAES gets defined (just above), then these functions are
  126. * provided by asm. */
  127. void bsaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
  128. const AES_KEY *key, uint8_t ivec[16], int enc);
  129. void bsaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t len,
  130. const AES_KEY *key, const uint8_t ivec[16]);
  131. #else
  132. static char bsaes_capable(void) {
  133. return 0;
  134. }
  135. /* On other platforms, bsaes_capable() will always return false and so the
  136. * following will never be called. */
  137. static void bsaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
  138. const AES_KEY *key, uint8_t ivec[16], int enc) {
  139. abort();
  140. }
  141. static void bsaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out,
  142. size_t len, const AES_KEY *key,
  143. const uint8_t ivec[16]) {
  144. abort();
  145. }
  146. #endif
  147. #if defined(VPAES)
  148. /* On platforms where VPAES gets defined (just above), then these functions are
  149. * provided by asm. */
  150. int vpaes_set_encrypt_key(const uint8_t *userKey, int bits, AES_KEY *key);
  151. int vpaes_set_decrypt_key(const uint8_t *userKey, int bits, AES_KEY *key);
  152. void vpaes_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
  153. void vpaes_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
  154. void vpaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
  155. const AES_KEY *key, uint8_t *ivec, int enc);
  156. #else
  157. static char vpaes_capable(void) {
  158. return 0;
  159. }
  160. /* On other platforms, vpaes_capable() will always return false and so the
  161. * following will never be called. */
  162. static int vpaes_set_encrypt_key(const uint8_t *userKey, int bits,
  163. AES_KEY *key) {
  164. abort();
  165. }
  166. static int vpaes_set_decrypt_key(const uint8_t *userKey, int bits,
  167. AES_KEY *key) {
  168. abort();
  169. }
  170. static void vpaes_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key) {
  171. abort();
  172. }
  173. static void vpaes_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key) {
  174. abort();
  175. }
  176. static void vpaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
  177. const AES_KEY *key, uint8_t *ivec, int enc) {
  178. abort();
  179. }
  180. #endif
  181. #if !defined(HWAES)
  182. /* If HWAES isn't defined then we provide dummy functions for each of the hwaes
  183. * functions. */
  184. static int hwaes_capable(void) {
  185. return 0;
  186. }
  187. static int aes_v8_set_encrypt_key(const uint8_t *user_key, int bits,
  188. AES_KEY *key) {
  189. abort();
  190. }
  191. static int aes_v8_set_decrypt_key(const uint8_t *user_key, int bits,
  192. AES_KEY *key) {
  193. abort();
  194. }
  195. static void aes_v8_encrypt(const uint8_t *in, uint8_t *out,
  196. const AES_KEY *key) {
  197. abort();
  198. }
  199. static void aes_v8_decrypt(const uint8_t *in, uint8_t *out,
  200. const AES_KEY *key) {
  201. abort();
  202. }
  203. static void aes_v8_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
  204. const AES_KEY *key, uint8_t *ivec, int enc) {
  205. abort();
  206. }
  207. static void aes_v8_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out,
  208. size_t len, const AES_KEY *key,
  209. const uint8_t ivec[16]) {
  210. abort();
  211. }
  212. #endif
  213. #if !defined(OPENSSL_NO_ASM) && \
  214. (defined(OPENSSL_X86_64) || defined(OPENSSL_X86))
  215. int aesni_set_encrypt_key(const uint8_t *userKey, int bits, AES_KEY *key);
  216. int aesni_set_decrypt_key(const uint8_t *userKey, int bits, AES_KEY *key);
  217. void aesni_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
  218. void aesni_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
  219. void aesni_ecb_encrypt(const uint8_t *in, uint8_t *out, size_t length,
  220. const AES_KEY *key, int enc);
  221. void aesni_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
  222. const AES_KEY *key, uint8_t *ivec, int enc);
  223. void aesni_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t blocks,
  224. const void *key, const uint8_t *ivec);
  225. #if defined(OPENSSL_X86_64)
  226. size_t aesni_gcm_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  227. const void *key, uint8_t ivec[16], uint64_t *Xi);
  228. #define AES_gcm_encrypt aesni_gcm_encrypt
  229. size_t aesni_gcm_decrypt(const uint8_t *in, uint8_t *out, size_t len,
  230. const void *key, uint8_t ivec[16], uint64_t *Xi);
  231. #define AES_gcm_decrypt aesni_gcm_decrypt
  232. void gcm_ghash_avx(uint64_t Xi[2], const u128 Htable[16], const uint8_t *in,
  233. size_t len);
  234. #define AES_GCM_ASM(gctx) \
  235. (gctx->ctr == aesni_ctr32_encrypt_blocks && gctx->gcm.ghash == gcm_ghash_avx)
  236. #endif /* OPENSSL_X86_64 */
  237. #else
  238. /* On other platforms, aesni_capable() will always return false and so the
  239. * following will never be called. */
  240. static void aesni_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key) {
  241. abort();
  242. }
  243. static int aesni_set_encrypt_key(const uint8_t *userKey, int bits,
  244. AES_KEY *key) {
  245. abort();
  246. }
  247. static void aesni_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out,
  248. size_t blocks, const void *key,
  249. const uint8_t *ivec) {
  250. abort();
  251. }
  252. #endif
  253. static int aes_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
  254. const uint8_t *iv, int enc)
  255. OPENSSL_SUPPRESS_UNREACHABLE_CODE_WARNINGS {
  256. int ret, mode;
  257. EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
  258. mode = ctx->cipher->flags & EVP_CIPH_MODE_MASK;
  259. if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && !enc) {
  260. if (hwaes_capable()) {
  261. ret = aes_v8_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
  262. dat->block = (block128_f)aes_v8_decrypt;
  263. dat->stream.cbc = NULL;
  264. if (mode == EVP_CIPH_CBC_MODE) {
  265. dat->stream.cbc = (cbc128_f)aes_v8_cbc_encrypt;
  266. }
  267. } else if (bsaes_capable() && mode == EVP_CIPH_CBC_MODE) {
  268. ret = AES_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
  269. dat->block = (block128_f)AES_decrypt;
  270. dat->stream.cbc = (cbc128_f)bsaes_cbc_encrypt;
  271. } else if (vpaes_capable()) {
  272. ret = vpaes_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
  273. dat->block = (block128_f)vpaes_decrypt;
  274. dat->stream.cbc =
  275. mode == EVP_CIPH_CBC_MODE ? (cbc128_f)vpaes_cbc_encrypt : NULL;
  276. } else {
  277. ret = AES_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
  278. dat->block = (block128_f)AES_decrypt;
  279. dat->stream.cbc =
  280. mode == EVP_CIPH_CBC_MODE ? (cbc128_f)AES_cbc_encrypt : NULL;
  281. }
  282. } else if (hwaes_capable()) {
  283. ret = aes_v8_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
  284. dat->block = (block128_f)aes_v8_encrypt;
  285. dat->stream.cbc = NULL;
  286. if (mode == EVP_CIPH_CBC_MODE) {
  287. dat->stream.cbc = (cbc128_f)aes_v8_cbc_encrypt;
  288. } else if (mode == EVP_CIPH_CTR_MODE) {
  289. dat->stream.ctr = (ctr128_f)aes_v8_ctr32_encrypt_blocks;
  290. }
  291. } else if (bsaes_capable() && mode == EVP_CIPH_CTR_MODE) {
  292. ret = AES_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
  293. dat->block = (block128_f)AES_encrypt;
  294. dat->stream.ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks;
  295. } else if (vpaes_capable()) {
  296. ret = vpaes_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
  297. dat->block = (block128_f)vpaes_encrypt;
  298. dat->stream.cbc =
  299. mode == EVP_CIPH_CBC_MODE ? (cbc128_f)vpaes_cbc_encrypt : NULL;
  300. } else {
  301. ret = AES_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
  302. dat->block = (block128_f)AES_encrypt;
  303. dat->stream.cbc =
  304. mode == EVP_CIPH_CBC_MODE ? (cbc128_f)AES_cbc_encrypt : NULL;
  305. }
  306. if (ret < 0) {
  307. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_AES_KEY_SETUP_FAILED);
  308. return 0;
  309. }
  310. return 1;
  311. }
  312. static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
  313. size_t len) {
  314. EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
  315. if (dat->stream.cbc) {
  316. (*dat->stream.cbc)(in, out, len, &dat->ks, ctx->iv, ctx->encrypt);
  317. } else if (ctx->encrypt) {
  318. CRYPTO_cbc128_encrypt(in, out, len, &dat->ks, ctx->iv, dat->block);
  319. } else {
  320. CRYPTO_cbc128_decrypt(in, out, len, &dat->ks, ctx->iv, dat->block);
  321. }
  322. return 1;
  323. }
  324. static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
  325. size_t len) {
  326. size_t bl = ctx->cipher->block_size;
  327. size_t i;
  328. EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
  329. if (len < bl) {
  330. return 1;
  331. }
  332. for (i = 0, len -= bl; i <= len; i += bl) {
  333. (*dat->block)(in + i, out + i, &dat->ks);
  334. }
  335. return 1;
  336. }
  337. static int aes_ctr_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
  338. size_t len) {
  339. unsigned num = (unsigned)ctx->num;
  340. EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
  341. if (dat->stream.ctr) {
  342. CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks, ctx->iv, ctx->buf, &num,
  343. dat->stream.ctr);
  344. } else {
  345. CRYPTO_ctr128_encrypt(in, out, len, &dat->ks, ctx->iv, ctx->buf, &num,
  346. dat->block);
  347. }
  348. ctx->num = (int)num;
  349. return 1;
  350. }
  351. static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
  352. size_t len) {
  353. EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
  354. CRYPTO_ofb128_encrypt(in, out, len, &dat->ks, ctx->iv, &ctx->num, dat->block);
  355. return 1;
  356. }
  357. static char aesni_capable(void);
  358. static ctr128_f aes_ctr_set_key(AES_KEY *aes_key, GCM128_CONTEXT *gcm_ctx,
  359. block128_f *out_block, const uint8_t *key,
  360. size_t key_len)
  361. OPENSSL_SUPPRESS_UNREACHABLE_CODE_WARNINGS {
  362. if (aesni_capable()) {
  363. aesni_set_encrypt_key(key, key_len * 8, aes_key);
  364. if (gcm_ctx != NULL) {
  365. CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)aesni_encrypt);
  366. }
  367. if (out_block) {
  368. *out_block = (block128_f) aesni_encrypt;
  369. }
  370. return (ctr128_f)aesni_ctr32_encrypt_blocks;
  371. }
  372. if (hwaes_capable()) {
  373. aes_v8_set_encrypt_key(key, key_len * 8, aes_key);
  374. if (gcm_ctx != NULL) {
  375. CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)aes_v8_encrypt);
  376. }
  377. if (out_block) {
  378. *out_block = (block128_f) aes_v8_encrypt;
  379. }
  380. return (ctr128_f)aes_v8_ctr32_encrypt_blocks;
  381. }
  382. if (bsaes_capable()) {
  383. AES_set_encrypt_key(key, key_len * 8, aes_key);
  384. if (gcm_ctx != NULL) {
  385. CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt);
  386. }
  387. if (out_block) {
  388. *out_block = (block128_f) AES_encrypt;
  389. }
  390. return (ctr128_f)bsaes_ctr32_encrypt_blocks;
  391. }
  392. if (vpaes_capable()) {
  393. vpaes_set_encrypt_key(key, key_len * 8, aes_key);
  394. if (out_block) {
  395. *out_block = (block128_f) vpaes_encrypt;
  396. }
  397. if (gcm_ctx != NULL) {
  398. CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)vpaes_encrypt);
  399. }
  400. return NULL;
  401. }
  402. AES_set_encrypt_key(key, key_len * 8, aes_key);
  403. if (gcm_ctx != NULL) {
  404. CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt);
  405. }
  406. if (out_block) {
  407. *out_block = (block128_f) AES_encrypt;
  408. }
  409. return NULL;
  410. }
  411. static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
  412. const uint8_t *iv, int enc) {
  413. EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
  414. if (!iv && !key) {
  415. return 1;
  416. }
  417. if (key) {
  418. gctx->ctr =
  419. aes_ctr_set_key(&gctx->ks.ks, &gctx->gcm, NULL, key, ctx->key_len);
  420. /* If we have an iv can set it directly, otherwise use saved IV. */
  421. if (iv == NULL && gctx->iv_set) {
  422. iv = gctx->iv;
  423. }
  424. if (iv) {
  425. CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, iv, gctx->ivlen);
  426. gctx->iv_set = 1;
  427. }
  428. gctx->key_set = 1;
  429. } else {
  430. /* If key set use IV, otherwise copy */
  431. if (gctx->key_set) {
  432. CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, iv, gctx->ivlen);
  433. } else {
  434. memcpy(gctx->iv, iv, gctx->ivlen);
  435. }
  436. gctx->iv_set = 1;
  437. gctx->iv_gen = 0;
  438. }
  439. return 1;
  440. }
  441. static void aes_gcm_cleanup(EVP_CIPHER_CTX *c) {
  442. EVP_AES_GCM_CTX *gctx = c->cipher_data;
  443. OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm));
  444. if (gctx->iv != c->iv) {
  445. OPENSSL_free(gctx->iv);
  446. }
  447. }
  448. /* increment counter (64-bit int) by 1 */
  449. static void ctr64_inc(uint8_t *counter) {
  450. int n = 8;
  451. uint8_t c;
  452. do {
  453. --n;
  454. c = counter[n];
  455. ++c;
  456. counter[n] = c;
  457. if (c) {
  458. return;
  459. }
  460. } while (n);
  461. }
  462. static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) {
  463. EVP_AES_GCM_CTX *gctx = c->cipher_data;
  464. switch (type) {
  465. case EVP_CTRL_INIT:
  466. gctx->key_set = 0;
  467. gctx->iv_set = 0;
  468. gctx->ivlen = c->cipher->iv_len;
  469. gctx->iv = c->iv;
  470. gctx->taglen = -1;
  471. gctx->iv_gen = 0;
  472. return 1;
  473. case EVP_CTRL_GCM_SET_IVLEN:
  474. if (arg <= 0) {
  475. return 0;
  476. }
  477. /* Allocate memory for IV if needed */
  478. if (arg > EVP_MAX_IV_LENGTH && arg > gctx->ivlen) {
  479. if (gctx->iv != c->iv) {
  480. OPENSSL_free(gctx->iv);
  481. }
  482. gctx->iv = OPENSSL_malloc(arg);
  483. if (!gctx->iv) {
  484. return 0;
  485. }
  486. }
  487. gctx->ivlen = arg;
  488. return 1;
  489. case EVP_CTRL_GCM_SET_TAG:
  490. if (arg <= 0 || arg > 16 || c->encrypt) {
  491. return 0;
  492. }
  493. memcpy(c->buf, ptr, arg);
  494. gctx->taglen = arg;
  495. return 1;
  496. case EVP_CTRL_GCM_GET_TAG:
  497. if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0) {
  498. return 0;
  499. }
  500. memcpy(ptr, c->buf, arg);
  501. return 1;
  502. case EVP_CTRL_GCM_SET_IV_FIXED:
  503. /* Special case: -1 length restores whole IV */
  504. if (arg == -1) {
  505. memcpy(gctx->iv, ptr, gctx->ivlen);
  506. gctx->iv_gen = 1;
  507. return 1;
  508. }
  509. /* Fixed field must be at least 4 bytes and invocation field
  510. * at least 8. */
  511. if (arg < 4 || (gctx->ivlen - arg) < 8) {
  512. return 0;
  513. }
  514. if (arg) {
  515. memcpy(gctx->iv, ptr, arg);
  516. }
  517. if (c->encrypt && !RAND_bytes(gctx->iv + arg, gctx->ivlen - arg)) {
  518. return 0;
  519. }
  520. gctx->iv_gen = 1;
  521. return 1;
  522. case EVP_CTRL_GCM_IV_GEN:
  523. if (gctx->iv_gen == 0 || gctx->key_set == 0) {
  524. return 0;
  525. }
  526. CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, gctx->iv, gctx->ivlen);
  527. if (arg <= 0 || arg > gctx->ivlen) {
  528. arg = gctx->ivlen;
  529. }
  530. memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
  531. /* Invocation field will be at least 8 bytes in size and
  532. * so no need to check wrap around or increment more than
  533. * last 8 bytes. */
  534. ctr64_inc(gctx->iv + gctx->ivlen - 8);
  535. gctx->iv_set = 1;
  536. return 1;
  537. case EVP_CTRL_GCM_SET_IV_INV:
  538. if (gctx->iv_gen == 0 || gctx->key_set == 0 || c->encrypt) {
  539. return 0;
  540. }
  541. memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
  542. CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, gctx->iv, gctx->ivlen);
  543. gctx->iv_set = 1;
  544. return 1;
  545. case EVP_CTRL_COPY: {
  546. EVP_CIPHER_CTX *out = ptr;
  547. EVP_AES_GCM_CTX *gctx_out = out->cipher_data;
  548. if (gctx->iv == c->iv) {
  549. gctx_out->iv = out->iv;
  550. } else {
  551. gctx_out->iv = OPENSSL_malloc(gctx->ivlen);
  552. if (!gctx_out->iv) {
  553. return 0;
  554. }
  555. memcpy(gctx_out->iv, gctx->iv, gctx->ivlen);
  556. }
  557. return 1;
  558. }
  559. default:
  560. return -1;
  561. }
  562. }
  563. static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
  564. size_t len) {
  565. EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
  566. /* If not set up, return error */
  567. if (!gctx->key_set) {
  568. return -1;
  569. }
  570. if (!gctx->iv_set) {
  571. return -1;
  572. }
  573. if (in) {
  574. if (out == NULL) {
  575. if (!CRYPTO_gcm128_aad(&gctx->gcm, in, len)) {
  576. return -1;
  577. }
  578. } else if (ctx->encrypt) {
  579. if (gctx->ctr) {
  580. size_t bulk = 0;
  581. #if defined(AES_GCM_ASM)
  582. if (len >= 32 && AES_GCM_ASM(gctx)) {
  583. size_t res = (16 - gctx->gcm.mres) % 16;
  584. if (!CRYPTO_gcm128_encrypt(&gctx->gcm, &gctx->ks.ks, in, out, res)) {
  585. return -1;
  586. }
  587. bulk = AES_gcm_encrypt(in + res, out + res, len - res, &gctx->ks.ks,
  588. gctx->gcm.Yi.c, gctx->gcm.Xi.u);
  589. gctx->gcm.len.u[1] += bulk;
  590. bulk += res;
  591. }
  592. #endif
  593. if (!CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm, &gctx->ks.ks, in + bulk,
  594. out + bulk, len - bulk, gctx->ctr)) {
  595. return -1;
  596. }
  597. } else {
  598. size_t bulk = 0;
  599. if (!CRYPTO_gcm128_encrypt(&gctx->gcm, &gctx->ks.ks, in + bulk,
  600. out + bulk, len - bulk)) {
  601. return -1;
  602. }
  603. }
  604. } else {
  605. if (gctx->ctr) {
  606. size_t bulk = 0;
  607. #if defined(AES_GCM_ASM)
  608. if (len >= 16 && AES_GCM_ASM(gctx)) {
  609. size_t res = (16 - gctx->gcm.mres) % 16;
  610. if (!CRYPTO_gcm128_decrypt(&gctx->gcm, &gctx->ks.ks, in, out, res)) {
  611. return -1;
  612. }
  613. bulk = AES_gcm_decrypt(in + res, out + res, len - res, &gctx->ks.ks,
  614. gctx->gcm.Yi.c, gctx->gcm.Xi.u);
  615. gctx->gcm.len.u[1] += bulk;
  616. bulk += res;
  617. }
  618. #endif
  619. if (!CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm, &gctx->ks.ks, in + bulk,
  620. out + bulk, len - bulk, gctx->ctr)) {
  621. return -1;
  622. }
  623. } else {
  624. size_t bulk = 0;
  625. if (!CRYPTO_gcm128_decrypt(&gctx->gcm, &gctx->ks.ks, in + bulk,
  626. out + bulk, len - bulk)) {
  627. return -1;
  628. }
  629. }
  630. }
  631. return len;
  632. } else {
  633. if (!ctx->encrypt) {
  634. if (gctx->taglen < 0 ||
  635. !CRYPTO_gcm128_finish(&gctx->gcm, ctx->buf, gctx->taglen)) {
  636. return -1;
  637. }
  638. gctx->iv_set = 0;
  639. return 0;
  640. }
  641. CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16);
  642. gctx->taglen = 16;
  643. /* Don't reuse the IV */
  644. gctx->iv_set = 0;
  645. return 0;
  646. }
  647. }
  648. static const EVP_CIPHER aes_128_cbc = {
  649. NID_aes_128_cbc, 16 /* block_size */, 16 /* key_size */,
  650. 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CBC_MODE,
  651. NULL /* app_data */, aes_init_key, aes_cbc_cipher,
  652. NULL /* cleanup */, NULL /* ctrl */};
  653. static const EVP_CIPHER aes_128_ctr = {
  654. NID_aes_128_ctr, 1 /* block_size */, 16 /* key_size */,
  655. 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CTR_MODE,
  656. NULL /* app_data */, aes_init_key, aes_ctr_cipher,
  657. NULL /* cleanup */, NULL /* ctrl */};
  658. static const EVP_CIPHER aes_128_ecb = {
  659. NID_aes_128_ecb, 16 /* block_size */, 16 /* key_size */,
  660. 0 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_ECB_MODE,
  661. NULL /* app_data */, aes_init_key, aes_ecb_cipher,
  662. NULL /* cleanup */, NULL /* ctrl */};
  663. static const EVP_CIPHER aes_128_ofb = {
  664. NID_aes_128_ofb128, 1 /* block_size */, 16 /* key_size */,
  665. 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_OFB_MODE,
  666. NULL /* app_data */, aes_init_key, aes_ofb_cipher,
  667. NULL /* cleanup */, NULL /* ctrl */};
  668. static const EVP_CIPHER aes_128_gcm = {
  669. NID_aes_128_gcm, 1 /* block_size */, 16 /* key_size */, 12 /* iv_len */,
  670. sizeof(EVP_AES_GCM_CTX),
  671. EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER |
  672. EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT |
  673. EVP_CIPH_FLAG_AEAD_CIPHER,
  674. NULL /* app_data */, aes_gcm_init_key, aes_gcm_cipher, aes_gcm_cleanup,
  675. aes_gcm_ctrl};
  676. static const EVP_CIPHER aes_192_cbc = {
  677. NID_aes_192_cbc, 16 /* block_size */, 24 /* key_size */,
  678. 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CBC_MODE,
  679. NULL /* app_data */, aes_init_key, aes_cbc_cipher,
  680. NULL /* cleanup */, NULL /* ctrl */};
  681. static const EVP_CIPHER aes_192_ctr = {
  682. NID_aes_192_ctr, 1 /* block_size */, 24 /* key_size */,
  683. 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CTR_MODE,
  684. NULL /* app_data */, aes_init_key, aes_ctr_cipher,
  685. NULL /* cleanup */, NULL /* ctrl */};
  686. static const EVP_CIPHER aes_192_ecb = {
  687. NID_aes_192_ecb, 16 /* block_size */, 24 /* key_size */,
  688. 0 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_ECB_MODE,
  689. NULL /* app_data */, aes_init_key, aes_ecb_cipher,
  690. NULL /* cleanup */, NULL /* ctrl */};
  691. static const EVP_CIPHER aes_192_gcm = {
  692. NID_aes_192_gcm, 1 /* block_size */, 24 /* key_size */, 12 /* iv_len */,
  693. sizeof(EVP_AES_GCM_CTX),
  694. EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER |
  695. EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT |
  696. EVP_CIPH_FLAG_AEAD_CIPHER,
  697. NULL /* app_data */, aes_gcm_init_key, aes_gcm_cipher, aes_gcm_cleanup,
  698. aes_gcm_ctrl};
  699. static const EVP_CIPHER aes_256_cbc = {
  700. NID_aes_256_cbc, 16 /* block_size */, 32 /* key_size */,
  701. 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CBC_MODE,
  702. NULL /* app_data */, aes_init_key, aes_cbc_cipher,
  703. NULL /* cleanup */, NULL /* ctrl */};
  704. static const EVP_CIPHER aes_256_ctr = {
  705. NID_aes_256_ctr, 1 /* block_size */, 32 /* key_size */,
  706. 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CTR_MODE,
  707. NULL /* app_data */, aes_init_key, aes_ctr_cipher,
  708. NULL /* cleanup */, NULL /* ctrl */};
  709. static const EVP_CIPHER aes_256_ecb = {
  710. NID_aes_256_ecb, 16 /* block_size */, 32 /* key_size */,
  711. 0 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_ECB_MODE,
  712. NULL /* app_data */, aes_init_key, aes_ecb_cipher,
  713. NULL /* cleanup */, NULL /* ctrl */};
  714. static const EVP_CIPHER aes_256_ofb = {
  715. NID_aes_256_ofb128, 1 /* block_size */, 32 /* key_size */,
  716. 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_OFB_MODE,
  717. NULL /* app_data */, aes_init_key, aes_ofb_cipher,
  718. NULL /* cleanup */, NULL /* ctrl */};
  719. static const EVP_CIPHER aes_256_gcm = {
  720. NID_aes_256_gcm, 1 /* block_size */, 32 /* key_size */, 12 /* iv_len */,
  721. sizeof(EVP_AES_GCM_CTX),
  722. EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER |
  723. EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT |
  724. EVP_CIPH_FLAG_AEAD_CIPHER,
  725. NULL /* app_data */, aes_gcm_init_key, aes_gcm_cipher, aes_gcm_cleanup,
  726. aes_gcm_ctrl};
  727. #if !defined(OPENSSL_NO_ASM) && \
  728. (defined(OPENSSL_X86_64) || defined(OPENSSL_X86))
  729. /* AES-NI section. */
  730. static char aesni_capable(void) {
  731. return (OPENSSL_ia32cap_P[1] & (1 << (57 - 32))) != 0;
  732. }
  733. static int aesni_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
  734. const uint8_t *iv, int enc) {
  735. int ret, mode;
  736. EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
  737. mode = ctx->cipher->flags & EVP_CIPH_MODE_MASK;
  738. if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && !enc) {
  739. ret = aesni_set_decrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
  740. dat->block = (block128_f)aesni_decrypt;
  741. dat->stream.cbc =
  742. mode == EVP_CIPH_CBC_MODE ? (cbc128_f)aesni_cbc_encrypt : NULL;
  743. } else {
  744. ret = aesni_set_encrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
  745. dat->block = (block128_f)aesni_encrypt;
  746. if (mode == EVP_CIPH_CBC_MODE) {
  747. dat->stream.cbc = (cbc128_f)aesni_cbc_encrypt;
  748. } else if (mode == EVP_CIPH_CTR_MODE) {
  749. dat->stream.ctr = (ctr128_f)aesni_ctr32_encrypt_blocks;
  750. } else {
  751. dat->stream.cbc = NULL;
  752. }
  753. }
  754. if (ret < 0) {
  755. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_AES_KEY_SETUP_FAILED);
  756. return 0;
  757. }
  758. return 1;
  759. }
  760. static int aesni_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
  761. const uint8_t *in, size_t len) {
  762. aesni_cbc_encrypt(in, out, len, ctx->cipher_data, ctx->iv, ctx->encrypt);
  763. return 1;
  764. }
  765. static int aesni_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
  766. const uint8_t *in, size_t len) {
  767. size_t bl = ctx->cipher->block_size;
  768. if (len < bl) {
  769. return 1;
  770. }
  771. aesni_ecb_encrypt(in, out, len, ctx->cipher_data, ctx->encrypt);
  772. return 1;
  773. }
  774. static int aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
  775. const uint8_t *iv, int enc) {
  776. EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
  777. if (!iv && !key) {
  778. return 1;
  779. }
  780. if (key) {
  781. aesni_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks);
  782. CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, (block128_f)aesni_encrypt);
  783. gctx->ctr = (ctr128_f)aesni_ctr32_encrypt_blocks;
  784. /* If we have an iv can set it directly, otherwise use
  785. * saved IV. */
  786. if (iv == NULL && gctx->iv_set) {
  787. iv = gctx->iv;
  788. }
  789. if (iv) {
  790. CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, iv, gctx->ivlen);
  791. gctx->iv_set = 1;
  792. }
  793. gctx->key_set = 1;
  794. } else {
  795. /* If key set use IV, otherwise copy */
  796. if (gctx->key_set) {
  797. CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, iv, gctx->ivlen);
  798. } else {
  799. memcpy(gctx->iv, iv, gctx->ivlen);
  800. }
  801. gctx->iv_set = 1;
  802. gctx->iv_gen = 0;
  803. }
  804. return 1;
  805. }
  806. static const EVP_CIPHER aesni_128_cbc = {
  807. NID_aes_128_cbc, 16 /* block_size */, 16 /* key_size */,
  808. 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CBC_MODE,
  809. NULL /* app_data */, aesni_init_key, aesni_cbc_cipher,
  810. NULL /* cleanup */, NULL /* ctrl */};
  811. static const EVP_CIPHER aesni_128_ctr = {
  812. NID_aes_128_ctr, 1 /* block_size */, 16 /* key_size */,
  813. 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CTR_MODE,
  814. NULL /* app_data */, aesni_init_key, aes_ctr_cipher,
  815. NULL /* cleanup */, NULL /* ctrl */};
  816. static const EVP_CIPHER aesni_128_ecb = {
  817. NID_aes_128_ecb, 16 /* block_size */, 16 /* key_size */,
  818. 0 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_ECB_MODE,
  819. NULL /* app_data */, aesni_init_key, aesni_ecb_cipher,
  820. NULL /* cleanup */, NULL /* ctrl */};
  821. static const EVP_CIPHER aesni_128_ofb = {
  822. NID_aes_128_ofb128, 1 /* block_size */, 16 /* key_size */,
  823. 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_OFB_MODE,
  824. NULL /* app_data */, aesni_init_key, aes_ofb_cipher,
  825. NULL /* cleanup */, NULL /* ctrl */};
  826. static const EVP_CIPHER aesni_128_gcm = {
  827. NID_aes_128_gcm, 1 /* block_size */, 16 /* key_size */, 12 /* iv_len */,
  828. sizeof(EVP_AES_GCM_CTX),
  829. EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER |
  830. EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT |
  831. EVP_CIPH_FLAG_AEAD_CIPHER,
  832. NULL /* app_data */, aesni_gcm_init_key, aes_gcm_cipher, aes_gcm_cleanup,
  833. aes_gcm_ctrl};
  834. static const EVP_CIPHER aesni_192_cbc = {
  835. NID_aes_192_cbc, 16 /* block_size */, 24 /* key_size */,
  836. 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CBC_MODE,
  837. NULL /* app_data */, aesni_init_key, aesni_cbc_cipher,
  838. NULL /* cleanup */, NULL /* ctrl */};
  839. static const EVP_CIPHER aesni_192_ctr = {
  840. NID_aes_192_ctr, 1 /* block_size */, 24 /* key_size */,
  841. 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CTR_MODE,
  842. NULL /* app_data */, aesni_init_key, aes_ctr_cipher,
  843. NULL /* cleanup */, NULL /* ctrl */};
  844. static const EVP_CIPHER aesni_192_ecb = {
  845. NID_aes_192_ecb, 16 /* block_size */, 24 /* key_size */,
  846. 0 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_ECB_MODE,
  847. NULL /* app_data */, aesni_init_key, aesni_ecb_cipher,
  848. NULL /* cleanup */, NULL /* ctrl */};
  849. static const EVP_CIPHER aesni_192_gcm = {
  850. NID_aes_192_gcm, 1 /* block_size */, 24 /* key_size */, 12 /* iv_len */,
  851. sizeof(EVP_AES_GCM_CTX),
  852. EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER |
  853. EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT |
  854. EVP_CIPH_FLAG_AEAD_CIPHER,
  855. NULL /* app_data */, aesni_gcm_init_key, aes_gcm_cipher, aes_gcm_cleanup,
  856. aes_gcm_ctrl};
  857. static const EVP_CIPHER aesni_256_cbc = {
  858. NID_aes_256_cbc, 16 /* block_size */, 32 /* key_size */,
  859. 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CBC_MODE,
  860. NULL /* app_data */, aesni_init_key, aesni_cbc_cipher,
  861. NULL /* cleanup */, NULL /* ctrl */};
  862. static const EVP_CIPHER aesni_256_ctr = {
  863. NID_aes_256_ctr, 1 /* block_size */, 32 /* key_size */,
  864. 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CTR_MODE,
  865. NULL /* app_data */, aesni_init_key, aes_ctr_cipher,
  866. NULL /* cleanup */, NULL /* ctrl */};
  867. static const EVP_CIPHER aesni_256_ecb = {
  868. NID_aes_256_ecb, 16 /* block_size */, 32 /* key_size */,
  869. 0 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_ECB_MODE,
  870. NULL /* app_data */, aesni_init_key, aesni_ecb_cipher,
  871. NULL /* cleanup */, NULL /* ctrl */};
  872. static const EVP_CIPHER aesni_256_ofb = {
  873. NID_aes_256_ofb128, 1 /* block_size */, 32 /* key_size */,
  874. 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_OFB_MODE,
  875. NULL /* app_data */, aesni_init_key, aes_ofb_cipher,
  876. NULL /* cleanup */, NULL /* ctrl */};
  877. static const EVP_CIPHER aesni_256_gcm = {
  878. NID_aes_256_gcm, 1 /* block_size */, 32 /* key_size */, 12 /* iv_len */,
  879. sizeof(EVP_AES_GCM_CTX),
  880. EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER |
  881. EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT | EVP_CIPH_CUSTOM_COPY |
  882. EVP_CIPH_FLAG_AEAD_CIPHER,
  883. NULL /* app_data */, aesni_gcm_init_key, aes_gcm_cipher, aes_gcm_cleanup,
  884. aes_gcm_ctrl};
  885. #define EVP_CIPHER_FUNCTION(keybits, mode) \
  886. const EVP_CIPHER *EVP_aes_##keybits##_##mode(void) { \
  887. if (aesni_capable()) { \
  888. return &aesni_##keybits##_##mode; \
  889. } else { \
  890. return &aes_##keybits##_##mode; \
  891. } \
  892. }
  893. #else /* ^^^ OPENSSL_X86_64 || OPENSSL_X86 */
  894. static char aesni_capable(void) {
  895. return 0;
  896. }
  897. #define EVP_CIPHER_FUNCTION(keybits, mode) \
  898. const EVP_CIPHER *EVP_aes_##keybits##_##mode(void) { \
  899. return &aes_##keybits##_##mode; \
  900. }
  901. #endif
  902. EVP_CIPHER_FUNCTION(128, cbc)
  903. EVP_CIPHER_FUNCTION(128, ctr)
  904. EVP_CIPHER_FUNCTION(128, ecb)
  905. EVP_CIPHER_FUNCTION(128, ofb)
  906. EVP_CIPHER_FUNCTION(128, gcm)
  907. EVP_CIPHER_FUNCTION(192, cbc)
  908. EVP_CIPHER_FUNCTION(192, ctr)
  909. EVP_CIPHER_FUNCTION(192, ecb)
  910. EVP_CIPHER_FUNCTION(192, gcm)
  911. EVP_CIPHER_FUNCTION(256, cbc)
  912. EVP_CIPHER_FUNCTION(256, ctr)
  913. EVP_CIPHER_FUNCTION(256, ecb)
  914. EVP_CIPHER_FUNCTION(256, ofb)
  915. EVP_CIPHER_FUNCTION(256, gcm)
  916. #define EVP_AEAD_AES_GCM_TAG_LEN 16
  917. struct aead_aes_gcm_ctx {
  918. union {
  919. double align;
  920. AES_KEY ks;
  921. } ks;
  922. GCM128_CONTEXT gcm;
  923. ctr128_f ctr;
  924. uint8_t tag_len;
  925. };
  926. static int aead_aes_gcm_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  927. size_t key_len, size_t tag_len) {
  928. struct aead_aes_gcm_ctx *gcm_ctx;
  929. const size_t key_bits = key_len * 8;
  930. if (key_bits != 128 && key_bits != 256) {
  931. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
  932. return 0; /* EVP_AEAD_CTX_init should catch this. */
  933. }
  934. if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) {
  935. tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
  936. }
  937. if (tag_len > EVP_AEAD_AES_GCM_TAG_LEN) {
  938. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TAG_TOO_LARGE);
  939. return 0;
  940. }
  941. gcm_ctx = OPENSSL_malloc(sizeof(struct aead_aes_gcm_ctx));
  942. if (gcm_ctx == NULL) {
  943. return 0;
  944. }
  945. gcm_ctx->ctr =
  946. aes_ctr_set_key(&gcm_ctx->ks.ks, &gcm_ctx->gcm, NULL, key, key_len);
  947. gcm_ctx->tag_len = tag_len;
  948. ctx->aead_state = gcm_ctx;
  949. return 1;
  950. }
  951. static void aead_aes_gcm_cleanup(EVP_AEAD_CTX *ctx) {
  952. struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state;
  953. OPENSSL_cleanse(gcm_ctx, sizeof(struct aead_aes_gcm_ctx));
  954. OPENSSL_free(gcm_ctx);
  955. }
  956. static int aead_aes_gcm_seal(const EVP_AEAD_CTX *ctx, uint8_t *out,
  957. size_t *out_len, size_t max_out_len,
  958. const uint8_t *nonce, size_t nonce_len,
  959. const uint8_t *in, size_t in_len,
  960. const uint8_t *ad, size_t ad_len) {
  961. const struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state;
  962. GCM128_CONTEXT gcm;
  963. if (in_len + gcm_ctx->tag_len < in_len) {
  964. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  965. return 0;
  966. }
  967. if (max_out_len < in_len + gcm_ctx->tag_len) {
  968. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
  969. return 0;
  970. }
  971. const AES_KEY *key = &gcm_ctx->ks.ks;
  972. memcpy(&gcm, &gcm_ctx->gcm, sizeof(gcm));
  973. CRYPTO_gcm128_setiv(&gcm, key, nonce, nonce_len);
  974. if (ad_len > 0 && !CRYPTO_gcm128_aad(&gcm, ad, ad_len)) {
  975. return 0;
  976. }
  977. if (gcm_ctx->ctr) {
  978. if (!CRYPTO_gcm128_encrypt_ctr32(&gcm, key, in, out, in_len,
  979. gcm_ctx->ctr)) {
  980. return 0;
  981. }
  982. } else {
  983. if (!CRYPTO_gcm128_encrypt(&gcm, key, in, out, in_len)) {
  984. return 0;
  985. }
  986. }
  987. CRYPTO_gcm128_tag(&gcm, out + in_len, gcm_ctx->tag_len);
  988. *out_len = in_len + gcm_ctx->tag_len;
  989. return 1;
  990. }
  991. static int aead_aes_gcm_open(const EVP_AEAD_CTX *ctx, uint8_t *out,
  992. size_t *out_len, size_t max_out_len,
  993. const uint8_t *nonce, size_t nonce_len,
  994. const uint8_t *in, size_t in_len,
  995. const uint8_t *ad, size_t ad_len) {
  996. const struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state;
  997. uint8_t tag[EVP_AEAD_AES_GCM_TAG_LEN];
  998. size_t plaintext_len;
  999. GCM128_CONTEXT gcm;
  1000. if (in_len < gcm_ctx->tag_len) {
  1001. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  1002. return 0;
  1003. }
  1004. plaintext_len = in_len - gcm_ctx->tag_len;
  1005. if (max_out_len < plaintext_len) {
  1006. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
  1007. return 0;
  1008. }
  1009. const AES_KEY *key = &gcm_ctx->ks.ks;
  1010. memcpy(&gcm, &gcm_ctx->gcm, sizeof(gcm));
  1011. CRYPTO_gcm128_setiv(&gcm, key, nonce, nonce_len);
  1012. if (!CRYPTO_gcm128_aad(&gcm, ad, ad_len)) {
  1013. return 0;
  1014. }
  1015. if (gcm_ctx->ctr) {
  1016. if (!CRYPTO_gcm128_decrypt_ctr32(&gcm, key, in, out,
  1017. in_len - gcm_ctx->tag_len, gcm_ctx->ctr)) {
  1018. return 0;
  1019. }
  1020. } else {
  1021. if (!CRYPTO_gcm128_decrypt(&gcm, key, in, out, in_len - gcm_ctx->tag_len)) {
  1022. return 0;
  1023. }
  1024. }
  1025. CRYPTO_gcm128_tag(&gcm, tag, gcm_ctx->tag_len);
  1026. if (CRYPTO_memcmp(tag, in + plaintext_len, gcm_ctx->tag_len) != 0) {
  1027. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  1028. return 0;
  1029. }
  1030. *out_len = plaintext_len;
  1031. return 1;
  1032. }
  1033. static const EVP_AEAD aead_aes_128_gcm = {
  1034. 16, /* key len */
  1035. 12, /* nonce len */
  1036. EVP_AEAD_AES_GCM_TAG_LEN, /* overhead */
  1037. EVP_AEAD_AES_GCM_TAG_LEN, /* max tag length */
  1038. aead_aes_gcm_init,
  1039. NULL, /* init_with_direction */
  1040. aead_aes_gcm_cleanup,
  1041. aead_aes_gcm_seal,
  1042. aead_aes_gcm_open,
  1043. NULL, /* get_rc4_state */
  1044. NULL, /* get_iv */
  1045. };
  1046. static const EVP_AEAD aead_aes_256_gcm = {
  1047. 32, /* key len */
  1048. 12, /* nonce len */
  1049. EVP_AEAD_AES_GCM_TAG_LEN, /* overhead */
  1050. EVP_AEAD_AES_GCM_TAG_LEN, /* max tag length */
  1051. aead_aes_gcm_init,
  1052. NULL, /* init_with_direction */
  1053. aead_aes_gcm_cleanup,
  1054. aead_aes_gcm_seal,
  1055. aead_aes_gcm_open,
  1056. NULL, /* get_rc4_state */
  1057. NULL, /* get_iv */
  1058. };
  1059. const EVP_AEAD *EVP_aead_aes_128_gcm(void) { return &aead_aes_128_gcm; }
  1060. const EVP_AEAD *EVP_aead_aes_256_gcm(void) { return &aead_aes_256_gcm; }
  1061. /* AES Key Wrap is specified in
  1062. * http://csrc.nist.gov/groups/ST/toolkit/documents/kms/key-wrap.pdf
  1063. * or https://tools.ietf.org/html/rfc3394 */
  1064. struct aead_aes_key_wrap_ctx {
  1065. uint8_t key[32];
  1066. unsigned key_bits;
  1067. };
  1068. static int aead_aes_key_wrap_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  1069. size_t key_len, size_t tag_len) {
  1070. struct aead_aes_key_wrap_ctx *kw_ctx;
  1071. const size_t key_bits = key_len * 8;
  1072. if (key_bits != 128 && key_bits != 256) {
  1073. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
  1074. return 0; /* EVP_AEAD_CTX_init should catch this. */
  1075. }
  1076. if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) {
  1077. tag_len = 8;
  1078. }
  1079. if (tag_len != 8) {
  1080. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_TAG_SIZE);
  1081. return 0;
  1082. }
  1083. kw_ctx = OPENSSL_malloc(sizeof(struct aead_aes_key_wrap_ctx));
  1084. if (kw_ctx == NULL) {
  1085. OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE);
  1086. return 0;
  1087. }
  1088. memcpy(kw_ctx->key, key, key_len);
  1089. kw_ctx->key_bits = key_bits;
  1090. ctx->aead_state = kw_ctx;
  1091. return 1;
  1092. }
  1093. static void aead_aes_key_wrap_cleanup(EVP_AEAD_CTX *ctx) {
  1094. struct aead_aes_key_wrap_ctx *kw_ctx = ctx->aead_state;
  1095. OPENSSL_cleanse(kw_ctx, sizeof(struct aead_aes_key_wrap_ctx));
  1096. OPENSSL_free(kw_ctx);
  1097. }
  1098. /* kDefaultAESKeyWrapNonce is the default nonce value given in 2.2.3.1. */
  1099. static const uint8_t kDefaultAESKeyWrapNonce[8] = {0xa6, 0xa6, 0xa6, 0xa6,
  1100. 0xa6, 0xa6, 0xa6, 0xa6};
  1101. static int aead_aes_key_wrap_seal(const EVP_AEAD_CTX *ctx, uint8_t *out,
  1102. size_t *out_len, size_t max_out_len,
  1103. const uint8_t *nonce, size_t nonce_len,
  1104. const uint8_t *in, size_t in_len,
  1105. const uint8_t *ad, size_t ad_len) {
  1106. const struct aead_aes_key_wrap_ctx *kw_ctx = ctx->aead_state;
  1107. union {
  1108. double align;
  1109. AES_KEY ks;
  1110. } ks;
  1111. /* Variables in this function match up with the variables in the second half
  1112. * of section 2.2.1. */
  1113. unsigned i, j, n;
  1114. uint8_t A[AES_BLOCK_SIZE];
  1115. if (ad_len != 0) {
  1116. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_AD_SIZE);
  1117. return 0;
  1118. }
  1119. if (nonce_len == 0) {
  1120. nonce = kDefaultAESKeyWrapNonce;
  1121. nonce_len = sizeof(kDefaultAESKeyWrapNonce);
  1122. }
  1123. if (nonce_len != 8) {
  1124. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
  1125. return 0;
  1126. }
  1127. if (in_len % 8 != 0) {
  1128. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_INPUT_SIZE);
  1129. return 0;
  1130. }
  1131. /* The code below only handles a 32-bit |t| thus 6*|n| must be less than
  1132. * 2^32, where |n| is |in_len| / 8. So in_len < 4/3 * 2^32 and we
  1133. * conservatively cap it to 2^32-16 to stop 32-bit platforms complaining that
  1134. * a comparison is always true. */
  1135. if (in_len > 0xfffffff0) {
  1136. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  1137. return 0;
  1138. }
  1139. n = in_len / 8;
  1140. if (n < 2) {
  1141. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_INPUT_SIZE);
  1142. return 0;
  1143. }
  1144. if (in_len + 8 < in_len) {
  1145. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  1146. return 0;
  1147. }
  1148. if (max_out_len < in_len + 8) {
  1149. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
  1150. return 0;
  1151. }
  1152. if (AES_set_encrypt_key(kw_ctx->key, kw_ctx->key_bits, &ks.ks) < 0) {
  1153. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_AES_KEY_SETUP_FAILED);
  1154. return 0;
  1155. }
  1156. memmove(out + 8, in, in_len);
  1157. memcpy(A, nonce, 8);
  1158. for (j = 0; j < 6; j++) {
  1159. for (i = 1; i <= n; i++) {
  1160. uint32_t t;
  1161. memcpy(A + 8, out + 8 * i, 8);
  1162. AES_encrypt(A, A, &ks.ks);
  1163. t = n * j + i;
  1164. A[7] ^= t & 0xff;
  1165. A[6] ^= (t >> 8) & 0xff;
  1166. A[5] ^= (t >> 16) & 0xff;
  1167. A[4] ^= (t >> 24) & 0xff;
  1168. memcpy(out + 8 * i, A + 8, 8);
  1169. }
  1170. }
  1171. memcpy(out, A, 8);
  1172. *out_len = in_len + 8;
  1173. return 1;
  1174. }
  1175. static int aead_aes_key_wrap_open(const EVP_AEAD_CTX *ctx, uint8_t *out,
  1176. size_t *out_len, size_t max_out_len,
  1177. const uint8_t *nonce, size_t nonce_len,
  1178. const uint8_t *in, size_t in_len,
  1179. const uint8_t *ad, size_t ad_len) {
  1180. const struct aead_aes_key_wrap_ctx *kw_ctx = ctx->aead_state;
  1181. union {
  1182. double align;
  1183. AES_KEY ks;
  1184. } ks;
  1185. /* Variables in this function match up with the variables in the second half
  1186. * of section 2.2.1. */
  1187. unsigned i, j, n;
  1188. uint8_t A[AES_BLOCK_SIZE];
  1189. if (ad_len != 0) {
  1190. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_AD_SIZE);
  1191. return 0;
  1192. }
  1193. if (nonce_len == 0) {
  1194. nonce = kDefaultAESKeyWrapNonce;
  1195. nonce_len = sizeof(kDefaultAESKeyWrapNonce);
  1196. }
  1197. if (nonce_len != 8) {
  1198. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
  1199. return 0;
  1200. }
  1201. if (in_len % 8 != 0) {
  1202. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_INPUT_SIZE);
  1203. return 0;
  1204. }
  1205. /* The code below only handles a 32-bit |t| thus 6*|n| must be less than
  1206. * 2^32, where |n| is |in_len| / 8. So in_len < 4/3 * 2^32 and we
  1207. * conservatively cap it to 2^32-8 to stop 32-bit platforms complaining that
  1208. * a comparison is always true. */
  1209. if (in_len > 0xfffffff8) {
  1210. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  1211. return 0;
  1212. }
  1213. if (in_len < 24) {
  1214. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  1215. return 0;
  1216. }
  1217. n = (in_len / 8) - 1;
  1218. if (max_out_len < in_len - 8) {
  1219. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
  1220. return 0;
  1221. }
  1222. if (AES_set_decrypt_key(kw_ctx->key, kw_ctx->key_bits, &ks.ks) < 0) {
  1223. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_AES_KEY_SETUP_FAILED);
  1224. return 0;
  1225. }
  1226. memcpy(A, in, 8);
  1227. memmove(out, in + 8, in_len - 8);
  1228. for (j = 5; j < 6; j--) {
  1229. for (i = n; i > 0; i--) {
  1230. uint32_t t;
  1231. t = n * j + i;
  1232. A[7] ^= t & 0xff;
  1233. A[6] ^= (t >> 8) & 0xff;
  1234. A[5] ^= (t >> 16) & 0xff;
  1235. A[4] ^= (t >> 24) & 0xff;
  1236. memcpy(A + 8, out + 8 * (i - 1), 8);
  1237. AES_decrypt(A, A, &ks.ks);
  1238. memcpy(out + 8 * (i - 1), A + 8, 8);
  1239. }
  1240. }
  1241. if (CRYPTO_memcmp(A, nonce, 8) != 0) {
  1242. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  1243. return 0;
  1244. }
  1245. *out_len = in_len - 8;
  1246. return 1;
  1247. }
  1248. static const EVP_AEAD aead_aes_128_key_wrap = {
  1249. 16, /* key len */
  1250. 8, /* nonce len */
  1251. 8, /* overhead */
  1252. 8, /* max tag length */
  1253. aead_aes_key_wrap_init,
  1254. NULL, /* init_with_direction */
  1255. aead_aes_key_wrap_cleanup,
  1256. aead_aes_key_wrap_seal,
  1257. aead_aes_key_wrap_open,
  1258. NULL, /* get_rc4_state */
  1259. NULL, /* get_iv */
  1260. };
  1261. static const EVP_AEAD aead_aes_256_key_wrap = {
  1262. 32, /* key len */
  1263. 8, /* nonce len */
  1264. 8, /* overhead */
  1265. 8, /* max tag length */
  1266. aead_aes_key_wrap_init,
  1267. NULL, /* init_with_direction */
  1268. aead_aes_key_wrap_cleanup,
  1269. aead_aes_key_wrap_seal,
  1270. aead_aes_key_wrap_open,
  1271. NULL, /* get_rc4_state */
  1272. NULL, /* get_iv */
  1273. };
  1274. const EVP_AEAD *EVP_aead_aes_128_key_wrap(void) { return &aead_aes_128_key_wrap; }
  1275. const EVP_AEAD *EVP_aead_aes_256_key_wrap(void) { return &aead_aes_256_key_wrap; }
  1276. #define EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN SHA256_DIGEST_LENGTH
  1277. #define EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN 12
  1278. struct aead_aes_ctr_hmac_sha256_ctx {
  1279. union {
  1280. double align;
  1281. AES_KEY ks;
  1282. } ks;
  1283. ctr128_f ctr;
  1284. block128_f block;
  1285. SHA256_CTX inner_init_state;
  1286. SHA256_CTX outer_init_state;
  1287. uint8_t tag_len;
  1288. };
  1289. static void hmac_init(SHA256_CTX *out_inner, SHA256_CTX *out_outer,
  1290. const uint8_t hmac_key[32]) {
  1291. static const size_t hmac_key_len = 32;
  1292. uint8_t block[SHA256_CBLOCK];
  1293. memcpy(block, hmac_key, hmac_key_len);
  1294. memset(block + hmac_key_len, 0x36, sizeof(block) - hmac_key_len);
  1295. unsigned i;
  1296. for (i = 0; i < hmac_key_len; i++) {
  1297. block[i] ^= 0x36;
  1298. }
  1299. SHA256_Init(out_inner);
  1300. SHA256_Update(out_inner, block, sizeof(block));
  1301. memset(block + hmac_key_len, 0x5c, sizeof(block) - hmac_key_len);
  1302. for (i = 0; i < hmac_key_len; i++) {
  1303. block[i] ^= (0x36 ^ 0x5c);
  1304. }
  1305. SHA256_Init(out_outer);
  1306. SHA256_Update(out_outer, block, sizeof(block));
  1307. }
  1308. static int aead_aes_ctr_hmac_sha256_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  1309. size_t key_len, size_t tag_len) {
  1310. struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx;
  1311. static const size_t hmac_key_len = 32;
  1312. if (key_len < hmac_key_len) {
  1313. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
  1314. return 0; /* EVP_AEAD_CTX_init should catch this. */
  1315. }
  1316. const size_t aes_key_len = key_len - hmac_key_len;
  1317. if (aes_key_len != 16 && aes_key_len != 32) {
  1318. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
  1319. return 0; /* EVP_AEAD_CTX_init should catch this. */
  1320. }
  1321. if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) {
  1322. tag_len = EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN;
  1323. }
  1324. if (tag_len > EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN) {
  1325. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TAG_TOO_LARGE);
  1326. return 0;
  1327. }
  1328. aes_ctx = OPENSSL_malloc(sizeof(struct aead_aes_ctr_hmac_sha256_ctx));
  1329. if (aes_ctx == NULL) {
  1330. OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE);
  1331. return 0;
  1332. }
  1333. aes_ctx->ctr =
  1334. aes_ctr_set_key(&aes_ctx->ks.ks, NULL, &aes_ctx->block, key, aes_key_len);
  1335. aes_ctx->tag_len = tag_len;
  1336. hmac_init(&aes_ctx->inner_init_state, &aes_ctx->outer_init_state,
  1337. key + aes_key_len);
  1338. ctx->aead_state = aes_ctx;
  1339. return 1;
  1340. }
  1341. static void aead_aes_ctr_hmac_sha256_cleanup(EVP_AEAD_CTX *ctx) {
  1342. struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx = ctx->aead_state;
  1343. OPENSSL_cleanse(aes_ctx, sizeof(struct aead_aes_ctr_hmac_sha256_ctx));
  1344. OPENSSL_free(aes_ctx);
  1345. }
  1346. static void hmac_update_uint64(SHA256_CTX *sha256, uint64_t value) {
  1347. unsigned i;
  1348. uint8_t bytes[8];
  1349. for (i = 0; i < sizeof(bytes); i++) {
  1350. bytes[i] = value & 0xff;
  1351. value >>= 8;
  1352. }
  1353. SHA256_Update(sha256, bytes, sizeof(bytes));
  1354. }
  1355. static void hmac_calculate(uint8_t out[SHA256_DIGEST_LENGTH],
  1356. const SHA256_CTX *inner_init_state,
  1357. const SHA256_CTX *outer_init_state,
  1358. const uint8_t *ad, size_t ad_len,
  1359. const uint8_t *nonce, const uint8_t *ciphertext,
  1360. size_t ciphertext_len) {
  1361. SHA256_CTX sha256;
  1362. memcpy(&sha256, inner_init_state, sizeof(sha256));
  1363. hmac_update_uint64(&sha256, ad_len);
  1364. hmac_update_uint64(&sha256, ciphertext_len);
  1365. SHA256_Update(&sha256, nonce, EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN);
  1366. SHA256_Update(&sha256, ad, ad_len);
  1367. /* Pad with zeros to the end of the SHA-256 block. */
  1368. const unsigned num_padding =
  1369. (SHA256_CBLOCK - ((sizeof(uint64_t)*2 +
  1370. EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN + ad_len) %
  1371. SHA256_CBLOCK)) %
  1372. SHA256_CBLOCK;
  1373. uint8_t padding[SHA256_CBLOCK];
  1374. memset(padding, 0, num_padding);
  1375. SHA256_Update(&sha256, padding, num_padding);
  1376. SHA256_Update(&sha256, ciphertext, ciphertext_len);
  1377. uint8_t inner_digest[SHA256_DIGEST_LENGTH];
  1378. SHA256_Final(inner_digest, &sha256);
  1379. memcpy(&sha256, outer_init_state, sizeof(sha256));
  1380. SHA256_Update(&sha256, inner_digest, sizeof(inner_digest));
  1381. SHA256_Final(out, &sha256);
  1382. }
  1383. static void aead_aes_ctr_hmac_sha256_crypt(
  1384. const struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx, uint8_t *out,
  1385. const uint8_t *in, size_t len, const uint8_t *nonce) {
  1386. /* Since the AEAD operation is one-shot, keeping a buffer of unused keystream
  1387. * bytes is pointless. However, |CRYPTO_ctr128_encrypt| requires it. */
  1388. uint8_t partial_block_buffer[AES_BLOCK_SIZE];
  1389. unsigned partial_block_offset = 0;
  1390. memset(partial_block_buffer, 0, sizeof(partial_block_buffer));
  1391. uint8_t counter[AES_BLOCK_SIZE];
  1392. memcpy(counter, nonce, EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN);
  1393. memset(counter + EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN, 0, 4);
  1394. if (aes_ctx->ctr) {
  1395. CRYPTO_ctr128_encrypt_ctr32(in, out, len, &aes_ctx->ks.ks, counter,
  1396. partial_block_buffer, &partial_block_offset,
  1397. aes_ctx->ctr);
  1398. } else {
  1399. CRYPTO_ctr128_encrypt(in, out, len, &aes_ctx->ks.ks, counter,
  1400. partial_block_buffer, &partial_block_offset,
  1401. aes_ctx->block);
  1402. }
  1403. }
  1404. static int aead_aes_ctr_hmac_sha256_seal(const EVP_AEAD_CTX *ctx, uint8_t *out,
  1405. size_t *out_len, size_t max_out_len,
  1406. const uint8_t *nonce, size_t nonce_len,
  1407. const uint8_t *in, size_t in_len,
  1408. const uint8_t *ad, size_t ad_len) {
  1409. const struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx = ctx->aead_state;
  1410. const uint64_t in_len_64 = in_len;
  1411. if (in_len + aes_ctx->tag_len < in_len ||
  1412. /* This input is so large it would overflow the 32-bit block counter. */
  1413. in_len_64 >= (UINT64_C(1) << 32) * AES_BLOCK_SIZE) {
  1414. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  1415. return 0;
  1416. }
  1417. if (max_out_len < in_len + aes_ctx->tag_len) {
  1418. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
  1419. return 0;
  1420. }
  1421. if (nonce_len != EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN) {
  1422. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
  1423. return 0;
  1424. }
  1425. aead_aes_ctr_hmac_sha256_crypt(aes_ctx, out, in, in_len, nonce);
  1426. uint8_t hmac_result[SHA256_DIGEST_LENGTH];
  1427. hmac_calculate(hmac_result, &aes_ctx->inner_init_state,
  1428. &aes_ctx->outer_init_state, ad, ad_len, nonce, out, in_len);
  1429. memcpy(out + in_len, hmac_result, aes_ctx->tag_len);
  1430. *out_len = in_len + aes_ctx->tag_len;
  1431. return 1;
  1432. }
  1433. static int aead_aes_ctr_hmac_sha256_open(const EVP_AEAD_CTX *ctx, uint8_t *out,
  1434. size_t *out_len, size_t max_out_len,
  1435. const uint8_t *nonce, size_t nonce_len,
  1436. const uint8_t *in, size_t in_len,
  1437. const uint8_t *ad, size_t ad_len) {
  1438. const struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx = ctx->aead_state;
  1439. size_t plaintext_len;
  1440. if (in_len < aes_ctx->tag_len) {
  1441. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  1442. return 0;
  1443. }
  1444. plaintext_len = in_len - aes_ctx->tag_len;
  1445. if (max_out_len < plaintext_len) {
  1446. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
  1447. return 0;
  1448. }
  1449. if (nonce_len != EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN) {
  1450. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
  1451. return 0;
  1452. }
  1453. uint8_t hmac_result[SHA256_DIGEST_LENGTH];
  1454. hmac_calculate(hmac_result, &aes_ctx->inner_init_state,
  1455. &aes_ctx->outer_init_state, ad, ad_len, nonce, in,
  1456. plaintext_len);
  1457. if (CRYPTO_memcmp(hmac_result, in + plaintext_len, aes_ctx->tag_len) != 0) {
  1458. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  1459. return 0;
  1460. }
  1461. aead_aes_ctr_hmac_sha256_crypt(aes_ctx, out, in, plaintext_len, nonce);
  1462. *out_len = plaintext_len;
  1463. return 1;
  1464. }
  1465. static const EVP_AEAD aead_aes_128_ctr_hmac_sha256 = {
  1466. 16 /* AES key */ + 32 /* HMAC key */,
  1467. 12, /* nonce length */
  1468. EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, /* overhead */
  1469. EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, /* max tag length */
  1470. aead_aes_ctr_hmac_sha256_init,
  1471. NULL /* init_with_direction */,
  1472. aead_aes_ctr_hmac_sha256_cleanup,
  1473. aead_aes_ctr_hmac_sha256_seal,
  1474. aead_aes_ctr_hmac_sha256_open,
  1475. NULL /* get_rc4_state */,
  1476. NULL /* get_iv */,
  1477. };
  1478. static const EVP_AEAD aead_aes_256_ctr_hmac_sha256 = {
  1479. 32 /* AES key */ + 32 /* HMAC key */,
  1480. 12, /* nonce length */
  1481. EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, /* overhead */
  1482. EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, /* max tag length */
  1483. aead_aes_ctr_hmac_sha256_init,
  1484. NULL /* init_with_direction */,
  1485. aead_aes_ctr_hmac_sha256_cleanup,
  1486. aead_aes_ctr_hmac_sha256_seal,
  1487. aead_aes_ctr_hmac_sha256_open,
  1488. NULL /* get_rc4_state */,
  1489. NULL /* get_iv */,
  1490. };
  1491. const EVP_AEAD *EVP_aead_aes_128_ctr_hmac_sha256(void) {
  1492. return &aead_aes_128_ctr_hmac_sha256;
  1493. }
  1494. const EVP_AEAD *EVP_aead_aes_256_ctr_hmac_sha256(void) {
  1495. return &aead_aes_256_ctr_hmac_sha256;
  1496. }
  1497. int EVP_has_aes_hardware(void) {
  1498. #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
  1499. return aesni_capable() && crypto_gcm_clmul_enabled();
  1500. #elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
  1501. return hwaes_capable() && CRYPTO_is_ARMv8_PMULL_capable();
  1502. #else
  1503. return 0;
  1504. #endif
  1505. }