From beb702054b42a2ecccf10445da9d174d35222d0a Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 7 Oct 2015 13:08:04 +1030 Subject: [PATCH] test_onion: minor protocol change; use single SHA to create both IVs. Suggested-by: Anthony Towns Signed-off-by: Rusty Russell --- test/test_onion.c | 25 ++++++++----------------- test/test_onion.py | 2 +- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/test/test_onion.c b/test/test_onion.c index 21b9aafe4..7a44173b9 100644 --- a/test/test_onion.c +++ b/test/test_onion.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -73,22 +74,14 @@ static struct hmackey hmackey_from_secret(const unsigned char secret[32]) } -static struct iv iv_from_secret(const unsigned char secret[32]) +static void ivs_from_secret(const unsigned char secret[32], + struct iv *iv, struct iv *pad_iv) { - struct iv iv; struct sha256 sha; sha_with_seed(secret, 2, &sha); - memcpy(iv.iv, sha.u.u8, sizeof(iv.iv)); - return iv; -} - -static struct iv pad_iv_from_secret(const unsigned char secret[32]) -{ - struct iv iv; - struct sha256 sha; - sha_with_seed(secret, 3, &sha); - memcpy(iv.iv, sha.u.u8, sizeof(iv.iv)); - return iv; + BUILD_ASSERT(sizeof(*iv) + sizeof(*pad_iv) == sizeof(sha)); + memcpy(iv->iv, sha.u.u8, sizeof(iv->iv)); + memcpy(pad_iv->iv, sha.u.u8 + sizeof(iv->iv), sizeof(pad_iv->iv)); } /* Not really! */ @@ -411,8 +404,7 @@ bool create_onion(const secp256k1_pubkey pubkey[], hmackeys[i] = hmackey_from_secret(memcheck(secret, 32)); enckeys[i] = enckey_from_secret(secret); - ivs[i] = iv_from_secret(secret); - pad_ivs[i] = pad_iv_from_secret(secret); + ivs_from_secret(secret, &ivs[i], &pad_ivs[i]); } /* @@ -525,8 +517,7 @@ bool decrypt_onion(const struct seckey *myseckey, struct onion *onion, hmackey = hmackey_from_secret(secret); *enckey = enckey_from_secret(secret); - iv = iv_from_secret(secret); - *pad_iv = pad_iv_from_secret(secret); + ivs_from_secret(secret, &iv, pad_iv); /* Check HMAC. */ #if 0 diff --git a/test/test_onion.py b/test/test_onion.py index e346ea1a1..c631f5e08 100644 --- a/test/test_onion.py +++ b/test/test_onion.py @@ -175,7 +175,7 @@ class Onion(object): enckey = cls.tweak_sha(sec, b'\x00')[:16] hmac = cls.tweak_sha(sec, b'\x01') iv = cls.tweak_sha(sec, b'\x02')[:16] - pad_iv = cls.tweak_sha(sec, b'\x03')[:16] + pad_iv = cls.tweak_sha(sec, b'\x02')[16:] return enckey, hmac, iv, pad_iv