Coverage Report

Created: 2024-11-21 07:03

/src/boringssl/crypto/fipsmodule/tls/kdf.c.inc
Line
Count
Source (jump to first uncovered line)
1
/* ====================================================================
2
 * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or without
5
 * modification, are permitted provided that the following conditions
6
 * are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 *
11
 * 2. Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in
13
 *    the documentation and/or other materials provided with the
14
 *    distribution.
15
 *
16
 * 3. All advertising materials mentioning features or use of this
17
 *    software must display the following acknowledgment:
18
 *    "This product includes software developed by the OpenSSL Project
19
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
20
 *
21
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22
 *    endorse or promote products derived from this software without
23
 *    prior written permission. For written permission, please contact
24
 *    openssl-core@openssl.org.
25
 *
26
 * 5. Products derived from this software may not be called "OpenSSL"
27
 *    nor may "OpenSSL" appear in their names without prior written
28
 *    permission of the OpenSSL Project.
29
 *
30
 * 6. Redistributions of any form whatsoever must retain the following
31
 *    acknowledgment:
32
 *    "This product includes software developed by the OpenSSL Project
33
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34
 *
35
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46
 * OF THE POSSIBILITY OF SUCH DAMAGE.
47
 * ====================================================================
48
 *
49
 * This product includes cryptographic software written by Eric Young
50
 * (eay@cryptsoft.com).  This product includes software written by Tim
51
 * Hudson (tjh@cryptsoft.com). */
