cipher_aes_cbc_hmac_sha1_hw.c 25 KB

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