2
0

e_aes_cbc_hmac_sha1.c 31 KB

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