e_aes_cbc_hmac_sha1.c 31 KB

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