Skip to content

Commit

Permalink
sphinx: Switch to big-endian number encoding
Browse files Browse the repository at this point in the history
See lightning/bolts#619 and
lightning/bolts#619 for discussion.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
  • Loading branch information
cdecker committed Jul 26, 2019
1 parent 01b7fdd commit ede7f88
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
62 changes: 62 additions & 0 deletions bitcoin/varint.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,65 @@ size_t varint_get(const u8 *p, size_t max, varint_t *val)
return 1;
}
}

size_t bigsize_put(u8 buf[VARINT_MAX_LEN], varint_t v)
{
u8 *p = buf;

if (v < 0xfd) {
*(p++) = v;
} else if (v <= 0xffff) {
(*p++) = 0xfd;
(*p++) = v >> 8;
(*p++) = v;
} else if (v <= 0xffffffff) {
(*p++) = 0xfe;
(*p++) = v >> 24;
(*p++) = v >> 16;
(*p++) = v >> 8;
(*p++) = v;
} else {
(*p++) = 0xff;
(*p++) = v >> 56;
(*p++) = v >> 48;
(*p++) = v >> 40;
(*p++) = v >> 32;
(*p++) = v >> 24;
(*p++) = v >> 16;
(*p++) = v >> 8;
(*p++) = v;
}
return p - buf;
}

size_t bigsize_get(const u8 *p, size_t max, varint_t *val)
{
if (max < 1)
return 0;

switch (*p) {
case 0xfd:
if (max < 3)
return 0;
*val = ((u64)p[1] << 8) + p[2];
return 3;
case 0xfe:
if (max < 5)
return 0;
*val = ((u64)p[1] << 24) + ((u64)p[2] << 16)
+ ((u64)p[3] << 8) + p[4];
return 5;
case 0xff:
if (max < 9)
return 0;
*val = ((u64)p[1] << 56) + ((u64)p[2] << 48)
+ ((u64)p[3] << 40) + ((u64)p[4] << 32)
+ ((u64)p[5] << 24) + ((u64)p[6] << 16)
+ ((u64)p[7] << 8) + p[8];
return 9;
default:
*val = *p;
return 1;
}

}
9 changes: 9 additions & 0 deletions bitcoin/varint.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@ size_t varint_put(u8 buf[VARINT_MAX_LEN], varint_t v);

/* Returns bytes used: 0 if max_len too small. */
size_t varint_get(const u8 *p, size_t max_len, varint_t *val);


/* Big-endian variant of varint_put, used in lightning */
size_t bigsize_put(u8 buf[VARINT_MAX_LEN], varint_t v);

/* Big-endian variant of varint_get, used in lightning */
size_t bigsize_get(const u8 *p, size_t max, varint_t *val);


#endif /* LIGHTNING_BITCOIN_VARINT_H */
4 changes: 2 additions & 2 deletions common/sphinx.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ static bool sphinx_write_frame(u8 *dest, const struct sphinx_hop *hop)
if (hop->type == SPHINX_V0_PAYLOAD)
dest[pos++] = 0x00;
else
pos += varint_put(dest+pos, raw_size);
pos += bigsize_put(dest+pos, raw_size);

memcpy(dest + pos, hop->payload, raw_size);
pos += raw_size;
Expand Down Expand Up @@ -610,7 +610,7 @@ struct route_step *process_onionpacket(
} else {
/* In addition to the raw payload we need to also shift the
* length encoding itself and the HMAC away. */
vsize = varint_get(paddedheader, 3, &shift_size);
vsize = bigsize_get(paddedheader, 3, &shift_size);
shift_size += vsize + HMAC_SIZE;
}

Expand Down

0 comments on commit ede7f88

Please sign in to comment.