iPXE
hkdf.h File Reference

HMAC-based Extract-and-Expand Key Derivation Function (HKDF). More...

#include <ipxe/crypto.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
void hkdf_extract (struct digest_algorithm *digest, const void *salt, size_t salt_len, const void *ikm, size_t ikm_len, void *prk)
 Extract fixed-length pseudorandom key.
void hkdf_expand (struct digest_algorithm *digest, const void *prk, const void *info, size_t info_len, void *out, size_t len)
 Expand pseudorandom key.

Detailed Description

HMAC-based Extract-and-Expand Key Derivation Function (HKDF).

Definition in file hkdf.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

References info, info_len, len, and out.

◆ hkdf_extract()

void hkdf_extract ( struct digest_algorithm * digest,
const void * salt,
size_t salt_len,
const void * ikm,
size_t ikm_len,
void * prk )
extern

Extract fixed-length pseudorandom key.

Parameters
digestDigest algorithm
saltSalt value (or NULL)
salt_lenLength of salt value
ikmInput keying material
ikm_lenLength of input keying material
prkPseudorandom key to fill in

The salt and input keying material are allowed to overlap the buffer that will be filled in with the pseudorandom key.

Definition at line 56 of file hkdf.c.

58 {
59 uint8_t ctx[ hmac_ctxsize ( digest ) ];
60 uint8_t zero[ digest->digestsize ];
61
62 /* Use all-zero salt if not specified */
63 if ( ! salt ) {
64 assert ( salt_len == 0 );
65 memset ( zero, 0, sizeof ( zero ) );
66 salt = zero;
67 salt_len = sizeof ( zero );
68 }
69
70 /* Calculate pseudorandom key */
71 hmac_init ( digest, ctx, salt, salt_len );
72 hmac_update ( digest, ctx, ikm, ikm_len );
73 hmac_final ( digest, ctx, prk );
74}
struct golan_eq_context ctx
Definition CIB_PRM.h:0
unsigned char uint8_t
Definition stdint.h:10
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:61
void hmac_init(struct digest_algorithm *digest, void *ctx, const void *secret, size_t len)
Initialise HMAC.
Definition hmac.c:106
void hmac_final(struct digest_algorithm *digest, void *ctx, void *hmac)
Finalise HMAC.
Definition hmac.c:124
static void hmac_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Update HMAC.
Definition hmac.h:62
static size_t hmac_ctxsize(struct digest_algorithm *digest)
Calculate HMAC context size.
Definition hmac.h:48
void * memset(void *dest, int character, size_t len) __nonnull
size_t digestsize
Digest size.
Definition crypto.h:27

References assert, ctx, digest_algorithm::digestsize, hmac_ctxsize(), hmac_final(), hmac_init(), hmac_update(), and memset().

Referenced by hkdf_okx(), and tls_generate_ephemeral_master().

◆ hkdf_expand()

void hkdf_expand ( struct digest_algorithm * digest,
const void * prk,
const void * info,
size_t info_len,
void * out,
size_t len )
extern

Expand pseudorandom key.

Parameters
digestDigest algorithm
prkPseudorandom key
infoAdditional information (or NULL)
info_lenLength of additional information
outOutput keying material
lenLength of output keying material

The pseudorandom key and additional information are allowed to overlap the buffer that will be filled in with the output keying material, provided that the length of the output keying material is less than or equal to the length of the pseudorandom key.

In particular, this allows hkdf_expand() to be used to create a new generation of pseudorandom key (i.e. to have the output keying material overwrite the existing pseudorandom key).

Definition at line 95 of file hkdf.c.

96 {
97 size_t digestsize = digest->digestsize;
98 uint8_t ctx[ hmac_ctxsize ( digest ) ];
100 uint8_t index = 0;
101 size_t frag_len;
102
103 /* Sanity check */
104 assert ( len <= ( digestsize * 0xff /* max index */ ) );
105
106 /* Construct output keying material */
107 while ( len ) {
108
109 /* Calculate T(n) */
110 hmac_init ( digest, ctx, prk, digestsize );
111 if ( index++ )
112 hmac_update ( digest, ctx, hash, digestsize );
113 hmac_update ( digest, ctx, info, info_len );
114 hmac_update ( digest, ctx, &index, sizeof ( index ) );
115 hmac_final ( digest, ctx, hash );
116
117 /* Copy to output buffer */
118 frag_len = len;
119 if ( frag_len > digestsize )
120 frag_len = digestsize;
121 memcpy ( out, hash, frag_len );
122 out += frag_len;
123 len -= frag_len;
124 }
125}
__be32 out[4]
Definition CIB_PRM.h:8
u32 info
Definition ar9003_mac.h:0
pseudo_bit_t hash[0x00010]
Definition arbel.h:2
long index
Definition bigint.h:30
ring len
Length.
Definition dwmac.h:226
uint8_t info_len
Reject information length.
Definition ib_mad.h:7
void * memcpy(void *dest, const void *src, size_t len) __nonnull
uint32_t digestsize
Digest size (i.e.
Definition pccrr.h:1

References assert, ctx, digest_algorithm::digestsize, digestsize, hash, hmac_ctxsize(), hmac_final(), hmac_init(), hmac_update(), index, info, info_len, len, memcpy(), and out.

Referenced by hkdf_okx(), and tls_ephemeral().