iPXE
Data Structures | Defines | Functions
cipher_test.h File Reference

Cipher self-tests. More...

#include <stdint.h>
#include <ipxe/crypto.h>
#include <ipxe/test.h>

Go to the source code of this file.

Data Structures

struct  cipher_test
 A cipher test. More...

Defines

#define KEY(...)   { __VA_ARGS__ }
 Define inline key.
#define IV(...)   { __VA_ARGS__ }
 Define inline initialisation vector.
#define PLAINTEXT(...)   { __VA_ARGS__ }
 Define inline plaintext data.
#define CIPHERTEXT(...)   { __VA_ARGS__ }
 Define inline ciphertext data.
#define CIPHER_TEST(name, CIPHER, KEY, IV, PLAINTEXT, CIPHERTEXT)
 Define a cipher test.
#define cipher_encrypt_ok(test)   cipher_encrypt_okx ( test, __FILE__, __LINE__ )
 Report a cipher encryption test result.
#define cipher_decrypt_ok(test)   cipher_decrypt_okx ( test, __FILE__, __LINE__ )
 Report a cipher decryption test result.
#define cipher_ok(test)   cipher_okx ( test, __FILE__, __LINE__ )
 Report a cipher encryption and decryption test result.

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.
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.h.


Define Documentation

#define KEY (   ...)    { __VA_ARGS__ }

Define inline key.

Definition at line 37 of file cipher_test.h.

#define IV (   ...)    { __VA_ARGS__ }

Define inline initialisation vector.

Definition at line 40 of file cipher_test.h.

#define PLAINTEXT (   ...)    { __VA_ARGS__ }

Define inline plaintext data.

Definition at line 43 of file cipher_test.h.

#define CIPHERTEXT (   ...)    { __VA_ARGS__ }

Define inline ciphertext data.

Definition at line 46 of file cipher_test.h.

#define CIPHER_TEST (   name,
  CIPHER,
  KEY,
  IV,
  PLAINTEXT,
  CIPHERTEXT 
)
Value:
static const uint8_t name ## _key [] = KEY;                     \
        static const uint8_t name ## _iv [] = IV;                       \
        static const uint8_t name ## _plaintext [] = PLAINTEXT;         \
        static const uint8_t name ## _ciphertext                        \
                [ sizeof ( name ## _plaintext ) ] = CIPHERTEXT;         \
        static struct cipher_test name = {                              \
                .cipher = CIPHER,                                       \
                .key = name ## _key,                                    \
                .key_len = sizeof ( name ## _key ),                     \
                .iv = name ## _iv,                                      \
                .iv_len = sizeof ( name ## _iv ),                       \
                .plaintext = name ## _plaintext,                        \
                .ciphertext = name ## _ciphertext,                      \
                .len = sizeof ( name ## _plaintext ),                   \
        }

Define a cipher test.

Parameters:
nameTest name
CIPHERCipher algorithm
KEYKey
IVInitialisation vector
PLAINTEXTPlaintext
CIPHERTEXTCiphertext
Return values:
testCipher test

Definition at line 59 of file cipher_test.h.

#define cipher_encrypt_ok (   test)    cipher_encrypt_okx ( test, __FILE__, __LINE__ )

Report a cipher encryption test result.

Parameters:
testCipher test

Definition at line 92 of file cipher_test.h.

#define cipher_decrypt_ok (   test)    cipher_decrypt_okx ( test, __FILE__, __LINE__ )

Report a cipher decryption test result.

Parameters:
testCipher test

Definition at line 100 of file cipher_test.h.

#define cipher_ok (   test)    cipher_okx ( test, __FILE__, __LINE__ )

Report a cipher encryption and decryption test result.

Parameters:
testCipher test

Definition at line 108 of file cipher_test.h.

Referenced by aes_test_exec().


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 );
}
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 );
}