iPXE
digest_test.h
Go to the documentation of this file.
00001 #ifndef _DIGEST_TEST_H
00002 #define _DIGEST_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 /** A digest test */
00011 struct digest_test {
00012         /** Digest algorithm */
00013         struct digest_algorithm *digest;
00014         /** Test data */
00015         const void *data;
00016         /** Length of test data */
00017         size_t len;
00018         /** Expected digest value */
00019         const void *expected;
00020         /** Expected digest length */
00021         size_t expected_len;
00022 };
00023 
00024 /** Define inline test data */
00025 #define DATA(...) { __VA_ARGS__ }
00026 
00027 /** Define inline expected digest value */
00028 #define DIGEST(...) { __VA_ARGS__ }
00029 
00030 /**
00031  * Define a digest test
00032  *
00033  * @v name              Test name
00034  * @v DIGEST            Digest algorithm
00035  * @v DATA              Test data
00036  * @v EXPECTED          Expected digest value
00037  * @ret test            Digest test
00038  */
00039 #define DIGEST_TEST( name, DIGEST, DATA, EXPECTED )                     \
00040         static const uint8_t name ## _data[] = DATA;                    \
00041         static const uint8_t name ## _expected[] = EXPECTED;            \
00042         static struct digest_test name = {                              \
00043                 .digest = DIGEST,                                       \
00044                 .data = name ## _data,                                  \
00045                 .len = sizeof ( name ## _data ),                        \
00046                 .expected = name ## _expected,                          \
00047                 .expected_len = sizeof ( name ## _expected ),           \
00048         };
00049 
00050 /** Standard test vector: empty data */
00051 #define DIGEST_EMPTY DATA()
00052 
00053 /** Standard test vector: NIST string "abc"
00054  *
00055  * The NIST Cryptographic Toolkit examples for all digest algorithms
00056  * include a test vector which is the unterminated string
00057  *
00058  *   "abc"
00059  */
00060 #define DIGEST_NIST_ABC                                                 \
00061         DATA ( 0x61, 0x62, 0x63 )
00062 
00063 /** Standard test vector: NIST string "abc...opq"
00064  *
00065  * The NIST Cryptographic Toolkit examples for all 32-bit digest
00066  * algorithms (SHA-1 and the SHA-256 family) include a test vector
00067  * which is the unterminated string
00068  *
00069  *   "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
00070  */
00071 #define DIGEST_NIST_ABC_OPQ                                             \
00072         DATA ( 0x61, 0x62, 0x63, 0x64, 0x62, 0x63, 0x64, 0x65, 0x63,    \
00073                0x64, 0x65, 0x66, 0x64, 0x65, 0x66, 0x67, 0x65, 0x66,    \
00074                0x67, 0x68, 0x66, 0x67, 0x68, 0x69, 0x67, 0x68, 0x69,    \
00075                0x6a, 0x68, 0x69, 0x6a, 0x6b, 0x69, 0x6a, 0x6b, 0x6c,    \
00076                0x6a, 0x6b, 0x6c, 0x6d, 0x6b, 0x6c, 0x6d, 0x6e, 0x6c,    \
00077                0x6d, 0x6e, 0x6f, 0x6d, 0x6e, 0x6f, 0x70, 0x6e, 0x6f,    \
00078                0x70, 0x71 )
00079 
00080 /** Standard test vector: NIST string "abc...stu"
00081  *
00082  * The NIST Cryptographic Toolkit examples for all 64-bit digest
00083  * algorithms (SHA-512 family) include a test vector which is the
00084  * unterminated string
00085  *
00086  *   "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
00087  *   "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
00088  */
00089 #define DIGEST_NIST_ABC_STU                                             \
00090         DATA ( 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x62,    \
00091                0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x63, 0x64,    \
00092                0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x64, 0x65, 0x66,    \
00093                0x67, 0x68, 0x69, 0x6a, 0x6b, 0x65, 0x66, 0x67, 0x68,    \
00094                0x69, 0x6a, 0x6b, 0x6c, 0x66, 0x67, 0x68, 0x69, 0x6a,    \
00095                0x6b, 0x6c, 0x6d, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,    \
00096                0x6d, 0x6e, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,    \
00097                0x6f, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,    \
00098                0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x6b,    \
00099                0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x6c, 0x6d,    \
00100                0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x6d, 0x6e, 0x6f,    \
00101                0x70, 0x71, 0x72, 0x73, 0x74, 0x6e, 0x6f, 0x70, 0x71,    \
00102                0x72, 0x73, 0x74, 0x75 )
00103 
00104 /**
00105  * Report a digest test result
00106  *
00107  * @v test              Digest test
00108  */
00109 #define digest_ok(test) digest_okx ( test, __FILE__, __LINE__ )
00110 
00111 extern void digest_okx ( struct digest_test *test, const char *file,
00112                          unsigned int line );
00113 extern unsigned long digest_cost ( struct digest_algorithm *digest );
00114 
00115 #endif /* _DIGEST_TEST_H */