Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

newtypes support #659

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
12 changes: 5 additions & 7 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ members = [

[profile.dev]
opt-level = 2

[patch.crates-io]
#digest = { path = "../traits/digest" }
digest = { git = "https://github.com/baloo/traits.git", branch = "baloo/digest/new-type" }
109 changes: 61 additions & 48 deletions ascon-hash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,55 +381,68 @@ impl AlgorithmName for AsconAXofCore {
}
}

/// Ascon hash
///
/// ```
/// use ascon_hash::{AsconHash, Digest};
///
/// let mut hasher = AsconHash::new();
/// hasher.update(b"some bytes");
/// let digest = hasher.finalize();
/// assert_eq!(&digest[..], b"\xb7\x42\xca\x75\xe5\x70\x38\x75\x70\x59\xcc\xcc\x68\x74\x71\x4f\x9d\xbd\x7f\xc5\x92\x4a\x7d\xf4\xe3\x16\x59\x4f\xd1\x42\x6c\xa8");
/// ```
pub type AsconHash = CoreWrapper<AsconCore>;
/// AsconA hash
///
/// ```
/// use ascon_hash::{AsconAHash, Digest};
///
/// let mut hasher = AsconAHash::new();
/// hasher.update(b"some bytes");
/// let digest = hasher.finalize();
/// assert_eq!(&digest[..], b"\x1d\x1a\xc8\x74\x4a\x4a\x05\x81\x33\x7d\x5a\xf2\x78\xc2\x55\x88\xe1\xa3\xdd\x2d\x86\x73\x07\x64\x26\x53\xdc\xa4\x45\xf5\x5c\x2a");
/// ```
pub type AsconAHash = CoreWrapper<AsconACore>;
/// AsconXof
///
/// ```
/// use ascon_hash::{AsconXof, ExtendableOutput, Update, XofReader};
///
/// let mut xof = AsconXof::default();
/// xof.update(b"some bytes");
/// let mut reader = xof.finalize_xof();
/// let mut dst = [0u8; 5];
/// reader.read(&mut dst);
/// assert_eq!(&dst, b"\xc2\x19\x72\xfd\xe9");
/// ```
pub type AsconXof = CoreWrapper<AsconXofCore>;
digest::newtype!(
r#"Ascon hash

```
use ascon_hash::{AsconHash, Digest};

let mut hasher = AsconHash::new();
hasher.update(b"some bytes");
let digest = hasher.finalize();
assert_eq!(&digest[..], b"\xb7\x42\xca\x75\xe5\x70\x38\x75\x70\x59\xcc\xcc\x68\x74\x71\x4f\x9d\xbd\x7f\xc5\x92\x4a\x7d\xf4\xe3\x16\x59\x4f\xd1\x42\x6c\xa8");
```"#,
AsconHash = CoreWrapper<AsconCore>
);

digest::newtype!(
r#"AsconA hash

```
use ascon_hash::{AsconAHash, Digest};

let mut hasher = AsconAHash::new();
hasher.update(b"some bytes");
let digest = hasher.finalize();
assert_eq!(&digest[..], b"\x1d\x1a\xc8\x74\x4a\x4a\x05\x81\x33\x7d\x5a\xf2\x78\xc2\x55\x88\xe1\xa3\xdd\x2d\x86\x73\x07\x64\x26\x53\xdc\xa4\x45\xf5\x5c\x2a");
```"#,
AsconAHash = CoreWrapper<AsconACore>
);

digest::newtype!(
r#"AsconXof

```
use ascon_hash::{AsconXof, ExtendableOutput, Update, XofReader};

let mut xof = AsconXof::default();
xof.update(b"some bytes");
let mut reader = xof.finalize_xof();
let mut dst = [0u8; 5];
reader.read(&mut dst);
assert_eq!(&dst, b"\xc2\x19\x72\xfd\xe9");
```"#,
AsconXof = CoreWrapper<AsconXofCore>
);

