summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/deps/picotls/deps/micro-ecc/test/test_compute.c
blob: 7b936d829cb1e6de17750eeabd4e4c151c020db9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */

#include "uECC.h"

#include <stdio.h>
#include <string.h>

void vli_print(char *str, uint8_t *vli, unsigned int size) {
    printf("%s ", str);
    for(unsigned i=0; i<size; ++i) {
        printf("%02X ", (unsigned)vli[i]);
    }
    printf("\n");
}

int main() {
    int i;
    int success;
    uint8_t private[32];
    uint8_t public[64];
    uint8_t public_computed[64];
    
    int c;
    
    const struct uECC_Curve_t * curves[5];
    int num_curves = 0;
#if uECC_SUPPORTS_secp160r1
    curves[num_curves++] = uECC_secp160r1();
#endif
#if uECC_SUPPORTS_secp192r1
    curves[num_curves++] = uECC_secp192r1();
#endif
#if uECC_SUPPORTS_secp224r1
    curves[num_curves++] = uECC_secp224r1();
#endif
#if uECC_SUPPORTS_secp256r1
    curves[num_curves++] = uECC_secp256r1();
#endif
#if uECC_SUPPORTS_secp256k1
    curves[num_curves++] = uECC_secp256k1();
#endif

    printf("Testing 256 random private key pairs\n");
    for (c = 0; c < num_curves; ++c) {
        for (i = 0; i < 256; ++i) {
            printf(".");
            fflush(stdout);
            
            memset(public, 0, sizeof(public));
            memset(public_computed, 0, sizeof(public_computed));
            
            if (!uECC_make_key(public, private, curves[c])) {
                printf("uECC_make_key() failed\n");
                continue;
            }

            if (!uECC_compute_public_key(private, public_computed, curves[c])) {
                printf("uECC_compute_public_key() failed\n");
            }

            if (memcmp(public, public_computed, sizeof(public)) != 0) {
                printf("Computed and provided public keys are not identical!\n");
                vli_print("Computed public key = ", public_computed, sizeof(public_computed));
                vli_print("Provided public key = ", public, sizeof(public));
                vli_print("Private key = ", private, sizeof(private));
            }
        }
        
        printf("\n");
        printf("Testing private key = 0\n");

        memset(private, 0, sizeof(private));
        success = uECC_compute_public_key(private, public_computed, curves[c]);
        if (success) {
            printf("uECC_compute_public_key() should have failed\n");
        }
        printf("\n");
    }
    
    return 0;
}