2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
32 /* Forcibly enable assertions */
37 #include <ipxe/crypto.h>
38 #include <ipxe/profile.h>
39 #include "digest_test.h"
41 /** Maximum number of digest test fragments */
42 #define NUM_DIGEST_TEST_FRAG 8
44 /** A digest test fragment list */
45 struct digest_test_fragments {
46 /** Fragment lengths */
47 size_t len[NUM_DIGEST_TEST_FRAG];
50 /** Digest test fragment lists */
51 static struct digest_test_fragments digest_test_fragments[] = {
53 { { 1, 1, 1, 1, 1, 1, 1, 1 } },
54 { { 2, 0, 23, 4, 6, 1, 0 } },
57 /** Number of sample iterations for profiling */
58 #define PROFILE_COUNT 16
61 * Report a digest fragmented test result
64 * @v fragments Fragment list
65 * @v file Test code file
66 * @v line Test code line
68 void digest_frag_okx ( struct digest_test *test,
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;
80 okx ( test->expected_len == sizeof ( out ), file, line );
82 /* Initialise digest */
83 digest_init ( digest, ctx );
85 /* Update digest fragment-by-fragment */
86 for ( i = 0 ; len && ( i < ( sizeof ( fragments->len ) /
87 sizeof ( fragments->len[0] ) ) ) ; i++ ) {
89 frag_len = fragments->len[i];
90 if ( ( frag_len == 0 ) || ( frag_len < len ) )
92 digest_update ( digest, ctx, data, frag_len );
98 digest_final ( digest, ctx, out );
100 /* Compare against expected output */
101 okx ( memcmp ( test->expected, out, sizeof ( out ) ) == 0, file, line );
105 * Report a digest test result
107 * @v test Digest test
108 * @v file Test code file
109 * @v line Test code line
111 void digest_okx ( struct digest_test *test, const char *file,
112 unsigned int line ) {
115 /* Test with a single pass */
116 digest_frag_okx ( test, NULL, file, line );
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 );
126 * Calculate digest algorithm cost
128 * @v digest Digest algorithm
129 * @ret cost Cost (in cycles per byte)
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;
139 /* Fill buffer with pseudo-random data */
141 for ( i = 0 ; i < sizeof ( random ) ; i++ )
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 );
154 /* Round to nearest whole number of cycles per byte */
155 cost = ( ( profile_mean ( &profiler ) + ( sizeof ( random ) / 2 ) ) /