iPXE
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(...)
 Define inline test data.
#define DIGEST(...)
 Define inline expected digest value.
#define DIGEST_TEST(name, DIGEST, DATA, EXPECTED)
 Define a digest test.
#define DIGEST_EMPTY   DATA()
 Standard test vector: empty data.
#define DIGEST_NIST_ABC    DATA ( 0x61, 0x62, 0x63 )
 Standard test vector: NIST string "abc".
#define DIGEST_NIST_ABC_OPQ
 Standard test vector: NIST string "abc...opq".
#define DIGEST_NIST_ABC_STU
 Standard test vector: NIST string "abc...stu".
#define digest_ok(test)
 Report a digest test result.

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.
unsigned long digest_cost (struct digest_algorithm *digest)
 Calculate digest algorithm cost.

Macro Definition Documentation

◆ DATA

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

Define inline test data.

Definition at line 25 of file digest_test.h.

◆ DIGEST

#define DIGEST ( ...)
Value:
{ __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 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 DIGEST(...)
Define inline expected digest.
Definition der_test.c:45
#define EXPECTED(...)
Define inline expected result point.
A digest test.
Definition digest_test.h:11

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.

39#define DIGEST_TEST( name, DIGEST, DATA, EXPECTED ) \
40 static const uint8_t name ## _data[] = DATA; \
41 static const uint8_t name ## _expected[] = EXPECTED; \
42 static struct digest_test name = { \
43 .digest = DIGEST, \
44 .data = name ## _data, \
45 .len = sizeof ( name ## _data ), \
46 .expected = name ## _expected, \
47 .expected_len = sizeof ( name ## _expected ), \
48 };

◆ DIGEST_EMPTY

#define DIGEST_EMPTY   DATA()

Standard test vector: empty data.

Definition at line 51 of file digest_test.h.

Referenced by DIGEST_TEST(), DIGEST_TEST(), DIGEST_TEST(), DIGEST_TEST(), DIGEST_TEST(), DIGEST_TEST(), DIGEST_TEST(), DIGEST_TEST(), and DIGEST_TEST().

◆ 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.

60#define DIGEST_NIST_ABC \
61 DATA ( 0x61, 0x62, 0x63 )

Referenced by DIGEST_TEST(), DIGEST_TEST(), DIGEST_TEST(), DIGEST_TEST(), DIGEST_TEST(), DIGEST_TEST(), DIGEST_TEST(), DIGEST_TEST(), and DIGEST_TEST().

◆ 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 )

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.

71#define DIGEST_NIST_ABC_OPQ \
72 DATA ( 0x61, 0x62, 0x63, 0x64, 0x62, 0x63, 0x64, 0x65, 0x63, \
73 0x64, 0x65, 0x66, 0x64, 0x65, 0x66, 0x67, 0x65, 0x66, \
74 0x67, 0x68, 0x66, 0x67, 0x68, 0x69, 0x67, 0x68, 0x69, \
75 0x6a, 0x68, 0x69, 0x6a, 0x6b, 0x69, 0x6a, 0x6b, 0x6c, \
76 0x6a, 0x6b, 0x6c, 0x6d, 0x6b, 0x6c, 0x6d, 0x6e, 0x6c, \
77 0x6d, 0x6e, 0x6f, 0x6d, 0x6e, 0x6f, 0x70, 0x6e, 0x6f, \
78 0x70, 0x71 )

Referenced by DIGEST_TEST(), DIGEST_TEST(), DIGEST_TEST(), DIGEST_TEST(), and DIGEST_TEST().

◆ 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 )

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.

89#define DIGEST_NIST_ABC_STU \
90 DATA ( 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x62, \
91 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x63, 0x64, \
92 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x64, 0x65, 0x66, \
93 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x65, 0x66, 0x67, 0x68, \
94 0x69, 0x6a, 0x6b, 0x6c, 0x66, 0x67, 0x68, 0x69, 0x6a, \
95 0x6b, 0x6c, 0x6d, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, \
96 0x6d, 0x6e, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, \
97 0x6f, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, \
98 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x6b, \
99 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x6c, 0x6d, \
100 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x6d, 0x6e, 0x6f, \
101 0x70, 0x71, 0x72, 0x73, 0x74, 0x6e, 0x6f, 0x70, 0x71, \
102 0x72, 0x73, 0x74, 0x75 )

Referenced by DIGEST_TEST(), DIGEST_TEST(), DIGEST_TEST(), and DIGEST_TEST().

◆ digest_ok

#define digest_ok ( test)
Value:
digest_okx ( test, __FILE__, __LINE__ )
void digest_okx(struct digest_test *test, const char *file, unsigned int line)
Report a digest test result.
static int test
Definition epic100.c:73

Report a digest test result.

Parameters
testDigest test

Definition at line 109 of file digest_test.h.

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

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 )
extern

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}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
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
A digest test fragment list.
Definition digest_test.c:45

References digest_frag_okx(), NULL, and test.

◆ digest_cost()

unsigned long digest_cost ( struct digest_algorithm * digest)
extern

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 */
133 uint8_t ctx[digest->ctxsize];
134 uint8_t out[digest->digestsize];
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++ ) {
148 digest_init ( digest, ctx );
149 digest_update ( digest, ctx, random, sizeof ( random ) );
150 digest_final ( digest, ctx, out );
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}
struct golan_eq_context ctx
Definition CIB_PRM.h:0
__be32 out[4]
Definition CIB_PRM.h:8
#define PROFILE_COUNT
Number of sample iterations for profiling.
Definition cipher_test.c:45
static void digest_init(struct digest_algorithm *digest, void *ctx)
Definition crypto.h:219
static void digest_final(struct digest_algorithm *digest, void *ctx, void *out)
Definition crypto.h:230
static void digest_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Definition crypto.h:224
static void profile_stop(struct profiler *profiler)
Stop profiling.
Definition profile.h:174
static void profile_start(struct profiler *profiler)
Start profiling.
Definition profile.h:161
void * memset(void *dest, int character, size_t len) __nonnull
unsigned long profile_mean(struct profiler *profiler)
Get mean sample value.
Definition profile.c:242
long int random(void)
Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
Definition random.c:32
static void srand(unsigned int seed)
Definition stdlib.h:64
static int rand(void)
Definition stdlib.h:60
uint32_t cost
Root path cost.
Definition stp.h:17
size_t digestsize
Digest size.
Definition crypto.h:27
size_t ctxsize
Context size.
Definition crypto.h:23
A data structure for storing profiling information.
Definition profile.h:27

References cost, ctx, digest_algorithm::ctxsize, digest_final(), digest_init(), digest_update(), 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().