Skip to content

Commit fee7d4b

Browse files
elichaijonasnick
andcommitted
Add an ECDSA signing and verifying example
Co-authored-by: Jonas Nick <jonasd.nick@gmail.com>
1 parent a1102b1 commit fee7d4b

File tree

2 files changed

+210
-0
lines changed

2 files changed

+210
-0
lines changed

examples/ecdsa.c

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*************************************************************************
2+
* Written in 2020-2022 by Elichai Turkel *
3+
* To the extent possible under law, the author(s) have dedicated all *
4+
* copyright and related and neighboring rights to the software in this *
5+
* file to the public domain worldwide. This software is distributed *
6+
* without any warranty. For the CC0 Public Domain Dedication, see *
7+
* EXAMPLES_COPYING or https://creativecommons.org/publicdomain/zero/1.0 *
8+
*************************************************************************/
9+
10+
#include <stdio.h>
11+
#include <assert.h>
12+
#include <string.h>
13+
14+
#include <secp256k1.h>
15+
16+
#include "random.h"
17+
18+
19+
20+
int main(void) {
21+
/* Instead of signing the message directly, we must sign a 32-byte hash.
22+
* Here the message is "Hello, world!" and the hash function was SHA-256.
23+
* An actual implementation should just call SHA-256, but this example
24+
* hardcodes the output to avoid depending on an additional library.
25+
* See https://bitcoin.stackexchange.com/questions/81115/if-someone-wanted-to-pretend-to-be-satoshi-by-posting-a-fake-signature-to-defrau/81116#81116 */
26+
unsigned char msg_hash[32] = {
27+
0x31, 0x5F, 0x5B, 0xDB, 0x76, 0xD0, 0x78, 0xC4,
28+
0x3B, 0x8A, 0xC0, 0x06, 0x4E, 0x4A, 0x01, 0x64,
29+
0x61, 0x2B, 0x1F, 0xCE, 0x77, 0xC8, 0x69, 0x34,
30+
0x5B, 0xFC, 0x94, 0xC7, 0x58, 0x94, 0xED, 0xD3,
31+
};
32+
unsigned char seckey[32];
33+
unsigned char randomize[32];
34+
unsigned char compressed_pubkey[33];
35+
unsigned char serialized_signature[64];
36+
size_t len;
37+
int is_signature_valid;
38+
int return_val;
39+
secp256k1_pubkey pubkey;
40+
secp256k1_ecdsa_signature sig;
41+
/* The specification in secp256k1.h states that `secp256k1_ec_pubkey_create` needs
42+
* a context object initialized for signing and `secp256k1_ecdsa_verify` needs
43+
* a context initialized for verification, which is why we create a context
44+
* for both signing and verification with the SECP256K1_CONTEXT_SIGN and
45+
* SECP256K1_CONTEXT_VERIFY flags. */
46+
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
47+
if (!fill_random(randomize, sizeof(randomize))) {
48+
printf("Failed to generate randomness\n");
49+
return 1;
50+
}
51+
/* Randomizing the context is recommended to protect against side-channel
52+
* leakage See `secp256k1_context_randomize` in secp256k1.h for more
53+
* information about it. This should never fail. */
54+
return_val = secp256k1_context_randomize(ctx, randomize);
55+
assert(return_val);
56+
57+
/*** Key Generation ***/
58+
59+
/* If the secret key is zero or out of range (bigger than secp256k1's
60+
* order), we try to sample a new key. Note that the probability of this
61+
* happening is negligible. */
62+
while (1) {
63+
if (!fill_random(seckey, sizeof(seckey))) {
64+
printf("Failed to generate randomness\n");
65+
return 1;
66+
}
67+
if (secp256k1_ec_seckey_verify(ctx, seckey)) {
68+
break;
69+
}
70+
}
71+
72+
/* Public key creation using a valid context with a verified secret key should never fail */
73+
return_val = secp256k1_ec_pubkey_create(ctx, &pubkey, seckey);
74+
assert(return_val);
75+
76+
/* Serialize the pubkey in a compressed form(33 bytes). Should always return 1. */
77+
len = sizeof(compressed_pubkey);
78+
return_val = secp256k1_ec_pubkey_serialize(ctx, compressed_pubkey, &len, &pubkey, SECP256K1_EC_COMPRESSED);
79+
assert(return_val);
80+
/* Should be the same size as the size of the output, because we passed a 33 byte array. */
81+
assert(len == sizeof(compressed_pubkey));
82+
83+
/*** Signing ***/
84+
85+
/* Generate an ECDSA signature `noncefp` and `ndata` allows you to pass a
86+
* custom nonce function, passing `NULL` will use the RFC-6979 safe default.
87+
* Signing with a valid context, verified secret key
88+
* and the default nonce function should never fail. */
89+
return_val = secp256k1_ecdsa_sign(ctx, &sig, msg_hash, seckey, NULL, NULL);
90+
assert(return_val);
91+
92+
/* Serialize the signature in a compact form. Should always return 1
93+
* according to the documentation in secp256k1.h. */
94+
return_val = secp256k1_ecdsa_signature_serialize_compact(ctx, serialized_signature, &sig);
95+
assert(return_val);
96+
97+
98+
/*** Verification ***/
99+
100+
/* Deserialize the signature. This will return 0 if the signature can't be parsed correctly. */
101+
if (!secp256k1_ecdsa_signature_parse_compact(ctx, &sig, serialized_signature)) {
102+
printf("Failed parsing the signature\n");
103+
return 1;
104+
}
105+
106+
/* Deserialize the public key. This will return 0 if the public key can't be parsed correctly. */
107+
if (!secp256k1_ec_pubkey_parse(ctx, &pubkey, compressed_pubkey, sizeof(compressed_pubkey))) {
108+
printf("Failed parsing the public key\n");
109+
return 1;
110+
}
111+
112+
/* Verify a signature. This will return 1 if it's valid and 0 if it's not. */
113+
is_signature_valid = secp256k1_ecdsa_verify(ctx, &sig, msg_hash, &pubkey);
114+
115+
printf("Is the signature valid? %s\n", is_signature_valid ? "true" : "false");
116+
printf("Secret Key: ");
117+
print_hex(seckey, sizeof(seckey));
118+
printf("Public Key: ");
119+
print_hex(compressed_pubkey, sizeof(compressed_pubkey));
120+
printf("Signature: ");
121+
print_hex(serialized_signature, sizeof(serialized_signature));
122+
123+
124+
/* This will clear everything from the context and free the memory */
125+
secp256k1_context_destroy(ctx);
126+
127+
/* It's best practice to try to clear secrets from memory after using them.
128+
* This is done because some bugs can allow an attacker to leak memory, for
129+
* example through "out of bounds" array access (see Heartbleed), Or the OS
130+
* swapping them to disk. Hence, we overwrite the secret key buffer with zeros.
131+
*
132+
* TODO: Prevent these writes from being optimized out, as any good compiler
133+
* will remove any writes that aren't used. */
134+
memset(seckey, 0, sizeof(seckey));
135+
136+
return 0;
137+
}

