Browse Source

[test] Add speed tests for cipher algorithms

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 years ago
parent
commit
c1adf7dabe
3 changed files with 94 additions and 4 deletions
  1. 12
    0
      src/tests/aes_cbc_test.c
  2. 78
    4
      src/tests/cbc_test.c
  3. 4
    0
      src/tests/cbc_test.h

+ 12
- 0
src/tests/aes_cbc_test.c View File

169
  *
169
  *
170
  */
170
  */
171
 static void aes_cbc_test_exec ( void ) {
171
 static void aes_cbc_test_exec ( void ) {
172
+	struct cipher_algorithm *cipher = &aes_cbc_algorithm;
172
 
173
 
174
+	/* Correctness tests */
173
 	aes_cbc_ok ( &test_128 );
175
 	aes_cbc_ok ( &test_128 );
174
 	aes_cbc_ok ( &test_256 );
176
 	aes_cbc_ok ( &test_256 );
177
+
178
+	/* Speed tests */
179
+	DBG ( "AES128 encryption required %ld cycles per byte\n",
180
+	      cbc_cost_encrypt ( cipher, test_128.key_len ) );
181
+	DBG ( "AES128 decryption required %ld cycles per byte\n",
182
+	      cbc_cost_decrypt ( cipher, test_128.key_len ) );
183
+	DBG ( "AES256 encryption required %ld cycles per byte\n",
184
+	      cbc_cost_encrypt ( cipher, test_256.key_len ) );
185
+	DBG ( "AES256 decryption required %ld cycles per byte\n",
186
+	      cbc_cost_decrypt ( cipher, test_256.key_len ) );
175
 }
187
 }
176
 
188
 
177
 /** AES-in-CBC-mode self-test */
189
 /** AES-in-CBC-mode self-test */

+ 78
- 4
src/tests/cbc_test.c View File

29
 #undef NDEBUG
29
 #undef NDEBUG
30
 
30
 
31
 #include <stdint.h>
31
 #include <stdint.h>
32
+#include <stdlib.h>
32
 #include <string.h>
33
 #include <string.h>
33
 #include <assert.h>
34
 #include <assert.h>
34
 #include <ipxe/crypto.h>
35
 #include <ipxe/crypto.h>
36
+#include <ipxe/profile.h>
35
 #include "cbc_test.h"
37
 #include "cbc_test.h"
36
 
38
 
37
 /**
39
 /**
49
 int cbc_test_encrypt ( struct cipher_algorithm *cipher, const void *key,
51
 int cbc_test_encrypt ( struct cipher_algorithm *cipher, const void *key,
50
 		       size_t key_len, const void *iv, const void *plaintext,
52
 		       size_t key_len, const void *iv, const void *plaintext,
51
 		       const void *expected_ciphertext, size_t len ) {
53
 		       const void *expected_ciphertext, size_t len ) {
52
-	uint8_t ctx[ cipher->ctxsize ];
53
-	uint8_t ciphertext[ len ];
54
+	uint8_t ctx[cipher->ctxsize];
55
+	uint8_t ciphertext[len];
54
 	int rc;
56
 	int rc;
55
 
57
 
56
 	/* Initialise cipher */
58
 	/* Initialise cipher */
80
 int cbc_test_decrypt ( struct cipher_algorithm *cipher, const void *key,
82
 int cbc_test_decrypt ( struct cipher_algorithm *cipher, const void *key,
81
 		       size_t key_len, const void *iv, const void *ciphertext,
83
 		       size_t key_len, const void *iv, const void *ciphertext,
82
 		       const void *expected_plaintext, size_t len ) {
84
 		       const void *expected_plaintext, size_t len ) {
83
-	uint8_t ctx[ cipher->ctxsize ];
84
-	uint8_t plaintext[ len ];
85
+	uint8_t ctx[cipher->ctxsize];
86
+	uint8_t plaintext[len];
85
 	int rc;
87
 	int rc;
86
 
88
 
87
 	/* Initialise cipher */
89
 	/* Initialise cipher */
95
 	/* Verify result */
97
 	/* Verify result */
96
 	return ( memcmp ( plaintext, expected_plaintext, len ) == 0 );
98
 	return ( memcmp ( plaintext, expected_plaintext, len ) == 0 );
97
 }
99
 }
