iPXE
digest_test.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /** @file
27  *
28  * Digest self-tests
29  *
30  */
31 
32 /* Forcibly enable assertions */
33 #undef NDEBUG
34 
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ipxe/crypto.h>
38 #include <ipxe/profile.h>
39 #include "digest_test.h"
40 
41 /** Maximum number of digest test fragments */
42 #define NUM_DIGEST_TEST_FRAG 8
43 
44 /** A digest test fragment list */
46  /** Fragment lengths */
48 };
49 
50 /** Digest test fragment lists */
52  { { 0, -1UL, } },
53  { { 1, 1, 1, 1, 1, 1, 1, 1 } },
54  { { 2, 0, 23, 4, 6, 1, 0 } },
55 };
56 
57 /** Number of sample iterations for profiling */
58 #define PROFILE_COUNT 16
59 
60 /**
61  * Report a digest fragmented test result
62  *
63  * @v test Digest test
64  * @v fragments Fragment list
65  * @v file Test code file
66  * @v line Test code line
67  */
69  struct digest_test_fragments *fragments,
70  const char *file, unsigned int line ) {
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 }
103 
104 /**
105  * Report a digest test result
106  *
107  * @v test Digest test
108  * @v file Test code file
109  * @v line Test code line
110  */
111 void digest_okx ( struct digest_test *test, const char *file,
112  unsigned int line ) {
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 }
124 
125 /**
126  * Calculate digest algorithm cost
127  *
128  * @v digest Digest algorithm
129  * @ret cost Cost (in cycles per byte)
130  */
131 unsigned long digest_cost ( struct digest_algorithm *digest ) {
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 struct digest_test_fragments digest_test_fragments[]
Digest test fragment lists.
Definition: digest_test.c:51
void digest_okx(struct digest_test *test, const char *file, unsigned int line)
Report a digest test result.
Definition: digest_test.c:111
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
A digest test.
Definition: digest_test.h:11
static void digest_final(struct digest_algorithm *digest, void *ctx, void *out)
Definition: crypto.h:182
A digest test fragment list.
Definition: digest_test.c:45
Cryptographic API.
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
size_t len[NUM_DIGEST_TEST_FRAG]
Fragment lengths.
Definition: digest_test.c:47
unsigned long digest_cost(struct digest_algorithm *digest)
Calculate digest algorithm cost.
Definition: digest_test.c:131
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 okx(success, file, line)
Report test result.
Definition: test.h:44
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
Profiling.
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
#define NUM_DIGEST_TEST_FRAG
Maximum number of digest test fragments.
Definition: digest_test.c:42
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
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
String functions.
static int test
Definition: epic100.c:73
void * memset(void *dest, int character, size_t len) __nonnull