Skip to content

Commit 0055b86

Browse files
committed
Merge #1551: Add ellswift usage example
31f8459 Add ellswift usage example (Sebastian Falbesoner) fe4fbaa examples: fix case typos in secret clearing paragraphs (s/, Or/, or/) (Sebastian Falbesoner) Pull request description: ACKs for top commit: real-or-random: utACK 31f8459 jonasnick: ACK 31f8459 Tree-SHA512: 5fe894b599c78db585a6d5238c17a894d020106561a625027efc4d67c870a288aad0814e77e7c3cb03e96b7cf940db95a6933d5a5e34a93aa644b3dcbfd8e938
2 parents ea2d5f0 + 31f8459 commit 0055b86

9 files changed

+146
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ctime_tests
1010
ecdh_example
1111
ecdsa_example
1212
schnorr_example
13+
ellswift_example
1314
*.exe
1415
*.so
1516
*.a

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
#### Added
11+
- Added usage example for an ElligatorSwift key exchange.
12+
1013
## [0.5.0] - 2024-05-06
1114

1215
#### Added

Makefile.am

+11
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,17 @@ schnorr_example_LDFLAGS += -lbcrypt
184184
endif
185185
TESTS += schnorr_example
186186
endif
187+
if ENABLE_MODULE_ELLSWIFT
188+
noinst_PROGRAMS += ellswift_example
189+
ellswift_example_SOURCES = examples/ellswift.c
190+
ellswift_example_CPPFLAGS = -I$(top_srcdir)/include -DSECP256K1_STATIC
191+
ellswift_example_LDADD = libsecp256k1.la
192+
ellswift_example_LDFLAGS = -static
193+
if BUILD_WINDOWS
194+
ellswift_example_LDFLAGS += -lbcrypt
195+
endif
196+
TESTS += ellswift_example
197+
endif
187198
endif
188199

189200
### Precomputed tables

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ Usage examples can be found in the [examples](examples) directory. To compile th
114114
* [ECDSA example](examples/ecdsa.c)
115115
* [Schnorr signatures example](examples/schnorr.c)
116116
* [Deriving a shared secret (ECDH) example](examples/ecdh.c)
117+
* [ElligatorSwift key exchange example](examples/ellswift.c)
117118

118119
To compile the Schnorr signature and ECDH examples, you also need to configure with `--enable-module-schnorrsig` and `--enable-module-ecdh`.
119120

examples/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ endif()
2828
if(SECP256K1_ENABLE_MODULE_SCHNORRSIG)
2929
add_example(schnorr)
3030
endif()
31+
32+
if(SECP256K1_ENABLE_MODULE_ELLSWIFT)
33+
add_example(ellswift)
34+
endif()

