| 1 |
// Copyright (c) 2008 The OpenSSL Project. All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/base.h> |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include <openssl/mem.h> |
| 10 |
|
| 11 |
#include "internal.h" |
| 12 |
#include "../../internal.h" |
| 13 |
|
| 14 |
|
| 15 |
// kSizeTWithoutLower4Bits is a mask that can be used to zero the lower four |
| 16 |
// bits of a |size_t|. |
| 17 |
static const size_t kSizeTWithoutLower4Bits = (size_t) -16; |
| 18 |
|
| 19 |
|
| 20 |
#define GCM_MUL(ctx, Xi) gcm_gmult_nohw((ctx)->Xi, (ctx)->gcm_key.Htable) |
| 21 |
#define GHASH(ctx, in, len) \ |
| 22 |
gcm_ghash_nohw((ctx)->Xi, (ctx)->gcm_key.Htable, in, len) |
| 23 |
// GHASH_CHUNK is "stride parameter" missioned to mitigate cache |
| 24 |
// trashing effect. In other words idea is to hash data while it's |
| 25 |
// still in L1 cache after encryption pass... |
| 26 |
#define GHASH_CHUNK (3 * 1024) |
| 27 |
|
| 28 |
#if defined(GHASH_ASM_X86_64) || defined(GHASH_ASM_X86) |
| 29 |
static inline void gcm_reduce_1bit(u128 *V) { |
| 30 |
if (sizeof(crypto_word_t) == 8) { |
| 31 |
uint64_t T = UINT64_C(0xe100000000000000) & (0 - (V->hi & 1)); |
| 32 |
V->hi = (V->lo << 63) | (V->hi >> 1); |
| 33 |
V->lo = (V->lo >> 1) ^ T; |
| 34 |
} else { |
| 35 |
uint32_t T = 0xe1000000U & (0 - (uint32_t)(V->hi & 1)); |
| 36 |
V->hi = (V->lo << 63) | (V->hi >> 1); |
| 37 |
V->lo = (V->lo >> 1) ^ ((uint64_t)T << 32); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
void gcm_init_ssse3(u128 Htable[16], const uint64_t H[2]) { |
| 42 |
Htable[0].hi = 0; |
| 43 |
Htable[0].lo = 0; |
| 44 |
u128 V; |
| 45 |
V.hi = H[1]; |
| 46 |
V.lo = H[0]; |
| 47 |
|
| 48 |
Htable[8] = V; |
| 49 |
gcm_reduce_1bit(&V); |
| 50 |
Htable[4] = V; |
| 51 |
gcm_reduce_1bit(&V); |
| 52 |
Htable[2] = V; |
| 53 |
gcm_reduce_1bit(&V); |
| 54 |
Htable[1] = V; |
| 55 |
Htable[3].hi = V.hi ^ Htable[2].hi, Htable[3].lo = V.lo ^ Htable[2].lo; |
| 56 |
V = Htable[4]; |
| 57 |
Htable[5].hi = V.hi ^ Htable[1].hi, Htable[5].lo = V.lo ^ Htable[1].lo; |
| 58 |
Htable[6].hi = V.hi ^ Htable[2].hi, Htable[6].lo = V.lo ^ Htable[2].lo; |
| 59 |
Htable[7].hi = V.hi ^ Htable[3].hi, Htable[7].lo = V.lo ^ Htable[3].lo; |
| 60 |
V = Htable[8]; |
| 61 |
Htable[9].hi = V.hi ^ Htable[1].hi, Htable[9].lo = V.lo ^ Htable[1].lo; |
| 62 |
Htable[10].hi = V.hi ^ Htable[2].hi, Htable[10].lo = V.lo ^ Htable[2].lo; |
| 63 |
Htable[11].hi = V.hi ^ Htable[3].hi, Htable[11].lo = V.lo ^ Htable[3].lo; |
| 64 |
Htable[12].hi = V.hi ^ Htable[4].hi, Htable[12].lo = V.lo ^ Htable[4].lo; |
| 65 |
Htable[13].hi = V.hi ^ Htable[5].hi, Htable[13].lo = V.lo ^ Htable[5].lo; |
| 66 |
Htable[14].hi = V.hi ^ Htable[6].hi, Htable[14].lo = V.lo ^ Htable[6].lo; |
| 67 |
Htable[15].hi = V.hi ^ Htable[7].hi, Htable[15].lo = V.lo ^ Htable[7].lo; |
| 68 |
|
| 69 |
// Treat |Htable| as a 16x16 byte table and transpose it. Thus, Htable[i] |
| 70 |
// contains the i'th byte of j*H for all j. |
| 71 |
uint8_t *Hbytes = (uint8_t *)Htable; |
| 72 |
for (int i = 0; i < 16; i++) { |
| 73 |
for (int j = 0; j < i; j++) { |
| 74 |
uint8_t tmp = Hbytes[16*i + j]; |
| 75 |
Hbytes[16*i + j] = Hbytes[16*j + i]; |
| 76 |
Hbytes[16*j + i] = tmp; |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
#endif // GHASH_ASM_X86_64 || GHASH_ASM_X86 |
| 81 |
|
| 82 |
#ifdef GCM_FUNCREF |
| 83 |
#undef GCM_MUL |
| 84 |
#define GCM_MUL(ctx, Xi) (*gcm_gmult_p)((ctx)->Xi, (ctx)->gcm_key.Htable) |
| 85 |
#undef GHASH |
| 86 |
#define GHASH(ctx, in, len) \ |
| 87 |
(*gcm_ghash_p)((ctx)->Xi, (ctx)->gcm_key.Htable, in, len) |
| 88 |
#endif // GCM_FUNCREF |
| 89 |
|
| 90 |
#if defined(HW_GCM) && defined(OPENSSL_X86_64) |
| 91 |
static size_t hw_gcm_encrypt(const uint8_t *in, uint8_t *out, size_t len, |
| 92 |
const AES_KEY *key, uint8_t ivec[16], |
| 93 |
uint8_t Xi[16], const u128 Htable[16]) { |
| 94 |
return aesni_gcm_encrypt(in, out, len, key, ivec, Htable, Xi); |
| 95 |
} |
| 96 |
|
| 97 |
static size_t hw_gcm_decrypt(const uint8_t *in, uint8_t *out, size_t len, |
| 98 |
const AES_KEY *key, uint8_t ivec[16], |
| 99 |
uint8_t Xi[16], const u128 Htable[16]) { |
| 100 |
return aesni_gcm_decrypt(in, out, len, key, ivec, Htable, Xi); |
| 101 |
} |
| 102 |
#endif // HW_GCM && X86_64 |
| 103 |
|
| 104 |
#if defined(HW_GCM) && defined(OPENSSL_AARCH64) |
| 105 |
|
| 106 |
static size_t hw_gcm_encrypt(const uint8_t *in, uint8_t *out, size_t len, |
| 107 |
const AES_KEY *key, uint8_t ivec[16], |
| 108 |
uint8_t Xi[16], const u128 Htable[16]) { |
| 109 |
const size_t len_blocks = len & kSizeTWithoutLower4Bits; |
| 110 |
if (!len_blocks) { |
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
// The 8x-unrolled assembly implementation starts outperforming |
| 115 |
// the 4x-unrolled one starting around input length of 256 bytes |
| 116 |
// in the case of the EVP API. |
| 117 |
// In the case of the AEAD API, it can be used for all input lengths |
| 118 |
// but we are not identifying which API calls the code below. |
| 119 |
if (CRYPTO_is_ARMv8_GCM_8x_capable() && len >= 256) { |
| 120 |
switch(key->rounds) { |
| 121 |
case 10: |
| 122 |
aesv8_gcm_8x_enc_128(in, len_blocks * 8, out, Xi, ivec, key, Htable); |
| 123 |
break; |
| 124 |
case 12: |
| 125 |
aesv8_gcm_8x_enc_192(in, len_blocks * 8, out, Xi, ivec, key, Htable); |
| 126 |
break; |
| 127 |
case 14: |
| 128 |
aesv8_gcm_8x_enc_256(in, len_blocks * 8, out, Xi, ivec, key, Htable); |
| 129 |
break; |
| 130 |
default: |
| 131 |
// The subsequent logic after returning can process |
| 132 |
// the input or return an error. |
| 133 |
return 0; |
| 134 |
break; |
| 135 |
} |
| 136 |
} else { |
| 137 |
aes_gcm_enc_kernel(in, len_blocks * 8, out, Xi, ivec, key, Htable); |
| 138 |
} |
| 139 |
|
| 140 |
return len_blocks; |
| 141 |
} |
| 142 |
|
| 143 |
static size_t hw_gcm_decrypt(const uint8_t *in, uint8_t *out, size_t len, |
| 144 |
const AES_KEY *key, uint8_t ivec[16], |
| 145 |
uint8_t Xi[16], const u128 Htable[16]) { |
| 146 |
const size_t len_blocks = len & kSizeTWithoutLower4Bits; |
| 147 |
if (!len_blocks) { |
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
// The 8x-unrolled assembly implementation starts outperforming |
| 152 |
// the 4x-unrolled one starting around input length of 256 bytes |
| 153 |
// in the case of the EVP API. |
| 154 |
// In the case of the AEAD API, it can be used for all input lengths |
| 155 |
// but we are not identifying which API calls the code below. |
| 156 |
if (CRYPTO_is_ARMv8_GCM_8x_capable() && len >= 256) { |
| 157 |
switch(key->rounds) { |
| 158 |
case 10: |
| 159 |
aesv8_gcm_8x_dec_128(in, len_blocks * 8, out, Xi, ivec, key, Htable); |
| 160 |
break; |
| 161 |
case 12: |
| 162 |
aesv8_gcm_8x_dec_192(in, len_blocks * 8, out, Xi, ivec, key, Htable); |
| 163 |
break; |
| 164 |
case 14: |
| 165 |
aesv8_gcm_8x_dec_256(in, len_blocks * 8, out, Xi, ivec, key, Htable); |
| 166 |
break; |
| 167 |
default: |
| 168 |
// The subsequent logic after returning can process |
| 169 |
// the input or return an error. |
| 170 |
return 0; |
| 171 |
break; |
| 172 |
} |
| 173 |
} else { |
| 174 |
aes_gcm_dec_kernel(in, len_blocks * 8, out, Xi, ivec, key, Htable); |
| 175 |
} |
| 176 |
|
| 177 |
return len_blocks; |
| 178 |
} |
| 179 |
|
| 180 |
#endif // HW_GCM && AARCH64 |
| 181 |
|
| 182 |
// Trampolines for GCM function pointers to avoid delocator issues with adr |
| 183 |
// on AArch64. Without these wrappers, the function pointer calculations |
| 184 |
// may require PC-relative offsets outside the addressable range. |
| 185 |
#if defined(GHASH_ASM_ARM) |
| 186 |
static inline void gcm_gmult_v8_wrapper(uint8_t Xi[16], const u128 Htable[16]) { |
| 187 |
gcm_gmult_v8(Xi, Htable); |
| 188 |
} |
| 189 |
|
| 190 |
static inline void gcm_ghash_v8_wrapper(uint8_t Xi[16], const u128 Htable[16], |
| 191 |
const uint8_t *inp, size_t len) { |
| 192 |
gcm_ghash_v8(Xi, Htable, inp, len); |
| 193 |
} |
| 194 |
|
| 195 |
static inline void gcm_gmult_neon_wrapper(uint8_t Xi[16], const u128 Htable[16]) { |
| 196 |
gcm_gmult_neon(Xi, Htable); |
| 197 |
} |
| 198 |
|
| 199 |
static inline void gcm_ghash_neon_wrapper(uint8_t Xi[16], const u128 Htable[16], |
| 200 |
const uint8_t *inp, size_t len) { |
| 201 |
gcm_ghash_neon(Xi, Htable, inp, len); |
| 202 |
} |
| 203 |
#endif |
| 204 |
|
| 205 |
void CRYPTO_ghash_init(gmult_func *out_mult, ghash_func *out_hash, |
| 206 |
u128 out_table[16], int *out_is_avx, |
| 207 |
const uint8_t gcm_key[16]) { |
| 208 |
*out_is_avx = 0; |
| 209 |
|
| 210 |
// H is passed to |gcm_init_*| as a pair of byte-swapped, 64-bit values. |
| 211 |
uint64_t H[2] = {CRYPTO_load_u64_be(gcm_key), |
| 212 |
CRYPTO_load_u64_be(gcm_key + 8)}; |
| 213 |
|
| 214 |
#if defined(GHASH_ASM_X86_64) |
| 215 |
#if !defined(MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX) |
| 216 |
if (crypto_gcm_avx512_enabled()) { |
| 217 |
gcm_init_avx512(out_table, H); |
| 218 |
*out_mult = gcm_gmult_avx512; |
| 219 |
*out_hash = gcm_ghash_avx512; |
| 220 |
*out_is_avx = 1; |
| 221 |
goto out; |
| 222 |
} |
| 223 |
#endif |
| 224 |
if (crypto_gcm_clmul_enabled()) { |
| 225 |
if (CRYPTO_is_AVX_capable() && CRYPTO_is_MOVBE_capable()) { |
| 226 |
gcm_init_avx(out_table, H); |
| 227 |
*out_mult = gcm_gmult_avx; |
| 228 |
*out_hash = gcm_ghash_avx; |
| 229 |
*out_is_avx = 1; |
| 230 |
goto out; |
| 231 |
} |
| 232 |
gcm_init_clmul(out_table, H); |
| 233 |
*out_mult = gcm_gmult_clmul; |
| 234 |
*out_hash = gcm_ghash_clmul; |
| 235 |
goto out; |
| 236 |
} |
| 237 |
if (CRYPTO_is_SSSE3_capable()) { |
| 238 |
gcm_init_ssse3(out_table, H); |
| 239 |
*out_mult = gcm_gmult_ssse3; |
| 240 |
*out_hash = gcm_ghash_ssse3; |
| 241 |
goto out; |
| 242 |
} |
| 243 |
#elif defined(GHASH_ASM_X86) |
| 244 |
if (crypto_gcm_clmul_enabled()) { |
| 245 |
gcm_init_clmul(out_table, H); |
| 246 |
*out_mult = gcm_gmult_clmul; |
| 247 |
*out_hash = gcm_ghash_clmul; |
| 248 |
goto out; |
| 249 |
} |
| 250 |
if (CRYPTO_is_SSSE3_capable()) { |
| 251 |
gcm_init_ssse3(out_table, H); |
| 252 |
*out_mult = gcm_gmult_ssse3; |
| 253 |
*out_hash = gcm_ghash_ssse3; |
| 254 |
goto out; |
| 255 |
} |
| 256 |
#elif defined(GHASH_ASM_ARM) |
| 257 |
if (gcm_pmull_capable()) { |
| 258 |
gcm_init_v8(out_table, H); |
| 259 |
*out_mult = gcm_gmult_v8_wrapper; |
| 260 |
*out_hash = gcm_ghash_v8_wrapper; |
| 261 |
goto out; |
| 262 |
} |
| 263 |
|
| 264 |
if (gcm_neon_capable()) { |
| 265 |
gcm_init_neon(out_table, H); |
| 266 |
*out_mult = gcm_gmult_neon_wrapper; |
| 267 |
*out_hash = gcm_ghash_neon_wrapper; |
| 268 |
goto out; |
| 269 |
} |
| 270 |
#elif defined(GHASH_ASM_PPC64LE) |
| 271 |
if (CRYPTO_is_PPC64LE_vcrypto_capable()) { |
| 272 |
gcm_init_p8(out_table, H); |
| 273 |
*out_mult = gcm_gmult_p8; |
| 274 |
*out_hash = gcm_ghash_p8; |
| 275 |
goto out; |
| 276 |
} |
| 277 |
#endif |
| 278 |
|
| 279 |
gcm_init_nohw(out_table, H); |
| 280 |
*out_mult = gcm_gmult_nohw; |
| 281 |
*out_hash = gcm_ghash_nohw; |
| 282 |
|
| 283 |
#if defined(GHASH_ASM_X86_64) || defined(GHASH_ASM_X86) || defined(GHASH_ASM_ARM) || defined(GHASH_ASM_PPC64LE) |
| 284 |
out: |
| 285 |
#endif |
| 286 |
OPENSSL_cleanse(H, sizeof(H)); |
| 287 |
} |
| 288 |
|
| 289 |
void CRYPTO_gcm128_init_key(GCM128_KEY *gcm_key, const AES_KEY *aes_key, |
| 290 |
block128_f block, int block_is_hwaes) { |
| 291 |
OPENSSL_memset(gcm_key, 0, sizeof(*gcm_key)); |
| 292 |
gcm_key->block = block; |
| 293 |
|
| 294 |
uint8_t ghash_key[16]; |
| 295 |
OPENSSL_memset(ghash_key, 0, sizeof(ghash_key)); |
| 296 |
(*block)(ghash_key, ghash_key, aes_key); |
| 297 |
|
| 298 |
int is_avx; |
| 299 |
CRYPTO_ghash_init(&gcm_key->gmult, &gcm_key->ghash, gcm_key->Htable, &is_avx, |
| 300 |
ghash_key); |
| 301 |
OPENSSL_cleanse(ghash_key, sizeof(ghash_key)); |
| 302 |
|
| 303 |
#if defined(OPENSSL_AARCH64) && defined(GHASH_ASM_ARM) |
| 304 |
gcm_key->use_hw_gcm_crypt = (gcm_pmull_capable() && block_is_hwaes) ? 1 : 0; |
| 305 |
#else |
| 306 |
gcm_key->use_hw_gcm_crypt = (is_avx && block_is_hwaes) ? 1 : 0; |
| 307 |
#endif |
| 308 |
} |
| 309 |
|
| 310 |
void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx, const AES_KEY *key, |
| 311 |
const uint8_t *iv, size_t len) { |
| 312 |
#ifdef GCM_FUNCREF |
| 313 |
void (*gcm_gmult_p)(uint8_t Xi[16], const u128 Htable[16]) = |
| 314 |
ctx->gcm_key.gmult; |
| 315 |
#endif |
| 316 |
|
| 317 |
OPENSSL_memset(&ctx->Yi, 0, sizeof(ctx->Yi)); |
| 318 |
OPENSSL_memset(&ctx->Xi, 0, sizeof(ctx->Xi)); |
| 319 |
ctx->len.aad = 0; |
| 320 |
ctx->len.msg = 0; |
| 321 |
ctx->ares = 0; |
| 322 |
ctx->mres = 0; |
| 323 |
|
| 324 |
#if defined(GHASH_ASM_X86_64) && !defined(MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX) |
| 325 |
if (ctx->gcm_key.use_hw_gcm_crypt && crypto_gcm_avx512_enabled()) { |
| 326 |
gcm_setiv_avx512(key, ctx, iv, len); |
| 327 |
return; |
| 328 |
} |
| 329 |
#endif |
| 330 |
|
| 331 |
uint32_t ctr; |
| 332 |
if (len == 12) { |
| 333 |
OPENSSL_memcpy(ctx->Yi, iv, 12); |
| 334 |
ctx->Yi[15] = 1; |
| 335 |
ctr = 1; |
| 336 |
} else { |
| 337 |
uint64_t len0 = len; |
| 338 |
|
| 339 |
while (len >= 16) { |
| 340 |
CRYPTO_xor16(ctx->Yi, ctx->Yi, iv); |
| 341 |
GCM_MUL(ctx, Yi); |
| 342 |
iv += 16; |
| 343 |
len -= 16; |
| 344 |
} |
| 345 |
if (len) { |
| 346 |
for (size_t i = 0; i < len; ++i) { |
| 347 |
ctx->Yi[i] ^= iv[i]; |
| 348 |
} |
| 349 |
GCM_MUL(ctx, Yi); |
| 350 |
} |
| 351 |
|
| 352 |
uint8_t len_block[16]; |
| 353 |
OPENSSL_memset(len_block, 0, 8); |
| 354 |
CRYPTO_store_u64_be(len_block + 8, len0 << 3); |
| 355 |
CRYPTO_xor16(ctx->Yi, ctx->Yi, len_block); |
| 356 |
|
| 357 |
GCM_MUL(ctx, Yi); |
| 358 |
ctr = CRYPTO_load_u32_be(ctx->Yi + 12); |
| 359 |
} |
| 360 |
|
| 361 |
(*ctx->gcm_key.block)(ctx->Yi, ctx->EK0, key); |
| 362 |
++ctr; |
| 363 |
CRYPTO_store_u32_be(ctx->Yi + 12, ctr); |
| 364 |
} |
| 365 |
|
| 366 |
int CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx, const uint8_t *aad, size_t len) { |
| 367 |
#ifdef GCM_FUNCREF |
| 368 |
void (*gcm_gmult_p)(uint8_t Xi[16], const u128 Htable[16]) = |
| 369 |
ctx->gcm_key.gmult; |
| 370 |
void (*gcm_ghash_p)(uint8_t Xi[16], const u128 Htable[16], const uint8_t *inp, |
| 371 |
size_t len) = ctx->gcm_key.ghash; |
| 372 |
#endif |
| 373 |
|
| 374 |
if (ctx->len.msg != 0) { |
| 375 |
// The caller must have finished the AAD before providing other input. |
| 376 |
return 0; |
| 377 |
} |
| 378 |
|
| 379 |
uint64_t alen = ctx->len.aad + len; |
| 380 |
if (alen > (UINT64_C(1) << 61) || (sizeof(len) == 8 && alen < len)) { |
| 381 |
return 0; |
| 382 |
} |
| 383 |
ctx->len.aad = alen; |
| 384 |
|
| 385 |
unsigned n = ctx->ares; |
| 386 |
if (n) { |
| 387 |
while (n && len) { |
| 388 |
ctx->Xi[n] ^= *(aad++); |
| 389 |
--len; |
| 390 |
n = (n + 1) % 16; |
| 391 |
} |
| 392 |
if (n == 0) { |
| 393 |
GCM_MUL(ctx, Xi); |
| 394 |
} else { |
| 395 |
ctx->ares = n; |
| 396 |
return 1; |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
// Process a whole number of blocks. |
| 401 |
size_t len_blocks = len & kSizeTWithoutLower4Bits; |
| 402 |
if (len_blocks != 0) { |
| 403 |
GHASH(ctx, aad, len_blocks); |
| 404 |
aad += len_blocks; |
| 405 |
len -= len_blocks; |
| 406 |
} |
| 407 |
|
| 408 |
// Process the remainder. |
| 409 |
if (len != 0) { |
| 410 |
// This is needed to avoid a compiler warning on powerpc64le using GCC 12.2: |
| 411 |
// .../aws-lc/crypto/fipsmodule/modes/gcm.c:428:18: error: writing 1 byte into |
| 412 |
// a region of size 0 [-Werror=stringop-overflow=] |
| 413 |
// 428 | ctx->Xi[i] ^= aad[i]; |
| 414 |
// | ~~~~~~~~~~~^~~~~~~~~ |
| 415 |
if (len > 16) { |
| 416 |
abort(); |
| 417 |
return 0; |
| 418 |
} |
| 419 |
n = (unsigned int)len; |
| 420 |
for (size_t i = 0; i < len; ++i) { |
| 421 |
ctx->Xi[i] ^= aad[i]; |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
ctx->ares = n; |
| 426 |
return 1; |
| 427 |
} |
| 428 |
|
| 429 |
int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx, const AES_KEY *key, |
| 430 |
const uint8_t *in, uint8_t *out, size_t len) { |
| 431 |
block128_f block = ctx->gcm_key.block; |
| 432 |
#ifdef GCM_FUNCREF |
| 433 |
void (*gcm_gmult_p)(uint8_t Xi[16], const u128 Htable[16]) = |
| 434 |
ctx->gcm_key.gmult; |
| 435 |
void (*gcm_ghash_p)(uint8_t Xi[16], const u128 Htable[16], const uint8_t *inp, |
| 436 |
size_t len) = ctx->gcm_key.ghash; |
| 437 |
#endif |
| 438 |
|
| 439 |
uint64_t mlen = ctx->len.msg + len; |
| 440 |
if (mlen > ((UINT64_C(1) << 36) - 32) || |
| 441 |
(sizeof(len) == 8 && mlen < len)) { |
| 442 |
return 0; |
| 443 |
} |
| 444 |
ctx->len.msg = mlen; |
| 445 |
|
| 446 |
if (ctx->ares) { |
| 447 |
// First call to encrypt finalizes GHASH(AAD) |
| 448 |
GCM_MUL(ctx, Xi); |
| 449 |
ctx->ares = 0; |
| 450 |
} |
| 451 |
|
| 452 |
unsigned n = ctx->mres; |
| 453 |
if (n) { |
| 454 |
while (n && len) { |
| 455 |
ctx->Xi[n] ^= *(out++) = *(in++) ^ ctx->EKi[n]; |
| 456 |
--len; |
| 457 |
n = (n + 1) % 16; |
| 458 |
} |
| 459 |
if (n == 0) { |
| 460 |
GCM_MUL(ctx, Xi); |
| 461 |
} else { |
| 462 |
ctx->mres = n; |
| 463 |
return 1; |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
uint32_t ctr = CRYPTO_load_u32_be(ctx->Yi + 12); |
| 468 |
while (len >= GHASH_CHUNK) { |
| 469 |
size_t j = GHASH_CHUNK; |
| 470 |
|
| 471 |
while (j) { |
| 472 |
(*block)(ctx->Yi, ctx->EKi, key); |
| 473 |
++ctr; |
| 474 |
CRYPTO_store_u32_be(ctx->Yi + 12, ctr); |
| 475 |
CRYPTO_xor16(out, in, ctx->EKi); |
| 476 |
out += 16; |
| 477 |
in += 16; |
| 478 |
j -= 16; |
| 479 |
} |
| 480 |
GHASH(ctx, out - GHASH_CHUNK, GHASH_CHUNK); |
| 481 |
len -= GHASH_CHUNK; |
| 482 |
} |
| 483 |
size_t len_blocks = len & kSizeTWithoutLower4Bits; |
| 484 |
if (len_blocks != 0) { |
| 485 |
while (len >= 16) { |
| 486 |
(*block)(ctx->Yi, ctx->EKi, key); |
| 487 |
++ctr; |
| 488 |
CRYPTO_store_u32_be(ctx->Yi + 12, ctr); |
| 489 |
CRYPTO_xor16(out, in, ctx->EKi); |
| 490 |
out += 16; |
| 491 |
in += 16; |
| 492 |
len -= 16; |
| 493 |
} |
| 494 |
GHASH(ctx, out - len_blocks, len_blocks); |
| 495 |
} |
| 496 |
if (len) { |
| 497 |
(*block)(ctx->Yi, ctx->EKi, key); |
| 498 |
++ctr; |
| 499 |
CRYPTO_store_u32_be(ctx->Yi + 12, ctr); |
| 500 |
while (len--) { |
| 501 |
ctx->Xi[n] ^= out[n] = in[n] ^ ctx->EKi[n]; |
| 502 |
++n; |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
ctx->mres = n; |
| 507 |
return 1; |
| 508 |
} |
| 509 |
|
| 510 |
int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx, const AES_KEY *key, |
| 511 |
const unsigned char *in, unsigned char *out, |
| 512 |
size_t len) { |
| 513 |
block128_f block = ctx->gcm_key.block; |
| 514 |
#ifdef GCM_FUNCREF |
| 515 |
void (*gcm_gmult_p)(uint8_t Xi[16], const u128 Htable[16]) = |
| 516 |
ctx->gcm_key.gmult; |
| 517 |
void (*gcm_ghash_p)(uint8_t Xi[16], const u128 Htable[16], const uint8_t *inp, |
| 518 |
size_t len) = ctx->gcm_key.ghash; |
| 519 |
#endif |
| 520 |
|
| 521 |
uint64_t mlen = ctx->len.msg + len; |
| 522 |
if (mlen > ((UINT64_C(1) << 36) - 32) || |
| 523 |
(sizeof(len) == 8 && mlen < len)) { |
| 524 |
return 0; |
| 525 |
} |
| 526 |
ctx->len.msg = mlen; |
| 527 |
|
| 528 |
if (ctx->ares) { |
| 529 |
// First call to decrypt finalizes GHASH(AAD) |
| 530 |
GCM_MUL(ctx, Xi); |
| 531 |
ctx->ares = 0; |
| 532 |
} |
| 533 |
|
| 534 |
unsigned n = ctx->mres; |
| 535 |
if (n) { |
| 536 |
while (n && len) { |
| 537 |
uint8_t c = *(in++); |
| 538 |
*(out++) = c ^ ctx->EKi[n]; |
| 539 |
ctx->Xi[n] ^= c; |
| 540 |
--len; |
| 541 |
n = (n + 1) % 16; |
| 542 |
} |
| 543 |
if (n == 0) { |
| 544 |
GCM_MUL(ctx, Xi); |
| 545 |
} else { |
| 546 |
ctx->mres = n; |
| 547 |
return 1; |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
uint32_t ctr = CRYPTO_load_u32_be(ctx->Yi + 12); |
| 552 |
while (len >= GHASH_CHUNK) { |
| 553 |
size_t j = GHASH_CHUNK; |
| 554 |
|
| 555 |
GHASH(ctx, in, GHASH_CHUNK); |
| 556 |
while (j) { |
| 557 |
(*block)(ctx->Yi, ctx->EKi, key); |
| 558 |
++ctr; |
| 559 |
CRYPTO_store_u32_be(ctx->Yi + 12, ctr); |
| 560 |
CRYPTO_xor16(out, in, ctx->EKi); |
| 561 |
out += 16; |
| 562 |
in += 16; |
| 563 |
j -= 16; |
| 564 |
} |
| 565 |
len -= GHASH_CHUNK; |
| 566 |
} |
| 567 |
size_t len_blocks = len & kSizeTWithoutLower4Bits; |
| 568 |
if (len_blocks != 0) { |
| 569 |
GHASH(ctx, in, len_blocks); |
| 570 |
while (len >= 16) { |
| 571 |
(*block)(ctx->Yi, ctx->EKi, key); |
| 572 |
++ctr; |
| 573 |
CRYPTO_store_u32_be(ctx->Yi + 12, ctr); |
| 574 |
CRYPTO_xor16(out, in, ctx->EKi); |
| 575 |
out += 16; |
| 576 |
in += 16; |
| 577 |
len -= 16; |
| 578 |
} |
| 579 |
} |
| 580 |
if (len) { |
| 581 |
(*block)(ctx->Yi, ctx->EKi, key); |
| 582 |
++ctr; |
| 583 |
CRYPTO_store_u32_be(ctx->Yi + 12, ctr); |
| 584 |
while (len--) { |
| 585 |
uint8_t c = in[n]; |
| 586 |
ctx->Xi[n] ^= c; |
| 587 |
out[n] = c ^ ctx->EKi[n]; |
| 588 |
++n; |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
ctx->mres = n; |
| 593 |
return 1; |
| 594 |
} |
| 595 |
|
| 596 |
int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx, const AES_KEY *key, |
| 597 |
const uint8_t *in, uint8_t *out, size_t len, |
| 598 |
ctr128_f stream) { |
| 599 |
#ifdef GCM_FUNCREF |
| 600 |
void (*gcm_gmult_p)(uint8_t Xi[16], const u128 Htable[16]) = |
| 601 |
ctx->gcm_key.gmult; |
| 602 |
void (*gcm_ghash_p)(uint8_t Xi[16], const u128 Htable[16], const uint8_t *inp, |
| 603 |
size_t len) = ctx->gcm_key.ghash; |
| 604 |
#endif |
| 605 |
|
| 606 |
uint64_t mlen = ctx->len.msg + len; |
| 607 |
if (mlen > ((UINT64_C(1) << 36) - 32) || |
| 608 |
(sizeof(len) == 8 && mlen < len)) { |
| 609 |
return 0; |
| 610 |
} |
| 611 |
ctx->len.msg = mlen; |
| 612 |
|
| 613 |
if (ctx->ares) { |
| 614 |
// First call to encrypt finalizes GHASH(AAD) |
| 615 |
GCM_MUL(ctx, Xi); |
| 616 |
ctx->ares = 0; |
| 617 |
} |
| 618 |
|
| 619 |
#if defined(GHASH_ASM_X86_64) && !defined(MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX) |
| 620 |
if (ctx->gcm_key.use_hw_gcm_crypt && crypto_gcm_avx512_enabled() && len > 0) { |
| 621 |
aes_gcm_encrypt_avx512(key, ctx, &ctx->mres, in, len, out); |
| 622 |
return 1; |
| 623 |
} |
| 624 |
#endif |
| 625 |
|
| 626 |
unsigned n = ctx->mres; |
| 627 |
if (n) { |
| 628 |
while (n && len) { |
| 629 |
ctx->Xi[n] ^= *(out++) = *(in++) ^ ctx->EKi[n]; |
| 630 |
--len; |
| 631 |
n = (n + 1) % 16; |
| 632 |
} |
| 633 |
if (n == 0) { |
| 634 |
GCM_MUL(ctx, Xi); |
| 635 |
} else { |
| 636 |
ctx->mres = n; |
| 637 |
return 1; |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
#if defined(HW_GCM) |
| 642 |
// Check |len| to work around a C language bug. See https://crbug.com/1019588. |
| 643 |
if (ctx->gcm_key.use_hw_gcm_crypt && len > 0) { |
| 644 |
// |hw_gcm_encrypt| may not process all the input given to it. It may |
| 645 |
// not process *any* of its input if it is deemed too small. |
| 646 |
size_t bulk = hw_gcm_encrypt(in, out, len, key, ctx->Yi, ctx->Xi, |
| 647 |
ctx->gcm_key.Htable); |
| 648 |
in += bulk; |
| 649 |
out += bulk; |
| 650 |
len -= bulk; |
| 651 |
} |
| 652 |
#endif |
| 653 |
|
| 654 |
uint32_t ctr = CRYPTO_load_u32_be(ctx->Yi + 12); |
| 655 |
while (len >= GHASH_CHUNK) { |
| 656 |
(*stream)(in, out, GHASH_CHUNK / 16, key, ctx->Yi); |
| 657 |
ctr += GHASH_CHUNK / 16; |
| 658 |
CRYPTO_store_u32_be(ctx->Yi + 12, ctr); |
| 659 |
GHASH(ctx, out, GHASH_CHUNK); |
| 660 |
out += GHASH_CHUNK; |
| 661 |
in += GHASH_CHUNK; |
| 662 |
len -= GHASH_CHUNK; |
| 663 |
} |
| 664 |
size_t len_blocks = len & kSizeTWithoutLower4Bits; |
| 665 |
if (len_blocks != 0) { |
| 666 |
size_t j = len_blocks / 16; |
| 667 |
|
| 668 |
(*stream)(in, out, j, key, ctx->Yi); |
| 669 |
ctr += (unsigned int)j; |
| 670 |
CRYPTO_store_u32_be(ctx->Yi + 12, ctr); |
| 671 |
in += len_blocks; |
| 672 |
len -= len_blocks; |
| 673 |
GHASH(ctx, out, len_blocks); |
| 674 |
out += len_blocks; |
| 675 |
} |
| 676 |
if (len) { |
| 677 |
(*ctx->gcm_key.block)(ctx->Yi, ctx->EKi, key); |
| 678 |
++ctr; |
| 679 |
CRYPTO_store_u32_be(ctx->Yi + 12, ctr); |
| 680 |
// This is needed to avoid a compiler warning on powerpc64le using GCC 12.2: |
| 681 |
// .../aws-lc/crypto/fipsmodule/modes/gcm.c:688:18: error: writing 1 byte into a region of size 0 [-Werror=stringop-overflow=] |
| 682 |
// 688 | ctx->Xi[n] ^= out[n] = in[n] ^ ctx->EKi[n]; |
| 683 |
// | ^~ |
| 684 |
if ((n + len) > 16) { |
| 685 |
abort(); |
| 686 |
return 0; |
| 687 |
} |
| 688 |
while (len--) { |
| 689 |
ctx->Xi[n] ^= out[n] = in[n] ^ ctx->EKi[n]; |
| 690 |
++n; |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
ctx->mres = n; |
| 695 |
return 1; |
| 696 |
} |
| 697 |
|
| 698 |
int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx, const AES_KEY *key, |
| 699 |
const uint8_t *in, uint8_t *out, size_t len, |
| 700 |
ctr128_f stream) { |
| 701 |
#ifdef GCM_FUNCREF |
| 702 |
void (*gcm_gmult_p)(uint8_t Xi[16], const u128 Htable[16]) = |
| 703 |
ctx->gcm_key.gmult; |
| 704 |
void (*gcm_ghash_p)(uint8_t Xi[16], const u128 Htable[16], const uint8_t *inp, |
| 705 |
size_t len) = ctx->gcm_key.ghash; |
| 706 |
#endif |
| 707 |
|
| 708 |
uint64_t mlen = ctx->len.msg + len; |
| 709 |
if (mlen > ((UINT64_C(1) << 36) - 32) || |
| 710 |
(sizeof(len) == 8 && mlen < len)) { |
| 711 |
return 0; |
| 712 |
} |
| 713 |
ctx->len.msg = mlen; |
| 714 |
|
| 715 |
if (ctx->ares) { |
| 716 |
// First call to decrypt finalizes GHASH(AAD) |
| 717 |
GCM_MUL(ctx, Xi); |
| 718 |
ctx->ares = 0; |
| 719 |
} |
| 720 |
|
| 721 |
#if defined(GHASH_ASM_X86_64) && !defined(MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX) |
| 722 |
if (ctx->gcm_key.use_hw_gcm_crypt && crypto_gcm_avx512_enabled() && len > 0) { |
| 723 |
aes_gcm_decrypt_avx512(key, ctx, &ctx->mres, in, len, out); |
| 724 |
return 1; |
| 725 |
} |
| 726 |
#endif |
| 727 |
|
| 728 |
unsigned n = ctx->mres; |
| 729 |
if (n) { |
| 730 |
while (n && len) { |
| 731 |
uint8_t c = *(in++); |
| 732 |
*(out++) = c ^ ctx->EKi[n]; |
| 733 |
ctx->Xi[n] ^= c; |
| 734 |
--len; |
| 735 |
n = (n + 1) % 16; |
| 736 |
} |
| 737 |
if (n == 0) { |
| 738 |
GCM_MUL(ctx, Xi); |
| 739 |
} else { |
| 740 |
ctx->mres = n; |
| 741 |
return 1; |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
#if defined(HW_GCM) |
| 746 |
// Check |len| to work around a C language bug. See https://crbug.com/1019588. |
| 747 |
if (ctx->gcm_key.use_hw_gcm_crypt && len > 0) { |
| 748 |
// |hw_gcm_decrypt| may not process all the input given to it. It may |
| 749 |
// not process *any* of its input if it is deemed too small. |
| 750 |
size_t bulk = hw_gcm_decrypt(in, out, len, key, ctx->Yi, ctx->Xi, |
| 751 |
ctx->gcm_key.Htable); |
| 752 |
in += bulk; |
| 753 |
out += bulk; |
| 754 |
len -= bulk; |
| 755 |
} |
| 756 |
#endif |
| 757 |
|
| 758 |
uint32_t ctr = CRYPTO_load_u32_be(ctx->Yi + 12); |
| 759 |
while (len >= GHASH_CHUNK) { |
| 760 |
GHASH(ctx, in, GHASH_CHUNK); |
| 761 |
(*stream)(in, out, GHASH_CHUNK / 16, key, ctx->Yi); |
| 762 |
ctr += GHASH_CHUNK / 16; |
| 763 |
CRYPTO_store_u32_be(ctx->Yi + 12, ctr); |
| 764 |
out += GHASH_CHUNK; |
| 765 |
in += GHASH_CHUNK; |
| 766 |
len -= GHASH_CHUNK; |
| 767 |
} |
| 768 |
size_t len_blocks = len & kSizeTWithoutLower4Bits; |
| 769 |
if (len_blocks != 0) { |
| 770 |
size_t j = len_blocks / 16; |
| 771 |
|
| 772 |
GHASH(ctx, in, len_blocks); |
| 773 |
(*stream)(in, out, j, key, ctx->Yi); |
| 774 |
ctr += (unsigned int)j; |
| 775 |
CRYPTO_store_u32_be(ctx->Yi + 12, ctr); |
| 776 |
out += len_blocks; |
| 777 |
in += len_blocks; |
| 778 |
len -= len_blocks; |
| 779 |
} |
| 780 |
if (len) { |
| 781 |
(*ctx->gcm_key.block)(ctx->Yi, ctx->EKi, key); |
| 782 |
++ctr; |
| 783 |
CRYPTO_store_u32_be(ctx->Yi + 12, ctr); |
| 784 |
// This is needed to avoid a compiler warning on powerpc64le using GCC 12.2: |
| 785 |
// aws-lc/crypto/fipsmodule/modes/gcm.c:785:18: error: writing 1 byte into a |
| 786 |
// region of size 0 [-Werror=stringop-overflow=] |
| 787 |
// 785 | ctx->Xi[n] ^= c; |
| 788 |
// | ~~~~~~~~~~~^~~~ |
| 789 |
if ((n + len) > 16) { |
| 790 |
abort(); |
| 791 |
return 0; |
| 792 |
} |
| 793 |
while (len--) { |
| 794 |
uint8_t c = in[n]; |
| 795 |
ctx->Xi[n] ^= c; |
| 796 |
out[n] = c ^ ctx->EKi[n]; |
| 797 |
++n; |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
ctx->mres = n; |
| 802 |
return 1; |
| 803 |
} |
| 804 |
|
| 805 |
int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const uint8_t *tag, size_t len) { |
| 806 |
#ifdef GCM_FUNCREF |
| 807 |
void (*gcm_gmult_p)(uint8_t Xi[16], const u128 Htable[16]) = |
| 808 |
ctx->gcm_key.gmult; |
| 809 |
#endif |
| 810 |
|
| 811 |
if (ctx->mres || ctx->ares) { |
| 812 |
GCM_MUL(ctx, Xi); |
| 813 |
} |
| 814 |
|
| 815 |
uint8_t len_block[16]; |
| 816 |
CRYPTO_store_u64_be(len_block, ctx->len.aad << 3); |
| 817 |
CRYPTO_store_u64_be(len_block + 8, ctx->len.msg << 3); |
| 818 |
CRYPTO_xor16(ctx->Xi, ctx->Xi, len_block); |
| 819 |
GCM_MUL(ctx, Xi); |
| 820 |
CRYPTO_xor16(ctx->Xi, ctx->Xi, ctx->EK0); |
| 821 |
|
| 822 |
if (tag && len <= sizeof(ctx->Xi)) { |
| 823 |
return CRYPTO_memcmp(ctx->Xi, tag, len) == 0; |
| 824 |
} else { |
| 825 |
return 0; |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, unsigned char *tag, size_t len) { |
| 830 |
CRYPTO_gcm128_finish(ctx, NULL, 0); |
| 831 |
OPENSSL_memcpy(tag, ctx->Xi, len <= sizeof(ctx->Xi) ? len : sizeof(ctx->Xi)); |
| 832 |
} |
| 833 |
|
| 834 |
#if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) |
| 835 |
int crypto_gcm_clmul_enabled(void) { |
| 836 |
#if defined(GHASH_ASM_X86) || defined(GHASH_ASM_X86_64) |
| 837 |
return CRYPTO_is_FXSR_capable() && CRYPTO_is_PCLMUL_capable(); |
| 838 |
#else |
| 839 |
return 0; |
| 840 |
#endif |
| 841 |
} |
| 842 |
|
| 843 |
int crypto_gcm_avx512_enabled(void) { |
| 844 |
// This must align with ImplDispatchTest.AEAD_AES_GCM |
| 845 |
#if defined(GHASH_ASM_X86_64) && \ |
| 846 |
!defined(OPENSSL_WINDOWS) && \ |
| 847 |
!defined(MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX) |
| 848 |
// TODO(awslc): remove the Windows guard once CryptoAlg-1701 is resolved. |
| 849 |
return (CRYPTO_is_VAES_capable() && |
| 850 |
CRYPTO_is_AVX512_capable() && |
| 851 |
CRYPTO_is_VPCLMULQDQ_capable()); |
| 852 |
#else |
| 853 |
return 0; |
| 854 |
#endif |
| 855 |
} |
| 856 |
#endif |