e_aes_cbc_hmac_sha1.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. /*
  2. * Copyright 2011-2021 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. #include "evp_local.h"
  28. typedef struct {
  29. AES_KEY ks;
  30. SHA_CTX head, tail, md;
  31. size_t payload_length; /* AAD length in decrypt case */
  32. union {
  33. unsigned int tls_ver;
  34. unsigned char tls_aad[16]; /* 13 used */
  35. } aux;
  36. } EVP_AES_HMAC_SHA1;
  37. #define NO_PAYLOAD_LENGTH ((size_t)-1)
  38. #if defined(AES_ASM) && ( \
  39. defined(__x86_64) || defined(__x86_64__) || \
  40. defined(_M_AMD64) || defined(_M_X64) )
  41. # define AESNI_CAPABLE (1<<(57-32))
  42. int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
  43. AES_KEY *key);
  44. int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
  45. AES_KEY *key);
  46. void aesni_cbc_encrypt(const unsigned char *in,
  47. unsigned char *out,
  48. size_t length,
  49. const AES_KEY *key, unsigned char *ivec, int enc);
  50. void aesni_cbc_sha1_enc(const void *inp, void *out, size_t blocks,
  51. const AES_KEY *key, unsigned char iv[16],
  52. SHA_CTX *ctx, const void *in0);
  53. void aesni256_cbc_sha1_dec(const void *inp, void *out, size_t blocks,
  54. const AES_KEY *key, unsigned char iv[16],
  55. SHA_CTX *ctx, const void *in0);
  56. # define data(ctx) ((EVP_AES_HMAC_SHA1 *)EVP_CIPHER_CTX_get_cipher_data(ctx))
  57. static int aesni_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx,
  58. const unsigned char *inkey,
  59. const unsigned char *iv, int enc)
  60. {
  61. EVP_AES_HMAC_SHA1 *key = data(ctx);
  62. int ret;
  63. if (enc)
  64. ret = aesni_set_encrypt_key(inkey,
  65. EVP_CIPHER_CTX_key_length(ctx) * 8,
  66. &key->ks);
  67. else
  68. ret = aesni_set_decrypt_key(inkey,
  69. EVP_CIPHER_CTX_key_length(ctx) * 8,
  70. &key->ks);
  71. SHA1_Init(&key->head); /* handy when benchmarking */
  72. key->tail = key->head;
  73. key->md = key->head;
  74. key->payload_length = NO_PAYLOAD_LENGTH;
  75. return ret < 0 ? 0 : 1;
  76. }
  77. # define STITCHED_CALL
  78. # undef STITCHED_DECRYPT_CALL
  79. # if !defined(STITCHED_CALL)
  80. # define aes_off 0
  81. # endif
  82. void sha1_block_data_order(void *c, const void *p, size_t len);
  83. static void sha1_update(SHA_CTX *c, const void *data, size_t len)
  84. {
  85. const unsigned char *ptr = data;
  86. size_t res;
  87. if ((res = c->num)) {
  88. res = SHA_CBLOCK - res;
  89. if (len < res)
  90. res = len;
  91. SHA1_Update(c, ptr, res);
  92. ptr += res;
  93. len -= res;
  94. }
  95. res = len % SHA_CBLOCK;
  96. len -= res;
  97. if (len) {
  98. sha1_block_data_order(c, ptr, len / SHA_CBLOCK);
  99. ptr += len;
  100. c->Nh += len >> 29;
  101. c->Nl += len <<= 3;
  102. if (c->Nl < (unsigned int)len)
  103. c->Nh++;
  104. }
  105. if (res)
  106. SHA1_Update(c, ptr, res);
  107. }
  108. # ifdef SHA1_Update
  109. # undef SHA1_Update
  110. # endif
  111. # define SHA1_Update sha1_update
  112. # if !defined(OPENSSL_NO_MULTIBLOCK)
  113. typedef struct {
  114. unsigned int A[8], B[8], C[8], D[8], E[8];
  115. } SHA1_MB_CTX;
  116. typedef struct {
  117. const unsigned char *ptr;
  118. int blocks;
  119. } HASH_DESC;
  120. void sha1_multi_block(SHA1_MB_CTX *, const HASH_DESC *, int);
  121. typedef struct {
  122. const unsigned char *inp;
  123. unsigned char *out;
  124. int blocks;
  125. u64 iv[2];
  126. } CIPH_DESC;
  127. void aesni_multi_cbc_encrypt(CIPH_DESC *, void *, int);
  128. static size_t tls1_1_multi_block_encrypt(EVP_AES_HMAC_SHA1 *key,
  129. unsigned char *out,
  130. const unsigned char *inp,
  131. size_t inp_len, int n4x)
  132. { /* n4x is 1 or 2 */
  133. HASH_DESC hash_d[8], edges[8];
  134. CIPH_DESC ciph_d[8];
  135. unsigned char storage[sizeof(SHA1_MB_CTX) + 32];
  136. union {
  137. u64 q[16];
  138. u32 d[32];
  139. u8 c[128];
  140. } blocks[8];
  141. SHA1_MB_CTX *ctx;
  142. unsigned int frag, last, packlen, i, x4 = 4 * n4x, minblocks, processed =
  143. 0;
  144. size_t ret = 0;
  145. u8 *IVs;
  146. # if defined(BSWAP8)
  147. u64 seqnum;
  148. # endif
  149. /* ask for IVs in bulk */
  150. if (RAND_bytes((IVs = blocks[0].c), 16 * x4) <= 0)
  151. return 0;
  152. ctx = (SHA1_MB_CTX *) (storage + 32 - ((size_t)storage % 32)); /* align */
  153. frag = (unsigned int)inp_len >> (1 + n4x);
  154. last = (unsigned int)inp_len + frag - (frag << (1 + n4x));
  155. if (last > frag && ((last + 13 + 9) % 64) < (x4 - 1)) {
  156. frag++;
  157. last -= x4 - 1;
  158. }
  159. packlen = 5 + 16 + ((frag + 20 + 16) & -16);
  160. /* populate descriptors with pointers and IVs */
  161. hash_d[0].ptr = inp;
  162. ciph_d[0].inp = inp;
  163. /* 5+16 is place for header and explicit IV */
  164. ciph_d[0].out = out + 5 + 16;
  165. memcpy(ciph_d[0].out - 16, IVs, 16);
  166. memcpy(ciph_d[0].iv, IVs, 16);
  167. IVs += 16;
  168. for (i = 1; i < x4; i++) {
  169. ciph_d[i].inp = hash_d[i].ptr = hash_d[i - 1].ptr + frag;
  170. ciph_d[i].out = ciph_d[i - 1].out + packlen;
  171. memcpy(ciph_d[i].out - 16, IVs, 16);
  172. memcpy(ciph_d[i].iv, IVs, 16);
  173. IVs += 16;
  174. }
  175. # if defined(BSWAP8)
  176. memcpy(blocks[0].c, key->md.data, 8);
  177. seqnum = BSWAP8(blocks[0].q[0]);
  178. # endif
  179. for (i = 0; i < x4; i++) {
  180. unsigned int len = (i == (x4 - 1) ? last : frag);
  181. # if !defined(BSWAP8)
  182. unsigned int carry, j;
  183. # endif
  184. ctx->A[i] = key->md.h0;
  185. ctx->B[i] = key->md.h1;
  186. ctx->C[i] = key->md.h2;
  187. ctx->D[i] = key->md.h3;
  188. ctx->E[i] = key->md.h4;
  189. /* fix seqnum */
  190. # if defined(BSWAP8)
  191. blocks[i].q[0] = BSWAP8(seqnum + i);
  192. # else
  193. for (carry = i, j = 8; j--;) {
  194. blocks[i].c[j] = ((u8 *)key->md.data)[j] + carry;
  195. carry = (blocks[i].c[j] - carry) >> (sizeof(carry) * 8 - 1);
  196. }
  197. # endif
  198. blocks[i].c[8] = ((u8 *)key->md.data)[8];
  199. blocks[i].c[9] = ((u8 *)key->md.data)[9];
  200. blocks[i].c[10] = ((u8 *)key->md.data)[10];
  201. /* fix length */
  202. blocks[i].c[11] = (u8)(len >> 8);
  203. blocks[i].c[12] = (u8)(len);
  204. memcpy(blocks[i].c + 13, hash_d[i].ptr, 64 - 13);
  205. hash_d[i].ptr += 64 - 13;
  206. hash_d[i].blocks = (len - (64 - 13)) / 64;
  207. edges[i].ptr = blocks[i].c;
  208. edges[i].blocks = 1;
  209. }
  210. /* hash 13-byte headers and first 64-13 bytes of inputs */
  211. sha1_multi_block(ctx, edges, n4x);
  212. /* hash bulk inputs */
  213. # define MAXCHUNKSIZE 2048
  214. # if MAXCHUNKSIZE%64
  215. # error "MAXCHUNKSIZE is not divisible by 64"
  216. # elif MAXCHUNKSIZE
  217. /*
  218. * goal is to minimize pressure on L1 cache by moving in shorter steps,
  219. * so that hashed data is still in the cache by the time we encrypt it
  220. */
  221. minblocks = ((frag <= last ? frag : last) - (64 - 13)) / 64;
  222. if (minblocks > MAXCHUNKSIZE / 64) {
  223. for (i = 0; i < x4; i++) {
  224. edges[i].ptr = hash_d[i].ptr;
  225. edges[i].blocks = MAXCHUNKSIZE / 64;
  226. ciph_d[i].blocks = MAXCHUNKSIZE / 16;
  227. }
  228. do {
  229. sha1_multi_block(ctx, edges, n4x);
  230. aesni_multi_cbc_encrypt(ciph_d, &key->ks, n4x);
  231. for (i = 0; i < x4; i++) {
  232. edges[i].ptr = hash_d[i].ptr += MAXCHUNKSIZE;
  233. hash_d[i].blocks -= MAXCHUNKSIZE / 64;
  234. edges[i].blocks = MAXCHUNKSIZE / 64;
  235. ciph_d[i].inp += MAXCHUNKSIZE;
  236. ciph_d[i].out += MAXCHUNKSIZE;
  237. ciph_d[i].blocks = MAXCHUNKSIZE / 16;
  238. memcpy(ciph_d[i].iv, ciph_d[i].out - 16, 16);
  239. }
  240. processed += MAXCHUNKSIZE;
  241. minblocks -= MAXCHUNKSIZE / 64;
  242. } while (minblocks > MAXCHUNKSIZE / 64);
  243. }
  244. # endif
  245. # undef MAXCHUNKSIZE
  246. sha1_multi_block(ctx, hash_d, n4x);
  247. memset(blocks, 0, sizeof(blocks));
  248. for (i = 0; i < x4; i++) {
  249. unsigned int len = (i == (x4 - 1) ? last : frag),
  250. off = hash_d[i].blocks * 64;
  251. const unsigned char *ptr = hash_d[i].ptr + off;
  252. off = (len - processed) - (64 - 13) - off; /* remainder actually */
  253. memcpy(blocks[i].c, ptr, off);
  254. blocks[i].c[off] = 0x80;
  255. len += 64 + 13; /* 64 is HMAC header */
  256. len *= 8; /* convert to bits */
  257. if (off < (64 - 8)) {
  258. # ifdef BSWAP4
  259. blocks[i].d[15] = BSWAP4(len);
  260. # else
  261. PUTU32(blocks[i].c + 60, len);
  262. # endif
  263. edges[i].blocks = 1;
  264. } else {
  265. # ifdef BSWAP4
  266. blocks[i].d[31] = BSWAP4(len);
  267. # else
  268. PUTU32(blocks[i].c + 124, len);
  269. # endif
  270. edges[i].blocks = 2;
  271. }
  272. edges[i].ptr = blocks[i].c;
  273. }
  274. /* hash input tails and finalize */
  275. sha1_multi_block(ctx, edges, n4x);
  276. memset(blocks, 0, sizeof(blocks));
  277. for (i = 0; i < x4; i++) {
  278. # ifdef BSWAP4
  279. blocks[i].d[0] = BSWAP4(ctx->A[i]);
  280. ctx->A[i] = key->tail.h0;
  281. blocks[i].d[1] = BSWAP4(ctx->B[i]);
  282. ctx->B[i] = key->tail.h1;
  283. blocks[i].d[2] = BSWAP4(ctx->C[i]);
  284. ctx->C[i] = key->tail.h2;
  285. blocks[i].d[3] = BSWAP4(ctx->D[i]);
  286. ctx->D[i] = key->tail.h3;
  287. blocks[i].d[4] = BSWAP4(ctx->E[i]);
  288. ctx->E[i] = key->tail.h4;
  289. blocks[i].c[20] = 0x80;
  290. blocks[i].d[15] = BSWAP4((64 + 20) * 8);
  291. # else
  292. PUTU32(blocks[i].c + 0, ctx->A[i]);
  293. ctx->A[i] = key->tail.h0;
  294. PUTU32(blocks[i].c + 4, ctx->B[i]);
  295. ctx->B[i] = key->tail.h1;
  296. PUTU32(blocks[i].c + 8, ctx->C[i]);
  297. ctx->C[i] = key->tail.h2;
  298. PUTU32(blocks[i].c + 12, ctx->D[i]);
  299. ctx->D[i] = key->tail.h3;
  300. PUTU32(blocks[i].c + 16, ctx->E[i]);
  301. ctx->E[i] = key->tail.h4;
  302. blocks[i].c[20] = 0x80;
  303. PUTU32(blocks[i].c + 60, (64 + 20) * 8);
  304. # endif
  305. edges[i].ptr = blocks[i].c;
  306. edges[i].blocks = 1;
  307. }
  308. /* finalize MACs */
  309. sha1_multi_block(ctx, edges, n4x);
  310. for (i = 0; i < x4; i++) {
  311. unsigned int len = (i == (x4 - 1) ? last : frag), pad, j;
  312. unsigned char *out0 = out;
  313. memcpy(ciph_d[i].out, ciph_d[i].inp, len - processed);
  314. ciph_d[i].inp = ciph_d[i].out;
  315. out += 5 + 16 + len;
  316. /* write MAC */
  317. PUTU32(out + 0, ctx->A[i]);
  318. PUTU32(out + 4, ctx->B[i]);
  319. PUTU32(out + 8, ctx->C[i]);
  320. PUTU32(out + 12, ctx->D[i]);
  321. PUTU32(out + 16, ctx->E[i]);
  322. out += 20;
  323. len += 20;
  324. /* pad */
  325. pad = 15 - len % 16;
  326. for (j = 0; j <= pad; j++)
  327. *(out++) = pad;
  328. len += pad + 1;
  329. ciph_d[i].blocks = (len - processed) / 16;
  330. len += 16; /* account for explicit iv */
  331. /* arrange header */
  332. out0[0] = ((u8 *)key->md.data)[8];
  333. out0[1] = ((u8 *)key->md.data)[9];
  334. out0[2] = ((u8 *)key->md.data)[10];
  335. out0[3] = (u8)(len >> 8);
  336. out0[4] = (u8)(len);
  337. ret += len + 5;
  338. inp += frag;
  339. }
  340. aesni_multi_cbc_encrypt(ciph_d, &key->ks, n4x);
  341. OPENSSL_cleanse(blocks, sizeof(blocks));
  342. OPENSSL_cleanse(ctx, sizeof(*ctx));
  343. return ret;
  344. }
  345. # endif
  346. static int aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  347. const unsigned char *in, size_t len)
  348. {
  349. EVP_AES_HMAC_SHA1 *key = data(ctx);
  350. unsigned int l;
  351. size_t plen = key->payload_length, iv = 0, /* explicit IV in TLS 1.1 and
  352. * later */
  353. sha_off = 0;
  354. # if defined(STITCHED_CALL)
  355. size_t aes_off = 0, blocks;
  356. sha_off = SHA_CBLOCK - key->md.num;
  357. # endif
  358. key->payload_length = NO_PAYLOAD_LENGTH;
  359. if (len % AES_BLOCK_SIZE)
  360. return 0;
  361. if (EVP_CIPHER_CTX_encrypting(ctx)) {
  362. if (plen == NO_PAYLOAD_LENGTH)
  363. plen = len;
  364. else if (len !=
  365. ((plen + SHA_DIGEST_LENGTH +
  366. AES_BLOCK_SIZE) & -AES_BLOCK_SIZE))
  367. return 0;
  368. else if (key->aux.tls_ver >= TLS1_1_VERSION)
  369. iv = AES_BLOCK_SIZE;
  370. # if defined(STITCHED_CALL)
  371. if (plen > (sha_off + iv)
  372. && (blocks = (plen - (sha_off + iv)) / SHA_CBLOCK)) {
  373. SHA1_Update(&key->md, in + iv, sha_off);
  374. aesni_cbc_sha1_enc(in, out, blocks, &key->ks, ctx->iv,
  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, ctx->iv, 1);
  404. } else {
  405. aesni_cbc_encrypt(in + aes_off, out + aes_off, len - aes_off,
  406. &key->ks, ctx->iv, 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(ctx->iv, 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. ctx->iv, 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. for (res = 0, i = 0, j = 0; j < maxpad + SHA_DIGEST_LENGTH; j++) {
  605. c = p[j];
  606. cmask =
  607. ((int)(j - off - SHA_DIGEST_LENGTH)) >> (sizeof(int) *
  608. 8 - 1);
  609. res |= (c ^ pad) & ~cmask; /* ... and padding */
  610. cmask &= ((int)(off - 1 - j)) >> (sizeof(int) * 8 - 1);
  611. res |= (c ^ pmac->c[i]) & cmask;
  612. i += 1 & cmask;
  613. }
  614. res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
  615. ret &= (int)~res;
  616. }
  617. # else /* pre-lucky-13 reference version of above */
  618. for (res = 0, i = 0; i < SHA_DIGEST_LENGTH; i++)
  619. res |= out[i] ^ pmac->c[i];
  620. res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
  621. ret &= (int)~res;
  622. /* verify padding */
  623. pad = (pad & ~res) | (maxpad & res);
  624. out = out + len - 1 - pad;
  625. for (res = 0, i = 0; i < pad; i++)
  626. res |= out[i] ^ pad;
  627. res = (0 - res) >> (sizeof(res) * 8 - 1);
  628. ret &= (int)~res;
  629. # endif
  630. return ret;
  631. } else {
  632. # if defined(STITCHED_DECRYPT_CALL)
  633. if (len >= 1024 && ctx->key_len == 32) {
  634. if (sha_off %= SHA_CBLOCK)
  635. blocks = (len - 3 * SHA_CBLOCK) / SHA_CBLOCK;
  636. else
  637. blocks = (len - 2 * SHA_CBLOCK) / SHA_CBLOCK;
  638. aes_off = len - blocks * SHA_CBLOCK;
  639. aesni_cbc_encrypt(in, out, aes_off, &key->ks, ctx->iv, 0);
  640. SHA1_Update(&key->md, out, sha_off);
  641. aesni256_cbc_sha1_dec(in + aes_off,
  642. out + aes_off, blocks, &key->ks,
  643. ctx->iv, &key->md, out + sha_off);
  644. sha_off += blocks *= SHA_CBLOCK;
  645. out += sha_off;
  646. len -= sha_off;
  647. key->md.Nh += blocks >> 29;
  648. key->md.Nl += blocks <<= 3;
  649. if (key->md.Nl < (unsigned int)blocks)
  650. key->md.Nh++;
  651. } else
  652. # endif
  653. /* decrypt HMAC|padding at once */
  654. aesni_cbc_encrypt(in, out, len, &key->ks,
  655. ctx->iv, 0);
  656. SHA1_Update(&key->md, out, len);
  657. }
  658. }
  659. return 1;
  660. }
  661. static int aesni_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
  662. void *ptr)
  663. {
  664. EVP_AES_HMAC_SHA1 *key = data(ctx);
  665. switch (type) {
  666. case EVP_CTRL_AEAD_SET_MAC_KEY:
  667. {
  668. unsigned int i;
  669. unsigned char hmac_key[64];
  670. memset(hmac_key, 0, sizeof(hmac_key));
  671. if (arg > (int)sizeof(hmac_key)) {
  672. SHA1_Init(&key->head);
  673. SHA1_Update(&key->head, ptr, arg);
  674. SHA1_Final(hmac_key, &key->head);
  675. } else {
  676. memcpy(hmac_key, ptr, arg);
  677. }
  678. for (i = 0; i < sizeof(hmac_key); i++)
  679. hmac_key[i] ^= 0x36; /* ipad */
  680. SHA1_Init(&key->head);
  681. SHA1_Update(&key->head, hmac_key, sizeof(hmac_key));
  682. for (i = 0; i < sizeof(hmac_key); i++)
  683. hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
  684. SHA1_Init(&key->tail);
  685. SHA1_Update(&key->tail, hmac_key, sizeof(hmac_key));
  686. OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
  687. return 1;
  688. }
  689. case EVP_CTRL_AEAD_TLS1_AAD:
  690. {
  691. unsigned char *p = ptr;
  692. unsigned int len;
  693. if (arg != EVP_AEAD_TLS1_AAD_LEN)
  694. return -1;
  695. len = p[arg - 2] << 8 | p[arg - 1];
  696. if (EVP_CIPHER_CTX_encrypting(ctx)) {
  697. key->payload_length = len;
  698. if ((key->aux.tls_ver =
  699. p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
  700. if (len < AES_BLOCK_SIZE)
  701. return 0;
  702. len -= AES_BLOCK_SIZE;
  703. p[arg - 2] = len >> 8;
  704. p[arg - 1] = len;
  705. }
  706. key->md = key->head;
  707. SHA1_Update(&key->md, p, arg);
  708. return (int)(((len + SHA_DIGEST_LENGTH +
  709. AES_BLOCK_SIZE) & -AES_BLOCK_SIZE)
  710. - len);
  711. } else {
  712. memcpy(key->aux.tls_aad, ptr, arg);
  713. key->payload_length = arg;
  714. return SHA_DIGEST_LENGTH;
  715. }
  716. }
  717. # if !defined(OPENSSL_NO_MULTIBLOCK)
  718. case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
  719. return (int)(5 + 16 + ((arg + 20 + 16) & -16));
  720. case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD:
  721. {
  722. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
  723. (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
  724. unsigned int n4x = 1, x4;
  725. unsigned int frag, last, packlen, inp_len;
  726. if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
  727. return -1;
  728. inp_len = param->inp[11] << 8 | param->inp[12];
  729. if (EVP_CIPHER_CTX_encrypting(ctx)) {
  730. if ((param->inp[9] << 8 | param->inp[10]) < TLS1_1_VERSION)
  731. return -1;
  732. if (inp_len) {
  733. if (inp_len < 4096)
  734. return 0; /* too short */
  735. if (inp_len >= 8192 && OPENSSL_ia32cap_P[2] & (1 << 5))
  736. n4x = 2; /* AVX2 */
  737. } else if ((n4x = param->interleave / 4) && n4x <= 2)
  738. inp_len = param->len;
  739. else
  740. return -1;
  741. key->md = key->head;
  742. SHA1_Update(&key->md, param->inp, 13);
  743. x4 = 4 * n4x;
  744. n4x += 1;
  745. frag = inp_len >> n4x;
  746. last = inp_len + frag - (frag << n4x);
  747. if (last > frag && ((last + 13 + 9) % 64 < (x4 - 1))) {
  748. frag++;
  749. last -= x4 - 1;
  750. }
  751. packlen = 5 + 16 + ((frag + 20 + 16) & -16);
  752. packlen = (packlen << n4x) - packlen;
  753. packlen += 5 + 16 + ((last + 20 + 16) & -16);
  754. param->interleave = x4;
  755. return (int)packlen;
  756. } else
  757. return -1; /* not yet */
  758. }
  759. case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT:
  760. {
  761. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
  762. (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
  763. return (int)tls1_1_multi_block_encrypt(key, param->out,
  764. param->inp, param->len,
  765. param->interleave / 4);
  766. }
  767. case EVP_CTRL_TLS1_1_MULTIBLOCK_DECRYPT:
  768. # endif
  769. default:
  770. return -1;
  771. }
  772. }
  773. static EVP_CIPHER aesni_128_cbc_hmac_sha1_cipher = {
  774. # ifdef NID_aes_128_cbc_hmac_sha1
  775. NID_aes_128_cbc_hmac_sha1,
  776. # else
  777. NID_undef,
  778. # endif
  779. AES_BLOCK_SIZE, 16, AES_BLOCK_SIZE,
  780. EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
  781. EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
  782. EVP_ORIG_GLOBAL,
  783. aesni_cbc_hmac_sha1_init_key,
  784. aesni_cbc_hmac_sha1_cipher,
  785. NULL,
  786. sizeof(EVP_AES_HMAC_SHA1),
  787. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
  788. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
  789. aesni_cbc_hmac_sha1_ctrl,
  790. NULL
  791. };
  792. static EVP_CIPHER aesni_256_cbc_hmac_sha1_cipher = {
  793. # ifdef NID_aes_256_cbc_hmac_sha1
  794. NID_aes_256_cbc_hmac_sha1,
  795. # else
  796. NID_undef,
  797. # endif
  798. AES_BLOCK_SIZE, 32, AES_BLOCK_SIZE,
  799. EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
  800. EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
  801. EVP_ORIG_GLOBAL,
  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