ascon: add no_std support (#36)

The permutation does not have any `std`/`alloc` dependency.

The current AEAD interface requires `alloc` (though we should migrate it
to use the `aead` crate anyway).
This commit is contained in:
Tony Arcieri 2023-02-11 12:28:02 -07:00 committed by GitHub
parent ace9dd7723
commit e4c0b56c7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 16 deletions

View File

@ -35,6 +35,28 @@ jobs:
override: true
- run: cargo build --benches
build:
needs: set-msrv
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- ${{needs.set-msrv.outputs.msrv}}
- stable
target:
- thumbv7em-none-eabi
- wasm32-unknown-unknown
steps:
- uses: actions/checkout@v3
- uses: RustCrypto/actions/cargo-cache@master
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
target: ${{ matrix.target }}
override: true
- run: cargo build --no-default-features --target ${{ matrix.target }}
minimal-versions:
uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master
with:
@ -56,4 +78,5 @@ jobs:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
- run: cargo test --no-default-features
- run: cargo test

View File

@ -16,3 +16,11 @@ edition = "2018"
[dependencies]
byteorder = { version = "1.0", default-features = false }
[features]
default = ["alloc"]
alloc = []
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

View File

@ -1,6 +1,7 @@
//! Authenticated Encryption with Associated Data.
use crate::{finalization, initialization, permutation, util, B, KEY_LEN, RATE, S_SIZE};
use crate::{finalization, initialization, permutation, B, KEY_LEN, RATE, S_SIZE};
use alloc::{vec, vec::Vec};
/// Decryption errors.
#[derive(Debug)]
@ -117,13 +118,26 @@ pub fn decrypt(
// finalization
finalization(&mut ss, key);
if util::eq(&ss[S_SIZE - KEY_LEN..], tag) {
if ct_eq(&ss[S_SIZE - KEY_LEN..], tag) {
Ok(mm[..ciphertext.len()].into())
} else {
Err(DecryptFail::AuthenticationFail)
}
}
// TODO(tarcieri): use `subtle`
fn ct_eq(a: &[u8], b: &[u8]) -> bool {
if a.len() != b.len() {
false
} else {
a.iter()
.zip(b)
.map(|(x, y)| x ^ y)
.fold(0, |sum, next| sum | next)
.eq(&0)
}
}
fn process_aad(ss: &mut [u8], aa: &[u8], s: usize) {
for i in 0..s {
for j in 0..RATE {
@ -136,7 +150,6 @@ fn process_aad(ss: &mut [u8], aa: &[u8], s: usize) {
#[cfg(test)]
mod tests {
use super::{decrypt, encrypt};
use crate::util;
#[test]
fn round_trip() {
@ -148,7 +161,6 @@ mod tests {
let (ciphertext, tag) = encrypt(&key, &iv, &message, &aad);
let plaintext = decrypt(&key, &iv, &ciphertext, &aad, &tag).unwrap();
assert_eq!(plaintext, &message[..]);
assert!(util::eq(&message, &plaintext));
}
#[test]

View File

@ -20,6 +20,7 @@
//! [NIST Lightweight Cryptography]: https://csrc.nist.gov/projects/lightweight-cryptography/finalists
//! [CAESAR competition]: https://competitions.cr.yp.to/caesar-submissions.html
#![no_std]
#![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"
@ -33,6 +34,10 @@
unused_qualifications
)]
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "alloc")]
pub mod aead;
mod util;

View File

@ -15,15 +15,3 @@ pub fn u64_to_u8(input: &[u64], output: &mut [u8]) {
Endian::write_u64(&mut output[(i * 8)..((i + 1) * 8)], b)
}
}
pub fn eq(a: &[u8], b: &[u8]) -> bool {
if a.len() != b.len() {
false
} else {
a.iter()
.zip(b)
.map(|(x, y)| x ^ y)
.fold(0, |sum, next| sum | next)
.eq(&0)
}
}