Debian Codegraph @

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

raw
1 // Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <assert.h>
5
6 #include <openssl/digest.h>
7 #include <openssl/hkdf.h>
8 #include <openssl/hmac.h>
9 #include <openssl/mem.h>
10
11 #include "../../internal.h"
12
13 // tls1_P_hash computes the TLS P_<hash> function as described in RFC 5246,
14 // section 5. It XORs |out_len| bytes to |out|, using |md| as the hash and
15 // |secret| as the secret. |label|, |seed1|, and |seed2| are concatenated to
16 // form the seed parameter. It returns true on success and false on failure.
17 static int tls1_P_hash(uint8_t *out, size_t out_len,
18 const EVP_MD *md,
19 const uint8_t *secret, size_t secret_len,
20 const char *label, size_t label_len,
21 const uint8_t *seed1, size_t seed1_len,
22 const uint8_t *seed2, size_t seed2_len) {
23 HMAC_CTX ctx, ctx_tmp, ctx_init;
24 uint8_t A1[EVP_MAX_MD_SIZE];
25 unsigned A1_len;
26 int ret = 0;
27
28 const size_t chunk = EVP_MD_size(md);
29 HMAC_CTX_init(&ctx);
30 HMAC_CTX_init(&ctx_tmp);
31 HMAC_CTX_init(&ctx_init);
32
33 if (!HMAC_Init_ex(&ctx_init, secret, secret_len, md, NULL) ||
34 !HMAC_CTX_copy_ex(&ctx, &ctx_init) ||
35 !HMAC_Update(&ctx, (const uint8_t *) label, label_len) ||
36 !HMAC_Update(&ctx, seed1, seed1_len) ||
37 !HMAC_Update(&ctx, seed2, seed2_len) ||
38 !HMAC_Final(&ctx, A1, &A1_len)) {
39 goto err;
40 }
41
42 for (;;) {
43 unsigned len_u;
44 uint8_t hmac[EVP_MAX_MD_SIZE];
45 if (!HMAC_CTX_copy_ex(&ctx, &ctx_init) ||
46 !HMAC_Update(&ctx, A1, A1_len) ||
47 // Save a copy of |ctx| to compute the next A1 value below.
48 (out_len > chunk && !HMAC_CTX_copy_ex(&ctx_tmp, &ctx)) ||
49 !HMAC_Update(&ctx, (const uint8_t *) label, label_len) ||
50 !HMAC_Update(&ctx, seed1, seed1_len) ||
51 !HMAC_Update(&ctx, seed2, seed2_len) ||
52 !HMAC_Final(&ctx, hmac, &len_u)) {
53 goto err;
54 }
55 size_t len = len_u;
56 assert(len == chunk);
57
58 // XOR the result into |out|.
59 if (len > out_len) {
60 len = out_len;
61 }
62 for (size_t i = 0; i < len; i++) {
63 out[i] ^= hmac[i];
64 }
65 out += len;
66 out_len -= len;
67
68 if (out_len == 0) {
69 break;
70 }
71
72 // Calculate the next A1 value.
73 if (!HMAC_Final(&ctx_tmp, A1, &A1_len)) {
74 goto err;
75 }
76 }
77
78 ret = 1;
79
80 err:
81 OPENSSL_cleanse(A1, sizeof(A1));
82 HMAC_CTX_cleanup(&ctx);
83 HMAC_CTX_cleanup(&ctx_tmp);
84 HMAC_CTX_cleanup(&ctx_init);
85 return ret;
86 }
87
88 int CRYPTO_tls1_prf(const EVP_MD *digest,
89 uint8_t *out, size_t out_len,
90 const uint8_t *secret, size_t secret_len,
91 const char *label, size_t label_len,
92 const uint8_t *seed1, size_t seed1_len,
93 const uint8_t *seed2, size_t seed2_len) {
94 // We have to avoid the underlying HMAC services updating the indicator state,
95 // so we lock the state here.
96 FIPS_service_indicator_lock_state();
97 SET_DIT_AUTO_RESET;
98 int ret = 0;
99 const EVP_MD *original_digest = digest;
100 if (out_len == 0) {
101 ret = 1;
102 goto end;
103 }
104
105 OPENSSL_memset(out, 0, out_len);
106
107 if (digest == EVP_md5_sha1()) {
108 // If using the MD5/SHA1 PRF, |secret| is partitioned between MD5 and SHA-1.
109 size_t secret_half = secret_len - (secret_len / 2);
110 if (!tls1_P_hash(out, out_len, EVP_md5(), secret, secret_half, label,
111 label_len, seed1, seed1_len, seed2, seed2_len)) {
112 goto end;
113 }
114
115 // Note that, if |secret_len| is odd, the two halves share a byte.
116 secret += secret_len - secret_half;
117 secret_len = secret_half;
118 digest = EVP_sha1();
119 }
120
121 ret = tls1_P_hash(out, out_len, digest, secret, secret_len, label, label_len,
122 seed1, seed1_len, seed2, seed2_len);
123 end:
124 FIPS_service_indicator_unlock_state();
125 if(ret) {
126 TLSKDF_verify_service_indicator(original_digest, label, label_len);
127 }
128 return ret;
129 }