Skip to content

Commit

Permalink
[test] Add speed tests for cipher algorithms
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Brown <mcb30@ipxe.org>
  • Loading branch information
mcb30 committed Sep 26, 2012
1 parent 681a219 commit c1adf7d
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/tests/aes_cbc_test.c
Expand Up @@ -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 */
Expand Down
82 changes: 78 additions & 4 deletions src/tests/cbc_test.c
Expand Up @@ -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"

/**
Expand All @@ -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 */
Expand Down Expand Up @@ -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 */
Expand All @@ -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 );
}
4 changes: 4 additions & 0 deletions src/tests/cbc_test.h
Expand Up @@ -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
Expand Down

0 comments on commit c1adf7d

Please sign in to comment.