iPXE
sha1.h File Reference

SHA-1 algorithm. More...

#include <stdint.h>
#include <ipxe/crypto.h>

Go to the source code of this file.

Data Structures

struct  sha1_digest
 An SHA-1 digest. More...
union  sha1_block
 An SHA-1 data block. More...
struct  sha1_digest_data
 SHA-1 digest and data block. More...
union  sha1_digest_data_dwords
 SHA-1 digest and data block. More...
struct  sha1_context
 An SHA-1 context. More...

Macros

#define SHA1_CTX_SIZE   sizeof ( struct sha1_context )
 SHA-1 context size.
#define SHA1_BLOCK_SIZE   sizeof ( union sha1_block )
 SHA-1 block size.
#define SHA1_DIGEST_SIZE   sizeof ( struct sha1_digest )
 SHA-1 digest size.

Functions

 FILE_LICENCE (GPL2_OR_LATER)
 FILE_SECBOOT (PERMITTED)
void prf_sha1 (const void *key, size_t key_len, const char *label, const void *data, size_t data_len, void *prf, size_t prf_len)
 SHA1 pseudorandom function for creating derived keys.
void pbkdf2_sha1 (const void *passphrase, size_t pass_len, const void *salt, size_t salt_len, int iterations, void *key, size_t key_len)
 PBKDF2 key derivation function using SHA1.

Variables

struct digest_algorithm sha1_algorithm
 SHA-1 algorithm.

Detailed Description

SHA-1 algorithm.

Definition in file sha1.h.

Macro Definition Documentation

◆ SHA1_CTX_SIZE

#define SHA1_CTX_SIZE   sizeof ( struct sha1_context )

SHA-1 context size.

Definition at line 67 of file sha1.h.

Referenced by ccmp_kie_mic(), ocsp_compare_responder_key_hash(), pbkdf2_sha1_f(), and prf_sha1().

◆ SHA1_BLOCK_SIZE

#define SHA1_BLOCK_SIZE   sizeof ( union sha1_block )

SHA-1 block size.

Definition at line 70 of file sha1.h.

◆ SHA1_DIGEST_SIZE

#define SHA1_DIGEST_SIZE   sizeof ( struct sha1_digest )

SHA-1 digest size.

Definition at line 73 of file sha1.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ prf_sha1()

void prf_sha1 ( const void * key,
size_t key_len,
const char * label,
const void * data,
size_t data_len,
void * prf,
size_t prf_len )
extern

SHA1 pseudorandom function for creating derived keys.

Parameters
keyMaster key with which this call is associated
key_lenLength of key
labelNUL-terminated ASCII string describing purpose of PRF data
dataFurther data that should be included in the PRF
data_lenLength of further PRF data
prf_lenBytes of PRF to generate
Return values
prfPseudorandom function bytes

This is the PRF variant used by 802.11, defined in IEEE 802.11-2007 8.5.5.1. EAP-FAST uses a different SHA1-based PRF, and TLS uses an MD5-based PRF.

Definition at line 44 of file sha1extra.c.

46{
47 u32 blk;
48 u8 keym[key_len]; /* modifiable copy of key */
49 u8 in[strlen ( label ) + 1 + data_len + 1]; /* message to HMAC */
50 u8 *in_blknr; /* pointer to last byte of in, block number */
51 u8 out[SHA1_DIGEST_SIZE]; /* HMAC-SHA1 result */
52 u8 ctx[SHA1_CTX_SIZE + SHA1_BLOCK_SIZE]; /* HMAC-SHA1 context */
53 const size_t label_len = strlen ( label );
54
55 /* The HMAC-SHA-1 is calculated using the given key on the
56 message text `label', followed by a NUL, followed by one
57 byte indicating the block number (0 for first). */
58
59 memcpy ( keym, key, key_len );
60
61 memcpy ( in, label, strlen ( label ) + 1 );
62 memcpy ( in + label_len + 1, data, data_len );
63 in_blknr = in + label_len + 1 + data_len;
64
65 for ( blk = 0 ;; blk++ ) {
66 *in_blknr = blk;
67
68 hmac_init ( &sha1_algorithm, ctx, keym, key_len );
69 hmac_update ( &sha1_algorithm, ctx, in, sizeof ( in ) );
71
72 if ( prf_len <= sizeof ( out ) ) {
73 memcpy ( prf, out, prf_len );
74 break;
75 }
76
77 memcpy ( prf, out, sizeof ( out ) );
78 prf_len -= sizeof ( out );
79 prf += sizeof ( out );
80 }
81}
union @162305117151260234136356364136041353210355154177 key
Sense key.
Definition scsi.h:3
struct golan_eq_context ctx
Definition CIB_PRM.h:0
__be32 in[4]
Definition CIB_PRM.h:7
__be32 out[4]
Definition CIB_PRM.h:8
#define SHA1_DIGEST_SIZE
Definition Tpm20.h:26
#define SHA1_BLOCK_SIZE
Definition Tpm20.h:27
uint8_t data[48]
Additional event data.
Definition ena.h:11
void hmac_init(struct digest_algorithm *digest, void *ctx, const void *key, size_t key_len)
Initialise HMAC.
Definition hmac.c:58
void hmac_final(struct digest_algorithm *digest, void *ctx, void *hmac)
Finalise HMAC.
Definition hmac.c:88
static void hmac_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Update HMAC.
Definition hmac.h:43
#define u8
Definition igbvf_osdep.h:40
void * memcpy(void *dest, const void *src, size_t len) __nonnull
struct digest_algorithm sha1_algorithm
SHA-1 algorithm.
Definition sha1.c:258
#define SHA1_CTX_SIZE
SHA-1 context size.
Definition sha1.h:67
size_t strlen(const char *src)
Get length of string.
Definition string.c:244
A text label widget.
Definition label.h:16
uint32_t data_len
Microcode data size (or 0 to indicate 2000 bytes)
Definition ucode.h:15
#define u32
Definition vga.h:21

