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
1617int 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. */
0 commit comments