Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

1786 linhas
57 KiB

  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/modes.h>
  56. #include <openssl/obj.h>
  57. #include <openssl/rand.h>
  58. #include <openssl/sha.h>
  59. #include "internal.h"
  60. #include "../internal.h"
  61. #include "../modes/internal.h"
  62. #if defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
  63. #include "../arm_arch.h"
  64. #endif
  65. typedef struct {
  66. union {
  67. double align;
  68. AES_KEY ks;
  69. } ks;
  70. block128_f block;
  71. union {
  72. cbc128_f cbc;
  73. ctr128_f ctr;
  74. } stream;
  75. } EVP_AES_KEY;
  76. typedef struct {
  77. union {
  78. double align;
  79. AES_KEY ks;
  80. } ks; /* AES key schedule to use */
  81. int key_set; /* Set if key initialised */
  82. int iv_set; /* Set if an iv is set */
  83. GCM128_CONTEXT gcm;
  84. uint8_t *iv; /* Temporary IV store */
  85. int ivlen; /* IV length */
  86. int taglen;
  87. int iv_gen; /* It is OK to generate IVs */
  88. ctr128_f ctr;
  89. } EVP_AES_GCM_CTX;
  90. #if !defined(OPENSSL_NO_ASM) && \
  91. (defined(OPENSSL_X86_64) || defined(OPENSSL_X86))
  92. #define VPAES
  93. extern unsigned int OPENSSL_ia32cap_P[];
  94. static char vpaes_capable(void) {
  95. return (OPENSSL_ia32cap_P[1] & (1 << (41 - 32))) != 0;
  96. }
  97. #if defined(OPENSSL_X86_64)
  98. #define BSAES
  99. static char bsaes_capable(void) {
  100. return vpaes_capable();
  101. }
  102. #endif
  103. #elif !defined(OPENSSL_NO_ASM) && \
  104. (defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64))
  105. #include "../arm_arch.h"
  106. #if defined(OPENSSL_ARM) && __ARM_ARCH__ >= 7
  107. #define BSAES
  108. static char bsaes_capable(void) {
  109. return CRYPTO_is_NEON_capable();
  110. }
  111. #endif
  112. #define HWAES
  113. static char hwaes_capable(void) {
  114. return (OPENSSL_armcap_P & ARMV8_AES) != 0;
  115. }
  116. int aes_v8_set_encrypt_key(const uint8_t *user_key, const int bits,
  117. AES_KEY *key);
  118. int aes_v8_set_decrypt_key(const uint8_t *user_key, const int bits,
  119. AES_KEY *key);
  120. void aes_v8_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
  121. void aes_v8_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
  122. void aes_v8_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
  123. const AES_KEY *key, uint8_t *ivec, const int enc);
  124. void aes_v8_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t len,
  125. const AES_KEY *key, const uint8_t ivec[16]);
  126. #endif /* OPENSSL_ARM */
  127. #if defined(BSAES)
  128. /* On platforms where BSAES gets defined (just above), then these functions are
  129. * provided by asm. */
  130. void bsaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
  131. const AES_KEY *key, uint8_t ivec[16], int enc);
  132. void bsaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t len,
  133. const AES_KEY *key, const uint8_t ivec[16]);
  134. #else
  135. static char bsaes_capable(void) {
  136. return 0;
  137. }
  138. /* On other platforms, bsaes_capable() will always return false and so the
  139. * following will never be called. */
  140. void bsaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
  141. const AES_KEY *key, uint8_t ivec[16], int enc) {
  142. abort();
  143. }
  144. void bsaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t len,
  145. const AES_KEY *key, const uint8_t ivec[16]) {
  146. abort();
  147. }
  148. #endif
  149. #if defined(VPAES)
  150. /* On platforms where VPAES gets defined (just above), then these functions are
  151. * provided by asm. */
  152. int vpaes_set_encrypt_key(const uint8_t *userKey, int bits, AES_KEY *key);
  153. int vpaes_set_decrypt_key(const uint8_t *userKey, int bits, AES_KEY *key);
  154. void vpaes_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
  155. void vpaes_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
  156. void vpaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
  157. const AES_KEY *key, uint8_t *ivec, int enc);
  158. #else
  159. static char vpaes_capable(void) {
  160. return 0;
  161. }
  162. /* On other platforms, vpaes_capable() will always return false and so the
  163. * following will never be called. */
  164. int vpaes_set_encrypt_key(const uint8_t *userKey, int bits, AES_KEY *key) {
  165. abort();
  166. }
  167. int vpaes_set_decrypt_key(const uint8_t *userKey, int bits, AES_KEY *key) {
  168. abort();
  169. }
  170. void vpaes_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key) {
  171. abort();
  172. }
  173. void vpaes_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key) {
  174. abort();
  175. }
  176. 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. int hwaes_capable(void) {
  185. return 0;
  186. }
  187. int aes_v8_set_encrypt_key(const uint8_t *user_key, int bits,
  188. AES_KEY *key) {
  189. abort();
  190. }
  191. int aes_v8_set_decrypt_key(const uint8_t *user_key, int bits, AES_KEY *key) {
  192. abort();
  193. }
  194. void aes_v8_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key) {
  195. abort();
  196. }
  197. void aes_v8_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key) {
  198. abort();
  199. }
  200. void aes_v8_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
  201. const AES_KEY *key, uint8_t *ivec, int enc) {
  202. abort();
  203. }
  204. void aes_v8_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t len,
  205. const AES_KEY *key, const uint8_t ivec[16]) {
  206. abort();
  207. }
  208. #endif
  209. #if !defined(OPENSSL_NO_ASM) && \
  210. (defined(OPENSSL_X86_64) || defined(OPENSSL_X86))
  211. int aesni_set_encrypt_key(const uint8_t *userKey, int bits, AES_KEY *key);
  212. int aesni_set_decrypt_key(const uint8_t *userKey, int bits, AES_KEY *key);
  213. void aesni_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
  214. void aesni_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key);
  215. void aesni_ecb_encrypt(const uint8_t *in, uint8_t *out, size_t length,
  216. const AES_KEY *key, int enc);
  217. void aesni_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
  218. const AES_KEY *key, uint8_t *ivec, int enc);
  219. void aesni_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t blocks,
  220. const void *key, const uint8_t *ivec);
  221. #if defined(OPENSSL_X86_64)
  222. size_t aesni_gcm_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  223. const void *key, uint8_t ivec[16], uint64_t *Xi);
  224. #define AES_gcm_encrypt aesni_gcm_encrypt
  225. size_t aesni_gcm_decrypt(const uint8_t *in, uint8_t *out, size_t len,
  226. const void *key, uint8_t ivec[16], uint64_t *Xi);
  227. #define AES_gcm_decrypt aesni_gcm_decrypt
  228. void gcm_ghash_avx(uint64_t Xi[2], const u128 Htable[16], const uint8_t *in,
  229. size_t len);
  230. #define AES_GCM_ASM(gctx) \
  231. (gctx->ctr == aesni_ctr32_encrypt_blocks && gctx->gcm.ghash == gcm_ghash_avx)
  232. #endif /* OPENSSL_X86_64 */
  233. #else
  234. /* On other platforms, aesni_capable() will always return false and so the
  235. * following will never be called. */
  236. void aesni_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key) {
  237. abort();
  238. }
  239. int aesni_set_encrypt_key(const uint8_t *userKey, int bits, AES_KEY *key) {
  240. abort();
  241. }
  242. void aesni_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t blocks,
  243. const void *key, const uint8_t *ivec) {
  244. abort();
  245. }
  246. #endif
  247. static int aes_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
  248. const uint8_t *iv, int enc)
  249. OPENSSL_SUPPRESS_UNREACHABLE_CODE_WARNINGS {
  250. int ret, mode;
  251. EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
  252. mode = ctx->cipher->flags & EVP_CIPH_MODE_MASK;
  253. if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && !enc) {
  254. if (hwaes_capable()) {
  255. ret = aes_v8_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
  256. dat->block = (block128_f)aes_v8_decrypt;
  257. dat->stream.cbc = NULL;
  258. if (mode == EVP_CIPH_CBC_MODE) {
  259. dat->stream.cbc = (cbc128_f)aes_v8_cbc_encrypt;
  260. }
  261. } else if (bsaes_capable() && mode == EVP_CIPH_CBC_MODE) {
  262. ret = AES_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
  263. dat->block = (block128_f)AES_decrypt;
  264. dat->stream.cbc = (cbc128_f)bsaes_cbc_encrypt;
  265. } else if (vpaes_capable()) {
  266. ret = vpaes_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
  267. dat->block = (block128_f)vpaes_decrypt;
  268. dat->stream.cbc =
  269. mode == EVP_CIPH_CBC_MODE ? (cbc128_f)vpaes_cbc_encrypt : NULL;
  270. } else {
  271. ret = AES_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
  272. dat->block = (block128_f)AES_decrypt;
  273. dat->stream.cbc =
  274. mode == EVP_CIPH_CBC_MODE ? (cbc128_f)AES_cbc_encrypt : NULL;
  275. }
  276. } else if (hwaes_capable()) {
  277. ret = aes_v8_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
  278. dat->block = (block128_f)aes_v8_encrypt;
  279. dat->stream.cbc = NULL;
  280. if (mode == EVP_CIPH_CBC_MODE) {
  281. dat->stream.cbc = (cbc128_f)aes_v8_cbc_encrypt;
  282. } else if (mode == EVP_CIPH_CTR_MODE) {
  283. dat->stream.ctr = (ctr128_f)aes_v8_ctr32_encrypt_blocks;
  284. }
  285. } else if (bsaes_capable() && mode == EVP_CIPH_CTR_MODE) {
  286. ret = AES_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
  287. dat->block = (block128_f)AES_encrypt;
  288. dat->stream.ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks;
  289. } else if (vpaes_capable()) {
  290. ret = vpaes_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
  291. dat->block = (block128_f)vpaes_encrypt;
  292. dat->stream.cbc =
  293. mode == EVP_CIPH_CBC_MODE ? (cbc128_f)vpaes_cbc_encrypt : NULL;
  294. } else {
  295. ret = AES_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
  296. dat->block = (block128_f)AES_encrypt;
  297. dat->stream.cbc =
  298. mode == EVP_CIPH_CBC_MODE ? (cbc128_f)AES_cbc_encrypt : NULL;
  299. }
  300. if (ret < 0) {
  301. OPENSSL_PUT_ERROR(CIPHER, aes_init_key, CIPHER_R_AES_KEY_SETUP_FAILED);
  302. return 0;
  303. }
  304. return 1;
  305. }
  306. static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
  307. size_t len) {
  308. EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
  309. if (dat->stream.cbc) {
  310. (*dat->stream.cbc)(in, out, len, &dat->ks, ctx->iv, ctx->encrypt);
  311. } else if (ctx->encrypt) {
  312. CRYPTO_cbc128_encrypt(in, out, len, &dat->ks, ctx->iv, dat->block);
  313. } else {
  314. CRYPTO_cbc128_decrypt(in, out, len, &dat->ks, ctx->iv, dat->block);
  315. }
  316. return 1;
  317. }
  318. static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
  319. size_t len) {
  320. size_t bl = ctx->cipher->block_size;
  321. size_t i;
  322. EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
  323. if (len < bl) {
  324. return 1;
  325. }
  326. for (i = 0, len -= bl; i <= len; i += bl) {
  327. (*dat->block)(in + i, out + i, &dat->ks);
  328. }
  329. return 1;
  330. }
  331. static int aes_ctr_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
  332. size_t len) {
  333. unsigned int num = ctx->num;
  334. EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
  335. if (dat->stream.ctr) {
  336. CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks, ctx->iv, ctx->buf, &num,
  337. dat->stream.ctr);
  338. } else {
  339. CRYPTO_ctr128_encrypt(in, out, len, &dat->ks, ctx->iv, ctx->buf, &num,
  340. dat->block);
  341. }
  342. ctx->num = (size_t)num;
  343. return 1;
  344. }
  345. static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
  346. size_t len) {
  347. EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
  348. CRYPTO_ofb128_encrypt(in, out, len, &dat->ks, ctx->iv, &ctx->num, dat->block);
  349. return 1;
  350. }
  351. static char aesni_capable(void);
  352. static ctr128_f aes_ctr_set_key(AES_KEY *aes_key, GCM128_CONTEXT *gcm_ctx,
  353. block128_f *out_block, const uint8_t *key,
  354. size_t key_len)
  355. OPENSSL_SUPPRESS_UNREACHABLE_CODE_WARNINGS {
  356. if (aesni_capable()) {
  357. aesni_set_encrypt_key(key, key_len * 8, aes_key);
  358. if (gcm_ctx != NULL) {
  359. CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)aesni_encrypt);
  360. }
  361. if (out_block) {
  362. *out_block = (block128_f) aesni_encrypt;
  363. }
  364. return (ctr128_f)aesni_ctr32_encrypt_blocks;
  365. }
  366. if (hwaes_capable()) {
  367. aes_v8_set_encrypt_key(key, key_len * 8, aes_key);
  368. if (gcm_ctx != NULL) {
  369. CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)aes_v8_encrypt);
  370. }
  371. if (out_block) {
  372. *out_block = (block128_f) aes_v8_encrypt;
  373. }
  374. return (ctr128_f)aes_v8_ctr32_encrypt_blocks;
  375. }
  376. if (bsaes_capable()) {
  377. AES_set_encrypt_key(key, key_len * 8, aes_key);
  378. if (gcm_ctx != NULL) {
  379. CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt);
  380. }
  381. if (out_block) {
  382. *out_block = (block128_f) AES_encrypt;
  383. }
  384. return (ctr128_f)bsaes_ctr32_encrypt_blocks;
  385. }
  386. if (vpaes_capable()) {
  387. vpaes_set_encrypt_key(key, key_len * 8, aes_key);
  388. if (out_block) {
  389. *out_block = (block128_f) vpaes_encrypt;
  390. }
  391. if (gcm_ctx != NULL) {
  392. CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)vpaes_encrypt);
  393. }
  394. return NULL;
  395. }
  396. AES_set_encrypt_key(key, key_len * 8, aes_key);
  397. if (gcm_ctx != NULL) {
  398. CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt);
  399. }
  400. if (out_block) {
  401. *out_block = (block128_f) AES_encrypt;
  402. }
  403. return NULL;
  404. }
  405. static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
  406. const uint8_t *iv, int enc) {
  407. EVP_AES_GCM_CTX *gctx = ctx->cipher_data;
  408. if (!iv && !key) {
  409. return 1;
  410. }
  411. if (key) {
  412. gctx->ctr =
  413. aes_ctr_set_key(&gctx->ks.ks, &gctx->gcm, NULL, key, ctx->key_len);
  414. /* If we have an iv can set it directly, otherwise use saved IV. */
  415. if (iv == NULL && gctx->iv_set) {
  416. iv = gctx->iv;
  417. }
  418. if (iv) {
  419. CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
  420. gctx->iv_set = 1;
  421. }
  422. gctx->key_set = 1;
  423. } else {
  424. /* If key set use IV, otherwise copy */
  425. if (gctx->key_set) {
  426. CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
  427. } else {
  428. memcpy(gctx->iv, iv, gctx->ivlen);
  429. }
  430. gctx->iv_set = 1;
  431. gctx->iv_gen = 0;
  432. }
  433. return 1;
  434. }
  435. static void aes_gcm_cleanup(EVP_CIPHER_CTX *c) {
  436. EVP_AES_GCM_CTX *gctx = c->cipher_data;
  437. OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm));
  438. if (gctx->iv != c->iv) {
  439. OPENSSL_free(gctx->iv);
  440. }
  441. }
  442. /* increment counter (64-bit int) by 1 */
  443. static void ctr64_inc(uint8_t *counter) {
  444. int n = 8;
  445. uint8_t c;
  446. do {
  447. --n;
  448. c = counter[n];
  449. ++c;
  450. counter[n] = c;
  451. if (c) {
  452. return;
  453. }
  454. } while (n);
  455. }
  456. static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) {
  457. EVP_AES_GCM_CTX *gctx = c->cipher_data;
  458. switch (type) {
  459. case EVP_CTRL_INIT:
  460. gctx->key_set = 0;
  461. gctx->iv_set = 0;
  462. gctx->ivlen = c->cipher->iv_len;
  463. gctx->iv = c->iv;
  464. gctx->taglen = -1;
  465. gctx->iv_gen = 0;
  466. return 1;
  467. case EVP_CTRL_GCM_SET_IVLEN:
  468. if (arg <= 0) {
  469. return 0;
  470. }
  471. /* Allocate memory for IV if needed */
  472. if (arg > EVP_MAX_IV_LENGTH && arg > gctx->ivlen) {
  473. if (gctx->iv != c->iv) {
  474. OPENSSL_free(gctx->iv);
  475. }
  476. gctx->iv = OPENSSL_malloc(arg);
  477. if (!gctx->iv) {
  478. return 0;
  479. }
  480. }
  481. gctx->ivlen = arg;
  482. return 1;
  483. case EVP_CTRL_GCM_SET_TAG:
  484. if (arg <= 0 || arg > 16 || c->encrypt) {
  485. return 0;
  486. }
  487. memcpy(c->buf, ptr, arg);
  488. gctx->taglen = arg;
  489. return 1;
  490. case EVP_CTRL_GCM_GET_TAG:
  491. if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0) {
  492. return 0;
  493. }
  494. memcpy(ptr, c->buf, arg);
  495. return 1;
  496. case EVP_CTRL_GCM_SET_IV_FIXED:
  497. /* Special case: -1 length restores whole IV */
  498. if (arg == -1) {
  499. memcpy(gctx->iv, ptr, gctx->ivlen);
  500. gctx->iv_gen = 1;
  501. return 1;
  502. }
  503. /* Fixed field must be at least 4 bytes and invocation field
  504. * at least 8. */
  505. if (arg < 4 || (gctx->ivlen - arg) < 8) {
  506. return 0;
  507. }
  508. if (arg) {
  509. memcpy(gctx->iv, ptr, arg);
  510. }
  511. if (c->encrypt && !RAND_bytes(gctx->iv + arg, gctx->ivlen - arg)) {
  512. return 0;
  513. }
  514. gctx->iv_gen = 1;
  515. return 1;
  516. case EVP_CTRL_GCM_IV_GEN:
  517. if (gctx->iv_gen == 0 || gctx->key_set == 0) {
  518. return 0;
  519. }
  520. CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
  521. if (arg <= 0 || arg > gctx->ivlen) {
  522. arg = gctx->ivlen;
  523. }
  524. memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
  525. /* Invocation field will be at least 8 bytes in size and
  526. * so no need to check wrap around or increment more than
  527. * last 8 bytes. */
  528. ctr64_inc(gctx->iv + gctx->ivlen - 8);
  529. gctx->iv_set = 1;
  530. return 1;
  531. case EVP_CTRL_GCM_SET_IV_INV:
  532. if (gctx->iv_gen == 0 || gctx->key_set == 0 || c->encrypt) {
  533. return 0;
  534. }
  535. memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
  536. CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
  537. gctx->iv_set = 1;
  538. return 1;
  539. case EVP_CTRL_COPY: {
  540. EVP_CIPHER_CTX *out = ptr;
  541. EVP_AES_GCM_CTX *gctx_out = out->cipher_data;
  542. if (gctx->gcm.key) {
  543. if (gctx->gcm.key != &gctx->ks) {
  544. return 0;
  545. }
  546. gctx_out->gcm.key = &gctx_out->ks;
  547. }
  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, in, out, res)) {
  585. return -1;
  586. }
  587. bulk = AES_gcm_encrypt(in + res, out + res, len - res, gctx->gcm.key,
  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, in + bulk, out + bulk,
  594. len - bulk, gctx->ctr)) {
  595. return -1;
  596. }
  597. } else {
  598. size_t bulk = 0;
  599. if (!CRYPTO_gcm128_encrypt(&gctx->gcm, in + bulk, out + bulk,
  600. 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, in, out, res)) {
  611. return -1;
  612. }
  613. bulk = AES_gcm_decrypt(in + res, out + res, len - res, gctx->gcm.key,
  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, in + bulk, out + bulk,
  620. len - bulk, gctx->ctr)) {
  621. return -1;
  622. }
  623. } else {
  624. size_t bulk = 0;
  625. if (!CRYPTO_gcm128_decrypt(&gctx->gcm, in + bulk, out + bulk,
  626. 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) != 0) {
  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, aesni_init_key, 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, 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, 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, aead_aes_gcm_init, 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, aead_aes_gcm_init, 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. size_t bulk = 0;
  962. const struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state;
  963. GCM128_CONTEXT gcm;
  964. if (in_len + gcm_ctx->tag_len < in_len) {
  965. OPENSSL_PUT_ERROR(CIPHER, aead_aes_gcm_seal, CIPHER_R_TOO_LARGE);
  966. return 0;
  967. }
  968. if (max_out_len < in_len + gcm_ctx->tag_len) {
  969. OPENSSL_PUT_ERROR(CIPHER, aead_aes_gcm_seal, CIPHER_R_BUFFER_TOO_SMALL);
  970. return 0;
  971. }
  972. memcpy(&gcm, &gcm_ctx->gcm, sizeof(gcm));
  973. CRYPTO_gcm128_setiv(&gcm, 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, in + bulk, out + bulk, in_len - bulk,
  979. gcm_ctx->ctr)) {
  980. return 0;
  981. }
  982. } else {
  983. if (!CRYPTO_gcm128_encrypt(&gcm, in + bulk, out + bulk, in_len - bulk)) {
  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. size_t bulk = 0;
  997. const struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state;
  998. uint8_t tag[EVP_AEAD_AES_GCM_TAG_LEN];
  999. size_t plaintext_len;
  1000. GCM128_CONTEXT gcm;
  1001. if (in_len < gcm_ctx->tag_len) {
  1002. OPENSSL_PUT_ERROR(CIPHER, aead_aes_gcm_open, CIPHER_R_BAD_DECRYPT);
  1003. return 0;
  1004. }
  1005. plaintext_len = in_len - gcm_ctx->tag_len;
  1006. if (max_out_len < plaintext_len) {
  1007. OPENSSL_PUT_ERROR(CIPHER, aead_aes_gcm_open, CIPHER_R_BUFFER_TOO_SMALL);
  1008. return 0;
  1009. }
  1010. memcpy(&gcm, &gcm_ctx->gcm, sizeof(gcm));
  1011. CRYPTO_gcm128_setiv(&gcm, 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, in + bulk, out + bulk,
  1017. in_len - bulk - gcm_ctx->tag_len,
  1018. gcm_ctx->ctr)) {
  1019. return 0;
  1020. }
  1021. } else {
  1022. if (!CRYPTO_gcm128_decrypt(&gcm, in + bulk, out + bulk,
  1023. in_len - bulk - gcm_ctx->tag_len)) {
  1024. return 0;
  1025. }
  1026. }
  1027. CRYPTO_gcm128_tag(&gcm, tag, gcm_ctx->tag_len);
  1028. if (CRYPTO_memcmp(tag, in + plaintext_len, gcm_ctx->tag_len) != 0) {
  1029. OPENSSL_PUT_ERROR(CIPHER, aead_aes_gcm_open, CIPHER_R_BAD_DECRYPT);
  1030. return 0;
  1031. }
  1032. *out_len = plaintext_len;
  1033. return 1;
  1034. }
  1035. static const EVP_AEAD aead_aes_128_gcm = {
  1036. 16, /* key len */
  1037. 12, /* nonce len */
  1038. EVP_AEAD_AES_GCM_TAG_LEN, /* overhead */
  1039. EVP_AEAD_AES_GCM_TAG_LEN, /* max tag length */
  1040. aead_aes_gcm_init,
  1041. NULL, /* init_with_direction */
  1042. aead_aes_gcm_cleanup,
  1043. aead_aes_gcm_seal,
  1044. aead_aes_gcm_open,
  1045. NULL, /* get_rc4_state */
  1046. };
  1047. static const EVP_AEAD aead_aes_256_gcm = {
  1048. 32, /* key len */
  1049. 12, /* nonce len */
  1050. EVP_AEAD_AES_GCM_TAG_LEN, /* overhead */
  1051. EVP_AEAD_AES_GCM_TAG_LEN, /* max tag length */
  1052. aead_aes_gcm_init,
  1053. NULL, /* init_with_direction */
  1054. aead_aes_gcm_cleanup,
  1055. aead_aes_gcm_seal,
  1056. aead_aes_gcm_open,
  1057. NULL, /* get_rc4_state */
  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, aead_aes_key_wrap_init, 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, aead_aes_key_wrap_init,
  1081. CIPHER_R_UNSUPPORTED_TAG_SIZE);
  1082. return 0;
  1083. }
  1084. kw_ctx = OPENSSL_malloc(sizeof(struct aead_aes_key_wrap_ctx));
  1085. if (kw_ctx == NULL) {
  1086. OPENSSL_PUT_ERROR(CIPHER, aead_aes_key_wrap_init, ERR_R_MALLOC_FAILURE);
  1087. return 0;
  1088. }
  1089. memcpy(kw_ctx->key, key, key_len);
  1090. kw_ctx->key_bits = key_bits;
  1091. ctx->aead_state = kw_ctx;
  1092. return 1;
  1093. }
  1094. static void aead_aes_key_wrap_cleanup(EVP_AEAD_CTX *ctx) {
  1095. struct aead_aes_key_wrap_ctx *kw_ctx = ctx->aead_state;
  1096. OPENSSL_cleanse(kw_ctx, sizeof(struct aead_aes_key_wrap_ctx));
  1097. OPENSSL_free(kw_ctx);
  1098. }
  1099. /* kDefaultAESKeyWrapNonce is the default nonce value given in 2.2.3.1. */
  1100. static const uint8_t kDefaultAESKeyWrapNonce[8] = {0xa6, 0xa6, 0xa6, 0xa6,
  1101. 0xa6, 0xa6, 0xa6, 0xa6};
  1102. static int aead_aes_key_wrap_seal(const EVP_AEAD_CTX *ctx, uint8_t *out,
  1103. size_t *out_len, size_t max_out_len,
  1104. const uint8_t *nonce, size_t nonce_len,
  1105. const uint8_t *in, size_t in_len,
  1106. const uint8_t *ad, size_t ad_len) {
  1107. const struct aead_aes_key_wrap_ctx *kw_ctx = ctx->aead_state;
  1108. union {
  1109. double align;
  1110. AES_KEY ks;
  1111. } ks;
  1112. /* Variables in this function match up with the variables in the second half
  1113. * of section 2.2.1. */
  1114. unsigned i, j, n;
  1115. uint8_t A[AES_BLOCK_SIZE];
  1116. if (ad_len != 0) {
  1117. OPENSSL_PUT_ERROR(CIPHER, aead_aes_key_wrap_seal,
  1118. CIPHER_R_UNSUPPORTED_AD_SIZE);
  1119. return 0;
  1120. }
  1121. if (nonce_len == 0) {
  1122. nonce = kDefaultAESKeyWrapNonce;
  1123. nonce_len = sizeof(kDefaultAESKeyWrapNonce);
  1124. }
  1125. if (nonce_len != 8) {
  1126. OPENSSL_PUT_ERROR(CIPHER, aead_aes_key_wrap_seal,
  1127. CIPHER_R_UNSUPPORTED_NONCE_SIZE);
  1128. return 0;
  1129. }
  1130. if (in_len % 8 != 0) {
  1131. OPENSSL_PUT_ERROR(CIPHER, aead_aes_key_wrap_seal,
  1132. CIPHER_R_UNSUPPORTED_INPUT_SIZE);
  1133. return 0;
  1134. }
  1135. /* The code below only handles a 32-bit |t| thus 6*|n| must be less than
  1136. * 2^32, where |n| is |in_len| / 8. So in_len < 4/3 * 2^32 and we
  1137. * conservatively cap it to 2^32-16 to stop 32-bit platforms complaining that
  1138. * a comparison is always true. */
  1139. if (in_len > 0xfffffff0) {
  1140. OPENSSL_PUT_ERROR(CIPHER, aead_aes_key_wrap_seal, CIPHER_R_TOO_LARGE);
  1141. return 0;
  1142. }
  1143. n = in_len / 8;
  1144. if (n < 2) {
  1145. OPENSSL_PUT_ERROR(CIPHER, aead_aes_key_wrap_seal,
  1146. CIPHER_R_UNSUPPORTED_INPUT_SIZE);
  1147. return 0;
  1148. }
  1149. if (in_len + 8 < in_len) {
  1150. OPENSSL_PUT_ERROR(CIPHER, aead_aes_key_wrap_seal, CIPHER_R_TOO_LARGE);
  1151. return 0;
  1152. }
  1153. if (max_out_len < in_len + 8) {
  1154. OPENSSL_PUT_ERROR(CIPHER, aead_aes_key_wrap_seal,
  1155. CIPHER_R_BUFFER_TOO_SMALL);
  1156. return 0;
  1157. }
  1158. if (AES_set_encrypt_key(kw_ctx->key, kw_ctx->key_bits, &ks.ks) < 0) {
  1159. OPENSSL_PUT_ERROR(CIPHER, aead_aes_key_wrap_seal,
  1160. CIPHER_R_AES_KEY_SETUP_FAILED);
  1161. return 0;
  1162. }
  1163. memmove(out + 8, in, in_len);
  1164. memcpy(A, nonce, 8);
  1165. for (j = 0; j < 6; j++) {
  1166. for (i = 1; i <= n; i++) {
  1167. uint32_t t;
  1168. memcpy(A + 8, out + 8 * i, 8);
  1169. AES_encrypt(A, A, &ks.ks);
  1170. t = n * j + i;
  1171. A[7] ^= t & 0xff;
  1172. A[6] ^= (t >> 8) & 0xff;
  1173. A[5] ^= (t >> 16) & 0xff;
  1174. A[4] ^= (t >> 24) & 0xff;
  1175. memcpy(out + 8 * i, A + 8, 8);
  1176. }
  1177. }
  1178. memcpy(out, A, 8);
  1179. *out_len = in_len + 8;
  1180. return 1;
  1181. }
  1182. static int aead_aes_key_wrap_open(const EVP_AEAD_CTX *ctx, uint8_t *out,
  1183. size_t *out_len, size_t max_out_len,
  1184. const uint8_t *nonce, size_t nonce_len,
  1185. const uint8_t *in, size_t in_len,
  1186. const uint8_t *ad, size_t ad_len) {
  1187. const struct aead_aes_key_wrap_ctx *kw_ctx = ctx->aead_state;
  1188. union {
  1189. double align;
  1190. AES_KEY ks;
  1191. } ks;
  1192. /* Variables in this function match up with the variables in the second half
  1193. * of section 2.2.1. */
  1194. unsigned i, j, n;
  1195. uint8_t A[AES_BLOCK_SIZE];
  1196. if (ad_len != 0) {
  1197. OPENSSL_PUT_ERROR(CIPHER, aead_aes_key_wrap_open,
  1198. CIPHER_R_UNSUPPORTED_AD_SIZE);
  1199. return 0;
  1200. }
  1201. if (nonce_len == 0) {
  1202. nonce = kDefaultAESKeyWrapNonce;
  1203. nonce_len = sizeof(kDefaultAESKeyWrapNonce);
  1204. }
  1205. if (nonce_len != 8) {
  1206. OPENSSL_PUT_ERROR(CIPHER, aead_aes_key_wrap_open,
  1207. CIPHER_R_UNSUPPORTED_NONCE_SIZE);
  1208. return 0;
  1209. }
  1210. if (in_len % 8 != 0) {
  1211. OPENSSL_PUT_ERROR(CIPHER, aead_aes_key_wrap_open,
  1212. CIPHER_R_UNSUPPORTED_INPUT_SIZE);
  1213. return 0;
  1214. }
  1215. /* The code below only handles a 32-bit |t| thus 6*|n| must be less than
  1216. * 2^32, where |n| is |in_len| / 8. So in_len < 4/3 * 2^32 and we
  1217. * conservatively cap it to 2^32-8 to stop 32-bit platforms complaining that
  1218. * a comparison is always true. */
  1219. if (in_len > 0xfffffff8) {
  1220. OPENSSL_PUT_ERROR(CIPHER, aead_aes_key_wrap_open, CIPHER_R_TOO_LARGE);
  1221. return 0;
  1222. }
  1223. if (in_len < 24) {
  1224. OPENSSL_PUT_ERROR(CIPHER, aead_aes_key_wrap_open, CIPHER_R_BAD_DECRYPT);
  1225. return 0;
  1226. }
  1227. n = (in_len / 8) - 1;
  1228. if (max_out_len < in_len - 8) {
  1229. OPENSSL_PUT_ERROR(CIPHER, aead_aes_key_wrap_open,
  1230. CIPHER_R_BUFFER_TOO_SMALL);
  1231. return 0;
  1232. }
  1233. if (AES_set_decrypt_key(kw_ctx->key, kw_ctx->key_bits, &ks.ks) < 0) {
  1234. OPENSSL_PUT_ERROR(CIPHER, aead_aes_key_wrap_open,
  1235. CIPHER_R_AES_KEY_SETUP_FAILED);
  1236. return 0;
  1237. }
  1238. memcpy(A, in, 8);
  1239. memmove(out, in + 8, in_len - 8);
  1240. for (j = 5; j < 6; j--) {
  1241. for (i = n; i > 0; i--) {
  1242. uint32_t t;
  1243. t = n * j + i;
  1244. A[7] ^= t & 0xff;
  1245. A[6] ^= (t >> 8) & 0xff;
  1246. A[5] ^= (t >> 16) & 0xff;
  1247. A[4] ^= (t >> 24) & 0xff;
  1248. memcpy(A + 8, out + 8 * (i - 1), 8);
  1249. AES_decrypt(A, A, &ks.ks);
  1250. memcpy(out + 8 * (i - 1), A + 8, 8);
  1251. }
  1252. }
  1253. if (CRYPTO_memcmp(A, nonce, 8) != 0) {
  1254. OPENSSL_PUT_ERROR(CIPHER, aead_aes_key_wrap_open, CIPHER_R_BAD_DECRYPT);
  1255. return 0;
  1256. }
  1257. *out_len = in_len - 8;
  1258. return 1;
  1259. }
  1260. static const EVP_AEAD aead_aes_128_key_wrap = {
  1261. 16, /* key len */
  1262. 8, /* nonce len */
  1263. 8, /* overhead */
  1264. 8, /* max tag length */
  1265. aead_aes_key_wrap_init,
  1266. NULL, /* init_with_direction */
  1267. aead_aes_key_wrap_cleanup,
  1268. aead_aes_key_wrap_seal,
  1269. aead_aes_key_wrap_open,
  1270. NULL, /* get_rc4_state */
  1271. };
  1272. static const EVP_AEAD aead_aes_256_key_wrap = {
  1273. 32, /* key len */
  1274. 8, /* nonce len */
  1275. 8, /* overhead */
  1276. 8, /* max tag length */
  1277. aead_aes_key_wrap_init,
  1278. NULL, /* init_with_direction */
  1279. aead_aes_key_wrap_cleanup,
  1280. aead_aes_key_wrap_seal,
  1281. aead_aes_key_wrap_open,
  1282. NULL, /* get_rc4_state */
  1283. };
  1284. const EVP_AEAD *EVP_aead_aes_128_key_wrap(void) { return &aead_aes_128_key_wrap; }
  1285. const EVP_AEAD *EVP_aead_aes_256_key_wrap(void) { return &aead_aes_256_key_wrap; }
  1286. #define EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN SHA256_DIGEST_LENGTH
  1287. #define EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN 12
  1288. struct aead_aes_ctr_hmac_sha256_ctx {
  1289. union {
  1290. double align;
  1291. AES_KEY ks;
  1292. } ks;
  1293. ctr128_f ctr;
  1294. block128_f block;
  1295. SHA256_CTX inner_init_state;
  1296. SHA256_CTX outer_init_state;
  1297. uint8_t tag_len;
  1298. };
  1299. static void hmac_init(SHA256_CTX *out_inner, SHA256_CTX *out_outer,
  1300. const uint8_t hmac_key[32]) {
  1301. static const size_t hmac_key_len = 32;
  1302. uint8_t block[SHA256_CBLOCK];
  1303. memcpy(block, hmac_key, hmac_key_len);
  1304. memset(block + hmac_key_len, 0x36, sizeof(block) - hmac_key_len);
  1305. unsigned i;
  1306. for (i = 0; i < hmac_key_len; i++) {
  1307. block[i] ^= 0x36;
  1308. }
  1309. SHA256_Init(out_inner);
  1310. SHA256_Update(out_inner, block, sizeof(block));
  1311. memset(block + hmac_key_len, 0x5c, sizeof(block) - hmac_key_len);
  1312. for (i = 0; i < hmac_key_len; i++) {
  1313. block[i] ^= (0x36 ^ 0x5c);
  1314. }
  1315. SHA256_Init(out_outer);
  1316. SHA256_Update(out_outer, block, sizeof(block));
  1317. }
  1318. static int aead_aes_ctr_hmac_sha256_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  1319. size_t key_len, size_t tag_len) {
  1320. struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx;
  1321. static const size_t hmac_key_len = 32;
  1322. if (key_len < hmac_key_len) {
  1323. OPENSSL_PUT_ERROR(CIPHER, aead_aes_ctr_hmac_sha256_init,
  1324. CIPHER_R_BAD_KEY_LENGTH);
  1325. return 0; /* EVP_AEAD_CTX_init should catch this. */
  1326. }
  1327. const size_t aes_key_len = key_len - hmac_key_len;
  1328. if (aes_key_len != 16 && aes_key_len != 32) {
  1329. OPENSSL_PUT_ERROR(CIPHER, aead_aes_ctr_hmac_sha256_init,
  1330. CIPHER_R_BAD_KEY_LENGTH);
  1331. return 0; /* EVP_AEAD_CTX_init should catch this. */
  1332. }
  1333. if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) {
  1334. tag_len = EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN;
  1335. }
  1336. if (tag_len > EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN) {
  1337. OPENSSL_PUT_ERROR(CIPHER, aead_aes_ctr_hmac_sha256_init,
  1338. CIPHER_R_TAG_TOO_LARGE);
  1339. return 0;
  1340. }
  1341. aes_ctx = OPENSSL_malloc(sizeof(struct aead_aes_ctr_hmac_sha256_ctx));
  1342. if (aes_ctx == NULL) {
  1343. OPENSSL_PUT_ERROR(CIPHER, aead_aes_ctr_hmac_sha256_init,
  1344. ERR_R_MALLOC_FAILURE);
  1345. return 0;
  1346. }
  1347. aes_ctx->ctr =
  1348. aes_ctr_set_key(&aes_ctx->ks.ks, NULL, &aes_ctx->block, key, aes_key_len);
  1349. aes_ctx->tag_len = tag_len;
  1350. hmac_init(&aes_ctx->inner_init_state, &aes_ctx->outer_init_state,
  1351. key + aes_key_len);
  1352. ctx->aead_state = aes_ctx;
  1353. return 1;
  1354. }
  1355. static void aead_aes_ctr_hmac_sha256_cleanup(EVP_AEAD_CTX *ctx) {
  1356. struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx = ctx->aead_state;
  1357. OPENSSL_cleanse(aes_ctx, sizeof(struct aead_aes_ctr_hmac_sha256_ctx));
  1358. OPENSSL_free(aes_ctx);
  1359. }
  1360. static void hmac_update_uint64(SHA256_CTX *sha256, uint64_t value) {
  1361. unsigned i;
  1362. uint8_t bytes[8];
  1363. for (i = 0; i < sizeof(bytes); i++) {
  1364. bytes[i] = value & 0xff;
  1365. value >>= 8;
  1366. }
  1367. SHA256_Update(sha256, bytes, sizeof(bytes));
  1368. }
  1369. static void hmac_calculate(uint8_t out[SHA256_DIGEST_LENGTH],
  1370. const SHA256_CTX *inner_init_state,
  1371. const SHA256_CTX *outer_init_state,
  1372. const uint8_t *ad, size_t ad_len,
  1373. const uint8_t *nonce, const uint8_t *ciphertext,
  1374. size_t ciphertext_len) {
  1375. SHA256_CTX sha256;
  1376. memcpy(&sha256, inner_init_state, sizeof(sha256));
  1377. hmac_update_uint64(&sha256, ad_len);
  1378. hmac_update_uint64(&sha256, ciphertext_len);
  1379. SHA256_Update(&sha256, nonce, EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN);
  1380. SHA256_Update(&sha256, ad, ad_len);
  1381. /* Pad with zeros to the end of the SHA-256 block. */
  1382. const unsigned num_padding =
  1383. (SHA256_CBLOCK - ((sizeof(uint64_t)*2 +
  1384. EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN + ad_len) %
  1385. SHA256_CBLOCK)) %
  1386. SHA256_CBLOCK;
  1387. uint8_t padding[SHA256_CBLOCK];
  1388. memset(padding, 0, num_padding);
  1389. SHA256_Update(&sha256, padding, num_padding);
  1390. SHA256_Update(&sha256, ciphertext, ciphertext_len);
  1391. uint8_t inner_digest[SHA256_DIGEST_LENGTH];
  1392. SHA256_Final(inner_digest, &sha256);
  1393. memcpy(&sha256, outer_init_state, sizeof(sha256));
  1394. SHA256_Update(&sha256, inner_digest, sizeof(inner_digest));
  1395. SHA256_Final(out, &sha256);
  1396. }
  1397. static void aead_aes_ctr_hmac_sha256_crypt(
  1398. const struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx, uint8_t *out,
  1399. const uint8_t *in, size_t len, const uint8_t *nonce) {
  1400. /* Since the AEAD operation is one-shot, keeping a buffer of unused keystream
  1401. * bytes is pointless. However, |CRYPTO_ctr128_encrypt| requires it. */
  1402. uint8_t partial_block_buffer[AES_BLOCK_SIZE];
  1403. unsigned partial_block_offset = 0;
  1404. memset(partial_block_buffer, 0, sizeof(partial_block_buffer));
  1405. uint8_t counter[AES_BLOCK_SIZE];
  1406. memcpy(counter, nonce, EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN);
  1407. memset(counter + EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN, 0, 4);
  1408. if (aes_ctx->ctr) {
  1409. CRYPTO_ctr128_encrypt_ctr32(in, out, len, &aes_ctx->ks.ks, counter,
  1410. partial_block_buffer, &partial_block_offset,
  1411. aes_ctx->ctr);
  1412. } else {
  1413. CRYPTO_ctr128_encrypt(in, out, len, &aes_ctx->ks.ks, counter,
  1414. partial_block_buffer, &partial_block_offset,
  1415. aes_ctx->block);
  1416. }
  1417. }
  1418. static int aead_aes_ctr_hmac_sha256_seal(const EVP_AEAD_CTX *ctx, uint8_t *out,
  1419. size_t *out_len, size_t max_out_len,
  1420. const uint8_t *nonce, size_t nonce_len,
  1421. const uint8_t *in, size_t in_len,
  1422. const uint8_t *ad, size_t ad_len) {
  1423. const struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx = ctx->aead_state;
  1424. const uint64_t in_len_64 = in_len;
  1425. if (in_len + aes_ctx->tag_len < in_len ||
  1426. /* This input is so large it would overflow the 32-bit block counter. */
  1427. in_len_64 >= (OPENSSL_U64(1) << 32) * AES_BLOCK_SIZE) {
  1428. OPENSSL_PUT_ERROR(CIPHER, aead_aes_ctr_hmac_sha256_seal,
  1429. CIPHER_R_TOO_LARGE);
  1430. return 0;
  1431. }
  1432. if (max_out_len < in_len + aes_ctx->tag_len) {
  1433. OPENSSL_PUT_ERROR(CIPHER, aead_aes_ctr_hmac_sha256_seal,
  1434. CIPHER_R_BUFFER_TOO_SMALL);
  1435. return 0;
  1436. }
  1437. if (nonce_len != EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN) {
  1438. OPENSSL_PUT_ERROR(CIPHER, aead_aes_ctr_hmac_sha256_seal,
  1439. CIPHER_R_UNSUPPORTED_NONCE_SIZE);
  1440. return 0;
  1441. }
  1442. aead_aes_ctr_hmac_sha256_crypt(aes_ctx, out, in, in_len, nonce);
  1443. uint8_t hmac_result[SHA256_DIGEST_LENGTH];
  1444. hmac_calculate(hmac_result, &aes_ctx->inner_init_state,
  1445. &aes_ctx->outer_init_state, ad, ad_len, nonce, out, in_len);
  1446. memcpy(out + in_len, hmac_result, aes_ctx->tag_len);
  1447. *out_len = in_len + aes_ctx->tag_len;
  1448. return 1;
  1449. }
  1450. static int aead_aes_ctr_hmac_sha256_open(const EVP_AEAD_CTX *ctx, uint8_t *out,
  1451. size_t *out_len, size_t max_out_len,
  1452. const uint8_t *nonce, size_t nonce_len,
  1453. const uint8_t *in, size_t in_len,
  1454. const uint8_t *ad, size_t ad_len) {
  1455. const struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx = ctx->aead_state;
  1456. size_t plaintext_len;
  1457. if (in_len < aes_ctx->tag_len) {
  1458. OPENSSL_PUT_ERROR(CIPHER, aead_aes_ctr_hmac_sha256_open,
  1459. CIPHER_R_BAD_DECRYPT);
  1460. return 0;
  1461. }
  1462. plaintext_len = in_len - aes_ctx->tag_len;
  1463. if (max_out_len < plaintext_len) {
  1464. OPENSSL_PUT_ERROR(CIPHER, aead_aes_ctr_hmac_sha256_open,
  1465. CIPHER_R_BUFFER_TOO_SMALL);
  1466. return 0;
  1467. }
  1468. if (nonce_len != EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN) {
  1469. OPENSSL_PUT_ERROR(CIPHER, aead_aes_ctr_hmac_sha256_open,
  1470. CIPHER_R_UNSUPPORTED_NONCE_SIZE);
  1471. return 0;
  1472. }
  1473. uint8_t hmac_result[SHA256_DIGEST_LENGTH];
  1474. hmac_calculate(hmac_result, &aes_ctx->inner_init_state,
  1475. &aes_ctx->outer_init_state, ad, ad_len, nonce, in,
  1476. plaintext_len);
  1477. if (CRYPTO_memcmp(hmac_result, in + plaintext_len, aes_ctx->tag_len) != 0) {
  1478. OPENSSL_PUT_ERROR(CIPHER, aead_aes_ctr_hmac_sha256_open,
  1479. CIPHER_R_BAD_DECRYPT);
  1480. return 0;
  1481. }
  1482. aead_aes_ctr_hmac_sha256_crypt(aes_ctx, out, in, plaintext_len, nonce);
  1483. *out_len = plaintext_len;
  1484. return 1;
  1485. }
  1486. static const EVP_AEAD aead_aes_128_ctr_hmac_sha256 = {
  1487. 16 /* AES key */ + 32 /* HMAC key */,
  1488. 12, /* nonce length */
  1489. EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, /* overhead */
  1490. EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, /* max tag length */
  1491. aead_aes_ctr_hmac_sha256_init,
  1492. NULL /* init_with_direction */,
  1493. aead_aes_ctr_hmac_sha256_cleanup,
  1494. aead_aes_ctr_hmac_sha256_seal,
  1495. aead_aes_ctr_hmac_sha256_open,
  1496. NULL /* get_rc4_state */,
  1497. };
  1498. static const EVP_AEAD aead_aes_256_ctr_hmac_sha256 = {
  1499. 32 /* AES key */ + 32 /* HMAC key */,
  1500. 12, /* nonce length */
  1501. EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, /* overhead */
  1502. EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, /* max tag length */
  1503. aead_aes_ctr_hmac_sha256_init,
  1504. NULL /* init_with_direction */,
  1505. aead_aes_ctr_hmac_sha256_cleanup,
  1506. aead_aes_ctr_hmac_sha256_seal,
  1507. aead_aes_ctr_hmac_sha256_open,
  1508. NULL /* get_rc4_state */,
  1509. };
  1510. const EVP_AEAD *EVP_aead_aes_128_ctr_hmac_sha256(void) {
  1511. return &aead_aes_128_ctr_hmac_sha256;
  1512. }
  1513. const EVP_AEAD *EVP_aead_aes_256_ctr_hmac_sha256(void) {
  1514. return &aead_aes_256_ctr_hmac_sha256;
  1515. }
  1516. int EVP_has_aes_hardware(void) {
  1517. #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
  1518. return aesni_capable() && crypto_gcm_clmul_enabled();
  1519. #elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
  1520. return hwaes_capable() && (OPENSSL_armcap_P & ARMV8_PMULL);
  1521. #else
  1522. return 0;
  1523. #endif
  1524. }