|
12 | 12 | #include <err.h>
|
13 | 13 | #include <stdbool.h>
|
14 | 14 | #include <assert.h>
|
| 15 | +#include <ccan/build_assert/build_assert.h> |
15 | 16 | #include <ccan/tal/tal.h>
|
16 | 17 | #include <ccan/mem/mem.h>
|
17 | 18 | #include <ccan/crypto/sha256/sha256.h>
|
@@ -73,22 +74,14 @@ static struct hmackey hmackey_from_secret(const unsigned char secret[32])
|
73 | 74 | }
|
74 | 75 |
|
75 | 76 |
|
76 |
| -static struct iv iv_from_secret(const unsigned char secret[32]) |
| 77 | +static void ivs_from_secret(const unsigned char secret[32], |
| 78 | + struct iv *iv, struct iv *pad_iv) |
77 | 79 | {
|
78 |
| - struct iv iv; |
79 | 80 | struct sha256 sha;
|
80 | 81 | sha_with_seed(secret, 2, &sha);
|
81 |
| - memcpy(iv.iv, sha.u.u8, sizeof(iv.iv)); |
82 |
| - return iv; |
83 |
| -} |
84 |
| - |
85 |
| -static struct iv pad_iv_from_secret(const unsigned char secret[32]) |
86 |
| -{ |
87 |
| - struct iv iv; |
88 |
| - struct sha256 sha; |
89 |
| - sha_with_seed(secret, 3, &sha); |
90 |
| - memcpy(iv.iv, sha.u.u8, sizeof(iv.iv)); |
91 |
| - return iv; |
| 82 | + BUILD_ASSERT(sizeof(*iv) + sizeof(*pad_iv) == sizeof(sha)); |
| 83 | + memcpy(iv->iv, sha.u.u8, sizeof(iv->iv)); |
| 84 | + memcpy(pad_iv->iv, sha.u.u8 + sizeof(iv->iv), sizeof(pad_iv->iv)); |
92 | 85 | }
|
93 | 86 |
|
94 | 87 | /* Not really! */
|
@@ -411,8 +404,7 @@ bool create_onion(const secp256k1_pubkey pubkey[],
|
411 | 404 |
|
412 | 405 | hmackeys[i] = hmackey_from_secret(memcheck(secret, 32));
|
413 | 406 | enckeys[i] = enckey_from_secret(secret);
|
414 |
| - ivs[i] = iv_from_secret(secret); |
415 |
| - pad_ivs[i] = pad_iv_from_secret(secret); |
| 407 | + ivs_from_secret(secret, &ivs[i], &pad_ivs[i]); |
416 | 408 | }
|
417 | 409 |
|
418 | 410 | /*
|
@@ -525,8 +517,7 @@ bool decrypt_onion(const struct seckey *myseckey, struct onion *onion,
|
525 | 517 |
|
526 | 518 | hmackey = hmackey_from_secret(secret);
|
527 | 519 | *enckey = enckey_from_secret(secret);
|
528 |
| - iv = iv_from_secret(secret); |
529 |
| - *pad_iv = pad_iv_from_secret(secret); |
| 520 | + ivs_from_secret(secret, &iv, pad_iv); |
530 | 521 |
|
531 | 522 | /* Check HMAC. */
|
532 | 523 | #if 0
|
|
0 commit comments