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