commit 6fe6f715d9a1b736f353107f8f266daaf9287149 Author: Aydin Mercan Date: Fri Mar 24 18:05:14 2023 +0300 0.0.1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4fffb2f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d4b5439 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "caead" +version = "0.0.1" +description = "Committing AEAD constructions from existing AEADs." +authors = ["Aydin Mercan "] +edition = "2021" +license = "BSD-3-Clause" +readme = "README.md" +documentation = "https://docs.rs/caead" +keywords = ["aead", "key-commitment"] +categories = ["cryptography", "no-std"] +rust-version = "1.68" + +[dependencies] +aead = { version = "0.5", default-features = false } +digest = { version = "0.10", default-features = false } + +[features] +std = ["aead/std", "digest/std"] +alloc = ["aead/alloc", "digest/alloc"] + +[package.metadata.docs.rs] +all-features = true diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ebd5d71 --- /dev/null +++ b/LICENSE @@ -0,0 +1,11 @@ +Copyright 2023 Aydin Mercan + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..2eb1309 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# cAEAD + +![BSD-3-Clause][license-image] + +Key committing AEAD constructions from existing AEADs. + +## License + +Licensed under the BSD-3-Clause. + +[//]: # (badges) + +[license-image]: https://img.shields.io/badge/license-BSD--3--Clause-blue diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..656b0d7 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,85 @@ +#![no_std] + +//! Committing AEAD constructions. Allows one to construct key/fully committing AEAD constructions +//! from [existing AEADs]. +//! +//! [existing AEADS]: aead + +use core::marker::PhantomData; + +use aead::generic_array::GenericArray; +use aead::{AeadCore, AeadInPlace, Error, KeyInit, KeySizeUser, Nonce, Tag}; +use digest::Digest; + +/// CTX key committing AEAD construction from [“On Committing Authenticated Encryption” by John Chan and Phillip Rogaway](https://eprint.iacr.org/2022/1260). +pub struct CtxCommitment +where + A: AeadCore + KeyInit, + H: Digest, +{ + key: GenericArray, + naead: A, + _digest: PhantomData, +} + +impl AeadCore for CtxCommitment +where + A: AeadCore + KeyInit, + H: Digest, +{ + type NonceSize = A::NonceSize; + type TagSize = H::OutputSize; + type CiphertextOverhead = A::CiphertextOverhead; +} + +impl KeySizeUser for CtxCommitment +where + A: AeadCore + KeyInit, + H: Digest, +{ + type KeySize = A::KeySize; +} + +impl KeyInit for CtxCommitment +where + A: AeadCore + KeyInit, + H: Digest, +{ + fn new(key: &GenericArray) -> Self { + Self { + key: key.clone(), + naead: A::new(key), + _digest: PhantomData, + } + } +} + +impl CtxCommitment +where + A: AeadCore + KeyInit + AeadInPlace, + H: Digest, +{ + /// Currently not in [`AeadInPlace`] due to the inability to decrypt without verifying + /// the tag. + pub fn encrypt_in_place_detached( + &self, + nonce: &Nonce, + associated_data: &[u8], + buffer: &mut [u8], + ) -> Result, Error> { + let mut tag = Tag::::default(); + + let ntag = self + .naead + .encrypt_in_place_detached(nonce, associated_data, buffer)?; + + H::new() + .chain_update(&self.key) + .chain_update(nonce) + .chain_update(associated_data) + .chain_update(ntag) + .finalize_into(&mut tag); + + Ok(tag) + } +}