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