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