cipher_aes_cbc_hmac_sha256_hw.c 28 KB

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