iPXE
sha256_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-256 family tests
00029  *
00030  * NIST test vectors are taken from
00031  *
00032  *  http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA256.pdf
00033  *  http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA224.pdf
00034  *
00035  */
00036 
00037 /* Forcibly enable assertions */
00038 #undef NDEBUG
00039 
00040 #include <ipxe/sha256.h>
00041 #include <ipxe/test.h>
00042 #include "digest_test.h"
00043 
00044 /* Empty test vector (digest obtained from "sha256sum /dev/null") */
00045 DIGEST_TEST ( sha256_empty, &sha256_algorithm, DIGEST_EMPTY,
00046               DIGEST ( 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a,
00047                        0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae,
00048                        0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99,
00049                        0x1b, 0x78, 0x52, 0xb8, 0x55 ) );
00050 
00051 /* NIST test vector "abc" */
00052 DIGEST_TEST ( sha256_nist_abc, &sha256_algorithm, DIGEST_NIST_ABC,
00053               DIGEST ( 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41,
00054                        0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03,
00055                        0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff,
00056                        0x61, 0xf2, 0x00, 0x15, 0xad ) );
00057 
00058 /* NIST test vector "abc...opq" */
00059 DIGEST_TEST ( sha256_nist_abc_opq, &sha256_algorithm, DIGEST_NIST_ABC_OPQ,
00060               DIGEST ( 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5,
00061                        0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c,
00062                        0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, 0xf6, 0xec, 0xed,
00063                        0xd4, 0x19, 0xdb, 0x06, 0xc1 ) );
00064 
00065 /* Empty test vector (digest obtained from "sha224sum /dev/null") */
00066 DIGEST_TEST ( sha224_empty, &sha224_algorithm, DIGEST_EMPTY,
00067               DIGEST ( 0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 0x47,
00068                        0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4, 0x15, 0xa2,
00069                        0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a, 0xc5, 0xb3, 0xe4,
00070                        0x2f ) );
00071 
00072 /* NIST test vector "abc" */
00073 DIGEST_TEST ( sha224_nist_abc, &sha224_algorithm, DIGEST_NIST_ABC,
00074               DIGEST ( 0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8, 0x22, 0x86,
00075                        0x42, 0xa4, 0x77, 0xbd, 0xa2, 0x55, 0xb3, 0x2a, 0xad,
00076                        0xbc, 0xe4, 0xbd, 0xa0, 0xb3, 0xf7, 0xe3, 0x6c, 0x9d,
00077                        0xa7 ) );
00078 
00079 /* NIST test vector "abc...opq" */
00080 DIGEST_TEST ( sha224_nist_abc_opq, &sha224_algorithm, DIGEST_NIST_ABC_OPQ,
00081               DIGEST ( 0x75, 0x38, 0x8b, 0x16, 0x51, 0x27, 0x76, 0xcc, 0x5d,
00082                        0xba, 0x5d, 0xa1, 0xfd, 0x89, 0x01, 0x50, 0xb0, 0xc6,
00083                        0x45, 0x5c, 0xb4, 0xf5, 0x8b, 0x19, 0x52, 0x52, 0x25,
00084                        0x25 ) );
00085 
00086 /**
00087  * Perform SHA-256 family self-test
00088  *
00089  */
00090 static void sha256_test_exec ( void ) {
00091 
00092         /* Correctness tests */
00093         digest_ok ( &sha256_empty );
00094         digest_ok ( &sha256_nist_abc );
00095         digest_ok ( &sha256_nist_abc_opq );
00096         digest_ok ( &sha224_empty );
00097         digest_ok ( &sha224_nist_abc );
00098         digest_ok ( &sha224_nist_abc_opq );
00099 
00100         /* Speed tests */
00101         DBG ( "SHA256 required %ld cycles per byte\n",
00102               digest_cost ( &sha256_algorithm ) );
00103         DBG ( "SHA224 required %ld cycles per byte\n",
00104               digest_cost ( &sha224_algorithm ) );
00105 }
00106 
00107 /** SHA-256 family self-test */
00108 struct self_test sha256_test __self_test = {
00109         .name = "sha256",
00110         .exec = sha256_test_exec,
00111 };