Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ccm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ arrayvec = ["aead/arrayvec"]
bytes = ["aead/bytes"]
getrandom = ["aead/getrandom"]
rand_core = ["aead/rand_core"]

[lints]
workspace = true

[package.metadata.docs.rs]
all-features = true
25 changes: 19 additions & 6 deletions ccm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc = include_str!("../README.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg"
)]
#![deny(unsafe_code)]
#![warn(missing_docs, rust_2018_idioms)]

//! # Usage
//!
Expand Down Expand Up @@ -102,7 +101,7 @@ use aead::{
use cipher::{
Block, BlockCipherEncrypt, BlockSizeUser, InnerIvInit, StreamCipher, StreamCipherSeek,
};
use core::marker::PhantomData;
use core::{fmt, marker::PhantomData};
use ctr::{Ctr32BE, Ctr64BE, CtrCore};
use subtle::ConstantTimeEq;

Expand Down Expand Up @@ -174,7 +173,7 @@ where
) -> Result<Tag<C::BlockSize>, Error> {
let is_ad = !adata.is_empty();
let l = N::get_l();
let flags = 64 * (is_ad as u8) + 8 * M::get_m_tick() + (l - 1);
let flags = 64 * u8::from(is_ad) + 8 * M::get_m_tick() + (l - 1);

if buffer.len() > N::get_max_len() {
return Err(Error);
Expand All @@ -186,8 +185,10 @@ where
b0[1..n].copy_from_slice(nonce);

let cb = b0.len() - n;

// the max len check makes certain that we discard only
// zero bytes from `b`
#[allow(clippy::cast_possible_truncation, reason = "TODO")]
if cb > 4 {
let b = (buffer.len() as u64).to_be_bytes();
b0[n..].copy_from_slice(&b[b.len() - cb..]);
Expand Down Expand Up @@ -292,7 +293,7 @@ where
ctr.apply_keystream_inout(buffer);
}

Ok(Tag::try_from(&full_tag[..M::to_usize()]).expect("tag size mismatch"))
full_tag[..M::to_usize()].try_into().map_err(|_| Error)
}

fn decrypt_inout_detached(
Expand Down Expand Up @@ -335,6 +336,17 @@ where
}
}

impl<C, M, N> fmt::Debug for Ccm<C, M, N>
where
C: BlockSizeUser<BlockSize = U16> + BlockCipherEncrypt,
M: ArraySize + TagSize,
N: ArraySize + NonceSize,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Ccm").finish_non_exhaustive()
}
}

struct CbcMac<'a, C: BlockCipherEncrypt> {
cipher: &'a C,
state: Block<C>,
Expand Down Expand Up @@ -378,14 +390,15 @@ where
}
}

#[allow(clippy::cast_possible_truncation, reason = "TODO")]
fn fill_aad_header(adata_len: usize) -> (usize, Array<u8, U16>) {
debug_assert_ne!(adata_len, 0);

let mut b = Array::<u8, U16>::default();
let n = if adata_len < 0xFF00 {
b[..2].copy_from_slice(&(adata_len as u16).to_be_bytes());
2
} else if adata_len <= u32::MAX as usize {
} else if u32::try_from(adata_len).is_ok() {
b[0] = 0xFF;
b[1] = 0xFE;
b[2..6].copy_from_slice(&(adata_len as u32).to_be_bytes());
Expand Down
2 changes: 1 addition & 1 deletion ccm/src/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub trait SealedNonce: Unsigned {
fn get_max_len() -> usize {
// a somewhat ugly code to prevent overflow.
// compiler should be able to completely optimize it out
let l = Self::get_l() as u128;
let l = u128::from(Self::get_l());
let v = (1 << (8 * l)) - 1;
core::cmp::min(v, usize::MAX as u128) as usize
}
Expand Down
10 changes: 7 additions & 3 deletions ccm/tests/mod.rs → ccm/tests/ccm.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! CCM integration tests.

#![cfg(feature = "alloc")]

use aead::{Aead, AeadInOut, KeyInit, Payload, array::Array};
Expand All @@ -13,7 +15,7 @@ fn test_data_len_check() {
let key = hex!("D7828D13B2B0BDC325A76236DF93CC6B");
let nonce = hex!("2F1DBD38CE3EDA7C23F04DD650");

type Cipher = Ccm<aes::Aes128, U10, U13>;
type Cipher = Ccm<Aes128, U10, U13>;
let key = Array(key);
let nonce = Array(nonce);
let c = Cipher::new(&key);
Expand All @@ -37,7 +39,7 @@ fn sp800_38c_examples() {
nonce: $nonce:expr, adata: $adata:expr, pt: $pt:expr, ct: $ct:expr,
) => {
let key = Array($key);
let c = Ccm::<aes::Aes128, $m, $n>::new(&key);
let c = Ccm::<Aes128, $m, $n>::new(&key);
let nonce = Array($nonce);
let res = c.encrypt(&nonce, Payload { aad: &$adata, msg: &$pt })
.unwrap();
Expand Down Expand Up @@ -80,7 +82,9 @@ fn sp800_38c_examples() {
"),
);

let adata = (0..524288 / 8).map(|i| i as u8).collect::<Vec<u8>>();
#[allow(clippy::cast_possible_truncation, reason = "TODO")]
let adata = (0u32..524288 / 8).map(|i| i as u8).collect::<Vec<u8>>();

check!(
key, U14, U13,
nonce: hex!("10111213 14151617 18191a1b 1c"),
Expand Down