| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/hmac.h> |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include <openssl/digest.h> |
| 10 |
#include <openssl/mem.h> |
| 11 |
|
| 12 |
#include "internal.h" |
| 13 |
#include "../../internal.h" |
| 14 |
#include "../service_indicator/internal.h" |
| 15 |
|
| 16 |
#include "../md5/internal.h" |
| 17 |
#include "../sha/internal.h" |
| 18 |
|
| 19 |
typedef int (*HashInit)(void *); |
| 20 |
typedef int (*HashUpdate)(void *, const void *, size_t); |
| 21 |
typedef int (*HashFinal)(uint8_t *, void *); |
| 22 |
typedef int (*HashInitFromState)(void *, const uint8_t *, uint64_t); |
| 23 |
typedef int (*HashGetState)(void *, uint8_t *, uint64_t *); |
| 24 |
|
| 25 |
struct hmac_methods_st { |
| 26 |
const EVP_MD* evp_md; |
| 27 |
size_t chaining_length; // chaining length in bytes |
| 28 |
HashInit init; |
| 29 |
HashUpdate update; |
| 30 |
HashFinal finalize; // Not named final to avoid keywords |
| 31 |
HashInitFromState init_from_state; |
| 32 |
HashGetState get_state; |
| 33 |
}; |
| 34 |
|
| 35 |
// We need trampolines from the generic void* methods we use to the properly typed underlying methods. |
| 36 |
// Without these methods some control flow integrity checks will fail because the function pointer types |
| 37 |
// do not exactly match the destination functions. (Namely function pointers use void* pointers for the contexts) |
| 38 |
// while the destination functions have specific pointer types for the relevant contexts. |
| 39 |
// |
| 40 |
// This also includes hash-specific static assertions as they can be added. |
| 41 |
#define MD_TRAMPOLINES_EXPLICIT(HASH_NAME, HASH_CTX, HASH_CBLOCK) \ |
| 42 |
static int AWS_LC_TRAMPOLINE_##HASH_NAME##_Init(void *); \ |
| 43 |
static int AWS_LC_TRAMPOLINE_##HASH_NAME##_Update(void *, const void *, \ |
| 44 |
size_t); \ |
| 45 |
static int AWS_LC_TRAMPOLINE_##HASH_NAME##_Final(uint8_t *, void *); \ |
| 46 |
static int AWS_LC_TRAMPOLINE_##HASH_NAME##_Init(void *ctx) { \ |
| 47 |
return HASH_NAME##_Init((HASH_CTX *)ctx); \ |
| 48 |
} \ |
| 49 |
static int AWS_LC_TRAMPOLINE_##HASH_NAME##_Update( \ |
| 50 |
void *ctx, const void *key, size_t key_len) { \ |
| 51 |
return HASH_NAME##_Update((HASH_CTX *)ctx, key, key_len); \ |
| 52 |
} \ |
| 53 |
static int AWS_LC_TRAMPOLINE_##HASH_NAME##_Final(uint8_t *out, void *ctx) { \ |
| 54 |
return HASH_NAME##_Final(out, (HASH_CTX *)ctx); \ |
| 55 |
} \ |
| 56 |
OPENSSL_STATIC_ASSERT(HASH_CBLOCK % 8 == 0, \ |
| 57 |
HASH_NAME##_has_blocksize_not_divisible_by_eight_t) \ |
| 58 |
OPENSSL_STATIC_ASSERT(HASH_CBLOCK <= EVP_MAX_MD_BLOCK_SIZE, \ |
| 59 |
HASH_NAME##_has_overlarge_blocksize_t) \ |
| 60 |
OPENSSL_STATIC_ASSERT(sizeof(HASH_CTX) <= sizeof(union md_ctx_union), \ |
| 61 |
HASH_NAME##_has_overlarge_context_t) |
| 62 |
|
| 63 |
// For merkle-damgard constructions, we also define functions for importing and |
| 64 |
// exporting hash state for precomputed keys. These are not applicable to |
| 65 |
// Keccak/SHA3. |
| 66 |
#define MD_TRAMPOLINES_EXPLICIT_PRECOMPUTED(HASH_NAME, HASH_CTX, HASH_CBLOCK) \ |
| 67 |
MD_TRAMPOLINES_EXPLICIT(HASH_NAME, HASH_CTX, HASH_CBLOCK); \ |
| 68 |
static int AWS_LC_TRAMPOLINE_##HASH_NAME##_Init_from_state( \ |
| 69 |
void *ctx, const uint8_t *h, uint64_t n) { \ |
| 70 |
return HASH_NAME##_Init_from_state((HASH_CTX *)ctx, h, n); \ |
| 71 |
} \ |
| 72 |
static int AWS_LC_TRAMPOLINE_##HASH_NAME##_get_state( \ |
| 73 |
void *ctx, uint8_t *out_h, uint64_t *out_n) { \ |
| 74 |
return HASH_NAME##_get_state((HASH_CTX *)ctx, out_h, out_n); \ |
| 75 |
} \ |
| 76 |
OPENSSL_STATIC_ASSERT(HMAC_##HASH_NAME##_PRECOMPUTED_KEY_SIZE == \ |
| 77 |
2 * HASH_NAME##_CHAINING_LENGTH, \ |
| 78 |
HASH_NAME##_has_incorrect_precomputed_key_size) \ |
| 79 |
OPENSSL_STATIC_ASSERT(HMAC_##HASH_NAME##_PRECOMPUTED_KEY_SIZE <= \ |
| 80 |
HMAC_MAX_PRECOMPUTED_KEY_SIZE, \ |
| 81 |
HASH_NAME##_has_too_large_precomputed_key_size) \ |
| 82 |
|
| 83 |
// The maximum number of HMAC implementations |
| 84 |
#define HMAC_METHOD_MAX 12 |
| 85 |
|
| 86 |
MD_TRAMPOLINES_EXPLICIT_PRECOMPUTED(MD5, MD5_CTX, MD5_CBLOCK) |
| 87 |
MD_TRAMPOLINES_EXPLICIT_PRECOMPUTED(SHA1, SHA_CTX, SHA_CBLOCK) |
| 88 |
MD_TRAMPOLINES_EXPLICIT_PRECOMPUTED(SHA224, SHA256_CTX, SHA256_CBLOCK) |
| 89 |
MD_TRAMPOLINES_EXPLICIT_PRECOMPUTED(SHA256, SHA256_CTX, SHA256_CBLOCK) |
| 90 |
MD_TRAMPOLINES_EXPLICIT_PRECOMPUTED(SHA384, SHA512_CTX, SHA512_CBLOCK) |
| 91 |
MD_TRAMPOLINES_EXPLICIT_PRECOMPUTED(SHA512, SHA512_CTX, SHA512_CBLOCK) |
| 92 |
MD_TRAMPOLINES_EXPLICIT_PRECOMPUTED(SHA512_224, SHA512_CTX, SHA512_CBLOCK) |
| 93 |
MD_TRAMPOLINES_EXPLICIT_PRECOMPUTED(SHA512_256, SHA512_CTX, SHA512_CBLOCK) |
| 94 |
MD_TRAMPOLINES_EXPLICIT(SHA3_224, KECCAK1600_CTX, SHA3_224_CBLOCK) |
| 95 |
MD_TRAMPOLINES_EXPLICIT(SHA3_256, KECCAK1600_CTX, SHA3_256_CBLOCK) |
| 96 |
MD_TRAMPOLINES_EXPLICIT(SHA3_384, KECCAK1600_CTX, SHA3_384_CBLOCK) |
| 97 |
MD_TRAMPOLINES_EXPLICIT(SHA3_512, KECCAK1600_CTX, SHA3_512_CBLOCK) |
| 98 |
|
| 99 |
struct hmac_method_array_st { |
| 100 |
HmacMethods methods[HMAC_METHOD_MAX]; |
| 101 |
}; |
| 102 |
|
| 103 |
// This macro does not set any values for precomputed keys for portable state, |
| 104 |
// and as such is suitable for use with Keccak/SHA3. |
| 105 |
#define DEFINE_IN_PLACE_METHODS(EVP_MD, HASH_NAME) { \ |
| 106 |
out->methods[idx].evp_md = EVP_MD; \ |
| 107 |
out->methods[idx].init = AWS_LC_TRAMPOLINE_##HASH_NAME##_Init; \ |
| 108 |
out->methods[idx].update = AWS_LC_TRAMPOLINE_##HASH_NAME##_Update; \ |
| 109 |
out->methods[idx].finalize = AWS_LC_TRAMPOLINE_##HASH_NAME##_Final; \ |
| 110 |
out->methods[idx].chaining_length = 0UL; \ |
| 111 |
out->methods[idx].init_from_state = NULL; \ |
| 112 |
out->methods[idx].get_state = NULL; \ |
| 113 |
idx++; \ |
| 114 |
assert(idx <= HMAC_METHOD_MAX); \ |
| 115 |
} |
| 116 |
|
| 117 |
// Use |idx-1| because DEFINE_IN_PLACE_METHODS has already incremented it. |
| 118 |
#define DEFINE_IN_PLACE_METHODS_PRECOMPUTED(EVP_MD, HASH_NAME) { \ |
| 119 |
DEFINE_IN_PLACE_METHODS(EVP_MD, HASH_NAME); \ |
| 120 |
assert(idx-1 >= 0); \ |
| 121 |
out->methods[idx-1].chaining_length = HASH_NAME##_CHAINING_LENGTH; \ |
| 122 |
out->methods[idx-1].init_from_state = \ |
| 123 |
AWS_LC_TRAMPOLINE_##HASH_NAME##_Init_from_state; \ |
| 124 |
out->methods[idx-1].get_state = \ |
| 125 |
AWS_LC_TRAMPOLINE_##HASH_NAME##_get_state; \ |
| 126 |
} |
| 127 |
|
| 128 |
DEFINE_LOCAL_DATA(struct hmac_method_array_st, AWSLC_hmac_in_place_methods) { |
| 129 |
OPENSSL_memset((void*) out->methods, 0, sizeof(out->methods)); |
| 130 |
int idx = 0; |
| 131 |
// Since we search these linearly it helps (just a bit) to put the most common ones first. |
| 132 |
// This isn't based on hard metrics and will not make a significant different on performance. |
| 133 |
DEFINE_IN_PLACE_METHODS_PRECOMPUTED(EVP_sha256(), SHA256); |
| 134 |
DEFINE_IN_PLACE_METHODS_PRECOMPUTED(EVP_sha1(), SHA1); |
| 135 |
DEFINE_IN_PLACE_METHODS_PRECOMPUTED(EVP_sha384(), SHA384); |
| 136 |
DEFINE_IN_PLACE_METHODS_PRECOMPUTED(EVP_sha512(), SHA512); |
| 137 |
DEFINE_IN_PLACE_METHODS_PRECOMPUTED(EVP_md5(), MD5); |
| 138 |
DEFINE_IN_PLACE_METHODS_PRECOMPUTED(EVP_sha224(), SHA224); |
| 139 |
DEFINE_IN_PLACE_METHODS_PRECOMPUTED(EVP_sha512_224(), SHA512_224); |
| 140 |
DEFINE_IN_PLACE_METHODS_PRECOMPUTED(EVP_sha512_256(), SHA512_256); |
| 141 |
DEFINE_IN_PLACE_METHODS(EVP_sha3_224(), SHA3_224); |
| 142 |
DEFINE_IN_PLACE_METHODS(EVP_sha3_256(), SHA3_256); |
| 143 |
DEFINE_IN_PLACE_METHODS(EVP_sha3_384(), SHA3_384); |
| 144 |
DEFINE_IN_PLACE_METHODS(EVP_sha3_512(), SHA3_512); |
| 145 |
} |
| 146 |
|
| 147 |
static const HmacMethods *GetInPlaceMethods(const EVP_MD *evp_md) { |
| 148 |
const struct hmac_method_array_st *method_array = AWSLC_hmac_in_place_methods(); |
| 149 |
const HmacMethods *methods = method_array->methods; |
| 150 |
for (size_t idx = 0; idx < sizeof(method_array->methods) / sizeof(struct hmac_methods_st); idx++) { |
| 151 |
if (methods[idx].evp_md == evp_md) { |
| 152 |
return &methods[idx]; |
| 153 |
} |
| 154 |
} |
| 155 |
return NULL; |
| 156 |
} |
| 157 |
|
| 158 |
// ctx->state has the following possible states |
| 159 |
// (Pre/Post conditions): |
| 160 |
// HMAC_STATE_UNINITIALIZED: Uninitialized. |
| 161 |
// HMAC_STATE_INIT_NO_DATA: Initialized with an md and key. No data processed. |
| 162 |
// This means that if init is called but nothing changes, we don't need to reset our state. |
| 163 |
// HMAC_STATE_IN_PROGRESS: Initialized with an md and key. Data processed. |
| 164 |
// This means that if init is called we do need to reset state. |
| 165 |
// HMAC_STATE_READY_NEEDS_INIT: Identical to state HMAC_STATE_INIT_NO_DATA but API contract requires that Init be called prior to use. |
| 166 |
// This is an optimization because we can leave the context in a state ready for use after completion. |
| 167 |
// HMAC_STATE_PRECOMPUTED_KEY_EXPORT_READY: Identical to state HMAC_STATE_READY_NEEDS_INIT but marked to allow precompute key export |
| 168 |
// This state is treated as HMAC_STATE_READY_NEEDS_INIT by Init/Update/Final. |
| 169 |
// This state is the only state that in which a precompute key can be exported. |
| 170 |
// This state is set by HMAC_set_precomputed_key_export. |
| 171 |
// other: Invalid state and likely a result of using unitialized memory. Treated the same as 0. |
| 172 |
// |
| 173 |
// While we are within HMAC methods we allow for the state value and actual state of the context to diverge. |
| 174 |
|
| 175 |
// HMAC_STATE_UNINITIALIZED *MUST* remain `0` so that callers can do `HMAC_CTX ctx = {0};` to get a usable context. |
| 176 |
#define HMAC_STATE_UNINITIALIZED 0 |
| 177 |
#define HMAC_STATE_INIT_NO_DATA 1 |
| 178 |
#define HMAC_STATE_IN_PROGRESS 2 |
| 179 |
#define HMAC_STATE_READY_NEEDS_INIT 3 |
| 180 |
#define HMAC_STATE_PRECOMPUTED_KEY_EXPORT_READY 4 |
| 181 |
|
| 182 |
// Static assertion to ensure that no one has changed the value of HMAC_STATE_UNINITIALIZED. |
| 183 |
// This really must stay with a zero value. |
| 184 |
OPENSSL_STATIC_ASSERT(HMAC_STATE_UNINITIALIZED == 0, HMAC_STATE_UNINITIALIZED_is_not_zero_t) |
| 185 |
|
| 186 |
// Indicates that a context has the md and methods configured and is ready to use |
| 187 |
#define hmac_ctx_is_initialized(ctx) ((HMAC_STATE_INIT_NO_DATA == (ctx)->state || HMAC_STATE_IN_PROGRESS == (ctx)->state)) |
| 188 |
|
| 189 |
uint8_t *HMAC(const EVP_MD *evp_md, const void *key, size_t key_len, |
| 190 |
const uint8_t *data, size_t data_len, uint8_t *out, |
| 191 |
unsigned int *out_len) { |
| 192 |
|
| 193 |
if (out == NULL) { |
| 194 |
// Prevent further work from being done |
| 195 |
return NULL; |
| 196 |
} |
| 197 |
|
| 198 |
HMAC_CTX ctx; |
| 199 |
OPENSSL_memset(&ctx, 0, sizeof(HMAC_CTX)); |
| 200 |
int result; |
| 201 |
|
| 202 |
// We have to avoid the underlying SHA services updating the indicator |
| 203 |
// state, so we lock the state here. |
| 204 |
FIPS_service_indicator_lock_state(); |
| 205 |
|
| 206 |
result = HMAC_Init_ex(&ctx, key, key_len, evp_md, NULL) && |
| 207 |
HMAC_Update(&ctx, data, data_len) && |
| 208 |
HMAC_Final(&ctx, out, out_len); |
| 209 |
|
| 210 |
FIPS_service_indicator_unlock_state(); |
| 211 |
|
| 212 |
// Regardless of our success we need to zeroize our working state. |
| 213 |
HMAC_CTX_cleanup(&ctx); |
| 214 |
if (result) { |
| 215 |
HMAC_verify_service_indicator(evp_md); |
| 216 |
return out; |
| 217 |
} else { |
| 218 |
OPENSSL_cleanse(out, EVP_MD_size(evp_md)); |
| 219 |
return NULL; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
uint8_t *HMAC_with_precompute(const EVP_MD *evp_md, const void *key, |
| 224 |
size_t key_len, const uint8_t *data, |
| 225 |
size_t data_len, uint8_t *out, |
| 226 |
unsigned int *out_len) { |
| 227 |
if (out == NULL) { |
| 228 |
// Prevent further work from being done |
| 229 |
return NULL; |
| 230 |
} |
| 231 |
|
| 232 |
HMAC_CTX ctx; |
| 233 |
OPENSSL_memset(&ctx, 0, sizeof(HMAC_CTX)); |
| 234 |
int result; |
| 235 |
|
| 236 |
// We have to avoid the underlying SHA services updating the indicator |
| 237 |
// state, so we lock the state here. |
| 238 |
FIPS_service_indicator_lock_state(); |
| 239 |
|
| 240 |
uint8_t precomputed_key[HMAC_MAX_PRECOMPUTED_KEY_SIZE]; |
| 241 |
size_t precomputed_key_len = HMAC_MAX_PRECOMPUTED_KEY_SIZE; |
| 242 |
|
| 243 |
result = |
| 244 |
HMAC_Init_ex(&ctx, key, key_len, evp_md, NULL) && |
| 245 |
HMAC_set_precomputed_key_export(&ctx) && |
| 246 |
HMAC_get_precomputed_key(&ctx, precomputed_key, &precomputed_key_len) && |
| 247 |
HMAC_Init_from_precomputed_key(&ctx, precomputed_key, precomputed_key_len, |
| 248 |
evp_md) && |
| 249 |
HMAC_Update(&ctx, data, data_len) && |
| 250 |
HMAC_Final(&ctx, out, out_len); |
| 251 |
|
| 252 |
FIPS_service_indicator_unlock_state(); |
| 253 |
|
| 254 |
// Regardless of our success we need to zeroize our working state. |
| 255 |
HMAC_CTX_cleanup(&ctx); |
| 256 |
OPENSSL_cleanse(precomputed_key, HMAC_MAX_PRECOMPUTED_KEY_SIZE); |
| 257 |
if (result) { |
| 258 |
// Contrary to what happens in the |HMAC| function, we do not update the |
| 259 |
// service indicator here (i.e., we do not call |
| 260 |
// |HMAC_verify_service_indicator|), because the function |
| 261 |
// |HMAC_with_precompute| is not FIPS-approved per se and is only used in |
| 262 |
// tests. |
| 263 |
return out; |
| 264 |
} else { |
| 265 |
OPENSSL_cleanse(out, EVP_MD_size(evp_md)); |
| 266 |
return NULL; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
void HMAC_CTX_init(HMAC_CTX *ctx) { |
| 271 |
OPENSSL_memset(ctx, 0, sizeof(HMAC_CTX)); |
| 272 |
} |
| 273 |
|
| 274 |
HMAC_CTX *HMAC_CTX_new(void) { |
| 275 |
HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(HMAC_CTX)); |
| 276 |
if (ctx != NULL) { |
| 277 |
// NO-OP: struct already zeroed |
| 278 |
//HMAC_CTX_init(ctx); |
| 279 |
} |
| 280 |
return ctx; |
| 281 |
} |
| 282 |
|
| 283 |
void HMAC_CTX_cleanup(HMAC_CTX *ctx) { |
| 284 |
// All of the contexts are flat and can simply be zeroed |
| 285 |
OPENSSL_cleanse(ctx, sizeof(HMAC_CTX)); |
| 286 |
} |
| 287 |
|
| 288 |
void HMAC_CTX_cleanse(HMAC_CTX *ctx) { |
| 289 |
HMAC_CTX_cleanup(ctx); |
| 290 |
} |
| 291 |
|
| 292 |
void HMAC_CTX_free(HMAC_CTX *ctx) { |
| 293 |
if (ctx == NULL) { |
| 294 |
return; |
| 295 |
} |
| 296 |
|
| 297 |
HMAC_CTX_cleanup(ctx); |
| 298 |
OPENSSL_free(ctx); |
| 299 |
} |
| 300 |
|
| 301 |
// hmac_ctx_set_md_methods is used to set ctx->methods and ctx->md from md. |
| 302 |
// It is called as part of the initialization of the ctx (HMAC_Init_*). |
| 303 |
// It returns one on success, and zero otherwise. |
| 304 |
static int hmac_ctx_set_md_methods(HMAC_CTX *ctx, const EVP_MD *md) { |
| 305 |
if (md && (HMAC_STATE_UNINITIALIZED == ctx->state || ctx->md != md)) { |
| 306 |
// The MD has changed |
| 307 |
ctx->methods = GetInPlaceMethods(md); |
| 308 |
if (ctx->methods == NULL) { |
| 309 |
// Unsupported md |
| 310 |
return 0; |
| 311 |
} |
| 312 |
ctx->md = md; |
| 313 |
} else if (!hmac_ctx_is_initialized(ctx)) { |
| 314 |
// We are not initialized but have not been provided with an md to |
| 315 |
// initialize ourselves with. |
| 316 |
return 0; |
| 317 |
} |
| 318 |
|
| 319 |
return 1; |
| 320 |
} |
| 321 |
|
| 322 |
int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, size_t key_len, |
| 323 |
const EVP_MD *md, ENGINE *impl) { |
| 324 |
assert(impl == NULL); |
| 325 |
|
| 326 |
GUARD_PTR(ctx); |
| 327 |
|
| 328 |
// HMAC does not support SHAKE (XOF) algorithms |
| 329 |
if (md && (EVP_MD_flags(md) & EVP_MD_FLAG_XOF)) { |
| 330 |
OPENSSL_PUT_ERROR(HMAC, HMAC_R_UNSUPPORTED_DIGEST); |
| 331 |
return 0; |
| 332 |
} |
| 333 |
|
| 334 |
if (HMAC_STATE_READY_NEEDS_INIT == ctx->state || |
| 335 |
HMAC_STATE_PRECOMPUTED_KEY_EXPORT_READY == ctx->state) { |
| 336 |
ctx->state = HMAC_STATE_INIT_NO_DATA; // Mark that init has been called |
| 337 |
} |
| 338 |
|
| 339 |
if (hmac_ctx_is_initialized(ctx)) { |
| 340 |
// TODO(davidben,eroman): Passing the previous |md| with a NULL |key| is |
| 341 |
// ambiguous between using the empty key and reusing the previous key. There |
| 342 |
// exist callers which intend the latter, but the former is an awkward edge |
| 343 |
// case. Fix to API to avoid this. |
| 344 |
if (key == NULL && (md == NULL || md == ctx->md)) { |
| 345 |
if(HMAC_STATE_IN_PROGRESS == ctx->state) { |
| 346 |
// Reinitialize |md_ctx| from |i_ctx| to start fresh with the same key. This |
| 347 |
// is the same behavior as Openssl. |
| 348 |
OPENSSL_memcpy(&ctx->md_ctx, &ctx->i_ctx, sizeof(ctx->i_ctx)); |
| 349 |
} |
| 350 |
// If nothing is changing then we can return without doing any further work. |
| 351 |
ctx->state = HMAC_STATE_INIT_NO_DATA; |
| 352 |
return 1; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
// At this point we *know* we need to change things and rekey because either the key has changed |
| 357 |
// or the md and they key has changed. |
| 358 |
// (It is a misuse to just change the md so we also assume that the key changes when the md changes.) |
| 359 |
|
| 360 |
if (!hmac_ctx_set_md_methods(ctx, md)) { |
| 361 |
return 0; |
| 362 |
} |
| 363 |
|
| 364 |
// At this point we know we have valid methods and a context allocated. |
| 365 |
const HmacMethods *methods = ctx->methods; |
| 366 |
size_t block_size = EVP_MD_block_size(methods->evp_md); |
| 367 |
assert(block_size % 8 == 0); |
| 368 |
assert(block_size <= EVP_MAX_MD_BLOCK_SIZE); |
| 369 |
|
| 370 |
// We have to avoid the underlying SHA services updating the indicator |
| 371 |
// state, so we lock the state here. |
| 372 |
FIPS_service_indicator_lock_state(); |
| 373 |
int result = 0; |
| 374 |
|
| 375 |
uint64_t pad[EVP_MAX_MD_BLOCK_SIZE / sizeof(uint64_t)] = {0}; |
| 376 |
uint64_t key_block[EVP_MAX_MD_BLOCK_SIZE / sizeof(uint64_t)] = {0}; |
| 377 |
if (block_size < key_len) { |
| 378 |
// Long keys are hashed. |
| 379 |
if (!methods->init(&ctx->md_ctx) || |
| 380 |
!methods->update(&ctx->md_ctx, key, key_len) || |
| 381 |
!methods->finalize((uint8_t *) key_block, &ctx->md_ctx)) { |
| 382 |
goto end; |
| 383 |
} |
| 384 |
} else { |
| 385 |
assert(key_len <= sizeof(key_block)); |
| 386 |
OPENSSL_memcpy(key_block, key, key_len); |
| 387 |
} |
| 388 |
for (size_t i = 0; i < block_size / 8; i++) { |
| 389 |
pad[i] = 0x3636363636363636 ^ key_block[i]; |
| 390 |
} |
| 391 |
if (!methods->init(&ctx->i_ctx) || |
| 392 |
!methods->update(&ctx->i_ctx, pad, block_size)) { |
| 393 |
goto end; |
| 394 |
} |
| 395 |
for (size_t i = 0; i < block_size / 8; i++) { |
| 396 |
pad[i] = 0x5c5c5c5c5c5c5c5c ^ key_block[i]; |
| 397 |
} |
| 398 |
if (!methods->init(&ctx->o_ctx) || |
| 399 |
!methods->update(&ctx->o_ctx, pad, block_size)) { |
| 400 |
goto end; |
| 401 |
} |
| 402 |
|
| 403 |
OPENSSL_memcpy(&ctx->md_ctx, &ctx->i_ctx, sizeof(ctx->i_ctx)); |
| 404 |
ctx->state = HMAC_STATE_INIT_NO_DATA; |
| 405 |
|
| 406 |
result = 1; |
| 407 |
end: |
| 408 |
OPENSSL_cleanse(pad, EVP_MAX_MD_BLOCK_SIZE); |
| 409 |
OPENSSL_cleanse(key_block, EVP_MAX_MD_BLOCK_SIZE); |
| 410 |
FIPS_service_indicator_unlock_state(); |
| 411 |
if (result != 1) { |
| 412 |
// We're in some error state, so return our context to a known and well defined zero state. |
| 413 |
HMAC_CTX_cleanup(ctx); |
| 414 |
} |
| 415 |
return result; |
| 416 |
} |
| 417 |
|
| 418 |
int HMAC_Update(HMAC_CTX *ctx, const uint8_t *data, size_t data_len) { |
| 419 |
if (!hmac_ctx_is_initialized(ctx)) { |
| 420 |
return 0; |
| 421 |
} |
| 422 |
ctx->state = HMAC_STATE_IN_PROGRESS; |
| 423 |
return ctx->methods->update(&ctx->md_ctx, data, data_len); |
| 424 |
} |
| 425 |
|
| 426 |
int HMAC_Final(HMAC_CTX *ctx, uint8_t *out, unsigned int *out_len) { |
| 427 |
if (out == NULL) { |
| 428 |
return 0; |
| 429 |
} |
| 430 |
|
| 431 |
const HmacMethods *methods = ctx->methods; |
| 432 |
if (!hmac_ctx_is_initialized(ctx)) { |
| 433 |
return 0; |
| 434 |
} |
| 435 |
// We have to avoid the underlying SHA services updating the indicator |
| 436 |
// state, so we lock the state here. |
| 437 |
FIPS_service_indicator_lock_state(); |
| 438 |
int result = 0; |
| 439 |
const EVP_MD *evp_md = ctx->md; |
| 440 |
int hmac_len = EVP_MD_size(evp_md); |
| 441 |
uint8_t tmp[EVP_MAX_MD_SIZE]; |
| 442 |
if (!methods->finalize(tmp, &ctx->md_ctx)) { |
| 443 |
goto end; |
| 444 |
} |
| 445 |
OPENSSL_memcpy(&ctx->md_ctx, &ctx->o_ctx, sizeof(ctx->o_ctx)); |
| 446 |
if (!ctx->methods->update(&ctx->md_ctx, tmp, hmac_len)) { |
| 447 |
goto end; |
| 448 |
} |
| 449 |
result = methods->finalize(out, &ctx->md_ctx); |
| 450 |
// Wipe out working state by initializing for next use |
| 451 |
OPENSSL_memcpy(&ctx->md_ctx, &ctx->i_ctx, sizeof(ctx->i_ctx)); |
| 452 |
ctx->state = HMAC_STATE_READY_NEEDS_INIT; // Mark that we are ready for use but still need HMAC_Init_ex called. |
| 453 |
end: |
| 454 |
// Cleanse sensitive intermediate inner hash from the stack. |
| 455 |
OPENSSL_cleanse(tmp, sizeof(tmp)); |
| 456 |
FIPS_service_indicator_unlock_state(); |
| 457 |
if (result) { |
| 458 |
HMAC_verify_service_indicator(evp_md); |
| 459 |
if (out_len) { |
| 460 |
*out_len = hmac_len; |
| 461 |
} |
| 462 |
return 1; |
| 463 |
} else { |
| 464 |
// On error, return context to a known and well-defined zero state. |
| 465 |
HMAC_CTX_cleanup(ctx); |
| 466 |
if (out_len) { |
| 467 |
*out_len = 0; |
| 468 |
} |
| 469 |
return 0; |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
size_t HMAC_size(const HMAC_CTX *ctx) { return EVP_MD_size(ctx->md); } |
| 474 |
|
| 475 |
const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx) { return ctx->md; } |
| 476 |
|
| 477 |
int HMAC_CTX_copy_ex(HMAC_CTX *dest, const HMAC_CTX *src) { |
| 478 |
OPENSSL_memcpy(dest, src, sizeof(HMAC_CTX)); |
| 479 |
return 1; |
| 480 |
} |
| 481 |
|
| 482 |
void HMAC_CTX_reset(HMAC_CTX *ctx) { |
| 483 |
HMAC_CTX_cleanup(ctx); |
| 484 |
// Cleanup intrinsicly inits to all zeros which is valid |
| 485 |
} |
| 486 |
|
| 487 |
int HMAC_set_precomputed_key_export(HMAC_CTX *ctx) { |
| 488 |
GUARD_PTR(ctx); |
| 489 |
if (ctx->methods != NULL && ctx->methods->get_state == NULL) { |
| 490 |
OPENSSL_PUT_ERROR(HMAC, HMAC_R_PRECOMPUTED_KEY_NOT_SUPPORTED_FOR_DIGEST); |
| 491 |
return 0; |
| 492 |
} |
| 493 |
if (HMAC_STATE_INIT_NO_DATA != ctx->state && |
| 494 |
HMAC_STATE_PRECOMPUTED_KEY_EXPORT_READY != ctx->state) { |
| 495 |
// HMAC_set_precomputed_key_export can only be called after Hmac_Init_* |
| 496 |
OPENSSL_PUT_ERROR(HMAC, HMAC_R_NOT_CALLED_JUST_AFTER_INIT); |
| 497 |
return 0; |
| 498 |
} |
| 499 |
ctx->state = HMAC_STATE_PRECOMPUTED_KEY_EXPORT_READY; |
| 500 |
return 1; |
| 501 |
} |
| 502 |
|
| 503 |
int HMAC_get_precomputed_key(HMAC_CTX *ctx, uint8_t *out, size_t *out_len) { |
| 504 |
GUARD_PTR(ctx); |
| 505 |
GUARD_PTR(ctx->methods); |
| 506 |
if (ctx->methods->get_state == NULL) { |
| 507 |
OPENSSL_PUT_ERROR(HMAC, HMAC_R_PRECOMPUTED_KEY_NOT_SUPPORTED_FOR_DIGEST); |
| 508 |
return 0; |
| 509 |
} |
| 510 |
|
| 511 |
if (HMAC_STATE_PRECOMPUTED_KEY_EXPORT_READY != ctx->state) { |
| 512 |
OPENSSL_PUT_ERROR(EVP, HMAC_R_SET_PRECOMPUTED_KEY_EXPORT_NOT_CALLED); |
| 513 |
return 0; |
| 514 |
} |
| 515 |
|
| 516 |
if (NULL == out_len) { |
| 517 |
OPENSSL_PUT_ERROR(EVP, HMAC_R_MISSING_PARAMETERS); |
| 518 |
return 0; |
| 519 |
} |
| 520 |
|
| 521 |
const size_t chaining_length = ctx->methods->chaining_length; |
| 522 |
size_t actual_out_len = chaining_length * 2; |
| 523 |
assert(actual_out_len <= HMAC_MAX_PRECOMPUTED_KEY_SIZE); |
| 524 |
if (NULL == out) { |
| 525 |
// When out is NULL, we just set out_len. |
| 526 |
// We keep the state as HMAC_STATE_PRECOMPUTED_KEY_EXPORT_READY |
| 527 |
// to allow an actual export of the precomputed key immediately afterward. |
| 528 |
*out_len = actual_out_len; |
| 529 |
return 1; |
| 530 |
} |
| 531 |
// When out is not NULL, we need to check that *out_len is large enough |
| 532 |
if (*out_len < actual_out_len) { |
| 533 |
OPENSSL_PUT_ERROR(HMAC, HMAC_R_BUFFER_TOO_SMALL); |
| 534 |
return 0; |
| 535 |
} |
| 536 |
*out_len = actual_out_len; |
| 537 |
|
| 538 |
uint64_t i_ctx_n; |
| 539 |
// Initializing o_ctx_n to zero to remove warning from Windows ARM64 compiler |
| 540 |
// "error : variable 'o_ctx_n' is used uninitialized whenever '&&' condition |
| 541 |
// is false". Note this should not be necessary because get_state cannot fail. |
| 542 |
uint64_t o_ctx_n = 0; |
| 543 |
|
| 544 |
if (!ctx->methods->get_state(&ctx->i_ctx, out, &i_ctx_n) || |
| 545 |
!ctx->methods->get_state(&ctx->o_ctx, out + chaining_length, &o_ctx_n)) { |
| 546 |
// get_state should always succeed since i_ctx/o_ctx have processed exactly |
| 547 |
// one block, but handle failure defensively. |
| 548 |
assert(0); // Should never happen |
| 549 |
OPENSSL_cleanse(out, actual_out_len); |
| 550 |
return 0; |
| 551 |
} |
| 552 |
|
| 553 |
// Sanity check: we must have processed a single block at this time |
| 554 |
size_t block_size = EVP_MD_block_size(ctx->md); |
| 555 |
if (8 * block_size != i_ctx_n || 8 * block_size != o_ctx_n) { |
| 556 |
assert(0); // Should never happen |
| 557 |
OPENSSL_cleanse(out, actual_out_len); |
| 558 |
return 0; |
| 559 |
} |
| 560 |
|
| 561 |
// The context is ready to be used to compute HMAC values at this point. |
| 562 |
ctx->state = HMAC_STATE_INIT_NO_DATA; |
| 563 |
|
| 564 |
return 1; |
| 565 |
} |
| 566 |
|
| 567 |
int HMAC_Init_from_precomputed_key(HMAC_CTX *ctx, |
| 568 |
const uint8_t *precomputed_key, |
| 569 |
size_t precomputed_key_len, |
| 570 |
const EVP_MD *md) { |
| 571 |
|
| 572 |
// HMAC does not support SHAKE (XOF) algorithms |
| 573 |
if (md && (EVP_MD_flags(md) & EVP_MD_FLAG_XOF)) { |
| 574 |
OPENSSL_PUT_ERROR(HMAC, HMAC_R_UNSUPPORTED_DIGEST); |
| 575 |
return 0; |
| 576 |
} |
| 577 |
|
| 578 |
if (HMAC_STATE_READY_NEEDS_INIT == ctx->state || |
| 579 |
HMAC_STATE_PRECOMPUTED_KEY_EXPORT_READY == ctx->state) { |
| 580 |
ctx->state = HMAC_STATE_INIT_NO_DATA; // Mark that init has been called |
| 581 |
} |
| 582 |
|
| 583 |
if (HMAC_STATE_INIT_NO_DATA == ctx->state) { |
| 584 |
if (precomputed_key == NULL && (md == NULL || md == ctx->md)) { |
| 585 |
// If nothing is changing then we can return without doing any further |
| 586 |
// work. |
| 587 |
return 1; |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
// Now we assume that we need to re-initialize everything. |
| 592 |
// See HMAC_Init_ex |
| 593 |
|
| 594 |
if (!hmac_ctx_set_md_methods(ctx, md)) { |
| 595 |
return 0; |
| 596 |
} |
| 597 |
|
| 598 |
const HmacMethods *methods = ctx->methods; |
| 599 |
if (ctx->methods->init_from_state == NULL) { |
| 600 |
OPENSSL_PUT_ERROR(HMAC, HMAC_R_PRECOMPUTED_KEY_NOT_SUPPORTED_FOR_DIGEST); |
| 601 |
return 0; |
| 602 |
} |
| 603 |
|
| 604 |
const size_t chaining_length = methods->chaining_length; |
| 605 |
const size_t block_size = EVP_MD_block_size(methods->evp_md); |
| 606 |
assert(block_size <= EVP_MAX_MD_BLOCK_SIZE); |
| 607 |
assert(2 * chaining_length <= HMAC_MAX_PRECOMPUTED_KEY_SIZE); |
| 608 |
|
| 609 |
if (2 * chaining_length != precomputed_key_len) { |
| 610 |
return 0; |
| 611 |
} |
| 612 |
|
| 613 |
// We require precomputed_key to be non-NULL, since here md changed |
| 614 |
if (NULL == precomputed_key) { |
| 615 |
OPENSSL_PUT_ERROR(HMAC, HMAC_R_MISSING_PARAMETERS); |
| 616 |
return 0; |
| 617 |
} |
| 618 |
|
| 619 |
// We have to avoid the underlying SHA services updating the indicator |
| 620 |
// state, so we lock the state here. Technically this is not really needed, |
| 621 |
// because the functions we call should not update the indicator state. |
| 622 |
// But this is safer. |
| 623 |
FIPS_service_indicator_lock_state(); |
| 624 |
int result = 0; |
| 625 |
|
| 626 |
// Initialize i_ctx from the state stored in the first part of precomputed_key |
| 627 |
// Recall that i_ctx is the state of the hash function after processing |
| 628 |
// one block (ipad xor keyOrHashedKey) |
| 629 |
if (!methods->init_from_state(&ctx->i_ctx, precomputed_key, block_size * 8)) { |
| 630 |
goto end; |
| 631 |
} |
| 632 |
|
| 633 |
// Same for o_ctx using the second part of precomputed_key |
| 634 |
if (!methods->init_from_state(&ctx->o_ctx, precomputed_key + chaining_length, |
| 635 |
block_size * 8)) { |
| 636 |
goto end; |
| 637 |
} |
| 638 |
|
| 639 |
OPENSSL_memcpy(&ctx->md_ctx, &ctx->i_ctx, sizeof(ctx->i_ctx)); |
| 640 |
ctx->state = HMAC_STATE_INIT_NO_DATA; |
| 641 |
|
| 642 |
result = 1; |
| 643 |
end: |
| 644 |
FIPS_service_indicator_unlock_state(); |
| 645 |
if (result != 1) { |
| 646 |
// We're in some error state, so return our context to a known and |
| 647 |
// well-defined zero state. |
| 648 |
HMAC_CTX_cleanup(ctx); |
| 649 |
} |
| 650 |
return result; |
| 651 |
} |
| 652 |
|
| 653 |
int HMAC_Init(HMAC_CTX *ctx, const void *key, int key_len, const EVP_MD *md) { |
| 654 |
if (key && key_len < 0) { |
| 655 |
return 0; |
| 656 |
} |
| 657 |
if (key && md) { |
| 658 |
HMAC_CTX_init(ctx); |
| 659 |
} |
| 660 |
return HMAC_Init_ex(ctx, key, (size_t)key_len, md, NULL); |
| 661 |
} |
| 662 |
|
| 663 |
int HMAC_CTX_copy(HMAC_CTX *dest, const HMAC_CTX *src) { |
| 664 |
HMAC_CTX_init(dest); |
| 665 |
return HMAC_CTX_copy_ex(dest, src); |
| 666 |
} |