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 #ifndef uECC_TEST_NUMBER_OF_ITERATIONS
9*af03003cSMatthias Ringwald #define uECC_TEST_NUMBER_OF_ITERATIONS 256
10*af03003cSMatthias Ringwald #endif
11*af03003cSMatthias Ringwald
12*af03003cSMatthias Ringwald #if LPC11XX
13*af03003cSMatthias Ringwald
14*af03003cSMatthias Ringwald #include "/Projects/lpc11xx/peripherals/uart.h"
15*af03003cSMatthias Ringwald #include "/Projects/lpc11xx/peripherals/time.h"
16*af03003cSMatthias Ringwald
17*af03003cSMatthias Ringwald static uint64_t g_rand = 88172645463325252ull;
fake_rng(uint8_t * dest,unsigned size)18*af03003cSMatthias Ringwald int fake_rng(uint8_t *dest, unsigned size) {
19*af03003cSMatthias Ringwald while(size) {
20*af03003cSMatthias Ringwald g_rand ^= (g_rand << 13);
21*af03003cSMatthias Ringwald g_rand ^= (g_rand >> 7);
22*af03003cSMatthias Ringwald g_rand ^= (g_rand << 17);
23*af03003cSMatthias Ringwald
24*af03003cSMatthias Ringwald unsigned amount = (size > 8 ? 8 : size);
25*af03003cSMatthias Ringwald memcpy(dest, &g_rand, amount);
26*af03003cSMatthias Ringwald dest += amount;
27*af03003cSMatthias Ringwald size -= amount;
28*af03003cSMatthias Ringwald }
29*af03003cSMatthias Ringwald return 1;
30*af03003cSMatthias Ringwald }
31*af03003cSMatthias Ringwald
32*af03003cSMatthias Ringwald #endif
33*af03003cSMatthias Ringwald
vli_print(char * str,uint8_t * vli,unsigned int size)34*af03003cSMatthias Ringwald void vli_print(char *str, uint8_t *vli, unsigned int size) {
35*af03003cSMatthias Ringwald printf("%s ", str);
36*af03003cSMatthias Ringwald while (size) {
37*af03003cSMatthias Ringwald printf("%02X ", (unsigned)vli[size - 1]);
38*af03003cSMatthias Ringwald --size;
39*af03003cSMatthias Ringwald }
40*af03003cSMatthias Ringwald printf("\n");
41*af03003cSMatthias Ringwald }
42*af03003cSMatthias Ringwald
main()43*af03003cSMatthias Ringwald int main() {
44*af03003cSMatthias Ringwald #if LPC11XX
45*af03003cSMatthias Ringwald uartInit(BAUD_115200);
46*af03003cSMatthias Ringwald initTime();
47*af03003cSMatthias Ringwald
48*af03003cSMatthias Ringwald uECC_set_rng(&fake_rng);
49*af03003cSMatthias Ringwald #endif
50*af03003cSMatthias Ringwald
51*af03003cSMatthias Ringwald uint8_t public[uECC_BYTES * 2];
52*af03003cSMatthias Ringwald uint8_t private[uECC_BYTES];
53*af03003cSMatthias Ringwald uint8_t compressed_point[uECC_BYTES + 1];
54*af03003cSMatthias Ringwald uint8_t decompressed_point[uECC_BYTES * 2];
55*af03003cSMatthias Ringwald
56*af03003cSMatthias Ringwald int i;
57*af03003cSMatthias Ringwald printf("Testing compression and decompression of %d random EC points\n", uECC_TEST_NUMBER_OF_ITERATIONS);
58*af03003cSMatthias Ringwald
59*af03003cSMatthias Ringwald for (i = 0; i < uECC_TEST_NUMBER_OF_ITERATIONS; ++i) {
60*af03003cSMatthias Ringwald printf(".");
61*af03003cSMatthias Ringwald #if !LPC11XX
62*af03003cSMatthias Ringwald fflush(stdout);
63*af03003cSMatthias Ringwald #endif
64*af03003cSMatthias Ringwald
65*af03003cSMatthias Ringwald /* Generate arbitrary EC point (public) on Curve */
66*af03003cSMatthias Ringwald if (!uECC_make_key(public, private)) {
67*af03003cSMatthias Ringwald printf("uECC_make_key() failed\n");
68*af03003cSMatthias Ringwald continue;
69*af03003cSMatthias Ringwald }
70*af03003cSMatthias Ringwald
71*af03003cSMatthias Ringwald /* compress and decompress point */
72*af03003cSMatthias Ringwald uECC_compress(public, compressed_point);
73*af03003cSMatthias Ringwald uECC_decompress(compressed_point, decompressed_point);
74*af03003cSMatthias Ringwald
75*af03003cSMatthias Ringwald if (memcmp(public, decompressed_point, 2 * uECC_BYTES) != 0) {
76*af03003cSMatthias Ringwald printf("Original and decompressed points are not identical!\n");
77*af03003cSMatthias Ringwald vli_print("Original point = ", public, 2 * uECC_BYTES);
78*af03003cSMatthias Ringwald vli_print("Compressed point = ", compressed_point, uECC_BYTES + 1);
79*af03003cSMatthias Ringwald vli_print("Decompressed point = ", decompressed_point, 2 * uECC_BYTES);
80*af03003cSMatthias Ringwald }
81*af03003cSMatthias Ringwald }
82*af03003cSMatthias Ringwald printf("\n");
83*af03003cSMatthias Ringwald
84*af03003cSMatthias Ringwald return 0;
85*af03003cSMatthias Ringwald }
86