iPXE
Data Structures | Macros | Functions
digest_test.h File Reference
#include <stdint.h>
#include <ipxe/crypto.h>
#include <ipxe/test.h>

Go to the source code of this file.

Data Structures

struct  digest_test
 A digest test. More...
 

Macros

#define DATA(...)   { __VA_ARGS__ }
 Define inline test data. More...
 
#define DIGEST(...)   { __VA_ARGS__ }
 Define inline expected digest value. More...
 
#define DIGEST_TEST(name, DIGEST, DATA, EXPECTED)
 Define a digest test. More...
 
#define DIGEST_EMPTY   DATA()
 Standard test vector: empty data. More...
 
#define DIGEST_NIST_ABC   DATA ( 0x61, 0x62, 0x63 )
 Standard test vector: NIST string "abc". More...
 
#define DIGEST_NIST_ABC_OPQ
 Standard test vector: NIST string "abc...opq". More...
 
#define DIGEST_NIST_ABC_STU
 Standard test vector: NIST string "abc...stu". More...
 
#define digest_ok(test)   digest_okx ( test, __FILE__, __LINE__ )
 Report a digest test result. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
void digest_okx (struct digest_test *test, const char *file, unsigned int line)
 Report a digest test result. More...
 
unsigned long digest_cost (struct digest_algorithm *digest)
 Calculate digest algorithm cost. More...
 

Macro Definition Documentation

◆ DATA

#define DATA (   ...)    { __VA_ARGS__ }

Define inline test data.

Definition at line 25 of file digest_test.h.

◆ DIGEST

#define DIGEST (   ...)    { __VA_ARGS__ }

Define inline expected digest value.

Definition at line 28 of file digest_test.h.

◆ DIGEST_TEST

