Skip to content

Commit 7e8dbc1

Browse files
rustyrussellcdecker
authored andcommitted
sphinx: use struct pubkey.
It was using raw secp256k1_pubkey; we have better helpers for struct pubkey. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 109c6eb commit 7e8dbc1

File tree

3 files changed

+26
-37
lines changed

3 files changed

+26
-37
lines changed

channeld/channeld.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,6 @@ static struct secret *get_shared_secret(const tal_t *ctx,
532532
enum onion_type *why_bad,
533533
struct sha256 *next_onion_sha)
534534
{
535-
struct pubkey ephemeral;
536535
struct onionpacket *op;
537536
struct secret *secret = tal(ctx, struct secret);
538537
const u8 *msg;
@@ -545,8 +544,7 @@ static struct secret *get_shared_secret(const tal_t *ctx,
545544
return tal_free(secret);
546545

547546
/* Because wire takes struct pubkey. */
548-
ephemeral.pubkey = op->ephemeralkey;
549-
msg = hsm_req(tmpctx, towire_hsm_ecdh_req(tmpctx, &ephemeral));
547+
msg = hsm_req(tmpctx, towire_hsm_ecdh_req(tmpctx, &op->ephemeralkey));
550548
if (!fromwire_hsm_ecdh_resp(msg, secret))
551549
status_failed(STATUS_FAIL_HSM_IO, "Reading ecdh response");
552550

common/sphinx.c

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <assert.h>
22

3+
#include <ccan/array_size/array_size.h>
34
#include <ccan/crypto/ripemd160/ripemd160.h>
45
#include <ccan/crypto/sha256/sha256.h>
56
#include <ccan/mem/mem.h>
@@ -24,7 +25,7 @@
2425
struct hop_params {
2526
u8 secret[SHARED_SECRET_SIZE];
2627
u8 blind[BLINDING_FACTOR_SIZE];
27-
secp256k1_pubkey ephemeralkey;
28+
struct pubkey ephemeralkey;
2829
};
2930

3031
struct keyset {
@@ -58,18 +59,12 @@ u8 *serialize_onionpacket(
5859
{
5960
u8 *dst = tal_arr(ctx, u8, TOTAL_PACKET_SIZE);
6061

61-
u8 der[33];
62-
size_t outputlen = 33;
62+
u8 der[PUBKEY_DER_LEN];
6363
int p = 0;
6464

65-
secp256k1_ec_pubkey_serialize(secp256k1_ctx,
66-
der,
67-
&outputlen,
68-
&m->ephemeralkey,
69-
SECP256K1_EC_COMPRESSED);
70-
65+
pubkey_to_der(der, &m->ephemeralkey);
7166
write_buffer(dst, &m->version, 1, &p);
72-
write_buffer(dst, der, outputlen, &p);
67+
write_buffer(dst, der, sizeof(der), &p);
7368
write_buffer(dst, m->routinginfo, ROUTING_INFO_SIZE, &p);
7469
write_buffer(dst, m->mac, sizeof(m->mac), &p);
7570
return dst;
@@ -82,7 +77,7 @@ struct onionpacket *parse_onionpacket(const tal_t *ctx,
8277
{
8378
struct onionpacket *m;
8479
int p = 0;
85-
u8 rawEphemeralkey[33];
80+
u8 rawEphemeralkey[PUBKEY_DER_LEN];
8681

8782
assert(srclen == TOTAL_PACKET_SIZE);
8883

@@ -94,9 +89,10 @@ struct onionpacket *parse_onionpacket(const tal_t *ctx,
9489
*why_bad = WIRE_INVALID_ONION_VERSION;
9590
return tal_free(m);
9691
}
97-
read_buffer(rawEphemeralkey, src, 33, &p);
92+
read_buffer(rawEphemeralkey, src, sizeof(rawEphemeralkey), &p);
9893

99-
if (secp256k1_ec_pubkey_parse(secp256k1_ctx, &m->ephemeralkey, rawEphemeralkey, 33) != 1) {
94+
if (!pubkey_from_der(rawEphemeralkey, sizeof(rawEphemeralkey),
95+
&m->ephemeralkey)) {
10096
*why_bad = WIRE_INVALID_ONION_KEY;
10197
return tal_free(m);
10298
}
@@ -185,17 +181,15 @@ static bool generate_header_padding(
185181
return true;
186182
}
187183

188-
static void compute_blinding_factor(const secp256k1_pubkey *key,
184+
static void compute_blinding_factor(const struct pubkey *key,
189185
const u8 sharedsecret[SHARED_SECRET_SIZE],
190186
u8 res[BLINDING_FACTOR_SIZE])
191187
{
192188
struct sha256_ctx ctx;
193-
u8 der[33];
194-
size_t outputlen = 33;
189+
u8 der[PUBKEY_DER_LEN];
195190
struct sha256 temp;
196191

197-
secp256k1_ec_pubkey_serialize(secp256k1_ctx, der, &outputlen, key,
198-
SECP256K1_EC_COMPRESSED);
192+
pubkey_to_der(der, key);
199193
sha256_init(&ctx);
200194
sha256_update(&ctx, der, sizeof(der));
201195
sha256_update(&ctx, sharedsecret, SHARED_SECRET_SIZE);
@@ -204,25 +198,26 @@ static void compute_blinding_factor(const secp256k1_pubkey *key,
204198
}
205199

206200
static bool blind_group_element(
207-
secp256k1_pubkey *blindedelement,
208-
const secp256k1_pubkey *pubkey,
201+
struct pubkey *blindedelement,
202+
const struct pubkey *pubkey,
209203
const u8 blind[BLINDING_FACTOR_SIZE])
210204
{
211205
/* tweak_mul is inplace so copy first. */
212206
if (pubkey != blindedelement)
213207
*blindedelement = *pubkey;
214-
if (secp256k1_ec_pubkey_tweak_mul(secp256k1_ctx, blindedelement, blind) != 1)
208+
if (secp256k1_ec_pubkey_tweak_mul(secp256k1_ctx, &blindedelement->pubkey, blind) != 1)
215209
return false;
216210
return true;
217211
}
218212

219213
static bool create_shared_secret(
220214
u8 *secret,
221-
const secp256k1_pubkey *pubkey,
215+
const struct pubkey *pubkey,
222216
const u8 *sessionkey)
223217
{
224218

225-
if (secp256k1_ecdh(secp256k1_ctx, secret, pubkey, sessionkey) != 1)
219+
if (secp256k1_ecdh(secp256k1_ctx, secret, &pubkey->pubkey, sessionkey)
220+
!= 1)
226221
return false;
227222
return true;
228223
}
@@ -251,17 +246,16 @@ static struct hop_params *generate_hop_params(
251246
struct pubkey path[])
252247
{
253248
int i, j, num_hops = tal_count(path);
254-
secp256k1_pubkey temp;
249+
struct pubkey temp;
255250
u8 blind[BLINDING_FACTOR_SIZE];
256251
struct hop_params *params = tal_arr(ctx, struct hop_params, num_hops);
257252

258253
/* Initialize the first hop with the raw information */
259254
if (secp256k1_ec_pubkey_create(
260-
secp256k1_ctx, &params[0].ephemeralkey, sessionkey) != 1)
255+
secp256k1_ctx, &params[0].ephemeralkey.pubkey, sessionkey) != 1)
261256
return NULL;
262257

263-
if (!create_shared_secret(
264-
params[0].secret, &path[0].pubkey, sessionkey))
258+
if (!create_shared_secret(params[0].secret, &path[0], sessionkey))
265259
return NULL;
266260

267261
compute_blinding_factor(
@@ -282,7 +276,7 @@ static struct hop_params *generate_hop_params(
282276
* Order is indifferent, multiplication is commutative.
283277
*/
284278
memcpy(&blind, sessionkey, 32);
285-
temp = path[i].pubkey;
279+
temp = path[i];
286280
if (!blind_group_element(&temp, &temp, blind))
287281
return NULL;
288282
for (j = 0; j < i; j++)
@@ -295,11 +289,8 @@ static struct hop_params *generate_hop_params(
295289
/* Now hash temp and store it. This requires us to
296290
* DER-serialize first and then skip the sign byte.
297291
*/
298-
u8 der[33];
299-
size_t outputlen = 33;
300-
secp256k1_ec_pubkey_serialize(
301-
secp256k1_ctx, der, &outputlen, &temp,
302-
SECP256K1_EC_COMPRESSED);
292+
u8 der[PUBKEY_DER_LEN];
293+
pubkey_to_der(der, &temp);
303294
struct sha256 h;
304295
sha256(&h, der, sizeof(der));
305296
memcpy(&params[i].secret, &h, sizeof(h));

common/sphinx.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct onionpacket {
2323
/* Cleartext information */
2424
u8 version;
2525
u8 mac[SECURITY_PARAMETER];
26-
secp256k1_pubkey ephemeralkey;
26+
struct pubkey ephemeralkey;
2727

2828
/* Encrypted information */
2929
u8 routinginfo[ROUTING_INFO_SIZE];

0 commit comments

Comments
 (0)