test_sodium.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "unity.h"
  2. #include "sodium/crypto_hash_sha256.h"
  3. #include "sodium/crypto_hash_sha512.h"
  4. /* Note: a lot of these libsodium test programs assert() things, but they're not complete unit tests - most expect
  5. output to be compared to the matching .exp file.
  6. We don't do this automatically yet, maybe once we have more options for
  7. internal filesystem storage.
  8. */
  9. extern int aead_chacha20poly1305_xmain(void);
  10. TEST_CASE("aead_chacha20poly1305 test vectors", "[libsodium]")
  11. {
  12. printf("Running aead_chacha20poly1305\n");
  13. TEST_ASSERT_EQUAL(0, aead_chacha20poly1305_xmain());
  14. }
  15. extern int chacha20_xmain(void);
  16. TEST_CASE("chacha20 test vectors", "[libsodium]")
  17. {
  18. printf("Running chacha20\n");
  19. TEST_ASSERT_EQUAL(0, chacha20_xmain());
  20. }
  21. extern int box_xmain(void);
  22. extern int box2_xmain(void);
  23. TEST_CASE("box tests", "[libsodium]")
  24. {
  25. printf("Running box\n");
  26. TEST_ASSERT_EQUAL(0, box_xmain());
  27. printf("Running box2\n");
  28. TEST_ASSERT_EQUAL(0, box2_xmain());
  29. }
  30. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2)
  31. extern int ed25519_convert_xmain(void);
  32. TEST_CASE("ed25519_convert tests", "[libsodium][timeout=60]")
  33. {
  34. printf("Running ed25519_convert\n");
  35. TEST_ASSERT_EQUAL(0, ed25519_convert_xmain() );
  36. }
  37. #endif
  38. extern int sign_xmain(void);
  39. TEST_CASE("sign tests", "[libsodium]")
  40. {
  41. printf("Running sign\n");
  42. TEST_ASSERT_EQUAL(0, sign_xmain() );
  43. }
  44. extern int hash_xmain(void);
  45. TEST_CASE("hash tests", "[libsodium]")
  46. {
  47. printf("Running hash\n");
  48. TEST_ASSERT_EQUAL(0, hash_xmain() );
  49. }
  50. TEST_CASE("sha256 sanity check", "[libsodium]")
  51. {
  52. const uint8_t expected[] = { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41,
  53. 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03,
  54. 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff,
  55. 0x61, 0xf2, 0x00, 0x15, 0xad, };
  56. uint8_t calculated[32];
  57. crypto_hash_sha256_state state;
  58. const uint8_t *in = (const uint8_t *)"abc";
  59. const size_t inlen = 3;
  60. // One-liner version
  61. crypto_hash_sha256(calculated, in, inlen);
  62. TEST_ASSERT_EQUAL(sizeof(calculated), sizeof(expected));
  63. TEST_ASSERT_EQUAL(sizeof(calculated), crypto_hash_sha256_bytes());
  64. TEST_ASSERT_EQUAL_MEMORY(expected, calculated, crypto_hash_sha256_bytes());
  65. // Multi-line version
  66. crypto_hash_sha256_init(&state);
  67. crypto_hash_sha256_update(&state, in, inlen - 1); // split into two updates
  68. crypto_hash_sha256_update(&state, in + (inlen -1), 1);
  69. crypto_hash_sha256_final(&state, calculated);
  70. TEST_ASSERT_EQUAL_MEMORY(expected, calculated, crypto_hash_sha256_bytes());
  71. }
  72. TEST_CASE("sha512 sanity check", "[libsodium]")
  73. {
  74. const uint8_t expected[] = { 0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba, 0xcc,
  75. 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31, 0x12, 0xe6,
  76. 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2, 0x0a, 0x9e, 0xee,
  77. 0xe6, 0x4b, 0x55, 0xd3, 0x9a, 0x21, 0x92, 0x99, 0x2a,
  78. 0x27, 0x4f, 0xc1, 0xa8, 0x36, 0xba, 0x3c, 0x23, 0xa3,
  79. 0xfe, 0xeb, 0xbd, 0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c,
  80. 0xe8, 0x0e, 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4,
  81. 0x9f };
  82. uint8_t calculated[64];
  83. crypto_hash_sha512_state state;
  84. const uint8_t *in = (const uint8_t *)"abc";
  85. const size_t inlen = 3;
  86. // One-liner version
  87. crypto_hash_sha512(calculated, in, inlen);
  88. TEST_ASSERT_EQUAL(sizeof(calculated), sizeof(expected));
  89. TEST_ASSERT_EQUAL(sizeof(calculated), crypto_hash_sha512_bytes());
  90. TEST_ASSERT_EQUAL_MEMORY(expected, calculated, crypto_hash_sha512_bytes());
  91. // Multi-line version
  92. crypto_hash_sha512_init(&state);
  93. crypto_hash_sha512_update(&state, in, inlen - 1); // split into two updates
  94. crypto_hash_sha512_update(&state, in + (inlen -1), 1);
  95. crypto_hash_sha512_final(&state, calculated);
  96. TEST_ASSERT_EQUAL_MEMORY(expected, calculated, crypto_hash_sha512_bytes());
  97. }