2
0

e_aes_cbc_hmac_sha1.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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. * AES 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 <stdio.h>
  16. #include <string.h>
  17. #include <openssl/opensslconf.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/objects.h>
  20. #include <openssl/aes.h>
  21. #include <openssl/sha.h>
  22. #include <openssl/rand.h>
  23. #include "internal/cryptlib.h"
  24. #include "crypto/modes.h"
  25. #include "crypto/evp.h"
  26. #include "internal/constant_time.h"
  27. typedef struct {
  28. AES_KEY ks;
  29. SHA_CTX head, tail, md;
  30. size_t payload_length; /* AAD length in decrypt case */
  31. union {
  32. unsigned int tls_ver;
  33. unsigned char tls_aad[16]; /* 13 used */
  34. } aux;
  35. } EVP_AES_HMAC_SHA1;
  36. #define NO_PAYLOAD_LENGTH ((size_t)-1)
  37. #if defined(AES_ASM) && ( \
  38. defined(__x86_64) || defined(__x86_64__) || \
  39. defined(_M_AMD64) || defined(_M_X64) )
  40. # define AESNI_CAPABLE (1<<(57-32))
  41. int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
  42. AES_KEY *key);
  43. int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
  44. AES_KEY *key);
  45. void aesni_cbc_encrypt(const unsigned char *in,
  46. unsigned char *out,
  47. size_t length,
  48. const AES_KEY *key, unsigned char *ivec, int enc);
  49. void aesni_cbc_sha1_enc(const void *inp, void *out, size_t blocks,
  50. const AES_KEY *key, unsigned char iv[16],
  51. SHA_CTX *ctx, const void *in0);
  52. void aesni256_cbc_sha1_dec(const void *inp, void *out, size_t blocks,
  53. const AES_KEY *key, unsigned char iv[16],
  54. SHA_CTX *ctx, const void *in0);
  55. # define data(ctx) ((EVP_AES_HMAC_SHA1 *)EVP_CIPHER_CTX_get_cipher_data(ctx))
  56. static int aesni_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
  57. const unsigned char *inkey,
  58. const unsigned char *iv, int enc)
  59. {
  60. EVP_AES_HMAC_SHA1 *key = data(ctx);
  61. int ret;
  62. if (enc)
  63. ret = aesni_set_encrypt_key(inkey,
  64. EVP_CIPHER_CTX_key_length(ctx) * 8,
  65. &key->ks);
  66. else
  67. ret = aesni_set_decrypt_key(inkey,
  68. EVP_CIPHER_CTX_key_length(ctx) * 8,
  69. &key->ks);
  70. SHA1_Init(&key->head); /* handy when benchmarking */
  71. key->tail = key->head;
  72. key->md = key->head;
  73. key->payload_length = NO_PAYLOAD_LENGTH;
  74. return ret < 0 ? 0 : 1;
  75. }
  76. # define STITCHED_CALL
  77. # undef STITCHED_DECRYPT_CALL
  78. # if !defined(STITCHED_CALL)
  79. # define aes_off 0
  80. # endif
  81. void sha1_block_data_order(void *c, const void *p, size_t len);
  82. static void sha1_update(SHA_CTX *c, const void *data, size_t len)
  83. {
  84. const unsigned char *ptr = data;
  85. size_t res;
  86. if ((res = c->num)) {
  87. res = SHA_CBLOCK - res;
  88. if (len < res)
  89. res = len;
  90. SHA1_Update(c, ptr, res);
  91. ptr += res;
  92. len -= res;
  93. }
  94. res = len % SHA_CBLOCK;
  95. len -= res;
  96. if (len) {
  97. sha1_block_data_order(c, ptr, len / SHA_CBLOCK);
  98. ptr += len;
  99. c->Nh += len >> 29;
  100. c->Nl += len <<= 3;
  101. if (c->Nl < (unsigned int)len)
  102. c->Nh++;
  103. }
  104. if (res)
  105. SHA1_Update(c, ptr, res);
  106. }
  107. # ifdef SHA1_Update
  108. # undef SHA1_Update
  109. # endif
  110. # define SHA1_Update sha1_update
  111. # if !defined(OPENSSL_NO_MULTIBLOCK)
  112. typedef struct {
  113. unsigned int A[8], B[8], C[8], D[8], E[8];
  114. } SHA1_MB_CTX;
  115. typedef struct {
  116. const unsigned char *ptr;
  117. int blocks;
  118. } HASH_DESC;
  119. void sha1_multi_block(SHA1_MB_CTX *, const HASH_DESC *, int);
  120. typedef struct {
  121. const unsigned char *inp;
  122. unsigned char *out;
  123. int blocks;
  124. u64 iv[2];
  125. } CIPH_DESC;
  126. void aesni_multi_cbc_encrypt(CIPH_DESC *, void *, int);
  127. static size_t tls1_1_multi_block_encrypt(EVP_AES_HMAC_SHA1 *key,
  128. unsigned char *out,
  129. const unsigned char *inp,
  130. size_t inp_len, int n4x)
  131. { /* n4x is 1 or 2 */
  132. HASH_DESC hash_d[8], edges[8];
  133. CIPH_DESC ciph_d[8];
  134. unsigned char storage[sizeof(SHA1_MB_CTX) + 32];
  135. union {
  136. u64 q[16];
  137. u32 d[32];
  138. u8 c[128];
  139. } blocks[8];
  140. SHA1_MB_CTX *ctx;
  141. unsigned int frag, last, packlen, i, x4 = 4 * n4x, minblocks, processed =
  142. 0;
  143. size_t ret = 0;
  144. u8 *IVs;
  145. # if defined(BSWAP8)
  146. u64 seqnum;
  147. # endif
  148. /* ask for IVs in bulk */
  149. if (RAND_bytes((IVs = blocks[0].c), 16 * x4) <= 0)
  150. return 0;
  151. ctx = (SHA1_MB_CTX *) (storage + 32 - ((size_t)storage % 32)); /* align */
  152. frag = (unsigned int)inp_len >> (1 + n4x);
  153. last = (unsigned int)inp_len + frag - (frag << (1 + n4x));
  154. if (last > frag && ((last + 13 + 9) % 64) < (x4 - 1)) {
  155. frag++;
  156. last -= x4 - 1;
  157. }
  158. packlen = 5 + 16 + ((frag + 20 + 16) & -16);
  159. /* populate descriptors with pointers and IVs */
  160. hash_d[0].ptr = inp;
  161. ciph_d[0].inp = inp;
  162. /* 5+16 is place for header and explicit IV */
  163. ciph_d[0].out = out + 5 + 16;
  164. memcpy(ciph_d[0].out - 16, IVs, 16);
  165. memcpy(ciph_d[0].iv, IVs, 16);
  166. IVs += 16;
  167. for (i = 1; i < x4; i++) {
  168. ciph_d[i].inp = hash_d[i].ptr = hash_d[i - 1].ptr + frag;
  169. ciph_d[i].out = ciph_d[i - 1].out + packlen;
  170. memcpy(ciph_d[i].out - 16, IVs, 16);
  171. memcpy(ciph_d[i].iv, IVs, 16);
  172. IVs += 16;
  173. }
  174. # if defined(BSWAP8)
  175. memcpy(blocks[0].c, key->md.data, 8);
  176. seqnum = BSWAP8(blocks[0].q[0]);
  177. # endif
  178. for (i = 0; i < x4; i++) {
  179. unsigned int len = (i == (x4 - 1) ? last : frag);
  180. # if !defined(BSWAP8)
  181. unsigned int carry, j;
  182. # endif
  183. ctx->A[i] = key->md.h0;
  184. ctx->B[i] = key->md.h1;
  185. ctx->C[i] = key->md.h2;
  186. ctx->D[i] = key->md.h3;
  187. ctx->E[i] = key->md.h4;
  188. /* fix seqnum */
  189. # if defined(BSWAP8)
  190. blocks[i].q[0] = BSWAP8(seqnum + i);
  191. # else
  192. for (carry = i, j = 8; j--;) {
  193. blocks[i].c[j] = ((u8 *)key->md.data)[j] + carry;
  194. carry = (blocks[i].c[j] - carry) >> (sizeof(carry) * 8 - 1);
  195. }
  196. # endif
  197. blocks[i].c[8] = ((u8 *)key->md.data)[8];
  198. blocks[i].c[9] = ((u8 *)key->md.data)[9];
  199. blocks[i].c[10] = ((u8 *)key->md.data)[10];
  200. /* fix length */
  201. blocks[i].c[11] = (u8)(len >> 8);
  202. blocks[i].c[12] = (u8)(len);
  203. memcpy(blocks[i].c + 13, hash_d[i].ptr, 64 - 13);
  204. hash_d[i].ptr += 64 - 13;
  205. hash_d[i].blocks = (len - (64 - 13)) / 64;
  206. edges[i].ptr = blocks[i].c;
  207. edges[i].blocks = 1;
  208. }
  209. /* hash 13-byte headers and first 64-13 bytes of inputs */
  210. sha1_multi_block(ctx, edges, n4x);
  211. /* hash bulk inputs */
  212. # define MAXCHUNKSIZE 2048
  213. # if MAXCHUNKSIZE%64
  214. # error "MAXCHUNKSIZE is not divisible by 64"
  215. # elif MAXCHUNKSIZE
  216. /*
  217. * goal is to minimize pressure on L1 cache by moving in shorter steps,
  218. * so that hashed data is still in the cache by the time we encrypt it
  219. */
  220. minblocks = ((frag <= last ? frag : last) - (64 - 13)) / 64;
  221. if (minblocks > MAXCHUNKSIZE / 64) {
  222. for (i = 0; i < x4; i++) {
  223. edges[i].ptr = hash_d[i].ptr;
  224. edges[i].blocks = MAXCHUNKSIZE / 64;
  225. ciph_d[i].blocks = MAXCHUNKSIZE / 16;
  226. }
  227. do {
  228. sha1_multi_block(ctx, edges, n4x);
  229. aesni_multi_cbc_encrypt(ciph_d, &key->ks, n4x);
  230. for (i = 0; i < x4; i++) {
  231. edges[i].ptr = hash_d[i].ptr += MAXCHUNKSIZE;
  232. hash_d[i].blocks -= MAXCHUNKSIZE / 64;
  233. edges[i].blocks = MAXCHUNKSIZE / 64;
  234. ciph_d[i].inp += MAXCHUNKSIZE;
  235. ciph_d[i].out += MAXCHUNKSIZE;
  236. ciph_d[i].blocks = MAXCHUNKSIZE / 16;
  237. memcpy(ciph_d[i].iv, ciph_d[i].out - 16, 16);
  238. }
  239. processed += MAXCHUNKSIZE;
  240. minblocks -= MAXCHUNKSIZE / 64;
  241. } while (minblocks > MAXCHUNKSIZE / 64);
  242. }
  243. # endif
  244. # undef MAXCHUNKSIZE
  245. sha1_multi_block(ctx, hash_d, n4x);
  246. memset(blocks, 0, sizeof(blocks));
  247. for (i = 0; i < x4; i++) {
  248. unsigned int len = (i == (x4 - 1) ? last : frag),
  249. off = hash_d[i].blocks * 64;
  250. const unsigned char *ptr = hash_d[i].ptr + off;
  251. off = (len - processed) - (64 - 13) - off; /* remainder actually */
  252. memcpy(blocks[i].c, ptr, off);
  253. blocks[i].c[off] = 0x80;
  254. len += 64 + 13; /* 64 is HMAC header */
  255. len *= 8; /* convert to bits */
  256. if (off < (64 - 8)) {
  257. # ifdef BSWAP4
  258. blocks[i].d[15] = BSWAP4(len);
  259. # else
  260. PUTU32(blocks[i].c + 60, len);
  261. # endif
  262. edges[i].blocks = 1;
  263. } else {
  264. # ifdef BSWAP4
  265. blocks[i].d[31] = BSWAP4(len);
  266. # else
  267. PUTU32(blocks[i].c + 124, len);
  268. # endif
  269. edges[i].blocks = 2;
  270. }
  271. edges[i].ptr = blocks[i].c;
  272. }
  273. /* hash input tails and finalize */
  274. sha1_multi_block(ctx, edges, n4x);
  275. memset(blocks, 0, sizeof(blocks));
  276. for (i = 0; i < x4; i++) {
  277. # ifdef BSWAP4
  278. blocks[i].d[0] = BSWAP4(ctx->A[i]);
  279. ctx->A[i] = key->tail.h0;
  280. blocks[i].d[1] = BSWAP4(ctx->B[i]);
  281. ctx->B[i] = key->tail.h1;
  282. blocks[i].d[2] = BSWAP4(ctx->C[i]);
  283. ctx->C[i] = key->tail.h2;
  284. blocks[i].d[3] = BSWAP4(ctx->D[i]);
  285. ctx->D[i] = key->tail.h3;
  286. blocks[i].d[4] = BSWAP4(ctx->E[i]);
  287. ctx->E[i] = key->tail.h4;
  288. blocks[i].c[20] = 0x80;
  289. blocks[i].d[15] = BSWAP4((64 + 20) * 8);
  290. # else
  291. PUTU32(blocks[i].c + 0, ctx->A[i]);
  292. ctx->A[i] = key->tail.h0;
  293. PUTU32(blocks[i].c + 4, ctx->B[i]);
  294. ctx->B[i] = key->tail.h1;
  295. PUTU32(blocks[i].c + 8, ctx->C[i]);
  296. ctx->C[i] = key->tail.h2;
  297. PUTU32(blocks[i].c + 12, ctx->D[i]);
  298. ctx->D[i] = key->tail.h3;
  299. PUTU32(blocks[i].c + 16, ctx->E[i]);
  300. ctx->E[i] = key->tail.h4;
  301. blocks[i].c[20] = 0x80;
  302. PUTU32(blocks[i].c + 60, (64 + 20) * 8);
  303. # endif
  304. edges[i].ptr = blocks[i].c;
  305. edges[i].blocks = 1;
  306. }
  307. /* finalize MACs */
  308. sha1_multi_block(ctx, edges, n4x);
  309. for (i = 0; i < x4; i++) {
  310. unsigned int len = (i == (x4 - 1) ? last : frag), pad, j;
  311. unsigned char *out0 = out;
  312. memcpy(ciph_d[i].out, ciph_d[i].inp, len - processed);
  313. ciph_d[i].inp = ciph_d[i].out;
  314. out += 5 + 16 + len;
  315. /* write MAC */
  316. PUTU32(out + 0, ctx->A[i]);
  317. PUTU32(out + 4, ctx->B[i]);
  318. PUTU32(out + 8, ctx->C[i]);
  319. PUTU32(out + 12, ctx->D[i]);
  320. PUTU32(out + 16, ctx->E[i]);
  321. out += 20;
  322. len += 20;
  323. /* pad */
  324. pad = 15 - len % 16;
  325. for (j = 0; j <= pad; j++)
  326. *(out++) = pad;
  327. len += pad + 1;
  328. ciph_d[i].blocks = (len - processed) / 16;
  329. len += 16; /* account for explicit iv */
  330. /* arrange header */
  331. out0[0] = ((u8 *)key->md.data)[8];
  332. out0[1] = ((u8 *)key->md.data)[9];
  333. out0[2] = ((u8 *)key->md.data)[10];
  334. out0[3] = (u8)(len >> 8);
  335. out0[4] = (u8)(len);
  336. ret += len + 5;
  337. inp += frag;
  338. }
  339. aesni_multi_cbc_encrypt(ciph_d, &key->ks, n4x);
  340. OPENSSL_cleanse(blocks, sizeof(blocks));
  341. OPENSSL_cleanse(ctx, sizeof(*ctx));
  342. return ret;
  343. }
  344. # endif
  345. static int aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  346. const unsigned char *in, size_t len)
  347. {
  348. EVP_AES_HMAC_SHA1 *key = data(ctx);
  349. unsigned int l;
  350. size_t plen = key->payload_length, iv = 0, /* explicit IV in TLS 1.1 and
  351. * later */
  352. sha_off = 0;
  353. # if defined(STITCHED_CALL)
  354. size_t aes_off = 0, blocks;
  355. sha_off = SHA_CBLOCK - key->md.num;
  356. # endif
  357. key->payload_length = NO_PAYLOAD_LENGTH;
  358. if (len % AES_BLOCK_SIZE)
  359. return 0;
  360. if (EVP_CIPHER_CTX_encrypting(ctx)) {
  361. if (plen == NO_PAYLOAD_LENGTH)
  362. plen = len;
  363. else if (len !=
  364. ((plen + SHA_DIGEST_LENGTH +
  365. AES_BLOCK_SIZE) & -AES_BLOCK_SIZE))
  366. return 0;
  367. else if (key->aux.tls_ver >= TLS1_1_VERSION)
  368. iv = AES_BLOCK_SIZE;
  369. # if defined(STITCHED_CALL)
  370. if (plen > (sha_off + iv)
  371. && (blocks = (plen - (sha_off + iv)) / SHA_CBLOCK)) {
  372. SHA1_Update(&key->md, in + iv, sha_off);
  373. aesni_cbc_sha1_enc(in, out, blocks, &key->ks,
  374. EVP_CIPHER_CTX_iv_noconst(ctx),
  375. &key->md, in + iv + sha_off);
  376. blocks *= SHA_CBLOCK;
  377. aes_off += blocks;
  378. sha_off += blocks;
  379. key->md.Nh += blocks >> 29;
  380. key->md.Nl += blocks <<= 3;
  381. if (key->md.Nl < (unsigned int)blocks)
  382. key->md.Nh++;
  383. } else {
  384. sha_off = 0;
  385. }
  386. # endif
  387. sha_off += iv;
  388. SHA1_Update(&key->md, in + sha_off, plen - sha_off);
  389. if (plen != len) { /* "TLS" mode of operation */
  390. if (in != out)
  391. memcpy(out + aes_off, in + aes_off, plen - aes_off);
  392. /* calculate HMAC and append it to payload */
  393. SHA1_Final(out + plen, &key->md);
  394. key->md = key->tail;
  395. SHA1_Update(&key->md, out + plen, SHA_DIGEST_LENGTH);
  396. SHA1_Final(out + plen, &key->md);
  397. /* pad the payload|hmac */
  398. plen += SHA_DIGEST_LENGTH;
  399. for (l = len - plen - 1; plen < len; plen++)
  400. out[plen] = l;
  401. /* encrypt HMAC|padding at once */
  402. aesni_cbc_encrypt(out + aes_off, out + aes_off, len - aes_off,
  403. &key->ks, EVP_CIPHER_CTX_iv_noconst(ctx), 1);
  404. } else {
  405. aesni_cbc_encrypt(in + aes_off, out + aes_off, len - aes_off,
  406. &key->ks, EVP_CIPHER_CTX_iv_noconst(ctx), 1);
  407. }
  408. } else {
  409. union {
  410. unsigned int u[SHA_DIGEST_LENGTH / sizeof(unsigned int)];
  411. unsigned char c[32 + SHA_DIGEST_LENGTH];
  412. } mac, *pmac;
  413. /* arrange cache line alignment */
  414. pmac = (void *)(((size_t)mac.c + 31) & ((size_t)0 - 32));
  415. if (plen != NO_PAYLOAD_LENGTH) { /* "TLS" mode of operation */
  416. size_t inp_len, mask, j, i;
  417. unsigned int res, maxpad, pad, bitlen;
  418. int ret = 1;
  419. union {
  420. unsigned int u[SHA_LBLOCK];
  421. unsigned char c[SHA_CBLOCK];
  422. } *data = (void *)key->md.data;
  423. # if defined(STITCHED_DECRYPT_CALL)
  424. unsigned char tail_iv[AES_BLOCK_SIZE];
  425. int stitch = 0;
  426. # endif
  427. if ((key->aux.tls_aad[plen - 4] << 8 | key->aux.tls_aad[plen - 3])
  428. >= TLS1_1_VERSION) {
  429. if (len < (AES_BLOCK_SIZE + SHA_DIGEST_LENGTH + 1))
  430. return 0;
  431. /* omit explicit iv */
  432. memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), in, AES_BLOCK_SIZE);
  433. in += AES_BLOCK_SIZE;
  434. out += AES_BLOCK_SIZE;
  435. len -= AES_BLOCK_SIZE;
  436. } else if (len < (SHA_DIGEST_LENGTH + 1))
  437. return 0;
  438. # if defined(STITCHED_DECRYPT_CALL)
  439. if (len >= 1024 && ctx->key_len == 32) {
  440. /* decrypt last block */
  441. memcpy(tail_iv, in + len - 2 * AES_BLOCK_SIZE,
  442. AES_BLOCK_SIZE);
  443. aesni_cbc_encrypt(in + len - AES_BLOCK_SIZE,
  444. out + len - AES_BLOCK_SIZE, AES_BLOCK_SIZE,
  445. &key->ks, tail_iv, 0);
  446. stitch = 1;
  447. } else
  448. # endif
  449. /* decrypt HMAC|padding at once */
  450. aesni_cbc_encrypt(in, out, len, &key->ks,
  451. EVP_CIPHER_CTX_iv_noconst(ctx), 0);
  452. /* figure out payload length */
  453. pad = out[len - 1];
  454. maxpad = len - (SHA_DIGEST_LENGTH + 1);
  455. maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8);
  456. maxpad &= 255;
  457. mask = constant_time_ge(maxpad, pad);
  458. ret &= mask;
  459. /*
  460. * If pad is invalid then we will fail the above test but we must
  461. * continue anyway because we are in constant time code. However,
  462. * we'll use the maxpad value instead of the supplied pad to make
  463. * sure we perform well defined pointer arithmetic.
  464. */
  465. pad = constant_time_select(mask, pad, maxpad);
  466. inp_len = len - (SHA_DIGEST_LENGTH + pad + 1);
  467. key->aux.tls_aad[plen - 2] = inp_len >> 8;
  468. key->aux.tls_aad[plen - 1] = inp_len;
  469. /* calculate HMAC */
  470. key->md = key->head;
  471. SHA1_Update(&key->md, key->aux.tls_aad, plen);
  472. # if defined(STITCHED_DECRYPT_CALL)
  473. if (stitch) {
  474. blocks = (len - (256 + 32 + SHA_CBLOCK)) / SHA_CBLOCK;
  475. aes_off = len - AES_BLOCK_SIZE - blocks * SHA_CBLOCK;
  476. sha_off = SHA_CBLOCK - plen;
  477. aesni_cbc_encrypt(in, out, aes_off, &key->ks, ctx->iv, 0);
  478. SHA1_Update(&key->md, out, sha_off);
  479. aesni256_cbc_sha1_dec(in + aes_off,
  480. out + aes_off, blocks, &key->ks,
  481. ctx->iv, &key->md, out + sha_off);
  482. sha_off += blocks *= SHA_CBLOCK;
  483. out += sha_off;
  484. len -= sha_off;
  485. inp_len -= sha_off;
  486. key->md.Nl += (blocks << 3); /* at most 18 bits */
  487. memcpy(ctx->iv, tail_iv, AES_BLOCK_SIZE);
  488. }
  489. # endif
  490. # if 1 /* see original reference version in #else */
  491. len -= SHA_DIGEST_LENGTH; /* amend mac */
  492. if (len >= (256 + SHA_CBLOCK)) {
  493. j = (len - (256 + SHA_CBLOCK)) & (0 - SHA_CBLOCK);
  494. j += SHA_CBLOCK - key->md.num;
  495. SHA1_Update(&key->md, out, j);
  496. out += j;
  497. len -= j;
  498. inp_len -= j;
  499. }
  500. /* but pretend as if we hashed padded payload */
  501. bitlen = key->md.Nl + (inp_len << 3); /* at most 18 bits */
  502. # ifdef BSWAP4
  503. bitlen = BSWAP4(bitlen);
  504. # else
  505. mac.c[0] = 0;
  506. mac.c[1] = (unsigned char)(bitlen >> 16);
  507. mac.c[2] = (unsigned char)(bitlen >> 8);
  508. mac.c[3] = (unsigned char)bitlen;
  509. bitlen = mac.u[0];
  510. # endif
  511. pmac->u[0] = 0;
  512. pmac->u[1] = 0;
  513. pmac->u[2] = 0;
  514. pmac->u[3] = 0;
  515. pmac->u[4] = 0;
  516. for (res = key->md.num, j = 0; j < len; j++) {
  517. size_t c = out[j];
  518. mask = (j - inp_len) >> (sizeof(j) * 8 - 8);
  519. c &= mask;
  520. c |= 0x80 & ~mask & ~((inp_len - j) >> (sizeof(j) * 8 - 8));
  521. data->c[res++] = (unsigned char)c;
  522. if (res != SHA_CBLOCK)
  523. continue;
  524. /* j is not incremented yet */
  525. mask = 0 - ((inp_len + 7 - j) >> (sizeof(j) * 8 - 1));
  526. data->u[SHA_LBLOCK - 1] |= bitlen & mask;
  527. sha1_block_data_order(&key->md, data, 1);
  528. mask &= 0 - ((j - inp_len - 72) >> (sizeof(j) * 8 - 1));
  529. pmac->u[0] |= key->md.h0 & mask;
  530. pmac->u[1] |= key->md.h1 & mask;
  531. pmac->u[2] |= key->md.h2 & mask;
  532. pmac->u[3] |= key->md.h3 & mask;
  533. pmac->u[4] |= key->md.h4 & mask;
  534. res = 0;
  535. }
  536. for (i = res; i < SHA_CBLOCK; i++, j++)
  537. data->c[i] = 0;
  538. if (res > SHA_CBLOCK - 8) {
  539. mask = 0 - ((inp_len + 8 - j) >> (sizeof(j) * 8 - 1));
  540. data->u[SHA_LBLOCK - 1] |= bitlen & mask;
  541. sha1_block_data_order(&key->md, data, 1);
  542. mask &= 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
  543. pmac->u[0] |= key->md.h0 & mask;
  544. pmac->u[1] |= key->md.h1 & mask;
  545. pmac->u[2] |= key->md.h2 & mask;
  546. pmac->u[3] |= key->md.h3 & mask;
  547. pmac->u[4] |= key->md.h4 & mask;
  548. memset(data, 0, SHA_CBLOCK);
  549. j += 64;
  550. }
  551. data->u[SHA_LBLOCK - 1] = bitlen;
  552. sha1_block_data_order(&key->md, data, 1);
  553. mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
  554. pmac->u[0] |= key->md.h0 & mask;
  555. pmac->u[1] |= key->md.h1 & mask;
  556. pmac->u[2] |= key->md.h2 & mask;
  557. pmac->u[3] |= key->md.h3 & mask;
  558. pmac->u[4] |= key->md.h4 & mask;
  559. # ifdef BSWAP4
  560. pmac->u[0] = BSWAP4(pmac->u[0]);
  561. pmac->u[1] = BSWAP4(pmac->u[1]);
  562. pmac->u[2] = BSWAP4(pmac->u[2]);
  563. pmac->u[3] = BSWAP4(pmac->u[3]);
  564. pmac->u[4] = BSWAP4(pmac->u[4]);
  565. # else
  566. for (i = 0; i < 5; i++) {
  567. res = pmac->u[i];
  568. pmac->c[4 * i + 0] = (unsigned char)(res >> 24);
  569. pmac->c[4 * i + 1] = (unsigned char)(res >> 16);
  570. pmac->c[4 * i + 2] = (unsigned char)(res >> 8);
  571. pmac->c[4 * i + 3] = (unsigned char)res;
  572. }
  573. # endif
  574. len += SHA_DIGEST_LENGTH;
  575. # else /* pre-lucky-13 reference version of above */
  576. SHA1_Update(&key->md, out, inp_len);
  577. res = key->md.num;
  578. SHA1_Final(pmac->c, &key->md);
  579. {
  580. unsigned int inp_blocks, pad_blocks;
  581. /* but pretend as if we hashed padded payload */
  582. inp_blocks =
  583. 1 + ((SHA_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
  584. res += (unsigned int)(len - inp_len);
  585. pad_blocks = res / SHA_CBLOCK;
  586. res %= SHA_CBLOCK;
  587. pad_blocks +=
  588. 1 + ((SHA_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
  589. for (; inp_blocks < pad_blocks; inp_blocks++)
  590. sha1_block_data_order(&key->md, data, 1);
  591. }
  592. # endif
  593. key->md = key->tail;
  594. SHA1_Update(&key->md, pmac->c, SHA_DIGEST_LENGTH);
  595. SHA1_Final(pmac->c, &key->md);
  596. /* verify HMAC */
  597. out += inp_len;
  598. len -= inp_len;
  599. # if 1 /* see original reference version in #else */
  600. {
  601. unsigned char *p = out + len - 1 - maxpad - SHA_DIGEST_LENGTH;
  602. size_t off = out - p;
  603. unsigned int c, cmask;
  604. maxpad += SHA_DIGEST_LENGTH;
  605. for (res = 0, i = 0, j = 0; j < maxpad; j++) {
  606. c = p[j];
  607. cmask =
  608. ((int)(j - off - SHA_DIGEST_LENGTH)) >> (sizeof(int) *
  609. 8 - 1);
  610. res |= (c ^ pad) & ~cmask; /* ... and padding */
  611. cmask &= ((int)(off - 1 - j)) >> (sizeof(int) * 8 - 1);
  612. res |= (c ^ pmac->c[i]) & cmask;
  613. i += 1 & cmask;
  614. }
  615. maxpad -= SHA_DIGEST_LENGTH;
  616. res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
  617. ret &= (int)~res;
  618. }
  619. # else /* pre-lucky-13 reference version of above */
  620. for (res = 0, i = 0; i < SHA_DIGEST_LENGTH; i++)
  621. res |= out[i] ^ pmac->c[i];
  622. res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
  623. ret &= (int)~res;
  624. /* verify padding */
  625. pad = (pad & ~res) | (maxpad & res);
  626. out = out + len - 1 - pad;
  627. for (res = 0, i = 0; i < pad; i++)
  628. res |= out[i] ^ pad;
  629. res = (0 - res) >> (sizeof(res) * 8 - 1);
  630. ret &= (int)~res;
  631. # endif
  632. return ret;
  633. } else {
  634. # if defined(STITCHED_DECRYPT_CALL)
  635. if (len >= 1024 && ctx->key_len == 32) {
  636. if (sha_off %= SHA_CBLOCK)
  637. blocks = (len - 3 * SHA_CBLOCK) / SHA_CBLOCK;
  638. else
  639. blocks = (len - 2 * SHA_CBLOCK) / SHA_CBLOCK;
  640. aes_off = len - blocks * SHA_CBLOCK;
  641. aesni_cbc_encrypt(in, out, aes_off, &key->ks, ctx->iv, 0);
  642. SHA1_Update(&key->md, out, sha_off);
  643. aesni256_cbc_sha1_dec(in + aes_off,
  644. out + aes_off, blocks, &key->ks,
  645. ctx->iv, &key->md, out + sha_off);
  646. sha_off += blocks *= SHA_CBLOCK;
  647. out += sha_off;
  648. len -= sha_off;
  649. key->md.Nh += blocks >> 29;
  650. key->md.Nl += blocks <<= 3;
  651. if (key->md.Nl < (unsigned int)blocks)
  652. key->md.Nh++;
  653. } else
  654. # endif
  655. /* decrypt HMAC|padding at once */
  656. aesni_cbc_encrypt(in, out, len, &key->ks,
  657. EVP_CIPHER_CTX_iv_noconst(ctx), 0);
  658. SHA1_Update(&key->md, out, len);
  659. }
  660. }
  661. return 1;
  662. }
  663. static int aesni_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
  664. void *ptr)
  665. {
  666. EVP_AES_HMAC_SHA1 *key = data(ctx);
  667. switch (type) {
  668. case EVP_CTRL_AEAD_SET_MAC_KEY:
  669. {
  670. unsigned int i;
  671. unsigned char hmac_key[64];
  672. memset(hmac_key, 0, sizeof(hmac_key));
  673. if (arg > (int)sizeof(hmac_key)) {
  674. SHA1_Init(&key->head);
  675. SHA1_Update(&key->head, ptr, arg);
  676. SHA1_Final(hmac_key, &key->head);
  677. } else {
  678. memcpy(hmac_key, ptr, arg);
  679. }
  680. for (i = 0; i < sizeof(hmac_key); i++)
  681. hmac_key[i] ^= 0x36; /* ipad */
  682. SHA1_Init(&key->head);
  683. SHA1_Update(&key->head, hmac_key, sizeof(hmac_key));
  684. for (i = 0; i < sizeof(hmac_key); i++)
  685. hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
  686. SHA1_Init(&key->tail);
  687. SHA1_Update(&key->tail, hmac_key, sizeof(hmac_key));
  688. OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
  689. return 1;
  690. }
  691. case EVP_CTRL_AEAD_TLS1_AAD:
  692. {
  693. unsigned char *p = ptr;
  694. unsigned int len;
  695. if (arg != EVP_AEAD_TLS1_AAD_LEN)
  696. return -1;
  697. len = p[arg - 2] << 8 | p[arg - 1];
  698. if (EVP_CIPHER_CTX_encrypting(ctx)) {
  699. key->payload_length = len;
  700. if ((key->aux.tls_ver =
  701. p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
  702. if (len < AES_BLOCK_SIZE)
  703. return 0;
  704. len -= AES_BLOCK_SIZE;
  705. p[arg - 2] = len >> 8;
  706. p[arg - 1] = len;
  707. }
  708. key->md = key->head;
  709. SHA1_Update(&key->md, p, arg);
  710. return (int)(((len + SHA_DIGEST_LENGTH +
  711. AES_BLOCK_SIZE) & -AES_BLOCK_SIZE)
  712. - len);
  713. } else {
  714. memcpy(key->aux.tls_aad, ptr, arg);
  715. key->payload_length = arg;
  716. return SHA_DIGEST_LENGTH;
  717. }
  718. }
  719. # if !defined(OPENSSL_NO_MULTIBLOCK)
  720. case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
  721. return (int)(5 + 16 + ((arg + 20 + 16) & -16));
  722. case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD:
  723. {
  724. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
  725. (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
  726. unsigned int n4x = 1, x4;
  727. unsigned int frag, last, packlen, inp_len;
  728. if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
  729. return -1;
  730. inp_len = param->inp[11] << 8 | param->inp[12];
  731. if (EVP_CIPHER_CTX_encrypting(ctx)) {
  732. if ((param->inp[9] << 8 | param->inp[10]) < TLS1_1_VERSION)
  733. return -1;
  734. if (inp_len) {
  735. if (inp_len < 4096)
  736. return 0; /* too short */
  737. if (inp_len >= 8192 && OPENSSL_ia32cap_P[2] & (1 << 5))
  738. n4x = 2; /* AVX2 */
  739. } else if ((n4x = param->interleave / 4) && n4x <= 2)
  740. inp_len = param->len;
  741. else
  742. return -1;
  743. key->md = key->head;
  744. SHA1_Update(&key->md, param->inp, 13);
  745. x4 = 4 * n4x;
  746. n4x += 1;
  747. frag = inp_len >> n4x;
  748. last = inp_len + frag - (frag << n4x);
  749. if (last > frag && ((last + 13 + 9) % 64 < (x4 - 1))) {
  750. frag++;
  751. last -= x4 - 1;
  752. }
  753. packlen = 5 + 16 + ((frag + 20 + 16) & -16);
  754. packlen = (packlen << n4x) - packlen;
  755. packlen += 5 + 16 + ((last + 20 + 16) & -16);
  756. param->interleave = x4;
  757. return (int)packlen;
  758. } else
  759. return -1; /* not yet */
  760. }
  761. case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT:
  762. {
  763. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
  764. (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
  765. return (int)tls1_1_multi_block_encrypt(key, param->out,
  766. param->inp, param->len,
  767. param->interleave / 4);
  768. }
  769. case EVP_CTRL_TLS1_1_MULTIBLOCK_DECRYPT:
  770. # endif
  771. default:
  772. return -1;
  773. }
  774. }
  775. static EVP_CIPHER aesni_128_cbc_hmac_sha1_cipher = {
  776. # ifdef NID_aes_128_cbc_hmac_sha1
  777. NID_aes_128_cbc_hmac_sha1,
  778. # else
  779. NID_undef,
  780. # endif
  781. AES_BLOCK_SIZE, 16, AES_BLOCK_SIZE,
  782. EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
  783. EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
  784. aesni_cbc_hmac_sha1_init_key,
  785. aesni_cbc_hmac_sha1_cipher,
  786. NULL,
  787. sizeof(EVP_AES_HMAC_SHA1),
  788. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
  789. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
  790. aesni_cbc_hmac_sha1_ctrl,
  791. NULL
  792. };
  793. static EVP_CIPHER aesni_256_cbc_hmac_sha1_cipher = {
  794. # ifdef NID_aes_256_cbc_hmac_sha1
  795. NID_aes_256_cbc_hmac_sha1,
  796. # else
  797. NID_undef,
  798. # endif
  799. AES_BLOCK_SIZE, 32, AES_BLOCK_SIZE,
  800. EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
  801. EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
  802. aesni_cbc_hmac_sha1_init_key,
  803. aesni_cbc_hmac_sha1_cipher,
  804. NULL,
  805. sizeof(EVP_AES_HMAC_SHA1),
  806. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
  807. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
  808. aesni_cbc_hmac_sha1_ctrl,
  809. NULL
  810. };
  811. const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void)
  812. {
  813. return (OPENSSL_ia32cap_P[1] & AESNI_CAPABLE ?
  814. &aesni_128_cbc_hmac_sha1_cipher : NULL);
  815. }
  816. const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void)
  817. {
  818. return (OPENSSL_ia32cap_P[1] & AESNI_CAPABLE ?
  819. &aesni_256_cbc_hmac_sha1_cipher : NULL);
  820. }
  821. #else
  822. const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void)
  823. {
  824. return NULL;
  825. }
  826. const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void)
  827. {
  828. return NULL;
  829. }
  830. #endif