iPXE
pubkey_test.h
Go to the documentation of this file.
00001 #ifndef _PUBKEY_TEST_H
00002 #define _PUBKEY_TEST_H
00003 
00004 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00005 
00006 #include <stdint.h>
00007 #include <ipxe/crypto.h>
00008 #include <ipxe/test.h>
00009 
00010 /**
00011  * Report public key decryption test result
00012  *
00013  * @v pubkey            Public key algorithm
00014  * @v key               Key
00015  * @v key_len           Key length
00016  * @v ciphertext        Ciphertext
00017  * @v ciphertext_len    Ciphertext length
00018  * @v expected          Expected plaintext
00019  * @v expected_len      Expected plaintext length
00020  */
00021 #define pubkey_decrypt_ok( pubkey, key, key_len, ciphertext,            \
00022                            ciphertext_len, expected, expected_len ) do {\
00023         uint8_t ctx[ (pubkey)->ctxsize ];                               \
00024                                                                         \
00025         ok ( pubkey_init ( (pubkey), ctx, (key), (key_len) ) == 0 );    \
00026         {                                                               \
00027                 size_t max_len = pubkey_max_len ( (pubkey), ctx );      \
00028                 uint8_t decrypted[ max_len ];                           \
00029                 int decrypted_len;                                      \
00030                                                                         \
00031                 decrypted_len = pubkey_decrypt ( (pubkey), ctx,         \
00032                                                  (ciphertext),          \
00033                                                  (ciphertext_len),      \
00034                                                  decrypted );           \
00035                 ok ( decrypted_len == ( ( int ) (expected_len) ) );     \
00036                 ok ( memcmp ( decrypted, (expected),                    \
00037                               (expected_len) ) == 0 );                  \
00038         }                                                               \
00039         pubkey_final ( (pubkey), ctx );                                 \
00040         } while ( 0 )
00041 
00042 /**
00043  * Report public key encryption and decryption test result
00044  *
00045  * @v pubkey            Public key algorithm
00046  * @v encrypt_key       Encryption key
00047  * @v encrypt_key_len   Encryption key length
00048  * @v decrypt_key       Decryption key
00049  * @v decrypt_key_len   Decryption key length
00050  * @v plaintext         Plaintext
00051  * @v plaintext_len     Plaintext length
00052  */
00053 #define pubkey_encrypt_ok( pubkey, encrypt_key, encrypt_key_len,        \
00054                            decrypt_key, decrypt_key_len, plaintext,     \
00055                            plaintext_len ) do {                         \
00056         uint8_t ctx[ (pubkey)->ctxsize ];                               \
00057                                                                         \
00058         ok ( pubkey_init ( (pubkey), ctx, (encrypt_key),                \
00059                            (encrypt_key_len) ) == 0 );                  \
00060         {                                                               \
00061                 size_t max_len = pubkey_max_len ( (pubkey), ctx );      \
00062                 uint8_t encrypted[ max_len ];                           \
00063                 int encrypted_len;                                      \
00064                                                                         \
00065                 encrypted_len = pubkey_encrypt ( (pubkey), ctx,         \
00066                                                  (plaintext),           \
00067                                                  (plaintext_len),       \
00068                                                  encrypted );           \
00069                 ok ( encrypted_len >= 0 );                              \
00070                 pubkey_decrypt_ok ( (pubkey), (decrypt_key),            \
00071                                     (decrypt_key_len), encrypted,       \
00072                                     encrypted_len, (plaintext),         \
00073                                     (plaintext_len) );                  \
00074         }                                                               \
00075         pubkey_final ( (pubkey), ctx );                                 \
00076         } while ( 0 )
00077 
00078 /**
00079  * Report public key signature test result
00080  *
00081  * @v pubkey            Public key algorithm
00082  * @v key               Key
00083  * @v key_len           Key length
00084  * @v digest            Digest algorithm
00085  * @v plaintext         Plaintext
00086  * @v plaintext_len     Plaintext length
00087  * @v expected          Expected signature
00088  * @v expected_len      Expected signature length
00089  */
00090 #define pubkey_sign_ok( pubkey, key, key_len, digest, plaintext,        \
00091                         plaintext_len, expected, expected_len ) do {    \
00092         uint8_t ctx[ (pubkey)->ctxsize ];                               \
00093         uint8_t digestctx[ (digest)->ctxsize ];                         \
00094         uint8_t digestout[ (digest)->digestsize ];                      \
00095                                                                         \
00096         digest_init ( (digest), digestctx );                            \
00097         digest_update ( (digest), digestctx, (plaintext),               \
00098                         (plaintext_len) );                              \
00099         digest_final ( (digest), digestctx, digestout );                \
00100                                                                         \
00101         ok ( pubkey_init ( (pubkey), ctx, (key), (key_len) ) == 0 );    \
00102         {                                                               \
00103                 size_t max_len = pubkey_max_len ( (pubkey), ctx );      \
00104                 uint8_t signature[ max_len ];                           \
00105                 int signature_len;                                      \
00106                                                                         \
00107                 signature_len = pubkey_sign ( (pubkey), ctx, (digest),  \
00108                                               digestout, signature );   \
00109                 ok ( signature_len == ( ( int ) (expected_len) ) );     \
00110                 ok ( memcmp ( signature, (expected),                    \
00111                               (expected_len) ) == 0 );                  \
00112         }                                                               \
00113         pubkey_final ( (pubkey), ctx );                                 \
00114         } while ( 0 )
00115 
00116 /**
00117  * Report public key verification test result
00118  *
00119  * @v pubkey            Public key algorithm
00120  * @v key               Key
00121  * @v key_len           Key length
00122  * @v digest            Digest algorithm
00123  * @v plaintext         Plaintext
00124  * @v plaintext_len     Plaintext length
00125  * @v signature         Signature
00126  * @v signature_len     Signature length
00127  */
00128 #define pubkey_verify_ok( pubkey, key, key_len, digest, plaintext,      \
00129                           plaintext_len, signature, signature_len ) do {\
00130         uint8_t ctx[ (pubkey)->ctxsize ];                               \
00131         uint8_t digestctx[ (digest)->ctxsize ];                         \
00132         uint8_t digestout[ (digest)->digestsize ];                      \
00133                                                                         \
00134         digest_init ( (digest), digestctx );                            \
00135         digest_update ( (digest), digestctx, (plaintext),               \
00136                         (plaintext_len) );                              \
00137         digest_final ( (digest), digestctx, digestout );                \
00138                                                                         \
00139         ok ( pubkey_init ( (pubkey), ctx, (key), (key_len) ) == 0 );    \
00140         ok ( pubkey_verify ( (pubkey), ctx, (digest), digestout,        \
00141                              (signature), (signature_len) ) == 0 );     \
00142         pubkey_final ( (pubkey), ctx );                                 \
00143         } while ( 0 )
00144 
00145 /**
00146  * Report public key verification test result
00147  *
00148  * @v pubkey            Public key algorithm
00149  * @v key               Key
00150  * @v key_len           Key length
00151  * @v digest            Digest algorithm
00152  * @v plaintext         Plaintext
00153  * @v plaintext_len     Plaintext length
00154  * @v signature         Signature
00155  * @v signature_len     Signature length
00156  */
00157 #define pubkey_verify_fail_ok( pubkey, key, key_len, digest, plaintext, \
00158                                plaintext_len, signature,                \
00159                                signature_len ) do {                     \
00160         uint8_t ctx[ (pubkey)->ctxsize ];                               \
00161         uint8_t digestctx[ (digest)->ctxsize ];                         \
00162         uint8_t digestout[ (digest)->digestsize ];                      \
00163                                                                         \
00164         digest_init ( (digest), digestctx );                            \
00165         digest_update ( (digest), digestctx, (plaintext),               \
00166                         (plaintext_len) );                              \
00167         digest_final ( (digest), digestctx, digestout );                \
00168                                                                         \
00169         ok ( pubkey_init ( (pubkey), ctx, (key), (key_len) ) == 0 );    \
00170         ok ( pubkey_verify ( (pubkey), ctx, (digest), digestout,        \
00171                              (signature), (signature_len) ) != 0 );     \
00172         pubkey_final ( (pubkey), ctx );                                 \
00173         } while ( 0 )
00174 
00175 #endif /* _PUBKEY_TEST_H */