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