test_sodium.c 4.1 KB

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