diff options
Diffstat (limited to 'src/lib/libcrypto/evp/e_aes_cbc_hmac_sha1.c')
| -rw-r--r-- | src/lib/libcrypto/evp/e_aes_cbc_hmac_sha1.c | 601 |
1 files changed, 0 insertions, 601 deletions
diff --git a/src/lib/libcrypto/evp/e_aes_cbc_hmac_sha1.c b/src/lib/libcrypto/evp/e_aes_cbc_hmac_sha1.c deleted file mode 100644 index 7c23face34..0000000000 --- a/src/lib/libcrypto/evp/e_aes_cbc_hmac_sha1.c +++ /dev/null | |||
| @@ -1,601 +0,0 @@ | |||
| 1 | /* $OpenBSD: e_aes_cbc_hmac_sha1.c,v 1.8 2014/07/12 20:37:07 miod 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 | |||
| 64 | #ifndef EVP_CIPH_FLAG_AEAD_CIPHER | ||
| 65 | #define EVP_CIPH_FLAG_AEAD_CIPHER 0x200000 | ||
| 66 | #define EVP_CTRL_AEAD_TLS1_AAD 0x16 | ||
| 67 | #define EVP_CTRL_AEAD_SET_MAC_KEY 0x17 | ||
| 68 | #endif | ||
| 69 | |||
| 70 | #define TLS1_1_VERSION 0x0302 | ||
| 71 | |||
| 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 | |||
| 82 | #define NO_PAYLOAD_LENGTH ((size_t)-1) | ||
| 83 | |||
| 84 | #if defined(AES_ASM) && ( \ | ||
| 85 | defined(__x86_64) || defined(__x86_64__) || \ | ||
| 86 | defined(_M_AMD64) || defined(_M_X64) || \ | ||
| 87 | defined(__INTEL__) ) | ||
| 88 | |||
| 89 | #if defined(__GNUC__) && __GNUC__>=2 | ||
| 90 | # define BSWAP(x) ({ unsigned int r=(x); asm ("bswapl %0":"=r"(r):"0"(r)); r; }) | ||
| 91 | #endif | ||
| 92 | |||
| 93 | extern unsigned int OPENSSL_ia32cap_P[2]; | ||
| 94 | #define AESNI_CAPABLE (1<<(57-32)) | ||
| 95 | |||
| 96 | int aesni_set_encrypt_key(const unsigned char *userKey, int bits, AES_KEY *key); | ||
| 97 | int aesni_set_decrypt_key(const unsigned char *userKey, int bits, AES_KEY *key); | ||
| 98 | |||
| 99 | void aesni_cbc_encrypt(const unsigned char *in, unsigned char *out, | ||
| 100 | size_t length, const AES_KEY *key, unsigned char *ivec, int enc); | ||
| 101 | |||
| 102 | void aesni_cbc_sha1_enc (const void *inp, void *out, size_t blocks, | ||
| 103 | const AES_KEY *key, unsigned char iv[16], SHA_CTX *ctx, const void *in0); | ||
| 104 | |||
| 105 | #define data(ctx) ((EVP_AES_HMAC_SHA1 *)(ctx)->cipher_data) | ||
| 106 | |||
| 107 | static int | ||
| 108 | aesni_cbc_hmac_sha1_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *inkey, | ||
| 109 | const unsigned char *iv, int enc) | ||
| 110 | { | ||
| 111 | EVP_AES_HMAC_SHA1 *key = data(ctx); | ||
| 112 | int ret; | ||
| 113 | |||
| 114 | if (enc) | ||
| 115 | ret = aesni_set_encrypt_key(inkey, ctx->key_len * 8, &key->ks); | ||
| 116 | else | ||
| 117 | ret = aesni_set_decrypt_key(inkey, ctx->key_len * 8, &key->ks); | ||
| 118 | |||
| 119 | SHA1_Init(&key->head); /* handy when benchmarking */ | ||
| 120 | key->tail = key->head; | ||
| 121 | key->md = key->head; | ||
| 122 | |||
| 123 | key->payload_length = NO_PAYLOAD_LENGTH; | ||
| 124 | |||
| 125 | return ret < 0 ? 0 : 1; | ||
| 126 | } | ||
| 127 | |||
| 128 | #define STITCHED_CALL | ||
| 129 | |||
| 130 | #if !defined(STITCHED_CALL) | ||
| 131 | #define aes_off 0 | ||
| 132 | #endif | ||
| 133 | |||
| 134 | void sha1_block_data_order (void *c, const void *p, size_t len); | ||
| 135 | |||
| 136 | static void | ||
| 137 | sha1_update(SHA_CTX *c, const void *data, size_t len) | ||
| 138 | { | ||
| 139 | const unsigned char *ptr = data; | ||
| 140 | size_t res; | ||
| 141 | |||
| 142 | if ((res = c->num)) { | ||
| 143 | res = SHA_CBLOCK - res; | ||
| 144 | if (len < res) | ||
| 145 | res = len; | ||
| 146 | SHA1_Update(c, ptr, res); | ||
| 147 | ptr += res; | ||
| 148 | len -= res; | ||
| 149 | } | ||
| 150 | |||
| 151 | res = len % SHA_CBLOCK; | ||
| 152 | len -= res; | ||
| 153 | |||
| 154 | if (len) { | ||
| 155 | sha1_block_data_order(c, ptr, len / SHA_CBLOCK); | ||
| 156 | |||
| 157 | ptr += len; | ||
| 158 | c->Nh += len >> 29; | ||
| 159 | c->Nl += len <<= 3; | ||
| 160 | if (c->Nl < (unsigned int)len) | ||
| 161 | c->Nh++; | ||
| 162 | } | ||
| 163 | |||
| 164 | if (res) | ||
| 165 | SHA1_Update(c, ptr, res); | ||
| 166 | } | ||
| 167 | |||
| 168 | #ifdef SHA1_Update | ||
| 169 | #undef SHA1_Update | ||
| 170 | #endif | ||
| 171 | #define SHA1_Update sha1_update | ||
| 172 | |||
| 173 | static int | ||
| 174 | aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | ||
| 175 | const unsigned char *in, size_t len) | ||
| 176 | { | ||
| 177 | EVP_AES_HMAC_SHA1 *key = data(ctx); | ||
| 178 | unsigned int l; | ||
| 179 | size_t plen = key->payload_length, | ||
| 180 | iv = 0, /* explicit IV in TLS 1.1 and later */ | ||
| 181 | sha_off = 0; | ||
| 182 | #if defined(STITCHED_CALL) | ||
| 183 | size_t aes_off = 0, blocks; | ||
| 184 | |||
| 185 | sha_off = SHA_CBLOCK - key->md.num; | ||
| 186 | #endif | ||
| 187 | |||
| 188 | key->payload_length = NO_PAYLOAD_LENGTH; | ||
| 189 | |||
| 190 | if (len % AES_BLOCK_SIZE) | ||
| 191 | return 0; | ||
| 192 | |||
| 193 | if (ctx->encrypt) { | ||
| 194 | if (plen == NO_PAYLOAD_LENGTH) | ||
| 195 | plen = len; | ||
| 196 | else if (len != ((plen + SHA_DIGEST_LENGTH + AES_BLOCK_SIZE) & | ||
| 197 | -AES_BLOCK_SIZE)) | ||
| 198 | return 0; | ||
| 199 | else if (key->aux.tls_ver >= TLS1_1_VERSION) | ||
| 200 | iv = AES_BLOCK_SIZE; | ||
| 201 | |||
| 202 | #if defined(STITCHED_CALL) | ||
| 203 | if (plen > (sha_off + iv) && | ||
| 204 | (blocks = (plen - (sha_off + iv)) / SHA_CBLOCK)) { | ||
| 205 | SHA1_Update(&key->md, in + iv, sha_off); | ||
| 206 | |||
| 207 | aesni_cbc_sha1_enc(in, out, blocks, &key->ks, | ||
| 208 | ctx->iv, &key->md, in + iv + sha_off); | ||
| 209 | blocks *= SHA_CBLOCK; | ||
| 210 | aes_off += blocks; | ||
| 211 | sha_off += blocks; | ||
| 212 | key->md.Nh += blocks >> 29; | ||
| 213 | key->md.Nl += blocks <<= 3; | ||
| 214 | if (key->md.Nl < (unsigned int)blocks) | ||
| 215 | key->md.Nh++; | ||
| 216 | } else { | ||
| 217 | sha_off = 0; | ||
| 218 | } | ||
| 219 | #endif | ||
| 220 | sha_off += iv; | ||
| 221 | SHA1_Update(&key->md, in + sha_off, plen - sha_off); | ||
| 222 | |||
| 223 | if (plen != len) { /* "TLS" mode of operation */ | ||
| 224 | if (in != out) | ||
| 225 | memcpy(out + aes_off, in + aes_off, | ||
| 226 | plen - aes_off); | ||
| 227 | |||
| 228 | /* calculate HMAC and append it to payload */ | ||
| 229 | SHA1_Final(out + plen, &key->md); | ||
| 230 | key->md = key->tail; | ||
| 231 | SHA1_Update(&key->md, out + plen, SHA_DIGEST_LENGTH); | ||
| 232 | SHA1_Final(out + plen, &key->md); | ||
| 233 | |||
| 234 | /* pad the payload|hmac */ | ||
| 235 | plen += SHA_DIGEST_LENGTH; | ||
| 236 | for (l = len - plen - 1; plen < len; plen++) | ||
| 237 | out[plen] = l; | ||
| 238 | |||
| 239 | /* encrypt HMAC|padding at once */ | ||
| 240 | aesni_cbc_encrypt(out + aes_off, out + aes_off, | ||
| 241 | len - aes_off, &key->ks, ctx->iv, 1); | ||
| 242 | } else { | ||
| 243 | aesni_cbc_encrypt(in + aes_off, out + aes_off, | ||
| 244 | len - aes_off, &key->ks, ctx->iv, 1); | ||
| 245 | } | ||
| 246 | } else { | ||
| 247 | union { | ||
| 248 | unsigned int u[SHA_DIGEST_LENGTH/sizeof(unsigned int)]; | ||
| 249 | unsigned char c[32 + SHA_DIGEST_LENGTH]; | ||
| 250 | } mac, *pmac; | ||
| 251 | |||
| 252 | /* arrange cache line alignment */ | ||
| 253 | pmac = (void *)(((size_t)mac.c + 31) & ((size_t)0 - 32)); | ||
| 254 | |||
| 255 | /* decrypt HMAC|padding at once */ | ||
| 256 | aesni_cbc_encrypt(in, out, len, &key->ks, ctx->iv, 0); | ||
| 257 | |||
| 258 | if (plen) { /* "TLS" mode of operation */ | ||
| 259 | size_t inp_len, mask, j, i; | ||
| 260 | unsigned int res, maxpad, pad, bitlen; | ||
| 261 | int ret = 1; | ||
| 262 | union { | ||
| 263 | unsigned int u[SHA_LBLOCK]; | ||
| 264 | unsigned char c[SHA_CBLOCK]; | ||
| 265 | } | ||
| 266 | *data = (void *)key->md.data; | ||
| 267 | |||
| 268 | if ((key->aux.tls_aad[plen - 4] << 8 | | ||
| 269 | key->aux.tls_aad[plen - 3]) >= TLS1_1_VERSION) | ||
| 270 | iv = AES_BLOCK_SIZE; | ||
| 271 | |||
| 272 | if (len < (iv + SHA_DIGEST_LENGTH + 1)) | ||
| 273 | return 0; | ||
| 274 | |||
| 275 | /* omit explicit iv */ | ||
| 276 | out += iv; | ||
| 277 | len -= iv; | ||
| 278 | |||
| 279 | /* figure out payload length */ | ||
| 280 | pad = out[len - 1]; | ||
| 281 | maxpad = len - (SHA_DIGEST_LENGTH + 1); | ||
| 282 | maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8); | ||
| 283 | maxpad &= 255; | ||
| 284 | |||
| 285 | inp_len = len - (SHA_DIGEST_LENGTH + pad + 1); | ||
| 286 | mask = (0 - ((inp_len - len) >> | ||
| 287 | (sizeof(inp_len) * 8 - 1))); | ||
| 288 | inp_len &= mask; | ||
| 289 | ret &= (int)mask; | ||
| 290 | |||
| 291 | key->aux.tls_aad[plen - 2] = inp_len >> 8; | ||
| 292 | key->aux.tls_aad[plen - 1] = inp_len; | ||
| 293 | |||
| 294 | /* calculate HMAC */ | ||
| 295 | key->md = key->head; | ||
| 296 | SHA1_Update(&key->md, key->aux.tls_aad, plen); | ||
| 297 | |||
| 298 | #if 1 | ||
| 299 | len -= SHA_DIGEST_LENGTH; /* amend mac */ | ||
| 300 | if (len >= (256 + SHA_CBLOCK)) { | ||
| 301 | j = (len - (256 + SHA_CBLOCK)) & | ||
| 302 | (0 - SHA_CBLOCK); | ||
| 303 | j += SHA_CBLOCK - key->md.num; | ||
| 304 | SHA1_Update(&key->md, out, j); | ||
| 305 | out += j; | ||
| 306 | len -= j; | ||
| 307 | inp_len -= j; | ||
| 308 | } | ||
| 309 | |||
| 310 | /* but pretend as if we hashed padded payload */ | ||
| 311 | bitlen = key->md.Nl + (inp_len << 3); /* at most 18 bits */ | ||
| 312 | #ifdef BSWAP | ||
| 313 | bitlen = BSWAP(bitlen); | ||
| 314 | #else | ||
| 315 | mac.c[0] = 0; | ||
| 316 | mac.c[1] = (unsigned char)(bitlen >> 16); | ||
| 317 | mac.c[2] = (unsigned char)(bitlen >> 8); | ||
| 318 | mac.c[3] = (unsigned char)bitlen; | ||
| 319 | bitlen = mac.u[0]; | ||
| 320 | #endif | ||
| 321 | |||
| 322 | pmac->u[0] = 0; | ||
| 323 | pmac->u[1] = 0; | ||
| 324 | pmac->u[2] = 0; | ||
| 325 | pmac->u[3] = 0; | ||
| 326 | pmac->u[4] = 0; | ||
| 327 | |||
| 328 | for (res = key->md.num, j = 0; j < len; j++) { | ||
| 329 | size_t c = out[j]; | ||
| 330 | mask = (j - inp_len) >> (sizeof(j) * 8 - 8); | ||
| 331 | c &= mask; | ||
| 332 | c |= 0x80 & ~mask & | ||
| 333 | ~((inp_len - j) >> (sizeof(j) * 8 - 8)); | ||
| 334 | data->c[res++] = (unsigned char)c; | ||
| 335 | |||
| 336 | if (res != SHA_CBLOCK) | ||
| 337 | continue; | ||
| 338 | |||
| 339 | /* j is not incremented yet */ | ||
| 340 | mask = 0 - ((inp_len + 7 - j) >> | ||
| 341 | (sizeof(j) * 8 - 1)); | ||
| 342 | data->u[SHA_LBLOCK - 1] |= bitlen&mask; | ||
| 343 | sha1_block_data_order(&key->md, data, 1); | ||
| 344 | mask &= 0 - ((j - inp_len - 72) >> | ||
| 345 | (sizeof(j) * 8 - 1)); | ||
| 346 | pmac->u[0] |= key->md.h0 & mask; | ||
| 347 | pmac->u[1] |= key->md.h1 & mask; | ||
| 348 | pmac->u[2] |= key->md.h2 & mask; | ||
| 349 | pmac->u[3] |= key->md.h3 & mask; | ||
| 350 | pmac->u[4] |= key->md.h4 & mask; | ||
| 351 | res = 0; | ||
| 352 | } | ||
| 353 | |||
| 354 | for (i = res; i < SHA_CBLOCK; i++, j++) | ||
| 355 | data->c[i] = 0; | ||
| 356 | |||
| 357 | if (res > SHA_CBLOCK - 8) { | ||
| 358 | mask = 0 - ((inp_len + 8 - j) >> | ||
| 359 | (sizeof(j) * 8 - 1)); | ||
| 360 | data->u[SHA_LBLOCK - 1] |= bitlen & mask; | ||
| 361 | sha1_block_data_order(&key->md, data, 1); | ||
| 362 | mask &= 0 - ((j - inp_len - 73) >> | ||
| 363 | (sizeof(j) * 8 - 1)); | ||
| 364 | pmac->u[0] |= key->md.h0 & mask; | ||
| 365 | pmac->u[1] |= key->md.h1 & mask; | ||
| 366 | pmac->u[2] |= key->md.h2 & mask; | ||
| 367 | pmac->u[3] |= key->md.h3 & mask; | ||
| 368 | pmac->u[4] |= key->md.h4 & mask; | ||
| 369 | |||
| 370 | memset(data, 0, SHA_CBLOCK); | ||
| 371 | j += 64; | ||
| 372 | } | ||
| 373 | data->u[SHA_LBLOCK - 1] = bitlen; | ||
| 374 | sha1_block_data_order(&key->md, data, 1); | ||
| 375 | mask = 0 - ((j - inp_len - 73) >> (sizeof(j) * 8 - 1)); | ||
| 376 | pmac->u[0] |= key->md.h0 & mask; | ||
| 377 | pmac->u[1] |= key->md.h1 & mask; | ||
| 378 | pmac->u[2] |= key->md.h2 & mask; | ||
| 379 | pmac->u[3] |= key->md.h3 & mask; | ||
| 380 | pmac->u[4] |= key->md.h4 & mask; | ||
| 381 | |||
| 382 | #ifdef BSWAP | ||
| 383 | pmac->u[0] = BSWAP(pmac->u[0]); | ||
| 384 | pmac->u[1] = BSWAP(pmac->u[1]); | ||
| 385 | pmac->u[2] = BSWAP(pmac->u[2]); | ||
| 386 | pmac->u[3] = BSWAP(pmac->u[3]); | ||
| 387 | pmac->u[4] = BSWAP(pmac->u[4]); | ||
| 388 | #else | ||
| 389 | for (i = 0; i < 5; i++) { | ||
| 390 | res = pmac->u[i]; | ||
| 391 | pmac->c[4 * i + 0] = (unsigned char)(res >> 24); | ||
| 392 | pmac->c[4 * i + 1] = (unsigned char)(res >> 16); | ||
| 393 | pmac->c[4 * i + 2] = (unsigned char)(res >> 8); | ||
| 394 | pmac->c[4 * i + 3] = (unsigned char)res; | ||
| 395 | } | ||
| 396 | #endif | ||
| 397 | len += SHA_DIGEST_LENGTH; | ||
| 398 | #else | ||
| 399 | SHA1_Update(&key->md, out, inp_len); | ||
| 400 | res = key->md.num; | ||
| 401 | SHA1_Final(pmac->c, &key->md); | ||
| 402 | |||
| 403 | { | ||
| 404 | unsigned int inp_blocks, pad_blocks; | ||
| 405 | |||
| 406 | /* but pretend as if we hashed padded payload */ | ||
| 407 | inp_blocks = 1 + ((SHA_CBLOCK - 9 - res) >> | ||
| 408 | (sizeof(res) * 8 - 1)); | ||
| 409 | res += (unsigned int)(len - inp_len); | ||
| 410 | pad_blocks = res / SHA_CBLOCK; | ||
| 411 | res %= SHA_CBLOCK; | ||
| 412 | pad_blocks += 1 + ((SHA_CBLOCK - 9 - res) >> | ||
| 413 | (sizeof(res) * 8 - 1)); | ||
| 414 | for (; inp_blocks < pad_blocks; inp_blocks++) | ||
| 415 | sha1_block_data_order(&key->md, | ||
| 416 | data, 1); | ||
| 417 | } | ||
| 418 | #endif | ||
| 419 | key->md = key->tail; | ||
| 420 | SHA1_Update(&key->md, pmac->c, SHA_DIGEST_LENGTH); | ||
| 421 | SHA1_Final(pmac->c, &key->md); | ||
| 422 | |||
| 423 | /* verify HMAC */ | ||
| 424 | out += inp_len; | ||
| 425 | len -= inp_len; | ||
| 426 | #if 1 | ||
| 427 | { | ||
| 428 | unsigned char *p = | ||
| 429 | out + len - 1 - maxpad - SHA_DIGEST_LENGTH; | ||
| 430 | size_t off = out - p; | ||
| 431 | unsigned int c, cmask; | ||
| 432 | |||
| 433 | maxpad += SHA_DIGEST_LENGTH; | ||
| 434 | for (res = 0, i = 0, j = 0; j < maxpad; j++) { | ||
| 435 | c = p[j]; | ||
| 436 | cmask = ((int)(j - off - | ||
| 437 | SHA_DIGEST_LENGTH)) >> | ||
| 438 | (sizeof(int) * 8 - 1); | ||
| 439 | res |= (c ^ pad) & ~cmask; /* ... and padding */ | ||
| 440 | cmask &= ((int)(off - 1 - j)) >> | ||
| 441 | (sizeof(int) * 8 - 1); | ||
| 442 | res |= (c ^ pmac->c[i]) & cmask; | ||
| 443 | i += 1 & cmask; | ||
| 444 | } | ||
| 445 | maxpad -= SHA_DIGEST_LENGTH; | ||
| 446 | |||
| 447 | res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1)); | ||
| 448 | ret &= (int)~res; | ||
| 449 | } | ||
| 450 | #else | ||
| 451 | for (res = 0, i = 0; i < SHA_DIGEST_LENGTH; i++) | ||
| 452 | res |= out[i] ^ pmac->c[i]; | ||
| 453 | res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1)); | ||
| 454 | ret &= (int)~res; | ||
| 455 | |||
| 456 | /* verify padding */ | ||
| 457 | pad = (pad & ~res) | (maxpad & res); | ||
| 458 | out = out + len - 1 - pad; | ||
| 459 | for (res = 0, i = 0; i < pad; i++) | ||
| 460 | res |= out[i] ^ pad; | ||
| 461 | |||
| 462 | res = (0 - res) >> (sizeof(res) * 8 - 1); | ||
| 463 | ret &= (int)~res; | ||
| 464 | #endif | ||
| 465 | return ret; | ||
| 466 | } else { | ||
| 467 | SHA1_Update(&key->md, out, len); | ||
| 468 | } | ||
| 469 | } | ||
| 470 | |||
| 471 | return 1; | ||
| 472 | } | ||
| 473 | |||
| 474 | static int | ||
| 475 | aesni_cbc_hmac_sha1_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) | ||
| 476 | { | ||
| 477 | EVP_AES_HMAC_SHA1 *key = data(ctx); | ||
| 478 | |||
| 479 | switch (type) { | ||
| 480 | case EVP_CTRL_AEAD_SET_MAC_KEY: | ||
| 481 | { | ||
| 482 | unsigned int i; | ||
| 483 | unsigned char hmac_key[64]; | ||
| 484 | |||
| 485 | memset(hmac_key, 0, sizeof(hmac_key)); | ||
| 486 | |||
| 487 | if (arg > (int)sizeof(hmac_key)) { | ||
| 488 | SHA1_Init(&key->head); | ||
| 489 | SHA1_Update(&key->head, ptr, arg); | ||
| 490 | SHA1_Final(hmac_key, &key->head); | ||
| 491 | } else { | ||
| 492 | memcpy(hmac_key, ptr, arg); | ||
| 493 | } | ||
| 494 | |||
| 495 | for (i = 0; i < sizeof(hmac_key); i++) | ||
| 496 | hmac_key[i] ^= 0x36; /* ipad */ | ||
| 497 | SHA1_Init(&key->head); | ||
| 498 | SHA1_Update(&key->head, hmac_key, sizeof(hmac_key)); | ||
| 499 | |||
| 500 | for (i = 0; i < sizeof(hmac_key); i++) | ||
| 501 | hmac_key[i] ^= 0x36 ^ 0x5c; /* opad */ | ||
| 502 | SHA1_Init(&key->tail); | ||
| 503 | SHA1_Update(&key->tail, hmac_key, sizeof(hmac_key)); | ||
| 504 | |||
| 505 | OPENSSL_cleanse(hmac_key, sizeof(hmac_key)); | ||
| 506 | |||
| 507 | return 1; | ||
| 508 | } | ||
| 509 | case EVP_CTRL_AEAD_TLS1_AAD: | ||
| 510 | { | ||
| 511 | unsigned char *p = ptr; | ||
| 512 | unsigned int len = p[arg - 2] << 8 | p[arg - 1]; | ||
| 513 | |||
| 514 | if (ctx->encrypt) { | ||
| 515 | key->payload_length = len; | ||
| 516 | if ((key->aux.tls_ver = p[arg - 4] << 8 | | ||
| 517 | p[arg - 3]) >= TLS1_1_VERSION) { | ||
| 518 | len -= AES_BLOCK_SIZE; | ||
| 519 | p[arg - 2] = len >> 8; | ||
| 520 | p[arg - 1] = len; | ||
| 521 | } | ||
| 522 | key->md = key->head; | ||
| 523 | SHA1_Update(&key->md, p, arg); | ||
| 524 | |||
| 525 | return (int)(((len + SHA_DIGEST_LENGTH + | ||
| 526 | AES_BLOCK_SIZE) & -AES_BLOCK_SIZE) - len); | ||
| 527 | } else { | ||
| 528 | if (arg > 13) | ||
| 529 | arg = 13; | ||
| 530 | memcpy(key->aux.tls_aad, ptr, arg); | ||
| 531 | key->payload_length = arg; | ||
| 532 | |||
| 533 | return SHA_DIGEST_LENGTH; | ||
| 534 | } | ||
| 535 | } | ||
| 536 | default: | ||
| 537 | return -1; | ||
| 538 | } | ||
| 539 | } | ||
| 540 | |||
| 541 | static EVP_CIPHER aesni_128_cbc_hmac_sha1_cipher = { | ||
| 542 | #ifdef NID_aes_128_cbc_hmac_sha1 | ||
| 543 | .nid = NID_aes_128_cbc_hmac_sha1, | ||
| 544 | #else | ||
| 545 | .nid = NID_undef, | ||
| 546 | #endif | ||
| 547 | .block_size = 16, | ||
| 548 | .key_len = 16, | ||
| 549 | .iv_len = 16, | ||
| 550 | .flags = EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 | | ||
| 551 | EVP_CIPH_FLAG_AEAD_CIPHER, | ||
| 552 | .init = aesni_cbc_hmac_sha1_init_key, | ||
| 553 | .do_cipher = aesni_cbc_hmac_sha1_cipher, | ||
| 554 | .ctx_size = sizeof(EVP_AES_HMAC_SHA1), | ||
| 555 | .ctrl = aesni_cbc_hmac_sha1_ctrl | ||
| 556 | }; | ||
| 557 | |||
| 558 | static EVP_CIPHER aesni_256_cbc_hmac_sha1_cipher = { | ||
| 559 | #ifdef NID_aes_256_cbc_hmac_sha1 | ||
| 560 | .nid = NID_aes_256_cbc_hmac_sha1, | ||
| 561 | #else | ||
| 562 | .nid = NID_undef, | ||
| 563 | #endif | ||
| 564 | .block_size = 16, | ||
| 565 | .key_len = 32, | ||
| 566 | .iv_len = 16, | ||
| 567 | .flags = EVP_CIPH_CBC_MODE | EVP_CIPH_FLAG_DEFAULT_ASN1 | | ||
| 568 | EVP_CIPH_FLAG_AEAD_CIPHER, | ||
| 569 | .init = aesni_cbc_hmac_sha1_init_key, | ||
| 570 | .do_cipher = aesni_cbc_hmac_sha1_cipher, | ||
| 571 | .ctx_size = sizeof(EVP_AES_HMAC_SHA1), | ||
| 572 | .ctrl = aesni_cbc_hmac_sha1_ctrl | ||
| 573 | }; | ||
| 574 | |||
| 575 | const EVP_CIPHER * | ||
| 576 | EVP_aes_128_cbc_hmac_sha1(void) | ||
| 577 | { | ||
| 578 | return OPENSSL_ia32cap_P[1] & AESNI_CAPABLE ? | ||
| 579 | &aesni_128_cbc_hmac_sha1_cipher : NULL; | ||
| 580 | } | ||
| 581 | |||
| 582 | const EVP_CIPHER * | ||
| 583 | EVP_aes_256_cbc_hmac_sha1(void) | ||
| 584 | { | ||
| 585 | return OPENSSL_ia32cap_P[1] & AESNI_CAPABLE ? | ||
| 586 | &aesni_256_cbc_hmac_sha1_cipher : NULL; | ||
| 587 | } | ||
| 588 | #else | ||
| 589 | const EVP_CIPHER * | ||
| 590 | EVP_aes_128_cbc_hmac_sha1(void) | ||
| 591 | { | ||
| 592 | return NULL; | ||
| 593 | } | ||
| 594 | |||
| 595 | const EVP_CIPHER * | ||
| 596 | EVP_aes_256_cbc_hmac_sha1(void) | ||
| 597 | { | ||
| 598 | return NULL; | ||
| 599 | } | ||
| 600 | #endif | ||
| 601 | #endif | ||
