cipher_aes_cbc_hmac_sha1_hw.c 26 KB

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