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.

pubkey_test.h 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #ifndef _PUBKEY_TEST_H
  2. #define _PUBKEY_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. /**
  8. * Report public key decryption test result
  9. *
  10. * @v pubkey Public key algorithm
  11. * @v key Key
  12. * @v key_len Key length
  13. * @v ciphertext Ciphertext
  14. * @v ciphertext_len Ciphertext length
  15. * @v expected Expected plaintext
  16. * @v expected_len Expected plaintext length
  17. */
  18. #define pubkey_decrypt_ok( pubkey, key, key_len, ciphertext, \
  19. ciphertext_len, expected, expected_len ) do {\
  20. uint8_t ctx[ (pubkey)->ctxsize ]; \
  21. \
  22. ok ( pubkey_init ( (pubkey), ctx, (key), (key_len) ) == 0 ); \
  23. { \
  24. size_t max_len = pubkey_max_len ( (pubkey), ctx ); \
  25. uint8_t decrypted[ max_len ]; \
  26. int decrypted_len; \
  27. \
  28. decrypted_len = pubkey_decrypt ( (pubkey), ctx, \
  29. (ciphertext), \
  30. (ciphertext_len), \
  31. decrypted ); \
  32. ok ( decrypted_len == ( ( int ) (expected_len) ) ); \
  33. ok ( memcmp ( decrypted, (expected), \
  34. (expected_len) ) == 0 ); \
  35. } \
  36. pubkey_final ( (pubkey), ctx ); \
  37. } while ( 0 )
  38. /**
  39. * Report public key encryption and decryption test result
  40. *
  41. * @v pubkey Public key algorithm
  42. * @v encrypt_key Encryption key
  43. * @v encrypt_key_len Encryption key length
  44. * @v decrypt_key Decryption key
  45. * @v decrypt_key_len Decryption key length
  46. * @v plaintext Plaintext
  47. * @v plaintext_len Plaintext length
  48. */
  49. #define pubkey_encrypt_ok( pubkey, encrypt_key, encrypt_key_len, \
  50. decrypt_key, decrypt_key_len, plaintext, \
  51. plaintext_len ) do { \
  52. uint8_t ctx[ (pubkey)->ctxsize ]; \
  53. \
  54. ok ( pubkey_init ( (pubkey), ctx, (encrypt_key), \
  55. (encrypt_key_len) ) == 0 ); \
  56. { \
  57. size_t max_len = pubkey_max_len ( (pubkey), ctx ); \
  58. uint8_t encrypted[ max_len ]; \
  59. int encrypted_len; \
  60. \
  61. encrypted_len = pubkey_encrypt ( (pubkey), ctx, \
  62. (plaintext), \
  63. (plaintext_len), \
  64. encrypted ); \
  65. ok ( encrypted_len >= 0 ); \
  66. pubkey_decrypt_ok ( (pubkey), (decrypt_key), \
  67. (decrypt_key_len), encrypted, \
  68. encrypted_len, (plaintext), \
  69. (plaintext_len) ); \
  70. } \
  71. pubkey_final ( (pubkey), ctx ); \
  72. } while ( 0 )
  73. /**
  74. * Report public key signature test result
  75. *
  76. * @v pubkey Public key algorithm
  77. * @v key Key
  78. * @v key_len Key length
  79. * @v digest Digest algorithm
  80. * @v plaintext Plaintext
  81. * @v plaintext_len Plaintext length
  82. * @v expected Expected signature
  83. * @v expected_len Expected signature length
  84. */
  85. #define pubkey_sign_ok( pubkey, key, key_len, digest, plaintext, \
  86. plaintext_len, expected, expected_len ) do { \
  87. uint8_t ctx[ (pubkey)->ctxsize ]; \
  88. uint8_t digestctx[ (digest)->ctxsize ]; \
  89. uint8_t digestout[ (digest)->digestsize ]; \
  90. \
  91. digest_init ( (digest), digestctx ); \
  92. digest_update ( (digest), digestctx, (plaintext), \
  93. (plaintext_len) ); \
  94. digest_final ( (digest), digestctx, digestout ); \
  95. \
  96. ok ( pubkey_init ( (pubkey), ctx, (key), (key_len) ) == 0 ); \
  97. { \
  98. size_t max_len = pubkey_max_len ( (pubkey), ctx ); \
  99. uint8_t signature[ max_len ]; \
  100. int signature_len; \
  101. \
  102. signature_len = pubkey_sign ( (pubkey), ctx, (digest), \
  103. digestout, signature ); \
  104. ok ( signature_len == ( ( int ) (expected_len) ) ); \
  105. ok ( memcmp ( signature, (expected), \
  106. (expected_len) ) == 0 ); \
  107. } \
  108. pubkey_final ( (pubkey), ctx ); \
  109. } while ( 0 )
  110. /**
  111. * Report public key verification test result
  112. *
  113. * @v pubkey Public key algorithm
  114. * @v key Key
  115. * @v key_len Key length
  116. * @v digest Digest algorithm
  117. * @v plaintext Plaintext
  118. * @v plaintext_len Plaintext length
  119. * @v signature Signature
  120. * @v signature_len Signature length
  121. */
  122. #define pubkey_verify_ok( pubkey, key, key_len, digest, plaintext, \
  123. plaintext_len, signature, signature_len ) do {\
  124. uint8_t ctx[ (pubkey)->ctxsize ]; \
  125. uint8_t digestctx[ (digest)->ctxsize ]; \
  126. uint8_t digestout[ (digest)->digestsize ]; \
  127. \
  128. digest_init ( (digest), digestctx ); \
  129. digest_update ( (digest), digestctx, (plaintext), \
  130. (plaintext_len) ); \
  131. digest_final ( (digest), digestctx, digestout ); \
  132. \
  133. ok ( pubkey_init ( (pubkey), ctx, (key), (key_len) ) == 0 ); \
  134. ok ( pubkey_verify ( (pubkey), ctx, (digest), digestout, \
  135. (signature), (signature_len) ) == 0 ); \
  136. pubkey_final ( (pubkey), ctx ); \
  137. } while ( 0 )
  138. /**
  139. * Report public key verification test result
  140. *
  141. * @v pubkey Public key algorithm
  142. * @v key Key
  143. * @v key_len Key length
  144. * @v digest Digest algorithm
  145. * @v plaintext Plaintext
  146. * @v plaintext_len Plaintext length
  147. * @v signature Signature
  148. * @v signature_len Signature length
  149. */
  150. #define pubkey_verify_fail_ok( pubkey, key, key_len, digest, plaintext, \
  151. plaintext_len, signature, \
  152. signature_len ) do { \
  153. uint8_t ctx[ (pubkey)->ctxsize ]; \
  154. uint8_t digestctx[ (digest)->ctxsize ]; \
  155. uint8_t digestout[ (digest)->digestsize ]; \
  156. \
  157. digest_init ( (digest), digestctx ); \
  158. digest_update ( (digest), digestctx, (plaintext), \
  159. (plaintext_len) ); \
  160. digest_final ( (digest), digestctx, digestout ); \
  161. \
  162. ok ( pubkey_init ( (pubkey), ctx, (key), (key_len) ) == 0 ); \
  163. ok ( pubkey_verify ( (pubkey), ctx, (digest), digestout, \
  164. (signature), (signature_len) ) != 0 ); \
  165. pubkey_final ( (pubkey), ctx ); \
  166. } while ( 0 )
  167. #endif /* _PUBKEY_TEST_H */