1 /* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */ 2 3 #include "uECC.h" 4 5 #include <stdio.h> 6 #include <string.h> 7 8 #if LPC11XX 9 10 #include "/Projects/lpc11xx/peripherals/uart.h" 11 #include "/Projects/lpc11xx/peripherals/time.h" 12 13 static uint64_t g_rand = 88172645463325252ull; 14 int fake_rng(uint8_t *dest, unsigned size) { 15 while (size) { 16 g_rand ^= (g_rand << 13); 17 g_rand ^= (g_rand >> 7); 18 g_rand ^= (g_rand << 17); 19 20 unsigned amount = (size > 8 ? 8 : size); 21 memcpy(dest, &g_rand, amount); 22 dest += amount; 23 size -= amount; 24 } 25 return 1; 26 } 27 28 #endif 29 30 void vli_print(uint8_t *vli, unsigned int size) { 31 while (size) { 32 printf("%02X ", (unsigned)vli[size - 1]); 33 --size; 34 } 35 } 36 37 int main() { 38 #if LPC11XX 39 uartInit(BAUD_115200); 40 initTime(); 41 42 uECC_set_rng(&fake_rng); 43 #endif 44 45 int i; 46 uint8_t private1[uECC_BYTES]; 47 uint8_t private2[uECC_BYTES]; 48 uint8_t public1[uECC_BYTES * 2]; 49 uint8_t public2[uECC_BYTES * 2]; 50 uint8_t secret1[uECC_BYTES]; 51 uint8_t secret2[uECC_BYTES]; 52 53 printf("Testing 256 random private key pairs\n"); 54 55 for (i = 0; i < 256; ++i) { 56 printf("."); 57 #if !LPC11XX 58 fflush(stdout); 59 #endif 60 61 if (!uECC_make_key(public1, private1) || !uECC_make_key(public2, private2)) { 62 printf("uECC_make_key() failed\n"); 63 return 1; 64 } 65 66 if (!uECC_shared_secret(public2, private1, secret1)) { 67 printf("shared_secret() failed (1)\n"); 68 return 1; 69 } 70 71 if (!uECC_shared_secret(public1, private2, secret2)) { 72 printf("shared_secret() failed (2)\n"); 73 return 1; 74 } 75 76 if (memcmp(secret1, secret2, sizeof(secret1)) != 0) { 77 printf("Shared secrets are not identical!\n"); 78 printf("Shared secret 1 = "); 79 vli_print(secret1, uECC_BYTES); 80 printf("\n"); 81 printf("Shared secret 2 = "); 82 vli_print(secret2, uECC_BYTES); 83 printf("\n"); 84 printf("Private key 1 = "); 85 vli_print(private1, uECC_BYTES); 86 printf("\n"); 87 printf("Private key 2 = "); 88 vli_print(private2, uECC_BYTES); 89 printf("\n"); 90 } 91 } 92 printf("\n"); 93 94 return 0; 95 } 96