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