2
0

e_aes_cbc_hmac_sha256.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /*
  2. * Copyright 2013-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * AES low level APIs are deprecated for public use, but still ok for internal
  11. * use where we're using them to implement the higher level EVP interface, as is
  12. * the case here.
  13. */
  14. #include "internal/deprecated.h"
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <openssl/opensslconf.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/objects.h>
  20. #include <openssl/aes.h>
  21. #include <openssl/sha.h>
  22. #include <openssl/rand.h>
  23. #include "internal/cryptlib.h"
  24. #include "crypto/modes.h"
  25. #include "internal/constant_time.h"
  26. #include "crypto/evp.h"
  27. #include "evp_local.h"
  28. typedef struct {
  29. AES_KEY ks;
  30. SHA256_CTX head, tail, md;
  31. size_t payload_length; /* AAD length in decrypt case */
  32. union {
  33. unsigned int tls_ver;
  34. unsigned char tls_aad[16]; /* 13 used */
  35. } aux;
  36. } EVP_AES_HMAC_SHA256;
  37. # define NO_PAYLOAD_LENGTH ((size_t)-1)
  38. #if defined(AES_ASM) && ( \
  39. defined(__x86_64) || defined(__x86_64__) || \
  40. defined(_M_AMD64) || defined(_M_X64) )
  41. # define AESNI_CAPABLE (1<<(57-32))
  42. int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
  43. AES_KEY *key);
  44. int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
  45. AES_KEY *key);
  46. void aesni_cbc_encrypt(const unsigned char *in,
  47. unsigned char *out,
  48. size_t length,
  49. const AES_KEY *key, unsigned char *ivec, int enc);
  50. int aesni_cbc_sha256_enc(const void *inp, void *out, size_t blocks,
  51. const AES_KEY *key, unsigned char iv[16],
  52. SHA256_CTX *ctx, const void *in0);
  53. # define data(ctx) ((EVP_AES_HMAC_SHA256 *)EVP_CIPHER_CTX_get_cipher_data(ctx))
  54. static int aesni_cbc_hmac_sha256_init_key(EVP_CIPHER_CTX *ctx,
  55. const unsigned char *inkey,
  56. const unsigned char *iv, int enc)
  57. {
  58. EVP_AES_HMAC_SHA256 *key = data(ctx);
  59. int ret;
  60. if (enc)
  61. ret = aesni_set_encrypt_key(inkey,
  62. EVP_CIPHER_CTX_get_key_length(ctx) * 8,
  63. &key->ks);
  64. else
  65. ret = aesni_set_decrypt_key(inkey,
  66. EVP_CIPHER_CTX_get_key_length(ctx) * 8,
  67. &key->ks);
  68. SHA256_Init(&key->head); /* handy when benchmarking */
  69. key->tail = key->head;
  70. key->md = key->head;
  71. key->payload_length = NO_PAYLOAD_LENGTH;
  72. return ret < 0 ? 0 : 1;
  73. }
  74. # define STITCHED_CALL
  75. # if !defined(STITCHED_CALL)
  76. # define aes_off 0
  77. # endif
  78. void sha256_block_data_order(void *c, const void *p, size_t len);
  79. static void sha256_update(SHA256_CTX *c, const void *data, size_t len)
  80. {
  81. const unsigned char *ptr = data;
  82. size_t res;
  83. if ((res = c->num)) {
  84. res = SHA256_CBLOCK - res;
  85. if (len < res)
  86. res = len;
  87. SHA256_Update(c, ptr, res);
  88. ptr += res;
  89. len -= res;
  90. }
  91. res = len % SHA256_CBLOCK;
  92. len -= res;
  93. if (len) {
  94. sha256_block_data_order(c, ptr, len / SHA256_CBLOCK);
  95. ptr += len;
  96. c->Nh += len >> 29;
  97. c->Nl += len <<= 3;
  98. if (c->Nl < (unsigned int)len)
  99. c->Nh++;
  100. }
  101. if (res)
  102. SHA256_Update(c, ptr, res);
  103. }
  104. # ifdef SHA256_Update
  105. # undef SHA256_Update
  106. # endif
  107. # define SHA256_Update sha256_update
  108. # if !defined(OPENSSL_NO_MULTIBLOCK)
  109. typedef struct {
  110. unsigned int A[8], B[8], C[8], D[8], E[8], F[8], G[8], H[8];
  111. } SHA256_MB_CTX;
  112. typedef struct {
  113. const unsigned char *ptr;
  114. int blocks;
  115. } HASH_DESC;
  116. void sha256_multi_block(SHA256_MB_CTX *, const HASH_DESC *, int);
  117. typedef struct {
  118. const unsigned char *inp;
  119. unsigned char *out;
  120. int blocks;
  121. u64 iv[2];
  122. } CIPH_DESC;
  123. void aesni_multi_cbc_encrypt(CIPH_DESC *, void *, int);
  124. static size_t tls1_1_multi_block_encrypt(EVP_AES_HMAC_SHA256 *key,
  125. unsigned char *out,
  126. const unsigned char *inp,
  127. size_t inp_len, int n4x)
  128. { /* n4x is 1 or 2 */
  129. HASH_DESC hash_d[8], edges[8];
  130. CIPH_DESC ciph_d[8];
  131. unsigned char storage[sizeof(SHA256_MB_CTX) + 32];
  132. union {
  133. u64 q[16];
  134. u32 d[32];
  135. u8 c[128];
  136. } blocks[8];
  137. SHA256_MB_CTX *ctx;
  138. unsigned int frag, last, packlen, i, x4 = 4 * n4x, minblocks, processed =
  139. 0;
  140. size_t ret = 0;
  141. u8 *IVs;
  142. # if defined(BSWAP8)
  143. u64 seqnum;
  144. # endif
  145. /* ask for IVs in bulk */
  146. if (RAND_bytes((IVs = blocks[0].c), 16 * x4) <= 0)
  147. return 0;
  148. /* align */
  149. ctx = (SHA256_MB_CTX *) (storage + 32 - ((size_t)storage % 32));
  150. frag = (unsigned int)inp_len >> (1 + n4x);
  151. last = (unsigned int)inp_len + frag - (frag << (1 + n4x));
  152. if (last > frag && ((last + 13 + 9) % 64) < (x4 - 1)) {
  153. frag++;
  154. last -= x4 - 1;
  155. }
  156. packlen = 5 + 16 + ((frag + 32 + 16) & -16);
  157. /* populate descriptors with pointers and IVs */
  158. hash_d[0].ptr = inp;
  159. ciph_d[0].inp = inp;
  160. /* 5+16 is place for header and explicit IV */
  161. ciph_d[0].out = out + 5 + 16;
  162. memcpy(ciph_d[0].out - 16, IVs, 16);
  163. memcpy(ciph_d[0].iv, IVs, 16);
  164. IVs += 16;
  165. for (i = 1; i < x4; i++) {
  166. ciph_d[i].inp = hash_d[i].ptr = hash_d[i - 1].ptr + frag;
  167. ciph_d[i].out = ciph_d[i - 1].out + packlen;
  168. memcpy(ciph_d[i].out - 16, IVs, 16);
  169. memcpy(ciph_d[i].iv, IVs, 16);
  170. IVs += 16;
  171. }
  172. # if defined(BSWAP8)
  173. memcpy(blocks[0].c, key->md.data, 8);
  174. seqnum = BSWAP8(blocks[0].q[0]);
  175. # endif
  176. for (i = 0; i < x4; i++) {
  177. unsigned int len = (i == (x4 - 1) ? last : frag);
  178. # if !defined(BSWAP8)
  179. unsigned int carry, j;
  180. # endif
  181. ctx->A[i] = key->md.h[0];
  182. ctx->B[i] = key->md.h[1];
  183. ctx->C[i] = key->md.h[2];
  184. ctx->D[i] = key->md.h[3];
  185. ctx->E[i] = key->md.h[4];
  186. ctx->F[i] = key->md.h[5];
  187. ctx->G[i] = key->md.h[6];
  188. ctx->H[i] = key->md.h[7];
  189. /* fix seqnum */
  190. # if defined(BSWAP8)
  191. blocks[i].q[0] = BSWAP8(seqnum + i);
  192. # else
  193. for (carry = i, j = 8; j--;) {
  194. blocks[i].c[j] = ((u8 *)key->md.data)[j] + carry;
  195. carry = (blocks[i].c[j] - carry) >> (sizeof(carry) * 8 - 1);
  196. }
  197. # endif
  198. blocks[i].c[8] = ((u8 *)key->md.data)[8];
  199. blocks[i].c[9] = ((u8 *)key->md.data)[9];
  200. blocks[i].c[10] = ((u8 *)key->md.data)[10];
  201. /* fix length */
  202. blocks[i].c[11] = (u8)(len >> 8);
  203. blocks[i].c[12] = (u8)(len);
  204. memcpy(blocks[i].c + 13, hash_d[i].ptr, 64 - 13);
  205. hash_d[i].ptr += 64 - 13;
  206. hash_d[i].blocks = (len - (64 - 13)) / 64;
  207. edges[i].ptr = blocks[i].c;
  208. edges[i].blocks = 1;
  209. }
  210. /* hash 13-byte headers and first 64-13 bytes of inputs */
  211. sha256_multi_block(ctx, edges, n4x);
  212. /* hash bulk inputs */
  213. # define MAXCHUNKSIZE 2048
  214. # if MAXCHUNKSIZE%64
  215. # error "MAXCHUNKSIZE is not divisible by 64"
  216. # elif MAXCHUNKSIZE
  217. /*
  218. * goal is to minimize pressure on L1 cache by moving in shorter steps,
  219. * so that hashed data is still in the cache by the time we encrypt it
  220. */
  221. minblocks = ((frag <= last ? frag : last) - (64 - 13)) / 64;
  222. if (minblocks > MAXCHUNKSIZE / 64) {
  223. for (i = 0; i < x4; i++) {
  224. edges[i].ptr = hash_d[i].ptr;
  225. edges[i].blocks = MAXCHUNKSIZE / 64;
  226. ciph_d[i].blocks = MAXCHUNKSIZE / 16;
  227. }
  228. do {
  229. sha256_multi_block(ctx, edges, n4x);
  230. aesni_multi_cbc_encrypt(ciph_d, &key->ks, n4x);
  231. for (i = 0; i < x4; i++) {
  232. edges[i].ptr = hash_d[i].ptr += MAXCHUNKSIZE;
  233. hash_d[i].blocks -= MAXCHUNKSIZE / 64;
  234. edges[i].blocks = MAXCHUNKSIZE / 64;
  235. ciph_d[i].inp += MAXCHUNKSIZE;
  236. ciph_d[i].out += MAXCHUNKSIZE;
  237. ciph_d[i].blocks = MAXCHUNKSIZE / 16;
  238. memcpy(ciph_d[i].iv, ciph_d[i].out - 16, 16);
  239. }
  240. processed += MAXCHUNKSIZE;
  241. minblocks -= MAXCHUNKSIZE / 64;
  242. } while (minblocks > MAXCHUNKSIZE / 64);
  243. }
  244. # endif
  245. # undef MAXCHUNKSIZE
  246. sha256_multi_block(ctx, hash_d, n4x);
  247. memset(blocks, 0, sizeof(blocks));
  248. for (i = 0; i < x4; i++) {
  249. unsigned int len = (i == (x4 - 1) ? last : frag),
  250. off = hash_d[i].blocks * 64;
  251. const unsigned char *ptr = hash_d[i].ptr + off;
  252. off = (len - processed) - (64 - 13) - off; /* remainder actually */
  253. memcpy(blocks[i].c, ptr, off);
  254. blocks[i].c[off] = 0x80;
  255. len += 64 + 13; /* 64 is HMAC header */
  256. len *= 8; /* convert to bits */
  257. if (off < (64 - 8)) {
  258. # ifdef BSWAP4
  259. blocks[i].d[15] = BSWAP4(len);
  260. # else
  261. PUTU32(blocks[i].c + 60, len);
  262. # endif
  263. edges[i].blocks = 1;
  264. } else {
  265. # ifdef BSWAP4
  266. blocks[i].d[31] = BSWAP4(len);
  267. # else
  268. PUTU32(blocks[i].c + 124, len);
  269. # endif
  270. edges[i].blocks = 2;
  271. }
  272. edges[i].ptr = blocks[i].c;
  273. }
  274. /* hash input tails and finalize */
  275. sha256_multi_block(ctx, edges, n4x);
  276. memset(blocks, 0, sizeof(blocks));
  277. for (i = 0; i < x4; i++) {
  278. # ifdef BSWAP4
  279. blocks[i].d[0] = BSWAP4(ctx->A[i]);
  280. ctx->A[i] = key->tail.h[0];
  281. blocks[i].d[1] = BSWAP4(ctx->B[i]);
  282. ctx->B[i] = key->tail.h[1];
  283. blocks[i].d[2] = BSWAP4(ctx->C[i]);
  284. ctx->C[i] = key->tail.h[2];
  285. blocks[i].d[3] = BSWAP4(ctx->D[i]);
  286. ctx->D[i] = key->tail.h[3];
  287. blocks[i].d[4] = BSWAP4(ctx->E[i]);
  288. ctx->E[i] = key->tail.h[4];
  289. blocks[i].d[5] = BSWAP4(ctx->F[i]);
  290. ctx->F[i] = key->tail.h[5];
  291. blocks[i].d[6] = BSWAP4(ctx->G[i]);
  292. ctx->G[i] = key->tail.h[6];
  293. blocks[i].d[7] = BSWAP4(ctx->H[i]);
  294. ctx->H[i] = key->tail.h[7];
  295. blocks[i].c[32] = 0x80;
  296. blocks[i].d[15] = BSWAP4((64 + 32) * 8);
  297. # else
  298. PUTU32(blocks[i].c + 0, ctx->A[i]);
  299. ctx->A[i] = key->tail.h[0];
  300. PUTU32(blocks[i].c + 4, ctx->B[i]);
  301. ctx->B[i] = key->tail.h[1];
  302. PUTU32(blocks[i].c + 8, ctx->C[i]);
  303. ctx->C[i] = key->tail.h[2];
  304. PUTU32(blocks[i].c + 12, ctx->D[i]);
  305. ctx->D[i] = key->tail.h[3];
  306. PUTU32(blocks[i].c + 16, ctx->E[i]);
  307. ctx->E[i] = key->tail.h[4];
  308. PUTU32(blocks[i].c + 20, ctx->F[i]);
  309. ctx->F[i] = key->tail.h[5];
  310. PUTU32(blocks[i].c + 24, ctx->G[i]);
  311. ctx->G[i] = key->tail.h[6];
  312. PUTU32(blocks[i].c + 28, ctx->H[i]);
  313. ctx->H[i] = key->tail.h[7];
  314. blocks[i].c[32] = 0x80;
  315. PUTU32(blocks[i].c + 60, (64 + 32) * 8);
  316. # endif
  317. edges[i].ptr = blocks[i].c;
  318. edges[i].blocks = 1;
  319. }
  320. /* finalize MACs */
  321. sha256_multi_block(ctx, edges, n4x);
  322. for (i = 0; i < x4; i++) {
  323. unsigned int len = (i == (x4 - 1) ? last : frag), pad, j;
  324. unsigned char *out0 = out;
  325. memcpy(ciph_d[i].out, ciph_d[i].inp, len - processed);
  326. ciph_d[i].inp = ciph_d[i].out;
  327. out += 5 + 16 + len;
  328. /* write MAC */
  329. PUTU32(out + 0, ctx->A[i]);
  330. PUTU32(out + 4, ctx->B[i]);
  331. PUTU32(out + 8, ctx->C[i]);
  332. PUTU32(out + 12, ctx->D[i]);
  333. PUTU32(out + 16, ctx->E[i]);
  334. PUTU32(out + 20, ctx->F[i]);
  335. PUTU32(out + 24, ctx->G[i]);
  336. PUTU32(out + 28, ctx->H[i]);
  337. out += 32;
  338. len += 32;
  339. /* pad */
  340. pad = 15 - len % 16;
  341. for (j = 0; j <= pad; j++)
  342. *(out++) = pad;
  343. len += pad + 1;
  344. ciph_d[i].blocks = (len - processed) / 16;
  345. len += 16; /* account for explicit iv */
  346. /* arrange header */
  347. out0[0] = ((u8 *)key->md.data)[8];
  348. out0[1] = ((u8 *)key->md.data)[9];
  349. out0[2] = ((u8 *)key->md.data)[10];
  350. out0[3] = (u8)(len >> 8);
  351. out0[4] = (u8)(len);
  352. ret += len + 5;
  353. inp += frag;
  354. }
  355. aesni_multi_cbc_encrypt(ciph_d, &key->ks, n4x);
  356. OPENSSL_cleanse(blocks, sizeof(blocks));
  357. OPENSSL_cleanse(ctx, sizeof(*ctx));
  358. return ret;
  359. }
  360. # endif
  361. static int aesni_cbc_hmac_sha256_cipher(EVP_CIPHER_CTX *ctx,
  362. unsigned char *out,
  363. const unsigned char *in, size_t len)
  364. {
  365. EVP_AES_HMAC_SHA256 *key = data(ctx);
  366. unsigned int l;
  367. size_t plen = key->payload_length, iv = 0, /* explicit IV in TLS 1.1 and
  368. * later */
  369. sha_off = 0;
  370. # if defined(STITCHED_CALL)
  371. size_t aes_off = 0, blocks;
  372. sha_off = SHA256_CBLOCK - key->md.num;
  373. # endif
  374. key->payload_length = NO_PAYLOAD_LENGTH;
  375. if (len % AES_BLOCK_SIZE)
  376. return 0;
  377. if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
  378. if (plen == NO_PAYLOAD_LENGTH)
  379. plen = len;
  380. else if (len !=
  381. ((plen + SHA256_DIGEST_LENGTH +
  382. AES_BLOCK_SIZE) & -AES_BLOCK_SIZE))
  383. return 0;
  384. else if (key->aux.tls_ver >= TLS1_1_VERSION)
  385. iv = AES_BLOCK_SIZE;
  386. # if defined(STITCHED_CALL)
  387. /*
  388. * Assembly stitch handles AVX-capable processors, but its
  389. * performance is not optimal on AMD Jaguar, ~40% worse, for
  390. * unknown reasons. Incidentally processor in question supports
  391. * AVX, but not AMD-specific XOP extension, which can be used
  392. * to identify it and avoid stitch invocation. So that after we
  393. * establish that current CPU supports AVX, we even see if it's
  394. * either even XOP-capable Bulldozer-based or GenuineIntel one.
  395. * But SHAEXT-capable go ahead...
  396. */
  397. if (((OPENSSL_ia32cap_P[2] & (1 << 29)) || /* SHAEXT? */
  398. ((OPENSSL_ia32cap_P[1] & (1 << (60 - 32))) && /* AVX? */
  399. ((OPENSSL_ia32cap_P[1] & (1 << (43 - 32))) /* XOP? */
  400. | (OPENSSL_ia32cap_P[0] & (1 << 30))))) && /* "Intel CPU"? */
  401. plen > (sha_off + iv) &&
  402. (blocks = (plen - (sha_off + iv)) / SHA256_CBLOCK)) {
  403. SHA256_Update(&key->md, in + iv, sha_off);
  404. (void)aesni_cbc_sha256_enc(in, out, blocks, &key->ks,
  405. ctx->iv, &key->md, in + iv + sha_off);
  406. blocks *= SHA256_CBLOCK;
  407. aes_off += blocks;
  408. sha_off += blocks;
  409. key->md.Nh += blocks >> 29;
  410. key->md.Nl += blocks <<= 3;
  411. if (key->md.Nl < (unsigned int)blocks)
  412. key->md.Nh++;
  413. } else {
  414. sha_off = 0;
  415. }
  416. # endif
  417. sha_off += iv;
  418. SHA256_Update(&key->md, in + sha_off, plen - sha_off);
  419. if (plen != len) { /* "TLS" mode of operation */
  420. if (in != out)
  421. memcpy(out + aes_off, in + aes_off, plen - aes_off);
  422. /* calculate HMAC and append it to payload */
  423. SHA256_Final(out + plen, &key->md);
  424. key->md = key->tail;
  425. SHA256_Update(&key->md, out + plen, SHA256_DIGEST_LENGTH);
  426. SHA256_Final(out + plen, &key->md);
  427. /* pad the payload|hmac */
  428. plen += SHA256_DIGEST_LENGTH;
  429. for (l = len - plen - 1; plen < len; plen++)
  430. out[plen] = l;
  431. /* encrypt HMAC|padding at once */
  432. aesni_cbc_encrypt(out + aes_off, out + aes_off, len - aes_off,
  433. &key->ks, ctx->iv, 1);
  434. } else {
  435. aesni_cbc_encrypt(in + aes_off, out + aes_off, len - aes_off,
  436. &key->ks, ctx->iv, 1);
  437. }
  438. } else {
  439. union {
  440. unsigned int u[SHA256_DIGEST_LENGTH / sizeof(unsigned int)];
  441. unsigned char c[64 + SHA256_DIGEST_LENGTH];
  442. } mac, *pmac;
  443. /* arrange cache line alignment */
  444. pmac = (void *)(((size_t)mac.c + 63) & ((size_t)0 - 64));
  445. /* decrypt HMAC|padding at once */
  446. aesni_cbc_encrypt(in, out, len, &key->ks,
  447. ctx->iv, 0);
  448. if (plen != NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */
  449. size_t inp_len, mask, j, i;
  450. unsigned int res, maxpad, pad, bitlen;
  451. int ret = 1;
  452. union {
  453. unsigned int u[SHA_LBLOCK];
  454. unsigned char c[SHA256_CBLOCK];
  455. } *data = (void *)key->md.data;
  456. if ((key->aux.tls_aad[plen - 4] << 8 | key->aux.tls_aad[plen - 3])
  457. >= TLS1_1_VERSION)
  458. iv = AES_BLOCK_SIZE;
  459. if (len < (iv + SHA256_DIGEST_LENGTH + 1))
  460. return 0;
  461. /* omit explicit iv */
  462. out += iv;
  463. len -= iv;
  464. /* figure out payload length */
  465. pad = out[len - 1];
  466. maxpad = len - (SHA256_DIGEST_LENGTH + 1);
  467. maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8);
  468. maxpad &= 255;
  469. mask = constant_time_ge(maxpad, pad);
  470. ret &= mask;
  471. /*
  472. * If pad is invalid then we will fail the above test but we must
  473. * continue anyway because we are in constant time code. However,
  474. * we'll use the maxpad value instead of the supplied pad to make
  475. * sure we perform well defined pointer arithmetic.
  476. */
  477. pad = constant_time_select(mask, pad, maxpad);
  478. inp_len = len - (SHA256_DIGEST_LENGTH + pad + 1);
  479. key->aux.tls_aad[plen - 2] = inp_len >> 8;
  480. key->aux.tls_aad[plen - 1] = inp_len;
  481. /* calculate HMAC */
  482. key->md = key->head;
  483. SHA256_Update(&key->md, key->aux.tls_aad, plen);
  484. # if 1 /* see original reference version in #else */
  485. len -= SHA256_DIGEST_LENGTH; /* amend mac */
  486. if (len >= (256 + SHA256_CBLOCK)) {
  487. j = (len - (256 + SHA256_CBLOCK)) & (0 - SHA256_CBLOCK);
  488. j += SHA256_CBLOCK - key->md.num;
  489. SHA256_Update(&key->md, out, j);
  490. out += j;
  491. len -= j;
  492. inp_len -= j;
  493. }
  494. /* but pretend as if we hashed padded payload */
  495. bitlen = key->md.Nl + (inp_len << 3); /* at most 18 bits */
  496. # ifdef BSWAP4
  497. bitlen = BSWAP4(bitlen);
  498. # else
  499. mac.c[0] = 0;
  500. mac.c[1] = (unsigned char)(bitlen >> 16);
  501. mac.c[2] = (unsigned char)(bitlen >> 8);
  502. mac.c[3] = (unsigned char)bitlen;
  503. bitlen = mac.u[0];
  504. # endif
  505. pmac->u[0] = 0;
  506. pmac->u[1] = 0;
  507. pmac->u[2] = 0;
  508. pmac->u[3] = 0;
  509. pmac->u[4] = 0;
  510. pmac->u[5] = 0;
  511. pmac->u[6] = 0;
  512. pmac->u[7] = 0;
  513. for (res = key->md.num, j = 0; j < len; j++) {
  514. size_t c = out[j];
  515. mask = (j - inp_len) >> (sizeof(j) * 8 - 8);
  516. c &= mask;
  517. c |= 0x80 & ~mask & ~((inp_len - j) >> (sizeof(j) * 8 - 8));
  518. data->c[res++] = (unsigned char)c;
  519. if (res != SHA256_CBLOCK)
  520. continue;
  521. /* j is not incremented yet */
  522. mask = 0 - ((inp_len + 7 - j) >> (sizeof(j) * 8 - 1));
  523. data->u[SHA_LBLOCK - 1] |= bitlen & mask;
  524. sha256_block_data_order(&key->md, data, 1);
  525. mask &= 0 - ((j - inp_len - 72) >> (sizeof(j) * 8 - 1));
  526. pmac->u[0] |= key->md.h[0] & mask;
  527. pmac->u[1] |= key->md.h[1] & mask;
  528. pmac->u[2] |= key->md.h[2] & mask;
  529. pmac->u[3] |= key->md.h[3] & mask;
  530. pmac->u[4] |= key->md.h[4] & mask;
  531. pmac->u[5] |= key->md.h[5] & mask;
  532. pmac->u[6] |= key->md.h[6] & mask;
  533. pmac->u[7] |= key->md.h[7] & mask;
  534. res = 0;
  535. }
  536. for (i = res; i < SHA256_CBLOCK; i++, j++)
  537. data->c[i] = 0;
  538. if (res > SHA256_CBLOCK - 8) {
  539. mask = 0 - ((inp_len + 8 - j) >> (sizeof(j) * 8 - 1));
  540. data->u[SHA_LBLOCK - 1] |= bitlen & mask;
  541. sha256_block_data_order(&key->md, data, 1);
  542. mask &= 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
  543. pmac->u[0] |= key->md.h[0] & mask;
  544. pmac->u[1] |= key->md.h[1] & mask;
  545. pmac->u[2] |= key->md.h[2] & mask;
  546. pmac->u[3] |= key->md.h[3] & mask;
  547. pmac->u[4] |= key->md.h[4] & mask;
  548. pmac->u[5] |= key->md.h[5] & mask;
  549. pmac->u[6] |= key->md.h[6] & mask;
  550. pmac->u[7] |= key->md.h[7] & mask;
  551. memset(data, 0, SHA256_CBLOCK);
  552. j += 64;
  553. }
  554. data->u[SHA_LBLOCK - 1] = bitlen;
  555. sha256_block_data_order(&key->md, data, 1);
  556. mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
  557. pmac->u[0] |= key->md.h[0] & mask;
  558. pmac->u[1] |= key->md.h[1] & mask;
  559. pmac->u[2] |= key->md.h[2] & mask;
  560. pmac->u[3] |= key->md.h[3] & mask;
  561. pmac->u[4] |= key->md.h[4] & mask;
  562. pmac->u[5] |= key->md.h[5] & mask;
  563. pmac->u[6] |= key->md.h[6] & mask;
  564. pmac->u[7] |= key->md.h[7] & mask;
  565. # ifdef BSWAP4
  566. pmac->u[0] = BSWAP4(pmac->u[0]);
  567. pmac->u[1] = BSWAP4(pmac->u[1]);
  568. pmac->u[2] = BSWAP4(pmac->u[2]);
  569. pmac->u[3] = BSWAP4(pmac->u[3]);
  570. pmac->u[4] = BSWAP4(pmac->u[4]);
  571. pmac->u[5] = BSWAP4(pmac->u[5]);
  572. pmac->u[6] = BSWAP4(pmac->u[6]);
  573. pmac->u[7] = BSWAP4(pmac->u[7]);
  574. # else
  575. for (i = 0; i < 8; i++) {
  576. res = pmac->u[i];
  577. pmac->c[4 * i + 0] = (unsigned char)(res >> 24);
  578. pmac->c[4 * i + 1] = (unsigned char)(res >> 16);
  579. pmac->c[4 * i + 2] = (unsigned char)(res >> 8);
  580. pmac->c[4 * i + 3] = (unsigned char)res;
  581. }
  582. # endif
  583. len += SHA256_DIGEST_LENGTH;
  584. # else
  585. SHA256_Update(&key->md, out, inp_len);
  586. res = key->md.num;
  587. SHA256_Final(pmac->c, &key->md);
  588. {
  589. unsigned int inp_blocks, pad_blocks;
  590. /* but pretend as if we hashed padded payload */
  591. inp_blocks =
  592. 1 + ((SHA256_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
  593. res += (unsigned int)(len - inp_len);
  594. pad_blocks = res / SHA256_CBLOCK;
  595. res %= SHA256_CBLOCK;
  596. pad_blocks +=
  597. 1 + ((SHA256_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
  598. for (; inp_blocks < pad_blocks; inp_blocks++)
  599. sha1_block_data_order(&key->md, data, 1);
  600. }
  601. # endif /* pre-lucky-13 reference version of above */
  602. key->md = key->tail;
  603. SHA256_Update(&key->md, pmac->c, SHA256_DIGEST_LENGTH);
  604. SHA256_Final(pmac->c, &key->md);
  605. /* verify HMAC */
  606. out += inp_len;
  607. len -= inp_len;
  608. # if 1 /* see original reference version in #else */
  609. {
  610. unsigned char *p =
  611. out + len - 1 - maxpad - SHA256_DIGEST_LENGTH;
  612. size_t off = out - p;
  613. unsigned int c, cmask;
  614. for (res = 0, i = 0, j = 0; j < maxpad + SHA256_DIGEST_LENGTH;
  615. j++) {
  616. c = p[j];
  617. cmask =
  618. ((int)(j - off - SHA256_DIGEST_LENGTH)) >>
  619. (sizeof(int) * 8 - 1);
  620. res |= (c ^ pad) & ~cmask; /* ... and padding */
  621. cmask &= ((int)(off - 1 - j)) >> (sizeof(int) * 8 - 1);
  622. res |= (c ^ pmac->c[i]) & cmask;
  623. i += 1 & cmask;
  624. }
  625. res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
  626. ret &= (int)~res;
  627. }
  628. # else /* pre-lucky-13 reference version of above */
  629. for (res = 0, i = 0; i < SHA256_DIGEST_LENGTH; i++)
  630. res |= out[i] ^ pmac->c[i];
  631. res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
  632. ret &= (int)~res;
  633. /* verify padding */
  634. pad = (pad & ~res) | (maxpad & res);
  635. out = out + len - 1 - pad;
  636. for (res = 0, i = 0; i < pad; i++)
  637. res |= out[i] ^ pad;
  638. res = (0 - res) >> (sizeof(res) * 8 - 1);
  639. ret &= (int)~res;
  640. # endif
  641. return ret;
  642. } else {
  643. SHA256_Update(&key->md, out, len);
  644. }
  645. }
  646. return 1;
  647. }
  648. static int aesni_cbc_hmac_sha256_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
  649. void *ptr)
  650. {
  651. EVP_AES_HMAC_SHA256 *key = data(ctx);
  652. unsigned int u_arg = (unsigned int)arg;
  653. switch (type) {
  654. case EVP_CTRL_AEAD_SET_MAC_KEY:
  655. {
  656. unsigned int i;
  657. unsigned char hmac_key[64];
  658. memset(hmac_key, 0, sizeof(hmac_key));
  659. if (arg < 0)
  660. return -1;
  661. if (u_arg > sizeof(hmac_key)) {
  662. SHA256_Init(&key->head);
  663. SHA256_Update(&key->head, ptr, arg);
  664. SHA256_Final(hmac_key, &key->head);
  665. } else {
  666. memcpy(hmac_key, ptr, arg);
  667. }
  668. for (i = 0; i < sizeof(hmac_key); i++)
  669. hmac_key[i] ^= 0x36; /* ipad */
  670. SHA256_Init(&key->head);
  671. SHA256_Update(&key->head, hmac_key, sizeof(hmac_key));
  672. for (i = 0; i < sizeof(hmac_key); i++)
  673. hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
  674. SHA256_Init(&key->tail);
  675. SHA256_Update(&key->tail, hmac_key, sizeof(hmac_key));
  676. OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
  677. return 1;
  678. }
  679. case EVP_CTRL_AEAD_TLS1_AAD:
  680. {
  681. unsigned char *p = ptr;
  682. unsigned int len;
  683. if (arg != EVP_AEAD_TLS1_AAD_LEN)
  684. return -1;
  685. len = p[arg - 2] << 8 | p[arg - 1];
  686. if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
  687. key->payload_length = len;
  688. if ((key->aux.tls_ver =
  689. p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
  690. if (len < AES_BLOCK_SIZE)
  691. return 0;
  692. len -= AES_BLOCK_SIZE;
  693. p[arg - 2] = len >> 8;
  694. p[arg - 1] = len;
  695. }
  696. key->md = key->head;
  697. SHA256_Update(&key->md, p, arg);
  698. return (int)(((len + SHA256_DIGEST_LENGTH +
  699. AES_BLOCK_SIZE) & -AES_BLOCK_SIZE)
  700. - len);
  701. } else {
  702. memcpy(key->aux.tls_aad, ptr, arg);
  703. key->payload_length = arg;
  704. return SHA256_DIGEST_LENGTH;
  705. }
  706. }
  707. # if !defined(OPENSSL_NO_MULTIBLOCK)
  708. case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
  709. return (int)(5 + 16 + ((arg + 32 + 16) & -16));
  710. case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD:
  711. {
  712. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
  713. (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
  714. unsigned int n4x = 1, x4;
  715. unsigned int frag, last, packlen, inp_len;
  716. if (arg < 0)
  717. return -1;
  718. if (u_arg < sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
  719. return -1;
  720. inp_len = param->inp[11] << 8 | param->inp[12];
  721. if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
  722. if ((param->inp[9] << 8 | param->inp[10]) < TLS1_1_VERSION)
  723. return -1;
  724. if (inp_len) {
  725. if (inp_len < 4096)
  726. return 0; /* too short */
  727. if (inp_len >= 8192 && OPENSSL_ia32cap_P[2] & (1 << 5))
  728. n4x = 2; /* AVX2 */
  729. } else if ((n4x = param->interleave / 4) && n4x <= 2)
  730. inp_len = param->len;
  731. else
  732. return -1;
  733. key->md = key->head;
  734. SHA256_Update(&key->md, param->inp, 13);
  735. x4 = 4 * n4x;
  736. n4x += 1;
  737. frag = inp_len >> n4x;
  738. last = inp_len + frag - (frag << n4x);
  739. if (last > frag && ((last + 13 + 9) % 64 < (x4 - 1))) {
  740. frag++;
  741. last -= x4 - 1;
  742. }
  743. packlen = 5 + 16 + ((frag + 32 + 16) & -16);
  744. packlen = (packlen << n4x) - packlen;
  745. packlen += 5 + 16 + ((last + 32 + 16) & -16);
  746. param->interleave = x4;
  747. return (int)packlen;
  748. } else
  749. return -1; /* not yet */
  750. }
  751. case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT:
  752. {
  753. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
  754. (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
  755. return (int)tls1_1_multi_block_encrypt(key, param->out,
  756. param->inp, param->len,
  757. param->interleave / 4);
  758. }
  759. case EVP_CTRL_TLS1_1_MULTIBLOCK_DECRYPT:
  760. # endif
  761. default:
  762. return -1;
  763. }
  764. }
  765. static EVP_CIPHER aesni_128_cbc_hmac_sha256_cipher = {
  766. # ifdef NID_aes_128_cbc_hmac_sha256
  767. NID_aes_128_cbc_hmac_sha256,
  768. # else
  769. NID_undef,
  770. # endif
  771. AES_BLOCK_SIZE, 16, AES_BLOCK_SIZE,
  772. EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
  773. EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
  774. EVP_ORIG_GLOBAL,
  775. aesni_cbc_hmac_sha256_init_key,
  776. aesni_cbc_hmac_sha256_cipher,
  777. NULL,
  778. sizeof(EVP_AES_HMAC_SHA256),
  779. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
  780. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
  781. aesni_cbc_hmac_sha256_ctrl,
  782. NULL
  783. };
  784. static EVP_CIPHER aesni_256_cbc_hmac_sha256_cipher = {
  785. # ifdef NID_aes_256_cbc_hmac_sha256
  786. NID_aes_256_cbc_hmac_sha256,
  787. # else
  788. NID_undef,
  789. # endif
  790. AES_BLOCK_SIZE, 32, AES_BLOCK_SIZE,
  791. EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
  792. EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
  793. EVP_ORIG_GLOBAL,
  794. aesni_cbc_hmac_sha256_init_key,
  795. aesni_cbc_hmac_sha256_cipher,
  796. NULL,
  797. sizeof(EVP_AES_HMAC_SHA256),
  798. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
  799. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
  800. aesni_cbc_hmac_sha256_ctrl,
  801. NULL
  802. };
  803. const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha256(void)
  804. {
  805. return ((OPENSSL_ia32cap_P[1] & AESNI_CAPABLE) &&
  806. aesni_cbc_sha256_enc(NULL, NULL, 0, NULL, NULL, NULL, NULL) ?
  807. &aesni_128_cbc_hmac_sha256_cipher : NULL);
  808. }
  809. const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha256(void)
  810. {
  811. return ((OPENSSL_ia32cap_P[1] & AESNI_CAPABLE) &&
  812. aesni_cbc_sha256_enc(NULL, NULL, 0, NULL, NULL, NULL, NULL) ?
  813. &aesni_256_cbc_hmac_sha256_cipher : NULL);
  814. }
  815. #else
  816. const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha256(void)
  817. {
  818. return NULL;
  819. }
  820. const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha256(void)
  821. {
  822. return NULL;
  823. }
  824. #endif