-
Notifications
You must be signed in to change notification settings - Fork 973
Expand file tree
/
Copy pathblake2_digest.rs
More file actions
169 lines (149 loc) · 4.7 KB
/
blake2_digest.rs
File metadata and controls
169 lines (149 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
/*!
RustCrypto `digest` trait implementations for the wolfCrypt BLAKE2 hash
types.
Because BLAKE2b and BLAKE2s have variable digest sizes, this module exposes
typed wrappers that pin the digest size of an underlying [`crate::blake2`]
instance to a specific value, matching the typed aliases provided by the
RustCrypto `blake2` crate (`Blake2b512`, `Blake2b256`, `Blake2s256`, etc.).
Each typed wrapper implements the traits from the `digest` crate
(`HashMarker`, `OutputSizeUser`, `BlockSizeUser`, `Update`, `Reset`,
`FixedOutput`, and `FixedOutputReset`). With these implementations the
`digest::Digest` trait becomes available via its blanket implementation,
allowing these hashers to be used anywhere a RustCrypto `Digest` is
accepted.
Any failure returned by the underlying wolfCrypt call in a trait method
will result in a panic, matching the infallible signatures required by the
RustCrypto traits.
*/
use digest::consts::{U16, U24, U32, U48, U64, U128};
macro_rules! impl_blake2_digest {
(
$(#[$attr:meta])*
$name:ident,
wc_ty = $wc_ty:path,
digest_size = $digest_size:literal,
out = $out_size:ty,
block = $block_size:ty
) => {
$(#[$attr])*
pub struct $name {
blake2: $wc_ty,
}
$(#[$attr])*
impl Default for $name {
fn default() -> Self {
Self {
blake2: <$wc_ty>::new($digest_size)
.expect("wolfCrypt BLAKE2 init failed"),
}
}
}
$(#[$attr])*
impl digest::HashMarker for $name {}
$(#[$attr])*
impl digest::OutputSizeUser for $name {
type OutputSize = $out_size;
}
$(#[$attr])*
impl digest::block_api::BlockSizeUser for $name {
type BlockSize = $block_size;
}
$(#[$attr])*
impl digest::Update for $name {
fn update(&mut self, data: &[u8]) {
<$wc_ty>::update(&mut self.blake2, data)
.expect("wolfCrypt BLAKE2 update failed");
}
}
$(#[$attr])*
impl digest::Reset for $name {
fn reset(&mut self) {
self.blake2 = <$wc_ty>::new($digest_size)
.expect("wolfCrypt BLAKE2 init failed");
}
}
$(#[$attr])*
impl digest::FixedOutput for $name {
fn finalize_into(mut self, out: &mut digest::Output<Self>) {
<$wc_ty>::finalize(&mut self.blake2, out.as_mut_slice())
.expect("wolfCrypt BLAKE2 finalize failed");
}
}
$(#[$attr])*
impl digest::FixedOutputReset for $name {
fn finalize_into_reset(&mut self, out: &mut digest::Output<Self>) {
<$wc_ty>::finalize(&mut self.blake2, out.as_mut_slice())
.expect("wolfCrypt BLAKE2 finalize failed");
self.blake2 = <$wc_ty>::new($digest_size)
.expect("wolfCrypt BLAKE2 init failed");
}
}
};
}
impl_blake2_digest! {
#[cfg(blake2b)]
Blake2b256,
wc_ty = crate::blake2::BLAKE2b,
digest_size = 32,
out = U32,
block = U128
}
impl_blake2_digest! {
#[cfg(blake2b)]
Blake2b384,
wc_ty = crate::blake2::BLAKE2b,
digest_size = 48,
out = U48,
block = U128
}
impl_blake2_digest! {
#[cfg(blake2b)]
Blake2b512,
wc_ty = crate::blake2::BLAKE2b,
digest_size = 64,
out = U64,
block = U128
}
impl_blake2_digest! {
#[cfg(blake2s)]
Blake2s128,
wc_ty = crate::blake2::BLAKE2s,
digest_size = 16,
out = U16,
block = U64
}
impl_blake2_digest! {
#[cfg(blake2s)]
Blake2s192,
wc_ty = crate::blake2::BLAKE2s,
digest_size = 24,
out = U24,
block = U64
}
impl_blake2_digest! {
#[cfg(blake2s)]
Blake2s256,
wc_ty = crate::blake2::BLAKE2s,
digest_size = 32,
out = U32,
block = U64
}