Skip to content

Commit

Permalink
universal-hash: bump crypto-common to v0.2.0-pre; MSRV 1.65 (#1385)
Browse files Browse the repository at this point in the history
Replaces `generic-array` with `hybrid-array`, which is built on a
combination of `typenum` and const generics, providing a degree of
interoperability between the two systems.
  • Loading branch information
tarcieri authored Nov 12, 2023
1 parent ddbbc90 commit 8f77112
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 117 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/universal-hash.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
strategy:
matrix:
rust:
- 1.56.0 # MSRV
- 1.65.0 # MSRV
- stable
target:
- thumbv7em-none-eabi
Expand All @@ -36,17 +36,18 @@ jobs:
targets: ${{ matrix.target }}
- run: cargo build --no-default-features --release --target ${{ matrix.target }}

minimal-versions:
uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master
with:
working-directory: ${{ github.workflow }}
# TODO(tarcieri): re-enable after next `crypto-common` release
# minimal-versions:
# uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master
# with:
# working-directory: ${{ github.workflow }}

test:
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- 1.56.0 # MSRV
- 1.65.0 # MSRV
- stable
steps:
- uses: actions/checkout@v4
Expand Down
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ members = [
"elliptic-curve",
"kem",
"password-hash",
"universal-hash",
]
# TODO: re-add to `members` when MSRV has been bumped to 1.60+
exclude = [
"signature",
"signature/async",
"universal-hash",
]

[patch.crates-io]
Expand Down
2 changes: 1 addition & 1 deletion crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ digest = { version = "0.10", optional = true, features = ["mac"] }
elliptic-curve = { version = "0.13", optional = true, path = "../elliptic-curve" }
password-hash = { version = "0.5", optional = true, path = "../password-hash" }
signature = { version = "2", optional = true, default-features = false, path = "../signature" }
universal-hash = { version = "0.5", optional = true, path = "../universal-hash" }
universal-hash = { version = "0.5", optional = true }

[features]
std = [
Expand Down
49 changes: 0 additions & 49 deletions universal-hash/Cargo.lock

This file was deleted.

6 changes: 3 additions & 3 deletions universal-hash/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
[package]
name = "universal-hash"
version = "0.5.1"
version = "0.6.0-pre"
description = "Traits which describe the functionality of universal hash functions (UHFs)"
authors = ["RustCrypto Developers"]
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.56"
rust-version = "1.65"
readme = "README.md"
documentation = "https://docs.rs/universal-hash"
repository = "https://github.com/RustCrypto/traits"
keywords = ["crypto", "mac"]
categories = ["cryptography", "no-std"]

[dependencies]
crypto-common = "0.1.6"
crypto-common = "=0.2.0-pre"
subtle = { version = "2.4", default-features = false }

[features]
Expand Down
4 changes: 2 additions & 2 deletions universal-hash/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ See [RustCrypto/universal-hashes] for implementations which use this trait.

## Minimum Supported Rust Version

Rust **1.56** or higher.
Rust **1.65** or higher.

Minimum supported Rust version can be changed in the future, but it will be
done with a minor version bump.
Expand Down Expand Up @@ -47,7 +47,7 @@ dual licensed as above, without any additional terms or conditions.
[docs-image]: https://docs.rs/universal-hash/badge.svg
[docs-link]: https://docs.rs/universal-hash/
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
[rustc-image]: https://img.shields.io/badge/rustc-1.56+-blue.svg
[rustc-image]: https://img.shields.io/badge/rustc-1.65+-blue.svg
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg
[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260051-universal-hashes
[build-image]: https://github.com/RustCrypto/traits/workflows/universal-hash/badge.svg?branch=master&event=push
Expand Down
57 changes: 12 additions & 45 deletions universal-hash/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,24 @@
//! Traits for [Universal Hash Functions].
//!
//! # About universal hashes
//!
//! Universal hash functions provide a "universal family" of possible
//! hash functions where a given member of a family is selected by a key.
//!
//! They are well suited to the purpose of "one time authenticators" for a
//! sequence of bytestring inputs, as their construction has a number of
//! desirable properties such as pairwise independence as well as amenability
//! to efficient implementations, particularly when implemented using SIMD
//! instructions.
//!
//! When combined with a cipher, such as in Galois/Counter Mode (GCM) or the
//! Salsa20 family AEAD constructions, they can provide the core functionality
//! for a Message Authentication Code (MAC).
//!
//! [Universal Hash Functions]: https://en.wikipedia.org/wiki/Universal_hashing

#![no_std]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![doc = include_str!("../README.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg"
)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![deny(unsafe_code)]
#![forbid(unsafe_code)]
#![warn(missing_docs, rust_2018_idioms)]

#[cfg(feature = "std")]
extern crate std;

pub use crypto_common::{
self, generic_array,
self, array,
typenum::{self, consts},
Block, Key, KeyInit, ParBlocks, Reset,
};

use core::slice;
use crypto_common::{BlockSizeUser, ParBlocksSizeUser};
use generic_array::{ArrayLength, GenericArray};
use crypto_common::{array::Array, BlockSizeUser, BlockSizes, ParBlocksSizeUser};
use subtle::ConstantTimeEq;
use typenum::Unsigned;

Expand Down Expand Up @@ -79,20 +60,20 @@ pub trait UniversalHash: BlockSizeUser + Sized {
/// Update hash function state with the provided block.
#[inline]
fn update(&mut self, blocks: &[Block<Self>]) {
struct Ctx<'a, BS: ArrayLength<u8>> {
struct Ctx<'a, BS: BlockSizes> {
blocks: &'a [Block<Self>],
}

impl<'a, BS: ArrayLength<u8>> BlockSizeUser for Ctx<'a, BS> {
impl<'a, BS: BlockSizes> BlockSizeUser for Ctx<'a, BS> {
type BlockSize = BS;
}

impl<'a, BS: ArrayLength<u8>> UhfClosure for Ctx<'a, BS> {
impl<'a, BS: BlockSizes> UhfClosure for Ctx<'a, BS> {
#[inline(always)]
fn call<B: UhfBackend<BlockSize = BS>>(self, backend: &mut B) {
let pb = B::ParBlocksSize::USIZE;
if pb > 1 {
let (par_blocks, tail) = to_blocks(self.blocks);
let (par_blocks, tail) = array::slice_as_chunks(self.blocks);
for par_block in par_blocks {
backend.proc_par_blocks(par_block);
}
Expand All @@ -118,12 +99,12 @@ pub trait UniversalHash: BlockSizeUser + Sized {
/// Message Authentication Codes (MACs) based on universal hashing.
#[inline]
fn update_padded(&mut self, data: &[u8]) {
let (blocks, tail) = to_blocks(data);
let (blocks, tail) = array::slice_as_chunks(data);

self.update(blocks);

if !tail.is_empty() {
let mut padded_block = GenericArray::default();
let mut padded_block = Array::default();
padded_block[..tail.len()].copy_from_slice(tail);
self.update(slice::from_ref(&padded_block));
}
Expand All @@ -132,7 +113,7 @@ pub trait UniversalHash: BlockSizeUser + Sized {
/// Retrieve result and consume hasher instance.
fn finalize(self) -> Block<Self>;

/// Obtain the [`Output`] of a [`UniversalHash`] computation and reset it back
/// Obtain the output of a [`UniversalHash`] computation and reset it back
/// to its initial state.
#[inline]
fn finalize_reset(&mut self) -> Block<Self>
Expand Down Expand Up @@ -173,17 +154,3 @@ impl core::fmt::Display for Error {

#[cfg(feature = "std")]
impl std::error::Error for Error {}

/// Split message into slice of blocks and leftover tail.
// TODO: replace with `slice::as_chunks` on migration to const generics
#[inline(always)]
fn to_blocks<T, N: ArrayLength<T>>(data: &[T]) -> (&[GenericArray<T, N>], &[T]) {
let nb = data.len() / N::USIZE;
let (left, right) = data.split_at(nb * N::USIZE);
let p = left.as_ptr() as *const GenericArray<T, N>;
// SAFETY: we guarantee that `blocks` does not point outside of `data`
// and `p` is valid for reads
#[allow(unsafe_code)]
let blocks = unsafe { slice::from_raw_parts(p, nb) };
(blocks, right)
}

0 comments on commit 8f77112

Please sign in to comment.