e_aes_cbc_hmac_sha1.c 33 KB

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