1 /* $OpenBSD: e_aes_cbc_hmac_sha1.c,v 1.11 2016/05/04 14:53:29 tedu Exp $ */ 2 /* ==================================================================== 3 * Copyright (c) 2011-2013 The OpenSSL Project. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the 15 * distribution. 16 * 17 * 3. All advertising materials mentioning features or use of this 18 * software must display the following acknowledgment: 19 * "This product includes software developed by the OpenSSL Project 20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 21 * 22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 23 * endorse or promote products derived from this software without 24 * prior written permission. For written permission, please contact 25 * licensing@OpenSSL.org. 26 * 27 * 5. Products derived from this software may not be called "OpenSSL" 28 * nor may "OpenSSL" appear in their names without prior written 29 * permission of the OpenSSL Project. 30 * 31 * 6. Redistributions of any form whatsoever must retain the following 32 * acknowledgment: 33 * "This product includes software developed by the OpenSSL Project 34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 35 * 36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 47 * OF THE POSSIBILITY OF SUCH DAMAGE. 48 * ==================================================================== 49 */ 50 51 #include <stdio.h> 52 #include <string.h> 53 54 #include <openssl/opensslconf.h> 55 56 #if !defined(OPENSSL_NO_AES) && !defined(OPENSSL_NO_SHA1) 57 58 #include <openssl/evp.h> 59 #include <openssl/objects.h> 60 #include <openssl/aes.h> 61 #include <openssl/sha.h> 62 #include "evp_locl.h" 63 #include "constant_time_locl.h" 64 65 #ifndef EVP_CIPH_FLAG_AEAD_CIPHER 66 #define EVP_CIPH_FLAG_AEAD_CIPHER 0x200000 67 #define EVP_CTRL_AEAD_TLS1_AAD 0x16 68 #define EVP_CTRL_AEAD_SET_MAC_KEY 0x17 69 #endif 70 71 #define TLS1_1_VERSION 0x0302 72 73 typedef struct { 74 AES_KEY ks; 75 SHA_CTX head, tail, md; 76 size_t payload_length; /* AAD length in decrypt case */ 77 union { 78 unsigned int tls_ver; 79 unsigned char tls_aad[16]; /* 13 used */ 80 } aux; 81 } EVP_AES_HMAC_SHA1; 82 83 #define NO_PAYLOAD_LENGTH ((size_t)-1) 84 85 #if defined(AES_ASM) && ( \ 86 defined(__x86_64) || defined(__x86_64__) || \ 87 defined(_M_AMD64) || defined(_M_X64) || \ 88 defined(__INTEL__) ) 89 90 #if defined(__GNUC__) && __GNUC__>=2 91 # define BSWAP(x) ({ unsigned int r=(x); asm ("bswapl %0":"=r"(r):"0"(r)); r; }) 92 #endif 93 94 extern unsigned int OPENSSL_ia32cap_P[2]; 95 #define AESNI_CAPABLE (1<<(57-32)) 96 97 int aesni_set_encrypt_key(const unsigned char *userKey, int bits, AES_KEY *key); 98 int aesni_set_decrypt_key(const unsigned char *userKey, int bits, AES_KEY *key); 99 100 void aesni_cbc_encrypt(const unsigned char *in, unsigned char *out, 101 size_t length, const AES_KEY *key, unsigned char *ivec, int enc); 102 103 void aesni_cbc_sha1_enc (const void *inp, void *out, size_t blocks, 104 const AES_KEY *key, unsigned char iv[16], SHA_CTX *ctx, const void *in0); 105 106 #define data(ctx) ((EVP_AES_HMAC_SHA1 *)(ctx)->cipher_data) 107 108 static int 109 aesni_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *inkey, 110 const unsigned char *iv, int enc) 111 { 112 EVP_AES_HMAC_SHA1 *key = data(ctx); 113 int ret; 114 115 if (enc) 116 ret = aesni_set_encrypt_key(inkey, ctx->key_len * 8, &key->ks); 117 else 118 ret = aesni_set_decrypt_key(inkey, ctx->key_len * 8, &key->ks); 119 120 SHA1_Init(&key->head); /* handy when benchmarking */ 121 key->tail = key->head; 122 key->md = key->head; 123 124 key->payload_length = NO_PAYLOAD_LENGTH; 125 126 return ret < 0 ? 0 : 1; 127 } 128 129 #define STITCHED_CALL 130 131 #if !defined(STITCHED_CALL) 132 #define aes_off 0 133 #endif 134 135 void sha1_block_data_order (void *c, const void *p, size_t len); 136 137 static void 138 sha1_update(SHA_CTX *c, const void *data, size_t len) 139 { 140 const unsigned char *ptr = data; 141 size_t res; 142 143 if ((res = c->num)) { 144 res = SHA_CBLOCK - res; 145 if (len < res) 146 res = len; 147 SHA1_Update(c, ptr, res); 148 ptr += res; 149 len -= res; 150 } 151 152 res = len % SHA_CBLOCK; 153 len -= res; 154 155 if (len) { 156 sha1_block_data_order(c, ptr, len / SHA_CBLOCK); 157 158 ptr += len; 159 c->Nh += len >> 29; 160 c->Nl += len <<= 3; 161 if (c->Nl < (unsigned int)len) 162 c->Nh++; 163 } 164 165 if (res) 166 SHA1_Update(c, ptr, res); 167 } 168 169 #ifdef SHA1_Update 170 #undef SHA1_Update 171 #endif 172 #define SHA1_Update sha1_update 173 174 static int 175 aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 176 const unsigned char *in, size_t len) 177 { 178 EVP_AES_HMAC_SHA1 *key = data(ctx); 179 unsigned int l; 180 size_t plen = key->payload_length, 181 iv = 0, /* explicit IV in TLS 1.1 and later */ 182 sha_off = 0; 183 #if defined(STITCHED_CALL) 184 size_t aes_off = 0, blocks; 185 186 sha_off = SHA_CBLOCK - key->md.num; 187 #endif 188 189 key->payload_length = NO_PAYLOAD_LENGTH; 190 191 if (len % AES_BLOCK_SIZE) 192 return 0; 193 194 if (ctx->encrypt) { 195 if (plen == NO_PAYLOAD_LENGTH) 196 plen = len; 197 else if (len != ((plen + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE) & 198 -AES_BLOCK_SIZE)) 199 return 0; 200 else if (key->aux.tls_ver >= TLS1_1_VERSION) 201 iv = AES_BLOCK_SIZE; 202 203 #if defined(STITCHED_CALL) 204 if (plen > (sha_off + iv) && 205 (blocks = (plen - (sha_off + iv)) / SHA_CBLOCK)) { 206 SHA1_Update(&key->md, in + iv, sha_off); 207 208 aesni_cbc_sha1_enc(in, out, blocks, &key->ks, 209 ctx->iv, &key->md, in + iv + sha_off); 210 blocks *= SHA_CBLOCK; 211 aes_off += blocks; 212 sha_off += blocks; 213 key->md.Nh += blocks >> 29; 214 key->md.Nl += blocks <<= 3; 215 if (key->md.Nl < (unsigned int)blocks) 216 key->md.Nh++; 217 } else { 218 sha_off = 0; 219 } 220 #endif 221 sha_off += iv; 222 SHA1_Update(&key->md, in + sha_off, plen - sha_off); 223 224 if (plen != len) { /* "TLS" mode of operation */ 225 if (in != out) 226 memcpy(out + aes_off, in + aes_off, 227 plen - aes_off); 228 229 /* calculate HMAC and append it to payload */ 230 SHA1_Final(out + plen, &key->md); 231 key->md = key->tail; 232 SHA1_Update(&key->md, out + plen, SHA_DIGEST_LENGTH); 233 SHA1_Final(out + plen, &key->md); 234 235 /* pad the payload|hmac */ 236 plen += SHA_DIGEST_LENGTH; 237 for (l = len - plen - 1; plen < len; plen++) 238 out[plen] = l; 239 240 /* encrypt HMAC|padding at once */ 241 aesni_cbc_encrypt(out + aes_off, out + aes_off, 242 len - aes_off, &key->ks, ctx->iv, 1); 243 } else { 244 aesni_cbc_encrypt(in + aes_off, out + aes_off, 245 len - aes_off, &key->ks, ctx->iv, 1); 246 } 247 } else { 248 union { 249 unsigned int u[SHA_DIGEST_LENGTH/sizeof(unsigned int)]; 250 unsigned char c[32 + SHA_DIGEST_LENGTH]; 251 } mac, *pmac; 252 253 /* arrange cache line alignment */ 254 pmac = (void *)(((size_t)mac.c + 31) & ((size_t)0 - 32)); 255 256 /* decrypt HMAC|padding at once */ 257 aesni_cbc_encrypt(in, out, len, &key->ks, ctx->iv, 0); 258 259 if (plen) { /* "TLS" mode of operation */ 260 size_t inp_len, mask, j, i; 261 unsigned int res, maxpad, pad, bitlen; 262 int ret = 1; 263 union { 264 unsigned int u[SHA_LBLOCK]; 265 unsigned char c[SHA_CBLOCK]; 266 } 267 *data = (void *)key->md.data; 268 269 if ((key->aux.tls_aad[plen - 4] << 8 | 270 key->aux.tls_aad[plen - 3]) >= TLS1_1_VERSION) 271 iv = AES_BLOCK_SIZE; 272 273 if (len < (iv + SHA_DIGEST_LENGTH + 1)) 274 return 0; 275 276 /* omit explicit iv */ 277 out += iv; 278 len -= iv; 279 280 /* figure out payload length */ 281 pad = out[len - 1]; 282 maxpad = len - (SHA_DIGEST_LENGTH + 1); 283 maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8); 284 maxpad &= 255; 285 286 ret &= constant_time_ge(maxpad, pad); 287 288 inp_len = len - (SHA_DIGEST_LENGTH + pad + 1); 289 mask = (0 - ((inp_len - len) >> 290 (sizeof(inp_len) * 8 - 1))); 291 inp_len &= mask; 292 ret &= (int)mask; 293 294 key->aux.tls_aad[plen - 2] = inp_len >> 8; 295 key->aux.tls_aad[plen - 1] = inp_len; 296 297 /* calculate HMAC */ 298 key->md = key->head; 299 SHA1_Update(&key->md, key->aux.tls_aad, plen); 300 301 #if 1 302 len -= SHA_DIGEST_LENGTH; /* amend mac */ 303 if (len >= (256 + SHA_CBLOCK)) { 304 j = (len - (256 + SHA_CBLOCK)) & 305 (0 - SHA_CBLOCK); 306 j += SHA_CBLOCK - key->md.num; 307 SHA1_Update(&key->md, out, j); 308 out += j; 309 len -= j; 310 inp_len -= j; 311 } 312 313 /* but pretend as if we hashed padded payload */ 314 bitlen = key->md.Nl + (inp_len << 3); /* at most 18 bits */ 315 #ifdef BSWAP 316 bitlen = BSWAP(bitlen); 317 #else 318 mac.c[0] = 0; 319 mac.c[1] = (unsigned char)(bitlen >> 16); 320 mac.c[2] = (unsigned char)(bitlen >> 8); 321 mac.c[3] = (unsigned char)bitlen; 322 bitlen = mac.u[0]; 323 #endif 324 325 pmac->u[0] = 0; 326 pmac->u[1] = 0; 327 pmac->u[2] = 0; 328 pmac->u[3] = 0; 329 pmac->u[4] = 0; 330 331 for (res = key->md.num, j = 0; j < len; j++) { 332 size_t c = out[j]; 333 mask = (j - inp_len) >> (sizeof(j) * 8 - 8); 334 c &= mask; 335 c |= 0x80 & ~mask & 336 ~((inp_len - j) >> (sizeof(j) * 8 - 8)); 337 data->c[res++] = (unsigned char)c; 338 339 if (res != SHA_CBLOCK) 340 continue; 341 342 /* j is not incremented yet */ 343 mask = 0 - ((inp_len + 7 - j) >> 344 (sizeof(j) * 8 - 1)); 345 data->u[SHA_LBLOCK - 1] |= bitlen&mask; 346 sha1_block_data_order(&key->md, data, 1); 347 mask &= 0 - ((j - inp_len - 72) >> 348 (sizeof(j) * 8 - 1)); 349 pmac->u[0] |= key->md.h0 & mask; 350 pmac->u[1] |= key->md.h1 & mask; 351 pmac->u[2] |= key->md.h2 & mask; 352 pmac->u[3] |= key->md.h3 & mask; 353 pmac->u[4] |= key->md.h4 & mask; 354 res = 0; 355 } 356 357 for (i = res; i < SHA_CBLOCK; i++, j++) 358 data->c[i] = 0; 359 360 if (res > SHA_CBLOCK - 8) { 361 mask = 0 - ((inp_len + 8 - j) >> 362 (sizeof(j) * 8 - 1)); 363 data->u[SHA_LBLOCK - 1] |= bitlen & mask; 364 sha1_block_data_order(&key->md, data, 1); 365 mask &= 0 - ((j - inp_len - 73) >> 366 (sizeof(j) * 8 - 1)); 367 pmac->u[0] |= key->md.h0 & mask; 368 pmac->u[1] |= key->md.h1 & mask; 369 pmac->u[2] |= key->md.h2 & mask; 370 pmac->u[3] |= key->md.h3 & mask; 371 pmac->u[4] |= key->md.h4 & mask; 372 373 memset(data, 0, SHA_CBLOCK); 374 j += 64; 375 } 376 data->u[SHA_LBLOCK - 1] = bitlen; 377 sha1_block_data_order(&key->md, data, 1); 378 mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1)); 379 pmac->u[0] |= key->md.h0 & mask; 380 pmac->u[1] |= key->md.h1 & mask; 381 pmac->u[2] |= key->md.h2 & mask; 382 pmac->u[3] |= key->md.h3 & mask; 383 pmac->u[4] |= key->md.h4 & mask; 384 385 #ifdef BSWAP 386 pmac->u[0] = BSWAP(pmac->u[0]); 387 pmac->u[1] = BSWAP(pmac->u[1]); 388 pmac->u[2] = BSWAP(pmac->u[2]); 389 pmac->u[3] = BSWAP(pmac->u[3]); 390 pmac->u[4] = BSWAP(pmac->u[4]); 391 #else 392 for (i = 0; i < 5; i++) { 393 res = pmac->u[i]; 394 pmac->c[4 * i + 0] = (unsigned char)(res >> 24); 395 pmac->c[4 * i + 1] = (unsigned char)(res >> 16); 396 pmac->c[4 * i + 2] = (unsigned char)(res >> 8); 397 pmac->c[4 * i + 3] = (unsigned char)res; 398 } 399 #endif 400 len += SHA_DIGEST_LENGTH; 401 #else 402 SHA1_Update(&key->md, out, inp_len); 403 res = key->md.num; 404 SHA1_Final(pmac->c, &key->md); 405 406 { 407 unsigned int inp_blocks, pad_blocks; 408 409 /* but pretend as if we hashed padded payload */ 410 inp_blocks = 1 + ((SHA_CBLOCK - 9 - res) >> 411 (sizeof(res) * 8 - 1)); 412 res += (unsigned int)(len - inp_len); 413 pad_blocks = res / SHA_CBLOCK; 414 res %= SHA_CBLOCK; 415 pad_blocks += 1 + ((SHA_CBLOCK - 9 - res) >> 416 (sizeof(res) * 8 - 1)); 417 for (; inp_blocks < pad_blocks; inp_blocks++) 418 sha1_block_data_order(&key->md, 419 data, 1); 420 } 421 #endif 422 key->md = key->tail; 423 SHA1_Update(&key->md, pmac->c, SHA_DIGEST_LENGTH); 424 SHA1_Final(pmac->c, &key->md); 425 426 /* verify HMAC */ 427 out += inp_len; 428 len -= inp_len; 429 #if 1 430 { 431 unsigned char *p = 432 out + len - 1 - maxpad - SHA_DIGEST_LENGTH; 433 size_t off = out - p; 434 unsigned int c, cmask; 435 436 maxpad += SHA_DIGEST_LENGTH; 437 for (res = 0, i = 0, j = 0; j < maxpad; j++) { 438 c = p[j]; 439 cmask = ((int)(j - off - 440 SHA_DIGEST_LENGTH)) >> 441 (sizeof(int) * 8 - 1); 442 res |= (c ^ pad) & ~cmask; /* ... and padding */ 443 cmask &= ((int)(off - 1 - j)) >> 444 (sizeof(int) * 8 - 1); 445 res |= (c ^ pmac->c[i]) & cmask; 446 i += 1 & cmask; 447 } 448 maxpad -= SHA_DIGEST_LENGTH; 449 450 res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1)); 451 ret &= (int)~res; 452 } 453 #else 454 for (res = 0, i = 0; i < SHA_DIGEST_LENGTH; i++) 455 res |= out[i] ^ pmac->c[i]; 456 res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1)); 457 ret &= (int)~res; 458 459 /* verify padding */ 460 pad = (pad & ~res) | (maxpad & res); 461 out = out + len - 1 - pad; 462 for (res = 0, i = 0; i < pad; i++) 463 res |= out[i] ^ pad; 464 465 res = (0 - res) >> (sizeof(res) * 8 - 1); 466 ret &= (int)~res; 467 #endif 468 return ret; 469 } else { 470 SHA1_Update(&key->md, out, len); 471 } 472 } 473 474 return 1; 475 } 476 477 static int 478 aesni_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) 479 { 480 EVP_AES_HMAC_SHA1 *key = data(ctx); 481 482 switch (type) { 483 case EVP_CTRL_AEAD_SET_MAC_KEY: 484 { 485 unsigned int i; 486 unsigned char hmac_key[64]; 487 488 memset(hmac_key, 0, sizeof(hmac_key)); 489 490 if (arg > (int)sizeof(hmac_key)) { 491 SHA1_Init(&key->head); 492 SHA1_Update(&key->head, ptr, arg); 493 SHA1_Final(hmac_key, &key->head); 494 } else { 495 memcpy(hmac_key, ptr, arg); 496 } 497 498 for (i = 0; i < sizeof(hmac_key); i++) 499 hmac_key[i] ^= 0x36; /* ipad */ 500 SHA1_Init(&key->head); 501 SHA1_Update(&key->head, hmac_key, sizeof(hmac_key)); 502 503 for (i = 0; i < sizeof(hmac_key); i++) 504 hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */ 505 SHA1_Init(&key->tail); 506 SHA1_Update(&key->tail, hmac_key, sizeof(hmac_key)); 507 508 explicit_bzero(hmac_key, sizeof(hmac_key)); 509 510 return 1; 511 } 512 case EVP_CTRL_AEAD_TLS1_AAD: 513 { 514 unsigned char *p = ptr; 515 unsigned int len = p[arg - 2] << 8 | p[arg - 1]; 516 517 if (ctx->encrypt) { 518 key->payload_length = len; 519 if ((key->aux.tls_ver = p[arg - 4] << 8 | 520 p[arg - 3]) >= TLS1_1_VERSION) { 521 len -= AES_BLOCK_SIZE; 522 p[arg - 2] = len >> 8; 523 p[arg - 1] = len; 524 } 525 key->md = key->head; 526 SHA1_Update(&key->md, p, arg); 527 528 return (int)(((len + SHA_DIGEST_LENGTH + 529 AES_BLOCK_SIZE) & -AES_BLOCK_SIZE) - len); 530 } else { 531 if (arg > 13) 532 arg = 13; 533 memcpy(key->aux.tls_aad, ptr, arg); 534 key->payload_length = arg; 535 536 return SHA_DIGEST_LENGTH; 537 } 538 } 539 default: 540 return -1; 541 } 542 } 543 544 static EVP_CIPHER aesni_128_cbc_hmac_sha1_cipher = { 545 #ifdef NID_aes_128_cbc_hmac_sha1 546 .nid = NID_aes_128_cbc_hmac_sha1, 547 #else 548 .nid = NID_undef, 549 #endif 550 .block_size = 16, 551 .key_len = 16, 552 .iv_len = 16, 553 .flags = EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 | 554 EVP_CIPH_FLAG_AEAD_CIPHER, 555 .init = aesni_cbc_hmac_sha1_init_key, 556 .do_cipher = aesni_cbc_hmac_sha1_cipher, 557 .ctx_size = sizeof(EVP_AES_HMAC_SHA1), 558 .ctrl = aesni_cbc_hmac_sha1_ctrl 559 }; 560 561 static EVP_CIPHER aesni_256_cbc_hmac_sha1_cipher = { 562 #ifdef NID_aes_256_cbc_hmac_sha1 563 .nid = NID_aes_256_cbc_hmac_sha1, 564 #else 565 .nid = NID_undef, 566 #endif 567 .block_size = 16, 568 .key_len = 32, 569 .iv_len = 16, 570 .flags = EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 | 571 EVP_CIPH_FLAG_AEAD_CIPHER, 572 .init = aesni_cbc_hmac_sha1_init_key, 573 .do_cipher = aesni_cbc_hmac_sha1_cipher, 574 .ctx_size = sizeof(EVP_AES_HMAC_SHA1), 575 .ctrl = aesni_cbc_hmac_sha1_ctrl 576 }; 577 578 const EVP_CIPHER * 579 EVP_aes_128_cbc_hmac_sha1(void) 580 { 581 return OPENSSL_ia32cap_P[1] & AESNI_CAPABLE ? 582 &aesni_128_cbc_hmac_sha1_cipher : NULL; 583 } 584 585 const EVP_CIPHER * 586 EVP_aes_256_cbc_hmac_sha1(void) 587 { 588 return OPENSSL_ia32cap_P[1] & AESNI_CAPABLE ? 589 &aesni_256_cbc_hmac_sha1_cipher : NULL; 590 } 591 #else 592 const EVP_CIPHER * 593 EVP_aes_128_cbc_hmac_sha1(void) 594 { 595 return NULL; 596 } 597 598 const EVP_CIPHER * 599 EVP_aes_256_cbc_hmac_sha1(void) 600 { 601 return NULL; 602 } 603 #endif 604 #endif 605