References ctx, data, data_len, hmac_final(), hmac_init(), hmac_update(), in, key, memcpy(), out, sha1_algorithm, SHA1_BLOCK_SIZE, SHA1_CTX_SIZE, SHA1_DIGEST_SIZE, strlen(), u32, and u8.

Referenced by wpa_derive_ptk().

◆ pbkdf2_sha1()

void pbkdf2_sha1 ( const void * passphrase,
size_t pass_len,
const void * salt,
size_t salt_len,
int iterations,
void * key,
size_t key_len )
extern

PBKDF2 key derivation function using SHA1.

Parameters
passphrasePassphrase from which to derive key
pass_lenLength of passphrase
saltSalt to include in key
salt_lenLength of salt
iterationsNumber of iterations of SHA1 to perform
key_lenLength of key to generate
Return values
keyGenerated key bytes

This is used most notably in 802.11 WPA passphrase hashing, in which case the salt is the SSID, 4096 iterations are used, and a 32-byte key is generated that serves as the Pairwise Master Key for EAPOL authentication.

The operation of this function is further described in RFC 2898.

Definition at line 148 of file sha1extra.c.

151{
152 u32 blocks = ( key_len + SHA1_DIGEST_SIZE - 1 ) / SHA1_DIGEST_SIZE;
153 u32 blk;
154 u8 buf[SHA1_DIGEST_SIZE];
155
156 for ( blk = 1; blk <= blocks; blk++ ) {
157 pbkdf2_sha1_f ( passphrase, pass_len, salt, salt_len,
158 iterations, blk, buf );
159 if ( key_len <= sizeof ( buf ) ) {
160 memcpy ( key, buf, key_len );
161 break;
162 }
163
164 memcpy ( key, buf, sizeof ( buf ) );
165 key_len -= sizeof ( buf );
166 key += sizeof ( buf );
167 }
168}
static void pbkdf2_sha1_f(const void *passphrase, size_t pass_len, const void *salt, size_t salt_len, int iterations, u32 blocknr, u8 *block)
PBKDF2 key derivation function inner block operation.
Definition sha1extra.c:96

References key, memcpy(), pbkdf2_sha1_f(), SHA1_DIGEST_SIZE, u32, and u8.

Referenced by wpa_psk_start().

Variable Documentation

◆ sha1_algorithm

struct digest_algorithm sha1_algorithm
extern

SHA-1 algorithm.

Definition at line 258 of file sha1.c.

258 {
259 .name = "sha1",
260 .ctxsize = sizeof ( struct sha1_context ),
261 .blocksize = sizeof ( union sha1_block ),
262 .digestsize = sizeof ( struct sha1_digest ),
263 .init = sha1_init,
264 .update = sha1_update,
265 .final = sha1_final,
266};
uint32_t digestsize
Digest size (i.e.
Definition pccrr.h:1
static void sha1_final(void *ctx, void *out)
Generate SHA-1 digest.
Definition sha1.c:232
static void sha1_init(void *ctx)
Initialise SHA-1 algorithm.
Definition sha1.c:114
static void sha1_update(void *ctx, const void *data, size_t len)
Accumulate data with SHA-1 algorithm.
Definition sha1.c:209
An SHA-1 context.
Definition sha1.h:59
An SHA-1 digest.
Definition sha1.h:17
An SHA-1 data block.
Definition sha1.h:23

Referenced by __tls_cipher_suite(), __tls_cipher_suite(), __tls_cipher_suite(), __tls_cipher_suite(), __tls_cipher_suite(), __tls_cipher_suite(), ccmp_kie_mic(), certstat(), DIGEST_TEST(), DIGEST_TEST(), DIGEST_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HASH_DF_TEST(), HMAC_TEST(), md5_sha1_final(), md5_sha1_init(), md5_sha1_update(), mschapv2_auth(), mschapv2_challenge_hash(), ocsp_compare_responder_key_hash(), pbkdf2_sha1_f(), prf_sha1(), PUBKEY_SIGN_TEST(), sha1_test_exec(), sha1sum_exec(), tls_prf(), and x509_name().