From 8599d6325653b0fdcbc68d36d30d571ef23e928e Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 16 Nov 2016 14:02:56 +1030 Subject: [PATCH 1/4] sphinx: add brackets around constant definition Always do this, otherwise "MACRO * foo" can have unexpected results. Signed-off-by: Rusty Russell --- daemon/sphinx.c | 2 +- daemon/sphinx.h | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/daemon/sphinx.c b/daemon/sphinx.c index 9ad7fdcbc..f31405a11 100644 --- a/daemon/sphinx.c +++ b/daemon/sphinx.c @@ -12,7 +12,7 @@ #define BLINDING_FACTOR_SIZE 32 #define SHARED_SECRET_SIZE 32 -#define NUM_STREAM_BYTES (2 * NUM_MAX_HOPS + 2) * SECURITY_PARAMETER +#define NUM_STREAM_BYTES ((2 * NUM_MAX_HOPS + 2) * SECURITY_PARAMETER) #define KEY_LEN 32 struct hop_params { diff --git a/daemon/sphinx.h b/daemon/sphinx.h index 0109391a9..d9253b8bc 100644 --- a/daemon/sphinx.h +++ b/daemon/sphinx.h @@ -13,11 +13,11 @@ #define SECURITY_PARAMETER 20 #define NUM_MAX_HOPS 20 #define HOP_PAYLOAD_SIZE 20 -#define TOTAL_HOP_PAYLOAD_SIZE NUM_MAX_HOPS * HOP_PAYLOAD_SIZE +#define TOTAL_HOP_PAYLOAD_SIZE (NUM_MAX_HOPS * HOP_PAYLOAD_SIZE) #define MESSAGE_SIZE 0 -#define ROUTING_INFO_SIZE 2 * NUM_MAX_HOPS * SECURITY_PARAMETER -#define TOTAL_PACKET_SIZE 1 + 33 + SECURITY_PARAMETER + ROUTING_INFO_SIZE + \ - TOTAL_HOP_PAYLOAD_SIZE + MESSAGE_SIZE +#define ROUTING_INFO_SIZE (2 * NUM_MAX_HOPS * SECURITY_PARAMETER) +#define TOTAL_PACKET_SIZE (1 + 33 + SECURITY_PARAMETER + ROUTING_INFO_SIZE + \ + TOTAL_HOP_PAYLOAD_SIZE + MESSAGE_SIZE) struct onionpacket { /* Cleartext information */ From 6f9dedbe7f67ce27715a3636f0791f522f1bc0af Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 16 Nov 2016 14:04:50 +1030 Subject: [PATCH 2/4] sphinx: remove redundant initialization Signed-off-by: Rusty Russell --- daemon/sphinx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/daemon/sphinx.c b/daemon/sphinx.c index f31405a11..701a51ff0 100644 --- a/daemon/sphinx.c +++ b/daemon/sphinx.c @@ -128,7 +128,7 @@ static void serialize_hoppayload(u8 *dst, struct hoppayload *hp) static void xorbytes(uint8_t *d, const uint8_t *a, const uint8_t *b, size_t len) { - size_t i = 0; + size_t i; for (i = 0; i < len; i++) d[i] = a[i] ^ b[i]; From 423572190be5d3e3b3fd1b0550c78a5fec246ecb Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 16 Nov 2016 14:11:08 +1030 Subject: [PATCH 3/4] sphinx: use assignment instead of memcpy where possible. Signed-off-by: Rusty Russell --- daemon/sphinx.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/daemon/sphinx.c b/daemon/sphinx.c index 701a51ff0..35f99866b 100644 --- a/daemon/sphinx.c +++ b/daemon/sphinx.c @@ -248,7 +248,7 @@ static bool blind_group_element( { /* tweak_mul is inplace so copy first. */ if (pubkey != blindedelement) - memcpy(blindedelement, pubkey, sizeof(secp256k1_pubkey)); + *blindedelement = *pubkey; if (secp256k1_ec_pubkey_tweak_mul(secpctx, blindedelement, blind) != 1) return false; return true; @@ -264,7 +264,7 @@ static bool create_shared_secret( secp256k1_pubkey pkcopy; u8 ecres[33]; - memcpy(&pkcopy, pubkey, sizeof(pkcopy)); + pkcopy = *pubkey; if (secp256k1_ec_pubkey_tweak_mul(secpctx, &pkcopy, sessionkey) != 1) return false; @@ -346,7 +346,7 @@ static struct hop_params *generate_hop_params( * Order is indifferent, multiplication is commutative. */ memcpy(&blind, sessionkey, 32); - memcpy(&temp, &path[i], sizeof(temp)); + temp = path[i].pubkey; if (!blind_group_element(secpctx, &temp, &temp, blind)) return NULL; for (j = 0; j < i; j++) From 41299b679c32b79499ddd0011b5a3b1ac3623014 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 16 Nov 2016 14:18:59 +1030 Subject: [PATCH 4/4] sphinx: process_onionpacket doesn't modify the packet. Make it const. Signed-off-by: Rusty Russell --- daemon/sphinx.c | 12 ++++++------ daemon/sphinx.h | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/daemon/sphinx.c b/daemon/sphinx.c index 35f99866b..8917528dc 100644 --- a/daemon/sphinx.c +++ b/daemon/sphinx.c @@ -181,7 +181,7 @@ static bool compute_hmac( return true; } -static void compute_packet_hmac(struct onionpacket *packet, u8 *mukey, u8 *hmac) +static void compute_packet_hmac(const struct onionpacket *packet, u8 *mukey, u8 *hmac) { u8 mactemp[ROUTING_INFO_SIZE + TOTAL_HOP_PAYLOAD_SIZE + MESSAGE_SIZE]; @@ -222,8 +222,8 @@ static bool generate_header_padding( } static void compute_blinding_factor(secp256k1_context *secpctx, - secp256k1_pubkey *key, - u8 sharedsecret[SHARED_SECRET_SIZE], + const secp256k1_pubkey *key, + const u8 sharedsecret[SHARED_SECRET_SIZE], u8 res[BLINDING_FACTOR_SIZE]) { struct sha256_ctx ctx; @@ -243,8 +243,8 @@ static void compute_blinding_factor(secp256k1_context *secpctx, static bool blind_group_element( secp256k1_context *secpctx, secp256k1_pubkey *blindedelement, - secp256k1_pubkey *pubkey, - u8 blind[BLINDING_FACTOR_SIZE]) + const secp256k1_pubkey *pubkey, + const u8 blind[BLINDING_FACTOR_SIZE]) { /* tweak_mul is inplace so copy first. */ if (pubkey != blindedelement) @@ -464,7 +464,7 @@ struct onionpacket *create_onionpacket( struct route_step *process_onionpacket( const tal_t *ctx, secp256k1_context *secpctx, - struct onionpacket *msg, + const struct onionpacket *msg, struct privkey *hop_privkey ) { diff --git a/daemon/sphinx.h b/daemon/sphinx.h index d9253b8bc..f688cb135 100644 --- a/daemon/sphinx.h +++ b/daemon/sphinx.h @@ -87,7 +87,7 @@ struct onionpacket *create_onionpacket( struct route_step *process_onionpacket( const tal_t * ctx, secp256k1_context * secpctx, - struct onionpacket *packet, + const struct onionpacket *packet, struct privkey *hop_privkey );