iPXE
Defines | Functions
cipher_test.c File Reference

Cipher self-tests. More...

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ipxe/crypto.h>
#include <ipxe/profile.h>
#include <ipxe/test.h>
#include "cipher_test.h"

Go to the source code of this file.

Defines

#define PROFILE_COUNT   16
 Number of sample iterations for profiling.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
void cipher_encrypt_okx (struct cipher_test *test, const char *file, unsigned int line)
 Report a cipher encryption test result.
void cipher_decrypt_okx (struct cipher_test *test, const char *file, unsigned int line)
 Report a cipher decryption test result.
void cipher_okx (struct cipher_test *test, const char *file, unsigned int line)
 Report a cipher encryption and decryption test result.
static unsigned long cipher_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))
 Calculate cipher encryption or decryption cost.
unsigned long cipher_cost_encrypt (struct cipher_algorithm *cipher, size_t key_len)
 Calculate cipher encryption cost.
unsigned long cipher_cost_decrypt (struct cipher_algorithm *cipher, size_t key_len)
 Calculate cipher decryption cost.

Detailed Description

Cipher self-tests.

Definition in file cipher_test.c.


Define Documentation

#define PROFILE_COUNT   16

Number of sample iterations for profiling.

Definition at line 45 of file cipher_test.c.

Referenced by cipher_cost().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
void cipher_encrypt_okx ( struct cipher_test test,
const char *  file,
unsigned int  line 
)

Report a cipher encryption test result.

Parameters:
testCipher test
fileTest code file
lineTest code line

Definition at line 54 of file cipher_test.c.

References cipher_test::cipher, cipher_encrypt, cipher_setiv(), cipher_setkey(), cipher_test::ciphertext, ctx, cipher_algorithm::ctxsize, cipher_test::iv, cipher_test::key, cipher_test::key_len, cipher_test::len, len, memcmp(), okx, and cipher_test::plaintext.

Referenced by cipher_okx().

                                              {
        struct cipher_algorithm *cipher = test->cipher;
        size_t len = test->len;
        uint8_t ctx[cipher->ctxsize];
        uint8_t ciphertext[len];

        /* Initialise cipher */
        okx ( cipher_setkey ( cipher, ctx, test->key, test->key_len ) == 0,
              file, line );
        cipher_setiv ( cipher, ctx, test->iv );

        /* Perform encryption */
        cipher_encrypt ( cipher, ctx, test->plaintext, ciphertext, len );

        /* Compare against expected ciphertext */
        okx ( memcmp ( ciphertext, test->ciphertext, len ) == 0, file, line );
}
void cipher_decrypt_okx ( struct cipher_test test,
const char *  file,
unsigned int  line 
)

Report a cipher decryption test result.

Parameters:
testCipher test
fileTest code file
lineTest code line

Definition at line 80 of file cipher_test.c.

References cipher_test::cipher, cipher_decrypt, cipher_setiv(), cipher_setkey(), cipher_test::ciphertext, ctx, cipher_algorithm::ctxsize, cipher_test::iv, cipher_test::key, cipher_test::key_len, cipher_test::len, len, memcmp(), okx, and cipher_test::plaintext.

Referenced by cipher_okx().

                                              {
        struct cipher_algorithm *cipher = test->cipher;
        size_t len = test->len;
        uint8_t ctx[cipher->ctxsize];
        uint8_t plaintext[len];

        /* Initialise cipher */
        okx ( cipher_setkey ( cipher, ctx, test->key, test->key_len ) == 0,
              file, line );
        cipher_setiv ( cipher, ctx, test->iv );

        /* Perform encryption */
        cipher_decrypt ( cipher, ctx, test->ciphertext, plaintext, len );

        /* Compare against expected plaintext */
        okx ( memcmp ( plaintext, test->plaintext, len ) == 0, file, line );
}
void cipher_okx ( struct cipher_test test,
const char *  file,
unsigned int  line 
)

Report a cipher encryption and decryption test result.

Parameters:
testCipher test
fileTest code file
lineTest code line

Definition at line 106 of file cipher_test.c.

References cipher_decrypt_okx(), and cipher_encrypt_okx().

                                      {

        cipher_encrypt_okx ( test, file, line );
        cipher_decrypt_okx ( test, file, line );
}
static unsigned long cipher_cost ( struct cipher_algorithm cipher,
size_t  key_len,
void(*)(struct cipher_algorithm *cipher, void *ctx, const void *src, void *dst, size_t len op 
) [static]

Calculate cipher encryption or decryption cost.

Parameters:
cipherCipher algorithm
key_lenLength of key
opEncryption or decryption operation
Return values:
costCost (in cycles per byte)

Definition at line 122 of file cipher_test.c.

References assert, cipher_algorithm::blocksize, cipher_setiv(), cipher_setkey(), cost, cipher_algorithm::ctxsize, iv, key, memset(), op, PROFILE_COUNT, profile_mean(), profile_start(), profile_stop(), rand(), random(), rc, and srand().

Referenced by cipher_cost_decrypt(), and cipher_cost_encrypt().

                                                                         {
        static uint8_t random[8192]; /* Too large for stack */
        uint8_t key[key_len];
        uint8_t iv[cipher->blocksize];
        uint8_t ctx[cipher->ctxsize];
        struct profiler profiler;
        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 );

        /* Profile cipher operation */
        memset ( &profiler, 0, sizeof ( profiler ) );
        for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
                profile_start ( &profiler );
                op ( cipher, ctx, random, random, sizeof ( random ) );
                profile_stop ( &profiler );
        }

        /* Round to nearest whole number of cycles per byte */
        cost = ( ( profile_mean ( &profiler ) + ( sizeof ( random ) / 2 ) ) /
                 sizeof ( random ) );

        return cost;
}
unsigned long cipher_cost_encrypt ( struct cipher_algorithm cipher,
size_t  key_len 
)

Calculate cipher encryption cost.

Parameters:
cipherCipher algorithm
key_lenLength of key
Return values:
costCost (in cycles per byte)

Definition at line 170 of file cipher_test.c.

References cipher_cost(), and cipher_encrypt.

Referenced by aes_test_exec().

                                                     {
        return cipher_cost ( cipher, key_len, cipher_encrypt );
}
unsigned long cipher_cost_decrypt ( struct cipher_algorithm cipher,
size_t  key_len 
)

Calculate cipher decryption cost.

Parameters:
cipherCipher algorithm
key_lenLength of key
Return values:
costCost (in cycles per byte)

Definition at line 182 of file cipher_test.c.

References cipher_cost(), and cipher_decrypt.

Referenced by aes_test_exec().

                                                     {
        return cipher_cost ( cipher, key_len, cipher_decrypt );
}