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 message hash prefix */
43 /** Message type */
45 /** Must be zero */
47 /** Hash length */
49} __attribute__ (( packed ));
50
51/** A TLS message hash */
52#define TLS_MESSAGE_HASH 254
53
54/** A TLS running handshake transcript digest */
56 /** Digest context */
57 void *ctx;
58 /**
59 * Running digest value
60 *
61 * This is continuously updated to represent the digest value
62 * over the transcript up to and including the most recently
63 * digested handshake record.
64 */
65 void *running;
66 /**
67 * Running digest value up to the most recent Finished (if any)
68 *
69 * If no Finished record has been digested, then this mirrors
70 * the continuously running digest value.
71 *
72 * If any Finished record has been digested, then this
73 * represents the digest value over the transcript up to and
74 * including the most recently digested Finished record.
75 */
76 void *finishing;
77 /** Transcript flags */
78 unsigned int flags;
79};
80
81/** Transcript digest includes a Hello record containing the local nonce */
82#define TLSKEY_TSF_NONCED 0x0001
83
84/** Transcript digest includes a Finished record */
85#define TLSKEY_TSF_FINISHED 0x0002
86
87/** A TLS key derivation function secret */
89 /** Secret */
90 void *secret;
91 /** Key flags */
92 unsigned int flags;
93};
94
95/** Key derivation function has key material */
96#define TLSKEY_KDF_KEYED 0x0001
97
98/** Key derivation function has a master secret */
99#define TLSKEY_KDF_MASTER 0x0002
100
101/** Key derivation function has an extended master secret */
102#define TLSKEY_KDF_EMS 0x0004
103
104/** A TLS traffic secret */
106 /** Secret */
107 void *secret;
108 /** Phase */
109 const struct tls_phase *phase;
110};
111
112/** A TLS key schedule */
114 /** Key schedule operations */
116 /** Digest algorithm */
118 /** Secret size */
120
121 /** Dynamically-allocated storage */
122 void *dynamic;
123 /** Secret(s) */
124 void *secret;
125 /** Flags */
126 unsigned int flags;
127
128 /** Running handshake transcript digest */
130 /** Local endpoint random bytes */
132 /** Client and server random bytes */
134 /** Key derivation function secret */
136 /** Traffic secrets */
138};
139
140/** A TLS pre-shared key */
142 /** Key schedule operations (if set) */
144 /** Digest algorithm (if set) */
146 /** Key flags */
147 unsigned int flags;
148 /** Key material */
149 union {
150 /**
151 * Master secret
152 *
153 * For TLSv1.2 and earlier, session resumption works
154 * by simply copying the 48-byte raw master secret
155 * value.
156 *
157 * For TLSv1.1 and earlier, this 48-byte master secret
158 * logically comprises two halves: 24 bytes for MD5
159 * and 24 bytes for SHA-1.
160 */
161 union {
163 struct {
166 } __attribute__ (( packed ));
167 } master_secret;
168 /**
169 * Resumption secret
170 *
171 * For TLSv1.3 and later, session resumption uses a
172 * pre-shared resumption secret value generated using
173 * the HKDF Derive-Secret function.
174 *
175 * The longest possible value for any supported HKDF
176 * digest algorithm is 48 bytes, when using SHA-384.
177 */
180};
181
182/** TLS key schedule operations */
184 /** Name */
185 const char *name;
186 /**
187 * Key schedule accumulates all shared secrets
188 *
189 * The ability to demonstrate possession of the key material
190 * is sufficient to demonstrate possession of the most
191 * recently applied shared secret.
192 *
193 * This flag indicates that the ability to demonstrate
194 * possession of the key material is also sufficient to
195 * demonstrate possession of all previously applied shared
196 * secrets.
197 */
199 /** Key flags preserved when loading key material */
200 unsigned int mask;
201 /**
202 * Calculate secret size
203 *
204 * @v digest Digest algorithm
205 * @ret secretsize Secret size, or zero if unsupported
206 */
207 size_t ( * secretsize ) ( struct digest_algorithm *digest );
208 /**
209 * Expand key material
210 *
211 * @v digest Digest algorithm
212 * @v secret Secret
213 * @v label Label string
214 * @v seed Seed material
215 * @v seed_len Length of seed material
216 * @v out Output buffer
217 * @v out_len Length of output buffer
218 */
219 void ( * expand ) ( struct digest_algorithm *digest,
220 const void *secret, const char *label,
221 const void *seed, size_t seed_len,
222 void *out, size_t out_len );
223 /**
224 * Reset key schedule
225 *
226 * @v tlskey Key schedule
227 */
228 void ( * reset ) ( struct tls_key_schedule *tlskey );
229 /**
230 * Apply a new shared secret
231 *
232 * @v tlskey Key schedule
233 * @v shared New shared secret
234 * @v shared_len Length of new shared secret
235 * @ret rc Return status code
236 */
237 int ( * apply ) ( struct tls_key_schedule *tlskey,
238 const void *shared, size_t shared_len );
239 /**
240 * Generate master secret
241 *
242 * @v tlskey Key schedule
243 * @v ems Extended master secret extension is enabled
244 * @ret rc Return status code
245 */
246 int ( * master ) ( struct tls_key_schedule *tlskey, int ems );
247 /**
248 * Generate verification data
249 *
250 * @v tlskey Key schedule
251 * @v end Verification endpoint
252 * @v verify Verification data
253 * @v verify_len Length of verification data
254 * @ret rc Return status code
255 */
256 int ( * verify ) ( struct tls_key_schedule *tlskey,
257 const struct tls_endpoint *end,
258 void *verify, size_t verify_len );
259 /**
260 * Generate traffic secrets
261 *
262 * @v tlskey Key schedule
263 * @v writer Writer endpoint
264 * @v phase Traffic phase
265 * @ret rc Return status code
266 */
267 int ( * traffic ) ( struct tls_key_schedule *tlskey,
268 const struct tls_endpoint *writer,
269 const struct tls_phase *phase );
270 /**
271 * Generate cipher key material
272 *
273 * @v tlskey Key schedule
274 * @v writer Writer endpoint
275 * @v key Cipher key to fill in
276 * @v key_len Length of cipher key
277 * @v iv Fixed portion of IV to fill in
278 * @v iv_len Length of fixed portion of IV
279 * @v mac MAC secret to fill in
280 * @v mac_len Length of MAC secret
281 * @ret rc Return status code
282 */
283 int ( * cipher ) ( struct tls_key_schedule *tlskey,
284 const struct tls_endpoint *writer,
285 void *key, size_t key_len, void *iv,
286 size_t iv_len, void *mac, size_t mac_len );
287 /**
288 * Generate signable digest value
289 *
290 * @v tlskey Key schedule
291 * @v end Endpoint
292 * @v digest Signature digest algorithm
293 * @v data Additional data
294 * @v len Length of additional data
295 * @v tbs Signature digest value to fill in
296 * @ret rc Return status code
297 */
298 int ( * tbshash ) ( struct tls_key_schedule *tlskey,
299 const struct tls_endpoint *end,
300 struct digest_algorithm *digest,
301 const void *data, size_t len, void *tbs );
302 /**
303 * Save pre-shared key
304 *
305 * @v tlskey Key schedule
306 * @v nonce Ticket nonce
307 * @v nonce_len Length of ticket nonce
308 * @v psk Pre-shared key to fill in
309 * @ret rc Return status code
310 */
311 int ( * save ) ( struct tls_key_schedule *tlskey, const void *nonce,
312 size_t nonce_len, struct tls_preshared_key *psk );
313 /**
314 * Load pre-shared key
315 *
316 * @v tlskey Key schedule
317 * @v psk Pre-shared key
318 * @ret rc Return status code
319 */
320 int ( * load ) ( struct tls_key_schedule *tlskey,
321 const struct tls_preshared_key *psk );
322 /**
323 * Generate pre-shared key binder value
324 *
325 * @v psk Pre-shared key
326 * @v hash Partial transcript hash
327 * @v binder Binder value to fill in
328 * @v binder_len Length of binder value
329 * @ret rc Return status code
330 */
331 int ( * bind ) ( const struct tls_preshared_key *psk,
332 const void *hash, void *binder, size_t binder_len );
333};
334
335/**
336 * Check if key schedule accumulates shared secrets
337 *
338 * @v tlskey Key schedule
339 * @ret is_accumulating Key schedule accumulates all shared secrets
340 */
341static inline __attribute__ (( always_inline )) int
343 const struct tls_key_schedule_operations *op = tlskey->op;
344
345 return ( op && op->accumulates );
346}
347
348extern const struct tls_endpoint tls_client;
349extern const struct tls_endpoint tls_server;
350extern const struct tls_phase tls_early;
351extern const struct tls_phase tls_handshake;
352extern const struct tls_phase tls_application;
353
354extern const struct tls_key_schedule_operations tlskey_hkdf;
355extern const struct tls_key_schedule_operations tlskey_hash;
357
358extern int tlskey_start ( struct tls_key_schedule *tlskey,
359 const struct tls_key_schedule_operations *op,
360 struct digest_algorithm *digest,
361 const struct tls_random *random );
362extern void tlskey_stop ( struct tls_key_schedule *tlskey );
363extern void tlskey_digest ( struct tls_key_schedule *tlskey,
364 const void *data, size_t len );
365extern void tlskey_message ( struct tls_key_schedule *tlskey );
366extern void tlskey_reset ( struct tls_key_schedule *tlskey );
367extern int tlskey_apply ( struct tls_key_schedule *tlskey,
368 const void *shared, size_t shared_len );
369extern int tlskey_master ( struct tls_key_schedule *tlskey, int ems );
370extern int tlskey_verify ( struct tls_key_schedule *tlskey,
371 const struct tls_endpoint *end,
372 void *verify, size_t verify_len );
373extern int tlskey_traffic ( struct tls_key_schedule *tlskey,
374 const struct tls_endpoint *writer,
375 const struct tls_phase *phase );
376extern int tlskey_cipher ( struct tls_key_schedule *tlskey,
377 const struct tls_endpoint *writer,
378 void *key, size_t key_len, void *iv,
379 size_t iv_len, void *mac, size_t mac_len );
380extern int tlskey_tbshash ( struct tls_key_schedule *tlskey,
381 const struct tls_endpoint *end,
382 struct digest_algorithm *digest,
383 const void *data, size_t len, void *tbs );
384extern int tlskey_save ( struct tls_key_schedule *tlskey, const void *nonce,
385 size_t nonce_len, struct tls_preshared_key *psk );
386extern int tlskey_load ( struct tls_key_schedule *tlskey, int ems,
387 const struct tls_preshared_key *psk );
388extern int tlskey_bind ( const struct tls_preshared_key *psk,
389 const void *prefix, size_t prefix_len,
390 void *binder, size_t binder_len );
391
392#endif /* _IPXE_TLSKEY_H */
__be32 out[4]
Definition CIB_PRM.h:8
union @162305117151260234136356364136041353210355154177 key
pseudo_bit_t hash[0x00010]
Definition arbel.h:2
unsigned short uint16_t
Definition stdint.h:11
__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:454
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:88
void * secret
Secret.
Definition tlskey.h:90
unsigned int flags
Key flags.
Definition tlskey.h:92
TLS key schedule operations.
Definition tlskey.h:183
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:311
int(* load)(struct tls_key_schedule *tlskey, const struct tls_preshared_key *psk)
Load pre-shared key.
Definition tlskey.h:320
unsigned int mask
Key flags preserved when loading key material.
Definition tlskey.h:200
int(* master)(struct tls_key_schedule *tlskey, int ems)
Generate master secret.
Definition tlskey.h:246
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:283
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:331
int(* verify)(struct tls_key_schedule *tlskey, const struct tls_endpoint *end, void *verify, size_t verify_len)
Generate verification data.
Definition tlskey.h:256
const char * name
Name.
Definition tlskey.h:185
size_t(* secretsize)(struct digest_algorithm *digest)
Calculate secret size.
Definition tlskey.h:207
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:298
int(* traffic)(struct tls_key_schedule *tlskey, const struct tls_endpoint *writer, const struct tls_phase *phase)
Generate traffic secrets.
Definition tlskey.h:267
int accumulates
Key schedule accumulates all shared secrets.
Definition tlskey.h:198
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:219
void(* reset)(struct tls_key_schedule *tlskey)
Reset key schedule.
Definition tlskey.h:228
int(* apply)(struct tls_key_schedule *tlskey, const void *shared, size_t shared_len)
Apply a new shared secret.
Definition tlskey.h:237
A TLS key schedule.
Definition tlskey.h:113
const struct tls_key_schedule_operations * op
Key schedule operations.
Definition tlskey.h:115
void * secret
Secret(s).
Definition tlskey.h:124
struct tls_random random[2]
Client and server random bytes.
Definition tlskey.h:133
size_t secretsize
Secret size.
Definition tlskey.h:119
struct tls_transcript transcript
Running handshake transcript digest.
Definition tlskey.h:129
struct tls_random nonce
Local endpoint random bytes.
Definition tlskey.h:131
unsigned int flags
Flags.
Definition tlskey.h:126
struct tls_traffic_secret traffic[2]
Traffic secrets.
Definition tlskey.h:137
struct digest_algorithm * digest
Digest algorithm.
Definition tlskey.h:117
void * dynamic
Dynamically-allocated storage.
Definition tlskey.h:122
struct tls_kdf_secret kdf
Key derivation function secret.
Definition tlskey.h:135
A message hash prefix.
Definition tlskey.h:42
uint8_t len
Hash length.
Definition tlskey.h:48
uint16_t zero
Must be zero.
Definition tlskey.h:46
uint8_t type
Message type.
Definition tlskey.h:44
A TLS traffic phase.
Definition tlskey.c:127
A TLS pre-shared key.
Definition tlskey.h:141
uint8_t resumption[48]
Resumption secret.
Definition tlskey.h:178
struct digest_algorithm * digest
Digest algorithm (if set).
Definition tlskey.h:145
unsigned int flags
Key flags.
Definition tlskey.h:147
uint8_t md5[24]
Definition tlskey.h:164
uint8_t sha1[24]
Definition tlskey.h:165
uint8_t raw[48]
Definition tlskey.h:162
const struct tls_key_schedule_operations * op
Key schedule operations (if set).
Definition tlskey.h:143
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:464
A TLS traffic secret.
Definition tlskey.h:105
const struct tls_phase * phase
Phase.
Definition tlskey.h:109
void * secret
Secret.
Definition tlskey.h:107
A TLS running handshake transcript digest.
Definition tlskey.h:55
void * finishing
Running digest value up to the most recent Finished (if any).
Definition tlskey.h:76
void * running
Running digest value.
Definition tlskey.h:65
unsigned int flags
Transcript flags.
Definition tlskey.h:78
void * ctx
Digest context.
Definition tlskey.h:57
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:1868
const struct tls_key_schedule_operations tlskey_md5_sha1
TLS key schedule based on P_MD5()+P_SHA1().
Definition tlskey.c:2097
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:1407
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:823
int tlskey_master(struct tls_key_schedule *tlskey, int ems)
Generate master secret.
Definition tlskey.c:585
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:719
int tlskey_load(struct tls_key_schedule *tlskey, int ems, const struct tls_preshared_key *psk)
Load pre-shared key.
Definition tlskey.c:866
static int tlskey_is_accumulating(struct tls_key_schedule *tlskey)
Check if key schedule accumulates shared secrets.
Definition tlskey.h:342
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:520
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:772
int tlskey_apply(struct tls_key_schedule *tlskey, const void *shared, size_t shared_len)
Apply a new shared secret.
Definition tlskey.c:550
int tlskey_traffic(struct tls_key_schedule *tlskey, const struct tls_endpoint *writer, const struct tls_phase *phase)
Generate traffic secret.
Definition tlskey.c:667
void tlskey_message(struct tls_key_schedule *tlskey)
Replace running transcript digest with a message hash of itself.
Definition tlskey.c:457
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:630
#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:923
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