Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/kdf/kbkdf.c

raw
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0 OR ISC
3
4 #include <openssl/kdf.h>
5 #include <openssl/mem.h>
6
7 #include "../../internal.h"
8 #include "../service_indicator/internal.h"
9 #include "internal.h"
10
11 int KBKDF_ctr_hmac(uint8_t *out_key, size_t out_len, const EVP_MD *digest,
12 const uint8_t *secret, size_t secret_len,
13 const uint8_t *info, size_t info_len) {
14 SET_DIT_AUTO_RESET;
15
16 // We have to avoid the underlying |HMAC_Final| services updating
17 // the indicator state, so we lock the state here.
18 FIPS_service_indicator_lock_state();
19
20 HMAC_CTX hmac_ctx;
21 int ret = 0;
22
23 if (!out_key || out_len == 0 || !secret || secret_len == 0) {
24 goto err;
25 }
26
27 HMAC_CTX_init(&hmac_ctx);
28 if (!HMAC_Init_ex(&hmac_ctx, secret, secret_len, digest, NULL)) {
29 goto err;
30 }
31
32 // Determine the length of the output in bytes of a single invocation of the
33 // HMAC function.
34 const size_t h_output_bytes = HMAC_size(&hmac_ctx);
35 if (h_output_bytes == 0 || h_output_bytes > EVP_MAX_MD_SIZE) {
36 goto err;
37 }
38
39 if (out_len > SIZE_MAX - h_output_bytes) {
40 goto err;
41 }
42
43 // NIST.SP.800-108r1-upd1: Step 1:
44 // Determine how many output chunks are required to produce the requested
45 // output length |out_len|. This determines how many times the variant compute
46 // function will be called to output key material.
47 uint64_t n = ((uint64_t)out_len + (uint64_t)h_output_bytes - 1) /
48 (uint64_t)h_output_bytes;
49
50 // NIST.SP.800-108r1-upd1: Step 2:
51 // Verify that the number of output chunks does not exceed an unsigned 32-bit
52 // integer.
53 if (n > UINT32_MAX) {
54 goto err;
55 }
56
57 uint8_t out_key_i[EVP_MAX_MD_SIZE];
58 uint8_t counter[KBKDF_COUNTER_SIZE];
59 size_t done = 0;
60 uint32_t written = 0;
61
62 for (uint32_t i = 0; i < n; i++) {
63 // Increment the counter
64 CRYPTO_store_u32_be(&counter[0], i + 1);
65
66 written = 0;
67 // NIST.SP.800-108r1-upd1: Step 4a:
68 // K(i) := PRF(K_IN, [i] || FixedInfo)
69 // Note |hmac_ctx| has already been configured with the secret key
70 if (!HMAC_Init_ex(&hmac_ctx, NULL, 0, NULL, NULL) ||
71 !HMAC_Update(&hmac_ctx, &counter[0], KBKDF_COUNTER_SIZE) ||
72 !HMAC_Update(&hmac_ctx, info, info_len) ||
73 !HMAC_Final(&hmac_ctx, out_key_i, &written) ||
74 written != h_output_bytes) {
75 goto err;
76 }
77
78 // NIST.SP.800-108r1-upd1: Step 4b, Step 5
79 // result := result || K(i)
80 // Ensure that we only copy |out_len| bytes in total from all chunks.
81 if (written > out_len - done) {
82 written = out_len - done;
83 }
84 OPENSSL_memcpy(out_key + done, out_key_i, written);
85 done += written;
86 }
87
88 ret = 1;
89
90 err:
91 OPENSSL_cleanse(&out_key_i[0], EVP_MAX_MD_SIZE);
92 if (ret <= 0 && out_key && out_len > 0) {
93 OPENSSL_cleanse(out_key, out_len);
94 }
95 HMAC_CTX_cleanup(&hmac_ctx);
96 FIPS_service_indicator_unlock_state();
97 if (ret) {
98 KBKDF_ctr_hmac_verify_service_indicator(digest, secret_len);
99 }
100 return ret;
101 }