Skip to content

Commit

Permalink
Merge pull request #68 from RustCrypto/chacha20/security-fix-issue-64
Browse files Browse the repository at this point in the history
[SECURITY] chacha20: ensure block counter < MAX_BLOCKS (fixes #64)
  • Loading branch information
tarcieri committed Oct 23, 2019
2 parents 0f63178 + 20d0a20 commit 661bdf4
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions chacha20/src/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use byteorder::{ByteOrder, LE};
use salsa20_core::{SalsaFamilyCipher, IV_WORDS, KEY_WORDS, STATE_WORDS};
use super::MAX_BLOCKS;

/// ChaCha20 core cipher functionality
#[derive(Debug)]
Expand Down Expand Up @@ -43,6 +44,9 @@ impl SalsaFamilyCipher for Cipher {
#[inline]
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn block(&self, counter: u64) -> [u32; STATE_WORDS] {
// TODO(tarcieri): avoid panic by making block API fallible
assert!(counter < MAX_BLOCKS as u64, "MAX_BLOCKS exceeded");

if cfg!(target_feature = "sse2") {
unsafe {
super::block::sse2::Block::generate(
Expand All @@ -59,6 +63,8 @@ impl SalsaFamilyCipher for Cipher {
#[inline]
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
fn block(&self, counter: u64) -> [u32; STATE_WORDS] {
// TODO(tarcieri): avoid panic by making block API fallible
assert!(counter < MAX_BLOCKS as u64, "MAX_BLOCKS exceeded");
super::block::Block::generate(&self.key, self.iv, self.counter_offset + counter)
}
}

0 comments on commit 661bdf4

Please sign in to comment.