/// Reader for AsconXof output
pub type AsconXofReader = XofReaderCoreWrapper<AsconXofReaderCore>;
/// AsconAXof
///
/// ```
/// use ascon_hash::{AsconAXof, ExtendableOutput, Update, XofReader};
///
/// let mut xof = AsconAXof::default();
/// xof.update(b"some bytes");
/// let mut reader = xof.finalize_xof();
/// let mut dst = [0u8; 5];
/// reader.read(&mut dst);
/// assert_eq!(&dst, b"\xb8\xd6\xbd\xf0\xa7");
/// ```
pub type AsconAXof = CoreWrapper<AsconAXofCore>;

digest::newtype!(
r#"AsconAXof

```
use ascon_hash::{AsconAXof, ExtendableOutput, Update, XofReader};

let mut xof = AsconAXof::default();
xof.update(b"some bytes");
let mut reader = xof.finalize_xof();
let mut dst = [0u8; 5];
reader.read(&mut dst);
assert_eq!(&dst, b"\xb8\xd6\xbd\xf0\xa7");
```"#,
AsconAXof = CoreWrapper<AsconAXofCore>
);

/// Reader for AsconAXof output
pub type AsconAXofReader = XofReaderCoreWrapper<AsconAXofReaderCore>;
3 changes: 1 addition & 2 deletions belt-hash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ pub struct BeltHashCore {
h: [u32; 8],
}

/// BelT hasher state.
pub type BeltHash = CoreWrapper<BeltHashCore>;
digest::newtype!("BelT hasher hasher", BeltHash = CoreWrapper<BeltHashCore>);