52
53
#include <assert.h>
54
55
#include <openssl/bytestring.h>
56
#include <openssl/digest.h>
57
#include <openssl/hkdf.h>
58
#include <openssl/hmac.h>
59
#include <openssl/mem.h>
60
61
#include "internal.h"
62
#include "../../internal.h"
63
#include "../service_indicator/internal.h"
64
65
66
// tls1_P_hash computes the TLS P_<hash> function as described in RFC 5246,
67
// section 5. It XORs |out_len| bytes to |out|, using |md| as the hash and
68
// |secret| as the secret. |label|, |seed1|, and |seed2| are concatenated to
69
// form the seed parameter. It returns true on success and false on failure.
70
static int tls1_P_hash(uint8_t *out, size_t out_len,
71
                       const EVP_MD *md,
72
                       const uint8_t *secret, size_t secret_len,
73
                       const char *label, size_t label_len,
74
                       const uint8_t *seed1, size_t seed1_len,
75
0
                       const uint8_t *seed2, size_t seed2_len) {
76
0
  HMAC_CTX ctx, ctx_tmp, ctx_init;
77
0
  uint8_t A1[EVP_MAX_MD_SIZE];
78
0
  unsigned A1_len;
79
0
  int ret = 0;
80
81
0
  const size_t chunk = EVP_MD_size(md);
82
0
  HMAC_CTX_init(&ctx);
83
0
  HMAC_CTX_init(&ctx_tmp);
84
0
  HMAC_CTX_init(&ctx_init);
85
86
0
  if (!HMAC_Init_ex(&ctx_init, secret, secret_len, md, NULL) ||
87
0
      !HMAC_CTX_copy_ex(&ctx, &ctx_init) ||
88
0
      !HMAC_Update(&ctx, (const uint8_t *) label, label_len) ||
89
0
      !HMAC_Update(&ctx, seed1, seed1_len) ||
90
0
      !HMAC_Update(&ctx, seed2, seed2_len) ||
91
0
      !HMAC_Final(&ctx, A1, &A1_len)) {
92
0
    goto err;
93
0
  }
94
95
0
  for (;;) {
96
0
    unsigned len_u;
97
0
    uint8_t hmac[EVP_MAX_MD_SIZE];
98
0
    if (!HMAC_CTX_copy_ex(&ctx, &ctx_init) ||
99
0
        !HMAC_Update(&ctx, A1, A1_len) ||
100
        // Save a copy of |ctx| to compute the next A1 value below.
101
0
        (out_len > chunk && !HMAC_CTX_copy_ex(&ctx_tmp, &ctx)) ||
102
0
        !HMAC_Update(&ctx, (const uint8_t *) label, label_len) ||
103
0
        !HMAC_Update(&ctx, seed1, seed1_len) ||
104
0
        !HMAC_Update(&ctx, seed2, seed2_len) ||
105
0
        !HMAC_Final(&ctx, hmac, &len_u)) {
106
0
      goto err;
107
0
    }
108
0
    size_t len = len_u;
109
0
    assert(len == chunk);
110
111
    // XOR the result into |out|.
112
0
    if (len > out_len) {
113
0
      len = out_len;
114
0
    }
115
0
    for (size_t i = 0; i < len; i++) {
116
0
      out[i] ^= hmac[i];
117
0
    }
118
0
    out += len;
119
0
    out_len -= len;
120
121
0
    if (out_len == 0) {
122
0
      break;
123
0
    }
124
125
    // Calculate the next A1 value.
126
0
    if (!HMAC_Final(&ctx_tmp, A1, &A1_len)) {
127
0
      goto err;
128
0
    }
129
0
  }
130
131
0
  ret = 1;
132
133
0
err:
134
0
  OPENSSL_cleanse(A1, sizeof(A1));
135
0
  HMAC_CTX_cleanup(&ctx);
136
0
  HMAC_CTX_cleanup(&ctx_tmp);
137
0
  HMAC_CTX_cleanup(&ctx_init);
138
0
  return ret;
139
0
}
140
141
int CRYPTO_tls1_prf(const EVP_MD *digest,
142
                    uint8_t *out, size_t out_len,
143
                    const uint8_t *secret, size_t secret_len,
144
                    const char *label, size_t label_len,
145
                    const uint8_t *seed1, size_t seed1_len,
146
0
                    const uint8_t *seed2, size_t seed2_len) {
147
0
  if (out_len == 0) {
148
0
    return 1;
149
0
  }
150
151
0
  OPENSSL_memset(out, 0, out_len);
152
153
0
  const EVP_MD *const original_digest = digest;
154
0
  FIPS_service_indicator_lock_state();
155
0
  int ret = 0;
156
157
0
  if (digest == EVP_md5_sha1()) {
158
    // If using the MD5/SHA1 PRF, |secret| is partitioned between MD5 and SHA-1.
159
0
    size_t secret_half = secret_len - (secret_len / 2);
160
0
    if (!tls1_P_hash(out, out_len, EVP_md5(), secret, secret_half, label,
161
0
                     label_len, seed1, seed1_len, seed2, seed2_len)) {
162
0
      goto end;
163
0
    }
164
165
    // Note that, if |secret_len| is odd, the two halves share a byte.
166
0
    secret += secret_len - secret_half;
167
0
    secret_len = secret_half;
168
0
    digest = EVP_sha1();
169
0
  }
170
171
0
  ret = tls1_P_hash(out, out_len, digest, secret, secret_len, label, label_len,
172
0
                    seed1, seed1_len, seed2, seed2_len);
173
174
0
end:
175
0
  FIPS_service_indicator_unlock_state();
176
0
  if (ret) {
177
0
    TLSKDF_verify_service_indicator(original_digest);
178
0
  }
179
0
  return ret;
180
0
}
181
182
int CRYPTO_tls13_hkdf_expand_label(uint8_t *out, size_t out_len,
183
                                   const EVP_MD *digest,  //
184
                                   const uint8_t *secret, size_t secret_len,
185
                                   const uint8_t *label, size_t label_len,
186
0
                                   const uint8_t *hash, size_t hash_len) {
187
0
  static const uint8_t kProtocolLabel[] = "tls13 ";
188
0
  CBB cbb, child;
189
0
  uint8_t *hkdf_label = NULL;
190
0
  size_t hkdf_label_len;
191
192
0
  FIPS_service_indicator_lock_state();
193
0
  CBB_zero(&cbb);
194
0
  if (!CBB_init(&cbb, 2 + 1 + sizeof(kProtocolLabel) - 1 + label_len + 1 +
195
0
                          hash_len) ||
196
0
      !CBB_add_u16(&cbb, out_len) ||
197
0
      !CBB_add_u8_length_prefixed(&cbb, &child) ||
198
0
      !CBB_add_bytes(&child, kProtocolLabel, sizeof(kProtocolLabel) - 1) ||
199
0
      !CBB_add_bytes(&child, label, label_len) ||
200
0
      !CBB_add_u8_length_prefixed(&cbb, &child) ||
201
0
      !CBB_add_bytes(&child, hash, hash_len) ||
202
0
      !CBB_finish(&cbb, &hkdf_label, &hkdf_label_len)) {
203
0
    CBB_cleanup(&cbb);
204
0
    FIPS_service_indicator_unlock_state();
205
0
    return 0;
206
0
  }
207
208
0
  const int ret = HKDF_expand(out, out_len, digest, secret, secret_len,
209
0
                              hkdf_label, hkdf_label_len);
210
0
  OPENSSL_free(hkdf_label);
211
212
0
  FIPS_service_indicator_unlock_state();
213
0
  if (ret) {
214
0
    TLSKDF_verify_service_indicator(digest);
215
0
  }
216
0
  return ret;
217
0
}
218