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
8#define SHA256_BLOCK_LENGTH  64
9#define SHA256_DIGEST_LENGTH 32
10
11typedef struct SHA256_CTX {
12	uint32_t	state[8];
13	uint64_t	bitcount;
14	uint8_t	buffer[SHA256_BLOCK_LENGTH];
15} SHA256_CTX;
16
17extern void SHA256_Init(SHA256_CTX *ctx);
18extern void SHA256_Update(SHA256_CTX *ctx, const uint8_t *message, size_t message_size);
19extern void SHA256_Final(uint8_t digest[SHA256_DIGEST_LENGTH], SHA256_CTX *ctx);
20
21typedef struct SHA256_HashContext {
22    uECC_HashContext uECC;
23    SHA256_CTX ctx;
24} SHA256_HashContext;
25
26static void init_SHA256(const uECC_HashContext *base) {
27    SHA256_HashContext *context = (SHA256_HashContext *)base;
28    SHA256_Init(&context->ctx);
29}
30
31static void update_SHA256(const uECC_HashContext *base,
32                          const uint8_t *message,
33                          unsigned message_size) {
34    SHA256_HashContext *context = (SHA256_HashContext *)base;
35    SHA256_Update(&context->ctx, message, message_size);
36}
37
38static void finish_SHA256(const uECC_HashContext *base, uint8_t *hash_result) {
39    SHA256_HashContext *context = (SHA256_HashContext *)base;
40    SHA256_Final(hash_result, &context->ctx);
41}
42
43int main() {
44    int i, c;
45    uint8_t private[32] = {0};
46    uint8_t public[64] = {0};
47    uint8_t hash[32] = {0};
48    uint8_t sig[64] = {0};
49
50    uint8_t tmp[2 * SHA256_DIGEST_LENGTH + SHA256_BLOCK_LENGTH];
51    SHA256_HashContext ctx = {{
52        &init_SHA256,
53        &update_SHA256,
54        &finish_SHA256,
55        SHA256_BLOCK_LENGTH,
56        SHA256_DIGEST_LENGTH,
57        tmp
58    }};
59
60    const struct uECC_Curve_t * curves[5];
61    curves[0] = uECC_secp160r1();
62    curves[1] = uECC_secp192r1();
63    curves[2] = uECC_secp224r1();
64    curves[3] = uECC_secp256r1();
65    curves[4] = uECC_secp256k1();
66
67    printf("Testing 256 signatures\n");
68    for (c = 0; c < 5; ++c) {
69        for (i = 0; i < 256; ++i) {
70            printf(".");
71            fflush(stdout);
72
73            if (!uECC_make_key(public, private, curves[c])) {
74                printf("uECC_make_key() failed\n");
75                return 1;
76            }
77            memcpy(hash, public, sizeof(hash));
78
79            if (!uECC_sign_deterministic(private, hash, sizeof(hash), &ctx.uECC, sig, curves[c])) {
80                printf("uECC_sign() failed\n");
81                return 1;
82            }
83
84            if (!uECC_verify(public, hash, sizeof(hash), sig, curves[c])) {
85                printf("uECC_verify() failed\n");
86                return 1;
87            }
88        }
89        printf("\n");
90    }
91
92    return 0;
93}
94