#include "tls_p256.h" #include "log.h" #include "util.h" #include #include extern void initialise_monitor_handles(void); static int s_num_tests; static bool s_error; #ifdef NO_ABORT static int s_abort; #define ABORT() ++s_abort, s_error = true #else #ifdef NO_SLEEP_ABORT #define ABORT() abort() #else #define ABORT() \ sleep(2); /* 2s, GH print reason */ \ abort(); #endif #endif static const uint8_t s_prv[32] = { 0xc9, 0xaf, 0xa9, 0xd8, 0x45, 0xba, 0x75, 0x16, 0x6b, 0x5c, 0x21, 0x57, 0x67, 0xb1, 0xd6, 0x93, 0x4e, 0x50, 0xc3, 0xdb, 0x36, 0xe8, 0x9b, 0x12, 0x7b, 0x8a, 0x62, 0x2b, 0x12, 0x0f, 0x67, 0x21}; static const uint8_t s_pub[64] = { 0x60, 0xfe, 0xd4, 0xba, 0x25, 0x5a, 0x9d, 0x31, 0xc9, 0x61, 0xeb, 0x74, 0xc6, 0x35, 0x6d, 0x68, 0xc0, 0x49, 0xb8, 0x92, 0x3b, 0x61, 0xfa, 0x6c, 0xe6, 0x69, 0x62, 0x2e, 0x60, 0xf2, 0x9f, 0xb6, 0x79, 0x03, 0xfe, 0x10, 0x08, 0xb8, 0xbc, 0x99, 0xa4, 0x1a, 0xe9, 0xe9, 0x56, 0x28, 0xbc, 0x64, 0xf2, 0xf1, 0xb2, 0x0c, 0x2d, 0x7e, 0x9f, 0x51, 0x77, 0xa3, 0xc2, 0x94, 0xd4, 0x46, 0x22, 0x99}; static const uint8_t s_hash[32] = { 0xaf, 0x2b, 0xdb, 0xe1, 0xaa, 0x9b, 0x6e, 0xc1, 0xe2, 0xad, 0xe1, 0xd6, 0x94, 0xf4, 0x1f, 0xc7, 0x1a, 0x83, 0x1d, 0x02, 0x68, 0xe9, 0x89, 0x15, 0x62, 0x11, 0x3d, 0x8a, 0x62, 0xad, 0xd1, 0xbf}; static const uint8_t s_sig[64] = { 0xef, 0xd4, 0x8b, 0x2a, 0xac, 0xb6, 0xa8, 0xfd, 0x11, 0x40, 0xdd, 0x9c, 0xd4, 0x5e, 0x81, 0xd6, 0x9d, 0x2c, 0x87, 0x7b, 0x56, 0xaa, 0xf9, 0x91, 0xc3, 0x4d, 0x0e, 0xa8, 0x4e, 0xaf, 0x37, 0x16, 0xf7, 0xcb, 0x1c, 0x94, 0x2d, 0x65, 0x7c, 0x41, 0xd4, 0x36, 0xc7, 0xa1, 0xb6, 0xe2, 0x9f, 0x65, 0xf3, 0xe9, 0x00, 0xdb, 0xb9, 0xaf, 0xf4, 0x06, 0x4d, 0xc4, 0xab, 0x2f, 0x84, 0x3a, 0xcd, 0xa8}; bool mg_random(void *buf, size_t len) { if (len != sizeof(s_prv)) return false; memcpy(buf, s_prv, len); return true; } #define ASSERT(expr) \ do { \ s_num_tests++; \ if (!(expr)) { \ printf("FAILURE %s:%d: %s\n", __FILE__, __LINE__, #expr); \ fflush(stdout); \ ABORT(); \ } \ } while (0) static void print_char(char c, void *param) { (void) param; putchar(c); } static void test_p256(void) { uint8_t pub[64], sig[64], prv2[32], pub2[64], sec1[32], sec2[32]; ASSERT(mg_p256_compute_public_key(s_prv, pub) == 1); ASSERT(memcmp(pub, s_pub, sizeof(pub)) == 0); ASSERT(mg_p256_sign(s_prv, s_hash, sizeof(s_hash), sig) == 1); ASSERT(memcmp(sig, s_sig, sizeof(sig)) == 0); ASSERT(mg_p256_verify(s_pub, s_hash, sizeof(s_hash), s_sig) == 1); sig[10] ^= 1; ASSERT(mg_p256_verify(s_pub, s_hash, sizeof(s_hash), sig) == 0); ASSERT(mg_p256_make_key(pub2, prv2) == 1); ASSERT(mg_p256_compute_public_key(prv2, pub) == 1); ASSERT(memcmp(pub, pub2, sizeof(pub)) == 0); ASSERT(mg_p256_shared_secret(pub2, s_prv, sec1) == 1); ASSERT(mg_p256_shared_secret(s_pub, prv2, sec2) == 1); ASSERT(memcmp(sec1, sec2, sizeof(sec1)) == 0); pub2[63] ^= 1; ASSERT(mg_p256_shared_secret(pub2, s_prv, sec1) == 0); } int main(void) { initialise_monitor_handles(); mg_log_set_fn(print_char, NULL); test_p256(); #ifdef NO_ABORT if (s_abort != 0) return EXIT_FAILURE; #endif if (s_error) return EXIT_FAILURE; printf("SUCCESS. Total tests: %d\n", s_num_tests); return EXIT_SUCCESS; }