Expand description
§RustCrypto: CCM
Pure Rust implementation of the Counter with CBC-MAC (CCM) mode (RFC 3610): an Authenticated Encryption with Associated Data (AEAD) algorithm generic over block ciphers with block size equal to 128 bits.
For example, it can be combined with AES into the various parametrizations of AES-CCM.
§Security Notes
No security audits of this crate have ever been performed, and it has not been thoroughly assessed to ensure its operation is constant-time on common CPU architectures.
USE AT YOUR OWN RISK!
§License
Licensed under either of:
at your option.
§Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
§Usage
Simple usage (allocating, no associated data):
use aes::Aes256;
use ccm::{
aead::{Aead, AeadCore, KeyInit, array::Array},
consts::{U10, U13},
Ccm,
};
// AES-256-CCM type with tag and nonce size equal to 10 and 13 bytes respectively
pub type Aes256Ccm = Ccm<Aes256, U10, U13>;
let key = Aes256Ccm::generate_key().expect("key generation failure");
let cipher = Aes256Ccm::new(&key);
let nonce = Aes256Ccm::generate_nonce().expect("nonce failure"); // MUST be unique per message
let ciphertext = cipher.encrypt(&nonce, b"plaintext message".as_ref())?;
let plaintext = cipher.decrypt(&nonce, ciphertext.as_ref())?;
assert_eq!(&plaintext, b"plaintext message");§In-place Usage (eliminates alloc requirement)
This crate has an optional alloc feature which can be disabled in e.g.
microcontroller environments that don’t have a heap.
The AeadInOut::encrypt_in_place and AeadInOut::decrypt_in_place
methods accept any type that impls the aead::Buffer trait which
contains the plaintext for encryption or ciphertext for decryption.
Enabling the arrayvec feature of this crate will provide an impl of
aead::Buffer for arrayvec::ArrayVec (re-exported from the aead crate as
[aead::arrayvec::ArrayVec]), and enabling the bytes feature of this crate will
provide an impl of aead::Buffer for bytes::BytesMut (re-exported from the
aead crate as [aead::bytes::BytesMut]).
It can then be passed as the buffer parameter to the in-place encrypt
and decrypt methods:
use aes::Aes256;
use ccm::{
aead::{AeadCore, AeadInOut, KeyInit, arrayvec::ArrayVec},
consts::{U10, U13},
Ccm,
};
// AES-256-CCM type with tag and nonce size equal to 10 and 13 bytes respectively
pub type Aes256Ccm = Ccm<Aes256, U10, U13>;
let key = Aes256Ccm::generate_key().expect("key generation failure");
let cipher = Aes256Ccm::new(&key);
let nonce = Aes256Ccm::generate_nonce().expect("nonce failure"); // MUST be unique per message
let mut buffer: ArrayVec<u8, 128> = ArrayVec::new(); // Note: buffer needs 16-bytes overhead for auth tag
buffer.try_extend_from_slice(b"plaintext message").unwrap();
// Encrypt `buffer` in-place, replacing the plaintext contents with ciphertext
cipher.encrypt_in_place(&nonce, b"", &mut buffer)?;
// `buffer` now contains the message ciphertext
assert_ne!(buffer.as_ref(), b"plaintext message");
// Decrypt `buffer` in-place, replacing its ciphertext context with the original plaintext
cipher.decrypt_in_place(&nonce, b"", &mut buffer)?;
assert_eq!(buffer.as_ref(), b"plaintext message");Re-exports§
pub use aead;
Modules§
- consts
- Type aliases for many constants.
Structs§
Traits§
- Aead
Core - Authenticated Encryption with Associated Data (AEAD) algorithm.
- Aead
InOut - In-place and inout AEAD trait which handles the authentication tag as a return value/separate parameter.
- KeyInit
- Types which can be initialized from key.
- KeySize
User - Types which use key for initialization.
- Nonce
Size - Trait implemented for valid nonce sizes, i.e.
U7,U8,U9,U10,U11,U12, andU13. - TagSize
- Trait implemented for valid tag sizes, i.e.
U4,U6,U8,U10,U12,U14, andU16.
Type Aliases§
- Key
- Key used by
KeySizeUserimplementors. - Nonce
- CCM nonces
- Tag
- CCM tags