impl BeltHashCore {
fn compress_block(&mut self, block: &Block<Self>) {
Expand Down
10 changes: 4 additions & 6 deletions blake2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,10 @@ pub type Blake2bVar = RtVariableCoreWrapper<Blake2bVarCore>;
pub type Blake2bCore<OutSize> = CtVariableCoreWrapper<Blake2bVarCore, OutSize>;
/// BLAKE2b generic over output size.
pub type Blake2b<OutSize> = CoreWrapper<Blake2bCore<OutSize>>;
/// BLAKE2b-128 hasher state.
pub type Blake2b128 = Blake2b<U16>;
/// BLAKE2b-256 hasher state.
pub type Blake2b256 = Blake2b<U32>;
/// BLAKE2b-512 hasher state.
pub type Blake2b512 = Blake2b<U64>;

digest::newtype!("BLAKE2b-128 hasher state.", Blake2b128 = Blake2b<U16>);
digest::newtype!("BLAKE2b-256 hasher state.", Blake2b256 = Blake2b<U32>);
digest::newtype!("BLAKE2b-512 hasher state.", Blake2b512 = Blake2b<U64>);

blake2_mac_impl!(Blake2bMac, Blake2bVarCore, U64, "Blake2b MAC function");

Expand Down
3 changes: 1 addition & 2 deletions fsb/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ macro_rules! fsb_impl {
state: [u8; $r / 8],
}

#[doc=$full_doc]
pub type $full_state = CoreWrapper<$state>;
digest::newtype!($full_doc, $full_state = CoreWrapper<$state>);

impl HashMarker for $state {}

Expand Down
20 changes: 11 additions & 9 deletions gost94/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ impl AssociatedOid for Gost94Core<params::GOST28147UAParam> {
const OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.804.2.1.1.1.1.2.1");
}

/// GOST94 hash function with CryptoPro parameters.
pub type Gost94CryptoPro = CoreWrapper<Gost94Core<params::CryptoProParam>>;
/// GOST94 hash function with S-box defined in GOST R 34.12-2015.
pub type Gost94s2015 = CoreWrapper<Gost94Core<params::S2015Param>>;
/// GOST94 hash function with test parameters.
pub type Gost94Test = CoreWrapper<Gost94Core<params::TestParam>>;
/// GOST94 hash function with UAPKI GOST 34.311-95 parameters
/// (1.2.804.2.1.1.1.1.2.1 OID).
pub type Gost94UA = CoreWrapper<Gost94Core<params::GOST28147UAParam>>;
digest::newtype!("GOST94 hash function with CryptoPro parameters.", Gost94CryptoPro = CoreWrapper<Gost94Core<params::CryptoProParam>>);

digest::newtype!("GOST94 hash function with S-box defined in GOST R 34.12-2015.", Gost94s2015 = CoreWrapper<Gost94Core<params::S2015Param>>);

digest::newtype!("GOST94 hash function with test parameters.", Gost94Test = CoreWrapper<Gost94Core<params::TestParam>>);

digest::newtype!(
r#"GOST94 hash function with UAPKI GOST 34.311-95 parameters
(1.2.804.2.1.1.1.1.2.1 OID)."#,
Gost94UA = CoreWrapper<Gost94Core<params::GOST28147UAParam>>
);
12 changes: 4 additions & 8 deletions groestl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ pub type GroestlShortCore<OutSize> = CtVariableCoreWrapper<GroestlShortVarCore,
/// Hasher state of the short Groestl variant generic over output size.
pub type GroestlShort<OutSize> = CoreWrapper<GroestlShortCore<OutSize>>;

/// Groestl-224 hasher state.
pub type Groestl224 = CoreWrapper<GroestlShortCore<U28>>;
/// Groestl-256 hasher state.
pub type Groestl256 = CoreWrapper<GroestlShortCore<U32>>;
digest::newtype!("Groestl-224 hasher state.", Groestl224 = CoreWrapper<GroestlShortCore<U28>>);
digest::newtype!("Groestl-256 hasher state.", Groestl256 = CoreWrapper<GroestlShortCore<U32>>);

impl HashMarker for GroestlShortVarCore {}

Expand Down Expand Up @@ -175,10 +173,8 @@ pub type GroestlLongCore<OutSize> = CtVariableCoreWrapper<GroestlLongVarCore, Ou
/// Hasher state of the long Groestl variant generic over output size.
pub type GroestlLong<OutSize> = CoreWrapper<GroestlLongCore<OutSize>>;

/// Groestl-384 hasher state.
pub type Groestl384 = CoreWrapper<GroestlLongCore<U48>>;
/// Groestl-512 hasher state.
pub type Groestl512 = CoreWrapper<GroestlLongCore<U64>>;
digest::newtype!("Groestl-384 hasher state.", Groestl384 = CoreWrapper<GroestlLongCore<U48>>);
digest::newtype!("Groestl-512 hasher state.", Groestl512 = CoreWrapper<GroestlLongCore<U64>>);

impl HashMarker for GroestlLongVarCore {}

Expand Down
24 changes: 16 additions & 8 deletions jh/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,22 @@ pub struct JhCore {
block_len: u64,
}

/// Jh-224 hasher state
pub type Jh224 = CoreWrapper<CtVariableCoreWrapper<JhCore, U28>>;
/// Jh-256 hasher state
pub type Jh256 = CoreWrapper<CtVariableCoreWrapper<JhCore, U32>>;
/// Jh-384 hasher state
pub type Jh384 = CoreWrapper<CtVariableCoreWrapper<JhCore, U48>>;
/// Jh-512 hasher state
pub type Jh512 = CoreWrapper<CtVariableCoreWrapper<JhCore, U64>>;
digest::newtype!(
"Jh-224 hasher state",
Jh224 = CoreWrapper<CtVariableCoreWrapper<JhCore, U28>>
);
digest::newtype!(
"Jh-256 hasher state",
Jh256 = CoreWrapper<CtVariableCoreWrapper<JhCore, U32>>
);
digest::newtype!(
"Jh-384 hasher state",
Jh384 = CoreWrapper<CtVariableCoreWrapper<JhCore, U48>>
);
digest::newtype!(
"Jh-512 hasher state",
Jh512 = CoreWrapper<CtVariableCoreWrapper<JhCore, U64>>
);

impl HashMarker for JhCore {}

Expand Down
3 changes: 1 addition & 2 deletions md2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ pub struct Md2Core {
checksum: Block<Self>,
}

/// MD2 hasher state.
pub type Md2 = CoreWrapper<Md2Core>;
digest::newtype!("MD2 hasher state.", Md2 = CoreWrapper<Md2Core>);

const STATE_LEN: usize = 48;

Expand Down
3 changes: 1 addition & 2 deletions md4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ pub struct Md4Core {
state: [Wu32; STATE_LEN],
}

/// MD4 hasher state
pub type Md4 = CoreWrapper<Md4Core>;
digest::newtype!("MD4 hasher state", Md4 = CoreWrapper<Md4Core>);

const STATE_LEN: usize = 4;

Expand Down
3 changes: 1 addition & 2 deletions md5/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ pub struct Md5Core {
state: [u32; STATE_LEN],
}

/// MD5 hasher state.
pub type Md5 = CoreWrapper<Md5Core>;
digest::newtype!("MD5 hasher", Md5 = CoreWrapper<Md5Core>);

const STATE_LEN: usize = 4;

Expand Down
7 changes: 4 additions & 3 deletions ripemd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ macro_rules! impl_ripemd {
block_len: u64,
}

#[doc = $doc_name]
#[doc = " hasher."]
pub type $wrapped_name = CoreWrapper<$name>;
digest::newtype!(
concat!($doc_name, " hasher state."),
$wrapped_name = CoreWrapper<$name>
);

impl HashMarker for $name {}

Expand Down
3 changes: 1 addition & 2 deletions sha1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ pub struct Sha1Core {
block_len: u64,
}

/// SHA-1 hasher state.
pub type Sha1 = CoreWrapper<Sha1Core>;
digest::newtype!("SHA-1 hasher", Sha1 = CoreWrapper<Sha1Core>);

impl HashMarker for Sha1Core {}

Expand Down
41 changes: 29 additions & 12 deletions sha2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,32 @@ impl_oid_carrier!(OidSha224, "2.16.840.1.101.3.4.2.4");
impl_oid_carrier!(OidSha512_224, "2.16.840.1.101.3.4.2.5");
impl_oid_carrier!(OidSha512_256, "2.16.840.1.101.3.4.2.6");

/// SHA-224 hasher.
pub type Sha224 = CoreWrapper<CtVariableCoreWrapper<Sha256VarCore, U28, OidSha224>>;
/// SHA-256 hasher.
pub type Sha256 = CoreWrapper<CtVariableCoreWrapper<Sha256VarCore, U32, OidSha256>>;
/// SHA-512/224 hasher.
pub type Sha512_224 = CoreWrapper<CtVariableCoreWrapper<Sha512VarCore, U28, OidSha512_224>>;
/// SHA-512/256 hasher.
pub type Sha512_256 = CoreWrapper<CtVariableCoreWrapper<Sha512VarCore, U32, OidSha512_256>>;
/// SHA-384 hasher.
pub type Sha384 = CoreWrapper<CtVariableCoreWrapper<Sha512VarCore, U48, OidSha384>>;
/// SHA-512 hasher.
pub type Sha512 = CoreWrapper<CtVariableCoreWrapper<Sha512VarCore, U64, OidSha512>>;
digest::newtype!(
"SHA-224 hasher",
Sha224 = CoreWrapper<CtVariableCoreWrapper<Sha256VarCore, U28, OidSha224>>
);

digest::newtype!(
"SHA-256 hasher",
Sha256 = CoreWrapper<CtVariableCoreWrapper<Sha256VarCore, U32, OidSha256>>
);

digest::newtype!(
"SHA-512/224 hasher",
Sha512_224 = CoreWrapper<CtVariableCoreWrapper<Sha512VarCore, U28, OidSha512_224>>
);

digest::newtype!(
"SHA-512/256 hasher",
Sha512_256 = CoreWrapper<CtVariableCoreWrapper<Sha512VarCore, U32, OidSha512_256>>
);

digest::newtype!(
"SHA-384 hasher",
Sha384 = CoreWrapper<CtVariableCoreWrapper<Sha512VarCore, U48, OidSha384>>
);

digest::newtype!(
"SHA-512 hasher",
Sha512 = CoreWrapper<CtVariableCoreWrapper<Sha512VarCore, U64, OidSha512>>
);
2 changes: 1 addition & 1 deletion sha3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#![forbid(unsafe_code)]
#![warn(missing_docs)]

pub use digest::{self, Digest};
pub use digest::{self, CustomizedInit, Digest};

use core::fmt;
use digest::{
Expand Down
Loading
Loading