Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1 | /* ==================================================================== |
| 2 | * Copyright (c) 2001-2011 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 | * openssl-core@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 <string.h> |
| 50 | |
| 51 | #include <openssl/aead.h> |
| 52 | #include <openssl/aes.h> |
| 53 | #include <openssl/cipher.h> |
| 54 | #include <openssl/cpu.h> |
| 55 | #include <openssl/err.h> |
| 56 | #include <openssl/mem.h> |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 57 | #include <openssl/nid.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 58 | #include <openssl/rand.h> |
| 59 | |
| 60 | #include "internal.h" |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 61 | #include "../internal.h" |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame^] | 62 | #include "../fipsmodule/modes/internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 63 | |
| 64 | #if defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 65 | #include <openssl/arm_arch.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 66 | #endif |
| 67 | |
| 68 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 69 | OPENSSL_MSVC_PRAGMA(warning(disable: 4702)) /* Unreachable code. */ |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 70 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 71 | typedef struct { |
| 72 | union { |
| 73 | double align; |
| 74 | AES_KEY ks; |
| 75 | } ks; |
| 76 | block128_f block; |
| 77 | union { |
| 78 | cbc128_f cbc; |
| 79 | ctr128_f ctr; |
| 80 | } stream; |
| 81 | } EVP_AES_KEY; |
| 82 | |
| 83 | typedef struct { |
| 84 | union { |
| 85 | double align; |
| 86 | AES_KEY ks; |
| 87 | } ks; /* AES key schedule to use */ |
| 88 | int key_set; /* Set if key initialised */ |
| 89 | int iv_set; /* Set if an iv is set */ |
| 90 | GCM128_CONTEXT gcm; |
| 91 | uint8_t *iv; /* Temporary IV store */ |
| 92 | int ivlen; /* IV length */ |
| 93 | int taglen; |
| 94 | int iv_gen; /* It is OK to generate IVs */ |
| 95 | ctr128_f ctr; |
| 96 | } EVP_AES_GCM_CTX; |
| 97 | |
| 98 | #if !defined(OPENSSL_NO_ASM) && \ |
| 99 | (defined(OPENSSL_X86_64) || defined(OPENSSL_X86)) |
| 100 | #define VPAES |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 101 | static char vpaes_capable(void) { |
| 102 | return (OPENSSL_ia32cap_P[1] & (1 << (41 - 32))) != 0; |
| 103 | } |
| 104 | |
| 105 | #if defined(OPENSSL_X86_64) |
| 106 | #define BSAES |
| 107 | static char bsaes_capable(void) { |
| 108 | return vpaes_capable(); |
| 109 | } |
| 110 | #endif |
| 111 | |
| 112 | #elif !defined(OPENSSL_NO_ASM) && \ |
| 113 | (defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 114 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 115 | #if defined(OPENSSL_ARM) && __ARM_MAX_ARCH__ >= 7 |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 116 | #define BSAES |
| 117 | static char bsaes_capable(void) { |
| 118 | return CRYPTO_is_NEON_capable(); |
| 119 | } |
| 120 | #endif |
| 121 | |
| 122 | #define HWAES |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 123 | static int hwaes_capable(void) { |
| 124 | return CRYPTO_is_ARMv8_AES_capable(); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 127 | #elif !defined(OPENSSL_NO_ASM) && defined(OPENSSL_PPC64LE) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 128 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 129 | #define HWAES |
| 130 | static int hwaes_capable(void) { |
| 131 | return CRYPTO_is_PPC64LE_vcrypto_capable(); |
| 132 | } |
| 133 | |
| 134 | #endif /* OPENSSL_PPC64LE */ |
| 135 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 136 | |
| 137 | #if defined(BSAES) |
| 138 | /* On platforms where BSAES gets defined (just above), then these functions are |
| 139 | * provided by asm. */ |
| 140 | void bsaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 141 | const AES_KEY *key, uint8_t ivec[16], int enc); |
| 142 | void bsaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t len, |
| 143 | const AES_KEY *key, const uint8_t ivec[16]); |
| 144 | #else |
| 145 | static char bsaes_capable(void) { |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | /* On other platforms, bsaes_capable() will always return false and so the |
| 150 | * following will never be called. */ |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 151 | static void bsaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 152 | const AES_KEY *key, uint8_t ivec[16], int enc) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 153 | abort(); |
| 154 | } |
| 155 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 156 | static void bsaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, |
| 157 | size_t len, const AES_KEY *key, |
| 158 | const uint8_t ivec[16]) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 159 | abort(); |
| 160 | } |
| 161 | #endif |
| 162 | |
| 163 | #if defined(VPAES) |
| 164 | /* On platforms where VPAES gets defined (just above), then these functions are |
| 165 | * provided by asm. */ |
| 166 | int vpaes_set_encrypt_key(const uint8_t *userKey, int bits, AES_KEY *key); |
| 167 | int vpaes_set_decrypt_key(const uint8_t *userKey, int bits, AES_KEY *key); |
| 168 | |
| 169 | void vpaes_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); |
| 170 | void vpaes_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); |
| 171 | |
| 172 | void vpaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 173 | const AES_KEY *key, uint8_t *ivec, int enc); |
| 174 | #else |
| 175 | static char vpaes_capable(void) { |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | /* On other platforms, vpaes_capable() will always return false and so the |
| 180 | * following will never be called. */ |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 181 | static int vpaes_set_encrypt_key(const uint8_t *userKey, int bits, |
| 182 | AES_KEY *key) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 183 | abort(); |
| 184 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 185 | static int vpaes_set_decrypt_key(const uint8_t *userKey, int bits, |
| 186 | AES_KEY *key) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 187 | abort(); |
| 188 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 189 | static void vpaes_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 190 | abort(); |
| 191 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 192 | static void vpaes_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 193 | abort(); |
| 194 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 195 | static void vpaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 196 | const AES_KEY *key, uint8_t *ivec, int enc) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 197 | abort(); |
| 198 | } |
| 199 | #endif |
| 200 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 201 | #if defined(HWAES) |
| 202 | int aes_hw_set_encrypt_key(const uint8_t *user_key, const int bits, |
| 203 | AES_KEY *key); |
| 204 | int aes_hw_set_decrypt_key(const uint8_t *user_key, const int bits, |
| 205 | AES_KEY *key); |
| 206 | void aes_hw_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); |
| 207 | void aes_hw_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); |
| 208 | void aes_hw_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 209 | const AES_KEY *key, uint8_t *ivec, const int enc); |
| 210 | void aes_hw_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t len, |
| 211 | const AES_KEY *key, const uint8_t ivec[16]); |
| 212 | #else |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 213 | /* If HWAES isn't defined then we provide dummy functions for each of the hwaes |
| 214 | * functions. */ |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 215 | static int hwaes_capable(void) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 219 | static int aes_hw_set_encrypt_key(const uint8_t *user_key, int bits, |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 220 | AES_KEY *key) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 221 | abort(); |
| 222 | } |
| 223 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 224 | static int aes_hw_set_decrypt_key(const uint8_t *user_key, int bits, |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 225 | AES_KEY *key) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 226 | abort(); |
| 227 | } |
| 228 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 229 | static void aes_hw_encrypt(const uint8_t *in, uint8_t *out, |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 230 | const AES_KEY *key) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 231 | abort(); |
| 232 | } |
| 233 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 234 | static void aes_hw_decrypt(const uint8_t *in, uint8_t *out, |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 235 | const AES_KEY *key) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 236 | abort(); |
| 237 | } |
| 238 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 239 | static void aes_hw_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 240 | const AES_KEY *key, uint8_t *ivec, int enc) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 241 | abort(); |
| 242 | } |
| 243 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 244 | static void aes_hw_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 245 | size_t len, const AES_KEY *key, |
| 246 | const uint8_t ivec[16]) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 247 | abort(); |
| 248 | } |
| 249 | #endif |
| 250 | |
| 251 | #if !defined(OPENSSL_NO_ASM) && \ |
| 252 | (defined(OPENSSL_X86_64) || defined(OPENSSL_X86)) |
| 253 | int aesni_set_encrypt_key(const uint8_t *userKey, int bits, AES_KEY *key); |
| 254 | int aesni_set_decrypt_key(const uint8_t *userKey, int bits, AES_KEY *key); |
| 255 | |
| 256 | void aesni_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); |
| 257 | void aesni_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); |
| 258 | |
| 259 | void aesni_ecb_encrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 260 | const AES_KEY *key, int enc); |
| 261 | void aesni_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 262 | const AES_KEY *key, uint8_t *ivec, int enc); |
| 263 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 264 | #else |
| 265 | |
| 266 | /* On other platforms, aesni_capable() will always return false and so the |
| 267 | * following will never be called. */ |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 268 | static void aesni_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 269 | abort(); |
| 270 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 271 | static int aesni_set_encrypt_key(const uint8_t *userKey, int bits, |
| 272 | AES_KEY *key) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 273 | abort(); |
| 274 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 275 | static void aesni_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, |
| 276 | size_t blocks, const void *key, |
| 277 | const uint8_t *ivec) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 278 | abort(); |
| 279 | } |
| 280 | |
| 281 | #endif |
| 282 | |
| 283 | static int aes_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 284 | const uint8_t *iv, int enc) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 285 | int ret, mode; |
| 286 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
| 287 | |
| 288 | mode = ctx->cipher->flags & EVP_CIPH_MODE_MASK; |
| 289 | if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && !enc) { |
| 290 | if (hwaes_capable()) { |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 291 | ret = aes_hw_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks); |
| 292 | dat->block = (block128_f)aes_hw_decrypt; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 293 | dat->stream.cbc = NULL; |
| 294 | if (mode == EVP_CIPH_CBC_MODE) { |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 295 | dat->stream.cbc = (cbc128_f)aes_hw_cbc_encrypt; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 296 | } |
| 297 | } else if (bsaes_capable() && mode == EVP_CIPH_CBC_MODE) { |
| 298 | ret = AES_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks); |
| 299 | dat->block = (block128_f)AES_decrypt; |
| 300 | dat->stream.cbc = (cbc128_f)bsaes_cbc_encrypt; |
| 301 | } else if (vpaes_capable()) { |
| 302 | ret = vpaes_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks); |
| 303 | dat->block = (block128_f)vpaes_decrypt; |
| 304 | dat->stream.cbc = |
| 305 | mode == EVP_CIPH_CBC_MODE ? (cbc128_f)vpaes_cbc_encrypt : NULL; |
| 306 | } else { |
| 307 | ret = AES_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks); |
| 308 | dat->block = (block128_f)AES_decrypt; |
| 309 | dat->stream.cbc = |
| 310 | mode == EVP_CIPH_CBC_MODE ? (cbc128_f)AES_cbc_encrypt : NULL; |
| 311 | } |
| 312 | } else if (hwaes_capable()) { |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 313 | ret = aes_hw_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks); |
| 314 | dat->block = (block128_f)aes_hw_encrypt; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 315 | dat->stream.cbc = NULL; |
| 316 | if (mode == EVP_CIPH_CBC_MODE) { |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 317 | dat->stream.cbc = (cbc128_f)aes_hw_cbc_encrypt; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 318 | } else if (mode == EVP_CIPH_CTR_MODE) { |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 319 | dat->stream.ctr = (ctr128_f)aes_hw_ctr32_encrypt_blocks; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 320 | } |
| 321 | } else if (bsaes_capable() && mode == EVP_CIPH_CTR_MODE) { |
| 322 | ret = AES_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks); |
| 323 | dat->block = (block128_f)AES_encrypt; |
| 324 | dat->stream.ctr = (ctr128_f)bsaes_ctr32_encrypt_blocks; |
| 325 | } else if (vpaes_capable()) { |
| 326 | ret = vpaes_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks); |
| 327 | dat->block = (block128_f)vpaes_encrypt; |
| 328 | dat->stream.cbc = |
| 329 | mode == EVP_CIPH_CBC_MODE ? (cbc128_f)vpaes_cbc_encrypt : NULL; |
| 330 | } else { |
| 331 | ret = AES_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks); |
| 332 | dat->block = (block128_f)AES_encrypt; |
| 333 | dat->stream.cbc = |
| 334 | mode == EVP_CIPH_CBC_MODE ? (cbc128_f)AES_cbc_encrypt : NULL; |
| 335 | } |
| 336 | |
| 337 | if (ret < 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 338 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_AES_KEY_SETUP_FAILED); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | return 1; |
| 343 | } |
| 344 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 345 | static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, |
| 346 | size_t len) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 347 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
| 348 | |
| 349 | if (dat->stream.cbc) { |
| 350 | (*dat->stream.cbc)(in, out, len, &dat->ks, ctx->iv, ctx->encrypt); |
| 351 | } else if (ctx->encrypt) { |
| 352 | CRYPTO_cbc128_encrypt(in, out, len, &dat->ks, ctx->iv, dat->block); |
| 353 | } else { |
| 354 | CRYPTO_cbc128_decrypt(in, out, len, &dat->ks, ctx->iv, dat->block); |
| 355 | } |
| 356 | |
| 357 | return 1; |
| 358 | } |
| 359 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 360 | static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, |
| 361 | size_t len) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 362 | size_t bl = ctx->cipher->block_size; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 363 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
| 364 | |
| 365 | if (len < bl) { |
| 366 | return 1; |
| 367 | } |
| 368 | |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 369 | len -= bl; |
| 370 | for (size_t i = 0; i <= len; i += bl) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 371 | (*dat->block)(in + i, out + i, &dat->ks); |
| 372 | } |
| 373 | |
| 374 | return 1; |
| 375 | } |
| 376 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 377 | static int aes_ctr_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, |
| 378 | size_t len) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 379 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
| 380 | |
| 381 | if (dat->stream.ctr) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 382 | CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks, ctx->iv, ctx->buf, |
| 383 | &ctx->num, dat->stream.ctr); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 384 | } else { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 385 | CRYPTO_ctr128_encrypt(in, out, len, &dat->ks, ctx->iv, ctx->buf, &ctx->num, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 386 | dat->block); |
| 387 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 388 | return 1; |
| 389 | } |
| 390 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 391 | static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, |
| 392 | size_t len) { |
| 393 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
| 394 | |
| 395 | CRYPTO_ofb128_encrypt(in, out, len, &dat->ks, ctx->iv, &ctx->num, dat->block); |
| 396 | return 1; |
| 397 | } |
| 398 | |
| 399 | static char aesni_capable(void); |
| 400 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 401 | ctr128_f aes_ctr_set_key(AES_KEY *aes_key, GCM128_CONTEXT *gcm_ctx, |
| 402 | block128_f *out_block, const uint8_t *key, |
| 403 | size_t key_bytes) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 404 | if (aesni_capable()) { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 405 | aesni_set_encrypt_key(key, key_bytes * 8, aes_key); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 406 | if (gcm_ctx != NULL) { |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame^] | 407 | CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)aesni_encrypt, 1); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 408 | } |
| 409 | if (out_block) { |
| 410 | *out_block = (block128_f) aesni_encrypt; |
| 411 | } |
| 412 | return (ctr128_f)aesni_ctr32_encrypt_blocks; |
| 413 | } |
| 414 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 415 | if (hwaes_capable()) { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 416 | aes_hw_set_encrypt_key(key, key_bytes * 8, aes_key); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 417 | if (gcm_ctx != NULL) { |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame^] | 418 | CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)aes_hw_encrypt, 0); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 419 | } |
| 420 | if (out_block) { |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 421 | *out_block = (block128_f) aes_hw_encrypt; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 422 | } |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 423 | return (ctr128_f)aes_hw_ctr32_encrypt_blocks; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | if (bsaes_capable()) { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 427 | AES_set_encrypt_key(key, key_bytes * 8, aes_key); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 428 | if (gcm_ctx != NULL) { |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame^] | 429 | CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt, 0); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 430 | } |
| 431 | if (out_block) { |
| 432 | *out_block = (block128_f) AES_encrypt; |
| 433 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 434 | return (ctr128_f)bsaes_ctr32_encrypt_blocks; |
| 435 | } |
| 436 | |
| 437 | if (vpaes_capable()) { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 438 | vpaes_set_encrypt_key(key, key_bytes * 8, aes_key); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 439 | if (out_block) { |
| 440 | *out_block = (block128_f) vpaes_encrypt; |
| 441 | } |
| 442 | if (gcm_ctx != NULL) { |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame^] | 443 | CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)vpaes_encrypt, 0); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 444 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 445 | return NULL; |
| 446 | } |
| 447 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 448 | AES_set_encrypt_key(key, key_bytes * 8, aes_key); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 449 | if (gcm_ctx != NULL) { |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame^] | 450 | CRYPTO_gcm128_init(gcm_ctx, aes_key, (block128_f)AES_encrypt, 0); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 451 | } |
| 452 | if (out_block) { |
| 453 | *out_block = (block128_f) AES_encrypt; |
| 454 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 455 | return NULL; |
| 456 | } |
| 457 | |
| 458 | static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, |
| 459 | const uint8_t *iv, int enc) { |
| 460 | EVP_AES_GCM_CTX *gctx = ctx->cipher_data; |
| 461 | if (!iv && !key) { |
| 462 | return 1; |
| 463 | } |
| 464 | if (key) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 465 | gctx->ctr = |
| 466 | aes_ctr_set_key(&gctx->ks.ks, &gctx->gcm, NULL, key, ctx->key_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 467 | /* If we have an iv can set it directly, otherwise use saved IV. */ |
| 468 | if (iv == NULL && gctx->iv_set) { |
| 469 | iv = gctx->iv; |
| 470 | } |
| 471 | if (iv) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 472 | CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, iv, gctx->ivlen); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 473 | gctx->iv_set = 1; |
| 474 | } |
| 475 | gctx->key_set = 1; |
| 476 | } else { |
| 477 | /* If key set use IV, otherwise copy */ |
| 478 | if (gctx->key_set) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 479 | CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, iv, gctx->ivlen); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 480 | } else { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 481 | OPENSSL_memcpy(gctx->iv, iv, gctx->ivlen); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 482 | } |
| 483 | gctx->iv_set = 1; |
| 484 | gctx->iv_gen = 0; |
| 485 | } |
| 486 | return 1; |
| 487 | } |
| 488 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 489 | static void aes_gcm_cleanup(EVP_CIPHER_CTX *c) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 490 | EVP_AES_GCM_CTX *gctx = c->cipher_data; |
| 491 | OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm)); |
| 492 | if (gctx->iv != c->iv) { |
| 493 | OPENSSL_free(gctx->iv); |
| 494 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /* increment counter (64-bit int) by 1 */ |
| 498 | static void ctr64_inc(uint8_t *counter) { |
| 499 | int n = 8; |
| 500 | uint8_t c; |
| 501 | |
| 502 | do { |
| 503 | --n; |
| 504 | c = counter[n]; |
| 505 | ++c; |
| 506 | counter[n] = c; |
| 507 | if (c) { |
| 508 | return; |
| 509 | } |
| 510 | } while (n); |
| 511 | } |
| 512 | |
| 513 | static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) { |
| 514 | EVP_AES_GCM_CTX *gctx = c->cipher_data; |
| 515 | switch (type) { |
| 516 | case EVP_CTRL_INIT: |
| 517 | gctx->key_set = 0; |
| 518 | gctx->iv_set = 0; |
| 519 | gctx->ivlen = c->cipher->iv_len; |
| 520 | gctx->iv = c->iv; |
| 521 | gctx->taglen = -1; |
| 522 | gctx->iv_gen = 0; |
| 523 | return 1; |
| 524 | |
| 525 | case EVP_CTRL_GCM_SET_IVLEN: |
| 526 | if (arg <= 0) { |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | /* Allocate memory for IV if needed */ |
| 531 | if (arg > EVP_MAX_IV_LENGTH && arg > gctx->ivlen) { |
| 532 | if (gctx->iv != c->iv) { |
| 533 | OPENSSL_free(gctx->iv); |
| 534 | } |
| 535 | gctx->iv = OPENSSL_malloc(arg); |
| 536 | if (!gctx->iv) { |
| 537 | return 0; |
| 538 | } |
| 539 | } |
| 540 | gctx->ivlen = arg; |
| 541 | return 1; |
| 542 | |
| 543 | case EVP_CTRL_GCM_SET_TAG: |
| 544 | if (arg <= 0 || arg > 16 || c->encrypt) { |
| 545 | return 0; |
| 546 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 547 | OPENSSL_memcpy(c->buf, ptr, arg); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 548 | gctx->taglen = arg; |
| 549 | return 1; |
| 550 | |
| 551 | case EVP_CTRL_GCM_GET_TAG: |
| 552 | if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0) { |
| 553 | return 0; |
| 554 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 555 | OPENSSL_memcpy(ptr, c->buf, arg); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 556 | return 1; |
| 557 | |
| 558 | case EVP_CTRL_GCM_SET_IV_FIXED: |
| 559 | /* Special case: -1 length restores whole IV */ |
| 560 | if (arg == -1) { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 561 | OPENSSL_memcpy(gctx->iv, ptr, gctx->ivlen); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 562 | gctx->iv_gen = 1; |
| 563 | return 1; |
| 564 | } |
| 565 | /* Fixed field must be at least 4 bytes and invocation field |
| 566 | * at least 8. */ |
| 567 | if (arg < 4 || (gctx->ivlen - arg) < 8) { |
| 568 | return 0; |
| 569 | } |
| 570 | if (arg) { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 571 | OPENSSL_memcpy(gctx->iv, ptr, arg); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 572 | } |
| 573 | if (c->encrypt && !RAND_bytes(gctx->iv + arg, gctx->ivlen - arg)) { |
| 574 | return 0; |
| 575 | } |
| 576 | gctx->iv_gen = 1; |
| 577 | return 1; |
| 578 | |
| 579 | case EVP_CTRL_GCM_IV_GEN: |
| 580 | if (gctx->iv_gen == 0 || gctx->key_set == 0) { |
| 581 | return 0; |
| 582 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 583 | CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, gctx->iv, gctx->ivlen); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 584 | if (arg <= 0 || arg > gctx->ivlen) { |
| 585 | arg = gctx->ivlen; |
| 586 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 587 | OPENSSL_memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 588 | /* Invocation field will be at least 8 bytes in size and |
| 589 | * so no need to check wrap around or increment more than |
| 590 | * last 8 bytes. */ |
| 591 | ctr64_inc(gctx->iv + gctx->ivlen - 8); |
| 592 | gctx->iv_set = 1; |
| 593 | return 1; |
| 594 | |
| 595 | case EVP_CTRL_GCM_SET_IV_INV: |
| 596 | if (gctx->iv_gen == 0 || gctx->key_set == 0 || c->encrypt) { |
| 597 | return 0; |
| 598 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 599 | OPENSSL_memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg); |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 600 | CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, gctx->iv, gctx->ivlen); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 601 | gctx->iv_set = 1; |
| 602 | return 1; |
| 603 | |
| 604 | case EVP_CTRL_COPY: { |
| 605 | EVP_CIPHER_CTX *out = ptr; |
| 606 | EVP_AES_GCM_CTX *gctx_out = out->cipher_data; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 607 | if (gctx->iv == c->iv) { |
| 608 | gctx_out->iv = out->iv; |
| 609 | } else { |
| 610 | gctx_out->iv = OPENSSL_malloc(gctx->ivlen); |
| 611 | if (!gctx_out->iv) { |
| 612 | return 0; |
| 613 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 614 | OPENSSL_memcpy(gctx_out->iv, gctx->iv, gctx->ivlen); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 615 | } |
| 616 | return 1; |
| 617 | } |
| 618 | |
| 619 | default: |
| 620 | return -1; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, |
| 625 | size_t len) { |
| 626 | EVP_AES_GCM_CTX *gctx = ctx->cipher_data; |
| 627 | |
| 628 | /* If not set up, return error */ |
| 629 | if (!gctx->key_set) { |
| 630 | return -1; |
| 631 | } |
| 632 | if (!gctx->iv_set) { |
| 633 | return -1; |
| 634 | } |
| 635 | |
| 636 | if (in) { |
| 637 | if (out == NULL) { |
| 638 | if (!CRYPTO_gcm128_aad(&gctx->gcm, in, len)) { |
| 639 | return -1; |
| 640 | } |
| 641 | } else if (ctx->encrypt) { |
| 642 | if (gctx->ctr) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 643 | if (!CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm, &gctx->ks.ks, in, out, len, |
| 644 | gctx->ctr)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 645 | return -1; |
| 646 | } |
| 647 | } else { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 648 | if (!CRYPTO_gcm128_encrypt(&gctx->gcm, &gctx->ks.ks, in, out, len)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 649 | return -1; |
| 650 | } |
| 651 | } |
| 652 | } else { |
| 653 | if (gctx->ctr) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 654 | if (!CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm, &gctx->ks.ks, in, out, len, |
| 655 | gctx->ctr)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 656 | return -1; |
| 657 | } |
| 658 | } else { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 659 | if (!CRYPTO_gcm128_decrypt(&gctx->gcm, &gctx->ks.ks, in, out, len)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 660 | return -1; |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | return len; |
| 665 | } else { |
| 666 | if (!ctx->encrypt) { |
| 667 | if (gctx->taglen < 0 || |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 668 | !CRYPTO_gcm128_finish(&gctx->gcm, ctx->buf, gctx->taglen)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 669 | return -1; |
| 670 | } |
| 671 | gctx->iv_set = 0; |
| 672 | return 0; |
| 673 | } |
| 674 | CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, 16); |
| 675 | gctx->taglen = 16; |
| 676 | /* Don't reuse the IV */ |
| 677 | gctx->iv_set = 0; |
| 678 | return 0; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | static const EVP_CIPHER aes_128_cbc = { |
| 683 | NID_aes_128_cbc, 16 /* block_size */, 16 /* key_size */, |
| 684 | 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CBC_MODE, |
| 685 | NULL /* app_data */, aes_init_key, aes_cbc_cipher, |
| 686 | NULL /* cleanup */, NULL /* ctrl */}; |
| 687 | |
| 688 | static const EVP_CIPHER aes_128_ctr = { |
| 689 | NID_aes_128_ctr, 1 /* block_size */, 16 /* key_size */, |
| 690 | 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CTR_MODE, |
| 691 | NULL /* app_data */, aes_init_key, aes_ctr_cipher, |
| 692 | NULL /* cleanup */, NULL /* ctrl */}; |
| 693 | |
| 694 | static const EVP_CIPHER aes_128_ecb = { |
| 695 | NID_aes_128_ecb, 16 /* block_size */, 16 /* key_size */, |
| 696 | 0 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_ECB_MODE, |
| 697 | NULL /* app_data */, aes_init_key, aes_ecb_cipher, |
| 698 | NULL /* cleanup */, NULL /* ctrl */}; |
| 699 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 700 | static const EVP_CIPHER aes_128_ofb = { |
| 701 | NID_aes_128_ofb128, 1 /* block_size */, 16 /* key_size */, |
| 702 | 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_OFB_MODE, |
| 703 | NULL /* app_data */, aes_init_key, aes_ofb_cipher, |
| 704 | NULL /* cleanup */, NULL /* ctrl */}; |
| 705 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 706 | static const EVP_CIPHER aes_128_gcm = { |
| 707 | NID_aes_128_gcm, 1 /* block_size */, 16 /* key_size */, 12 /* iv_len */, |
| 708 | sizeof(EVP_AES_GCM_CTX), |
| 709 | EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER | |
| 710 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT | |
| 711 | EVP_CIPH_FLAG_AEAD_CIPHER, |
| 712 | NULL /* app_data */, aes_gcm_init_key, aes_gcm_cipher, aes_gcm_cleanup, |
| 713 | aes_gcm_ctrl}; |
| 714 | |
| 715 | |
Adam Langley | 13d393e | 2015-04-08 11:18:53 -0700 | [diff] [blame] | 716 | static const EVP_CIPHER aes_192_cbc = { |
| 717 | NID_aes_192_cbc, 16 /* block_size */, 24 /* key_size */, |
| 718 | 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CBC_MODE, |
| 719 | NULL /* app_data */, aes_init_key, aes_cbc_cipher, |
| 720 | NULL /* cleanup */, NULL /* ctrl */}; |
| 721 | |
| 722 | static const EVP_CIPHER aes_192_ctr = { |
| 723 | NID_aes_192_ctr, 1 /* block_size */, 24 /* key_size */, |
| 724 | 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CTR_MODE, |
| 725 | NULL /* app_data */, aes_init_key, aes_ctr_cipher, |
| 726 | NULL /* cleanup */, NULL /* ctrl */}; |
| 727 | |
| 728 | static const EVP_CIPHER aes_192_ecb = { |
| 729 | NID_aes_192_ecb, 16 /* block_size */, 24 /* key_size */, |
| 730 | 0 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_ECB_MODE, |
| 731 | NULL /* app_data */, aes_init_key, aes_ecb_cipher, |
| 732 | NULL /* cleanup */, NULL /* ctrl */}; |
| 733 | |
| 734 | static const EVP_CIPHER aes_192_gcm = { |
| 735 | NID_aes_192_gcm, 1 /* block_size */, 24 /* key_size */, 12 /* iv_len */, |
| 736 | sizeof(EVP_AES_GCM_CTX), |
| 737 | EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER | |
| 738 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT | |
| 739 | EVP_CIPH_FLAG_AEAD_CIPHER, |
| 740 | NULL /* app_data */, aes_gcm_init_key, aes_gcm_cipher, aes_gcm_cleanup, |
| 741 | aes_gcm_ctrl}; |
| 742 | |
| 743 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 744 | static const EVP_CIPHER aes_256_cbc = { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 745 | NID_aes_256_cbc, 16 /* block_size */, 32 /* key_size */, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 746 | 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CBC_MODE, |
| 747 | NULL /* app_data */, aes_init_key, aes_cbc_cipher, |
| 748 | NULL /* cleanup */, NULL /* ctrl */}; |
| 749 | |
| 750 | static const EVP_CIPHER aes_256_ctr = { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 751 | NID_aes_256_ctr, 1 /* block_size */, 32 /* key_size */, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 752 | 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CTR_MODE, |
| 753 | NULL /* app_data */, aes_init_key, aes_ctr_cipher, |
| 754 | NULL /* cleanup */, NULL /* ctrl */}; |
| 755 | |
| 756 | static const EVP_CIPHER aes_256_ecb = { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 757 | NID_aes_256_ecb, 16 /* block_size */, 32 /* key_size */, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 758 | 0 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_ECB_MODE, |
| 759 | NULL /* app_data */, aes_init_key, aes_ecb_cipher, |
| 760 | NULL /* cleanup */, NULL /* ctrl */}; |
| 761 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 762 | static const EVP_CIPHER aes_256_ofb = { |
| 763 | NID_aes_256_ofb128, 1 /* block_size */, 32 /* key_size */, |
| 764 | 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_OFB_MODE, |
| 765 | NULL /* app_data */, aes_init_key, aes_ofb_cipher, |
| 766 | NULL /* cleanup */, NULL /* ctrl */}; |
| 767 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 768 | static const EVP_CIPHER aes_256_gcm = { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 769 | NID_aes_256_gcm, 1 /* block_size */, 32 /* key_size */, 12 /* iv_len */, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 770 | sizeof(EVP_AES_GCM_CTX), |
| 771 | EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER | |
| 772 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT | |
| 773 | EVP_CIPH_FLAG_AEAD_CIPHER, |
| 774 | NULL /* app_data */, aes_gcm_init_key, aes_gcm_cipher, aes_gcm_cleanup, |
| 775 | aes_gcm_ctrl}; |
| 776 | |
| 777 | #if !defined(OPENSSL_NO_ASM) && \ |
| 778 | (defined(OPENSSL_X86_64) || defined(OPENSSL_X86)) |
| 779 | |
| 780 | /* AES-NI section. */ |
| 781 | |
| 782 | static char aesni_capable(void) { |
| 783 | return (OPENSSL_ia32cap_P[1] & (1 << (57 - 32))) != 0; |
| 784 | } |
| 785 | |
| 786 | static int aesni_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, |
| 787 | const uint8_t *iv, int enc) { |
| 788 | int ret, mode; |
| 789 | EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data; |
| 790 | |
| 791 | mode = ctx->cipher->flags & EVP_CIPH_MODE_MASK; |
| 792 | if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && !enc) { |
| 793 | ret = aesni_set_decrypt_key(key, ctx->key_len * 8, ctx->cipher_data); |
| 794 | dat->block = (block128_f)aesni_decrypt; |
| 795 | dat->stream.cbc = |
| 796 | mode == EVP_CIPH_CBC_MODE ? (cbc128_f)aesni_cbc_encrypt : NULL; |
| 797 | } else { |
| 798 | ret = aesni_set_encrypt_key(key, ctx->key_len * 8, ctx->cipher_data); |
| 799 | dat->block = (block128_f)aesni_encrypt; |
| 800 | if (mode == EVP_CIPH_CBC_MODE) { |
| 801 | dat->stream.cbc = (cbc128_f)aesni_cbc_encrypt; |
| 802 | } else if (mode == EVP_CIPH_CTR_MODE) { |
| 803 | dat->stream.ctr = (ctr128_f)aesni_ctr32_encrypt_blocks; |
| 804 | } else { |
| 805 | dat->stream.cbc = NULL; |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | if (ret < 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 810 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_AES_KEY_SETUP_FAILED); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 811 | return 0; |
| 812 | } |
| 813 | |
| 814 | return 1; |
| 815 | } |
| 816 | |
| 817 | static int aesni_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, |
| 818 | const uint8_t *in, size_t len) { |
| 819 | aesni_cbc_encrypt(in, out, len, ctx->cipher_data, ctx->iv, ctx->encrypt); |
| 820 | |
| 821 | return 1; |
| 822 | } |
| 823 | |
| 824 | static int aesni_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, |
| 825 | const uint8_t *in, size_t len) { |
| 826 | size_t bl = ctx->cipher->block_size; |
| 827 | |
| 828 | if (len < bl) { |
| 829 | return 1; |
| 830 | } |
| 831 | |
| 832 | aesni_ecb_encrypt(in, out, len, ctx->cipher_data, ctx->encrypt); |
| 833 | |
| 834 | return 1; |
| 835 | } |
| 836 | |
| 837 | static int aesni_gcm_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, |
| 838 | const uint8_t *iv, int enc) { |
| 839 | EVP_AES_GCM_CTX *gctx = ctx->cipher_data; |
| 840 | if (!iv && !key) { |
| 841 | return 1; |
| 842 | } |
| 843 | if (key) { |
| 844 | aesni_set_encrypt_key(key, ctx->key_len * 8, &gctx->ks.ks); |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame^] | 845 | CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks, (block128_f)aesni_encrypt, 1); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 846 | gctx->ctr = (ctr128_f)aesni_ctr32_encrypt_blocks; |
| 847 | /* If we have an iv can set it directly, otherwise use |
| 848 | * saved IV. */ |
| 849 | if (iv == NULL && gctx->iv_set) { |
| 850 | iv = gctx->iv; |
| 851 | } |
| 852 | if (iv) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 853 | CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, iv, gctx->ivlen); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 854 | gctx->iv_set = 1; |
| 855 | } |
| 856 | gctx->key_set = 1; |
| 857 | } else { |
| 858 | /* If key set use IV, otherwise copy */ |
| 859 | if (gctx->key_set) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 860 | CRYPTO_gcm128_setiv(&gctx->gcm, &gctx->ks.ks, iv, gctx->ivlen); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 861 | } else { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 862 | OPENSSL_memcpy(gctx->iv, iv, gctx->ivlen); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 863 | } |
| 864 | gctx->iv_set = 1; |
| 865 | gctx->iv_gen = 0; |
| 866 | } |
| 867 | return 1; |
| 868 | } |
| 869 | |
| 870 | static const EVP_CIPHER aesni_128_cbc = { |
| 871 | NID_aes_128_cbc, 16 /* block_size */, 16 /* key_size */, |
| 872 | 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CBC_MODE, |
| 873 | NULL /* app_data */, aesni_init_key, aesni_cbc_cipher, |
| 874 | NULL /* cleanup */, NULL /* ctrl */}; |
| 875 | |
| 876 | static const EVP_CIPHER aesni_128_ctr = { |
| 877 | NID_aes_128_ctr, 1 /* block_size */, 16 /* key_size */, |
| 878 | 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CTR_MODE, |
| 879 | NULL /* app_data */, aesni_init_key, aes_ctr_cipher, |
| 880 | NULL /* cleanup */, NULL /* ctrl */}; |
| 881 | |
| 882 | static const EVP_CIPHER aesni_128_ecb = { |
| 883 | NID_aes_128_ecb, 16 /* block_size */, 16 /* key_size */, |
| 884 | 0 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_ECB_MODE, |
| 885 | NULL /* app_data */, aesni_init_key, aesni_ecb_cipher, |
| 886 | NULL /* cleanup */, NULL /* ctrl */}; |
| 887 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 888 | static const EVP_CIPHER aesni_128_ofb = { |
| 889 | NID_aes_128_ofb128, 1 /* block_size */, 16 /* key_size */, |
| 890 | 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_OFB_MODE, |
| 891 | NULL /* app_data */, aesni_init_key, aes_ofb_cipher, |
| 892 | NULL /* cleanup */, NULL /* ctrl */}; |
| 893 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 894 | static const EVP_CIPHER aesni_128_gcm = { |
| 895 | NID_aes_128_gcm, 1 /* block_size */, 16 /* key_size */, 12 /* iv_len */, |
| 896 | sizeof(EVP_AES_GCM_CTX), |
| 897 | EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER | |
| 898 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT | |
| 899 | EVP_CIPH_FLAG_AEAD_CIPHER, |
| 900 | NULL /* app_data */, aesni_gcm_init_key, aes_gcm_cipher, aes_gcm_cleanup, |
| 901 | aes_gcm_ctrl}; |
| 902 | |
| 903 | |
Adam Langley | 13d393e | 2015-04-08 11:18:53 -0700 | [diff] [blame] | 904 | static const EVP_CIPHER aesni_192_cbc = { |
| 905 | NID_aes_192_cbc, 16 /* block_size */, 24 /* key_size */, |
| 906 | 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CBC_MODE, |
| 907 | NULL /* app_data */, aesni_init_key, aesni_cbc_cipher, |
| 908 | NULL /* cleanup */, NULL /* ctrl */}; |
| 909 | |
| 910 | static const EVP_CIPHER aesni_192_ctr = { |
| 911 | NID_aes_192_ctr, 1 /* block_size */, 24 /* key_size */, |
| 912 | 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CTR_MODE, |
| 913 | NULL /* app_data */, aesni_init_key, aes_ctr_cipher, |
| 914 | NULL /* cleanup */, NULL /* ctrl */}; |
| 915 | |
| 916 | static const EVP_CIPHER aesni_192_ecb = { |
| 917 | NID_aes_192_ecb, 16 /* block_size */, 24 /* key_size */, |
| 918 | 0 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_ECB_MODE, |
| 919 | NULL /* app_data */, aesni_init_key, aesni_ecb_cipher, |
| 920 | NULL /* cleanup */, NULL /* ctrl */}; |
| 921 | |
| 922 | static const EVP_CIPHER aesni_192_gcm = { |
| 923 | NID_aes_192_gcm, 1 /* block_size */, 24 /* key_size */, 12 /* iv_len */, |
| 924 | sizeof(EVP_AES_GCM_CTX), |
| 925 | EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER | |
| 926 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT | |
| 927 | EVP_CIPH_FLAG_AEAD_CIPHER, |
| 928 | NULL /* app_data */, aesni_gcm_init_key, aes_gcm_cipher, aes_gcm_cleanup, |
| 929 | aes_gcm_ctrl}; |
| 930 | |
| 931 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 932 | static const EVP_CIPHER aesni_256_cbc = { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 933 | NID_aes_256_cbc, 16 /* block_size */, 32 /* key_size */, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 934 | 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CBC_MODE, |
| 935 | NULL /* app_data */, aesni_init_key, aesni_cbc_cipher, |
| 936 | NULL /* cleanup */, NULL /* ctrl */}; |
| 937 | |
| 938 | static const EVP_CIPHER aesni_256_ctr = { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 939 | NID_aes_256_ctr, 1 /* block_size */, 32 /* key_size */, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 940 | 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_CTR_MODE, |
| 941 | NULL /* app_data */, aesni_init_key, aes_ctr_cipher, |
| 942 | NULL /* cleanup */, NULL /* ctrl */}; |
| 943 | |
| 944 | static const EVP_CIPHER aesni_256_ecb = { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 945 | NID_aes_256_ecb, 16 /* block_size */, 32 /* key_size */, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 946 | 0 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_ECB_MODE, |
| 947 | NULL /* app_data */, aesni_init_key, aesni_ecb_cipher, |
| 948 | NULL /* cleanup */, NULL /* ctrl */}; |
| 949 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 950 | static const EVP_CIPHER aesni_256_ofb = { |
| 951 | NID_aes_256_ofb128, 1 /* block_size */, 32 /* key_size */, |
| 952 | 16 /* iv_len */, sizeof(EVP_AES_KEY), EVP_CIPH_OFB_MODE, |
| 953 | NULL /* app_data */, aesni_init_key, aes_ofb_cipher, |
| 954 | NULL /* cleanup */, NULL /* ctrl */}; |
| 955 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 956 | static const EVP_CIPHER aesni_256_gcm = { |
| 957 | NID_aes_256_gcm, 1 /* block_size */, 32 /* key_size */, 12 /* iv_len */, |
| 958 | sizeof(EVP_AES_GCM_CTX), |
| 959 | EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER | |
| 960 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT | EVP_CIPH_CUSTOM_COPY | |
| 961 | EVP_CIPH_FLAG_AEAD_CIPHER, |
| 962 | NULL /* app_data */, aesni_gcm_init_key, aes_gcm_cipher, aes_gcm_cleanup, |
| 963 | aes_gcm_ctrl}; |
| 964 | |
| 965 | #define EVP_CIPHER_FUNCTION(keybits, mode) \ |
| 966 | const EVP_CIPHER *EVP_aes_##keybits##_##mode(void) { \ |
| 967 | if (aesni_capable()) { \ |
| 968 | return &aesni_##keybits##_##mode; \ |
| 969 | } else { \ |
| 970 | return &aes_##keybits##_##mode; \ |
| 971 | } \ |
| 972 | } |
| 973 | |
| 974 | #else /* ^^^ OPENSSL_X86_64 || OPENSSL_X86 */ |
| 975 | |
| 976 | static char aesni_capable(void) { |
| 977 | return 0; |
| 978 | } |
| 979 | |
| 980 | #define EVP_CIPHER_FUNCTION(keybits, mode) \ |
| 981 | const EVP_CIPHER *EVP_aes_##keybits##_##mode(void) { \ |
| 982 | return &aes_##keybits##_##mode; \ |
| 983 | } |
| 984 | |
| 985 | #endif |
| 986 | |
| 987 | EVP_CIPHER_FUNCTION(128, cbc) |
| 988 | EVP_CIPHER_FUNCTION(128, ctr) |
| 989 | EVP_CIPHER_FUNCTION(128, ecb) |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 990 | EVP_CIPHER_FUNCTION(128, ofb) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 991 | EVP_CIPHER_FUNCTION(128, gcm) |
| 992 | |
Adam Langley | 13d393e | 2015-04-08 11:18:53 -0700 | [diff] [blame] | 993 | EVP_CIPHER_FUNCTION(192, cbc) |
| 994 | EVP_CIPHER_FUNCTION(192, ctr) |
| 995 | EVP_CIPHER_FUNCTION(192, ecb) |
| 996 | EVP_CIPHER_FUNCTION(192, gcm) |
| 997 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 998 | EVP_CIPHER_FUNCTION(256, cbc) |
| 999 | EVP_CIPHER_FUNCTION(256, ctr) |
| 1000 | EVP_CIPHER_FUNCTION(256, ecb) |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1001 | EVP_CIPHER_FUNCTION(256, ofb) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1002 | EVP_CIPHER_FUNCTION(256, gcm) |
| 1003 | |
| 1004 | |
| 1005 | #define EVP_AEAD_AES_GCM_TAG_LEN 16 |
| 1006 | |
| 1007 | struct aead_aes_gcm_ctx { |
| 1008 | union { |
| 1009 | double align; |
| 1010 | AES_KEY ks; |
| 1011 | } ks; |
| 1012 | GCM128_CONTEXT gcm; |
| 1013 | ctr128_f ctr; |
| 1014 | uint8_t tag_len; |
| 1015 | }; |
| 1016 | |
| 1017 | static int aead_aes_gcm_init(EVP_AEAD_CTX *ctx, const uint8_t *key, |
| 1018 | size_t key_len, size_t tag_len) { |
| 1019 | struct aead_aes_gcm_ctx *gcm_ctx; |
| 1020 | const size_t key_bits = key_len * 8; |
| 1021 | |
| 1022 | if (key_bits != 128 && key_bits != 256) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1023 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1024 | return 0; /* EVP_AEAD_CTX_init should catch this. */ |
| 1025 | } |
| 1026 | |
| 1027 | if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) { |
| 1028 | tag_len = EVP_AEAD_AES_GCM_TAG_LEN; |
| 1029 | } |
| 1030 | |
| 1031 | if (tag_len > EVP_AEAD_AES_GCM_TAG_LEN) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1032 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TAG_TOO_LARGE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1033 | return 0; |
| 1034 | } |
| 1035 | |
| 1036 | gcm_ctx = OPENSSL_malloc(sizeof(struct aead_aes_gcm_ctx)); |
| 1037 | if (gcm_ctx == NULL) { |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1041 | gcm_ctx->ctr = |
| 1042 | aes_ctr_set_key(&gcm_ctx->ks.ks, &gcm_ctx->gcm, NULL, key, key_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1043 | gcm_ctx->tag_len = tag_len; |
| 1044 | ctx->aead_state = gcm_ctx; |
| 1045 | |
| 1046 | return 1; |
| 1047 | } |
| 1048 | |
| 1049 | static void aead_aes_gcm_cleanup(EVP_AEAD_CTX *ctx) { |
| 1050 | struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state; |
| 1051 | OPENSSL_cleanse(gcm_ctx, sizeof(struct aead_aes_gcm_ctx)); |
| 1052 | OPENSSL_free(gcm_ctx); |
| 1053 | } |
| 1054 | |
| 1055 | static int aead_aes_gcm_seal(const EVP_AEAD_CTX *ctx, uint8_t *out, |
| 1056 | size_t *out_len, size_t max_out_len, |
| 1057 | const uint8_t *nonce, size_t nonce_len, |
| 1058 | const uint8_t *in, size_t in_len, |
| 1059 | const uint8_t *ad, size_t ad_len) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1060 | const struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state; |
| 1061 | GCM128_CONTEXT gcm; |
| 1062 | |
| 1063 | if (in_len + gcm_ctx->tag_len < in_len) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1064 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1065 | return 0; |
| 1066 | } |
| 1067 | |
| 1068 | if (max_out_len < in_len + gcm_ctx->tag_len) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1069 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1070 | return 0; |
| 1071 | } |
| 1072 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1073 | const AES_KEY *key = &gcm_ctx->ks.ks; |
| 1074 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 1075 | OPENSSL_memcpy(&gcm, &gcm_ctx->gcm, sizeof(gcm)); |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1076 | CRYPTO_gcm128_setiv(&gcm, key, nonce, nonce_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1077 | |
| 1078 | if (ad_len > 0 && !CRYPTO_gcm128_aad(&gcm, ad, ad_len)) { |
| 1079 | return 0; |
| 1080 | } |
| 1081 | |
| 1082 | if (gcm_ctx->ctr) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1083 | if (!CRYPTO_gcm128_encrypt_ctr32(&gcm, key, in, out, in_len, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1084 | gcm_ctx->ctr)) { |
| 1085 | return 0; |
| 1086 | } |
| 1087 | } else { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1088 | if (!CRYPTO_gcm128_encrypt(&gcm, key, in, out, in_len)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1089 | return 0; |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | CRYPTO_gcm128_tag(&gcm, out + in_len, gcm_ctx->tag_len); |
| 1094 | *out_len = in_len + gcm_ctx->tag_len; |
| 1095 | return 1; |
| 1096 | } |
| 1097 | |
| 1098 | static int aead_aes_gcm_open(const EVP_AEAD_CTX *ctx, uint8_t *out, |
| 1099 | size_t *out_len, size_t max_out_len, |
| 1100 | const uint8_t *nonce, size_t nonce_len, |
| 1101 | const uint8_t *in, size_t in_len, |
| 1102 | const uint8_t *ad, size_t ad_len) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1103 | const struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state; |
| 1104 | uint8_t tag[EVP_AEAD_AES_GCM_TAG_LEN]; |
| 1105 | size_t plaintext_len; |
| 1106 | GCM128_CONTEXT gcm; |
| 1107 | |
| 1108 | if (in_len < gcm_ctx->tag_len) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1109 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1110 | return 0; |
| 1111 | } |
| 1112 | |
| 1113 | plaintext_len = in_len - gcm_ctx->tag_len; |
| 1114 | |
| 1115 | if (max_out_len < plaintext_len) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1116 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1117 | return 0; |
| 1118 | } |
| 1119 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1120 | const AES_KEY *key = &gcm_ctx->ks.ks; |
| 1121 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 1122 | OPENSSL_memcpy(&gcm, &gcm_ctx->gcm, sizeof(gcm)); |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1123 | CRYPTO_gcm128_setiv(&gcm, key, nonce, nonce_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1124 | |
| 1125 | if (!CRYPTO_gcm128_aad(&gcm, ad, ad_len)) { |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
| 1129 | if (gcm_ctx->ctr) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1130 | if (!CRYPTO_gcm128_decrypt_ctr32(&gcm, key, in, out, |
| 1131 | in_len - gcm_ctx->tag_len, gcm_ctx->ctr)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1132 | return 0; |
| 1133 | } |
| 1134 | } else { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1135 | if (!CRYPTO_gcm128_decrypt(&gcm, key, in, out, in_len - gcm_ctx->tag_len)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1136 | return 0; |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | CRYPTO_gcm128_tag(&gcm, tag, gcm_ctx->tag_len); |
| 1141 | if (CRYPTO_memcmp(tag, in + plaintext_len, gcm_ctx->tag_len) != 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1142 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1143 | return 0; |
| 1144 | } |
| 1145 | |
| 1146 | *out_len = plaintext_len; |
| 1147 | return 1; |
| 1148 | } |
| 1149 | |
| 1150 | static const EVP_AEAD aead_aes_128_gcm = { |
| 1151 | 16, /* key len */ |
| 1152 | 12, /* nonce len */ |
| 1153 | EVP_AEAD_AES_GCM_TAG_LEN, /* overhead */ |
| 1154 | EVP_AEAD_AES_GCM_TAG_LEN, /* max tag length */ |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1155 | aead_aes_gcm_init, |
| 1156 | NULL, /* init_with_direction */ |
| 1157 | aead_aes_gcm_cleanup, |
| 1158 | aead_aes_gcm_seal, |
| 1159 | aead_aes_gcm_open, |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 1160 | NULL, /* get_iv */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1161 | }; |
| 1162 | |
| 1163 | static const EVP_AEAD aead_aes_256_gcm = { |
| 1164 | 32, /* key len */ |
| 1165 | 12, /* nonce len */ |
| 1166 | EVP_AEAD_AES_GCM_TAG_LEN, /* overhead */ |
| 1167 | EVP_AEAD_AES_GCM_TAG_LEN, /* max tag length */ |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1168 | aead_aes_gcm_init, |
| 1169 | NULL, /* init_with_direction */ |
| 1170 | aead_aes_gcm_cleanup, |
| 1171 | aead_aes_gcm_seal, |
| 1172 | aead_aes_gcm_open, |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 1173 | NULL, /* get_iv */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1174 | }; |
| 1175 | |
| 1176 | const EVP_AEAD *EVP_aead_aes_128_gcm(void) { return &aead_aes_128_gcm; } |
| 1177 | |
| 1178 | const EVP_AEAD *EVP_aead_aes_256_gcm(void) { return &aead_aes_256_gcm; } |
| 1179 | |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame^] | 1180 | #if defined(BORINGSSL_FIPS) |
| 1181 | #define FIPS_AES_GCM_IV_LEN 12 |
| 1182 | |
| 1183 | static int aead_aes_gcm_fips_testonly_seal( |
| 1184 | const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len, size_t max_out_len, |
| 1185 | const uint8_t *nonce, size_t nonce_len, const uint8_t *in, size_t in_len, |
| 1186 | const uint8_t *ad, size_t ad_len) { |
| 1187 | if (nonce_len != 0) { |
| 1188 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE); |
| 1189 | return 0; |
| 1190 | } |
| 1191 | if (max_out_len < FIPS_AES_GCM_IV_LEN) { |
| 1192 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
| 1196 | uint8_t real_nonce[FIPS_AES_GCM_IV_LEN]; |
| 1197 | if (!RAND_bytes(real_nonce, sizeof(real_nonce))) { |
| 1198 | return 0; |
| 1199 | } |
| 1200 | int ret = |
| 1201 | aead_aes_gcm_seal(ctx, out, out_len, max_out_len - FIPS_AES_GCM_IV_LEN, |
| 1202 | real_nonce, sizeof(real_nonce), in, in_len, ad, ad_len); |
| 1203 | if (ret) { |
| 1204 | /* Copy the generated IV into the start of the ciphertext. */ |
| 1205 | OPENSSL_memmove(out + sizeof(real_nonce), out, *out_len); |
| 1206 | OPENSSL_memcpy(out, real_nonce, sizeof(real_nonce)); |
| 1207 | *out_len += sizeof(real_nonce); |
| 1208 | } |
| 1209 | |
| 1210 | return ret; |
| 1211 | } |
| 1212 | |
| 1213 | static int aead_aes_gcm_fips_testonly_open( |
| 1214 | const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len, size_t max_out_len, |
| 1215 | const uint8_t *nonce, size_t nonce_len, const uint8_t *in, size_t in_len, |
| 1216 | const uint8_t *ad, size_t ad_len) { |
| 1217 | if (nonce_len != 0) { |
| 1218 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE); |
| 1219 | return 0; |
| 1220 | } |
| 1221 | if (in_len < FIPS_AES_GCM_IV_LEN) { |
| 1222 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
| 1223 | return 0; |
| 1224 | } |
| 1225 | |
| 1226 | /* Parse the generated IV from the start of the ciphertext. */ |
| 1227 | uint8_t real_nonce[FIPS_AES_GCM_IV_LEN]; |
| 1228 | OPENSSL_memcpy(real_nonce, in, sizeof(real_nonce)); |
| 1229 | |
| 1230 | /* Aliasing guarantees only allow the use of overlapping buffers when the |
| 1231 | * input and output buffers are identical, so a new input needs to be |
| 1232 | * allocated for the actual input ciphertext. */ |
| 1233 | size_t real_len = in_len - FIPS_AES_GCM_IV_LEN; |
| 1234 | uint8_t *real_in = OPENSSL_malloc(real_len); |
| 1235 | if (real_in == NULL) { |
| 1236 | OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE); |
| 1237 | return 0; |
| 1238 | } |
| 1239 | OPENSSL_memcpy(real_in, in + FIPS_AES_GCM_IV_LEN, real_len); |
| 1240 | |
| 1241 | int ret = |
| 1242 | aead_aes_gcm_open(ctx, out, out_len, max_out_len, real_nonce, |
| 1243 | sizeof(real_nonce), real_in, real_len, ad, ad_len); |
| 1244 | |
| 1245 | OPENSSL_free(real_in); |
| 1246 | return ret; |
| 1247 | } |
| 1248 | |
| 1249 | static const EVP_AEAD aead_aes_128_gcm_fips_testonly = { |
| 1250 | 16, /* key len */ |
| 1251 | 0, /* nonce len */ |
| 1252 | EVP_AEAD_AES_GCM_TAG_LEN + FIPS_AES_GCM_IV_LEN, /* overhead */ |
| 1253 | EVP_AEAD_AES_GCM_TAG_LEN, /* max tag length */ |
| 1254 | aead_aes_gcm_init, |
| 1255 | NULL, /* init_with_direction */ |
| 1256 | aead_aes_gcm_cleanup, |
| 1257 | aead_aes_gcm_fips_testonly_seal, |
| 1258 | aead_aes_gcm_fips_testonly_open, |
| 1259 | NULL, /* get_iv */ |
| 1260 | }; |
| 1261 | |
| 1262 | static const EVP_AEAD aead_aes_256_gcm_fips_testonly = { |
| 1263 | 32, /* key len */ |
| 1264 | 0, /* nonce len */ |
| 1265 | EVP_AEAD_AES_GCM_TAG_LEN + FIPS_AES_GCM_IV_LEN, /* overhead */ |
| 1266 | EVP_AEAD_AES_GCM_TAG_LEN, /* max tag length */ |
| 1267 | aead_aes_gcm_init, |
| 1268 | NULL, /* init_with_direction */ |
| 1269 | aead_aes_gcm_cleanup, |
| 1270 | aead_aes_gcm_fips_testonly_seal, |
| 1271 | aead_aes_gcm_fips_testonly_open, |
| 1272 | NULL, /* get_iv */ |
| 1273 | }; |
| 1274 | |
| 1275 | const EVP_AEAD *EVP_aead_aes_128_gcm_fips_testonly(void) { |
| 1276 | return &aead_aes_128_gcm_fips_testonly; |
| 1277 | } |
| 1278 | |
| 1279 | const EVP_AEAD *EVP_aead_aes_256_gcm_fips_testonly(void) { |
| 1280 | return &aead_aes_256_gcm_fips_testonly; |
| 1281 | } |
| 1282 | #endif /* BORINGSSL_FIPS */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1283 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1284 | int EVP_has_aes_hardware(void) { |
| 1285 | #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) |
| 1286 | return aesni_capable() && crypto_gcm_clmul_enabled(); |
| 1287 | #elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1288 | return hwaes_capable() && CRYPTO_is_ARMv8_PMULL_capable(); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1289 | #else |
| 1290 | return 0; |
| 1291 | #endif |
| 1292 | } |