mirror of https://github.com/ipxe/ipxe.git
[test] Simplify digest algorithm self-tests
Update the digest self-tests to use okx(), and centralise concepts and data shared between tests for multiple algorithms to reduce duplicated code. Signed-off-by: Michael Brown <mcb30@ipxe.org>pull/35/merge
parent
b12b1b620f
commit
a9da129122
|
@ -38,27 +38,47 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
|||
#include <ipxe/profile.h>
|
||||
#include "digest_test.h"
|
||||
|
||||
/** Maximum number of digest test fragments */
|
||||
#define NUM_DIGEST_TEST_FRAG 8
|
||||
|
||||
/** A digest test fragment list */
|
||||
struct digest_test_fragments {
|
||||
/** Fragment lengths */
|
||||
size_t len[NUM_DIGEST_TEST_FRAG];
|
||||
};
|
||||
|
||||
/** Digest test fragment lists */
|
||||
static struct digest_test_fragments digest_test_fragments[] = {
|
||||
{ { 0, -1UL, } },
|
||||
{ { 1, 1, 1, 1, 1, 1, 1, 1 } },
|
||||
{ { 2, 0, 23, 4, 6, 1, 0 } },
|
||||
};
|
||||
|
||||
/** Number of sample iterations for profiling */
|
||||
#define PROFILE_COUNT 16
|
||||
|
||||
/**
|
||||
* Test digest algorithm
|
||||
* Report a digest fragmented test result
|
||||
*
|
||||
* @v digest Digest algorithm
|
||||
* @v fragments Digest test fragment list, or NULL
|
||||
* @v data Test data
|
||||
* @v len Length of test data
|
||||
* @v expected Expected digest value
|
||||
* @ret ok Digest value is as expected
|
||||
* @v test Digest test
|
||||
* @v fragments Fragment list
|
||||
* @v file Test code file
|
||||
* @v line Test code line
|
||||
*/
|
||||
int digest_test ( struct digest_algorithm *digest,
|
||||
struct digest_test_fragments *fragments,
|
||||
void *data, size_t len, void *expected ) {
|
||||
void digest_frag_okx ( struct digest_test *test,
|
||||
struct digest_test_fragments *fragments,
|
||||
const char *file, unsigned int line ) {
|
||||
struct digest_algorithm *digest = test->digest;
|
||||
uint8_t ctx[digest->ctxsize];
|
||||
uint8_t out[digest->digestsize];
|
||||
const void *data = test->data;
|
||||
size_t len = test->len;
|
||||
size_t frag_len = 0;
|
||||
unsigned int i;
|
||||
|
||||
/* Sanity check */
|
||||
okx ( test->expected_len == sizeof ( out ), file, line );
|
||||
|
||||
/* Initialise digest */
|
||||
digest_init ( digest, ctx );
|
||||
|
||||
|
@ -78,7 +98,28 @@ int digest_test ( struct digest_algorithm *digest,
|
|||
digest_final ( digest, ctx, out );
|
||||
|
||||
/* Compare against expected output */
|
||||
return ( memcmp ( expected, out, sizeof ( out ) ) == 0 );
|
||||
okx ( memcmp ( test->expected, out, sizeof ( out ) ) == 0, file, line );
|
||||
}
|
||||
|
||||
/**
|
||||
* Report a digest test result
|
||||
*
|
||||
* @v test Digest test
|
||||
* @v file Test code file
|
||||
* @v line Test code line
|
||||
*/
|
||||
void digest_okx ( struct digest_test *test, const char *file,
|
||||
unsigned int line ) {
|
||||
unsigned int i;
|
||||
|
||||
/* Test with a single pass */
|
||||
digest_frag_okx ( test, NULL, file, line );
|
||||
|
||||
/* Test with fragment lists */
|
||||
for ( i = 0 ; i < ( sizeof ( digest_test_fragments ) /
|
||||
sizeof ( digest_test_fragments[0] ) ) ; i++ ) {
|
||||
digest_frag_okx ( test, &digest_test_fragments[i], file, line );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -7,31 +7,109 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
|||
#include <ipxe/crypto.h>
|
||||
#include <ipxe/test.h>
|
||||
|
||||
/** Maximum number of digest test fragments */
|
||||
#define NUM_DIGEST_TEST_FRAG 8
|
||||
|
||||
/** A digest test fragment list */
|
||||
struct digest_test_fragments {
|
||||
/** Fragment lengths */
|
||||
size_t len[NUM_DIGEST_TEST_FRAG];
|
||||
/** A digest test */
|
||||
struct digest_test {
|
||||
/** Digest algorithm */
|
||||
struct digest_algorithm *digest;
|
||||
/** Test data */
|
||||
const void *data;
|
||||
/** Length of test data */
|
||||
size_t len;
|
||||
/** Expected digest value */
|
||||
const void *expected;
|
||||
/** Expected digest length */
|
||||
size_t expected_len;
|
||||
};
|
||||
|
||||
extern int digest_test ( struct digest_algorithm *digest,
|
||||
struct digest_test_fragments *fragments,
|
||||
void *data, size_t len, void *expected );
|
||||
extern unsigned long digest_cost ( struct digest_algorithm *digest );
|
||||
/** Define inline test data */
|
||||
#define DATA(...) { __VA_ARGS__ }
|
||||
|
||||
/** Define inline expected digest value */
|
||||
#define DIGEST(...) { __VA_ARGS__ }
|
||||
|
||||
/**
|
||||
* Report digest test result
|
||||
* Define a digest test
|
||||
*
|
||||
* @v digest Digest algorithm
|
||||
* @v fragments Digest test fragment list, or NULL
|
||||
* @v data Test data
|
||||
* @v len Length of test data
|
||||
* @v expected Expected digest value
|
||||
* @v name Test name
|
||||
* @v DIGEST Digest algorithm
|
||||
* @v DATA Test data
|
||||
* @v EXPECTED Expected digest value
|
||||
* @ret test Digest test
|
||||
*/
|
||||
#define digest_ok( digest, fragments, data, len, expected ) do { \
|
||||
ok ( digest_test ( digest, fragments, data, len, expected ) ); \
|
||||
} while ( 0 )
|
||||
#define DIGEST_TEST( name, DIGEST, DATA, EXPECTED ) \
|
||||
static const uint8_t name ## _data[] = DATA; \
|
||||
static const uint8_t name ## _expected[] = EXPECTED; \
|
||||
static struct digest_test name = { \
|
||||
.digest = DIGEST, \
|
||||
.data = name ## _data, \
|
||||
.len = sizeof ( name ## _data ), \
|
||||
.expected = name ## _expected, \
|
||||
.expected_len = sizeof ( name ## _expected ), \
|
||||
};
|
||||
|
||||
/** Standard test vector: empty data */
|
||||
#define DIGEST_EMPTY DATA()
|
||||
|
||||
/** Standard test vector: NIST string "abc"
|
||||
*
|
||||
* The NIST Cryptographic Toolkit examples for all digest algorithms
|
||||
* include a test vector which is the unterminated string
|
||||
*
|
||||
* "abc"
|
||||
*/
|
||||
#define DIGEST_NIST_ABC \
|
||||
DATA ( 0x61, 0x62, 0x63 )
|
||||
|
||||
/** Standard test vector: NIST string "abc...opq"
|
||||
*
|
||||
* The NIST Cryptographic Toolkit examples for all 32-bit digest
|
||||
* algorithms (SHA-1 and the SHA-256 family) include a test vector
|
||||
* which is the unterminated string
|
||||
*
|
||||
* "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
|
||||
*/
|
||||
#define DIGEST_NIST_ABC_OPQ \
|
||||
DATA ( 0x61, 0x62, 0x63, 0x64, 0x62, 0x63, 0x64, 0x65, 0x63, \
|
||||
0x64, 0x65, 0x66, 0x64, 0x65, 0x66, 0x67, 0x65, 0x66, \
|
||||
0x67, 0x68, 0x66, 0x67, 0x68, 0x69, 0x67, 0x68, 0x69, \
|
||||
0x6a, 0x68, 0x69, 0x6a, 0x6b, 0x69, 0x6a, 0x6b, 0x6c, \
|
||||
0x6a, 0x6b, 0x6c, 0x6d, 0x6b, 0x6c, 0x6d, 0x6e, 0x6c, \
|
||||
0x6d, 0x6e, 0x6f, 0x6d, 0x6e, 0x6f, 0x70, 0x6e, 0x6f, \
|
||||
0x70, 0x71 )
|
||||
|
||||
/** Standard test vector: NIST string "abc...stu"
|
||||
*
|
||||
* The NIST Cryptographic Toolkit examples for all 64-bit digest
|
||||
* algorithms (SHA-512 family) include a test vector which is the
|
||||
* unterminated string
|
||||
*
|
||||
* "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
|
||||
* "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
|
||||
*/
|
||||
#define DIGEST_NIST_ABC_STU \
|
||||
DATA ( 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x62, \
|
||||
0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x63, 0x64, \
|
||||
0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x64, 0x65, 0x66, \
|
||||
0x67, 0x68, 0x69, 0x6a, 0x6b, 0x65, 0x66, 0x67, 0x68, \
|
||||
0x69, 0x6a, 0x6b, 0x6c, 0x66, 0x67, 0x68, 0x69, 0x6a, \
|
||||
0x6b, 0x6c, 0x6d, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, \
|
||||
0x6d, 0x6e, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, \
|
||||
0x6f, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, \
|
||||
0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x6b, \
|
||||
0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x6c, 0x6d, \
|
||||
0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x6d, 0x6e, 0x6f, \
|
||||
0x70, 0x71, 0x72, 0x73, 0x74, 0x6e, 0x6f, 0x70, 0x71, \
|
||||
0x72, 0x73, 0x74, 0x75 )
|
||||
|
||||
/**
|
||||
* Report a digest test result
|
||||
*
|
||||
* @v test Digest test
|
||||
*/
|
||||
#define digest_ok(test) digest_okx ( test, __FILE__, __LINE__ )
|
||||
|
||||
extern void digest_okx ( struct digest_test *test, const char *file,
|
||||
unsigned int line );
|
||||
extern unsigned long digest_cost ( struct digest_algorithm *digest );
|
||||
|
||||
#endif /* _DIGEST_TEST_H */
|
||||
|
|
|
@ -27,74 +27,46 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
|||
*
|
||||
* MD5 tests
|
||||
*
|
||||
* Test inputs borrowed from NIST SHA-1 tests, with results calculated
|
||||
* using md5sum.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
/* Forcibly enable assertions */
|
||||
#undef NDEBUG
|
||||
|
||||
#include <ipxe/md5.h>
|
||||
#include <ipxe/test.h>
|
||||
#include "digest_test.h"
|
||||
|
||||
/** An MD5 test vector */
|
||||
struct md5_test_vector {
|
||||
/** Test data */
|
||||
void *data;
|
||||
/** Test data length */
|
||||
size_t len;
|
||||
/** Expected digest */
|
||||
uint8_t digest[MD5_DIGEST_SIZE];
|
||||
};
|
||||
/* Empty test vector (digest obtained from "md5sum /dev/null") */
|
||||
DIGEST_TEST ( md5_empty, &md5_algorithm, DIGEST_EMPTY,
|
||||
DIGEST ( 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9,
|
||||
0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e ) );
|
||||
|
||||
/** MD5 test vectors */
|
||||
static struct md5_test_vector md5_test_vectors[] = {
|
||||
/* Test inputs borrowed from SHA-1 tests, with results
|
||||
* calculated using md5sum.
|
||||
*/
|
||||
{ NULL, 0,
|
||||
{ 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
|
||||
0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e } },
|
||||
{ "abc", 3,
|
||||
{ 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0,
|
||||
0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 } },
|
||||
{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56,
|
||||
{ 0x82, 0x15, 0xef, 0x07, 0x96, 0xa2, 0x0b, 0xca,
|
||||
0xaa, 0xe1, 0x16, 0xd3, 0x87, 0x6c, 0x66, 0x4a } },
|
||||
};
|
||||
/* NIST test vector "abc" (digest obtained from "md5sum <data>") */
|
||||
DIGEST_TEST ( md5_nist_abc, &md5_algorithm, DIGEST_NIST_ABC,
|
||||
DIGEST ( 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 0xd6,
|
||||
0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 ) );
|
||||
|
||||
/** MD5 test fragment lists */
|
||||
static struct digest_test_fragments md5_test_fragments[] = {
|
||||
{ { 0, -1UL, } },
|
||||
{ { 1, 1, 1, 1, 1, 1, 1, 1 } },
|
||||
{ { 2, 0, 23, 4, 6, 1, 0 } },
|
||||
};
|
||||
/* NIST test vector "abc...opq" (digest obtained from "md5sum <data>") */
|
||||
DIGEST_TEST ( md5_nist_abc_opq, &md5_algorithm, DIGEST_NIST_ABC_OPQ,
|
||||
DIGEST ( 0x82, 0x15, 0xef, 0x07, 0x96, 0xa2, 0x0b, 0xca, 0xaa,
|
||||
0xe1, 0x16, 0xd3, 0x87, 0x6c, 0x66, 0x4a ) );
|
||||
|
||||
/**
|
||||
* Perform MD5 self-test
|
||||
*
|
||||
*/
|
||||
static void md5_test_exec ( void ) {
|
||||
struct digest_algorithm *digest = &md5_algorithm;
|
||||
struct md5_test_vector *test;
|
||||
unsigned long cost;
|
||||
unsigned int i;
|
||||
unsigned int j;
|
||||
|
||||
/* Correctness test */
|
||||
for ( i = 0 ; i < ( sizeof ( md5_test_vectors ) /
|
||||
sizeof ( md5_test_vectors[0] ) ) ; i++ ) {
|
||||
test = &md5_test_vectors[i];
|
||||
/* Test with a single pass */
|
||||
digest_ok ( digest, NULL, test->data, test->len, test->digest );
|
||||
/* Test with fragment lists */
|
||||
for ( j = 0 ; j < ( sizeof ( md5_test_fragments ) /
|
||||
sizeof ( md5_test_fragments[0] ) ) ; j++ ){
|
||||
digest_ok ( digest, &md5_test_fragments[j],
|
||||
test->data, test->len, test->digest );
|
||||
}
|
||||
}
|
||||
/* Correctness tests */
|
||||
digest_ok ( &md5_empty );
|
||||
digest_ok ( &md5_nist_abc );
|
||||
digest_ok ( &md5_nist_abc_opq );
|
||||
|
||||
/* Speed test */
|
||||
cost = digest_cost ( digest );
|
||||
DBG ( "MD5 required %ld cycles per byte\n", cost );
|
||||
/* Speed tests */
|
||||
DBG ( "MD5 required %ld cycles per byte\n",
|
||||
digest_cost ( &md5_algorithm ) );
|
||||
}
|
||||
|
||||
/** MD5 self-test */
|
||||
|
|
|
@ -27,79 +27,51 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
|||
*
|
||||
* SHA-1 tests
|
||||
*
|
||||
* NIST test vectors are taken from
|
||||
*
|
||||
* http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA1.pdf
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
/* Forcibly enable assertions */
|
||||
#undef NDEBUG
|
||||
|
||||
#include <ipxe/sha1.h>
|
||||
#include <ipxe/test.h>
|
||||
#include "digest_test.h"
|
||||
|
||||
/** An SHA-1 test vector */
|
||||
struct sha1_test_vector {
|
||||
/** Test data */
|
||||
void *data;
|
||||
/** Test data length */
|
||||
size_t len;
|
||||
/** Expected digest */
|
||||
uint8_t digest[SHA1_DIGEST_SIZE];
|
||||
};
|
||||
/* Empty test vector (digest obtained from "sha1sum /dev/null") */
|
||||
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 ) );
|
||||
|
||||
/** SHA-1 test vectors */
|
||||
static struct sha1_test_vector sha1_test_vectors[] = {
|
||||
/* Empty test data
|
||||
*
|
||||
* Expected digest value obtained from "sha1sum /dev/null"
|
||||
*/
|
||||
{ NULL, 0,
|
||||
{ 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55,
|
||||
0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09 } },
|
||||
/* Test data and expected digests taken from the NIST
|
||||
* Cryptographic Toolkit Algorithm Examples at
|
||||
* http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA1.pdf
|
||||
*/
|
||||
{ "abc", 3,
|
||||
{ 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e,
|
||||
0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d } },
|
||||
{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56,
|
||||
{ 0x84, 0x98, 0x3e, 0x44, 0x1c, 0x3b, 0xd2, 0x6e, 0xba, 0xae,
|
||||
0x4a, 0xa1, 0xf9, 0x51, 0x29, 0xe5, 0xe5, 0x46, 0x70, 0xf1 } },
|
||||
};
|
||||
/* NIST test vector "abc" */
|
||||
DIGEST_TEST ( sha1_nist_abc, &sha1_algorithm, DIGEST_NIST_ABC,
|
||||
DIGEST ( 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba,
|
||||
0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0,
|
||||
0xd8, 0x9d ) );
|
||||
|
||||
/** SHA-1 test fragment lists */
|
||||
static struct digest_test_fragments sha1_test_fragments[] = {
|
||||
{ { 0, -1UL, } },
|
||||
{ { 1, 1, 1, 1, 1, 1, 1, 1 } },
|
||||
{ { 2, 0, 23, 4, 6, 1, 0 } },
|
||||
};
|
||||
/* NIST test vector "abc...opq" */
|
||||
DIGEST_TEST ( sha1_nist_abc_opq, &sha1_algorithm, DIGEST_NIST_ABC_OPQ,
|
||||
DIGEST ( 0x84, 0x98, 0x3e, 0x44, 0x1c, 0x3b, 0xd2, 0x6e, 0xba,
|
||||
0xae, 0x4a, 0xa1, 0xf9, 0x51, 0x29, 0xe5, 0xe5, 0x46,
|
||||
0x70, 0xf1 ) );
|
||||
|
||||
/**
|
||||
* Perform SHA-1 self-test
|
||||
*
|
||||
*/
|
||||
static void sha1_test_exec ( void ) {
|
||||
struct digest_algorithm *digest = &sha1_algorithm;
|
||||
struct sha1_test_vector *test;
|
||||
unsigned long cost;
|
||||
unsigned int i;
|
||||
unsigned int j;
|
||||
|
||||
/* Correctness test */
|
||||
for ( i = 0 ; i < ( sizeof ( sha1_test_vectors ) /
|
||||
sizeof ( sha1_test_vectors[0] ) ) ; i++ ) {
|
||||
test = &sha1_test_vectors[i];
|
||||
/* Test with a single pass */
|
||||
digest_ok ( digest, NULL, test->data, test->len, test->digest );
|
||||
/* Test with fragment lists */
|
||||
for ( j = 0 ; j < ( sizeof ( sha1_test_fragments ) /
|
||||
sizeof ( sha1_test_fragments[0] ) ) ; j++ ){
|
||||
digest_ok ( digest, &sha1_test_fragments[j],
|
||||
test->data, test->len, test->digest );
|
||||
}
|
||||
}
|
||||
/* Correctness tests */
|
||||
digest_ok ( &sha1_empty );
|
||||
digest_ok ( &sha1_nist_abc );
|
||||
digest_ok ( &sha1_nist_abc_opq );
|
||||
|
||||
/* Speed test */
|
||||
cost = digest_cost ( digest );
|
||||
DBG ( "SHA1 required %ld cycles per byte\n", cost );
|
||||
/* Speed tests */
|
||||
DBG ( "SHA1 required %ld cycles per byte\n",
|
||||
digest_cost ( &sha1_algorithm ) );
|
||||
}
|
||||
|
||||
/** SHA-1 self-test */
|
||||
|
|
|
@ -27,82 +27,54 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
|||
*
|
||||
* SHA-256 tests
|
||||
*
|
||||
* NIST test vectors are taken from
|
||||
*
|
||||
* http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA256.pdf
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
/* Forcibly enable assertions */
|
||||
#undef NDEBUG
|
||||
|
||||
#include <ipxe/sha256.h>
|
||||
#include <ipxe/test.h>
|
||||
#include "digest_test.h"
|
||||
|
||||
/** An SHA-256 test vector */
|
||||
struct sha256_test_vector {
|
||||
/** Test data */
|
||||
void *data;
|
||||
/** Test data length */
|
||||
size_t len;
|
||||
/** Expected digest */
|
||||
uint8_t digest[SHA256_DIGEST_SIZE];
|
||||
};
|
||||
/* Empty test vector (digest obtained from "sha256sum /dev/null") */
|
||||
DIGEST_TEST ( sha256_empty, &sha256_algorithm, DIGEST_EMPTY,
|
||||
DIGEST ( 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a,
|
||||
0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae,
|
||||
0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99,
|
||||
0x1b, 0x78, 0x52, 0xb8, 0x55 ) );
|
||||
|
||||
/** SHA-256 test vectors */
|
||||
static struct sha256_test_vector sha256_test_vectors[] = {
|
||||
/* Empty test data
|
||||
*
|
||||
* Expected digest value obtained from "sha256sum /dev/null"
|
||||
*/
|
||||
{ NULL, 0,
|
||||
{ 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4,
|
||||
0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b,
|
||||
0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 } },
|
||||
/* Test data and expected digests taken from the NIST
|
||||
* Cryptographic Toolkit Algorithm Examples at
|
||||
* http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA256.pdf
|
||||
*/
|
||||
{ "abc", 3,
|
||||
{ 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40,
|
||||
0xde, 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17,
|
||||
0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad } },
|
||||
{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56,
|
||||
{ 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26,
|
||||
0x93, 0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff,
|
||||
0x21, 0x67, 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 } },
|
||||
};
|
||||
/* NIST test vector "abc" */
|
||||
DIGEST_TEST ( sha256_nist_abc, &sha256_algorithm, DIGEST_NIST_ABC,
|
||||
DIGEST ( 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41,
|
||||
0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03,
|
||||
0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff,
|
||||
0x61, 0xf2, 0x00, 0x15, 0xad ) );
|
||||
|
||||
/** SHA-256 test fragment lists */
|
||||
static struct digest_test_fragments sha256_test_fragments[] = {
|
||||
{ { 0, -1UL, } },
|
||||
{ { 1, 1, 1, 1, 1, 1, 1, 1 } },
|
||||
{ { 2, 0, 23, 4, 6, 1, 0 } },
|
||||
};
|
||||
/* NIST test vector "abc...opq" */
|
||||
DIGEST_TEST ( sha256_nist_abc_opq, &sha256_algorithm, DIGEST_NIST_ABC_OPQ,
|
||||
DIGEST ( 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5,
|
||||
0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c,
|
||||
0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, 0xf6, 0xec, 0xed,
|
||||
0xd4, 0x19, 0xdb, 0x06, 0xc1 ) );
|
||||
|
||||
/**
|
||||
* Perform SHA-256 self-test
|
||||
*
|
||||
*/
|
||||
static void sha256_test_exec ( void ) {
|
||||
struct digest_algorithm *digest = &sha256_algorithm;
|
||||
struct sha256_test_vector *test;
|
||||
unsigned long cost;
|
||||
unsigned int i;
|
||||
unsigned int j;
|
||||
|
||||
/* Correctness test */
|
||||
for ( i = 0 ; i < ( sizeof ( sha256_test_vectors ) /
|
||||
sizeof ( sha256_test_vectors[0] ) ) ; i++ ) {
|
||||
test = &sha256_test_vectors[i];
|
||||
/* Test with a single pass */
|
||||
digest_ok ( digest, NULL, test->data, test->len, test->digest );
|
||||
/* Test with fragment lists */
|
||||
for ( j = 0 ; j < ( sizeof ( sha256_test_fragments ) /
|
||||
sizeof ( sha256_test_fragments[0] )); j++ ){
|
||||
digest_ok ( digest, &sha256_test_fragments[j],
|
||||
test->data, test->len, test->digest );
|
||||
}
|
||||
}
|
||||
/* Correctness tests */
|
||||
digest_ok ( &sha256_empty );
|
||||
digest_ok ( &sha256_nist_abc );
|
||||
digest_ok ( &sha256_nist_abc_opq );
|
||||
|
||||
/* Speed test */
|
||||
cost = digest_cost ( digest );
|
||||
DBG ( "SHA256 required %ld cycles per byte\n", cost );
|
||||
/* Speed tests */
|
||||
DBG ( "SHA256 required %ld cycles per byte\n",
|
||||
digest_cost ( &sha256_algorithm ) );
|
||||
}
|
||||
|
||||
/** SHA-256 self-test */
|
||||
|
|
Loading…
Reference in New Issue