cipher_aes_cbc_hmac_sha256_hw.c 28 KB

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