Skip to content

Commit 563c017

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 150067f commit 563c017

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

lightning/src/crypto/chacha20.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,14 @@ mod real_chacha {
152152
}
153153

154154
/// Get one block from a ChaCha stream.
155-
pub fn get_single_block(key: &[u8; 32], nonce: &[u8; 16]) -> [u8; 32] {
155+
pub fn get_single_block(key: &[u8; 32], nonce: &[u8; 16]) -> [u8; 64] {
156156
let mut chacha = ChaCha20 {
157157
state: ChaCha20::expand(key, nonce),
158158
output: [0u8; BLOCK_SIZE],
159159
offset: 64,
160160
};
161-
let mut chacha_bytes = [0; 32];
162-
chacha.process_in_place(&mut chacha_bytes);
163-
chacha_bytes
161+
chacha.update();
162+
chacha.output
164163
}
165164

166165
/// Encrypts `src` into `dest` using a single block from a ChaCha stream. Passing `dest` as
@@ -585,7 +584,7 @@ mod test {
585584
let mut chacha20 = ChaCha20::new(&key, nonce_12bytes);
586585
// Seek its counter to the block at counter_pos.
587586
chacha20.seek_to_block(u32::from_le_bytes(counter_pos.try_into().unwrap()));
588-
let mut block_bytes = [0; 32];
587+
let mut block_bytes = [0; 64];
589588
chacha20.process_in_place(&mut block_bytes);
590589

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

lightning/src/sign/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2507,7 +2507,10 @@ impl EntropySource for RandomBytes {
25072507
let index = self.index.next();
25082508
let mut nonce = [0u8; 16];
25092509
nonce[..8].copy_from_slice(&index.to_be_bytes());
2510-
ChaCha20::get_single_block(&self.seed, &nonce)
2510+
let block = ChaCha20::get_single_block(&self.seed, &nonce);
2511+
let mut half_block = [0; 32];
2512+
half_block.copy_from_slice(&block[..32]);
2513+
half_block
25112514
}
25122515
}
25132516

0 commit comments

Comments
 (0)