e_aes_cbc_hmac_sha1.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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_local.h"
  18. #include "crypto/evp.h"
  19. #include "internal/constant_time.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(AESNI_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. mask = constant_time_ge(maxpad, pad);
  452. ret &= mask;
  453. /*
  454. * If pad is invalid then we will fail the above test but we must
  455. * continue anyway because we are in constant time code. However,
  456. * we'll use the maxpad value instead of the supplied pad to make
  457. * sure we perform well defined pointer arithmetic.
  458. */
  459. pad = constant_time_select(mask, pad, maxpad);
  460. inp_len = len - (SHA_DIGEST_LENGTH + pad + 1);
  461. key->aux.tls_aad[plen - 2] = inp_len >> 8;
  462. key->aux.tls_aad[plen - 1] = inp_len;
  463. /* calculate HMAC */
  464. key->md = key->head;
  465. SHA1_Update(&key->md, key->aux.tls_aad, plen);
  466. # if defined(STITCHED_DECRYPT_CALL)
  467. if (stitch) {
  468. blocks = (len - (256 + 32 + SHA_CBLOCK)) / SHA_CBLOCK;
  469. aes_off = len - AES_BLOCK_SIZE - blocks * SHA_CBLOCK;
  470. sha_off = SHA_CBLOCK - plen;
  471. aesni_cbc_encrypt(in, out, aes_off, &key->ks, ctx->iv, 0);
  472. SHA1_Update(&key->md, out, sha_off);
  473. aesni256_cbc_sha1_dec(in + aes_off,
  474. out + aes_off, blocks, &key->ks,
  475. ctx->iv, &key->md, out + sha_off);
  476. sha_off += blocks *= SHA_CBLOCK;
  477. out += sha_off;
  478. len -= sha_off;
  479. inp_len -= sha_off;
  480. key->md.Nl += (blocks << 3); /* at most 18 bits */
  481. memcpy(ctx->iv, tail_iv, AES_BLOCK_SIZE);
  482. }
  483. # endif
  484. # if 1 /* see original reference version in #else */
  485. len -= SHA_DIGEST_LENGTH; /* amend mac */
  486. if (len >= (256 + SHA_CBLOCK)) {
  487. j = (len - (256 + SHA_CBLOCK)) & (0 - SHA_CBLOCK);
  488. j += SHA_CBLOCK - key->md.num;
  489. SHA1_Update(&key->md, out, j);
  490. out += j;
  491. len -= j;
  492. inp_len -= j;
  493. }
  494. /* but pretend as if we hashed padded payload */
  495. bitlen = key->md.Nl + (inp_len << 3); /* at most 18 bits */
  496. # ifdef BSWAP4
  497. bitlen = BSWAP4(bitlen);
  498. # else
  499. mac.c[0] = 0;
  500. mac.c[1] = (unsigned char)(bitlen >> 16);
  501. mac.c[2] = (unsigned char)(bitlen >> 8);
  502. mac.c[3] = (unsigned char)bitlen;
  503. bitlen = mac.u[0];
  504. # endif
  505. pmac->u[0] = 0;
  506. pmac->u[1] = 0;
  507. pmac->u[2] = 0;
  508. pmac->u[3] = 0;
  509. pmac->u[4] = 0;
  510. for (res = key->md.num, j = 0; j < len; j++) {
  511. size_t c = out[j];
  512. mask = (j - inp_len) >> (sizeof(j) * 8 - 8);
  513. c &= mask;
  514. c |= 0x80 & ~mask & ~((inp_len - j) >> (sizeof(j) * 8 - 8));
  515. data->c[res++] = (unsigned char)c;
  516. if (res != SHA_CBLOCK)
  517. continue;
  518. /* j is not incremented yet */
  519. mask = 0 - ((inp_len + 7 - j) >> (sizeof(j) * 8 - 1));
  520. data->u[SHA_LBLOCK - 1] |= bitlen & mask;
  521. sha1_block_data_order(&key->md, data, 1);
  522. mask &= 0 - ((j - inp_len - 72) >> (sizeof(j) * 8 - 1));
  523. pmac->u[0] |= key->md.h0 & mask;
  524. pmac->u[1] |= key->md.h1 & mask;
  525. pmac->u[2] |= key->md.h2 & mask;
  526. pmac->u[3] |= key->md.h3 & mask;
  527. pmac->u[4] |= key->md.h4 & mask;
  528. res = 0;
  529. }
  530. for (i = res; i < SHA_CBLOCK; i++, j++)
  531. data->c[i] = 0;
  532. if (res > SHA_CBLOCK - 8) {
  533. mask = 0 - ((inp_len + 8 - j) >> (sizeof(j) * 8 - 1));
  534. data->u[SHA_LBLOCK - 1] |= bitlen & mask;
  535. sha1_block_data_order(&key->md, data, 1);
  536. mask &= 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
  537. pmac->u[0] |= key->md.h0 & mask;
  538. pmac->u[1] |= key->md.h1 & mask;
  539. pmac->u[2] |= key->md.h2 & mask;
  540. pmac->u[3] |= key->md.h3 & mask;
  541. pmac->u[4] |= key->md.h4 & mask;
  542. memset(data, 0, SHA_CBLOCK);
  543. j += 64;
  544. }
  545. data->u[SHA_LBLOCK - 1] = bitlen;
  546. sha1_block_data_order(&key->md, data, 1);
  547. mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1));
  548. pmac->u[0] |= key->md.h0 & mask;
  549. pmac->u[1] |= key->md.h1 & mask;
  550. pmac->u[2] |= key->md.h2 & mask;
  551. pmac->u[3] |= key->md.h3 & mask;
  552. pmac->u[4] |= key->md.h4 & mask;
  553. # ifdef BSWAP4
  554. pmac->u[0] = BSWAP4(pmac->u[0]);
  555. pmac->u[1] = BSWAP4(pmac->u[1]);
  556. pmac->u[2] = BSWAP4(pmac->u[2]);
  557. pmac->u[3] = BSWAP4(pmac->u[3]);
  558. pmac->u[4] = BSWAP4(pmac->u[4]);
  559. # else
  560. for (i = 0; i < 5; i++) {
  561. res = pmac->u[i];
  562. pmac->c[4 * i + 0] = (unsigned char)(res >> 24);
  563. pmac->c[4 * i + 1] = (unsigned char)(res >> 16);
  564. pmac->c[4 * i + 2] = (unsigned char)(res >> 8);
  565. pmac->c[4 * i + 3] = (unsigned char)res;
  566. }
  567. # endif
  568. len += SHA_DIGEST_LENGTH;
  569. # else /* pre-lucky-13 reference version of above */
  570. SHA1_Update(&key->md, out, inp_len);
  571. res = key->md.num;
  572. SHA1_Final(pmac->c, &key->md);
  573. {
  574. unsigned int inp_blocks, pad_blocks;
  575. /* but pretend as if we hashed padded payload */
  576. inp_blocks =
  577. 1 + ((SHA_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
  578. res += (unsigned int)(len - inp_len);
  579. pad_blocks = res / SHA_CBLOCK;
  580. res %= SHA_CBLOCK;
  581. pad_blocks +=
  582. 1 + ((SHA_CBLOCK - 9 - res) >> (sizeof(res) * 8 - 1));
  583. for (; inp_blocks < pad_blocks; inp_blocks++)
  584. sha1_block_data_order(&key->md, data, 1);
  585. }
  586. # endif
  587. key->md = key->tail;
  588. SHA1_Update(&key->md, pmac->c, SHA_DIGEST_LENGTH);
  589. SHA1_Final(pmac->c, &key->md);
  590. /* verify HMAC */
  591. out += inp_len;
  592. len -= inp_len;
  593. # if 1 /* see original reference version in #else */
  594. {
  595. unsigned char *p = out + len - 1 - maxpad - SHA_DIGEST_LENGTH;
  596. size_t off = out - p;
  597. unsigned int c, cmask;
  598. maxpad += SHA_DIGEST_LENGTH;
  599. for (res = 0, i = 0, j = 0; j < maxpad; j++) {
  600. c = p[j];
  601. cmask =
  602. ((int)(j - off - SHA_DIGEST_LENGTH)) >> (sizeof(int) *
  603. 8 - 1);
  604. res |= (c ^ pad) & ~cmask; /* ... and padding */
  605. cmask &= ((int)(off - 1 - j)) >> (sizeof(int) * 8 - 1);
  606. res |= (c ^ pmac->c[i]) & cmask;
  607. i += 1 & cmask;
  608. }
  609. maxpad -= SHA_DIGEST_LENGTH;
  610. res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
  611. ret &= (int)~res;
  612. }
  613. # else /* pre-lucky-13 reference version of above */
  614. for (res = 0, i = 0; i < SHA_DIGEST_LENGTH; i++)
  615. res |= out[i] ^ pmac->c[i];
  616. res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
  617. ret &= (int)~res;
  618. /* verify padding */
  619. pad = (pad & ~res) | (maxpad & res);
  620. out = out + len - 1 - pad;
  621. for (res = 0, i = 0; i < pad; i++)
  622. res |= out[i] ^ pad;
  623. res = (0 - res) >> (sizeof(res) * 8 - 1);
  624. ret &= (int)~res;
  625. # endif
  626. return ret;
  627. } else {
  628. # if defined(STITCHED_DECRYPT_CALL)
  629. if (len >= 1024 && ctx->key_len == 32) {
  630. if (sha_off %= SHA_CBLOCK)
  631. blocks = (len - 3 * SHA_CBLOCK) / SHA_CBLOCK;
  632. else
  633. blocks = (len - 2 * SHA_CBLOCK) / SHA_CBLOCK;
  634. aes_off = len - blocks * SHA_CBLOCK;
  635. aesni_cbc_encrypt(in, out, aes_off, &key->ks, ctx->iv, 0);
  636. SHA1_Update(&key->md, out, sha_off);
  637. aesni256_cbc_sha1_dec(in + aes_off,
  638. out + aes_off, blocks, &key->ks,
  639. ctx->iv, &key->md, out + sha_off);
  640. sha_off += blocks *= SHA_CBLOCK;
  641. out += sha_off;
  642. len -= sha_off;
  643. key->md.Nh += blocks >> 29;
  644. key->md.Nl += blocks <<= 3;
  645. if (key->md.Nl < (unsigned int)blocks)
  646. key->md.Nh++;
  647. } else
  648. # endif
  649. /* decrypt HMAC|padding at once */
  650. aesni_cbc_encrypt(in, out, len, &key->ks,
  651. EVP_CIPHER_CTX_iv_noconst(ctx), 0);
  652. SHA1_Update(&key->md, out, len);
  653. }
  654. }
  655. return 1;
  656. }
  657. static int aesni_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
  658. void *ptr)
  659. {
  660. EVP_AES_HMAC_SHA1 *key = data(ctx);
  661. switch (type) {
  662. case EVP_CTRL_AEAD_SET_MAC_KEY:
  663. {
  664. unsigned int i;
  665. unsigned char hmac_key[64];
  666. memset(hmac_key, 0, sizeof(hmac_key));
  667. if (arg > (int)sizeof(hmac_key)) {
  668. SHA1_Init(&key->head);
  669. SHA1_Update(&key->head, ptr, arg);
  670. SHA1_Final(hmac_key, &key->head);
  671. } else {
  672. memcpy(hmac_key, ptr, arg);
  673. }
  674. for (i = 0; i < sizeof(hmac_key); i++)
  675. hmac_key[i] ^= 0x36; /* ipad */
  676. SHA1_Init(&key->head);
  677. SHA1_Update(&key->head, hmac_key, sizeof(hmac_key));
  678. for (i = 0; i < sizeof(hmac_key); i++)
  679. hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */
  680. SHA1_Init(&key->tail);
  681. SHA1_Update(&key->tail, hmac_key, sizeof(hmac_key));
  682. OPENSSL_cleanse(hmac_key, sizeof(hmac_key));
  683. return 1;
  684. }
  685. case EVP_CTRL_AEAD_TLS1_AAD:
  686. {
  687. unsigned char *p = ptr;
  688. unsigned int len;
  689. if (arg != EVP_AEAD_TLS1_AAD_LEN)
  690. return -1;
  691. len = p[arg - 2] << 8 | p[arg - 1];
  692. if (EVP_CIPHER_CTX_encrypting(ctx)) {
  693. key->payload_length = len;
  694. if ((key->aux.tls_ver =
  695. p[arg - 4] << 8 | p[arg - 3]) >= TLS1_1_VERSION) {
  696. if (len < AES_BLOCK_SIZE)
  697. return 0;
  698. len -= AES_BLOCK_SIZE;
  699. p[arg - 2] = len >> 8;
  700. p[arg - 1] = len;
  701. }
  702. key->md = key->head;
  703. SHA1_Update(&key->md, p, arg);
  704. return (int)(((len + SHA_DIGEST_LENGTH +
  705. AES_BLOCK_SIZE) & -AES_BLOCK_SIZE)
  706. - len);
  707. } else {
  708. memcpy(key->aux.tls_aad, ptr, arg);
  709. key->payload_length = arg;
  710. return SHA_DIGEST_LENGTH;
  711. }
  712. }
  713. # if !defined(OPENSSL_NO_MULTIBLOCK)
  714. case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
  715. return (int)(5 + 16 + ((arg + 20 + 16) & -16));
  716. case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD:
  717. {
  718. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
  719. (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
  720. unsigned int n4x = 1, x4;
  721. unsigned int frag, last, packlen, inp_len;
  722. if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
  723. return -1;
  724. inp_len = param->inp[11] << 8 | param->inp[12];
  725. if (EVP_CIPHER_CTX_encrypting(ctx)) {
  726. if ((param->inp[9] << 8 | param->inp[10]) < TLS1_1_VERSION)
  727. return -1;
  728. if (inp_len) {
  729. if (inp_len < 4096)
  730. return 0; /* too short */
  731. if (inp_len >= 8192 && OPENSSL_ia32cap_P[2] & (1 << 5))
  732. n4x = 2; /* AVX2 */
  733. } else if ((n4x = param->interleave / 4) && n4x <= 2)
  734. inp_len = param->len;
  735. else
  736. return -1;
  737. key->md = key->head;
  738. SHA1_Update(&key->md, param->inp, 13);
  739. x4 = 4 * n4x;
  740. n4x += 1;
  741. frag = inp_len >> n4x;
  742. last = inp_len + frag - (frag << n4x);
  743. if (last > frag && ((last + 13 + 9) % 64 < (x4 - 1))) {
  744. frag++;
  745. last -= x4 - 1;
  746. }
  747. packlen = 5 + 16 + ((frag + 20 + 16) & -16);
  748. packlen = (packlen << n4x) - packlen;
  749. packlen += 5 + 16 + ((last + 20 + 16) & -16);
  750. param->interleave = x4;
  751. return (int)packlen;
  752. } else
  753. return -1; /* not yet */
  754. }
  755. case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT:
  756. {
  757. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *param =
  758. (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *) ptr;
  759. return (int)tls1_1_multi_block_encrypt(key, param->out,
  760. param->inp, param->len,
  761. param->interleave / 4);
  762. }
  763. case EVP_CTRL_TLS1_1_MULTIBLOCK_DECRYPT:
  764. # endif
  765. default:
  766. return -1;
  767. }
  768. }
  769. static EVP_CIPHER aesni_128_cbc_hmac_sha1_cipher = {
  770. # ifdef NID_aes_128_cbc_hmac_sha1
  771. NID_aes_128_cbc_hmac_sha1,
  772. # else
  773. NID_undef,
  774. # endif
  775. AES_BLOCK_SIZE, 16, AES_BLOCK_SIZE,
  776. EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
  777. EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
  778. aesni_cbc_hmac_sha1_init_key,
  779. aesni_cbc_hmac_sha1_cipher,
  780. NULL,
  781. sizeof(EVP_AES_HMAC_SHA1),
  782. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
  783. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
  784. aesni_cbc_hmac_sha1_ctrl,
  785. NULL
  786. };
  787. static EVP_CIPHER aesni_256_cbc_hmac_sha1_cipher = {
  788. # ifdef NID_aes_256_cbc_hmac_sha1
  789. NID_aes_256_cbc_hmac_sha1,
  790. # else
  791. NID_undef,
  792. # endif
  793. AES_BLOCK_SIZE, 32, AES_BLOCK_SIZE,
  794. EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 |
  795. EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK,
  796. aesni_cbc_hmac_sha1_init_key,
  797. aesni_cbc_hmac_sha1_cipher,
  798. NULL,
  799. sizeof(EVP_AES_HMAC_SHA1),
  800. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_set_asn1_iv,
  801. EVP_CIPH_FLAG_DEFAULT_ASN1 ? NULL : EVP_CIPHER_get_asn1_iv,
  802. aesni_cbc_hmac_sha1_ctrl,
  803. NULL
  804. };
  805. const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void)
  806. {
  807. return (OPENSSL_ia32cap_P[1] & AESNI_CAPABLE ?
  808. &aesni_128_cbc_hmac_sha1_cipher : NULL);
  809. }
  810. const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void)
  811. {
  812. return (OPENSSL_ia32cap_P[1] & AESNI_CAPABLE ?
  813. &aesni_256_cbc_hmac_sha1_cipher : NULL);
  814. }
  815. #else
  816. const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void)
  817. {
  818. return NULL;
  819. }
  820. const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void)
  821. {
  822. return NULL;
  823. }
  824. #endif