e_aes_cbc_hmac_sha256.c 32 KB

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