Skip to content

Commit 224008d

Browse files
committed
Make ChaCha20::get_single_block return a full, single block
While the current uses for `ChaCha20::get_single_block` only actually want 32 bytes, a ChaCha20 block is 64 bytes, and future uses may want another 32 bytes, so we can go ahead and return the whole block when asked for one.
1 parent 6777783 commit 224008d

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

lightning/src/crypto/chacha20.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,10 @@ mod real_chacha {
150150
}
151151

152152
/// Get one block from a ChaCha stream.
153-
pub fn get_single_block(key: &[u8; 32], nonce: &[u8; 16]) -> [u8; 32] {
153+
pub fn get_single_block(key: &[u8; 32], nonce: &[u8; 16]) -> [u8; 64] {
154154
let mut chacha = ChaCha20 { state: ChaCha20::expand(key, nonce), output: [0u8; BLOCK_SIZE], offset: 64 };
155-
let mut chacha_bytes = [0; 32];
156-
chacha.process_in_place(&mut chacha_bytes);
157-
chacha_bytes
155+
chacha.update();
156+
chacha.output
158157
}
159158

160159
/// Encrypts `src` into `dest` using a single block from a ChaCha stream. Passing `dest` as
@@ -633,7 +632,7 @@ mod test {
633632
let mut chacha20 = ChaCha20::new(&key, nonce_12bytes);
634633
// Seek its counter to the block at counter_pos.
635634
chacha20.seek_to_block(u32::from_le_bytes(counter_pos.try_into().unwrap()));
636-
let mut block_bytes = [0; 32];
635+
let mut block_bytes = [0; 64];
637636
chacha20.process_in_place(&mut block_bytes);
638637

639638
assert_eq!(ChaCha20::get_single_block(&key, &nonce_16bytes), block_bytes);

lightning/src/sign/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,10 @@ impl EntropySource for InMemorySigner {
10721072
let index = self.rand_bytes_index.get_increment();
10731073
let mut nonce = [0u8; 16];
10741074
nonce[..8].copy_from_slice(&index.to_be_bytes());
1075-
ChaCha20::get_single_block(&self.rand_bytes_unique_start, &nonce)
1075+
let block = ChaCha20::get_single_block(&self.rand_bytes_unique_start, &nonce);
1076+
let mut half_block = [0; 32];
1077+
half_block.copy_from_slice(&block[..32]);
1078+
half_block
10761079
}
10771080
}
10781081

@@ -1634,7 +1637,10 @@ impl EntropySource for KeysManager {
16341637
let index = self.rand_bytes_index.get_increment();
16351638
let mut nonce = [0u8; 16];
16361639
nonce[..8].copy_from_slice(&index.to_be_bytes());
1637-
ChaCha20::get_single_block(&self.rand_bytes_unique_start, &nonce)
1640+
let block = ChaCha20::get_single_block(&self.rand_bytes_unique_start, &nonce);
1641+
let mut half_block = [0; 32];
1642+
half_block.copy_from_slice(&block[..32]);
1643+
half_block
16381644
}
16391645
}
16401646

0 commit comments

Comments
 (0)