#define DIGEST_TEST (   name,
  DIGEST,
  DATA,
  EXPECTED 
)
Value:
static const uint8_t name ## _data[] = DATA; \
static const uint8_t name ## _expected[] = EXPECTED; \
static struct digest_test name = { \
.digest = DIGEST, \
.data = name ## _data, \
.len = sizeof ( name ## _data ), \
.expected = name ## _expected, \
.expected_len = sizeof ( name ## _expected ), \
};
#define EXPECTED(...)
Define inline expected HMAC.
Definition: hmac_test.c:49
const char * name
Definition: ath9k_hw.c:1984
size_t expected_len
Expected digest length.
Definition: digest_test.h:21
A digest test.
Definition: digest_test.h:11
const void * expected
Expected digest value.
Definition: digest_test.h:19
unsigned char uint8_t
Definition: stdint.h:10
#define DATA(...)
Define inline test data.
Definition: digest_test.h:25
#define DIGEST(...)
Define inline expected digest value.
Definition: digest_test.h:28

Define a digest test.

Parameters
nameTest name
DIGESTDigest algorithm
DATATest data
EXPECTEDExpected digest value
Return values
testDigest test

Definition at line 39 of file digest_test.h.

◆ DIGEST_EMPTY

#define DIGEST_EMPTY   DATA()

Standard test vector: empty data.

Definition at line 51 of file digest_test.h.

◆ DIGEST_NIST_ABC

#define DIGEST_NIST_ABC   DATA ( 0x61, 0x62, 0x63 )

Standard test vector: NIST string "abc".

The NIST Cryptographic Toolkit examples for all digest algorithms include a test vector which is the unterminated string

"abc"

Definition at line 60 of file digest_test.h.

◆ DIGEST_NIST_ABC_OPQ

#define DIGEST_NIST_ABC_OPQ
Value:
DATA ( 0x61, 0x62, 0x63, 0x64, 0x62, 0x63, 0x64, 0x65, 0x63, \
0x64, 0x65, 0x66, 0x64, 0x65, 0x66, 0x67, 0x65, 0x66, \
0x67, 0x68, 0x66, 0x67, 0x68, 0x69, 0x67, 0x68, 0x69, \
0x6a, 0x68, 0x69, 0x6a, 0x6b, 0x69, 0x6a, 0x6b, 0x6c, \
0x6a, 0x6b, 0x6c, 0x6d, 0x6b, 0x6c, 0x6d, 0x6e, 0x6c, \
0x6d, 0x6e, 0x6f, 0x6d, 0x6e, 0x6f, 0x70, 0x6e, 0x6f, \
0x70, 0x71 )
#define DATA(...)
Define inline test data.
Definition: digest_test.h:25

Standard test vector: NIST string "abc...opq".

The NIST Cryptographic Toolkit examples for all 32-bit digest algorithms (SHA-1 and the SHA-256 family) include a test vector which is the unterminated string

"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"

Definition at line 71 of file digest_test.h.

◆ DIGEST_NIST_ABC_STU

#define DIGEST_NIST_ABC_STU
Value:
DATA ( 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x62, \
0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x63, 0x64, \
0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x64, 0x65, 0x66, \
0x67, 0x68, 0x69, 0x6a, 0x6b, 0x65, 0x66, 0x67, 0x68, \
0x69, 0x6a, 0x6b, 0x6c, 0x66, 0x67, 0x68, 0x69, 0x6a, \
0x6b, 0x6c, 0x6d, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, \
0x6d, 0x6e, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, \
0x6f, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, \
0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x6b, \
0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x6c, 0x6d, \
0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x6d, 0x6e, 0x6f, \
0x70, 0x71, 0x72, 0x73, 0x74, 0x6e, 0x6f, 0x70, 0x71, \
0x72, 0x73, 0x74, 0x75 )
#define DATA(...)
Define inline test data.
Definition: digest_test.h:25

Standard test vector: NIST string "abc...stu".

The NIST Cryptographic Toolkit examples for all 64-bit digest algorithms (SHA-512 family) include a test vector which is the unterminated string

"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"

Definition at line 89 of file digest_test.h.

◆ digest_ok

#define digest_ok (   test)    digest_okx ( test, __FILE__, __LINE__ )

Report a digest test result.

Parameters
testDigest test

Definition at line 109 of file digest_test.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ digest_okx()

void digest_okx ( struct digest_test test,
const char *  file,
unsigned int  line 
)

Report a digest test result.

Parameters
testDigest test
fileTest code file
lineTest code line

Definition at line 111 of file digest_test.c.

112  {
113  unsigned int i;
114 
115  /* Test with a single pass */
116  digest_frag_okx ( test, NULL, file, line );
117 
118  /* Test with fragment lists */
119  for ( i = 0 ; i < ( sizeof ( digest_test_fragments ) /
120  sizeof ( digest_test_fragments[0] ) ) ; i++ ) {
121  digest_frag_okx ( test, &digest_test_fragments[i], file, line );
122  }
123 }
static struct digest_test_fragments digest_test_fragments[]
Digest test fragment lists.
Definition: digest_test.c:51
A digest test fragment list.
Definition: digest_test.c:45
void digest_frag_okx(struct digest_test *test, struct digest_test_fragments *fragments, const char *file, unsigned int line)
Report a digest fragmented test result.
Definition: digest_test.c:68
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
static int test
Definition: epic100.c:73

References digest_frag_okx(), digest_test_fragments, NULL, and test.

◆ digest_cost()

unsigned long digest_cost ( struct digest_algorithm digest)

Calculate digest algorithm cost.

Parameters
digestDigest algorithm
Return values
costCost (in cycles per byte)

Definition at line 131 of file digest_test.c.

131  {
132  static uint8_t random[8192]; /* Too large for stack */
135  struct profiler profiler;
136  unsigned long cost;
137  unsigned int i;
138 
139  /* Fill buffer with pseudo-random data */
140  srand ( 0x1234568 );
141  for ( i = 0 ; i < sizeof ( random ) ; i++ )
142  random[i] = rand();
143 
144  /* Profile digest calculation */
145  memset ( &profiler, 0, sizeof ( profiler ) );
146  for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
147  profile_start ( &profiler );
148  digest_init ( digest, ctx );
149  digest_update ( digest, ctx, random, sizeof ( random ) );
150  digest_final ( digest, ctx, out );
151  profile_stop ( &profiler );
152  }
153 
154  /* Round to nearest whole number of cycles per byte */
155  cost = ( ( profile_mean ( &profiler ) + ( sizeof ( random ) / 2 ) ) /
156  sizeof ( random ) );
157 
158  return cost;
159 }
unsigned long profile_mean(struct profiler *profiler)
Get mean sample value.
Definition: profile.c:241
A data structure for storing profiling information.
Definition: profile.h:26
static void profile_stop(struct profiler *profiler)
Stop profiling.
Definition: profile.h:171
__be32 out[4]
Definition: CIB_PRM.h:36
static int rand(void)
Definition: stdlib.h:59
static void srand(unsigned int seed)
Definition: stdlib.h:63
static void struct digest_algorithm * digest
HMAC-MD5 digest.
Definition: crypto.h:308
static void profile_start(struct profiler *profiler)
Start profiling.
Definition: profile.h:158
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
long int random(void)
Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
Definition: random.c:31
unsigned char uint8_t
Definition: stdint.h:10
uint32_t cost
Root path cost.
Definition: stp.h:28
#define PROFILE_COUNT
Number of sample iterations for profiling.
Definition: digest_test.c:58
size_t ctxsize
Context size.
Definition: crypto.h:21
size_t digestsize
Digest size.
Definition: crypto.h:25
void * memset(void *dest, int character, size_t len) __nonnull

References cost, ctx, digest_algorithm::ctxsize, digest, digest_algorithm::digestsize, memset(), out, PROFILE_COUNT, profile_mean(), profile_start(), profile_stop(), rand(), random(), and srand().

Referenced by md4_test_exec(), md5_test_exec(), sha1_test_exec(), sha256_test_exec(), and sha512_test_exec().