summaryrefslogtreecommitdiffstats
path: root/src/tests
diff options
context:
space:
mode:
authorMichael Brown2012-09-26 16:27:33 +0200
committerMichael Brown2012-09-26 16:27:33 +0200
commitc1adf7dabee103c1ca0b73e78c2d5a79b241830e (patch)
tree67ff39116bd8b2258cc974169a78087377a2128c /src/tests
parent[test] Add speed tests for digest algorithms (diff)
downloadipxe-c1adf7dabee103c1ca0b73e78c2d5a79b241830e.tar.gz
ipxe-c1adf7dabee103c1ca0b73e78c2d5a79b241830e.tar.xz
ipxe-c1adf7dabee103c1ca0b73e78c2d5a79b241830e.zip
[test] Add speed tests for cipher algorithms
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/aes_cbc_test.c12
-rw-r--r--src/tests/cbc_test.c82
-rw-r--r--src/tests/cbc_test.h4
3 files changed, 94 insertions, 4 deletions
diff --git a/src/tests/aes_cbc_test.c b/src/tests/aes_cbc_test.c
index 09d38075..4ae3a92e 100644
--- a/src/tests/aes_cbc_test.c
+++ b/src/tests/aes_cbc_test.c
@@ -169,9 +169,21 @@ AES_CBC_TEST ( test_256,
*
*/
static void aes_cbc_test_exec ( void ) {
+ struct cipher_algorithm *cipher = &aes_cbc_algorithm;
+ /* Correctness tests */
aes_cbc_ok ( &test_128 );
aes_cbc_ok ( &test_256 );
+
+ /* Speed tests */
+ DBG ( "AES128 encryption required %ld cycles per byte\n",
+ cbc_cost_encrypt ( cipher, test_128.key_len ) );
+ DBG ( "AES128 decryption required %ld cycles per byte\n",
+ cbc_cost_decrypt ( cipher, test_128.key_len ) );
+ DBG ( "AES256 encryption required %ld cycles per byte\n",
+ cbc_cost_encrypt ( cipher, test_256.key_len ) );
+ DBG ( "AES256 decryption required %ld cycles per byte\n",
+ cbc_cost_decrypt ( cipher, test_256.key_len ) );
}
/** AES-in-CBC-mode self-test */
diff --git a/src/tests/cbc_test.c b/src/tests/cbc_test.c
index 7d755309..ada991b2 100644
--- a/src/tests/cbc_test.c
+++ b/src/tests/cbc_test.c
@@ -29,9 +29,11 @@ FILE_LICENCE ( GPL2_OR_LATER );
#undef NDEBUG
#include <stdint.h>
+#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ipxe/crypto.h>
+#include <ipxe/profile.h>
#include "cbc_test.h"
/**
@@ -49,8 +51,8 @@ FILE_LICENCE ( GPL2_OR_LATER );
int cbc_test_encrypt ( struct cipher_algorithm *cipher, const void *key,
size_t key_len, const void *iv, const void *plaintext,
const void *expected_ciphertext, size_t len ) {
- uint8_t ctx[ cipher->ctxsize ];
- uint8_t ciphertext[ len ];
+ uint8_t ctx[cipher->ctxsize];
+ uint8_t ciphertext[len];
int rc;
/* Initialise cipher */
@@ -80,8 +82,8 @@ int cbc_test_encrypt ( struct cipher_algorithm *cipher, const void *key,
int cbc_test_decrypt ( struct cipher_algorithm *cipher, const void *key,
size_t key_len, const void *iv, const void *ciphertext,
const void *expected_plaintext, size_t len ) {
- uint8_t ctx[ cipher->ctxsize ];
- uint8_t plaintext[ len ];
+ uint8_t ctx[cipher->ctxsize];
+ uint8_t plaintext[len];
int rc;
/* Initialise cipher */
@@ -95,3 +97,75 @@ int cbc_test_decrypt ( struct cipher_algorithm *cipher, const void *key,
/* Verify result */
return ( memcmp ( plaintext, expected_plaintext, len ) == 0 );
}
+
+/**
+ * Calculate CBC encryption or decryption cost
+ *
+ * @v cipher Cipher algorithm
+ * @v key_len Length of key
+ * @v op Encryption or decryption operation
+ * @ret cost Cost (in cycles per byte)
+ */
+static unsigned long cbc_cost ( struct cipher_algorithm *cipher,
+ size_t key_len,
+ void ( * op ) ( struct cipher_algorithm *cipher,
+ void *ctx, const void *src,
+ void *dst, size_t len ) ) {
+ static uint8_t random[8192]; /* Too large for stack */
+ uint8_t key[key_len];
+ uint8_t iv[cipher->blocksize];
+ uint8_t ctx[cipher->ctxsize];
+ union profiler profiler;
+ unsigned long long elapsed;
+ unsigned long cost;
+ unsigned int i;
+ int rc;
+
+ /* Fill buffer with pseudo-random data */
+ srand ( 0x1234568 );
+ for ( i = 0 ; i < sizeof ( random ) ; i++ )
+ random[i] = rand();
+ for ( i = 0 ; i < sizeof ( key ) ; i++ )
+ key[i] = rand();
+ for ( i = 0 ; i < sizeof ( iv ) ; i++ )
+ iv[i] = rand();
+
+ /* Initialise cipher */
+ rc = cipher_setkey ( cipher, ctx, key, key_len );
+ assert ( rc == 0 );
+ cipher_setiv ( cipher, ctx, iv );
+
+ /* Time operation */
+ profile ( &profiler );
+ op ( cipher, ctx, random, random, sizeof ( random ) );
+ elapsed = profile ( &profiler );
+
+ /* Round to nearest whole number of cycles per byte */
+ cost = ( ( elapsed + ( sizeof ( random ) / 2 ) ) / sizeof ( random ) );
+
+ return cost;
+}
+
+/**
+ * Calculate CBC encryption cost
+ *
+ * @v cipher Cipher algorithm
+ * @v key_len Length of key
+ * @ret cost Cost (in cycles per byte)
+ */
+unsigned long cbc_cost_encrypt ( struct cipher_algorithm *cipher,
+ size_t key_len ) {
+ return cbc_cost ( cipher, key_len, cipher_encrypt );
+}
+
+/**
+ * Calculate CBC decryption cost
+ *
+ * @v cipher Cipher algorithm
+ * @v key_len Length of key
+ * @ret cost Cost (in cycles per byte)
+ */
+unsigned long cbc_cost_decrypt ( struct cipher_algorithm *cipher,
+ size_t key_len ) {
+ return cbc_cost ( cipher, key_len, cipher_decrypt );
+}
diff --git a/src/tests/cbc_test.h b/src/tests/cbc_test.h
index 40356cc3..ad9e6f34 100644
--- a/src/tests/cbc_test.h
+++ b/src/tests/cbc_test.h
@@ -15,6 +15,10 @@ extern int cbc_test_decrypt ( struct cipher_algorithm *cipher, const void *key,
size_t key_len, const void *iv,
const void *ciphertext,
const void *expected_plaintext, size_t len );
+extern unsigned long cbc_cost_encrypt ( struct cipher_algorithm *cipher,
+ size_t key_len );
+extern unsigned long cbc_cost_decrypt ( struct cipher_algorithm *cipher,
+ size_t key_len );
/**
* Report CBC encryption test result