2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include <openssl/evp.h>
13 #include <openssl/kdf.h>
15 #define TLS13_MAX_LABEL_LEN 246
17 /* Always filled with zeros */
18 static const unsigned char default_zeros[EVP_MAX_MD_SIZE];
20 static const unsigned char keylabel[] = "key";
21 static const unsigned char ivlabel[] = "iv";
24 * Given a |secret|; a |label| of length |labellen|; and a |hash| of the
25 * handshake messages, derive a new secret |outlen| bytes long and store it in
26 * the location pointed to be |out|. The |hash| value may be NULL.
28 * Returns 1 on success 0 on failure.
30 static int tls13_hkdf_expand(SSL *s, const unsigned char *secret,
31 const unsigned char *label, size_t labellen,
32 const unsigned char *hash,
33 unsigned char *out, size_t outlen)
35 const unsigned char label_prefix[] = "TLS 1.3, ";
36 const EVP_MD *md = ssl_handshake_md(s);
37 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
42 * 2 bytes for length of whole HkdfLabel + 1 byte for length of combined
43 * prefix and label + bytes for the label itself + bytes for the hash
45 unsigned char hkdflabel[sizeof(uint16_t) + sizeof(uint8_t) +
46 + sizeof(label_prefix) + TLS13_MAX_LABEL_LEN
53 hashlen = EVP_MD_size(md);
55 if (!WPACKET_init_static_len(&pkt, hkdflabel, sizeof(hkdflabel), 0)
56 || !WPACKET_put_bytes_u16(&pkt, outlen)
57 || !WPACKET_start_sub_packet_u8(&pkt)
58 || !WPACKET_memcpy(&pkt, label_prefix, sizeof(label_prefix) - 1)
59 || !WPACKET_memcpy(&pkt, label, labellen)
60 || !WPACKET_close(&pkt)
61 || !WPACKET_sub_memcpy_u8(&pkt, hash, (hash == NULL) ? 0 : hashlen)
62 || !WPACKET_get_total_written(&pkt, &hkdflabellen)
63 || !WPACKET_finish(&pkt)) {
64 WPACKET_cleanup(&pkt);
68 ret = EVP_PKEY_derive_init(pctx) <= 0
69 || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY)
71 || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0
72 || EVP_PKEY_CTX_set1_hkdf_key(pctx, secret, hashlen) <= 0
73 || EVP_PKEY_CTX_add1_hkdf_info(pctx, hkdflabel, hkdflabellen) <= 0
74 || EVP_PKEY_derive(pctx, out, &outlen) <= 0;
76 EVP_PKEY_CTX_free(pctx);
82 * Given a input secret |insecret| and a |label| of length |labellen|, derive a
83 * new |secret|. This will be the length of the current hash output size and
84 * will be based on the current state of the handshake hashes.
86 * Returns 1 on success 0 on failure.
88 int tls13_derive_secret(SSL *s, const unsigned char *insecret,
89 const unsigned char *label, size_t labellen,
90 unsigned char *secret)
92 unsigned char hash[EVP_MAX_MD_SIZE];
95 if (!ssl3_digest_cached_records(s, 1))
98 if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen))
101 return tls13_hkdf_expand(s, insecret, label, labellen, hash, secret,
106 * Given a |secret| generate a |key| of length |keylen| bytes.
108 * Returns 1 on success 0 on failure.
110 int tls13_derive_key(SSL *s, const unsigned char *secret, unsigned char *key,
113 return tls13_hkdf_expand(s, secret, keylabel, sizeof(keylabel) - 1, NULL,
118 * Given a |secret| generate an |iv| of length |ivlen| bytes.
120 * Returns 1 on success 0 on failure.
122 int tls13_derive_iv(SSL *s, const unsigned char *secret, unsigned char *iv,
125 return tls13_hkdf_expand(s, secret, ivlabel, sizeof(ivlabel) - 1, NULL,
130 * Given the previous secret |prevsecret| and a new input secret |insecret| of
131 * length |insecretlen|, generate a new secret and store it in the location
132 * pointed to by |outsecret|.
134 * Returns 1 on success 0 on failure.
136 static int tls13_generate_secret(SSL *s, const unsigned char *prevsecret,
137 const unsigned char *insecret,
139 unsigned char *outsecret)
141 const EVP_MD *md = ssl_handshake_md(s);
142 size_t mdlen, prevsecretlen;
144 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
149 mdlen = EVP_MD_size(md);
151 if (insecret == NULL) {
152 insecret = default_zeros;
155 if (prevsecret == NULL) {
156 prevsecret = default_zeros;
159 prevsecretlen = mdlen;
162 ret = EVP_PKEY_derive_init(pctx) <= 0
163 || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY)
165 || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0
166 || EVP_PKEY_CTX_set1_hkdf_key(pctx, insecret, insecretlen) <= 0
167 || EVP_PKEY_CTX_set1_hkdf_salt(pctx, prevsecret, prevsecretlen)
169 || EVP_PKEY_derive(pctx, outsecret, &mdlen)
172 EVP_PKEY_CTX_free(pctx);
177 * Given an input secret |insecret| of length |insecretlen| generate the early
180 * Returns 1 on success 0 on failure.
182 int tls13_generate_early_secret(SSL *s, const unsigned char *insecret,
185 return tls13_generate_secret(s, NULL, insecret, insecretlen,
186 (unsigned char *)&s->early_secret);
190 * Given an input secret |insecret| of length |insecretlen| generate the
191 * handshake secret. This requires the early secret to already have been
194 * Returns 1 on success 0 on failure.
196 int tls13_generate_handshake_secret(SSL *s, const unsigned char *insecret,
199 return tls13_generate_secret(s, s->early_secret, insecret, insecretlen,
200 (unsigned char *)&s->handshake_secret);
204 * Given the handshake secret |prev| of length |prevlen| generate the master
205 * secret and store its length in |*secret_size|
207 * Returns 1 on success 0 on failure.
209 int tls13_generate_master_secret(SSL *s, unsigned char *out,
210 unsigned char *prev, size_t prevlen,
213 *secret_size = EVP_MD_size(ssl_handshake_md(s));
214 return tls13_generate_secret(s, prev, NULL, 0, out);