iPXE
hmac_test.c File Reference

HMAC self-tests. More...

#include <string.h>
#include <ipxe/hmac.h>
#include <ipxe/md5.h>
#include <ipxe/sha1.h>
#include <ipxe/sha256.h>
#include <ipxe/test.h>

Go to the source code of this file.

Data Structures

struct  hmac_test
 An HMAC test. More...

Macros

#define KEY(...)
 Define inline key data.
#define DATA(...)
 Define inline data.
#define EXPECTED(...)
 Define inline expected HMAC.
#define HMAC_TEST(name, DIGEST, KEY, DATA, EXPECTED)
 Define an HMAC test.
#define hmac_ok(test)

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static void hmac_okx (struct hmac_test *test, const char *file, unsigned int line)
 Report an HMAC test result.
 HMAC_TEST (hmac_empty, &sha256_algorithm, KEY(), DATA(), EXPECTED(0xb6, 0x13, 0x67, 0x9a, 0x08, 0x14, 0xd9, 0xec, 0x77, 0x2f, 0x95, 0xd7, 0x78, 0xc3, 0x5f, 0xc5, 0xff, 0x16, 0x97, 0xc4, 0x93, 0x71, 0x56, 0x53, 0xc6, 0xc7, 0x12, 0x14, 0x42, 0x92, 0xc5, 0xad))
 HMAC_TEST (hmac_hw, &sha256_algorithm, KEY( 'H', 'e', 'l', 'l', 'o'), DATA( 'W', 'o', 'r', 'l', 'd'), EXPECTED(0x59, 0x16, 0x8e, 0x30, 0x9f, 0x2c, 0x97, 0xdd, 0x04, 0xe4, 0x5b, 0xe3, 0xe7, 0x9b, 0xd9, 0xac, 0xb6, 0xd2, 0x2f, 0xda, 0x65, 0x46, 0xc0, 0x0c, 0x53, 0x92, 0x82, 0xc4, 0x1e, 0xeb, 0x91, 0x6e))
 HMAC_TEST (hmac_hw_sha1, &sha1_algorithm, KEY( 'H', 'e', 'l', 'l', 'o'), DATA( 'W', 'o', 'r', 'l', 'd'), EXPECTED(0x9e, 0x29, 0xcf, 0x6d, 0x48, 0x90, 0x49, 0x9e, 0xf8, 0x5a, 0x31, 0x47, 0x55, 0x7b, 0x1a, 0x45, 0xd5, 0xae, 0xd1, 0x77))
 HMAC_TEST (hmac_hw_md5, &md5_algorithm, KEY( 'H', 'e', 'l', 'l', 'o'), DATA( 'W', 'o', 'r', 'l', 'd'), EXPECTED(0xc7, 0x1c, 0x0c, 0xd0, 0xdc, 0x24, 0x49, 0xbd, 0xd8, 0x9d, 0x28, 0xeb, 0x03, 0xbe, 0xf2, 0x04))
 HMAC_TEST (hmac_maxlen, &sha256_algorithm, KEY(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f), DATA( 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'), EXPECTED(0xdd, 0x05, 0xcc, 0xe6, 0xd6, 0xaf, 0x91, 0x61, 0x4b, 0xaf, 0x35, 0x6b, 0x86, 0x0a, 0x05, 0x67, 0x25, 0x22, 0xf0, 0x54, 0xd2, 0x5f, 0xd7, 0xe1, 0x54, 0x26, 0x01, 0x16, 0xfd, 0x8a, 0xf3, 0x5b))
 HMAC_TEST (hmac_overlen, &sha256_algorithm, KEY(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40), DATA( 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'), EXPECTED(0x4f, 0x0f, 0x42, 0x07, 0xda, 0x84, 0x3d, 0x2a, 0x34, 0xae, 0x5d, 0xd9, 0x05, 0x1e, 0x96, 0xa8, 0xb6, 0xef, 0xa1, 0xcd, 0x49, 0x5b, 0xea, 0x30, 0xbf, 0x47, 0x3b, 0xdc, 0xa9, 0x86, 0xbb, 0x31))
static void hmac_test_exec (void)
 Perform HMAC self-tests.

Variables

struct self_test hmac_test __self_test
 HMAC self-tests.

Detailed Description

HMAC self-tests.

Definition in file hmac_test.c.

Macro Definition Documentation

◆ KEY

#define KEY ( ...)
Value:
{ __VA_ARGS__ }

Define inline key data.

Definition at line 43 of file hmac_test.c.

◆ DATA

#define DATA ( ...)
Value:
{ __VA_ARGS__ }

Define inline data.

Definition at line 46 of file hmac_test.c.

◆ EXPECTED

#define EXPECTED ( ...)
Value:
{ __VA_ARGS__ }

Define inline expected HMAC.

Definition at line 49 of file hmac_test.c.

◆ HMAC_TEST

#define HMAC_TEST ( name,
DIGEST,
KEY,
DATA,
EXPECTED )
Value:
static const uint8_t name ## _key[] = KEY; \
static const uint8_t name ## _data[] = DATA; \
static const uint8_t name ## _expected[] = EXPECTED; \
static struct hmac_test name = { \
.digest = DIGEST, \
.key = name ## _key, \
.key_len = sizeof ( name ## _key ), \
.data = name ## _data, \
.data_len = sizeof ( name ## _data ), \
.expected = name ## _expected, \
.expected_len = sizeof ( name ## _expected ), \
}
#define DATA(...)
Define inline data.
Definition acpi_test.c:74
unsigned char uint8_t
Definition stdint.h:10
const char * name
Definition ath9k_hw.c:1986
#define KEY(...)
Define inline key.
Definition cipher_test.h:45
#define DIGEST(...)
Define inline expected digest.
Definition der_test.c:45
#define EXPECTED(...)
Define inline expected result point.
uint8_t data[48]
Additional event data.
Definition ena.h:11
An HMAC test.
Definition hmac_test.c:52
uint32_t data_len
Microcode data size (or 0 to indicate 2000 bytes)
Definition ucode.h:15

Define an HMAC test.

Parameters
nameTest name
DIGESTDigest algorithm
KEYKey
DATAData
EXPECTEDExpected HMAC
Return values
testHMAC test

Definition at line 79 of file hmac_test.c.

79#define HMAC_TEST( name, DIGEST, KEY, DATA, EXPECTED ) \
80 static const uint8_t name ## _key[] = KEY; \
81 static const uint8_t name ## _data[] = DATA; \
82 static const uint8_t name ## _expected[] = EXPECTED; \
83 static struct hmac_test name = { \
84 .digest = DIGEST, \
85 .key = name ## _key, \
86 .key_len = sizeof ( name ## _key ), \
87 .data = name ## _data, \
88 .data_len = sizeof ( name ## _data ), \
89 .expected = name ## _expected, \
90 .expected_len = sizeof ( name ## _expected ), \
91 }

◆ hmac_ok

#define hmac_ok ( test)
Value:
hmac_okx ( test, __FILE__, __LINE__ )
static int test
Definition epic100.c:73
static void hmac_okx(struct hmac_test *test, const char *file, unsigned int line)
Report an HMAC test result.
Definition hmac_test.c:100

Definition at line 126 of file hmac_test.c.

Referenced by hmac_test_exec().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ hmac_okx()

void hmac_okx ( struct hmac_test * test,
const char * file,
unsigned int line )
static

Report an HMAC test result.

Parameters
testHMAC test
fileTest code file
lineTest code line

Definition at line 100 of file hmac_test.c.

101 {
102 struct digest_algorithm *digest = test->digest;
103 uint8_t ctx[ hmac_ctxsize ( digest ) ];
104 uint8_t hmac[digest->digestsize];
105
106 /* Sanity checks */
107 okx ( sizeof ( ctx ) == ( digest->ctxsize + digest->blocksize ),
108 file, line );
109 okx ( test->expected_len == digest->digestsize, file, line );
110
111 /* Calculate HMAC */
112 DBGC ( test, "HMAC-%s key:\n", digest->name );
113 DBGC_HDA ( test, 0, test->key, test->key_len );
114 DBGC ( test, "HMAC-%s data:\n", digest->name );
115 DBGC_HDA ( test, 0, test->data, test->data_len );
116 hmac_init ( digest, ctx, test->key, test->key_len );
117 hmac_update ( digest, ctx, test->data, test->data_len );
118 hmac_final ( digest, ctx, hmac );
119 DBGC ( test, "HMAC-%s result:\n", digest->name );
120 DBGC_HDA ( test, 0, hmac, sizeof ( hmac ) );
121
122 /* Compare against expected result */
123 okx ( memcmp ( hmac, test->expected, test->expected_len ) == 0,
124 file, line );
125}
struct golan_eq_context ctx
Definition CIB_PRM.h:0
#define DBGC(...)
Definition compiler.h:505
#define DBGC_HDA(...)
Definition compiler.h:506
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
static size_t hmac_ctxsize(struct digest_algorithm *digest)
Calculate HMAC context size.
Definition hmac.h:29
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115
A message digest algorithm.
Definition crypto.h:19
size_t digestsize
Digest size.
Definition crypto.h:27
size_t blocksize
Block size.
Definition crypto.h:25
size_t ctxsize
Context size.
Definition crypto.h:23
const char * name
Algorithm name.
Definition crypto.h:21
#define okx(success, file, line)
Report test result.
Definition test.h:44

References digest_algorithm::blocksize, ctx, digest_algorithm::ctxsize, DBGC, DBGC_HDA, digest_algorithm::digestsize, hmac_ctxsize(), hmac_final(), hmac_init(), hmac_update(), memcmp(), digest_algorithm::name, okx, and test.

◆ HMAC_TEST() [1/6]

HMAC_TEST ( hmac_empty ,
& sha256_algorithm,
KEY() ,
DATA() ,
EXPECTED(0xb6, 0x13, 0x67, 0x9a, 0x08, 0x14, 0xd9, 0xec, 0x77, 0x2f, 0x95, 0xd7, 0x78, 0xc3, 0x5f, 0xc5, 0xff, 0x16, 0x97, 0xc4, 0x93, 0x71, 0x56, 0x53, 0xc6, 0xc7, 0x12, 0x14, 0x42, 0x92, 0xc5, 0xad)  )

References EXPECTED, and sha256_algorithm.

◆ HMAC_TEST() [2/6]

HMAC_TEST ( hmac_hw ,
& sha256_algorithm,
KEY('H', 'e', 'l', 'l', 'o') ,
DATA('W', 'o', 'r', 'l', 'd') ,
EXPECTED(0x59, 0x16, 0x8e, 0x30, 0x9f, 0x2c, 0x97, 0xdd, 0x04, 0xe4, 0x5b, 0xe3, 0xe7, 0x9b, 0xd9, 0xac, 0xb6, 0xd2, 0x2f, 0xda, 0x65, 0x46, 0xc0, 0x0c, 0x53, 0x92, 0x82, 0xc4, 0x1e, 0xeb, 0x91, 0x6e)  )

References DATA, EXPECTED, KEY, and sha256_algorithm.

◆ HMAC_TEST() [3/6]

HMAC_TEST ( hmac_hw_sha1 ,
& sha1_algorithm,
KEY('H', 'e', 'l', 'l', 'o') ,
DATA('W', 'o', 'r', 'l', 'd') ,
EXPECTED(0x9e, 0x29, 0xcf, 0x6d, 0x48, 0x90, 0x49, 0x9e, 0xf8, 0x5a, 0x31, 0x47, 0x55, 0x7b, 0x1a, 0x45, 0xd5, 0xae, 0xd1, 0x77)  )

References DATA, EXPECTED, KEY, and sha1_algorithm.

◆ HMAC_TEST() [4/6]

HMAC_TEST ( hmac_hw_md5 ,
& md5_algorithm,
KEY('H', 'e', 'l', 'l', 'o') ,
DATA('W', 'o', 'r', 'l', 'd') ,
EXPECTED(0xc7, 0x1c, 0x0c, 0xd0, 0xdc, 0x24, 0x49, 0xbd, 0xd8, 0x9d, 0x28, 0xeb, 0x03, 0xbe, 0xf2, 0x04)  )

References DATA, EXPECTED, KEY, and md5_algorithm.

◆ HMAC_TEST() [5/6]

HMAC_TEST ( hmac_maxlen ,
& sha256_algorithm,
KEY(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f) ,
DATA('H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd') ,
EXPECTED(0xdd, 0x05, 0xcc, 0xe6, 0xd6, 0xaf, 0x91, 0x61, 0x4b, 0xaf, 0x35, 0x6b, 0x86, 0x0a, 0x05, 0x67, 0x25, 0x22, 0xf0, 0x54, 0xd2, 0x5f, 0xd7, 0xe1, 0x54, 0x26, 0x01, 0x16, 0xfd, 0x8a, 0xf3, 0x5b)  )

References DATA, EXPECTED, KEY, and sha256_algorithm.

◆ HMAC_TEST() [6/6]

HMAC_TEST ( hmac_overlen ,
& sha256_algorithm,
KEY(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40) ,
DATA('H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd') ,
EXPECTED(0x4f, 0x0f, 0x42, 0x07, 0xda, 0x84, 0x3d, 0x2a, 0x34, 0xae, 0x5d, 0xd9, 0x05, 0x1e, 0x96, 0xa8, 0xb6, 0xef, 0xa1, 0xcd, 0x49, 0x5b, 0xea, 0x30, 0xbf, 0x47, 0x3b, 0xdc, 0xa9, 0x86, 0xbb, 0x31)  )

References DATA, EXPECTED, KEY, and sha256_algorithm.

◆ hmac_test_exec()

void hmac_test_exec ( void )
static

Perform HMAC self-tests.

Definition at line 193 of file hmac_test.c.

193 {
194
195 hmac_ok ( &hmac_empty );
196 hmac_ok ( &hmac_hw );
197 hmac_ok ( &hmac_hw_sha1 );
198 hmac_ok ( &hmac_hw_md5 );
199 hmac_ok ( &hmac_maxlen );
200 hmac_ok ( &hmac_overlen );
201}
#define hmac_ok(test)
Definition hmac_test.c:126

References hmac_ok.

Variable Documentation

◆ __self_test

struct self_test hmac_test __self_test
Initial value:
= {
.name = "hmac",
.exec = hmac_test_exec,
}
static void hmac_test_exec(void)
Perform HMAC self-tests.
Definition hmac_test.c:193

HMAC self-tests.

Definition at line 204 of file hmac_test.c.

204 {
205 .name = "hmac",
206 .exec = hmac_test_exec,
207};