examples/ecdh.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ int main(void) {
108108

109109
/* It's best practice to try to clear secrets from memory after using them.
110110
* This is done because some bugs can allow an attacker to leak memory, for
111-
* example through "out of bounds" array access (see Heartbleed), Or the OS
111+
* example through "out of bounds" array access (see Heartbleed), or the OS
112112
* swapping them to disk. Hence, we overwrite the secret key buffer with zeros.
113113
*
114114
* Here we are preventing these writes from being optimized out, as any good compiler

examples/ecdsa.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ int main(void) {
128128

129129
/* It's best practice to try to clear secrets from memory after using them.
130130
* This is done because some bugs can allow an attacker to leak memory, for
131-
* example through "out of bounds" array access (see Heartbleed), Or the OS
131+
* example through "out of bounds" array access (see Heartbleed), or the OS
132132
* swapping them to disk. Hence, we overwrite the secret key buffer with zeros.
133133
*
134134
* Here we are preventing these writes from being optimized out, as any good compiler

examples/ellswift.c

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*************************************************************************
2+
* Written in 2024 by Sebastian Falbesoner *
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+
/** This file demonstrates how to use the ElligatorSwift module to perform
11+
* a key exchange according to BIP 324. Additionally, see the documentation
12+
* in include/secp256k1_ellswift.h and doc/ellswift.md.
13+
*/
14+
15+
#include <stdio.h>
16+
#include <assert.h>
17+
#include <string.h>
18+
19+
#include <secp256k1.h>
20+
#include <secp256k1_ellswift.h>
21+
22+
#include "examples_util.h"
23+
24+
int main(void) {
25+
secp256k1_context* ctx;
26+
unsigned char randomize[32];
27+
unsigned char auxrand1[32];
28+
unsigned char auxrand2[32];
29+
unsigned char seckey1[32];
30+
unsigned char seckey2[32];
31+
unsigned char ellswift_pubkey1[64];
32+
unsigned char ellswift_pubkey2[64];
33+
unsigned char shared_secret1[32];
34+
unsigned char shared_secret2[32];
35+
int return_val;
36+
37+
/* Create a secp256k1 context */
38+
ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
39+
if (!fill_random(randomize, sizeof(randomize))) {
40+
printf("Failed to generate randomness\n");
41+
return 1;
42+
}
43+
/* Randomizing the context is recommended to protect against side-channel
44+
* leakage. See `secp256k1_context_randomize` in secp256k1.h for more
45+
* information about it. This should never fail. */
46+
return_val = secp256k1_context_randomize(ctx, randomize);
47+
assert(return_val);
48+
49+
/*** Generate secret keys ***/
50+
51+
/* If the secret key is zero or out of range (bigger than secp256k1's
52+
* order), we try to sample a new key. Note that the probability of this
53+
* happening is negligible. */
54+
while (1) {
55+
if (!fill_random(seckey1, sizeof(seckey1)) || !fill_random(seckey2, sizeof(seckey2))) {
56+
printf("Failed to generate randomness\n");
57+
return 1;
58+
}
59+
if (secp256k1_ec_seckey_verify(ctx, seckey1) && secp256k1_ec_seckey_verify(ctx, seckey2)) {
60+
break;
61+
}
62+
}
63+
64+
/* Generate ElligatorSwift public keys. This should never fail with valid context and
65+
verified secret keys. Note that providing additional randomness (fourth parameter) is
66+
optional, but recommended. */
67+
if (!fill_random(auxrand1, sizeof(auxrand1)) || !fill_random(auxrand2, sizeof(auxrand2))) {
68+
printf("Failed to generate randomness\n");
69+
return 1;
70+
}
71+
return_val = secp256k1_ellswift_create(ctx, ellswift_pubkey1, seckey1, auxrand1);
72+
assert(return_val);
73+
return_val = secp256k1_ellswift_create(ctx, ellswift_pubkey2, seckey2, auxrand2);
74+
assert(return_val);
75+
76+
/*** Create the shared secret on each side ***/
77+
78+
/* Perform x-only ECDH with seckey1 and ellswift_pubkey2. Should never fail
79+
* with a verified seckey and valid pubkey. Note that both parties pass both
80+
* EllSwift pubkeys in the same order; the pubkey of the calling party is
81+
* determined by the "party" boolean (sixth parameter). */
82+
return_val = secp256k1_ellswift_xdh(ctx, shared_secret1, ellswift_pubkey1, ellswift_pubkey2,
83+
seckey1, 0, secp256k1_ellswift_xdh_hash_function_bip324, NULL);
84+
assert(return_val);
85+
86+
/* Perform x-only ECDH with seckey2 and ellswift_pubkey1. Should never fail
87+
* with a verified seckey and valid pubkey. */
88+
return_val = secp256k1_ellswift_xdh(ctx, shared_secret2, ellswift_pubkey1, ellswift_pubkey2,
89+
seckey2, 1, secp256k1_ellswift_xdh_hash_function_bip324, NULL);
90+
assert(return_val);
91+
92+
/* Both parties should end up with the same shared secret */
93+
return_val = memcmp(shared_secret1, shared_secret2, sizeof(shared_secret1));
94+
assert(return_val == 0);
95+
96+
printf( " Secret Key1: ");
97+
print_hex(seckey1, sizeof(seckey1));
98+
printf( "EllSwift Pubkey1: ");
99+
print_hex(ellswift_pubkey1, sizeof(ellswift_pubkey1));
100+
printf("\n Secret Key2: ");
101+
print_hex(seckey2, sizeof(seckey2));
102+
printf( "EllSwift Pubkey2: ");
103+
print_hex(ellswift_pubkey2, sizeof(ellswift_pubkey2));
104+
printf("\n Shared Secret: ");
105+
print_hex(shared_secret1, sizeof(shared_secret1));
106+
107+
/* This will clear everything from the context and free the memory */
108+
secp256k1_context_destroy(ctx);
109+
110+
/* It's best practice to try to clear secrets from memory after using them.
111+
* This is done because some bugs can allow an attacker to leak memory, for
112+
* example through "out of bounds" array access (see Heartbleed), or the OS
113+
* swapping them to disk. Hence, we overwrite the secret key buffer with zeros.
114+
*
115+
* Here we are preventing these writes from being optimized out, as any good compiler
116+
* will remove any writes that aren't used. */
117+
secure_erase(seckey1, sizeof(seckey1));
118+
secure_erase(seckey2, sizeof(seckey2));
119+
secure_erase(shared_secret1, sizeof(shared_secret1));
120+
secure_erase(shared_secret2, sizeof(shared_secret2));
121+
122+
return 0;
123+
}

examples/schnorr.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ int main(void) {
146146

147147
/* It's best practice to try to clear secrets from memory after using them.
148148
* This is done because some bugs can allow an attacker to leak memory, for
149-
* example through "out of bounds" array access (see Heartbleed), Or the OS
149+
* example through "out of bounds" array access (see Heartbleed), or the OS
150150
* swapping them to disk. Hence, we overwrite the secret key buffer with zeros.
151151
*
152152
* Here we are preventing these writes from being optimized out, as any good compiler

0 commit comments

Comments
 (0)