You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

digest_test.h 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #ifndef _DIGEST_TEST_H
  2. #define _DIGEST_TEST_H
  3. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  4. #include <stdint.h>
  5. #include <ipxe/crypto.h>
  6. #include <ipxe/test.h>
  7. /** A digest test */
  8. struct digest_test {
  9. /** Digest algorithm */
  10. struct digest_algorithm *digest;
  11. /** Test data */
  12. const void *data;
  13. /** Length of test data */
  14. size_t len;
  15. /** Expected digest value */
  16. const void *expected;
  17. /** Expected digest length */
  18. size_t expected_len;
  19. };
  20. /** Define inline test data */
  21. #define DATA(...) { __VA_ARGS__ }
  22. /** Define inline expected digest value */
  23. #define DIGEST(...) { __VA_ARGS__ }
  24. /**
  25. * Define a digest test
  26. *
  27. * @v name Test name
  28. * @v DIGEST Digest algorithm
  29. * @v DATA Test data
  30. * @v EXPECTED Expected digest value
  31. * @ret test Digest test
  32. */
  33. #define DIGEST_TEST( name, DIGEST, DATA, EXPECTED ) \
  34. static const uint8_t name ## _data[] = DATA; \
  35. static const uint8_t name ## _expected[] = EXPECTED; \
  36. static struct digest_test name = { \
  37. .digest = DIGEST, \
  38. .data = name ## _data, \
  39. .len = sizeof ( name ## _data ), \
  40. .expected = name ## _expected, \
  41. .expected_len = sizeof ( name ## _expected ), \
  42. };
  43. /** Standard test vector: empty data */
  44. #define DIGEST_EMPTY DATA()
  45. /** Standard test vector: NIST string "abc"
  46. *
  47. * The NIST Cryptographic Toolkit examples for all digest algorithms
  48. * include a test vector which is the unterminated string
  49. *
  50. * "abc"
  51. */
  52. #define DIGEST_NIST_ABC \
  53. DATA ( 0x61, 0x62, 0x63 )
  54. /** Standard test vector: NIST string "abc...opq"
  55. *
  56. * The NIST Cryptographic Toolkit examples for all 32-bit digest
  57. * algorithms (SHA-1 and the SHA-256 family) include a test vector
  58. * which is the unterminated string
  59. *
  60. * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
  61. */
  62. #define DIGEST_NIST_ABC_OPQ \
  63. DATA ( 0x61, 0x62, 0x63, 0x64, 0x62, 0x63, 0x64, 0x65, 0x63, \
  64. 0x64, 0x65, 0x66, 0x64, 0x65, 0x66, 0x67, 0x65, 0x66, \
  65. 0x67, 0x68, 0x66, 0x67, 0x68, 0x69, 0x67, 0x68, 0x69, \
  66. 0x6a, 0x68, 0x69, 0x6a, 0x6b, 0x69, 0x6a, 0x6b, 0x6c, \
  67. 0x6a, 0x6b, 0x6c, 0x6d, 0x6b, 0x6c, 0x6d, 0x6e, 0x6c, \
  68. 0x6d, 0x6e, 0x6f, 0x6d, 0x6e, 0x6f, 0x70, 0x6e, 0x6f, \
  69. 0x70, 0x71 )
  70. /** Standard test vector: NIST string "abc...stu"
  71. *
  72. * The NIST Cryptographic Toolkit examples for all 64-bit digest
  73. * algorithms (SHA-512 family) include a test vector which is the
  74. * unterminated string
  75. *
  76. * "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
  77. * "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
  78. */
  79. #define DIGEST_NIST_ABC_STU \
  80. DATA ( 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x62, \
  81. 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x63, 0x64, \
  82. 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x64, 0x65, 0x66, \
  83. 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x65, 0x66, 0x67, 0x68, \
  84. 0x69, 0x6a, 0x6b, 0x6c, 0x66, 0x67, 0x68, 0x69, 0x6a, \
  85. 0x6b, 0x6c, 0x6d, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, \
  86. 0x6d, 0x6e, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, \
  87. 0x6f, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, \
  88. 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x6b, \
  89. 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x6c, 0x6d, \
  90. 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x6d, 0x6e, 0x6f, \
  91. 0x70, 0x71, 0x72, 0x73, 0x74, 0x6e, 0x6f, 0x70, 0x71, \
  92. 0x72, 0x73, 0x74, 0x75 )
  93. /**
  94. * Report a digest test result
  95. *
  96. * @v test Digest test
  97. */
  98. #define digest_ok(test) digest_okx ( test, __FILE__, __LINE__ )
  99. extern void digest_okx ( struct digest_test *test, const char *file,
  100. unsigned int line );
  101. extern unsigned long digest_cost ( struct digest_algorithm *digest );
  102. #endif /* _DIGEST_TEST_H */