iPXE
sha1_test.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * SHA-1 tests
00029  *
00030  * NIST test vectors are taken from
00031  *
00032  *  http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA1.pdf
00033  *
00034  */
00035 
00036 /* Forcibly enable assertions */
00037 #undef NDEBUG
00038 
00039 #include <ipxe/sha1.h>
00040 #include <ipxe/test.h>
00041 #include "digest_test.h"
00042 
00043 /* Empty test vector (digest obtained from "sha1sum /dev/null") */
00044 DIGEST_TEST ( sha1_empty, &sha1_algorithm, DIGEST_EMPTY,
00045               DIGEST ( 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32,
00046                        0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8,
00047                        0x07, 0x09 ) );
00048 
00049 /* NIST test vector "abc" */
00050 DIGEST_TEST ( sha1_nist_abc, &sha1_algorithm, DIGEST_NIST_ABC,
00051               DIGEST ( 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba,
00052                        0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0,
00053                        0xd8, 0x9d ) );
00054 
00055 /* NIST test vector "abc...opq" */
00056 DIGEST_TEST ( sha1_nist_abc_opq, &sha1_algorithm, DIGEST_NIST_ABC_OPQ,
00057               DIGEST ( 0x84, 0x98, 0x3e, 0x44, 0x1c, 0x3b, 0xd2, 0x6e, 0xba,
00058                        0xae, 0x4a, 0xa1, 0xf9, 0x51, 0x29, 0xe5, 0xe5, 0x46,
00059                        0x70, 0xf1 ) );
00060 
00061 /**
00062  * Perform SHA-1 self-test
00063  *
00064  */
00065 static void sha1_test_exec ( void ) {
00066 
00067         /* Correctness tests */
00068         digest_ok ( &sha1_empty );
00069         digest_ok ( &sha1_nist_abc );
00070         digest_ok ( &sha1_nist_abc_opq );
00071 
00072         /* Speed tests */
00073         DBG ( "SHA1 required %ld cycles per byte\n",
00074               digest_cost ( &sha1_algorithm ) );
00075 }
00076 
00077 /** SHA-1 self-test */
00078 struct self_test sha1_test __self_test = {
00079         .name = "sha1",
00080         .exec = sha1_test_exec,
00081 };