iPXE
tlskey.h
Go to the documentation of this file.
1#ifndef _IPXE_TLSKEY_H
2#define _IPXE_TLSKEY_H
3
4/** @file
5 *
6 * TLS key schedules
7 *
8 */
9
10FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11FILE_SECBOOT ( PERMITTED );
12
13#include <stdint.h>
14#include <ipxe/crypto.h>
15
16struct tls_phase;
17
18/** A TLS endpoint */
20 /** Endpoint index */
22 /** Name (for key expansion labels) */
23 const char name[ 7 /* "[client|server]" + NUL */ ];
24} __attribute__ (( packed ));
25
26/** Client endpoint index */
27#define TLS_CLIENT 0
28
29/** Server endpoint index */
30#define TLS_SERVER 1
31
32/** Number of random bytes */
33#define TLS_RANDOM_LEN 32
34
35/** TLS client or server random bytes */
36struct tls_random {
37 /** Random bytes */
39} __attribute__ (( packed ));
40
41/** A TLS running handshake transcript digest */
43 /** Digest context */
44 void *ctx;
45 /**
46 * Running digest value
47 *
48 * This is continuously updated to represent the digest value
49 * over the transcript up to and including the most recently
50 * digested handshake record.
51 */
52 void *running;
53 /**
54 * Running digest value up to the most recent Finished (if any)
55 *
56 * If no Finished record has been digested, then this mirrors
57 * the continuously running digest value.
58 *
59 * If any Finished record has been digested, then this
60 * represents the digest value over the transcript up to and
61 * including the most recently digested Finished record.
62 */
63 void *finishing;
64 /** Transcript flags */
65 unsigned int flags;
66};
67
68/** Transcript digest includes a Hello record containing the local nonce */
69#define TLSKEY_TSF_NONCED 0x0001
70
71/** Transcript digest includes a Finished record */
72#define TLSKEY_TSF_FINISHED 0x0002
73
74/** A TLS key derivation function secret */
76 /** Secret */
77 void *secret;
78 /** Key flags */
79 unsigned int flags;
80};
81
82/** Key derivation function has key material */
83#define TLSKEY_KDF_KEYED 0x0001
84
85/** Key derivation function has a master secret */
86#define TLSKEY_KDF_MASTER 0x0002
87
88/** Key derivation function has an extended master secret */
89#define TLSKEY_KDF_EMS 0x0004
90
91/** A TLS traffic secret */
93 /** Secret */
94 void *secret;
95 /** Phase */
96 const struct tls_phase *phase;
97};
98
99/** A TLS key schedule */
101 /** Key schedule operations */
103 /** Digest algorithm */
105 /** Secret size */
107
108 /** Dynamically-allocated storage */
109 void *dynamic;
110 /** Secret(s) */
111 void *secret;
112 /** Flags */
113 unsigned int flags;
114
115 /** Running handshake transcript digest */
117 /** Local endpoint random bytes */
119 /** Client and server random bytes */
121 /** Key derivation function secret */
123 /** Traffic secrets */
125};
126
127/** A TLS pre-shared key */
129 /** Key schedule operations (if set) */
131 /** Digest algorithm (if set) */
133 /** Key flags */
134 unsigned int flags;
135 /** Key material */
136 union {
137 /**
138 * Master secret
139 *
140 * For TLSv1.2 and earlier, session resumption works
141 * by simply copying the 48-byte raw master secret
142 * value.
143 *
144 * For TLSv1.1 and earlier, this 48-byte master secret
145 * logically comprises two halves: 24 bytes for MD5
146 * and 24 bytes for SHA-1.
147 */
148 union {
150 struct {
153 } __attribute__ (( packed ));
154 } master_secret;
155 /**
156 * Resumption secret
157 *
158 * For TLSv1.3 and later, session resumption uses a
159 * pre-shared resumption secret value generated using
160 * the HKDF Derive-Secret function.
161 *
162 * The longest possible value for any supported HKDF
163 * digest algorithm is 48 bytes, when using SHA-384.
164 */
167};
168
169/** TLS key schedule operations */
171 /** Name */
172 const char *name;
173 /**
174 * Key schedule accumulates all shared secrets
175 *
176 * The ability to demonstrate possession of the key material
177 * is sufficient to demonstrate possession of the most
178 * recently applied shared secret.
179 *
180 * This flag indicates that the ability to demonstrate
181 * possession of the key material is also sufficient to
182 * demonstrate possession of all previously applied shared
183 * secrets.
184 */
186 /** Key flags preserved when loading key material */
187 unsigned int mask;
188 /**
189 * Calculate secret size
190 *
191 * @v digest Digest algorithm
192 * @ret secretsize Secret size, or zero if unsupported
193 */
194 size_t ( * secretsize ) ( struct digest_algorithm *digest );
195 /**
196 * Expand key material
197 *
198 * @v digest Digest algorithm
199 * @v secret Secret
200 * @v label Label string
201 * @v seed Seed material
202 * @v seed_len Length of seed material
203 * @v out Output buffer
204 * @v out_len Length of output buffer
205 */
206 void ( * expand ) ( struct digest_algorithm *digest,
207 const void *secret, const char *label,
208 const void *seed, size_t seed_len,
209 void *out, size_t out_len );
210 /**
211 * Reset key schedule
212 *
213 * @v tlskey Key schedule
214 */
215 void ( * reset ) ( struct tls_key_schedule *tlskey );
216 /**
217 * Apply a new shared secret
218 *
219 * @v tlskey Key schedule
220 * @v shared New shared secret
221 * @v shared_len Length of new shared secret
222 * @ret rc Return status code
223 */
224 int ( * apply ) ( struct tls_key_schedule *tlskey,
225 const void *shared, size_t shared_len );
226 /**
227 * Generate master secret
228 *
229 * @v tlskey Key schedule
230 * @v ems Extended master secret extension is enabled
231 * @ret rc Return status code
232 */
233 int ( * master ) ( struct tls_key_schedule *tlskey, int ems );
234 /**
235 * Generate verification data
236 *
237 * @v tlskey Key schedule
238 * @v end Verification endpoint
239 * @v verify Verification data
240 * @v verify_len Length of verification data
241 * @ret rc Return status code
242 */
243 int ( * verify ) ( struct tls_key_schedule *tlskey,
244 const struct tls_endpoint *end,
245 void *verify, size_t verify_len );
246 /**
247 * Generate traffic secrets
248 *
249 * @v tlskey Key schedule
250 * @v writer Writer endpoint
251 * @v phase Traffic phase
252 * @ret rc Return status code
253 */
254 int ( * traffic ) ( struct tls_key_schedule *tlskey,
255 const struct tls_endpoint *writer,
256 const struct tls_phase *phase );
257 /**
258 * Generate cipher key material
259 *
260 * @v tlskey Key schedule
261 * @v writer Writer endpoint
262 * @v key Cipher key to fill in
263 * @v key_len Length of cipher key
264 * @v iv Fixed portion of IV to fill in
265 * @v iv_len Length of fixed portion of IV
266 * @v mac MAC secret to fill in
267 * @v mac_len Length of MAC secret
268 * @ret rc Return status code
269 */
270 int ( * cipher ) ( struct tls_key_schedule *tlskey,
271 const struct tls_endpoint *writer,
272 void *key, size_t key_len, void *iv,
273 size_t iv_len, void *mac, size_t mac_len );
274 /**
275 * Generate signable digest value
276 *
277 * @v tlskey Key schedule
278 * @v end Endpoint
279 * @v digest Signature digest algorithm
280 * @v data Additional data
281 * @v len Length of additional data
282 * @v tbs Signature digest value to fill in
283 * @ret rc Return status code
284 */
285 int ( * tbshash ) ( struct tls_key_schedule *tlskey,
286 const struct tls_endpoint *end,
287 struct digest_algorithm *digest,
288 const void *data, size_t len, void *tbs );
289 /**
290 * Save pre-shared key
291 *
292 * @v tlskey Key schedule
293 * @v nonce Ticket nonce
294 * @v nonce_len Length of ticket nonce
295 * @v psk Pre-shared key to fill in
296 * @ret rc Return status code
297 */
298 int ( * save ) ( struct tls_key_schedule *tlskey, const void *nonce,
299 size_t nonce_len, struct tls_preshared_key *psk );
300 /**
301 * Load pre-shared key
302 *
303 * @v tlskey Key schedule
304 * @v psk Pre-shared key
305 * @ret rc Return status code
306 */
307 int ( * load ) ( struct tls_key_schedule *tlskey,
308 const struct tls_preshared_key *psk );
309 /**
310 * Generate pre-shared key binder value
311 *
312 * @v psk Pre-shared key
313 * @v hash Partial transcript hash
314 * @v binder Binder value to fill in
315 * @v binder_len Length of binder value
316 * @ret rc Return status code
317 */
318 int ( * bind ) ( const struct tls_preshared_key *psk,
319 const void *hash, void *binder, size_t binder_len );
320};
321
322/**
323 * Check if key schedule accumulates shared secrets
324 *
325 * @v tlskey Key schedule
326 * @ret is_accumulating Key schedule accumulates all shared secrets
327 */
328static inline __attribute__ (( always_inline )) int
330 const struct tls_key_schedule_operations *op = tlskey->op;
331
332 return ( op && op->accumulates );
333}
334
335extern const struct tls_endpoint tls_client;
336extern const struct tls_endpoint tls_server;
337extern const struct tls_phase tls_early;
338extern const struct tls_phase tls_handshake;
339extern const struct tls_phase tls_application;
340
341extern const struct tls_key_schedule_operations tlskey_hkdf;
342extern const struct tls_key_schedule_operations tlskey_hash;
344
345extern int tlskey_start ( struct tls_key_schedule *tlskey,
346 const struct tls_key_schedule_operations *op,
347 struct digest_algorithm *digest,
348 const struct tls_random *random );
349extern void tlskey_stop ( struct tls_key_schedule *tlskey );
350extern void tlskey_digest ( struct tls_key_schedule *tlskey,
351 const void *data, size_t len );
352extern void tlskey_reset ( struct tls_key_schedule *tlskey );
353extern int tlskey_apply ( struct tls_key_schedule *tlskey,
354 const void *shared, size_t shared_len );
355extern int tlskey_master ( struct tls_key_schedule *tlskey, int ems );
356extern int tlskey_verify ( struct tls_key_schedule *tlskey,
357 const struct tls_endpoint *end,
358 void *verify, size_t verify_len );
359extern int tlskey_traffic ( struct tls_key_schedule *tlskey,
360 const struct tls_endpoint *writer,
361 const struct tls_phase *phase );
362extern int tlskey_cipher ( struct tls_key_schedule *tlskey,
363 const struct tls_endpoint *writer,
364 void *key, size_t key_len, void *iv,
365 size_t iv_len, void *mac, size_t mac_len );
366extern int tlskey_tbshash ( struct tls_key_schedule *tlskey,
367 const struct tls_endpoint *end,
368 struct digest_algorithm *digest,
369 const void *data, size_t len, void *tbs );
370extern int tlskey_save ( struct tls_key_schedule *tlskey, const void *nonce,
371 size_t nonce_len, struct tls_preshared_key *psk );
372extern int tlskey_load ( struct tls_key_schedule *tlskey, int ems,
373 const struct tls_preshared_key *psk );
374extern int tlskey_bind ( const struct tls_preshared_key *psk,
375 const void *prefix, size_t prefix_len,
376 void *binder, size_t binder_len );
377
378#endif /* _IPXE_TLSKEY_H */
__be32 out[4]
Definition CIB_PRM.h:8
union @162305117151260234136356364136041353210355154177 key
pseudo_bit_t hash[0x00010]
Definition arbel.h:2
__SIZE_TYPE__ size_t
Definition stdint.h:6
unsigned char uint8_t
Definition stdint.h:10
ring len
Length.
Definition dwmac.h:226
uint8_t data[48]
Additional event data.
Definition ena.h:11
uint8_t mac[ETH_ALEN]
MAC address.
Definition ena.h:13
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:951
#define __attribute__(x)
Definition compiler.h:10
Cryptographic API.
uint32_t end
Ending offset.
Definition netvsc.h:7
static uint16_t struct vmbus_xfer_pages_operations * op
Definition netvsc.h:327
long int random(void)
Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
Definition random.c:32
A message digest algorithm.
Definition crypto.h:19
A text label widget.
Definition label.h:16
TLS client state.
Definition tls.h:421
A TLS endpoint.
Definition tlskey.h:19
uint8_t index
Endpoint index.
Definition tlskey.h:21
const char name[7]
Name (for key expansion labels).
Definition tlskey.h:23
A TLS key derivation function secret.
Definition tlskey.h:75
void * secret
Secret.
Definition tlskey.h:77
unsigned int flags
Key flags.
Definition tlskey.h:79
TLS key schedule operations.
Definition tlskey.h:170
int(* save)(struct tls_key_schedule *tlskey, const void *nonce, size_t nonce_len, struct tls_preshared_key *psk)
Save pre-shared key.
Definition tlskey.h:298
int(* load)(struct tls_key_schedule *tlskey, const struct tls_preshared_key *psk)
Load pre-shared key.
Definition tlskey.h:307
unsigned int mask
Key flags preserved when loading key material.
Definition tlskey.h:187
int(* master)(struct tls_key_schedule *tlskey, int ems)
Generate master secret.
Definition tlskey.h:233
int(* cipher)(struct tls_key_schedule *tlskey, const struct tls_endpoint *writer, void *key, size_t key_len, void *iv, size_t iv_len, void *mac, size_t mac_len)
Generate cipher key material.
Definition tlskey.h:270
int(* bind)(const struct tls_preshared_key *psk, const void *hash, void *binder, size_t binder_len)
Generate pre-shared key binder value.
Definition tlskey.h:318
int(* verify)(struct tls_key_schedule *tlskey, const struct tls_endpoint *end, void *verify, size_t verify_len)
Generate verification data.
Definition tlskey.h:243
const char * name
Name.
Definition tlskey.h:172
size_t(* secretsize)(struct digest_algorithm *digest)
Calculate secret size.
Definition tlskey.h:194
int(* tbshash)(struct tls_key_schedule *tlskey, const struct tls_endpoint *end, struct digest_algorithm *digest, const void *data, size_t len, void *tbs)
Generate signable digest value.
Definition tlskey.h:285
int(* traffic)(struct tls_key_schedule *tlskey, const struct tls_endpoint *writer, const struct tls_phase *phase)
Generate traffic secrets.
Definition tlskey.h:254
int accumulates
Key schedule accumulates all shared secrets.
Definition tlskey.h:185
void(* expand)(struct digest_algorithm *digest, const void *secret, const char *label, const void *seed, size_t seed_len, void *out, size_t out_len)
Expand key material.
Definition tlskey.h:206
void(* reset)(struct tls_key_schedule *tlskey)
Reset key schedule.
Definition tlskey.h:215
int(* apply)(struct tls_key_schedule *tlskey, const void *shared, size_t shared_len)
Apply a new shared secret.
Definition tlskey.h:224
A TLS key schedule.
Definition tlskey.h:100
const struct tls_key_schedule_operations * op
Key schedule operations.
Definition tlskey.h:102
void * secret
Secret(s).
Definition tlskey.h:111
struct tls_random random[2]
Client and server random bytes.
Definition tlskey.h:120
size_t secretsize
Secret size.
Definition tlskey.h:106
struct tls_transcript transcript
Running handshake transcript digest.
Definition tlskey.h:116
struct tls_random nonce
Local endpoint random bytes.
Definition tlskey.h:118
unsigned int flags
Flags.
Definition tlskey.h:113
struct tls_traffic_secret traffic[2]
Traffic secrets.
Definition tlskey.h:124
struct digest_algorithm * digest
Digest algorithm.
Definition tlskey.h:104
void * dynamic
Dynamically-allocated storage.
Definition tlskey.h:109
struct tls_kdf_secret kdf
Key derivation function secret.
Definition tlskey.h:122
A TLS traffic phase.
Definition tlskey.c:127
A TLS pre-shared key.
Definition tlskey.h:128
uint8_t resumption[48]
Resumption secret.
Definition tlskey.h:165
struct digest_algorithm * digest
Digest algorithm (if set).
Definition tlskey.h:132
unsigned int flags
Key flags.
Definition tlskey.h:134
uint8_t md5[24]
Definition tlskey.h:151
uint8_t sha1[24]
Definition tlskey.h:152
uint8_t raw[48]
Definition tlskey.h:149
const struct tls_key_schedule_operations * op
Key schedule operations (if set).
Definition tlskey.h:130
TLS client or server random bytes.
Definition tlskey.h:36
uint8_t bytes[TLS_RANDOM_LEN]
Random bytes.
Definition tlskey.h:38
TLS server state.
Definition tls.h:431
A TLS traffic secret.
Definition tlskey.h:92
const struct tls_phase * phase
Phase.
Definition tlskey.h:96
void * secret
Secret.
Definition tlskey.h:94
A TLS running handshake transcript digest.
Definition tlskey.h:42
void * finishing
Running digest value up to the most recent Finished (if any).
Definition tlskey.h:63
void * running
Running digest value.
Definition tlskey.h:52
unsigned int flags
Transcript flags.
Definition tlskey.h:65
void * ctx
Digest context.
Definition tlskey.h:44
const struct tls_phase tls_early
Early traffic phase.
Definition tlskey.c:137
const struct tls_phase tls_handshake
Handshake traffic phase.
Definition tlskey.c:144
const struct tls_key_schedule_operations tlskey_hash
TLS key schedule based on P_Hash().
Definition tlskey.c:1819
const struct tls_key_schedule_operations tlskey_md5_sha1
TLS key schedule based on P_MD5()+P_SHA1().
Definition tlskey.c:1993
const struct tls_phase tls_application
Application traffic phase.
Definition tlskey.c:151
const struct tls_key_schedule_operations tlskey_hkdf
TLS key schedule based on HKDF.
Definition tlskey.c:1369
int tlskey_save(struct tls_key_schedule *tlskey, const void *nonce, size_t nonce_len, struct tls_preshared_key *psk)
Save pre-shared key.
Definition tlskey.c:785
int tlskey_master(struct tls_key_schedule *tlskey, int ems)
Generate master secret.
Definition tlskey.c:557
int tlskey_cipher(struct tls_key_schedule *tlskey, const struct tls_endpoint *writer, void *key, size_t key_len, void *iv, size_t iv_len, void *mac, size_t mac_len)
Generate cipher key material.
Definition tlskey.c:691
int tlskey_load(struct tls_key_schedule *tlskey, int ems, const struct tls_preshared_key *psk)
Load pre-shared key.
Definition tlskey.c:828
static int tlskey_is_accumulating(struct tls_key_schedule *tlskey)
Check if key schedule accumulates shared secrets.
Definition tlskey.h:329
void tlskey_stop(struct tls_key_schedule *tlskey)
Stop key schedule.
Definition tlskey.c:259
void tlskey_reset(struct tls_key_schedule *tlskey)
Reset key schedule.
Definition tlskey.c:492
int tlskey_start(struct tls_key_schedule *tlskey, const struct tls_key_schedule_operations *op, struct digest_algorithm *digest, const struct tls_random *random)
Start key schedule.
Definition tlskey.c:180
int tlskey_tbshash(struct tls_key_schedule *tlskey, const struct tls_endpoint *end, struct digest_algorithm *digest, const void *data, size_t len, void *tbs)
Generate signable digest value.
Definition tlskey.c:744
int tlskey_apply(struct tls_key_schedule *tlskey, const void *shared, size_t shared_len)
Apply a new shared secret.
Definition tlskey.c:522
int tlskey_traffic(struct tls_key_schedule *tlskey, const struct tls_endpoint *writer, const struct tls_phase *phase)
Generate traffic secret.
Definition tlskey.c:639
int tlskey_verify(struct tls_key_schedule *tlskey, const struct tls_endpoint *end, void *verify, size_t verify_len)
Generate verification data.
Definition tlskey.c:602
#define TLS_RANDOM_LEN
Number of random bytes.
Definition tlskey.h:33
void tlskey_digest(struct tls_key_schedule *tlskey, const void *data, size_t len)
Add handshake to running transcript digest.
Definition tlskey.c:416
int tlskey_bind(const struct tls_preshared_key *psk, const void *prefix, size_t prefix_len, void *binder, size_t binder_len)
Generate pre-shared key binder value.
Definition tlskey.c:885
char prefix[4]
Definition vmconsole.c:53
u8 iv[16]
Initialization vector.
Definition wpa.h:33
u8 nonce[32]
Nonce value.
Definition wpa.h:25