f3983bb4ecffb0a7679c79f09240f13e34079cd5
[platform/upstream/iotivity.git] / extlibs / tinydtls / ecc / test / test_ecdsa.c
1 /* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */
2
3 #include "../ecc.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 *p_dest, unsigned p_size)
15 {
16     while(p_size)
17     {
18         g_rand ^= (g_rand << 13);
19         g_rand ^= (g_rand >> 7);
20         g_rand ^= (g_rand << 17);
21
22         unsigned l_amount = (p_size > 8 ? 8 : p_size);
23         memcpy(p_dest, &g_rand, l_amount);
24         p_size -= l_amount;
25     }
26     return 1;
27 }
28
29 #endif
30
31 int main()
32 {
33 #if LPC11XX
34     uartInit(BAUD_115200);
35         initTime();
36
37     uECC_set_rng(&fake_rng);
38 #endif
39
40     uint8_t l_public[uECC_BYTES*2];
41     uint8_t l_private[uECC_BYTES];
42
43     uint8_t l_hash[uECC_BYTES];
44
45     uint8_t l_sig[uECC_BYTES*2];
46
47     int i;
48
49     printf("Testing 256 signatures\n");
50
51     for(i=0; i<256; ++i)
52     {
53         printf(".");
54     #if !LPC11XX
55         fflush(stdout);
56     #endif
57
58         if(!uECC_make_key(l_public, l_private))
59         {
60             printf("uECC_make_key() failed\n");
61             continue;
62         }
63         memcpy(l_hash, l_public, uECC_BYTES);
64
65         if(!uECC_sign(l_private, l_hash, l_sig))
66         {
67             printf("uECC_sign() failed\n");
68             continue;
69         }
70
71         if(!uECC_verify(l_public, l_hash, l_sig))
72         {
73             printf("uECC_verify() failed\n");
74         }
75     }
76     printf("\n");
77
78     return 0;
79 }