cipher_aes_cbc_hmac_sha1_hw.c 25 KB

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