Skip to content

Commit 4c43382

Browse files
committed
Fix review comments by Sipa
1 parent 1ef1dbd commit 4c43382

File tree

4 files changed

+71
-50
lines changed

4 files changed

+71
-50
lines changed

examples/ecdh.c

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
#include <assert.h>
99
#include <string.h>
1010

11+
#include <secp256k1.h>
12+
#include <secp256k1_ecdh.h>
13+
1114
#include "random.h"
12-
#include "secp256k1.h"
13-
#include "secp256k1_ecdh.h"
1415

1516

1617
int main(void) {
@@ -21,13 +22,14 @@ int main(void) {
2122
unsigned char shared_secret1[32];
2223
unsigned char shared_secret2[32];
2324
unsigned char randomize[32];
25+
int return_val;
2426
size_t len;
2527
secp256k1_pubkey pubkey1;
2628
secp256k1_pubkey pubkey2;
2729

28-
/* The docs in secp256k1.h above the `secp256k1_ec_pubkey_create` function
29-
* say: "pointer to a context object, initialized for signing" which is why
30-
* we create a context for signing with the SECP256K1_CONTEXT_SIGN flag.
30+
/* The specification in secp256k1.h states that `secp256k1_ec_pubkey_create`
31+
* needs a context object initialized for signing, which is why we create
32+
* a context with the SECP256K1_CONTEXT_SIGN flag.
3133
* (The docs for `secp256k1_ecdh` don't require any special context, just
3234
* some initialized context) */
3335
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
@@ -38,7 +40,8 @@ int main(void) {
3840
/* Randomizing the context is recommended to protect against side-channel
3941
* leakage See `secp256k1_context_randomize` in secp256k1.h for more
4042
* information about it. This should never fail. */
41-
assert(secp256k1_context_randomize(ctx, randomize));
43+
return_val = secp256k1_context_randomize(ctx, randomize);
44+
assert(return_val);
4245

4346
/*** Key Generation ***/
4447

@@ -56,32 +59,40 @@ int main(void) {
5659
}
5760

5861
/* Public key creation using a valid context with a verified secret key should never fail */
59-
assert(secp256k1_ec_pubkey_create(ctx, &pubkey1, seckey1));
60-
assert(secp256k1_ec_pubkey_create(ctx, &pubkey2, seckey2));
62+
return_val = secp256k1_ec_pubkey_create(ctx, &pubkey1, seckey1);
63+
assert(return_val);
64+
return_val = secp256k1_ec_pubkey_create(ctx, &pubkey2, seckey2);
65+
assert(return_val);
6166

6267
/* Serialize pubkey1 in a compressed form (33 bytes), should always return 1 */
6368
len = sizeof(compressed_pubkey1);
64-
assert(secp256k1_ec_pubkey_serialize(ctx, compressed_pubkey1, &len, &pubkey1, SECP256K1_EC_COMPRESSED));
69+
return_val = secp256k1_ec_pubkey_serialize(ctx, compressed_pubkey1, &len, &pubkey1, SECP256K1_EC_COMPRESSED);
70+
assert(return_val);
6571
/* Should be the same size as the size of the output, because we passed a 33 bytes array. */
6672
assert(len == sizeof(compressed_pubkey1));
6773

6874
/* Serialize pubkey2 in a compressed form (33 bytes) */
6975
len = sizeof(compressed_pubkey2);
70-
secp256k1_ec_pubkey_serialize(ctx, compressed_pubkey2, &len, &pubkey2, SECP256K1_EC_COMPRESSED);
76+
return_val = secp256k1_ec_pubkey_serialize(ctx, compressed_pubkey2, &len, &pubkey2, SECP256K1_EC_COMPRESSED);
77+
assert(return_val);
78+
/* Should be the same size as the size of the output, because we passed a 33 bytes array. */
7179
assert(len == sizeof(compressed_pubkey2));
7280

7381
/*** Creating the shared secret ***/
7482

7583
/* Perform ECDH with seckey1 and pubkey2. Should never fail with a verified
7684
* seckey and valid pubkey */
77-
assert(secp256k1_ecdh(ctx, shared_secret1, &pubkey2, seckey1, NULL, NULL));
85+
return_val = secp256k1_ecdh(ctx, shared_secret1, &pubkey2, seckey1, NULL, NULL);
86+
assert(return_val);
7887

7988
/* Perform ECDH with seckey2 and pubkey1. Should never fail with a verified
8089
* seckey and valid pubkey */
81-
assert(secp256k1_ecdh(ctx, shared_secret2, &pubkey1, seckey2, NULL, NULL));
90+
return_val = secp256k1_ecdh(ctx, shared_secret2, &pubkey1, seckey2, NULL, NULL);
91+
assert(return_val);
8292

8393
/* Both parties should end up with the same shared secret */
84-
assert(memcmp(shared_secret1, shared_secret2, sizeof(shared_secret1)) == 0);
94+
return_val = memcmp(shared_secret1, shared_secret2, sizeof(shared_secret1));
95+
assert(return_val == 0);
8596

8697
printf("Secret Key1: ");
8798
print_hex(seckey1, sizeof(seckey1));
@@ -97,10 +108,10 @@ int main(void) {
97108
/* This will clear everything from the context and free the memory */
98109
secp256k1_context_destroy(ctx);
99110

100-
/* It's best practice to try to remove secrets from memory after using them.
101-
* This is done because some bugs can allow an attacker leak memory, for
102-
* example through out of bounds array access (see Heartbleed for example).
103-
* Hence, we overwrite the secret key buffer with zeros.
111+
/* It's best practice to try to clear secrets from memory after using them.
112+
* This is done because some bugs can allow an attacker to leak memory, for
113+
* example through "out of bounds" array access (see Heartbleed), Or the OS
114+
* swapping them to disk. Hence, we overwrite the secret key buffer with zeros.
104115
*
105116
* TODO: Prevent these writes from being optimized out, as any good compiler
106117
* will remove any writes that aren't used. */

examples/ecdsa.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
#include <assert.h>
99
#include <string.h>
1010

11+
#include <secp256k1.h>
12+
1113
#include "random.h"
12-
#include "secp256k1.h"
1314

1415

1516

@@ -21,12 +22,12 @@ int main(void) {
2122
unsigned char serialized_signature[64];
2223
size_t len;
2324
int is_signature_valid;
25+
int return_val;
2426
secp256k1_pubkey pubkey;
2527
secp256k1_ecdsa_signature sig;
26-
/* The docs in secp256k1.h above the `secp256k1_ec_pubkey_create` function
27-
* say: "pointer to a context object, initialized for signing" And the docs
28-
* above the `secp256k1_ecdsa_verify` function say: "a secp256k1 context
29-
* object, initialized for verification" which is why we create a context
28+
/* The specification in secp256k1.h states that `secp256k1_ec_pubkey_create` needs
29+
* a context object initialized for signing and `secp256k1_ecdsa_verify` needs
30+
* a context initialized for verification, which is why we create a context
3031
* for both signing and verification with the SECP256K1_CONTEXT_SIGN and
3132
* SECP256K1_CONTEXT_VERIFY flags. */
3233
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
@@ -37,7 +38,8 @@ int main(void) {
3738
/* Randomizing the context is recommended to protect against side-channel
3839
* leakage See `secp256k1_context_randomize` in secp256k1.h for more
3940
* information about it. This should never fail. */
40-
assert(secp256k1_context_randomize(ctx, randomize));
41+
return_val = secp256k1_context_randomize(ctx, randomize);
42+
assert(return_val);
4143

4244
/*** Key Generation ***/
4345

@@ -55,11 +57,13 @@ int main(void) {
5557
}
5658

5759
/* Public key creation using a valid context with a verified secret key should never fail */
58-
assert(secp256k1_ec_pubkey_create(ctx, &pubkey, seckey));
60+
return_val = secp256k1_ec_pubkey_create(ctx, &pubkey, seckey);
61+
assert(return_val);
5962

6063
/* Serialize the pubkey in a compressed form(33 bytes). Should always return 1. */
6164
len = sizeof(compressed_pubkey);
62-
assert(secp256k1_ec_pubkey_serialize(ctx, compressed_pubkey, &len, &pubkey, SECP256K1_EC_COMPRESSED));
65+
return_val = secp256k1_ec_pubkey_serialize(ctx, compressed_pubkey, &len, &pubkey, SECP256K1_EC_COMPRESSED);
66+
assert(return_val);
6367
/* Should be the same size as the size of the output, because we passed a 33 bytes array. */
6468
assert(len == sizeof(compressed_pubkey));
6569

@@ -70,11 +74,13 @@ int main(void) {
7074
* `noncefp` and `ndata` allows you to pass a custom nonce function, passing
7175
* `NULL` will use the RFC-6979 safe default. Signing with a valid context,
7276
* verified secret key and the default nonce function should never fail. */
73-
assert(secp256k1_ecdsa_sign(ctx, &sig, msg_hash, seckey, NULL, NULL));
77+
return_val = secp256k1_ecdsa_sign(ctx, &sig, msg_hash, seckey, NULL, NULL);
78+
assert(return_val);
7479

7580
/* Serialize the signature in a compact form. Should always return 1
7681
* according to the documentation in secp256k1.h. */
77-
assert(secp256k1_ecdsa_signature_serialize_compact(ctx, serialized_signature, &sig));
82+
return_val = secp256k1_ecdsa_signature_serialize_compact(ctx, serialized_signature, &sig);
83+
assert(return_val);
7884

7985

8086
/*** Verification ***/
@@ -108,10 +114,10 @@ int main(void) {
108114
/* This will clear everything from the context and free the memory */
109115
secp256k1_context_destroy(ctx);
110116

111-
/* It's best practice to try to remove secrets from memory after using them.
112-
* This is done because some bugs can allow an attacker leak memory, for
113-
* example through out of bounds array access (see Heartbleed for example).
114-
* Hence, we overwrite the secret key buffer with zeros.
117+
/* It's best practice to try to clear secrets from memory after using them.
118+
* This is done because some bugs can allow an attacker to leak memory, for
119+
* example through "out of bounds" array access (see Heartbleed), Or the OS
120+
* swapping them to disk. Hence, we overwrite the secret key buffer with zeros.
115121
*
116122
* TODO: Prevent these writes from being optimized out, as any good compiler
117123
* will remove any writes that aren't used. */

examples/random.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
/*
88
* This file is an attempt at collecting best practice methods for obtaining randomness with different operating systems.
99
* It may be out-of-date. Consult the documentation of the operating system before considering to use the methods below.
10+
*
1011
* Platform randomness sources:
1112
* 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
1213
* 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

examples/schnorr.c

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
#include <assert.h>
99
#include <string.h>
1010

11-
#include "random.h"
12-
#include "secp256k1.h"
13-
#include "secp256k1_extrakeys.h"
14-
#include "secp256k1_schnorrsig.h"
11+
#include <secp256k1.h>
12+
#include <secp256k1_extrakeys.h>
13+
#include <secp256k1_schnorrsig.h>
1514

15+
#include "random.h"
1616

1717
int main(void) {
1818
unsigned char msg_hash[32] = {0}; /* This should be a hash of the message. */
@@ -22,14 +22,14 @@ int main(void) {
2222
unsigned char serialized_pubkey[32];
2323
unsigned char signature[64];
2424
int is_signature_valid;
25+
int return_val;
2526
secp256k1_xonly_pubkey pubkey;
2627
secp256k1_keypair keypair;
27-
/* The docs in secp256k1_extrakeys.h above the `secp256k1_keypair_create`
28-
* function say: "pointer to a context object, initialized for signing" And
29-
* the docs above the `secp256k1_schnorrsig_verify` function say: "a
30-
* secp256k1 context object, initialized for verification" which is why we
31-
* create a context for both signing and verification with the
32-
* SECP256K1_CONTEXT_SIGN and SECP256K1_CONTEXT_VERIFY flags. */
28+
/* The specification in secp256k1_extrakeys.h states that `secp256k1_keypair_create`
29+
* needs a context object initialized for signing. And in secp256k1_schnorrsig.h
30+
* they state that `secp256k1_schnorrsig_verify` needs a context initialized for
31+
* verification, which is why we create a context for both signing and verification
32+
* with the SECP256K1_CONTEXT_SIGN and SECP256K1_CONTEXT_VERIFY flags. */
3333
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
3434
if (!fill_random(randomize, sizeof(randomize))) {
3535
printf("Failed to generate randomness\n");
@@ -38,8 +38,8 @@ int main(void) {
3838
/* Randomizing the context is recommended to protect against side-channel
3939
* leakage See `secp256k1_context_randomize` in secp256k1.h for more
4040
* information about it. This should never fail. */
41-
assert(secp256k1_context_randomize(ctx, randomize));
42-
41+
return_val = secp256k1_context_randomize(ctx, randomize);
42+
assert(return_val);
4343
/*** Key Generation ***/
4444

4545
/* If the secret key is zero or out of range (bigger than secp256k1's
@@ -61,10 +61,12 @@ int main(void) {
6161
* `pk_parity` as we don't care about the parity of the key, only advanced
6262
* users might care about the parity. This should never fail with a valid
6363
* context and public key. */
64-
assert(secp256k1_keypair_xonly_pub(ctx, &pubkey, NULL, &keypair));
64+
return_val = secp256k1_keypair_xonly_pub(ctx, &pubkey, NULL, &keypair);
65+
assert(return_val);
6566

6667
/* Serialize the public key. Should always return 1 for a valid public key. */
67-
assert(secp256k1_xonly_pubkey_serialize(ctx, serialized_pubkey, &pubkey));
68+
return_val = secp256k1_xonly_pubkey_serialize(ctx, serialized_pubkey, &pubkey);
69+
assert(return_val);
6870

6971
/*** Signing ***/
7072

@@ -80,7 +82,8 @@ int main(void) {
8082
* improve security against side-channel attacks. Signing with a valid
8183
* context, verified keypair and the default nonce function should never
8284
* fail. */
83-
assert(secp256k1_schnorrsig_sign(ctx, signature, msg_hash, &keypair, auxiliary_rand));
85+
return_val = secp256k1_schnorrsig_sign(ctx, signature, msg_hash, &keypair, auxiliary_rand);
86+
assert(return_val);
8487

8588
/*** Verification ***/
8689

@@ -106,10 +109,10 @@ int main(void) {
106109
/* This will clear everything from the context and free the memory */
107110
secp256k1_context_destroy(ctx);
108111

109-
/* It's best practice to try to remove secrets from memory after using them.
110-
* This is done because some bugs can allow an attacker leak memory, for
111-
* example through out of bounds array access (see Heartbleed for example).
112-
* Hence, we overwrite the secret key buffer with zeros.
112+
/* It's best practice to try to clear secrets from memory after using them.
113+
* This is done because some bugs can allow an attacker to leak memory, for
114+
* example through "out of bounds" array access (see Heartbleed), Or the OS
115+
* swapping them to disk. Hence, we overwrite the secret key buffer with zeros.
113116
*
114117
* TODO: Prevent these writes from being optimized out, as any good compiler
115118
* will remove any writes that aren't used. */

0 commit comments

Comments
 (0)