Skip to content

Commit 9b0923b

Browse files
committed
fixed cargo clippy and added README.md content
1 parent 6adbaba commit 9b0923b

File tree

6 files changed

+125
-62
lines changed

6 files changed

+125
-62
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

md6/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ name = "md6"
1616

1717
[dependencies]
1818
digest = "=0.11.0-pre.9"
19+
hex = "0.4.3"
1920

2021
[dev-dependencies]
2122
digest = { version = "=0.11.0-pre.9", features = ["dev"] }

md6/README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# RustCrypto: MD6
2+
3+
Pure Rust implementation of the MD6 hash function.
4+
5+
## Example
6+
7+
### Fixed output size
8+
9+
```rust
10+
use md6::{Md6_256, Digest};
11+
use hex_literal::hex;
12+
13+
// create a Blake2b512 object
14+
let mut hasher = Md6_256::new();
15+
16+
// write input message
17+
hasher.update(b"hello world");
18+
19+
// read hash digest and consume hasher
20+
let hash = hasher.finalize();
21+
assert_eq!(hash.to_vec(), hex!(
22+
"9ae602639631cc2c60adaa7a952aae8756141f31a7e6a9b76adc1de121db2230"
23+
));
24+
```
25+
26+
Also, see the [examples section] in the RustCrypto/hashes readme.
27+
28+
### Variable output size
29+
30+
This implementation supports run and compile time variable sizes.
31+
32+
Output size set at run time:
33+
```rust
34+
use md6::Md6Var;
35+
use blake2::digest::{Update, VariableOutput};
36+
use hex_literal::hex;
37+
38+
let mut hasher = Md6Var::new(12).unwrap();
39+
hasher.update(b"hello rust");
40+
let mut buf = [0u8; 12];
41+
hasher.finalize_variable(&mut buf).unwrap();
42+
assert_eq!(buf, hex!("9c5b8d9744898ec981bcc573"));
43+
```
44+
45+
Output size set at compile time:
46+
```rust
47+
use md6::{Md6, Digest, digest::consts::U20};
48+
use hex_literal::hex;
49+
50+
type Md6_160 = Md6<U20>;
51+
52+
let mut hasher = Md6_160::new();
53+
hasher.update(b"my_input");
54+
let res = hasher.finalize();
55+
assert_eq!(res, hex!("576d736a93a555a1c868973cfdd2d21838a26623"));
56+
```
57+
58+
## Minimum Supported Rust Version
59+
60+
Rust **1.81** or higher.
61+
62+
Minimum supported Rust version can be changed in the future, but it will be
63+
done with a minor version bump.
64+
65+
## SemVer Policy
66+
67+
- All on-by-default features of this library are covered by SemVer
68+
- MSRV is considered exempt from SemVer as noted above
69+
70+
## License
71+
72+
The crate is licensed under either of:
73+
74+
* [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
75+
* [MIT license](http://opensource.org/licenses/MIT)
76+
77+
## Contributing
78+
79+
Unless you explicitly state otherwise, any contribution intentionally submitted
80+
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
81+
dual licensed as above, without any additional terms or conditions.

md6/src/compress.rs

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pub fn compress(c: &mut [Md6Word], n: &mut [Md6Word], r: usize, a: &mut [Md6Word
145145
c.copy_from_slice(&a[((r - 1) * C + N)..((r - 1) * C + N + C)]); // output into c
146146
}
147147

148-
fn make_control_word(
148+
pub fn make_control_word(
149149
r: usize,
150150
l: usize,
151151
z: usize,
@@ -170,15 +170,9 @@ pub fn pack(
170170
n: &mut [Md6Word],
171171
q: &[Md6Word],
172172
k: [Md6Word; K],
173-
ell: usize,
174-
i: Md6Word,
175-
r: usize,
176-
l: usize,
177-
z: usize,
178-
p: usize,
179-
keylen: usize,
180-
d: usize,
181173
b: [Md6Word; 64],
174+
u: Md6NodeID,
175+
v: Md6ControlWord,
182176
) {
183177
let mut ni = 0;
184178

@@ -188,46 +182,13 @@ pub fn pack(
188182
n[ni..ni + K].copy_from_slice(&k[..K]); // k: key in words 15--22
189183
ni += K;
190184

191-
let u = make_node_id(ell, i); // u: unique node ID in 23
185+
// u: unique node ID in 23
192186
n[ni] = u;
193187
ni += U;
194188

195-
let v = make_control_word(r, l, z, p, keylen, d); // v: control word in 24
189+
// v: control word in 24
196190
n[ni] = v;
197191
ni += V;
198192

199193
n[ni..ni + B].copy_from_slice(&b[..B]); // b: data words 25--88
200194
}
201-
202-
pub fn standard_compress(
203-
c: &mut [Md6Word],
204-
q: &[Md6Word],
205-
k: [Md6Word; K],
206-
ell: usize,
207-
i: Md6Word,
208-
r: usize,
209-
l: usize,
210-
z: usize,
211-
p: usize,
212-
keylen: usize,
213-
d: usize,
214-
b: [Md6Word; 64],
215-
) {
216-
let mut n = [0; MD6_N];
217-
let mut a = [0; 5000];
218-
219-
// check that the input values are sensible
220-
assert!(!c.is_empty());
221-
assert!(!q.is_empty());
222-
assert!(!b.is_empty());
223-
assert!(r <= MD6_MAX_R);
224-
assert!(l <= 255);
225-
assert!(ell <= 255);
226-
assert!(p <= B * W);
227-
assert!(d <= C * W / 2);
228-
assert!(!k.is_empty());
229-
230-
pack(&mut n, q, k, ell, i, r, l, z, p, keylen, d, b); // pack input data into N
231-
232-
compress(c, &mut n, r, &mut a); // compress
233-
}

md6/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ use crate::md6::Md6VarCore;
2424
pub type Md6Var = RtVariableCoreWrapper<Md6VarCore>;
2525
/// Core hash function for Md6 generic over output size
2626
pub type Md6Core<OutSize> = CtVariableCoreWrapper<Md6VarCore, OutSize>;
27+
/// Md6 generic over output size.
28+
pub type Md6<OutSize> = CoreWrapper<Md6Core<OutSize>>;
2729
/// Md6 with 64-bit output
2830
pub type Md6_64 = CoreWrapper<Md6Core<U8>>;
2931
/// Md6 with 128-bit output

md6/src/md6.rs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl SerializableState for Md6VarCore {
245245

246246
let mut k = [0u64; K];
247247
for word in &mut k {
248-
*word = read_u64(&serialized_state, &mut offset);
248+
*word = read_u64(serialized_state, &mut offset);
249249
}
250250

251251
let mut b = [[0u64; B]; MD6_MAX_STACK_HEIGHT];
@@ -298,7 +298,7 @@ impl Md6VarCore {
298298
if key.is_some() {
299299
assert!(keylen <= K * (W / 8), "bad keylen");
300300
}
301-
assert!(!(d < 1 || d > 512 || d > W * C / 2), "bad hashlen");
301+
assert!((1..=512).contains(&d), "bad hashlen");
302302

303303
let (k, keylen) = match key {
304304
Some(key) if keylen > 0 => {
@@ -354,6 +354,36 @@ impl Md6VarCore {
354354
}
355355
}
356356

357+
pub fn standard_compress(
358+
&self,
359+
c: &mut [Md6Word],
360+
q: &[Md6Word],
361+
ell: usize,
362+
p: usize,
363+
z: usize,
364+
) {
365+
let mut n = [0; MD6_N];
366+
let mut a = [0; 5000];
367+
368+
// check that the input values are sensible
369+
assert!(!c.is_empty());
370+
assert!(!q.is_empty());
371+
assert!(!self.b.is_empty());
372+
assert!(self.r <= MD6_MAX_R);
373+
assert!(self.l <= 255);
374+
assert!(ell <= 255);
375+
assert!(p <= B * W);
376+
assert!(self.d <= C * W / 2);
377+
assert!(!self.k.is_empty());
378+
379+
let u = make_node_id(ell, self.i_for_level[ell]);
380+
let v = make_control_word(self.r, self.l, z, p, self.keylen, self.d);
381+
382+
pack(&mut n, q, self.k, self.b[ell], u, v); // pack input data into N
383+
384+
compress(c, &mut n, self.r, &mut a); // compress
385+
}
386+
357387
#[inline]
358388
fn compress_block(&mut self, c: &mut [u64], ell: usize, z: usize) {
359389
// check that input values are sensible
@@ -365,20 +395,7 @@ impl Md6VarCore {
365395
let p = B * W - self.bits[ell]; // number of padding bits
366396
let q = get_round_constants(W); // Q constant
367397

368-
standard_compress(
369-
c,
370-
q,
371-
self.k,
372-
ell,
373-
self.i_for_level[ell],
374-
self.r,
375-
self.l,
376-
z,
377-
p,
378-
self.keylen,
379-
self.d,
380-
self.b[ell],
381-
);
398+
self.standard_compress(c, q, ell, p, z);
382399

383400
self.bits[ell] = 0; // clear bits used count this level
384401
self.i_for_level[ell] += 1; // increment i for this level
@@ -466,15 +483,15 @@ impl Md6VarCore {
466483
// Number of bytes (full or partial) in src
467484
let srcbytes = (srclen + 7) / 8;
468485

469-
for i in 0..srcbytes {
486+
for (i, item) in src.iter().enumerate().take(srcbytes) {
470487
if i != srcbytes - 1 {
471488
// Not the last byte
472489
accum = (accum << 8) ^ src[i] as u16;
473490
accumlen += 8;
474491
} else {
475492
// Last byte
476493
let newbits = if srclen % 8 == 0 { 8 } else { srclen % 8 };
477-
accum = (accum << newbits) ^ ((src[i] as u16) >> (8 - newbits));
494+
accum = (accum << newbits) ^ ((*item as u16) >> (8 - newbits));
478495
accumlen += newbits;
479496
}
480497

0 commit comments

Comments
 (0)