iPXE
Data Structures | Macros | Functions | Variables
digest_test.c File Reference

Digest self-tests. More...

#include <stdlib.h>
#include <string.h>
#include <ipxe/crypto.h>
#include <ipxe/profile.h>
#include "digest_test.h"

Go to the source code of this file.

Data Structures

struct  digest_test_fragments
 A digest test fragment list. More...
 

Macros

#define NUM_DIGEST_TEST_FRAG   8
 Maximum number of digest test fragments. More...
 
#define PROFILE_COUNT   16
 Number of sample iterations for profiling. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
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. More...
 
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...
 

Variables

static struct digest_test_fragments digest_test_fragments []
 Digest test fragment lists. More...
 

Detailed Description

Digest self-tests.

Definition in file digest_test.c.

Macro Definition Documentation

◆ NUM_DIGEST_TEST_FRAG

#define NUM_DIGEST_TEST_FRAG   8

Maximum number of digest test fragments.

Definition at line 42 of file digest_test.c.

◆ PROFILE_COUNT

#define PROFILE_COUNT   16

Number of sample iterations for profiling.

Definition at line 58 of file digest_test.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ digest_frag_okx()

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.

Parameters
testDigest test
fragmentsFragment list
fileTest code file
lineTest code line

Definition at line 68 of file digest_test.c.

70  {
71  struct digest_algorithm *digest = test->digest;
72  uint8_t ctx[digest->ctxsize];
73  uint8_t out[digest->digestsize];
74  const void *data = test->data;
75  size_t len = test->len;
76  size_t frag_len = 0;
77  unsigned int i;
78 
79  /* Sanity check */
80  okx ( test->expected_len == sizeof ( out ), file, line );
81 
82  /* Initialise digest */
83  digest_init ( digest, ctx );
84 
85  /* Update digest fragment-by-fragment */
86  for ( i = 0 ; len && ( i < ( sizeof ( fragments->len ) /
87  sizeof ( fragments->len[0] ) ) ) ; i++ ) {
88  if ( fragments )
89  frag_len = fragments->len[i];
90  if ( ( frag_len == 0 ) || ( frag_len < len ) )
91  frag_len = len;
92  digest_update ( digest, ctx, data, frag_len );
93  data += frag_len;
94  len -= frag_len;
95  }
96 
97  /* Finalise digest */
98  digest_final ( digest, ctx, out );
99 
100  /* Compare against expected output */
101  okx ( memcmp ( test->expected, out, sizeof ( out ) ) == 0, file, line );
102 }
static void digest_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Definition: crypto.h:177
static void digest_final(struct digest_algorithm *digest, void *ctx, void *out)
Definition: crypto.h:182
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
struct md4_digest digest
Digest of data already processed.
Definition: md4.h:12
size_t len[NUM_DIGEST_TEST_FRAG]
Fragment lengths.
Definition: digest_test.c:47
#define okx(success, file, line)
Report test result.
Definition: test.h:44
__be32 out[4]
Definition: CIB_PRM.h:36
static void digest_init(struct digest_algorithm *digest, void *ctx)
Definition: crypto.h:172
unsigned char uint8_t
Definition: stdint.h:10
uint32_t len
Length.
Definition: ena.h:14
A message digest algorithm.
Definition: crypto.h:16
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:98
static int test
Definition: epic100.c:73

References ctx, data, digest, digest_final(), digest_init(), digest_update(), len, digest_test_fragments::len, memcmp(), okx, out, and test.

Referenced by digest_okx().

◆ 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:362
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 */
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++ ) {
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
static void digest_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Definition: crypto.h:177
static void digest_final(struct digest_algorithm *digest, void *ctx, void *out)
Definition: crypto.h:182
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
A data structure for storing profiling information.
Definition: profile.h:26
static void profile_stop(struct profiler *profiler)
Stop profiling.
Definition: profile.h:171
struct md4_digest digest
Digest of data already processed.
Definition: md4.h:12
static int rand(void)
Definition: stdlib.h:59
static void srand(unsigned int seed)
Definition: stdlib.h:63
__be32 out[4]
Definition: CIB_PRM.h:36
static void profile_start(struct profiler *profiler)
Start profiling.
Definition: profile.h:158
static void digest_init(struct digest_algorithm *digest, void *ctx)
Definition: crypto.h:172
long int random(void)
Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
Definition: random.c:30
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
void * memset(void *dest, int character, size_t len) __nonnull

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

Variable Documentation

◆ digest_test_fragments

Initial value:
= {
{ { 0, -1UL, } },
{ { 1, 1, 1, 1, 1, 1, 1, 1 } },
{ { 2, 0, 23, 4, 6, 1, 0 } },
}

Digest test fragment lists.

Definition at line 51 of file digest_test.c.

Referenced by digest_okx().