examples/random.h

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*************************************************************************
2+
* Copyright (c) 2020-2021 Elichai Turkel *
3+
* Distributed under the CC0 software license, see the accompanying file *
4+
* EXAMPLES_COPYING or https://creativecommons.org/publicdomain/zero/1.0 *
5+
*************************************************************************/
6+
7+
/*
8+
* This file is an attempt at collecting best practice methods for obtaining randomness with different operating systems.
9+
* It may be out-of-date. Consult the documentation of the operating system before considering to use the methods below.
10+
*
11+
* Platform randomness sources:
12+
* Linux -> `getrandom(2)`(`sys/random.h`), if not available `/dev/urandom` should be used. http://man7.org/linux/man-pages/man2/getrandom.2.html, https://linux.die.net/man/4/urandom
13+
* macOS -> `getentropy(2)`(`sys/random.h`), if not available `/dev/urandom` should be used. https://www.unix.com/man-page/mojave/2/getentropy, https://opensource.apple.com/source/xnu/xnu-517.12.7/bsd/man/man4/random.4.auto.html
14+
* FreeBSD -> `getrandom(2)`(`sys/random.h`), if not available `kern.arandom` should be used. https://www.freebsd.org/cgi/man.cgi?query=getrandom, https://www.freebsd.org/cgi/man.cgi?query=random&sektion=4
15+
* OpenBSD -> `getentropy(2)`(`unistd.h`), if not available `/dev/urandom` should be used. https://man.openbsd.org/getentropy, https://man.openbsd.org/urandom
16+
* Windows -> `BCryptGenRandom`(`bcrypt.h`). https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom
17+
*/
18+
19+
#if defined(_WIN32)
20+
#include <windows.h>
21+
#include <ntstatus.h>
22+
#include <bcrypt.h>
23+
#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
24+
#include <sys/random.h>
25+
#elif defined(__OpenBSD__)
26+
#include <unistd.h>
27+
#else
28+
#error "Couldn't identify the OS"
29+
#endif
30+
31+
#include <stddef.h>
32+
#include <limits.h>
33+
#include <stdio.h>
34+
35+
36+
/* Returns 1 on success, and 0 on failure. */
37+
static int fill_random(unsigned char* data, size_t size) {
38+
#if defined(_WIN32)
39+
NTSTATUS res = BCryptGenRandom(NULL, data, size, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
40+
if (res != STATUS_SUCCESS || size > ULONG_MAX) {
41+
return 0;
42+
} else {
43+
return 1;
44+
}
45+
#elif defined(__linux__) || defined(__FreeBSD__)
46+
/* If `getrandom(2)` is not available you should fallback to /dev/urandom */
47+
ssize_t res = getrandom(data, size, 0);
48+
if (res < 0 || (size_t)res != size ) {
49+
return 0;
50+
} else {
51+
return 1;
52+
}
53+
#elif defined(__APPLE__) || defined(__OpenBSD__)
54+
/* If `getentropy(2)` is not available you should fallback to either
55+
* `SecRandomCopyBytes` or /dev/urandom */
56+
int res = getentropy(data, size);
57+
if (res == 0) {
58+
return 1;
59+
} else {
60+
return 0;
61+
}
62+
#endif
63+
return 0;
64+
}
65+
66+
static void print_hex(unsigned char* data, size_t size) {
67+
size_t i;
68+
printf("0x");
69+
for (i = 0; i < size; i++) {
70+
printf("%02x", data[i]);
71+
}
72+
printf("\n");
73+
}

0 commit comments

Comments
 (0)