These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / tests / digest_test.c
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 */
45 struct digest_test_fragments {
46         /** Fragment lengths */
47         size_t len[NUM_DIGEST_TEST_FRAG];
48 };
49
50 /** Digest test fragment lists */
51 static struct digest_test_fragments digest_test_fragments[] = {
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  */
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;
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 }