浏览代码

[test] Add speed tests for cipher algorithms

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 年前
父节点
当前提交
c1adf7dabe
共有 3 个文件被更改,包括 94 次插入4 次删除
  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 查看文件

@@ -169,9 +169,21 @@ AES_CBC_TEST ( test_256,
169 169
  *
170 170
  */
171 171
 static void aes_cbc_test_exec ( void ) {
172
+	struct cipher_algorithm *cipher = &aes_cbc_algorithm;
172 173
 
174
+	/* Correctness tests */
173 175
 	aes_cbc_ok ( &test_128 );
174 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 189
 /** AES-in-CBC-mode self-test */

+ 78
- 4
src/tests/cbc_test.c 查看文件

@@ -29,9 +29,11 @@ FILE_LICENCE ( GPL2_OR_LATER );
29 29
 #undef NDEBUG
30 30
 
31 31
 #include <stdint.h>
32
+#include <stdlib.h>
32 33
 #include <string.h>
33 34
 #include <assert.h>
34 35
 #include <ipxe/crypto.h>
36
+#include <ipxe/profile.h>
35 37
 #include "cbc_test.h"
36 38
 
37 39
 /**
@@ -49,8 +51,8 @@ FILE_LICENCE ( GPL2_OR_LATER );
49 51
 int cbc_test_encrypt ( struct cipher_algorithm *cipher, const void *key,
50 52
 		       size_t key_len, const void *iv, const void *plaintext,
51 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 56
 	int rc;
55 57
 
56 58
 	/* Initialise cipher */
@@ -80,8 +82,8 @@ int cbc_test_encrypt ( struct cipher_algorithm *cipher, const void *key,
80 82
 int cbc_test_decrypt ( struct cipher_algorithm *cipher, const void *key,
81 83
 		       size_t key_len, const void *iv, const void *ciphertext,
82 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 87
 	int rc;
86 88
 
87 89
 	/* Initialise cipher */
@@ -95,3 +97,75 @@ int cbc_test_decrypt ( struct cipher_algorithm *cipher, const void *key,
95 97
 	/* Verify result */
96 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 查看文件

@@ -15,6 +15,10 @@ extern int cbc_test_decrypt ( struct cipher_algorithm *cipher, const void *key,
15 15
 			      size_t key_len, const void *iv,
16 16
 			      const void *ciphertext,
17 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 24
  * Report CBC encryption test result

正在加载...
取消
保存