e_aes.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  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. *
  49. */
  50. #define OPENSSL_FIPSAPI
  51. #include <openssl/opensslconf.h>
  52. #ifndef OPENSSL_NO_AES
  53. #include <openssl/evp.h>
  54. #include <openssl/err.h>
  55. #include <string.h>
  56. #include <assert.h>
  57. #include <openssl/aes.h>
  58. #include "evp_locl.h"
  59. #include "modes_lcl.h"
  60. #include <openssl/rand.h>
  61. typedef struct
  62. {
  63. AES_KEY ks;
  64. block128_f block;
  65. union {
  66. cbc128_f cbc;
  67. ctr128_f ctr;
  68. } stream;
  69. } EVP_AES_KEY;
  70. typedef struct
  71. {
  72. AES_KEY ks; /* AES key schedule to use */
  73. int key_set; /* Set if key initialised */
  74. int iv_set; /* Set if an iv is set */
  75. GCM128_CONTEXT gcm;
  76. unsigned char *iv; /* Temporary IV store */
  77. int ivlen; /* IV length */
  78. int taglen;
  79. int iv_gen; /* It is OK to generate IVs */
  80. int tls_aad_len; /* TLS AAD length */
  81. ctr128_f ctr;
  82. } EVP_AES_GCM_CTX;
  83. typedef struct
  84. {
  85. AES_KEY ks1, ks2; /* AES key schedules to use */
  86. XTS128_CONTEXT xts;
  87. void (*stream)(const unsigned char *in,
  88. unsigned char *out, size_t length,
  89. const AES_KEY *key1, const AES_KEY *key2,
  90. const unsigned char iv[16]);
  91. } EVP_AES_XTS_CTX;
  92. typedef struct
  93. {
  94. AES_KEY ks; /* AES key schedule to use */
  95. int key_set; /* Set if key initialised */
  96. int iv_set; /* Set if an iv is set */
  97. int tag_set; /* Set if tag is valid */
  98. int len_set; /* Set if message length set */
  99. int L, M; /* L and M parameters from RFC3610 */
  100. CCM128_CONTEXT ccm;
  101. ccm128_f str;
  102. } EVP_AES_CCM_CTX;
  103. #define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4))
  104. #ifdef VPAES_ASM
  105. int vpaes_set_encrypt_key(const unsigned char *userKey, int bits,
  106. AES_KEY *key);
  107. int vpaes_set_decrypt_key(const unsigned char *userKey, int bits,
  108. AES_KEY *key);
  109. void vpaes_encrypt(const unsigned char *in, unsigned char *out,
  110. const AES_KEY *key);
  111. void vpaes_decrypt(const unsigned char *in, unsigned char *out,
  112. const AES_KEY *key);
  113. void vpaes_cbc_encrypt(const unsigned char *in,
  114. unsigned char *out,
  115. size_t length,
  116. const AES_KEY *key,
  117. unsigned char *ivec, int enc);
  118. #endif
  119. #ifdef BSAES_ASM
  120. void bsaes_cbc_encrypt(const unsigned char *in, unsigned char *out,
  121. size_t length, const AES_KEY *key,
  122. unsigned char ivec[16], int enc);
  123. void bsaes_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
  124. size_t len, const AES_KEY *key,
  125. const unsigned char ivec[16]);
  126. #endif
  127. #ifdef AES_CTR_ASM
  128. void AES_ctr32_encrypt(const unsigned char *in, unsigned char *out,
  129. size_t blocks, const AES_KEY *key,
  130. const unsigned char ivec[AES_BLOCK_SIZE]);
  131. #endif
  132. #if defined(OPENSSL_CPUID_OBJ) && (defined(__powerpc__) || defined(__ppc__) || defined(_ARCH_PPC))
  133. extern int OPENSSL_ppccap_P;
  134. # define HWAES_CAPABLE (OPENSSL_ppccap_P & (1<<2))
  135. # define HWAES_set_encrypt_key aes_p8_set_encrypt_key
  136. # define HWAES_set_decrypt_key aes_p8_set_decrypt_key
  137. # define HWAES_encrypt aes_p8_encrypt
  138. # define HWAES_decrypt aes_p8_decrypt
  139. # define HWAES_cbc_encrypt aes_p8_cbc_encrypt
  140. # define HWAES_ctr32_encrypt_blocks aes_p8_ctr32_encrypt_blocks
  141. # define HWAES_xts_encrypt aes_p8_xts_encrypt
  142. # define HWAES_xts_decrypt aes_p8_xts_decrypt
  143. #endif
  144. #if defined(AES_ASM) && !defined(I386_ONLY) && ( \
  145. ((defined(__i386) || defined(__i386__) || \
  146. defined(_M_IX86)) && defined(OPENSSL_IA32_SSE2))|| \
  147. defined(__x86_64) || defined(__x86_64__) || \
  148. defined(_M_AMD64) || defined(_M_X64) || \
  149. defined(__INTEL__) )
  150. extern unsigned int OPENSSL_ia32cap_P[2];
  151. #ifdef VPAES_ASM
  152. #define VPAES_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(41-32)))
  153. #endif
  154. #ifdef BSAES_ASM
  155. #define BSAES_CAPABLE VPAES_CAPABLE
  156. #endif
  157. /*
  158. * AES-NI section
  159. */
  160. #define AESNI_CAPABLE (OPENSSL_ia32cap_P[1]&(1<<(57-32)))
  161. int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
  162. AES_KEY *key);
  163. int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
  164. AES_KEY *key);
  165. void aesni_encrypt(const unsigned char *in, unsigned char *out,
  166. const AES_KEY *key);
  167. void aesni_decrypt(const unsigned char *in, unsigned char *out,
  168. const AES_KEY *key);
  169. void aesni_ecb_encrypt(const unsigned char *in,
  170. unsigned char *out,
  171. size_t length,
  172. const AES_KEY *key,
  173. int enc);
  174. void aesni_cbc_encrypt(const unsigned char *in,
  175. unsigned char *out,
  176. size_t length,
  177. const AES_KEY *key,
  178. unsigned char *ivec, int enc);
  179. void aesni_ctr32_encrypt_blocks(const unsigned char *in,
  180. unsigned char *out,
  181. size_t blocks,
  182. const void *key,
  183. const unsigned char *ivec);
  184. void aesni_xts_encrypt(const unsigned char *in,
  185. unsigned char *out,
  186. size_t length,
  187. const AES_KEY *key1, const AES_KEY *key2,
  188. const unsigned char iv[16]);
  189. void aesni_xts_decrypt(const unsigned char *in,
  190. unsigned char *out,
  191. size_t length,
  192. const AES_KEY *key1, const AES_KEY *key2,
  193. const unsigned char iv[16]);
  194. void aesni_ccm64_encrypt_blocks (const unsigned char *in,
  195. unsigned char *out,
  196. size_t blocks,
  197. const void *key,
  198. const unsigned char ivec[16],
  199. unsigned char cmac[16]);
  200. void aesni_ccm64_decrypt_blocks (const unsigned char *in,
  201. unsigned char *out,
  202. size_t blocks,
  203. const void *key,
  204. const unsigned char ivec[16],
  205. unsigned char cmac[16]);
  206. static int aesni_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  207. const unsigned char *iv, int enc)
  208. {
  209. int ret, mode;
  210. EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
  211. mode = ctx->cipher->flags & EVP_CIPH_MODE;
  212. if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
  213. && !enc)
  214. {
  215. ret = aesni_set_decrypt_key(key, ctx->key_len*8, ctx->cipher_data);
  216. dat->block = (block128_f)aesni_decrypt;
  217. dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
  218. (cbc128_f)aesni_cbc_encrypt :
  219. NULL;
  220. }
  221. else {
  222. ret = aesni_set_encrypt_key(key, ctx->key_len*8, ctx->cipher_data);
  223. dat->block = (block128_f)aesni_encrypt;
  224. if (mode==EVP_CIPH_CBC_MODE)
  225. dat->stream.cbc = (cbc128_f)aesni_cbc_encrypt;
  226. else if (mode==EVP_CIPH_CTR_MODE)
  227. dat->stream.ctr = (ctr128_f)aesni_ctr32_encrypt_blocks;
  228. else
  229. dat->stream.cbc = NULL;
  230. }
  231. if(ret < 0)
  232. {
  233. EVPerr(EVP_F_AESNI_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
  234. return 0;
  235. }
  236. return 1;
  237. }
  238. static int aesni_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
  239. const unsigned char *in, size_t len)
  240. {
  241. aesni_cbc_encrypt(in,out,len,ctx->cipher_data,ctx->iv,ctx->encrypt);
  242. return 1;
  243. }
  244. static int aesni_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
  245. const unsigned char *in, size_t len)
  246. {
  247. size_t bl = ctx->cipher->block_size;
  248. if (len<bl) return 1;
  249. aesni_ecb_encrypt(in,out,len,ctx->cipher_data,ctx->encrypt);
  250. return 1;
  251. }
  252. #define aesni_ofb_cipher aes_ofb_cipher
  253. static int aesni_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
  254. const unsigned char *in,size_t len);
  255. #define aesni_cfb_cipher aes_cfb_cipher
  256. static int aesni_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
  257. const unsigned char *in,size_t len);
  258. #define aesni_cfb8_cipher aes_cfb8_cipher
  259. static int aesni_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
  260. const unsigned char *in,size_t len);
  261. #define aesni_cfb1_cipher aes_cfb1_cipher
  262. static int aesni_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
  263. const unsigned char *in,size_t len);
  264. #define aesni_ctr_cipher aes_ctr_cipher
  265. static int aesni_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  266. const unsigned char *in, size_t len);
  267. static int aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  268. const unsigned char *iv, int enc)
  269. {
  270. EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
  271. if (!iv && !key)
  272. return 1;
  273. if (key)
  274. {
  275. aesni_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks);
  276. CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
  277. (block128_f)aesni_encrypt);
  278. gctx->ctr = (ctr128_f)aesni_ctr32_encrypt_blocks;
  279. /* If we have an iv can set it directly, otherwise use
  280. * saved IV.
  281. */
  282. if (iv == NULL && gctx->iv_set)
  283. iv = gctx->iv;
  284. if (iv)
  285. {
  286. CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
  287. gctx->iv_set = 1;
  288. }
  289. gctx->key_set = 1;
  290. }
  291. else
  292. {
  293. /* If key set use IV, otherwise copy */
  294. if (gctx->key_set)
  295. CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
  296. else
  297. memcpy(gctx->iv, iv, gctx->ivlen);
  298. gctx->iv_set = 1;
  299. gctx->iv_gen = 0;
  300. }
  301. return 1;
  302. }
  303. #define aesni_gcm_cipher aes_gcm_cipher
  304. static int aesni_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  305. const unsigned char *in, size_t len);
  306. static int aesni_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  307. const unsigned char *iv, int enc)
  308. {
  309. EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
  310. if (!iv && !key)
  311. return 1;
  312. if (key)
  313. {
  314. /* key_len is two AES keys */
  315. if (enc)
  316. {
  317. aesni_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1);
  318. xctx->xts.block1 = (block128_f)aesni_encrypt;
  319. xctx->stream = aesni_xts_encrypt;
  320. }
  321. else
  322. {
  323. aesni_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1);
  324. xctx->xts.block1 = (block128_f)aesni_decrypt;
  325. xctx->stream = aesni_xts_decrypt;
  326. }
  327. aesni_set_encrypt_key(key + ctx->key_len/2,
  328. ctx->key_len * 4, &xctx->ks2);
  329. xctx->xts.block2 = (block128_f)aesni_encrypt;
  330. xctx->xts.key1 = &xctx->ks1;
  331. }
  332. if (iv)
  333. {
  334. xctx->xts.key2 = &xctx->ks2;
  335. memcpy(ctx->iv, iv, 16);
  336. }
  337. return 1;
  338. }
  339. #define aesni_xts_cipher aes_xts_cipher
  340. static int aesni_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  341. const unsigned char *in, size_t len);
  342. static int aesni_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  343. const unsigned char *iv, int enc)
  344. {
  345. EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
  346. if (!iv && !key)
  347. return 1;
  348. if (key)
  349. {
  350. aesni_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks);
  351. CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
  352. &cctx->ks, (block128_f)aesni_encrypt);
  353. cctx->str = enc?(ccm128_f)aesni_ccm64_encrypt_blocks :
  354. (ccm128_f)aesni_ccm64_decrypt_blocks;
  355. cctx->key_set = 1;
  356. }
  357. if (iv)
  358. {
  359. memcpy(ctx->iv, iv, 15 - cctx->L);
  360. cctx->iv_set = 1;
  361. }
  362. return 1;
  363. }
  364. #define aesni_ccm_cipher aes_ccm_cipher
  365. static int aesni_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  366. const unsigned char *in, size_t len);
  367. #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
  368. static const EVP_CIPHER aesni_##keylen##_##mode = { \
  369. nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
  370. flags|EVP_CIPH_##MODE##_MODE, \
  371. aesni_init_key, \
  372. aesni_##mode##_cipher, \
  373. NULL, \
  374. sizeof(EVP_AES_KEY), \
  375. NULL,NULL,NULL,NULL }; \
  376. static const EVP_CIPHER aes_##keylen##_##mode = { \
  377. nid##_##keylen##_##nmode,blocksize, \
  378. keylen/8,ivlen, \
  379. flags|EVP_CIPH_##MODE##_MODE, \
  380. aes_init_key, \
  381. aes_##mode##_cipher, \
  382. NULL, \
  383. sizeof(EVP_AES_KEY), \
  384. NULL,NULL,NULL,NULL }; \
  385. const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
  386. { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
  387. #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
  388. static const EVP_CIPHER aesni_##keylen##_##mode = { \
  389. nid##_##keylen##_##mode,blocksize, \
  390. (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
  391. flags|EVP_CIPH_##MODE##_MODE, \
  392. aesni_##mode##_init_key, \
  393. aesni_##mode##_cipher, \
  394. aes_##mode##_cleanup, \
  395. sizeof(EVP_AES_##MODE##_CTX), \
  396. NULL,NULL,aes_##mode##_ctrl,NULL }; \
  397. static const EVP_CIPHER aes_##keylen##_##mode = { \
  398. nid##_##keylen##_##mode,blocksize, \
  399. (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
  400. flags|EVP_CIPH_##MODE##_MODE, \
  401. aes_##mode##_init_key, \
  402. aes_##mode##_cipher, \
  403. aes_##mode##_cleanup, \
  404. sizeof(EVP_AES_##MODE##_CTX), \
  405. NULL,NULL,aes_##mode##_ctrl,NULL }; \
  406. const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
  407. { return AESNI_CAPABLE?&aesni_##keylen##_##mode:&aes_##keylen##_##mode; }
  408. #else
  409. #define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
  410. static const EVP_CIPHER aes_##keylen##_##mode = { \
  411. nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
  412. flags|EVP_CIPH_##MODE##_MODE, \
  413. aes_init_key, \
  414. aes_##mode##_cipher, \
  415. NULL, \
  416. sizeof(EVP_AES_KEY), \
  417. NULL,NULL,NULL,NULL }; \
  418. const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
  419. { return &aes_##keylen##_##mode; }
  420. #define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags) \
  421. static const EVP_CIPHER aes_##keylen##_##mode = { \
  422. nid##_##keylen##_##mode,blocksize, \
  423. (EVP_CIPH_##MODE##_MODE==EVP_CIPH_XTS_MODE?2:1)*keylen/8, ivlen, \
  424. flags|EVP_CIPH_##MODE##_MODE, \
  425. aes_##mode##_init_key, \
  426. aes_##mode##_cipher, \
  427. aes_##mode##_cleanup, \
  428. sizeof(EVP_AES_##MODE##_CTX), \
  429. NULL,NULL,aes_##mode##_ctrl,NULL }; \
  430. const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
  431. { return &aes_##keylen##_##mode; }
  432. #endif
  433. #if defined(OPENSSL_CPUID_OBJ) && defined(__aarch64__)
  434. #include "arm_arch.h"
  435. #if __ARM_ARCH__>=7
  436. # define HWAES_CAPABLE (OPENSSL_armcap_P & ARMV8_AES)
  437. # define HWAES_set_encrypt_key aes_v8_set_encrypt_key
  438. # define HWAES_set_decrypt_key aes_v8_set_decrypt_key
  439. # define HWAES_encrypt aes_v8_encrypt
  440. # define HWAES_decrypt aes_v8_decrypt
  441. # define HWAES_cbc_encrypt aes_v8_cbc_encrypt
  442. # define HWAES_ctr32_encrypt_blocks aes_v8_ctr32_encrypt_blocks
  443. #endif
  444. #endif
  445. #if defined(HWAES_CAPABLE)
  446. int HWAES_set_encrypt_key(const unsigned char *userKey, const int bits,
  447. AES_KEY *key);
  448. int HWAES_set_decrypt_key(const unsigned char *userKey, const int bits,
  449. AES_KEY *key);
  450. void HWAES_encrypt(const unsigned char *in, unsigned char *out,
  451. const AES_KEY *key);
  452. void HWAES_decrypt(const unsigned char *in, unsigned char *out,
  453. const AES_KEY *key);
  454. void HWAES_cbc_encrypt(const unsigned char *in, unsigned char *out,
  455. size_t length, const AES_KEY *key,
  456. unsigned char *ivec, const int enc);
  457. void HWAES_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
  458. size_t len, const AES_KEY *key, const unsigned char ivec[16]);
  459. void HWAES_xts_encrypt(const unsigned char *inp, unsigned char *out,
  460. size_t len, const AES_KEY *key1,
  461. const AES_KEY *key2, const unsigned char iv[16]);
  462. void HWAES_xts_decrypt(const unsigned char *inp, unsigned char *out,
  463. size_t len, const AES_KEY *key1,
  464. const AES_KEY *key2, const unsigned char iv[16]);
  465. #endif
  466. #define BLOCK_CIPHER_generic_pack(nid,keylen,flags) \
  467. BLOCK_CIPHER_generic(nid,keylen,16,16,cbc,cbc,CBC,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
  468. BLOCK_CIPHER_generic(nid,keylen,16,0,ecb,ecb,ECB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
  469. BLOCK_CIPHER_generic(nid,keylen,1,16,ofb128,ofb,OFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
  470. BLOCK_CIPHER_generic(nid,keylen,1,16,cfb128,cfb,CFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
  471. BLOCK_CIPHER_generic(nid,keylen,1,16,cfb1,cfb1,CFB,flags) \
  472. BLOCK_CIPHER_generic(nid,keylen,1,16,cfb8,cfb8,CFB,flags) \
  473. BLOCK_CIPHER_generic(nid,keylen,1,16,ctr,ctr,CTR,flags)
  474. static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  475. const unsigned char *iv, int enc)
  476. {
  477. int ret, mode;
  478. EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
  479. mode = ctx->cipher->flags & EVP_CIPH_MODE;
  480. if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
  481. && !enc)
  482. #ifdef HWAES_CAPABLE
  483. if (HWAES_CAPABLE)
  484. {
  485. ret = HWAES_set_decrypt_key(key,ctx->key_len*8,&dat->ks);
  486. dat->block = (block128_f)HWAES_decrypt;
  487. dat->stream.cbc = NULL;
  488. #ifdef HWAES_cbc_encrypt
  489. if (mode==EVP_CIPH_CBC_MODE)
  490. dat->stream.cbc = (cbc128_f)HWAES_cbc_encrypt;
  491. #endif
  492. }
  493. else
  494. #endif
  495. #ifdef BSAES_CAPABLE
  496. if (BSAES_CAPABLE && mode==EVP_CIPH_CBC_MODE)
  497. {
  498. ret = AES_set_decrypt_key(key,ctx->key_len*8,&dat->ks);
  499. dat->block = (block128_f)AES_decrypt;
  500. dat->stream.cbc = (cbc128_f)bsaes_cbc_encrypt;
  501. }
  502. else
  503. #endif
  504. #ifdef VPAES_CAPABLE
  505. if (VPAES_CAPABLE)
  506. {
  507. ret = vpaes_set_decrypt_key(key,ctx->key_len*8,&dat->ks);
  508. dat->block = (block128_f)vpaes_decrypt;
  509. dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
  510. (cbc128_f)vpaes_cbc_encrypt :
  511. NULL;
  512. }
  513. else
  514. #endif
  515. {
  516. ret = AES_set_decrypt_key(key,ctx->key_len*8,&dat->ks);
  517. dat->block = (block128_f)AES_decrypt;
  518. dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
  519. (cbc128_f)AES_cbc_encrypt :
  520. NULL;
  521. }
  522. else
  523. #ifdef HWAES_CAPABLE
  524. if (HWAES_CAPABLE)
  525. {
  526. ret = HWAES_set_encrypt_key(key,ctx->key_len*8,&dat->ks);
  527. dat->block = (block128_f)HWAES_encrypt;
  528. dat->stream.cbc = NULL;
  529. #ifdef HWAES_cbc_encrypt
  530. if (mode==EVP_CIPH_CBC_MODE)
  531. dat->stream.cbc = (cbc128_f)HWAES_cbc_encrypt;
  532. else
  533. #endif
  534. #ifdef HWAES_ctr32_encrypt_blocks
  535. if (mode==EVP_CIPH_CTR_MODE)
  536. dat->stream.ctr = (ctr128_f)HWAES_ctr32_encrypt_blocks;
  537. else
  538. #endif
  539. (void)0; /* terminate potentially open 'else' */
  540. }
  541. else
  542. #endif
  543. #ifdef BSAES_CAPABLE
  544. if (BSAES_CAPABLE && mode==EVP_CIPH_CTR_MODE)
  545. {
  546. ret = AES_set_encrypt_key(key,ctx->key_len*8,&dat->ks);
  547. dat->block = (block128_f)AES_encrypt;
  548. dat->stream.ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks;
  549. }
  550. else
  551. #endif
  552. #ifdef VPAES_CAPABLE
  553. if (VPAES_CAPABLE)
  554. {
  555. ret = vpaes_set_encrypt_key(key,ctx->key_len*8,&dat->ks);
  556. dat->block = (block128_f)vpaes_encrypt;
  557. dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
  558. (cbc128_f)vpaes_cbc_encrypt :
  559. NULL;
  560. }
  561. else
  562. #endif
  563. {
  564. ret = AES_set_encrypt_key(key,ctx->key_len*8,&dat->ks);
  565. dat->block = (block128_f)AES_encrypt;
  566. dat->stream.cbc = mode==EVP_CIPH_CBC_MODE ?
  567. (cbc128_f)AES_cbc_encrypt :
  568. NULL;
  569. #ifdef AES_CTR_ASM
  570. if (mode==EVP_CIPH_CTR_MODE)
  571. dat->stream.ctr = (ctr128_f)AES_ctr32_encrypt;
  572. #endif
  573. }
  574. if(ret < 0)
  575. {
  576. EVPerr(EVP_F_AES_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
  577. return 0;
  578. }
  579. return 1;
  580. }
  581. static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
  582. const unsigned char *in, size_t len)
  583. {
  584. EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
  585. if (dat->stream.cbc)
  586. (*dat->stream.cbc)(in,out,len,&dat->ks,ctx->iv,ctx->encrypt);
  587. else if (ctx->encrypt)
  588. CRYPTO_cbc128_encrypt(in,out,len,&dat->ks,ctx->iv,dat->block);
  589. else
  590. CRYPTO_cbc128_encrypt(in,out,len,&dat->ks,ctx->iv,dat->block);
  591. return 1;
  592. }
  593. static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
  594. const unsigned char *in, size_t len)
  595. {
  596. size_t bl = ctx->cipher->block_size;
  597. size_t i;
  598. EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
  599. if (len<bl) return 1;
  600. for (i=0,len-=bl;i<=len;i+=bl)
  601. (*dat->block)(in+i,out+i,&dat->ks);
  602. return 1;
  603. }
  604. static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
  605. const unsigned char *in,size_t len)
  606. {
  607. EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
  608. CRYPTO_ofb128_encrypt(in,out,len,&dat->ks,
  609. ctx->iv,&ctx->num,dat->block);
  610. return 1;
  611. }
  612. static int aes_cfb_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
  613. const unsigned char *in,size_t len)
  614. {
  615. EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
  616. CRYPTO_cfb128_encrypt(in,out,len,&dat->ks,
  617. ctx->iv,&ctx->num,ctx->encrypt,dat->block);
  618. return 1;
  619. }
  620. static int aes_cfb8_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
  621. const unsigned char *in,size_t len)
  622. {
  623. EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
  624. CRYPTO_cfb128_8_encrypt(in,out,len,&dat->ks,
  625. ctx->iv,&ctx->num,ctx->encrypt,dat->block);
  626. return 1;
  627. }
  628. static int aes_cfb1_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
  629. const unsigned char *in,size_t len)
  630. {
  631. EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
  632. if (ctx->flags&EVP_CIPH_FLAG_LENGTH_BITS) {
  633. CRYPTO_cfb128_1_encrypt(in,out,len,&dat->ks,
  634. ctx->iv,&ctx->num,ctx->encrypt,dat->block);
  635. return 1;
  636. }
  637. while (len>=MAXBITCHUNK) {
  638. CRYPTO_cfb128_1_encrypt(in,out,MAXBITCHUNK*8,&dat->ks,
  639. ctx->iv,&ctx->num,ctx->encrypt,dat->block);
  640. len-=MAXBITCHUNK;
  641. }
  642. if (len)
  643. CRYPTO_cfb128_1_encrypt(in,out,len*8,&dat->ks,
  644. ctx->iv,&ctx->num,ctx->encrypt,dat->block);
  645. return 1;
  646. }
  647. static int aes_ctr_cipher (EVP_CIPHER_CTX *ctx, unsigned char *out,
  648. const unsigned char *in, size_t len)
  649. {
  650. unsigned int num = ctx->num;
  651. EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
  652. if (dat->stream.ctr)
  653. CRYPTO_ctr128_encrypt_ctr32(in,out,len,&dat->ks,
  654. ctx->iv,ctx->buf,&num,dat->stream.ctr);
  655. else
  656. CRYPTO_ctr128_encrypt(in,out,len,&dat->ks,
  657. ctx->iv,ctx->buf,&num,dat->block);
  658. ctx->num = (size_t)num;
  659. return 1;
  660. }
  661. BLOCK_CIPHER_generic_pack(NID_aes,128,EVP_CIPH_FLAG_FIPS)
  662. BLOCK_CIPHER_generic_pack(NID_aes,192,EVP_CIPH_FLAG_FIPS)
  663. BLOCK_CIPHER_generic_pack(NID_aes,256,EVP_CIPH_FLAG_FIPS)
  664. static int aes_gcm_cleanup(EVP_CIPHER_CTX *c)
  665. {
  666. EVP_AES_GCM_CTX *gctx = c->cipher_data;
  667. OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm));
  668. if (gctx->iv != c->iv)
  669. OPENSSL_free(gctx->iv);
  670. return 1;
  671. }
  672. /* increment counter (64-bit int) by 1 */
  673. static void ctr64_inc(unsigned char *counter) {
  674. int n=8;
  675. unsigned char c;
  676. do {
  677. --n;
  678. c = counter[n];
  679. ++c;
  680. counter[n] = c;
  681. if (c) return;
  682. } while (n);
  683. }
  684. static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
  685. {
  686. EVP_AES_GCM_CTX *gctx = c->cipher_data;
  687. switch (type)
  688. {
  689. case EVP_CTRL_INIT:
  690. gctx->key_set = 0;
  691. gctx->iv_set = 0;
  692. gctx->ivlen = c->cipher->iv_len;
  693. gctx->iv = c->iv;
  694. gctx->taglen = -1;
  695. gctx->iv_gen = 0;
  696. gctx->tls_aad_len = -1;
  697. return 1;
  698. case EVP_CTRL_GCM_SET_IVLEN:
  699. if (arg <= 0)
  700. return 0;
  701. #ifdef OPENSSL_FIPS
  702. if (FIPS_module_mode() && !(c->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW)
  703. && arg < 12)
  704. return 0;
  705. #endif
  706. /* Allocate memory for IV if needed */
  707. if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen))
  708. {
  709. if (gctx->iv != c->iv)
  710. OPENSSL_free(gctx->iv);
  711. gctx->iv = OPENSSL_malloc(arg);
  712. if (!gctx->iv)
  713. return 0;
  714. }
  715. gctx->ivlen = arg;
  716. return 1;
  717. case EVP_CTRL_GCM_SET_TAG:
  718. if (arg <= 0 || arg > 16 || c->encrypt)
  719. return 0;
  720. memcpy(c->buf, ptr, arg);
  721. gctx->taglen = arg;
  722. return 1;
  723. case EVP_CTRL_GCM_GET_TAG:
  724. if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0)
  725. return 0;
  726. memcpy(ptr, c->buf, arg);
  727. return 1;
  728. case EVP_CTRL_GCM_SET_IV_FIXED:
  729. /* Special case: -1 length restores whole IV */
  730. if (arg == -1)
  731. {
  732. memcpy(gctx->iv, ptr, gctx->ivlen);
  733. gctx->iv_gen = 1;
  734. return 1;
  735. }
  736. /* Fixed field must be at least 4 bytes and invocation field
  737. * at least 8.
  738. */
  739. if ((arg < 4) || (gctx->ivlen - arg) < 8)
  740. return 0;
  741. if (arg)
  742. memcpy(gctx->iv, ptr, arg);
  743. if (c->encrypt &&
  744. RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
  745. return 0;
  746. gctx->iv_gen = 1;
  747. return 1;
  748. case EVP_CTRL_GCM_IV_GEN:
  749. if (gctx->iv_gen == 0 || gctx->key_set == 0)
  750. return 0;
  751. CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
  752. if (arg <= 0 || arg > gctx->ivlen)
  753. arg = gctx->ivlen;
  754. memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
  755. /* Invocation field will be at least 8 bytes in size and
  756. * so no need to check wrap around or increment more than
  757. * last 8 bytes.
  758. */
  759. ctr64_inc(gctx->iv + gctx->ivlen - 8);
  760. gctx->iv_set = 1;
  761. return 1;
  762. case EVP_CTRL_GCM_SET_IV_INV:
  763. if (gctx->iv_gen == 0 || gctx->key_set == 0 || c->encrypt)
  764. return 0;
  765. memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
  766. CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
  767. gctx->iv_set = 1;
  768. return 1;
  769. case EVP_CTRL_AEAD_TLS1_AAD:
  770. /* Save the AAD for later use */
  771. if (arg != 13)
  772. return 0;
  773. memcpy(c->buf, ptr, arg);
  774. gctx->tls_aad_len = arg;
  775. {
  776. unsigned int len=c->buf[arg-2]<<8|c->buf[arg-1];
  777. /* Correct length for explicit IV */
  778. len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
  779. /* If decrypting correct for tag too */
  780. if (!c->encrypt)
  781. len -= EVP_GCM_TLS_TAG_LEN;
  782. c->buf[arg-2] = len>>8;
  783. c->buf[arg-1] = len & 0xff;
  784. }
  785. /* Extra padding: tag appended to record */
  786. return EVP_GCM_TLS_TAG_LEN;
  787. default:
  788. return -1;
  789. }
  790. }
  791. static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  792. const unsigned char *iv, int enc)
  793. {
  794. EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
  795. if (!iv && !key)
  796. return 1;
  797. if (key)
  798. { do {
  799. #ifdef HWAES_CAPABLE
  800. if (HWAES_CAPABLE)
  801. {
  802. HWAES_set_encrypt_key(key,ctx->key_len*8,&gctx->ks);
  803. CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks,
  804. (block128_f)HWAES_encrypt);
  805. #ifdef HWAES_ctr32_encrypt_blocks
  806. gctx->ctr = (ctr128_f)HWAES_ctr32_encrypt_blocks;
  807. #else
  808. gctx->ctr = NULL;
  809. #endif
  810. break;
  811. }
  812. else
  813. #endif
  814. #ifdef BSAES_CAPABLE
  815. if (BSAES_CAPABLE)
  816. {
  817. AES_set_encrypt_key(key,ctx->key_len*8,&gctx->ks);
  818. CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks,
  819. (block128_f)AES_encrypt);
  820. gctx->ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks;
  821. break;
  822. }
  823. else
  824. #endif
  825. #ifdef VPAES_CAPABLE
  826. if (VPAES_CAPABLE)
  827. {
  828. vpaes_set_encrypt_key(key,ctx->key_len*8,&gctx->ks);
  829. CRYPTO_gcm128_init(&gctx->gcm,&gctx->ks,
  830. (block128_f)vpaes_encrypt);
  831. gctx->ctr = NULL;
  832. break;
  833. }
  834. #endif
  835. AES_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks);
  836. CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, (block128_f)AES_encrypt);
  837. #ifdef AES_CTR_ASM
  838. gctx->ctr = (ctr128_f)AES_ctr32_encrypt;
  839. #else
  840. gctx->ctr = NULL;
  841. #endif
  842. } while (0);
  843. /* If we have an iv can set it directly, otherwise use
  844. * saved IV.
  845. */
  846. if (iv == NULL && gctx->iv_set)
  847. iv = gctx->iv;
  848. if (iv)
  849. {
  850. CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
  851. gctx->iv_set = 1;
  852. }
  853. gctx->key_set = 1;
  854. }
  855. else
  856. {
  857. /* If key set use IV, otherwise copy */
  858. if (gctx->key_set)
  859. CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
  860. else
  861. memcpy(gctx->iv, iv, gctx->ivlen);
  862. gctx->iv_set = 1;
  863. gctx->iv_gen = 0;
  864. }
  865. return 1;
  866. }
  867. /* Handle TLS GCM packet format. This consists of the last portion of the IV
  868. * followed by the payload and finally the tag. On encrypt generate IV,
  869. * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
  870. * and verify tag.
  871. */
  872. static int aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  873. const unsigned char *in, size_t len)
  874. {
  875. EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
  876. int rv = -1;
  877. /* Encrypt/decrypt must be performed in place */
  878. if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN+EVP_GCM_TLS_TAG_LEN))
  879. return -1;
  880. /* Set IV from start of buffer or generate IV and write to start
  881. * of buffer.
  882. */
  883. if (EVP_CIPHER_CTX_ctrl(ctx, ctx->encrypt ?
  884. EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV,
  885. EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
  886. goto err;
  887. /* Use saved AAD */
  888. if (CRYPTO_gcm128_aad(&gctx->gcm, ctx->buf, gctx->tls_aad_len))
  889. goto err;
  890. /* Fix buffer and length to point to payload */
  891. in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  892. out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  893. len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
  894. if (ctx->encrypt)
  895. {
  896. /* Encrypt payload */
  897. if (gctx->ctr)
  898. {
  899. if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
  900. in, out, len,
  901. gctx->ctr))
  902. goto err;
  903. }
  904. else {
  905. if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len))
  906. goto err;
  907. }
  908. out += len;
  909. /* Finally write tag */
  910. CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN);
  911. rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
  912. }
  913. else
  914. {
  915. /* Decrypt */
  916. if (gctx->ctr)
  917. {
  918. if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
  919. in, out, len,
  920. gctx->ctr))
  921. goto err;
  922. }
  923. else {
  924. if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len))
  925. goto err;
  926. }
  927. /* Retrieve tag */
  928. CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf,
  929. EVP_GCM_TLS_TAG_LEN);
  930. /* If tag mismatch wipe buffer */
  931. if (memcmp(ctx->buf, in + len, EVP_GCM_TLS_TAG_LEN))
  932. {
  933. OPENSSL_cleanse(out, len);
  934. goto err;
  935. }
  936. rv = len;
  937. }
  938. err:
  939. gctx->iv_set = 0;
  940. gctx->tls_aad_len = -1;
  941. return rv;
  942. }
  943. static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  944. const unsigned char *in, size_t len)
  945. {
  946. EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
  947. /* If not set up, return error */
  948. if (!gctx->key_set)
  949. return -1;
  950. if (gctx->tls_aad_len >= 0)
  951. return aes_gcm_tls_cipher(ctx, out, in, len);
  952. if (!gctx->iv_set)
  953. return -1;
  954. if (in)
  955. {
  956. if (out == NULL)
  957. {
  958. if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
  959. return -1;
  960. }
  961. else if (ctx->encrypt)
  962. {
  963. if (gctx->ctr)
  964. {
  965. if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,
  966. in, out, len,
  967. gctx->ctr))
  968. return -1;
  969. }
  970. else {
  971. if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len))
  972. return -1;
  973. }
  974. }
  975. else
  976. {
  977. if (gctx->ctr)
  978. {
  979. if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,
  980. in, out, len,
  981. gctx->ctr))
  982. return -1;
  983. }
  984. else {
  985. if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len))
  986. return -1;
  987. }
  988. }
  989. return len;
  990. }
  991. else
  992. {
  993. if (!ctx->encrypt)
  994. {
  995. if (gctx->taglen < 0)
  996. return -1;
  997. if (CRYPTO_gcm128_finish(&gctx->gcm,
  998. ctx->buf, gctx->taglen) != 0)
  999. return -1;
  1000. gctx->iv_set = 0;
  1001. return 0;
  1002. }
  1003. CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16);
  1004. gctx->taglen = 16;
  1005. /* Don't reuse the IV */
  1006. gctx->iv_set = 0;
  1007. return 0;
  1008. }
  1009. }
  1010. #define CUSTOM_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \
  1011. | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
  1012. | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT)
  1013. BLOCK_CIPHER_custom(NID_aes,128,1,12,gcm,GCM,
  1014. EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
  1015. BLOCK_CIPHER_custom(NID_aes,192,1,12,gcm,GCM,
  1016. EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
  1017. BLOCK_CIPHER_custom(NID_aes,256,1,12,gcm,GCM,
  1018. EVP_CIPH_FLAG_FIPS|EVP_CIPH_FLAG_AEAD_CIPHER|CUSTOM_FLAGS)
  1019. static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
  1020. {
  1021. EVP_AES_XTS_CTX *xctx = c->cipher_data;
  1022. if (type != EVP_CTRL_INIT)
  1023. return -1;
  1024. /* key1 and key2 are used as an indicator both key and IV are set */
  1025. xctx->xts.key1 = NULL;
  1026. xctx->xts.key2 = NULL;
  1027. return 1;
  1028. }
  1029. static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  1030. const unsigned char *iv, int enc)
  1031. {
  1032. EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
  1033. if (!iv && !key)
  1034. return 1;
  1035. if (key) do
  1036. {
  1037. xctx->stream = NULL;
  1038. /* key_len is two AES keys */
  1039. #ifdef HWAES_CAPABLE
  1040. if (HWAES_CAPABLE)
  1041. {
  1042. if (enc)
  1043. {
  1044. HWAES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1);
  1045. xctx->xts.block1 = (block128_f)HWAES_encrypt;
  1046. #ifdef HWAES_xts_encrypt
  1047. xctx->stream = HWAES_xts_encrypt;
  1048. #endif
  1049. }
  1050. else
  1051. {
  1052. HWAES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1);
  1053. xctx->xts.block1 = (block128_f)HWAES_decrypt;
  1054. #ifdef HWAES_xts_decrypt
  1055. xctx->stream = HWAES_xts_decrypt;
  1056. #endif
  1057. }
  1058. HWAES_set_encrypt_key(key + ctx->key_len/2,
  1059. ctx->key_len * 4, &xctx->ks2);
  1060. xctx->xts.block2 = (block128_f)HWAES_encrypt;
  1061. xctx->xts.key1 = &xctx->ks1;
  1062. break;
  1063. }
  1064. else
  1065. #endif
  1066. #ifdef VPAES_CAPABLE
  1067. if (VPAES_CAPABLE)
  1068. {
  1069. if (enc)
  1070. {
  1071. vpaes_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1);
  1072. xctx->xts.block1 = (block128_f)vpaes_encrypt;
  1073. }
  1074. else
  1075. {
  1076. vpaes_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1);
  1077. xctx->xts.block1 = (block128_f)vpaes_decrypt;
  1078. }
  1079. vpaes_set_encrypt_key(key + ctx->key_len/2,
  1080. ctx->key_len * 4, &xctx->ks2);
  1081. xctx->xts.block2 = (block128_f)vpaes_encrypt;
  1082. xctx->xts.key1 = &xctx->ks1;
  1083. break;
  1084. }
  1085. #endif
  1086. if (enc)
  1087. {
  1088. AES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1);
  1089. xctx->xts.block1 = (block128_f)AES_encrypt;
  1090. }
  1091. else
  1092. {
  1093. AES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1);
  1094. xctx->xts.block1 = (block128_f)AES_decrypt;
  1095. }
  1096. AES_set_encrypt_key(key + ctx->key_len/2,
  1097. ctx->key_len * 4, &xctx->ks2);
  1098. xctx->xts.block2 = (block128_f)AES_encrypt;
  1099. xctx->xts.key1 = &xctx->ks1;
  1100. } while (0);
  1101. if (iv)
  1102. {
  1103. xctx->xts.key2 = &xctx->ks2;
  1104. memcpy(ctx->iv, iv, 16);
  1105. }
  1106. return 1;
  1107. }
  1108. static int aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  1109. const unsigned char *in, size_t len)
  1110. {
  1111. EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
  1112. if (!xctx->xts.key1 || !xctx->xts.key2)
  1113. return 0;
  1114. if (!out || !in)
  1115. return 0;
  1116. #ifdef OPENSSL_FIPS
  1117. /* Requirement of SP800-38E */
  1118. if (FIPS_module_mode() && !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW) &&
  1119. (len > (1UL<<20)*16))
  1120. {
  1121. EVPerr(EVP_F_AES_XTS_CIPHER, EVP_R_TOO_LARGE);
  1122. return 0;
  1123. }
  1124. #endif
  1125. if (xctx->stream)
  1126. (*xctx->stream)(in, out, len,
  1127. xctx->xts.key1, xctx->xts.key2, ctx->iv);
  1128. else if (CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len,
  1129. ctx->encrypt))
  1130. return 0;
  1131. return 1;
  1132. }
  1133. #define aes_xts_cleanup NULL
  1134. #define XTS_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \
  1135. | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT)
  1136. BLOCK_CIPHER_custom(NID_aes,128,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS)
  1137. BLOCK_CIPHER_custom(NID_aes,256,1,16,xts,XTS,EVP_CIPH_FLAG_FIPS|XTS_FLAGS)
  1138. static int aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
  1139. {
  1140. EVP_AES_CCM_CTX *cctx = c->cipher_data;
  1141. switch (type)
  1142. {
  1143. case EVP_CTRL_INIT:
  1144. cctx->key_set = 0;
  1145. cctx->iv_set = 0;
  1146. cctx->L = 8;
  1147. cctx->M = 12;
  1148. cctx->tag_set = 0;
  1149. cctx->len_set = 0;
  1150. return 1;
  1151. case EVP_CTRL_CCM_SET_IVLEN:
  1152. arg = 15 - arg;
  1153. case EVP_CTRL_CCM_SET_L:
  1154. if (arg < 2 || arg > 8)
  1155. return 0;
  1156. cctx->L = arg;
  1157. return 1;
  1158. case EVP_CTRL_CCM_SET_TAG:
  1159. if ((arg & 1) || arg < 4 || arg > 16)
  1160. return 0;
  1161. if ((c->encrypt && ptr) || (!c->encrypt && !ptr))
  1162. return 0;
  1163. if (ptr)
  1164. {
  1165. cctx->tag_set = 1;
  1166. memcpy(c->buf, ptr, arg);
  1167. }
  1168. cctx->M = arg;
  1169. return 1;
  1170. case EVP_CTRL_CCM_GET_TAG:
  1171. if (!c->encrypt || !cctx->tag_set)
  1172. return 0;
  1173. if(!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg))
  1174. return 0;
  1175. cctx->tag_set = 0;
  1176. cctx->iv_set = 0;
  1177. cctx->len_set = 0;
  1178. return 1;
  1179. default:
  1180. return -1;
  1181. }
  1182. }
  1183. static int aes_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  1184. const unsigned char *iv, int enc)
  1185. {
  1186. EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
  1187. if (!iv && !key)
  1188. return 1;
  1189. if (key) do
  1190. {
  1191. #ifdef HWAES_CAPABLE
  1192. if (HWAES_CAPABLE)
  1193. {
  1194. HWAES_set_encrypt_key(key,ctx->key_len*8,&cctx->ks);
  1195. CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
  1196. &cctx->ks, (block128_f)HWAES_encrypt);
  1197. cctx->str = NULL;
  1198. cctx->key_set = 1;
  1199. break;
  1200. }
  1201. else
  1202. #endif
  1203. #ifdef VPAES_CAPABLE
  1204. if (VPAES_CAPABLE)
  1205. {
  1206. vpaes_set_encrypt_key(key, ctx->key_len*8, &cctx->ks);
  1207. CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
  1208. &cctx->ks, (block128_f)vpaes_encrypt);
  1209. cctx->key_set = 1;
  1210. break;
  1211. }
  1212. #endif
  1213. AES_set_encrypt_key(key, ctx->key_len * 8, &cctx->ks);
  1214. CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
  1215. &cctx->ks, (block128_f)AES_encrypt);
  1216. cctx->str = NULL;
  1217. cctx->key_set = 1;
  1218. } while (0);
  1219. if (iv)
  1220. {
  1221. memcpy(ctx->iv, iv, 15 - cctx->L);
  1222. cctx->iv_set = 1;
  1223. }
  1224. return 1;
  1225. }
  1226. static int aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  1227. const unsigned char *in, size_t len)
  1228. {
  1229. EVP_AES_CCM_CTX *cctx = ctx->cipher_data;
  1230. CCM128_CONTEXT *ccm = &cctx->ccm;
  1231. /* If not set up, return error */
  1232. if (!cctx->iv_set && !cctx->key_set)
  1233. return -1;
  1234. if (!ctx->encrypt && !cctx->tag_set)
  1235. return -1;
  1236. if (!out)
  1237. {
  1238. if (!in)
  1239. {
  1240. if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L,len))
  1241. return -1;
  1242. cctx->len_set = 1;
  1243. return len;
  1244. }
  1245. /* If have AAD need message length */
  1246. if (!cctx->len_set && len)
  1247. return -1;
  1248. CRYPTO_ccm128_aad(ccm, in, len);
  1249. return len;
  1250. }
  1251. /* EVP_*Final() doesn't return any data */
  1252. if (!in)
  1253. return 0;
  1254. /* If not set length yet do it */
  1255. if (!cctx->len_set)
  1256. {
  1257. if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))
  1258. return -1;
  1259. cctx->len_set = 1;
  1260. }
  1261. if (ctx->encrypt)
  1262. {
  1263. if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len,
  1264. cctx->str) :
  1265. CRYPTO_ccm128_encrypt(ccm, in, out, len))
  1266. return -1;
  1267. cctx->tag_set = 1;
  1268. return len;
  1269. }
  1270. else
  1271. {
  1272. int rv = -1;
  1273. if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len,
  1274. cctx->str) :
  1275. !CRYPTO_ccm128_decrypt(ccm, in, out, len))
  1276. {
  1277. unsigned char tag[16];
  1278. if (CRYPTO_ccm128_tag(ccm, tag, cctx->M))
  1279. {
  1280. if (!memcmp(tag, ctx->buf, cctx->M))
  1281. rv = len;
  1282. }
  1283. }
  1284. if (rv == -1)
  1285. OPENSSL_cleanse(out, len);
  1286. cctx->iv_set = 0;
  1287. cctx->tag_set = 0;
  1288. cctx->len_set = 0;
  1289. return rv;
  1290. }
  1291. }
  1292. #define aes_ccm_cleanup NULL
  1293. BLOCK_CIPHER_custom(NID_aes,128,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
  1294. BLOCK_CIPHER_custom(NID_aes,192,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
  1295. BLOCK_CIPHER_custom(NID_aes,256,1,12,ccm,CCM,EVP_CIPH_FLAG_FIPS|CUSTOM_FLAGS)
  1296. #endif