xref: /btstack/3rd-party/micro-ecc/test/test_ecdsa.c (revision af03003c8ac55cf0eea9563b597879b24aee256f)
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;
fake_rng(uint8_t * dest,unsigned size)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 
main()30 int main() {
31 #if LPC11XX
32     uartInit(BAUD_115200);
33 	initTime();
34 
35     uECC_set_rng(&fake_rng);
36 #endif
37 
38     uint8_t public[uECC_BYTES * 2];
39     uint8_t private[uECC_BYTES];
40     uint8_t hash[uECC_BYTES];
41     uint8_t sig[uECC_BYTES * 2];
42 
43     int i;
44     printf("Testing 256 signatures\n");
45     for (i = 0; i < 256; ++i) {
46         printf(".");
47     #if !LPC11XX
48         fflush(stdout);
49     #endif
50 
51         if (!uECC_make_key(public, private)) {
52             printf("uECC_make_key() failed\n");
53             continue;
54         }
55         memcpy(hash, public, uECC_BYTES);
56 
57         if (!uECC_sign(private, hash, sig)) {
58             printf("uECC_sign() failed\n");
59             continue;
60         }
61 
62         if (!uECC_verify(public, hash, sig)) {
63             printf("uECC_verify() failed\n");
64         }
65     }
66     printf("\n");
67 
68     return 0;
69 }
70