| 1 |
// Copyright (c) 2017, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#ifndef OPENSSL_HEADER_AES_INTERNAL_H |
| 5 |
#define OPENSSL_HEADER_AES_INTERNAL_H |
| 6 |
|
| 7 |
#include <stdlib.h> |
| 8 |
|
| 9 |
#include <openssl/cpu.h> |
| 10 |
#include "../service_indicator/internal.h" |
| 11 |
#include "../cpucap/internal.h" |
| 12 |
#include "../../internal.h" |
| 13 |
|
| 14 |
#if defined(__cplusplus) |
| 15 |
extern "C" { |
| 16 |
#endif |
| 17 |
|
| 18 |
|
| 19 |
#if !defined(OPENSSL_NO_ASM) |
| 20 |
|
| 21 |
#if defined(OPENSSL_X86_64) |
| 22 |
OPENSSL_INLINE int avx512_xts_available(void) { |
| 23 |
return (CRYPTO_is_VAES_capable() && |
| 24 |
CRYPTO_is_VBMI2_capable() && |
| 25 |
CRYPTO_is_AVX512_capable() && |
| 26 |
CRYPTO_is_VPCLMULQDQ_capable()); |
| 27 |
} |
| 28 |
#endif |
| 29 |
|
| 30 |
#if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) |
| 31 |
#define HWAES |
| 32 |
#define HWAES_ECB |
| 33 |
|
| 34 |
OPENSSL_INLINE int hwaes_capable(void) { return CRYPTO_is_AESNI_capable(); } |
| 35 |
|
| 36 |
#define VPAES |
| 37 |
#if defined(OPENSSL_X86_64) |
| 38 |
#define VPAES_CTR32 |
| 39 |
#define HWAES_XTS |
| 40 |
OPENSSL_INLINE int hwaes_xts_available(void) { |
| 41 |
return CRYPTO_is_AESNI_capable(); |
| 42 |
} |
| 43 |
#endif |
| 44 |
#define VPAES_CBC |
| 45 |
OPENSSL_INLINE int vpaes_capable(void) { return CRYPTO_is_SSSE3_capable(); } |
| 46 |
|
| 47 |
#elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) |
| 48 |
#define HWAES |
| 49 |
|
| 50 |
OPENSSL_INLINE int hwaes_capable(void) { return CRYPTO_is_ARMv8_AES_capable(); } |
| 51 |
|
| 52 |
#if defined(OPENSSL_ARM) |
| 53 |
#define BSAES |
| 54 |
#define VPAES |
| 55 |
#define VPAES_CTR32 |
| 56 |
OPENSSL_INLINE int bsaes_capable(void) { return CRYPTO_is_NEON_capable(); } |
| 57 |
OPENSSL_INLINE int vpaes_capable(void) { return CRYPTO_is_NEON_capable(); } |
| 58 |
#endif |
| 59 |
|
| 60 |
#if defined(OPENSSL_AARCH64) |
| 61 |
#define VPAES |
| 62 |
#define VPAES_CBC |
| 63 |
#define VPAES_CTR32 |
| 64 |
#define HWAES_XTS |
| 65 |
OPENSSL_INLINE int vpaes_capable(void) { return CRYPTO_is_NEON_capable(); } |
| 66 |
OPENSSL_INLINE int hwaes_xts_available(void) { |
| 67 |
// same as hwaes_capable() |
| 68 |
return CRYPTO_is_ARMv8_AES_capable(); |
| 69 |
} |
| 70 |
#endif |
| 71 |
|
| 72 |
#elif defined(OPENSSL_PPC64LE) |
| 73 |
#define HWAES |
| 74 |
|
| 75 |
OPENSSL_INLINE int hwaes_capable(void) { |
| 76 |
return CRYPTO_is_PPC64LE_vcrypto_capable(); |
| 77 |
} |
| 78 |
#endif |
| 79 |
|
| 80 |
#endif // !NO_ASM |
| 81 |
|
| 82 |
|
| 83 |
#if defined(HWAES) |
| 84 |
|
| 85 |
int aes_hw_set_encrypt_key(const uint8_t *user_key, const int bits, |
| 86 |
AES_KEY *key); |
| 87 |
int aes_hw_set_decrypt_key(const uint8_t *user_key, const int bits, |
| 88 |
AES_KEY *key); |
| 89 |
void aes_hw_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); |
| 90 |
void aes_hw_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); |
| 91 |
void aes_hw_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 92 |
const AES_KEY *key, uint8_t *ivec, const int enc); |
| 93 |
void aes_hw_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t len, |
| 94 |
const AES_KEY *key, const uint8_t ivec[16]); |
| 95 |
|
| 96 |
#else |
| 97 |
|
| 98 |
// If HWAES isn't defined then we provide dummy functions for each of the hwaes |
| 99 |
// functions. |
| 100 |
OPENSSL_INLINE int hwaes_capable(void) { return 0; } |
| 101 |
|
| 102 |
OPENSSL_INLINE int aes_hw_set_encrypt_key(const uint8_t *user_key, int bits, |
| 103 |
AES_KEY *key) { |
| 104 |
abort(); |
| 105 |
} |
| 106 |
|
| 107 |
OPENSSL_INLINE int aes_hw_set_decrypt_key(const uint8_t *user_key, int bits, |
| 108 |
AES_KEY *key) { |
| 109 |
abort(); |
| 110 |
} |
| 111 |
|
| 112 |
OPENSSL_INLINE void aes_hw_encrypt(const uint8_t *in, uint8_t *out, |
| 113 |
const AES_KEY *key) { |
| 114 |
abort(); |
| 115 |
} |
| 116 |
|
| 117 |
OPENSSL_INLINE void aes_hw_decrypt(const uint8_t *in, uint8_t *out, |
| 118 |
const AES_KEY *key) { |
| 119 |
abort(); |
| 120 |
} |
| 121 |
|
| 122 |
OPENSSL_INLINE void aes_hw_cbc_encrypt(const uint8_t *in, uint8_t *out, |
| 123 |
size_t length, const AES_KEY *key, |
| 124 |
uint8_t *ivec, int enc) { |
| 125 |
abort(); |
| 126 |
} |
| 127 |
|
| 128 |
OPENSSL_INLINE void aes_hw_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, |
| 129 |
size_t len, const AES_KEY *key, |
| 130 |
const uint8_t ivec[16]) { |
| 131 |
abort(); |
| 132 |
} |
| 133 |
|
| 134 |
#endif // !HWAES |
| 135 |
|
| 136 |
|
| 137 |
#if defined(HWAES_ECB) |
| 138 |
void aes_hw_ecb_encrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 139 |
const AES_KEY *key, const int enc); |
| 140 |
#endif // HWAES_ECB |
| 141 |
|
| 142 |
#if defined(HWAES_XTS) |
| 143 |
void aes_hw_xts_encrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 144 |
const AES_KEY *key1, const AES_KEY *key2, |
| 145 |
const uint8_t iv[16]); |
| 146 |
void aes_hw_xts_decrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 147 |
const AES_KEY *key1, const AES_KEY *key2, |
| 148 |
const uint8_t iv[16]); |
| 149 |
OPENSSL_EXPORT int aes_hw_xts_cipher(const uint8_t *in, uint8_t *out, size_t length, |
| 150 |
const AES_KEY *key1, const AES_KEY *key2, |
| 151 |
const uint8_t iv[16], int enc); |
| 152 |
|
| 153 |
#if defined(OPENSSL_X86_64) && !defined(MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX) && !defined(OPENSSL_WINDOWS) |
| 154 |
#define AES_XTS_X86_64_AVX512 |
| 155 |
void aes_hw_xts_encrypt_avx512(const uint8_t *in, uint8_t *out, size_t length, |
| 156 |
const AES_KEY *key1, const AES_KEY *key2, |
| 157 |
const uint8_t iv[16]); |
| 158 |
void aes_hw_xts_decrypt_avx512(const uint8_t *in, uint8_t *out, size_t length, |
| 159 |
const AES_KEY *key1, const AES_KEY *key2, |
| 160 |
const uint8_t iv[16]); |
| 161 |
int crypto_xts_avx512_enabled(void); |
| 162 |
|
| 163 |
#endif //AES_XTS_X86_64_AVX512 |
| 164 |
|
| 165 |
|
| 166 |
#else |
| 167 |
OPENSSL_INLINE int hwaes_xts_available(void) { return 0; } |
| 168 |
OPENSSL_INLINE void aes_hw_xts_encrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 169 |
const AES_KEY *key1, const AES_KEY *key2, |
| 170 |
const uint8_t iv[16]) { |
| 171 |
abort(); |
| 172 |
} |
| 173 |
OPENSSL_INLINE void aes_hw_xts_decrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 174 |
const AES_KEY *key1, const AES_KEY *key2, |
| 175 |
const uint8_t iv[16]) { |
| 176 |
abort(); |
| 177 |
} |
| 178 |
OPENSSL_INLINE int aes_hw_xts_cipher(const uint8_t *in, uint8_t *out, size_t length, |
| 179 |
const AES_KEY *key1, const AES_KEY *key2, |
| 180 |
const uint8_t iv[16], int enc) { |
| 181 |
abort(); |
| 182 |
} |
| 183 |
#endif // HWAES_XTS |
| 184 |
|
| 185 |
#if defined(BSAES) |
| 186 |
// Note |bsaes_cbc_encrypt| requires |enc| to be zero. |
| 187 |
void bsaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 188 |
const AES_KEY *key, uint8_t ivec[16], int enc); |
| 189 |
void bsaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t len, |
| 190 |
const AES_KEY *key, const uint8_t ivec[16]); |
| 191 |
// VPAES to BSAES conversions are available on all BSAES platforms. |
| 192 |
void vpaes_encrypt_key_to_bsaes(AES_KEY *out_bsaes, const AES_KEY *vpaes); |
| 193 |
void vpaes_decrypt_key_to_bsaes(AES_KEY *out_bsaes, const AES_KEY *vpaes); |
| 194 |
#else |
| 195 |
OPENSSL_INLINE char bsaes_capable(void) { return 0; } |
| 196 |
|
| 197 |
// On other platforms, bsaes_capable() will always return false and so the |
| 198 |
// following will never be called. |
| 199 |
OPENSSL_INLINE void bsaes_cbc_encrypt(const uint8_t *in, uint8_t *out, |
| 200 |
size_t length, const AES_KEY *key, |
| 201 |
uint8_t ivec[16], int enc) { |
| 202 |
abort(); |
| 203 |
} |
| 204 |
|
| 205 |
OPENSSL_INLINE void bsaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, |
| 206 |
size_t len, const AES_KEY *key, |
| 207 |
const uint8_t ivec[16]) { |
| 208 |
abort(); |
| 209 |
} |
| 210 |
|
| 211 |
OPENSSL_INLINE void vpaes_encrypt_key_to_bsaes(AES_KEY *out_bsaes, |
| 212 |
const AES_KEY *vpaes) { |
| 213 |
abort(); |
| 214 |
} |
| 215 |
|
| 216 |
OPENSSL_INLINE void vpaes_decrypt_key_to_bsaes(AES_KEY *out_bsaes, |
| 217 |
const AES_KEY *vpaes) { |
| 218 |
abort(); |
| 219 |
} |
| 220 |
#endif // !BSAES |
| 221 |
|
| 222 |
|
| 223 |
#if defined(VPAES) |
| 224 |
// On platforms where VPAES gets defined (just above), then these functions are |
| 225 |
// provided by asm. |
| 226 |
int vpaes_set_encrypt_key(const uint8_t *userKey, int bits, AES_KEY *key); |
| 227 |
int vpaes_set_decrypt_key(const uint8_t *userKey, int bits, AES_KEY *key); |
| 228 |
|
| 229 |
void vpaes_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); |
| 230 |
void vpaes_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); |
| 231 |
|
| 232 |
#if defined(VPAES_CBC) |
| 233 |
void vpaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, |
| 234 |
const AES_KEY *key, uint8_t *ivec, int enc); |
| 235 |
#endif |
| 236 |
#if defined(VPAES_CTR32) |
| 237 |
void vpaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t len, |
| 238 |
const AES_KEY *key, const uint8_t ivec[16]); |
| 239 |
#endif |
| 240 |
#else |
| 241 |
OPENSSL_INLINE char vpaes_capable(void) { return 0; } |
| 242 |
|
| 243 |
// On other platforms, vpaes_capable() will always return false and so the |
| 244 |
// following will never be called. |
| 245 |
OPENSSL_INLINE int vpaes_set_encrypt_key(const uint8_t *userKey, int bits, |
| 246 |
AES_KEY *key) { |
| 247 |
abort(); |
| 248 |
} |
| 249 |
OPENSSL_INLINE int vpaes_set_decrypt_key(const uint8_t *userKey, int bits, |
| 250 |
AES_KEY *key) { |
| 251 |
abort(); |
| 252 |
} |
| 253 |
OPENSSL_INLINE void vpaes_encrypt(const uint8_t *in, uint8_t *out, |
| 254 |
const AES_KEY *key) { |
| 255 |
abort(); |
| 256 |
} |
| 257 |
OPENSSL_INLINE void vpaes_decrypt(const uint8_t *in, uint8_t *out, |
| 258 |
const AES_KEY *key) { |
| 259 |
abort(); |
| 260 |
} |
| 261 |
OPENSSL_INLINE void vpaes_cbc_encrypt(const uint8_t *in, uint8_t *out, |
| 262 |
size_t length, const AES_KEY *key, |
| 263 |
uint8_t *ivec, int enc) { |
| 264 |
abort(); |
| 265 |
} |
| 266 |
#endif // !VPAES |
| 267 |
|
| 268 |
|
| 269 |
int aes_nohw_set_encrypt_key(const uint8_t *key, unsigned bits, |
| 270 |
AES_KEY *aeskey); |
| 271 |
int aes_nohw_set_decrypt_key(const uint8_t *key, unsigned bits, |
| 272 |
AES_KEY *aeskey); |
| 273 |
void aes_nohw_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); |
| 274 |
void aes_nohw_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); |
| 275 |
void aes_nohw_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, |
| 276 |
size_t blocks, const AES_KEY *key, |
| 277 |
const uint8_t ivec[16]); |
| 278 |
void aes_nohw_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t len, |
| 279 |
const AES_KEY *key, uint8_t *ivec, const int enc); |
| 280 |
|
| 281 |
|
| 282 |
#if defined(__cplusplus) |
| 283 |
} // extern C |
| 284 |
#endif |
| 285 |
|
| 286 |
#endif // OPENSSL_HEADER_AES_INTERNAL_H |