iPXE
sha1_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  * SHA-1 tests
29  *
30  * NIST test vectors are taken from
31  *
32  * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA1.pdf
33  *
34  */
35 
36 /* Forcibly enable assertions */
37 #undef NDEBUG
38 
39 #include <ipxe/sha1.h>
40 #include <ipxe/test.h>
41 #include "digest_test.h"
42 
43 /* Empty test vector (digest obtained from "sha1sum /dev/null") */
45  DIGEST ( 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32,
46  0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8,
47  0x07, 0x09 ) );
48 
49 /* NIST test vector "abc" */
50 DIGEST_TEST ( sha1_nist_abc, &sha1_algorithm, DIGEST_NIST_ABC,
51  DIGEST ( 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba,
52  0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0,
53  0xd8, 0x9d ) );
54 
55 /* NIST test vector "abc...opq" */
56 DIGEST_TEST ( sha1_nist_abc_opq, &sha1_algorithm, DIGEST_NIST_ABC_OPQ,
57  DIGEST ( 0x84, 0x98, 0x3e, 0x44, 0x1c, 0x3b, 0xd2, 0x6e, 0xba,
58  0xae, 0x4a, 0xa1, 0xf9, 0x51, 0x29, 0xe5, 0xe5, 0x46,
59  0x70, 0xf1 ) );
60 
61 /**
62  * Perform SHA-1 self-test
63  *
64  */
65 static void sha1_test_exec ( void ) {
66 
67  /* Correctness tests */
68  digest_ok ( &sha1_empty );
69  digest_ok ( &sha1_nist_abc );
70  digest_ok ( &sha1_nist_abc_opq );
71 
72  /* Speed tests */
73  DBG ( "SHA1 required %ld cycles per byte\n",
75 }
76 
77 /** SHA-1 self-test */
78 struct self_test sha1_test __self_test = {
79  .name = "sha1",
80  .exec = sha1_test_exec,
81 };
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define DIGEST_NIST_ABC
Standard test vector: NIST string "abc".
Definition: digest_test.h:60
Self-test infrastructure.
const char * name
Test set name.
Definition: test.h:17
A self-test set.
Definition: test.h:15
#define DIGEST_NIST_ABC_OPQ
Standard test vector: NIST string "abc...opq".
Definition: digest_test.h:71
unsigned long digest_cost(struct digest_algorithm *digest)
Calculate digest algorithm cost.
Definition: digest_test.c:131
DIGEST_TEST(sha1_empty, &sha1_algorithm, DIGEST_EMPTY, DIGEST(0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09))
#define digest_ok(test)
Report a digest test result.
Definition: digest_test.h:109
#define DIGEST_EMPTY
Standard test vector: empty data.
Definition: digest_test.h:51
struct self_test sha1_test __self_test
SHA-1 self-test.
Definition: sha1_test.c:78
static void sha1_test_exec(void)
Perform SHA-1 self-test.
Definition: sha1_test.c:65
SHA-1 algorithm.
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define DIGEST(...)
Define inline expected digest.
Definition: der_test.c:45
struct digest_algorithm sha1_algorithm
SHA-1 algorithm.
Definition: sha1.c:258