1 /* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */
10 #include "/Projects/lpc11xx/peripherals/uart.h"
11 #include "/Projects/lpc11xx/peripherals/time.h"
13 static uint64_t g_rand = 88172645463325252ull;
14 int fake_rng(uint8_t *p_dest, unsigned p_size)
18 g_rand ^= (g_rand << 13);
19 g_rand ^= (g_rand >> 7);
20 g_rand ^= (g_rand << 17);
22 unsigned l_amount = (p_size > 8 ? 8 : p_size);
23 memcpy(p_dest, &g_rand, l_amount);
31 void vli_print(uint8_t *p_vli, unsigned int p_size)
35 printf("%02X ", (unsigned)p_vli[p_size - 1]);
43 uartInit(BAUD_115200);
46 uECC_set_rng(&fake_rng);
51 uint8_t l_private1[uECC_BYTES];
52 uint8_t l_private2[uECC_BYTES];
54 uint8_t l_public1[uECC_BYTES * 2];
55 uint8_t l_public2[uECC_BYTES * 2];
57 uint8_t l_secret1[uECC_BYTES];
58 uint8_t l_secret2[uECC_BYTES];
60 printf("Testing 256 random private key pairs\n");
69 if(!uECC_make_key(l_public1, l_private1) || !uECC_make_key(l_public2, l_private2))
71 printf("uECC_make_key() failed\n");
75 if(!uECC_shared_secret(l_public2, l_private1, l_secret1))
77 printf("shared_secret() failed (1)\n");
81 if(!uECC_shared_secret(l_public1, l_private2, l_secret2))
83 printf("shared_secret() failed (2)\n");
87 if(memcmp(l_secret1, l_secret2, sizeof(l_secret1)) != 0)
89 printf("Shared secrets are not identical!\n");
90 printf("Shared secret 1 = ");
91 vli_print(l_secret1, uECC_BYTES);
93 printf("Shared secret 2 = ");
94 vli_print(l_secret2, uECC_BYTES);
96 printf("Private key 1 = ");
97 vli_print(l_private1, uECC_BYTES);
99 printf("Private key 2 = ");
100 vli_print(l_private2, uECC_BYTES);