iPXE
cipher_test.h
Go to the documentation of this file.
00001 #ifndef _CIPHER_TEST_H
00002 #define _CIPHER_TEST_H
00003 
00004 /** @file
00005  *
00006  * Cipher self-tests
00007  *
00008  */
00009 
00010 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00011 
00012 #include <stdint.h>
00013 #include <ipxe/crypto.h>
00014 #include <ipxe/test.h>
00015 
00016 /** A cipher test */
00017 struct cipher_test {
00018         /** Cipher algorithm */
00019         struct cipher_algorithm *cipher;
00020         /** Key */
00021         const void *key;
00022         /** Length of key */
00023         size_t key_len;
00024         /** Initialisation vector */
00025         const void *iv;
00026         /** Length of initialisation vector */
00027         size_t iv_len;
00028         /** Plaintext */
00029         const void *plaintext;
00030         /** Ciphertext */
00031         const void *ciphertext;
00032         /** Length of text */
00033         size_t len;
00034 };
00035 
00036 /** Define inline key */
00037 #define KEY(...) { __VA_ARGS__ }
00038 
00039 /** Define inline initialisation vector */
00040 #define IV(...) { __VA_ARGS__ }
00041 
00042 /** Define inline plaintext data */
00043 #define PLAINTEXT(...) { __VA_ARGS__ }
00044 
00045 /** Define inline ciphertext data */
00046 #define CIPHERTEXT(...) { __VA_ARGS__ }
00047 
00048 /**
00049  * Define a cipher test
00050  *
00051  * @v name              Test name
00052  * @v CIPHER            Cipher algorithm
00053  * @v KEY               Key
00054  * @v IV                Initialisation vector
00055  * @v PLAINTEXT         Plaintext
00056  * @v CIPHERTEXT        Ciphertext
00057  * @ret test            Cipher test
00058  */
00059 #define CIPHER_TEST( name, CIPHER, KEY, IV, PLAINTEXT, CIPHERTEXT )     \
00060         static const uint8_t name ## _key [] = KEY;                     \
00061         static const uint8_t name ## _iv [] = IV;                       \
00062         static const uint8_t name ## _plaintext [] = PLAINTEXT;         \
00063         static const uint8_t name ## _ciphertext                        \
00064                 [ sizeof ( name ## _plaintext ) ] = CIPHERTEXT;         \
00065         static struct cipher_test name = {                              \
00066                 .cipher = CIPHER,                                       \
00067                 .key = name ## _key,                                    \
00068                 .key_len = sizeof ( name ## _key ),                     \
00069                 .iv = name ## _iv,                                      \
00070                 .iv_len = sizeof ( name ## _iv ),                       \
00071                 .plaintext = name ## _plaintext,                        \
00072                 .ciphertext = name ## _ciphertext,                      \
00073                 .len = sizeof ( name ## _plaintext ),                   \
00074         }
00075 
00076 extern void cipher_encrypt_okx ( struct cipher_test *test, const char *file,
00077                                  unsigned int line );
00078 extern void cipher_decrypt_okx ( struct cipher_test *test, const char *file,
00079                                  unsigned int line );
00080 extern void cipher_okx ( struct cipher_test *test, const char *file,
00081                          unsigned int line );
00082 extern unsigned long cipher_cost_encrypt ( struct cipher_algorithm *cipher,
00083                                            size_t key_len );
00084 extern unsigned long cipher_cost_decrypt ( struct cipher_algorithm *cipher,
00085                                            size_t key_len );
00086 
00087 /**
00088  * Report a cipher encryption test result
00089  *
00090  * @v test              Cipher test
00091  */
00092 #define cipher_encrypt_ok( test ) \
00093         cipher_encrypt_okx ( test, __FILE__, __LINE__ )
00094 
00095 /**
00096  * Report a cipher decryption test result
00097  *
00098  * @v test              Cipher test
00099  */
00100 #define cipher_decrypt_ok( test ) \
00101         cipher_decrypt_okx ( test, __FILE__, __LINE__ )
00102 
00103 /**
00104  * Report a cipher encryption and decryption test result
00105  *
00106  * @v test              Cipher test
00107  */
00108 #define cipher_ok( test ) \
00109         cipher_okx ( test, __FILE__, __LINE__ )
00110 
00111 #endif /* _CIPHER_TEST_H */