xref: /btstack/3rd-party/micro-ecc/test/test_compute.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 
vli_print(uint8_t * vli,unsigned int size)8*af03003cSMatthias Ringwald void vli_print(uint8_t *vli, unsigned int size) {
9*af03003cSMatthias Ringwald     while (size) {
10*af03003cSMatthias Ringwald         printf("%02X ", (unsigned)vli[size - 1]);
11*af03003cSMatthias Ringwald         --size;
12*af03003cSMatthias Ringwald     }
13*af03003cSMatthias Ringwald }
14*af03003cSMatthias Ringwald 
main()15*af03003cSMatthias Ringwald int main() {
16*af03003cSMatthias Ringwald     int i;
17*af03003cSMatthias Ringwald     int success;
18*af03003cSMatthias Ringwald     uint8_t private[uECC_BYTES];
19*af03003cSMatthias Ringwald     uint8_t public[uECC_BYTES * 2];
20*af03003cSMatthias Ringwald     uint8_t public_computed[uECC_BYTES * 2];
21*af03003cSMatthias Ringwald 
22*af03003cSMatthias Ringwald     printf("Testing 256 random private key pairs\n");
23*af03003cSMatthias Ringwald     for (i = 0; i < 256; ++i) {
24*af03003cSMatthias Ringwald         printf(".");
25*af03003cSMatthias Ringwald     #if !LPC11XX
26*af03003cSMatthias Ringwald         fflush(stdout);
27*af03003cSMatthias Ringwald     #endif
28*af03003cSMatthias Ringwald 
29*af03003cSMatthias Ringwald         success = uECC_make_key(public, private);
30*af03003cSMatthias Ringwald         if (!success) {
31*af03003cSMatthias Ringwald             printf("uECC_make_key() failed\n");
32*af03003cSMatthias Ringwald             return 1;
33*af03003cSMatthias Ringwald         }
34*af03003cSMatthias Ringwald 
35*af03003cSMatthias Ringwald         success = uECC_compute_public_key(private, public_computed);
36*af03003cSMatthias Ringwald         if (!success) {
37*af03003cSMatthias Ringwald             printf("uECC_compute_public_key() failed\n");
38*af03003cSMatthias Ringwald         }
39*af03003cSMatthias Ringwald 
40*af03003cSMatthias Ringwald         if (memcmp(public, public_computed, sizeof(public)) != 0) {
41*af03003cSMatthias Ringwald             printf("Computed and provided public keys are not identical!\n");
42*af03003cSMatthias Ringwald             printf("Computed public key = ");
43*af03003cSMatthias Ringwald             vli_print(public_computed, uECC_BYTES);
44*af03003cSMatthias Ringwald             printf("\n");
45*af03003cSMatthias Ringwald             printf("Provided public key = ");
46*af03003cSMatthias Ringwald             vli_print(public, uECC_BYTES);
47*af03003cSMatthias Ringwald             printf("\n");
48*af03003cSMatthias Ringwald             printf("Private key = ");
49*af03003cSMatthias Ringwald             vli_print(private, uECC_BYTES);
50*af03003cSMatthias Ringwald             printf("\n");
51*af03003cSMatthias Ringwald         }
52*af03003cSMatthias Ringwald     }
53*af03003cSMatthias Ringwald 
54*af03003cSMatthias Ringwald     printf("\n");
55*af03003cSMatthias Ringwald     printf("Testing private key = 0\n");
56*af03003cSMatthias Ringwald 
57*af03003cSMatthias Ringwald     memset(private, 0, uECC_BYTES);
58*af03003cSMatthias Ringwald     success = uECC_compute_public_key(private, public_computed);
59*af03003cSMatthias Ringwald     if (success) {
60*af03003cSMatthias Ringwald         printf("uECC_compute_public_key() should have failed\n");
61*af03003cSMatthias Ringwald     }
62*af03003cSMatthias Ringwald 
63*af03003cSMatthias Ringwald     return 0;
64*af03003cSMatthias Ringwald }
65