-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy pathlib.rs
159 lines (131 loc) · 4.14 KB
/
lib.rs
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
#![no_std]
#![doc = include_str!("../README.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![warn(missing_docs, rust_2018_idioms)]
pub use digest::{self, Digest};
mod compress;
pub(crate) mod consts;
use core::{convert::TryInto, fmt, slice::from_ref};
use digest::{
array::Array,
block_buffer::Eager,
core_api::{
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper, FixedOutputCore,
OutputSizeUser, Reset, UpdateCore,
},
crypto_common::hazmat::{DeserializeStateError, SerializableState, SerializedState},
typenum::{Unsigned, U16, U24, U64},
HashMarker, Output,
};
#[cfg(feature = "oid")]
use digest::const_oid::{AssociatedOid, ObjectIdentifier};
#[cfg(feature = "zeroize")]
use digest::zeroize::{Zeroize, ZeroizeOnDrop};
/// Core MD5 hasher state.
#[derive(Clone)]
pub struct Md5Core {
block_len: u64,
state: [u32; STATE_LEN],
}
/// MD5 hasher state.
pub type Md5 = CoreWrapper<Md5Core>;
const STATE_LEN: usize = 4;
impl HashMarker for Md5Core {}
impl BlockSizeUser for Md5Core {
type BlockSize = U64;
}
impl BufferKindUser for Md5Core {
type BufferKind = Eager;
}
impl OutputSizeUser for Md5Core {
type OutputSize = U16;
}
impl UpdateCore for Md5Core {
#[inline]
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
self.block_len = self.block_len.wrapping_add(blocks.len() as u64);
let blocks = Array::cast_slice_to_core(blocks);
compress::compress(&mut self.state, blocks)
}
}
impl FixedOutputCore for Md5Core {
#[inline]
fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) {
let bit_len = self
.block_len
.wrapping_mul(Self::BlockSize::U64)
.wrapping_add(buffer.get_pos() as u64)
.wrapping_mul(8);
let mut s = self.state;
buffer.len64_padding_le(bit_len, |b| compress::compress(&mut s, from_ref(&b.0)));
for (chunk, v) in out.chunks_exact_mut(4).zip(s.iter()) {
chunk.copy_from_slice(&v.to_le_bytes());
}
}
}
impl Default for Md5Core {
#[inline]
fn default() -> Self {
Self {
block_len: 0,
state: consts::STATE_INIT,
}
}
}
impl Reset for Md5Core {
#[inline]
fn reset(&mut self) {
*self = Default::default();
}
}
impl AlgorithmName for Md5Core {
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Md5")
}
}
impl fmt::Debug for Md5Core {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Md5Core { ... }")
}
}
#[cfg(feature = "oid")]
impl AssociatedOid for Md5Core {
const OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.113549.2.5");
}
impl Drop for Md5Core {
fn drop(&mut self) {
#[cfg(feature = "zeroize")]
{
self.state.zeroize();
self.block_len.zeroize();
}
}
}
impl SerializableState for Md5Core {
type SerializedStateSize = U24;
fn serialize(&self) -> SerializedState<Self> {
let mut serialized_state = SerializedState::<Self>::default();
for (val, chunk) in self.state.iter().zip(serialized_state.chunks_exact_mut(4)) {
chunk.copy_from_slice(&val.to_le_bytes());
}
serialized_state[16..].copy_from_slice(&self.block_len.to_le_bytes());
serialized_state
}
fn deserialize(
serialized_state: &SerializedState<Self>,
) -> Result<Self, DeserializeStateError> {
let (serialized_state, serialized_block_len) = serialized_state.split::<U16>();
let mut state = [0; STATE_LEN];
for (val, chunk) in state.iter_mut().zip(serialized_state.chunks_exact(4)) {
*val = u32::from_le_bytes(chunk.try_into().unwrap());
}
let block_len = u64::from_le_bytes(*serialized_block_len.as_ref());
Ok(Self { state, block_len })
}
}
#[cfg(feature = "zeroize")]
impl ZeroizeOnDrop for Md5Core {}