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.

cipher_test.h 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #ifndef _CIPHER_TEST_H
  2. #define _CIPHER_TEST_H
  3. /** @file
  4. *
  5. * Cipher self-tests
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <stdint.h>
  10. #include <ipxe/crypto.h>
  11. #include <ipxe/test.h>
  12. /** A cipher test */
  13. struct cipher_test {
  14. /** Cipher algorithm */
  15. struct cipher_algorithm *cipher;
  16. /** Key */
  17. const void *key;
  18. /** Length of key */
  19. size_t key_len;
  20. /** Initialisation vector */
  21. const void *iv;
  22. /** Length of initialisation vector */
  23. size_t iv_len;
  24. /** Plaintext */
  25. const void *plaintext;
  26. /** Ciphertext */
  27. const void *ciphertext;
  28. /** Length of text */
  29. size_t len;
  30. };
  31. /** Define inline key */
  32. #define KEY(...) { __VA_ARGS__ }
  33. /** Define inline initialisation vector */
  34. #define IV(...) { __VA_ARGS__ }
  35. /** Define inline plaintext data */
  36. #define PLAINTEXT(...) { __VA_ARGS__ }
  37. /** Define inline ciphertext data */
  38. #define CIPHERTEXT(...) { __VA_ARGS__ }
  39. /**
  40. * Define a cipher test
  41. *
  42. * @v name Test name
  43. * @v CIPHER Cipher algorithm
  44. * @v KEY Key
  45. * @v IV Initialisation vector
  46. * @v PLAINTEXT Plaintext
  47. * @v CIPHERTEXT Ciphertext
  48. * @ret test Cipher test
  49. */
  50. #define CIPHER_TEST( name, CIPHER, KEY, IV, PLAINTEXT, CIPHERTEXT ) \
  51. static const uint8_t name ## _key [] = KEY; \
  52. static const uint8_t name ## _iv [] = IV; \
  53. static const uint8_t name ## _plaintext [] = PLAINTEXT; \
  54. static const uint8_t name ## _ciphertext \
  55. [ sizeof ( name ## _plaintext ) ] = CIPHERTEXT; \
  56. static struct cipher_test name = { \
  57. .cipher = CIPHER, \
  58. .key = name ## _key, \
  59. .key_len = sizeof ( name ## _key ), \
  60. .iv = name ## _iv, \
  61. .iv_len = sizeof ( name ## _iv ), \
  62. .plaintext = name ## _plaintext, \
  63. .ciphertext = name ## _ciphertext, \
  64. .len = sizeof ( name ## _plaintext ), \
  65. }
  66. extern void cipher_encrypt_okx ( struct cipher_test *test, const char *file,
  67. unsigned int line );
  68. extern void cipher_decrypt_okx ( struct cipher_test *test, const char *file,
  69. unsigned int line );
  70. extern void cipher_okx ( struct cipher_test *test, const char *file,
  71. unsigned int line );
  72. extern unsigned long cipher_cost_encrypt ( struct cipher_algorithm *cipher,
  73. size_t key_len );
  74. extern unsigned long cipher_cost_decrypt ( struct cipher_algorithm *cipher,
  75. size_t key_len );
  76. /**
  77. * Report a cipher encryption test result
  78. *
  79. * @v test Cipher test
  80. */
  81. #define cipher_encrypt_ok( test ) \
  82. cipher_encrypt_okx ( test, __FILE__, __LINE__ )
  83. /**
  84. * Report a cipher decryption test result
  85. *
  86. * @v test Cipher test
  87. */
  88. #define cipher_decrypt_ok( test ) \
  89. cipher_decrypt_okx ( test, __FILE__, __LINE__ )
  90. /**
  91. * Report a cipher encryption and decryption test result
  92. *
  93. * @v test Cipher test
  94. */
  95. #define cipher_ok( test ) \
  96. cipher_okx ( test, __FILE__, __LINE__ )
  97. #endif /* _CIPHER_TEST_H */