iPXE
hmac.c File Reference

Keyed-Hashing for Message Authentication. More...

#include <string.h>
#include <assert.h>
#include <ipxe/crypto.h>
#include <ipxe/hmac.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
void hmac_key (struct digest_algorithm *digest, void *ctx, const void *secret, size_t len, void *key)
 Construct HMAC reduced key.
void hmac_init_key (struct digest_algorithm *digest, void *ctx, const void *key)
 Initialise HMAC from reduced key.
void hmac_init (struct digest_algorithm *digest, void *ctx, const void *secret, size_t len)
 Initialise HMAC.
void hmac_final (struct digest_algorithm *digest, void *ctx, void *hmac)
 Finalise HMAC.

Detailed Description

Keyed-Hashing for Message Authentication.

Definition in file hmac.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ hmac_key()

void hmac_key ( struct digest_algorithm * digest,
void * ctx,
const void * secret,
size_t len,
void * key )

Construct HMAC reduced key.

Parameters
digestDigest algorithm to use
ctxHMAC context
secretSecret key
lenLength of secret key
keyHMAC reduced key to fill in

Definition at line 59 of file hmac.c.

60 {
61 hmac_context_t ( digest ) *hctx = ctx;
62 hmac_key_t ( digest ) *hkey = key;
63
64 /* Reduce key (if applicable) and zero-pad to block size */
65 memset ( hkey->pad, 0, sizeof ( hkey->pad ) );
66 if ( len <= sizeof ( hkey->pad ) ) {
67 memcpy ( hkey->pad, secret, len );
68 } else {
69 digest_init ( digest, hctx->ctx );
70 digest_update ( digest, hctx->ctx, secret, len );
71 digest_final ( digest, hctx->ctx, hkey->pad );
72 }
73}
struct golan_eq_context ctx
Definition CIB_PRM.h:0
union @162305117151260234136356364136041353210355154177 key
ring len
Length.
Definition dwmac.h:226
#define hmac_key_t(digest)
HMAC reduced key type.
Definition hmac.h:15
#define hmac_context_t(digest)
HMAC context type.
Definition hmac.h:21
static void digest_init(struct digest_algorithm *digest, void *ctx)
Definition crypto.h:294
static void digest_final(struct digest_algorithm *digest, void *ctx, void *out)
Definition crypto.h:305
static void digest_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Definition crypto.h:299
void * memcpy(void *dest, const void *src, size_t len) __nonnull
void * memset(void *dest, int character, size_t len) __nonnull

References ctx, digest_final(), digest_init(), digest_update(), hmac_context_t, hmac_key_t, key, len, memcpy(), and memset().

Referenced by hmac_init(), hmac_okx(), and tls_set_kdf_master().

◆ hmac_init_key()

void hmac_init_key ( struct digest_algorithm * digest,
void * ctx,
const void * key )

Initialise HMAC from reduced key.

Parameters
digestDigest algorithm
ctxHMAC context
hkeyHMAC key

Definition at line 82 of file hmac.c.

83 {
84 hmac_context_t ( digest ) *hctx = ctx;
85 const hmac_key_t ( digest ) *hkey = key;
86 unsigned int i;
87
88 /* Construct input pad */
89 for ( i = 0 ; i < sizeof ( hkey->pad ) ; i++ ) {
90 hctx->pad[i] = ( hkey->pad[i] ^ 0x36 );
91 }
92
93 /* Start inner hash */
94 digest_init ( digest, hctx->ctx );
95 digest_update ( digest, hctx->ctx, hctx->pad, sizeof ( hctx->pad ) );
96}

References ctx, digest_init(), digest_update(), hmac_context_t, hmac_key_t, and key.

Referenced by hmac_init(), hmac_okx(), and tls_p_hash_va().

◆ hmac_init()

void hmac_init ( struct digest_algorithm * digest,
void * ctx,
const void * secret,
size_t len )

Initialise HMAC.

Parameters
digestDigest algorithm to use
ctxHMAC context
secretSecret key
lenLength of secret key

Definition at line 106 of file hmac.c.

107 {
108 hmac_context_t ( digest ) *hctx = ctx;
109
110 /* Construct reduced key (in input pad) */
111 hmac_key ( digest, hctx->ctx, secret, len, hctx->pad );
112
113 /* Initialise HMAC */
114 hmac_init_key ( digest, hctx->ctx, hctx->pad );
115}
void hmac_key(struct digest_algorithm *digest, void *ctx, const void *secret, size_t len, void *key)
Construct HMAC reduced key.
Definition hmac.c:59
void hmac_init_key(struct digest_algorithm *digest, void *ctx, const void *key)
Initialise HMAC from reduced key.
Definition hmac.c:82

References ctx, hmac_context_t, hmac_init_key(), hmac_key(), and len.

Referenced by ccmp_kie_mic(), hkdf_expand(), hkdf_extract(), hmac_drbg_update_key(), hmac_drbg_update_value(), hmac_okx(), ntlm_key(), ntlm_response(), pbkdf2_sha1_f(), peerdist_info_passphrase_okx(), peerdist_info_segment_hash(), prf_sha1(), tkip_kie_mic(), and tls_hmac_init().

◆ hmac_final()

void hmac_final ( struct digest_algorithm * digest,
void * ctx,
void * hmac )

Finalise HMAC.

Parameters
digestDigest algorithm to use
ctxHMAC context
hmacHMAC digest to fill in

Definition at line 124 of file hmac.c.

124 {
125 hmac_context_t ( digest ) *hctx = ctx;
126 unsigned int i;
127
128 /* Construct output pad from input pad */
129 for ( i = 0 ; i < sizeof ( hctx->pad ) ; i++ ) {
130 hctx->pad[i] ^= 0x6a;
131 }
132
133 /* Finish inner hash */
134 digest_final ( digest, hctx->ctx, hmac );
135
136 /* Perform outer hash */
137 digest_init ( digest, hctx->ctx );
138 digest_update ( digest, hctx->ctx, hctx->pad, sizeof ( hctx->pad ) );
139 digest_update ( digest, hctx->ctx, hmac, digest->digestsize );
140 digest_final ( digest, hctx->ctx, hmac );
141
142 /* Erase output pad (from which the key may be derivable) */
143 memset ( hctx->pad, 0, sizeof ( hctx->pad ) );
144}
size_t digestsize
Digest size.
Definition crypto.h:27

References ctx, digest_final(), digest_init(), digest_update(), digest_algorithm::digestsize, hmac_context_t, and memset().

Referenced by ccmp_kie_mic(), hkdf_expand(), hkdf_extract(), hmac_drbg_update_key(), hmac_drbg_update_value(), hmac_okx(), ntlm_key(), ntlm_response(), pbkdf2_sha1_f(), peerdist_info_passphrase_okx(), peerdist_info_segment_hash(), prf_sha1(), tkip_kie_mic(), tls_hmac_final(), and tls_p_hash_va().