iPXE
digest_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  * Digest self-tests
00029  *
00030  */
00031 
00032 /* Forcibly enable assertions */
00033 #undef NDEBUG
00034 
00035 #include <stdlib.h>
00036 #include <string.h>
00037 #include <ipxe/crypto.h>
00038 #include <ipxe/profile.h>
00039 #include "digest_test.h"
00040 
00041 /** Maximum number of digest test fragments */
00042 #define NUM_DIGEST_TEST_FRAG 8
00043 
00044 /** A digest test fragment list */
00045 struct digest_test_fragments {
00046         /** Fragment lengths */
00047         size_t len[NUM_DIGEST_TEST_FRAG];
00048 };
00049 
00050 /** Digest test fragment lists */
00051 static struct digest_test_fragments digest_test_fragments[] = {
00052         { { 0, -1UL, } },
00053         { { 1, 1, 1, 1, 1, 1, 1, 1 } },
00054         { { 2, 0, 23, 4, 6, 1, 0 } },
00055 };
00056 
00057 /** Number of sample iterations for profiling */
00058 #define PROFILE_COUNT 16
00059 
00060 /**
00061  * Report a digest fragmented test result
00062  *
00063  * @v test              Digest test
00064  * @v fragments         Fragment list
00065  * @v file              Test code file
00066  * @v line              Test code line
00067  */
00068 void digest_frag_okx ( struct digest_test *test,
00069                        struct digest_test_fragments *fragments,
00070                        const char *file, unsigned int line ) {
00071         struct digest_algorithm *digest = test->digest;
00072         uint8_t ctx[digest->ctxsize];
00073         uint8_t out[digest->digestsize];
00074         const void *data = test->data;
00075         size_t len = test->len;
00076         size_t frag_len = 0;
00077         unsigned int i;
00078 
00079         /* Sanity check */
00080         okx ( test->expected_len == sizeof ( out ), file, line );
00081 
00082         /* Initialise digest */
00083         digest_init ( digest, ctx );
00084 
00085         /* Update digest fragment-by-fragment */
00086         for ( i = 0 ; len && ( i < ( sizeof ( fragments->len ) /
00087                                      sizeof ( fragments->len[0] ) ) ) ; i++ ) {
00088                 if ( fragments )
00089                         frag_len = fragments->len[i];
00090                 if ( ( frag_len == 0 ) || ( frag_len < len ) )
00091                         frag_len = len;
00092                 digest_update ( digest, ctx, data, frag_len );
00093                 data += frag_len;
00094                 len -= frag_len;
00095         }
00096 
00097         /* Finalise digest */
00098         digest_final ( digest, ctx, out );
00099 
00100         /* Compare against expected output */
00101         okx ( memcmp ( test->expected, out, sizeof ( out ) ) == 0, file, line );
00102 }
00103 
00104 /**
00105  * Report a digest test result
00106  *
00107  * @v test              Digest test
00108  * @v file              Test code file
00109  * @v line              Test code line
00110  */
00111 void digest_okx ( struct digest_test *test, const char *file,
00112                   unsigned int line ) {
00113         unsigned int i;
00114 
00115         /* Test with a single pass */
00116         digest_frag_okx ( test, NULL, file, line );
00117 
00118         /* Test with fragment lists */
00119         for ( i = 0 ; i < ( sizeof ( digest_test_fragments ) /
00120                             sizeof ( digest_test_fragments[0] ) ) ; i++ ) {
00121                 digest_frag_okx ( test, &digest_test_fragments[i], file, line );
00122         }
00123 }
00124 
00125 /**
00126  * Calculate digest algorithm cost
00127  *
00128  * @v digest            Digest algorithm
00129  * @ret cost            Cost (in cycles per byte)
00130  */
00131 unsigned long digest_cost ( struct digest_algorithm *digest ) {
00132         static uint8_t random[8192]; /* Too large for stack */
00133         uint8_t ctx[digest->ctxsize];
00134         uint8_t out[digest->digestsize];
00135         struct profiler profiler;
00136         unsigned long cost;
00137         unsigned int i;
00138 
00139         /* Fill buffer with pseudo-random data */
00140         srand ( 0x1234568 );
00141         for ( i = 0 ; i < sizeof ( random ) ; i++ )
00142                 random[i] = rand();
00143 
00144         /* Profile digest calculation */
00145         memset ( &profiler, 0, sizeof ( profiler ) );
00146         for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
00147                 profile_start ( &profiler );
00148                 digest_init ( digest, ctx );
00149                 digest_update ( digest, ctx, random, sizeof ( random ) );
00150                 digest_final ( digest, ctx, out );
00151                 profile_stop ( &profiler );
00152         }
00153 
00154         /* Round to nearest whole number of cycles per byte */
00155         cost = ( ( profile_mean ( &profiler ) + ( sizeof ( random ) / 2 ) ) /
00156                  sizeof ( random ) );
00157 
00158         return cost;
00159 }