Browse Source

[test] Add speed tests for digest algorithms

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 years ago
parent
commit
681a219caa
5 changed files with 54 additions and 0 deletions
  1. 35
    0
      src/tests/digest_test.c
  2. 1
    0
      src/tests/digest_test.h
  3. 6
    0
      src/tests/md5_test.c
  4. 6
    0
      src/tests/sha1_test.c
  5. 6
    0
      src/tests/sha256_test.c

+ 35
- 0
src/tests/digest_test.c View File

25
  *
25
  *
26
  */
26
  */
27
 
27
 
28
+#include <stdlib.h>
28
 #include <string.h>
29
 #include <string.h>
29
 #include <ipxe/crypto.h>
30
 #include <ipxe/crypto.h>
31
+#include <ipxe/profile.h>
30
 #include "digest_test.h"
32
 #include "digest_test.h"
31
 
33
 
32
 /**
34
 /**
68
 	/* Compare against expected output */
70
 	/* Compare against expected output */
69
 	return ( memcmp ( expected, out, sizeof ( out ) ) == 0 );
71
 	return ( memcmp ( expected, out, sizeof ( out ) ) == 0 );
70
 }
72
 }
73
+
74
+/**
75
+ * Calculate digest algorithm cost
76
+ *
77
+ * @v digest		Digest algorithm
78
+ * @ret cost		Cost (in cycles per byte)
79
+ */
80
+unsigned long digest_cost ( struct digest_algorithm *digest ) {
81
+	static uint8_t random[8192]; /* Too large for stack */
82
+	uint8_t ctx[digest->ctxsize];
83
+	uint8_t out[digest->digestsize];
84
+	union profiler profiler;
85
+	unsigned long long elapsed;
86
+	unsigned long cost;
87
+	unsigned int i;
88
+
89
+	/* Fill buffer with pseudo-random data */
90
+	srand ( 0x1234568 );
91
+	for ( i = 0 ; i < sizeof ( random ) ; i++ )
92
+		random[i] = rand();
93
+
94
+	/* Time digest calculation */
95
+	profile ( &profiler );
96
+	digest_init ( digest, ctx );
97
+	digest_update ( digest, ctx, random, sizeof ( random ) );
98
+	digest_final ( digest, ctx, out );
99
+	elapsed = profile ( &profiler );
100
+
101
+	/* Round to nearest whole number of cycles per byte */
102
+	cost = ( ( elapsed + ( sizeof ( random ) / 2 ) ) / sizeof ( random ) );
103
+
104
+	return cost;
105
+}

+ 1
- 0
src/tests/digest_test.h View File

19
 extern int digest_test ( struct digest_algorithm *digest,
19
 extern int digest_test ( struct digest_algorithm *digest,
20
 			 struct digest_test_fragments *fragments,
20
 			 struct digest_test_fragments *fragments,
21
 			 void *data, size_t len, void *expected );
21
 			 void *data, size_t len, void *expected );
22
+extern unsigned long digest_cost ( struct digest_algorithm *digest );
22
 
23
 
23
 /**
24
 /**
24
  * Report digest test result
25
  * Report digest test result

+ 6
- 0
src/tests/md5_test.c View File

70
 static void md5_test_exec ( void ) {
70
 static void md5_test_exec ( void ) {
71
 	struct digest_algorithm *digest = &md5_algorithm;
71
 	struct digest_algorithm *digest = &md5_algorithm;
72
 	struct md5_test_vector *test;
72
 	struct md5_test_vector *test;
73
+	unsigned long cost;
73
 	unsigned int i;
74
 	unsigned int i;
74
 	unsigned int j;
75
 	unsigned int j;
75
 
76
 
77
+	/* Correctness test */
76
 	for ( i = 0 ; i < ( sizeof ( md5_test_vectors ) /
78
 	for ( i = 0 ; i < ( sizeof ( md5_test_vectors ) /
77
 			    sizeof ( md5_test_vectors[0] ) ) ; i++ ) {
79
 			    sizeof ( md5_test_vectors[0] ) ) ; i++ ) {
78
 		test = &md5_test_vectors[i];
80
 		test = &md5_test_vectors[i];
85
 				    test->data, test->len, test->digest );
87
 				    test->data, test->len, test->digest );
86
 		}
88
 		}
87
 	}
89
 	}
90
+
91
+	/* Speed test */
92
+	cost = digest_cost ( digest );
93
+	DBG ( "MD5 required %ld cycles per byte\n", cost );
88
 }
94
 }
89
 
95
 
90
 /** MD5 self-test */
96
 /** MD5 self-test */

+ 6
- 0
src/tests/sha1_test.c View File

75
 static void sha1_test_exec ( void ) {
75
 static void sha1_test_exec ( void ) {
76
 	struct digest_algorithm *digest = &sha1_algorithm;
76
 	struct digest_algorithm *digest = &sha1_algorithm;
77
 	struct sha1_test_vector *test;
77
 	struct sha1_test_vector *test;
78
+	unsigned long cost;
78
 	unsigned int i;
79
 	unsigned int i;
79
 	unsigned int j;
80
 	unsigned int j;
80
 
81
 
82
+	/* Correctness test */
81
 	for ( i = 0 ; i < ( sizeof ( sha1_test_vectors ) /
83
 	for ( i = 0 ; i < ( sizeof ( sha1_test_vectors ) /
82
 			    sizeof ( sha1_test_vectors[0] ) ) ; i++ ) {
84
 			    sizeof ( sha1_test_vectors[0] ) ) ; i++ ) {
83
 		test = &sha1_test_vectors[i];
85
 		test = &sha1_test_vectors[i];
90
 				    test->data, test->len, test->digest );
92
 				    test->data, test->len, test->digest );
91
 		}
93
 		}
92
 	}
94
 	}
95
+
96
+	/* Speed test */
97
+	cost = digest_cost ( digest );
98
+	DBG ( "SHA1 required %ld cycles per byte\n", cost );
93
 }
99
 }
94
 
100
 
95
 /** SHA-1 self-test */
101
 /** SHA-1 self-test */

+ 6
- 0
src/tests/sha256_test.c View File

78
 static void sha256_test_exec ( void ) {
78
 static void sha256_test_exec ( void ) {
79
 	struct digest_algorithm *digest = &sha256_algorithm;
79
 	struct digest_algorithm *digest = &sha256_algorithm;
80
 	struct sha256_test_vector *test;
80
 	struct sha256_test_vector *test;
81
+	unsigned long cost;
81
 	unsigned int i;
82
 	unsigned int i;
82
 	unsigned int j;
83
 	unsigned int j;
83
 
84
 
85
+	/* Correctness test */
84
 	for ( i = 0 ; i < ( sizeof ( sha256_test_vectors ) /
86
 	for ( i = 0 ; i < ( sizeof ( sha256_test_vectors ) /
85
 			    sizeof ( sha256_test_vectors[0] ) ) ; i++ ) {
87
 			    sizeof ( sha256_test_vectors[0] ) ) ; i++ ) {
86
 		test = &sha256_test_vectors[i];
88
 		test = &sha256_test_vectors[i];
93
 				    test->data, test->len, test->digest );
95
 				    test->data, test->len, test->digest );
94
 		}
96
 		}
95
 	}
97
 	}
98
+
99
+	/* Speed test */
100
+	cost = digest_cost ( digest );
101
+	DBG ( "SHA256 required %ld cycles per byte\n", cost );
96
 }
102
 }
97
 
103
 
98
 /** SHA-256 self-test */
104
 /** SHA-256 self-test */

Loading…
Cancel
Save