Debian Codegraph @

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

raw
1 // Copyright (c) 2014, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #include <assert.h>
5 #include <string.h>
6
7 #include <openssl/err.h>
8 #include <openssl/hkdf.h>
9 #include <openssl/hmac.h>
10 #include <openssl/mem.h>
11
12 #include "../../internal.h"
13 #include "../cpucap/internal.h"
14 #include "../service_indicator/internal.h"
15
16
17 int HKDF(uint8_t *out_key, size_t out_len, const EVP_MD *digest,
18 const uint8_t *secret, size_t secret_len, const uint8_t *salt,
19 size_t salt_len, const uint8_t *info, size_t info_len) {
20
21 // HKDF is built on HMAC
22 // HMAC does not support SHAKE (XOF) algorithms
23 if (EVP_MD_flags(digest) & EVP_MD_FLAG_XOF) {
24 OPENSSL_PUT_ERROR(HKDF, HKDF_R_UNSUPPORTED_DIGEST);
25 return 0;
26 }
27
28 // https://tools.ietf.org/html/rfc5869#section-2
29 uint8_t prk[EVP_MAX_MD_SIZE];
30 size_t prk_len = 0;
31 int ret = 0;
32
33 // We have to avoid the underlying HKDF services updating the indicator
34 // state, so we lock the state here.
35 FIPS_service_indicator_lock_state();
36
37 if (!HKDF_extract(prk, &prk_len, digest, secret, secret_len, salt,
38 salt_len) ||
39 !HKDF_expand(out_key, out_len, digest, prk, prk_len, info, info_len)) {
40 // |HKDF_expand| zeroizes |out_key| on failure.
41 goto out;
42 }
43
44 ret = 1;
45
46 out:
47 OPENSSL_cleanse(prk, EVP_MAX_MD_SIZE);
48 FIPS_service_indicator_unlock_state();
49 if (ret == 1) {
50 HKDF_verify_service_indicator(digest, salt, salt_len, info_len);
51 }
52 return ret;
53 }
54
55 int HKDF_extract(uint8_t *out_key, size_t *out_len, const EVP_MD *digest,
56 const uint8_t *secret, size_t secret_len, const uint8_t *salt,
57 size_t salt_len) {
58 SET_DIT_AUTO_RESET;
59
60 // HKDF is built on HMAC
61 // HMAC does not support SHAKE (XOF) algorithms
62 if (EVP_MD_flags(digest) & EVP_MD_FLAG_XOF) {
63 OPENSSL_PUT_ERROR(HKDF, HKDF_R_UNSUPPORTED_DIGEST);
64 return 0;
65 }
66
67 // https://tools.ietf.org/html/rfc5869#section-2.2
68 int ret = 0;
69
70 // We have to avoid the underlying HMAC services updating the indicator
71 // state, so we lock the state here.
72 FIPS_service_indicator_lock_state();
73
74 // If salt is not given, HashLength zeros are used. However, HMAC does that
75 // internally already so we can ignore it.
76 unsigned len;
77 if (HMAC(digest, salt, salt_len, secret, secret_len, out_key, &len) == NULL) {
78 OPENSSL_PUT_ERROR(HKDF, ERR_R_HMAC_LIB);
79 goto out;
80 }
81
82 *out_len = len;
83 assert(*out_len == EVP_MD_size(digest));
84 ret = 1;
85
86 out:
87 FIPS_service_indicator_unlock_state();
88 return ret;
89 }
90
91 int HKDF_expand(uint8_t *out_key, size_t out_len, const EVP_MD *digest,
92 const uint8_t *prk, size_t prk_len, const uint8_t *info,
93 size_t info_len) {
94
95 // HKDF is built on HMAC
96 // HMAC does not support SHAKE (XOF) algorithms
97 if (EVP_MD_flags(digest) & EVP_MD_FLAG_XOF) {
98 OPENSSL_PUT_ERROR(HKDF, HKDF_R_UNSUPPORTED_DIGEST);
99 return 0;
100 }
101
102 // https://tools.ietf.org/html/rfc5869#section-2.3
103 SET_DIT_AUTO_RESET;
104 const size_t digest_len = EVP_MD_size(digest);
105 uint8_t previous[EVP_MAX_MD_SIZE];
106 int ret = 0;
107 HMAC_CTX hmac;
108
109 // Expand key material to desired length.
110 size_t n = (out_len + digest_len - 1) / digest_len;
111 if (out_len + digest_len < out_len || n > 255) {
112 OPENSSL_PUT_ERROR(HKDF, HKDF_R_OUTPUT_TOO_LARGE);
113 return 0;
114 }
115
116 // We have to avoid the underlying HMAC services updating the indicator
117 // state, so we lock the state here.
118 FIPS_service_indicator_lock_state();
119
120 HMAC_CTX_init(&hmac);
121 if (!HMAC_Init_ex(&hmac, prk, prk_len, digest, NULL)) {
122 goto out;
123 }
124
125 size_t done = 0;
126 uint32_t written = 0;
127
128 for (size_t i = 0; i < n; i++) {
129 // n is verified above to be <= 255. Hence 8-bit type is sufficient.
130 uint8_t ctr = i + 1;
131
132 if (i != 0 && (!HMAC_Init_ex(&hmac, NULL, 0, NULL, NULL) ||
133 !HMAC_Update(&hmac, previous, digest_len))) {
134 goto out;
135 }
136
137 written = 0;
138 if (!HMAC_Update(&hmac, info, info_len) ||
139 !HMAC_Update(&hmac, &ctr, 1) ||
140 !HMAC_Final(&hmac, previous, &written) ||
141 written != digest_len) {
142 goto out;
143 }
144
145 if (written > out_len - done) {
146 written = out_len - done;
147 }
148 OPENSSL_memcpy(out_key + done, previous, written);
149 done += written;
150 }
151
152 ret = 1;
153
154 out:
155 OPENSSL_cleanse(previous, EVP_MAX_MD_SIZE);
156 HMAC_CTX_cleanup(&hmac);
157 if (ret != 1) {
158 OPENSSL_PUT_ERROR(HKDF, ERR_R_HMAC_LIB);
159 OPENSSL_cleanse(out_key, out_len);
160 }
161
162 FIPS_service_indicator_unlock_state();
163 if (ret == 1) {
164 HKDFExpand_verify_service_indicator(digest);
165 }
166
167 return ret;
168 }