| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
#include <assert.h> |
| 5 |
#include <openssl/base.h> |
| 6 |
#include <openssl/digest.h> |
| 7 |
#include <openssl/hmac.h> |
| 8 |
#include <openssl/kdf.h> |
| 9 |
#include <openssl/mem.h> |
| 10 |
|
| 11 |
#include "../../internal.h" |
| 12 |
#include "../delocate.h" |
| 13 |
#include "../service_indicator/internal.h" |
| 14 |
#include "internal.h" |
| 15 |
|
| 16 |
static int sskdf_variant_digest_ctx_init(sskdf_variant_ctx *ctx, |
| 17 |
const EVP_MD *digest) { |
| 18 |
sskdf_variant_digest_ctx *variant_ctx = NULL; |
| 19 |
EVP_MD_CTX *md_ctx = NULL; |
| 20 |
|
| 21 |
int ret = 0; |
| 22 |
|
| 23 |
if (!ctx || ctx->data || !digest) { |
| 24 |
goto err; |
| 25 |
} |
| 26 |
|
| 27 |
variant_ctx = OPENSSL_malloc(sizeof(sskdf_variant_digest_ctx)); |
| 28 |
if (!variant_ctx) { |
| 29 |
goto err; |
| 30 |
} |
| 31 |
|
| 32 |
md_ctx = EVP_MD_CTX_new(); |
| 33 |
if (!md_ctx) { |
| 34 |
goto err; |
| 35 |
} |
| 36 |
|
| 37 |
ret = 1; |
| 38 |
variant_ctx->digest = digest; |
| 39 |
variant_ctx->md_ctx = md_ctx; |
| 40 |
ctx->data = variant_ctx; |
| 41 |
|
| 42 |
return ret; |
| 43 |
|
| 44 |
err: |
| 45 |
EVP_MD_CTX_free(md_ctx); |
| 46 |
OPENSSL_free(variant_ctx); |
| 47 |
|
| 48 |
return ret; |
| 49 |
} |
| 50 |
|
| 51 |
static void sskdf_variant_digest_ctx_cleanup(sskdf_variant_ctx *ctx) { |
| 52 |
if (!ctx || !ctx->data) { |
| 53 |
return; |
| 54 |
} |
| 55 |
sskdf_variant_digest_ctx *variant_ctx = (sskdf_variant_digest_ctx *)ctx->data; |
| 56 |
EVP_MD_CTX_free(variant_ctx->md_ctx); |
| 57 |
OPENSSL_free(variant_ctx); |
| 58 |
ctx->data = NULL; |
| 59 |
} |
| 60 |
|
| 61 |
static size_t sskdf_variant_digest_output_size(sskdf_variant_ctx *ctx) { |
| 62 |
if (!ctx || !ctx->data) { |
| 63 |
return 0; |
| 64 |
} |
| 65 |
sskdf_variant_digest_ctx *variant_ctx = (sskdf_variant_digest_ctx *)ctx->data; |
| 66 |
return EVP_MD_size(variant_ctx->digest); |
| 67 |
} |
| 68 |
|
| 69 |
static int sskdf_variant_digest_compute( |
| 70 |
sskdf_variant_ctx *ctx, uint8_t *out, size_t out_len, |
| 71 |
const uint8_t counter[SSKDF_COUNTER_SIZE], const uint8_t *secret, |
| 72 |
size_t secret_len, const uint8_t *info, size_t info_len) { |
| 73 |
if (!ctx || !ctx->data || !out || !counter || !secret) { |
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
sskdf_variant_digest_ctx *variant_ctx = (sskdf_variant_digest_ctx *)ctx->data; |
| 78 |
|
| 79 |
if (!variant_ctx->md_ctx || !variant_ctx->digest) { |
| 80 |
return 0; |
| 81 |
} |
| 82 |
|
| 83 |
uint32_t written; |
| 84 |
|
| 85 |
// NIST.SP.800-56Cr2: Step 6.2 hash(counter || secret || info) |
| 86 |
if (!EVP_MD_CTX_reset(variant_ctx->md_ctx) || |
| 87 |
!EVP_DigestInit_ex(variant_ctx->md_ctx, variant_ctx->digest, NULL) || |
| 88 |
!EVP_DigestUpdate(variant_ctx->md_ctx, &counter[0], SSKDF_COUNTER_SIZE) || |
| 89 |
!EVP_DigestUpdate(variant_ctx->md_ctx, secret, secret_len) || |
| 90 |
!EVP_DigestUpdate(variant_ctx->md_ctx, info, info_len) || |
| 91 |
!EVP_DigestFinal(variant_ctx->md_ctx, out, &written) || |
| 92 |
written != out_len) { |
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
return 1; |
| 97 |
} |
| 98 |
|
| 99 |
DEFINE_METHOD_FUNCTION(sskdf_variant, sskdf_variant_digest) { |
| 100 |
out->h_output_bytes = sskdf_variant_digest_output_size; |
| 101 |
out->compute = sskdf_variant_digest_compute; |
| 102 |
} |
| 103 |
|
| 104 |
static int sskdf_variant_hmac_ctx_init(sskdf_variant_ctx *ctx, |
| 105 |
const EVP_MD *digest, |
| 106 |
const uint8_t *salt, size_t salt_len) { |
| 107 |
sskdf_variant_hmac_ctx *variant_ctx = NULL; |
| 108 |
HMAC_CTX *hmac_ctx = NULL; |
| 109 |
|
| 110 |
int ret = 0; |
| 111 |
|
| 112 |
if (!ctx || ctx->data || !digest) { |
| 113 |
goto err; |
| 114 |
} |
| 115 |
|
| 116 |
variant_ctx = OPENSSL_malloc(sizeof(sskdf_variant_hmac_ctx)); |
| 117 |
if (!variant_ctx) { |
| 118 |
goto err; |
| 119 |
} |
| 120 |
|
| 121 |
hmac_ctx = HMAC_CTX_new(); |
| 122 |
if (!hmac_ctx) { |
| 123 |
goto err; |
| 124 |
} |
| 125 |
|
| 126 |
if (!HMAC_Init_ex(hmac_ctx, salt, salt_len, digest, NULL)) { |
| 127 |
goto err; |
| 128 |
} |
| 129 |
|
| 130 |
ret = 1; |
| 131 |
variant_ctx->hmac_ctx = hmac_ctx; |
| 132 |
ctx->data = variant_ctx; |
| 133 |
|
| 134 |
return ret; |
| 135 |
|
| 136 |
err: |
| 137 |
HMAC_CTX_free(hmac_ctx); |
| 138 |
OPENSSL_free(variant_ctx); |
| 139 |
|
| 140 |
return ret; |
| 141 |
} |
| 142 |
|
| 143 |
static void sskdf_variant_hmac_ctx_cleanup(sskdf_variant_ctx *ctx) { |
| 144 |
if (!ctx || !ctx->data) { |
| 145 |
return; |
| 146 |
} |
| 147 |
sskdf_variant_hmac_ctx *variant_ctx = (sskdf_variant_hmac_ctx *)ctx->data; |
| 148 |
HMAC_CTX_free(variant_ctx->hmac_ctx); |
| 149 |
OPENSSL_free(variant_ctx); |
| 150 |
ctx->data = NULL; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
static size_t sskdf_variant_hmac_output_size(sskdf_variant_ctx *ctx) { |
| 155 |
if (!ctx || !ctx->data) { |
| 156 |
return 0; |
| 157 |
} |
| 158 |
sskdf_variant_hmac_ctx *variant_ctx = (sskdf_variant_hmac_ctx *)ctx->data; |
| 159 |
if (!variant_ctx) { |
| 160 |
return 0; |
| 161 |
} |
| 162 |
return HMAC_size(variant_ctx->hmac_ctx); |
| 163 |
} |
| 164 |
|
| 165 |
static int sskdf_variant_hmac_compute(sskdf_variant_ctx *ctx, uint8_t *out, |
| 166 |
size_t out_len, |
| 167 |
const uint8_t counter[SSKDF_COUNTER_SIZE], |
| 168 |
const uint8_t *secret, size_t secret_len, |
| 169 |
const uint8_t *info, size_t info_len) { |
| 170 |
if (!ctx || !ctx->data || !out || !counter || !secret) { |
| 171 |
return 0; |
| 172 |
} |
| 173 |
|
| 174 |
sskdf_variant_hmac_ctx *variant_ctx = (sskdf_variant_hmac_ctx *)ctx->data; |
| 175 |
|
| 176 |
if (!variant_ctx->hmac_ctx) { |
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
uint32_t written; |
| 181 |
|
| 182 |
// NIST.SP.800-56Cr2: Step 6.2 HMAC-hash(salt, counter || secret || info) |
| 183 |
// Note: |variant_ctx->hmac_ctx| is already initalized with the salt during |
| 184 |
// its initial construction. |
| 185 |
if (!HMAC_Init_ex(variant_ctx->hmac_ctx, NULL, 0, NULL, NULL) || |
| 186 |
!HMAC_Update(variant_ctx->hmac_ctx, &counter[0], SSKDF_COUNTER_SIZE) || |
| 187 |
!HMAC_Update(variant_ctx->hmac_ctx, secret, secret_len) || |
| 188 |
!HMAC_Update(variant_ctx->hmac_ctx, info, info_len) || |
| 189 |
!HMAC_Final(variant_ctx->hmac_ctx, out, &written) || out_len != written) { |
| 190 |
return 0; |
| 191 |
} |
| 192 |
|
| 193 |
return 1; |
| 194 |
} |
| 195 |
|
| 196 |
DEFINE_METHOD_FUNCTION(sskdf_variant, sskdf_variant_hmac) { |
| 197 |
out->h_output_bytes = sskdf_variant_hmac_output_size; |
| 198 |
out->compute = sskdf_variant_hmac_compute; |
| 199 |
} |
| 200 |
|
| 201 |
static int SSKDF(const sskdf_variant *variant, sskdf_variant_ctx *ctx, |
| 202 |
uint8_t *out_key, size_t out_len, const uint8_t *secret, |
| 203 |
size_t secret_len, const uint8_t *info, size_t info_len) { |
| 204 |
int ret = 0; |
| 205 |
|
| 206 |
if (!ctx || !variant) { |
| 207 |
return 0; |
| 208 |
} |
| 209 |
|
| 210 |
// The SSKDF_MAX_INPUT_LEN is an upper bound chosen for improved |
| 211 |
// interoperability with OpenSSL's SSKDF implementation. Additionally this |
| 212 |
// upper bound satisfies the NIST.SP.800-56Cr2 requirements outlined in table |
| 213 |
// 2 and 3 for digest and HMAC variants with approved hashes. The smallest max |
| 214 |
// limit imposed is (2^64-512)/8 bytes, and the limits used here places the |
| 215 |
// maximum allowed input to be (2^31 + 4). This satisfies the requirement |
| 216 |
// outlined in Step 4 of the specification. |
| 217 |
if (!out_key || out_len == 0 || out_len > SSKDF_MAX_INPUT_LEN || !secret || |
| 218 |
secret_len == 0 || secret_len > SSKDF_MAX_INPUT_LEN || |
| 219 |
info_len > SSKDF_MAX_INPUT_LEN) { |
| 220 |
goto err; |
| 221 |
} |
| 222 |
|
| 223 |
// h_output_bytes is the length in bytes of output of the SSKDF variant |
| 224 |
// auxilary function (EVP_DigestFinal or HMAC_Final) |
| 225 |
const size_t h_output_bytes = variant->h_output_bytes(ctx); |
| 226 |
if (h_output_bytes == 0 || h_output_bytes > EVP_MAX_MD_SIZE) { |
| 227 |
goto err; |
| 228 |
} |
| 229 |
|
| 230 |
// NIST.SP.800-56Cr2 Step 1: |
| 231 |
// Determine how many output chunks are required to produce the requested |
| 232 |
// output length |out_len|. This determines how many times the variant compute |
| 233 |
// function will be called to output key material. |
| 234 |
uint64_t n = ((uint64_t)out_len + (uint64_t)h_output_bytes - 1) / |
| 235 |
(uint64_t)h_output_bytes; |
| 236 |
|
| 237 |
// NIST.SP.800-56Cr2 Step 2: |
| 238 |
// Verify that the number of output chunks does not exceed an unsigned 32-bit |
| 239 |
// integer. |
| 240 |
if (n > UINT32_MAX) { |
| 241 |
goto err; |
| 242 |
} |
| 243 |
|
| 244 |
// TODO(awslc): Abstract buffer size, if we ever need to support KMAC this |
| 245 |
// could be variable. Currently sufficient for HMAC and digest variants |
| 246 |
uint8_t out_key_i[EVP_MAX_MD_SIZE]; |
| 247 |
uint8_t counter[SSKDF_COUNTER_SIZE]; |
| 248 |
size_t done = 0; |
| 249 |
size_t todo = h_output_bytes; |
| 250 |
|
| 251 |
// NIST.SP.800-56Cr2 Step 6 |
| 252 |
for (uint32_t i = 0; i < n; i++) { |
| 253 |
// NIST.SP.800-56Cr2: Step 6.1 |
| 254 |
// Increment the big-endian counter by one. |
| 255 |
CRYPTO_store_u32_be(&counter[0], i + 1); |
| 256 |
|
| 257 |
// NIST.SP.800-56Cr2: Step 6.2 |
| 258 |
// Compute out_key_i = H(counter || secret || info) |
| 259 |
if (!variant->compute(ctx, &out_key_i[0], h_output_bytes, counter, secret, |
| 260 |
secret_len, info, info_len)) { |
| 261 |
goto err; |
| 262 |
} |
| 263 |
|
| 264 |
// NIST.SP.800-56Cr2: Step 6.3. Step 7, Step 8 |
| 265 |
// Combine the output from |out_key_i| with the output written to |out_key| |
| 266 |
// so far. Ensure that we only copy |out_len| bytes in total from all |
| 267 |
// chunks. |
| 268 |
todo = h_output_bytes; |
| 269 |
if (todo > out_len - done) { |
| 270 |
todo = out_len - done; |
| 271 |
} |
| 272 |
OPENSSL_memcpy(out_key + done, out_key_i, todo); |
| 273 |
done += todo; |
| 274 |
} |
| 275 |
|
| 276 |
ret = 1; |
| 277 |
|
| 278 |
err: |
| 279 |
OPENSSL_cleanse(&out_key_i[0], EVP_MAX_MD_SIZE); |
| 280 |
if (ret <= 0 && out_key && out_len > 0) { |
| 281 |
OPENSSL_cleanse(out_key, out_len); |
| 282 |
} |
| 283 |
return ret; |
| 284 |
} |
| 285 |
|
| 286 |
int SSKDF_digest(uint8_t *out_key, size_t out_len, const EVP_MD *digest, |
| 287 |
const uint8_t *secret, size_t secret_len, const uint8_t *info, |
| 288 |
size_t info_len) { |
| 289 |
SET_DIT_AUTO_RESET; |
| 290 |
|
| 291 |
// We have to avoid the underlying |EVP_DigestFinal| services updating |
| 292 |
// the indicator state, so we lock the state here. |
| 293 |
FIPS_service_indicator_lock_state(); |
| 294 |
|
| 295 |
sskdf_variant_ctx ctx = {0}; |
| 296 |
int ret = 0; |
| 297 |
|
| 298 |
if (!sskdf_variant_digest_ctx_init(&ctx, digest)) { |
| 299 |
FIPS_service_indicator_unlock_state(); |
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
if (!SSKDF(sskdf_variant_digest(), &ctx, out_key, out_len, secret, secret_len, |
| 304 |
info, info_len)) { |
| 305 |
goto end; |
| 306 |
} |
| 307 |
|
| 308 |
ret = 1; |
| 309 |
|
| 310 |
end: |
| 311 |
sskdf_variant_digest_ctx_cleanup(&ctx); |
| 312 |
FIPS_service_indicator_unlock_state(); |
| 313 |
if (ret) { |
| 314 |
SSKDF_digest_verify_service_indicator(digest); |
| 315 |
} |
| 316 |
return ret; |
| 317 |
} |
| 318 |
|
| 319 |
int SSKDF_hmac(uint8_t *out_key, size_t out_len, const EVP_MD *digest, |
| 320 |
const uint8_t *secret, size_t secret_len, const uint8_t *info, |
| 321 |
size_t info_len, const uint8_t *salt, size_t salt_len) { |
| 322 |
SET_DIT_AUTO_RESET; |
| 323 |
|
| 324 |
// We have to avoid the underlying |HMAC_Final| services updating |
| 325 |
// the indicator state, so we lock the state here. |
| 326 |
FIPS_service_indicator_lock_state(); |
| 327 |
|
| 328 |
sskdf_variant_ctx ctx = {0}; |
| 329 |
int ret = 0; |
| 330 |
|
| 331 |
if (!sskdf_variant_hmac_ctx_init(&ctx, digest, salt, salt_len)) { |
| 332 |
FIPS_service_indicator_unlock_state(); |
| 333 |
return 0; |
| 334 |
} |
| 335 |
|
| 336 |
if (!SSKDF(sskdf_variant_hmac(), &ctx, out_key, out_len, secret, secret_len, |
| 337 |
info, info_len)) { |
| 338 |
goto end; |
| 339 |
} |
| 340 |
|
| 341 |
ret = 1; |
| 342 |
|
| 343 |
end: |
| 344 |
sskdf_variant_hmac_ctx_cleanup(&ctx); |
| 345 |
FIPS_service_indicator_unlock_state(); |
| 346 |
if(ret) { |
| 347 |
SSKDF_hmac_verify_service_indicator(digest); |
| 348 |
} |
| 349 |
return ret; |
| 350 |
} |