100
+
101
+/**
102
+ * Calculate CBC encryption or decryption cost
103
+ *
104
+ * @v cipher			Cipher algorithm
105
+ * @v key_len			Length of key
106
+ * @v op			Encryption or decryption operation
107
+ * @ret cost			Cost (in cycles per byte)
108
+ */
109
+static unsigned long cbc_cost ( struct cipher_algorithm *cipher,
110
+				size_t key_len,
111
+				void ( * op ) ( struct cipher_algorithm *cipher,
112
+						void *ctx, const void *src,
113
+						void *dst, size_t len ) ) {
114
+	static uint8_t random[8192]; /* Too large for stack */
115
+	uint8_t key[key_len];
116
+	uint8_t iv[cipher->blocksize];
117
+	uint8_t ctx[cipher->ctxsize];
118
+	union profiler profiler;
119
+	unsigned long long elapsed;
120
+	unsigned long cost;
121
+	unsigned int i;
122
+	int rc;
123
+
124
+	/* Fill buffer with pseudo-random data */
125
+	srand ( 0x1234568 );
126
+	for ( i = 0 ; i < sizeof ( random ) ; i++ )
127
+		random[i] = rand();
128
+	for ( i = 0 ; i < sizeof ( key ) ; i++ )
129
+		key[i] = rand();
130
+	for ( i = 0 ; i < sizeof ( iv ) ; i++ )
131
+		iv[i] = rand();
132
+
133
+	/* Initialise cipher */
134
+	rc = cipher_setkey ( cipher, ctx, key, key_len );
135
+	assert ( rc == 0 );
136
+	cipher_setiv ( cipher, ctx, iv );
137
+
138
+	/* Time operation */
139
+	profile ( &profiler );
140
+	op ( cipher, ctx, random, random, sizeof ( random ) );
141
+	elapsed = profile ( &profiler );
142
+
143
+	/* Round to nearest whole number of cycles per byte */
144
+	cost = ( ( elapsed + ( sizeof ( random ) / 2 ) ) / sizeof ( random ) );
145
+
146
+	return cost;
147
+}
148
+
149
+/**
150
+ * Calculate CBC encryption cost
151
+ *
152
+ * @v cipher			Cipher algorithm
153
+ * @v key_len			Length of key
154
+ * @ret cost			Cost (in cycles per byte)
155
+ */
156
+unsigned long cbc_cost_encrypt ( struct cipher_algorithm *cipher,
157
+				 size_t key_len ) {
158
+	return cbc_cost ( cipher, key_len, cipher_encrypt );
159
+}
160
+
161
+/**
162
+ * Calculate CBC decryption cost
163
+ *
164
+ * @v cipher			Cipher algorithm
165
+ * @v key_len			Length of key
166
+ * @ret cost			Cost (in cycles per byte)
167
+ */
168
+unsigned long cbc_cost_decrypt ( struct cipher_algorithm *cipher,
169
+				 size_t key_len ) {
170
+	return cbc_cost ( cipher, key_len, cipher_decrypt );
171
+}

+ 4
- 0
src/tests/cbc_test.h View File

15
 			      size_t key_len, const void *iv,
15
 			      size_t key_len, const void *iv,
16
 			      const void *ciphertext,
16
 			      const void *ciphertext,
17
 			      const void *expected_plaintext, size_t len );
17
 			      const void *expected_plaintext, size_t len );
18
+extern unsigned long cbc_cost_encrypt ( struct cipher_algorithm *cipher,
19
+					size_t key_len );
20
+extern unsigned long cbc_cost_decrypt ( struct cipher_algorithm *cipher,
21
+					size_t key_len );
18
 
22
 
19
 /**
23
 /**
20
  * Report CBC encryption test result
24
  * Report CBC encryption test result

Loading…
Cancel
Save