david/ipxe
david
/
ipxe
Archived
1
0
Fork 0

[test] Add speed tests for digest algorithms

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown 2012-09-26 14:54:02 +01:00
parent 09cc63fc8b
commit 681a219caa
5 changed files with 54 additions and 0 deletions

View File

@ -25,8 +25,10 @@ FILE_LICENCE ( GPL2_OR_LATER );
*
*/
#include <stdlib.h>
#include <string.h>
#include <ipxe/crypto.h>
#include <ipxe/profile.h>
#include "digest_test.h"
/**
@ -68,3 +70,36 @@ int digest_test ( struct digest_algorithm *digest,
/* Compare against expected output */
return ( memcmp ( expected, out, sizeof ( out ) ) == 0 );
}
/**
* Calculate digest algorithm cost
*
* @v digest Digest algorithm
* @ret cost Cost (in cycles per byte)
*/
unsigned long digest_cost ( struct digest_algorithm *digest ) {
static uint8_t random[8192]; /* Too large for stack */
uint8_t ctx[digest->ctxsize];
uint8_t out[digest->digestsize];
union profiler profiler;
unsigned long long elapsed;
unsigned long cost;
unsigned int i;
/* Fill buffer with pseudo-random data */
srand ( 0x1234568 );
for ( i = 0 ; i < sizeof ( random ) ; i++ )
random[i] = rand();
/* Time digest calculation */
profile ( &profiler );
digest_init ( digest, ctx );
digest_update ( digest, ctx, random, sizeof ( random ) );
digest_final ( digest, ctx, out );
elapsed = profile ( &profiler );
/* Round to nearest whole number of cycles per byte */
cost = ( ( elapsed + ( sizeof ( random ) / 2 ) ) / sizeof ( random ) );
return cost;
}

View File

@ -19,6 +19,7 @@ struct digest_test_fragments {
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 );
/**
* Report digest test result

View File

@ -70,9 +70,11 @@ static struct digest_test_fragments md5_test_fragments[] = {
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];
@ -85,6 +87,10 @@ static void md5_test_exec ( void ) {
test->data, test->len, test->digest );
}
}
/* Speed test */
cost = digest_cost ( digest );
DBG ( "MD5 required %ld cycles per byte\n", cost );
}
/** MD5 self-test */

View File

@ -75,9 +75,11 @@ static struct digest_test_fragments sha1_test_fragments[] = {
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];
@ -90,6 +92,10 @@ static void sha1_test_exec ( void ) {
test->data, test->len, test->digest );
}
}
/* Speed test */
cost = digest_cost ( digest );
DBG ( "SHA1 required %ld cycles per byte\n", cost );
}
/** SHA-1 self-test */

View File

@ -78,9 +78,11 @@ static struct digest_test_fragments sha256_test_fragments[] = {
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];
@ -93,6 +95,10 @@ static void sha256_test_exec ( void ) {
test->data, test->len, test->digest );
}
}
/* Speed test */
cost = digest_cost ( digest );
DBG ( "SHA256 required %ld cycles per byte\n", cost );
}
/** SHA-256 self-test */