diff --git a/Cargo.toml b/Cargo.toml index 8eb15389d..4c70e3dd2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ members = [ "md4", "md5", "ripemd160", + "ripemd320", "sha1", "sha2", "sha3", diff --git a/ripemd320/Cargo.toml b/ripemd320/Cargo.toml new file mode 100644 index 000000000..e11a75f57 --- /dev/null +++ b/ripemd320/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "ripemd320" +version = "0.8.0" +authors = ["RustCrypto Developers"] +license = "MIT OR Apache-2.0" +description = "RIPEMD-320 hash function" +documentation = "https://docs.rs/ripemd320" +repository = "https://github.com/RustCrypto/hashes" +keywords = ["crypto", "ripemd320", "hash", "digest"] +categories = ["cryptography", "no-std"] + +[dependencies] +digest = "0.8" +block-buffer = "0.7" +opaque-debug = "0.2" + +[dev-dependencies] +digest = { version = "0.8", features = ["dev"] } +hex-literal = "0.1" + +[features] +default = ["std"] +std = ["digest/std"] + +[badges] +travis-ci = { repository = "RustCrypto/hashes" } diff --git a/ripemd320/LICENSE-APACHE b/ripemd320/LICENSE-APACHE new file mode 100644 index 000000000..78173fa2e --- /dev/null +++ b/ripemd320/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/ripemd320/LICENSE-MIT b/ripemd320/LICENSE-MIT new file mode 100644 index 000000000..66cf75563 --- /dev/null +++ b/ripemd320/LICENSE-MIT @@ -0,0 +1,27 @@ +Copyright (c) 2006-2009 Graydon Hoare +Copyright (c) 2009-2013 Mozilla Foundation +Copyright (c) 2016 Artyom Pavlov + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/ripemd320/benches/lib.rs b/ripemd320/benches/lib.rs new file mode 100644 index 000000000..d8f38a0c6 --- /dev/null +++ b/ripemd320/benches/lib.rs @@ -0,0 +1,7 @@ +#![no_std] +#![feature(test)] +#[macro_use] +extern crate digest; +extern crate ripemd320; + +bench!(ripemd320::Ripemd320); diff --git a/ripemd320/examples/ripemd320sum.rs b/ripemd320/examples/ripemd320sum.rs new file mode 100644 index 000000000..b255ed8fb --- /dev/null +++ b/ripemd320/examples/ripemd320sum.rs @@ -0,0 +1,49 @@ +extern crate ripemd320; + +use ripemd320::{Ripemd320, Digest}; +use std::env; +use std::fs; +use std::io::{self, Read}; + +const BUFFER_SIZE: usize = 1024; + +/// Print digest result as hex string and name pair +fn print_result(sum: &[u8], name: &str) { + for byte in sum { + print!("{:02x}", byte); + } + println!("\t{}", name); +} + +/// Compute digest value for given `Reader` and print it +/// On any error simply return without doing anything +fn process(reader: &mut R, name: &str) { + let mut sh = D::default(); + let mut buffer = [0u8; BUFFER_SIZE]; + loop { + let n = match reader.read(&mut buffer) { + Ok(n) => n, + Err(_) => return, + }; + sh.input(&buffer[..n]); + if n == 0 || n < BUFFER_SIZE { + break; + } + } + print_result(&sh.result(), name); +} + +fn main() { + let args = env::args(); + // Process files listed in command line arguments one by one + // If no files provided process input from stdin + if args.len() > 1 { + for path in args.skip(1) { + if let Ok(mut file) = fs::File::open(&path) { + process::(&mut file, &path); + } + } + } else { + process::(&mut io::stdin(), "-"); + } +} diff --git a/ripemd320/src/block.rs b/ripemd320/src/block.rs new file mode 100644 index 000000000..a1e3f12f2 --- /dev/null +++ b/ripemd320/src/block.rs @@ -0,0 +1,333 @@ +use block_buffer::byteorder::{ByteOrder, LE}; +use core::mem::swap; +use digest::generic_array::typenum::U64; +use digest::generic_array::GenericArray; + +pub const HALF_DIGEST_BUF_LEN: usize = 5; +pub const DIGEST_BUF_LEN: usize = 10; +pub const WORK_BUF_LEN: usize = 16; + +pub const H0: [u32; DIGEST_BUF_LEN] = [ + 0x6745_2301, + 0xefcd_ab89, + 0x98ba_dcfe, + 0x1032_5476, + 0xc3d2_e1f0, + 0x7654_3210, + 0xfedc_ba98, + 0x89a_bcdef, + 0x0123_4567, + 0x3c2d_1e0f, +]; + +type Block = GenericArray; + +macro_rules! round( + ($a:expr, $b:expr, $c:expr, $d:expr, $e:expr, + $x:expr, $bits:expr, $add:expr, $round:expr) => ({ + $a = $a.wrapping_add($round).wrapping_add($x).wrapping_add($add); + $a = $a.rotate_left($bits).wrapping_add($e); + $c = $c.rotate_left(10); + }); +); + +#[inline(always)] +fn swap_idx(bb: &mut [u32; HALF_DIGEST_BUF_LEN], bbb: &mut [u32; HALF_DIGEST_BUF_LEN], idx: usize) { + swap(&mut bb[idx], &mut bbb[idx]); +} + +macro_rules! process_block( + ($h:ident, $data:expr, + $( round1: h_ordering $f0:expr, $f1:expr, $f2:expr, $f3:expr, $f4:expr; + data_index $data_index1:expr; roll_shift $bits1:expr )*; + $( round2: h_ordering $g0:expr, $g1:expr, $g2:expr, $g3:expr, $g4:expr; + data_index $data_index2:expr; roll_shift $bits2:expr )*; + $( round3: h_ordering $h0:expr, $h1:expr, $h2:expr, $h3:expr, $h4:expr; + data_index $data_index3:expr; roll_shift $bits3:expr )*; + $( round4: h_ordering $i0:expr, $i1:expr, $i2:expr, $i3:expr, $i4:expr; + data_index $data_index4:expr; roll_shift $bits4:expr )*; + $( round5: h_ordering $j0:expr, $j1:expr, $j2:expr, $j3:expr, $j4:expr; + data_index $data_index5:expr; roll_shift $bits5:expr )*; + $( par_round1: h_ordering $pj0:expr, $pj1:expr, $pj2:expr, $pj3:expr, $pj4:expr; + data_index $pdata_index1:expr; roll_shift $pbits1:expr )*; + $( par_round2: h_ordering $pi0:expr, $pi1:expr, $pi2:expr, $pi3:expr, $pi4:expr; + data_index $pdata_index2:expr; roll_shift $pbits2:expr )*; + $( par_round3: h_ordering $ph0:expr, $ph1:expr, $ph2:expr, $ph3:expr, $ph4:expr; + data_index $pdata_index3:expr; roll_shift $pbits3:expr )*; + $( par_round4: h_ordering $pg0:expr, $pg1:expr, $pg2:expr, $pg3:expr, $pg4:expr; + data_index $pdata_index4:expr; roll_shift $pbits4:expr )*; + $( par_round5: h_ordering $pf0:expr, $pf1:expr, $pf2:expr, $pf3:expr, $pf4:expr; + data_index $pdata_index5:expr; roll_shift $pbits5:expr )*; + ) => ({ + let mut bb = [0u32; HALF_DIGEST_BUF_LEN]; + bb.copy_from_slice(&$h[..HALF_DIGEST_BUF_LEN]); + + let mut bbb = [0u32; HALF_DIGEST_BUF_LEN]; + bbb.copy_from_slice(&$h[HALF_DIGEST_BUF_LEN..]); + + + // Round 1 + $( round!(bb[$f0], bb[$f1], bb[$f2], bb[$f3], bb[$f4], + $data[$data_index1], $bits1, 0x0000_0000, + bb[$f1] ^ bb[$f2] ^ bb[$f3]); )* + + // Parallel Round 1 + $( round!(bbb[$pj0], bbb[$pj1], bbb[$pj2], bbb[$pj3], bbb[$pj4], + $data[$pdata_index1], $pbits1, 0x50a2_8be6, + bbb[$pj1] ^ (bbb[$pj2] | !bbb[$pj3])); )* + + swap_idx(&mut bb, &mut bbb, 0); + + + // Round 2 + $( round!(bb[$g0], bb[$g1], bb[$g2], bb[$g3], bb[$g4], + $data[$data_index2], $bits2, 0x5a82_7999, + (bb[$g1] & bb[$g2]) | (!bb[$g1] & bb[$g3])); )* + + // Parallel Round 2 + $( round!(bbb[$pi0], bbb[$pi1], bbb[$pi2], bbb[$pi3], bbb[$pi4], + $data[$pdata_index2], $pbits2, 0x5c4d_d124, + (bbb[$pi1] & bbb[$pi3]) | (bbb[$pi2] & !bbb[$pi3])); )* + + + swap_idx(&mut bb, &mut bbb, 1); + + + // Round 3 + $( round!(bb[$h0], bb[$h1], bb[$h2], bb[$h3], bb[$h4], + $data[$data_index3], $bits3, 0x6ed9_eba1, + (bb[$h1] | !bb[$h2]) ^ bb[$h3]); )* + + // Parallel Round 3 + $( round!(bbb[$ph0], bbb[$ph1], bbb[$ph2], bbb[$ph3], bbb[$ph4], + $data[$pdata_index3], $pbits3, 0x6d70_3ef3, + (bbb[$ph1] | !bbb[$ph2]) ^ bbb[$ph3]); )* + + swap_idx(&mut bb, &mut bbb, 2); + + + // Round 4 + $( round!(bb[$i0], bb[$i1], bb[$i2], bb[$i3], bb[$i4], + $data[$data_index4], $bits4, 0x8f1b_bcdc, + (bb[$i1] & bb[$i3]) | (bb[$i2] & !bb[$i3])); )* + + + // Parallel Round 4 + $( round!(bbb[$pg0], bbb[$pg1], bbb[$pg2], bbb[$pg3], bbb[$pg4], + $data[$pdata_index4], $pbits4, 0x7a6d_76e9, + (bbb[$pg1] & bbb[$pg2]) | (!bbb[$pg1] & bbb[$pg3])); )* + + swap_idx(&mut bb, &mut bbb, 3); + + // Round 5 + $( round!(bb[$j0], bb[$j1], bb[$j2], bb[$j3], bb[$j4], + $data[$data_index5], $bits5, 0xa953_fd4e, + bb[$j1] ^ (bb[$j2] | !bb[$j3])); )* + + // Parallel Round 5 + $( round!(bbb[$pf0], bbb[$pf1], bbb[$pf2], bbb[$pf3], bbb[$pf4], + $data[$pdata_index5], $pbits5, 0x0000_0000, + bbb[$pf1] ^ bbb[$pf2] ^ bbb[$pf3]); )* + + swap_idx(&mut bb, &mut bbb, 4); + + + $h[0] = $h[0].wrapping_add(bb[0]); + $h[1] = $h[1].wrapping_add(bb[1]); + $h[2] = $h[2].wrapping_add(bb[2]); + $h[3] = $h[3].wrapping_add(bb[3]); + $h[4] = $h[4].wrapping_add(bb[4]); + $h[5] = $h[5].wrapping_add(bbb[0]); + $h[6] = $h[6].wrapping_add(bbb[1]); + $h[7] = $h[7].wrapping_add(bbb[2]); + $h[8] = $h[8].wrapping_add(bbb[3]); + $h[9] = $h[9].wrapping_add(bbb[4]); + + }); +); + +pub fn process_msg_block(h: &mut [u32; DIGEST_BUF_LEN], data: &Block) { + let mut w = [0u32; WORK_BUF_LEN]; + LE::read_u32_into(data, &mut w[0..16]); + process_block!(h, w[..], + // Round 1 + round1: h_ordering 0, 1, 2, 3, 4; data_index 0; roll_shift 11 + round1: h_ordering 4, 0, 1, 2, 3; data_index 1; roll_shift 14 + round1: h_ordering 3, 4, 0, 1, 2; data_index 2; roll_shift 15 + round1: h_ordering 2, 3, 4, 0, 1; data_index 3; roll_shift 12 + round1: h_ordering 1, 2, 3, 4, 0; data_index 4; roll_shift 5 + round1: h_ordering 0, 1, 2, 3, 4; data_index 5; roll_shift 8 + round1: h_ordering 4, 0, 1, 2, 3; data_index 6; roll_shift 7 + round1: h_ordering 3, 4, 0, 1, 2; data_index 7; roll_shift 9 + round1: h_ordering 2, 3, 4, 0, 1; data_index 8; roll_shift 11 + round1: h_ordering 1, 2, 3, 4, 0; data_index 9; roll_shift 13 + round1: h_ordering 0, 1, 2, 3, 4; data_index 10; roll_shift 14 + round1: h_ordering 4, 0, 1, 2, 3; data_index 11; roll_shift 15 + round1: h_ordering 3, 4, 0, 1, 2; data_index 12; roll_shift 6 + round1: h_ordering 2, 3, 4, 0, 1; data_index 13; roll_shift 7 + round1: h_ordering 1, 2, 3, 4, 0; data_index 14; roll_shift 9 + round1: h_ordering 0, 1, 2, 3, 4; data_index 15; roll_shift 8; + + // Round 2 + round2: h_ordering 4, 0, 1, 2, 3; data_index 7; roll_shift 7 + round2: h_ordering 3, 4, 0, 1, 2; data_index 4; roll_shift 6 + round2: h_ordering 2, 3, 4, 0, 1; data_index 13; roll_shift 8 + round2: h_ordering 1, 2, 3, 4, 0; data_index 1; roll_shift 13 + round2: h_ordering 0, 1, 2, 3, 4; data_index 10; roll_shift 11 + round2: h_ordering 4, 0, 1, 2, 3; data_index 6; roll_shift 9 + round2: h_ordering 3, 4, 0, 1, 2; data_index 15; roll_shift 7 + round2: h_ordering 2, 3, 4, 0, 1; data_index 3; roll_shift 15 + round2: h_ordering 1, 2, 3, 4, 0; data_index 12; roll_shift 7 + round2: h_ordering 0, 1, 2, 3, 4; data_index 0; roll_shift 12 + round2: h_ordering 4, 0, 1, 2, 3; data_index 9; roll_shift 15 + round2: h_ordering 3, 4, 0, 1, 2; data_index 5; roll_shift 9 + round2: h_ordering 2, 3, 4, 0, 1; data_index 2; roll_shift 11 + round2: h_ordering 1, 2, 3, 4, 0; data_index 14; roll_shift 7 + round2: h_ordering 0, 1, 2, 3, 4; data_index 11; roll_shift 13 + round2: h_ordering 4, 0, 1, 2, 3; data_index 8; roll_shift 12; + + // Round 3 + round3: h_ordering 3, 4, 0, 1, 2; data_index 3; roll_shift 11 + round3: h_ordering 2, 3, 4, 0, 1; data_index 10; roll_shift 13 + round3: h_ordering 1, 2, 3, 4, 0; data_index 14; roll_shift 6 + round3: h_ordering 0, 1, 2, 3, 4; data_index 4; roll_shift 7 + round3: h_ordering 4, 0, 1, 2, 3; data_index 9; roll_shift 14 + round3: h_ordering 3, 4, 0, 1, 2; data_index 15; roll_shift 9 + round3: h_ordering 2, 3, 4, 0, 1; data_index 8; roll_shift 13 + round3: h_ordering 1, 2, 3, 4, 0; data_index 1; roll_shift 15 + round3: h_ordering 0, 1, 2, 3, 4; data_index 2; roll_shift 14 + round3: h_ordering 4, 0, 1, 2, 3; data_index 7; roll_shift 8 + round3: h_ordering 3, 4, 0, 1, 2; data_index 0; roll_shift 13 + round3: h_ordering 2, 3, 4, 0, 1; data_index 6; roll_shift 6 + round3: h_ordering 1, 2, 3, 4, 0; data_index 13; roll_shift 5 + round3: h_ordering 0, 1, 2, 3, 4; data_index 11; roll_shift 12 + round3: h_ordering 4, 0, 1, 2, 3; data_index 5; roll_shift 7 + round3: h_ordering 3, 4, 0, 1, 2; data_index 12; roll_shift 5; + + // Round 4 + round4: h_ordering 2, 3, 4, 0, 1; data_index 1; roll_shift 11 + round4: h_ordering 1, 2, 3, 4, 0; data_index 9; roll_shift 12 + round4: h_ordering 0, 1, 2, 3, 4; data_index 11; roll_shift 14 + round4: h_ordering 4, 0, 1, 2, 3; data_index 10; roll_shift 15 + round4: h_ordering 3, 4, 0, 1, 2; data_index 0; roll_shift 14 + round4: h_ordering 2, 3, 4, 0, 1; data_index 8; roll_shift 15 + round4: h_ordering 1, 2, 3, 4, 0; data_index 12; roll_shift 9 + round4: h_ordering 0, 1, 2, 3, 4; data_index 4; roll_shift 8 + round4: h_ordering 4, 0, 1, 2, 3; data_index 13; roll_shift 9 + round4: h_ordering 3, 4, 0, 1, 2; data_index 3; roll_shift 14 + round4: h_ordering 2, 3, 4, 0, 1; data_index 7; roll_shift 5 + round4: h_ordering 1, 2, 3, 4, 0; data_index 15; roll_shift 6 + round4: h_ordering 0, 1, 2, 3, 4; data_index 14; roll_shift 8 + round4: h_ordering 4, 0, 1, 2, 3; data_index 5; roll_shift 6 + round4: h_ordering 3, 4, 0, 1, 2; data_index 6; roll_shift 5 + round4: h_ordering 2, 3, 4, 0, 1; data_index 2; roll_shift 12; + + // Round 5 + round5: h_ordering 1, 2, 3, 4, 0; data_index 4; roll_shift 9 + round5: h_ordering 0, 1, 2, 3, 4; data_index 0; roll_shift 15 + round5: h_ordering 4, 0, 1, 2, 3; data_index 5; roll_shift 5 + round5: h_ordering 3, 4, 0, 1, 2; data_index 9; roll_shift 11 + round5: h_ordering 2, 3, 4, 0, 1; data_index 7; roll_shift 6 + round5: h_ordering 1, 2, 3, 4, 0; data_index 12; roll_shift 8 + round5: h_ordering 0, 1, 2, 3, 4; data_index 2; roll_shift 13 + round5: h_ordering 4, 0, 1, 2, 3; data_index 10; roll_shift 12 + round5: h_ordering 3, 4, 0, 1, 2; data_index 14; roll_shift 5 + round5: h_ordering 2, 3, 4, 0, 1; data_index 1; roll_shift 12 + round5: h_ordering 1, 2, 3, 4, 0; data_index 3; roll_shift 13 + round5: h_ordering 0, 1, 2, 3, 4; data_index 8; roll_shift 14 + round5: h_ordering 4, 0, 1, 2, 3; data_index 11; roll_shift 11 + round5: h_ordering 3, 4, 0, 1, 2; data_index 6; roll_shift 8 + round5: h_ordering 2, 3, 4, 0, 1; data_index 15; roll_shift 5 + round5: h_ordering 1, 2, 3, 4, 0; data_index 13; roll_shift 6; + + // Parallel Round 1 + par_round1: h_ordering 0, 1, 2, 3, 4; data_index 5; roll_shift 8 + par_round1: h_ordering 4, 0, 1, 2, 3; data_index 14; roll_shift 9 + par_round1: h_ordering 3, 4, 0, 1, 2; data_index 7; roll_shift 9 + par_round1: h_ordering 2, 3, 4, 0, 1; data_index 0; roll_shift 11 + par_round1: h_ordering 1, 2, 3, 4, 0; data_index 9; roll_shift 13 + par_round1: h_ordering 0, 1, 2, 3, 4; data_index 2; roll_shift 15 + par_round1: h_ordering 4, 0, 1, 2, 3; data_index 11; roll_shift 15 + par_round1: h_ordering 3, 4, 0, 1, 2; data_index 4; roll_shift 5 + par_round1: h_ordering 2, 3, 4, 0, 1; data_index 13; roll_shift 7 + par_round1: h_ordering 1, 2, 3, 4, 0; data_index 6; roll_shift 7 + par_round1: h_ordering 0, 1, 2, 3, 4; data_index 15; roll_shift 8 + par_round1: h_ordering 4, 0, 1, 2, 3; data_index 8; roll_shift 11 + par_round1: h_ordering 3, 4, 0, 1, 2; data_index 1; roll_shift 14 + par_round1: h_ordering 2, 3, 4, 0, 1; data_index 10; roll_shift 14 + par_round1: h_ordering 1, 2, 3, 4, 0; data_index 3; roll_shift 12 + par_round1: h_ordering 0, 1, 2, 3, 4; data_index 12; roll_shift 6; + + // Parallel Round 2 + par_round2: h_ordering 4, 0, 1, 2, 3; data_index 6; roll_shift 9 + par_round2: h_ordering 3, 4, 0, 1, 2; data_index 11; roll_shift 13 + par_round2: h_ordering 2, 3, 4, 0, 1; data_index 3; roll_shift 15 + par_round2: h_ordering 1, 2, 3, 4, 0; data_index 7; roll_shift 7 + par_round2: h_ordering 0, 1, 2, 3, 4; data_index 0; roll_shift 12 + par_round2: h_ordering 4, 0, 1, 2, 3; data_index 13; roll_shift 8 + par_round2: h_ordering 3, 4, 0, 1, 2; data_index 5; roll_shift 9 + par_round2: h_ordering 2, 3, 4, 0, 1; data_index 10; roll_shift 11 + par_round2: h_ordering 1, 2, 3, 4, 0; data_index 14; roll_shift 7 + par_round2: h_ordering 0, 1, 2, 3, 4; data_index 15; roll_shift 7 + par_round2: h_ordering 4, 0, 1, 2, 3; data_index 8; roll_shift 12 + par_round2: h_ordering 3, 4, 0, 1, 2; data_index 12; roll_shift 7 + par_round2: h_ordering 2, 3, 4, 0, 1; data_index 4; roll_shift 6 + par_round2: h_ordering 1, 2, 3, 4, 0; data_index 9; roll_shift 15 + par_round2: h_ordering 0, 1, 2, 3, 4; data_index 1; roll_shift 13 + par_round2: h_ordering 4, 0, 1, 2, 3; data_index 2; roll_shift 11; + + // Parallel Round 3 + par_round3: h_ordering 3, 4, 0, 1, 2; data_index 15; roll_shift 9 + par_round3: h_ordering 2, 3, 4, 0, 1; data_index 5; roll_shift 7 + par_round3: h_ordering 1, 2, 3, 4, 0; data_index 1; roll_shift 15 + par_round3: h_ordering 0, 1, 2, 3, 4; data_index 3; roll_shift 11 + par_round3: h_ordering 4, 0, 1, 2, 3; data_index 7; roll_shift 8 + par_round3: h_ordering 3, 4, 0, 1, 2; data_index 14; roll_shift 6 + par_round3: h_ordering 2, 3, 4, 0, 1; data_index 6; roll_shift 6 + par_round3: h_ordering 1, 2, 3, 4, 0; data_index 9; roll_shift 14 + par_round3: h_ordering 0, 1, 2, 3, 4; data_index 11; roll_shift 12 + par_round3: h_ordering 4, 0, 1, 2, 3; data_index 8; roll_shift 13 + par_round3: h_ordering 3, 4, 0, 1, 2; data_index 12; roll_shift 5 + par_round3: h_ordering 2, 3, 4, 0, 1; data_index 2; roll_shift 14 + par_round3: h_ordering 1, 2, 3, 4, 0; data_index 10; roll_shift 13 + par_round3: h_ordering 0, 1, 2, 3, 4; data_index 0; roll_shift 13 + par_round3: h_ordering 4, 0, 1, 2, 3; data_index 4; roll_shift 7 + par_round3: h_ordering 3, 4, 0, 1, 2; data_index 13; roll_shift 5; + + // Parallel Round 4 + par_round4: h_ordering 2, 3, 4, 0, 1; data_index 8; roll_shift 15 + par_round4: h_ordering 1, 2, 3, 4, 0; data_index 6; roll_shift 5 + par_round4: h_ordering 0, 1, 2, 3, 4; data_index 4; roll_shift 8 + par_round4: h_ordering 4, 0, 1, 2, 3; data_index 1; roll_shift 11 + par_round4: h_ordering 3, 4, 0, 1, 2; data_index 3; roll_shift 14 + par_round4: h_ordering 2, 3, 4, 0, 1; data_index 11; roll_shift 14 + par_round4: h_ordering 1, 2, 3, 4, 0; data_index 15; roll_shift 6 + par_round4: h_ordering 0, 1, 2, 3, 4; data_index 0; roll_shift 14 + par_round4: h_ordering 4, 0, 1, 2, 3; data_index 5; roll_shift 6 + par_round4: h_ordering 3, 4, 0, 1, 2; data_index 12; roll_shift 9 + par_round4: h_ordering 2, 3, 4, 0, 1; data_index 2; roll_shift 12 + par_round4: h_ordering 1, 2, 3, 4, 0; data_index 13; roll_shift 9 + par_round4: h_ordering 0, 1, 2, 3, 4; data_index 9; roll_shift 12 + par_round4: h_ordering 4, 0, 1, 2, 3; data_index 7; roll_shift 5 + par_round4: h_ordering 3, 4, 0, 1, 2; data_index 10; roll_shift 15 + par_round4: h_ordering 2, 3, 4, 0, 1; data_index 14; roll_shift 8; + + // Parallel Round 5 + par_round5: h_ordering 1, 2, 3, 4, 0; data_index 12; roll_shift 8 + par_round5: h_ordering 0, 1, 2, 3, 4; data_index 15; roll_shift 5 + par_round5: h_ordering 4, 0, 1, 2, 3; data_index 10; roll_shift 12 + par_round5: h_ordering 3, 4, 0, 1, 2; data_index 4; roll_shift 9 + par_round5: h_ordering 2, 3, 4, 0, 1; data_index 1; roll_shift 12 + par_round5: h_ordering 1, 2, 3, 4, 0; data_index 5; roll_shift 5 + par_round5: h_ordering 0, 1, 2, 3, 4; data_index 8; roll_shift 14 + par_round5: h_ordering 4, 0, 1, 2, 3; data_index 7; roll_shift 6 + par_round5: h_ordering 3, 4, 0, 1, 2; data_index 6; roll_shift 8 + par_round5: h_ordering 2, 3, 4, 0, 1; data_index 2; roll_shift 13 + par_round5: h_ordering 1, 2, 3, 4, 0; data_index 13; roll_shift 6 + par_round5: h_ordering 0, 1, 2, 3, 4; data_index 14; roll_shift 5 + par_round5: h_ordering 4, 0, 1, 2, 3; data_index 0; roll_shift 15 + par_round5: h_ordering 3, 4, 0, 1, 2; data_index 3; roll_shift 13 + par_round5: h_ordering 2, 3, 4, 0, 1; data_index 9; roll_shift 11 + par_round5: h_ordering 1, 2, 3, 4, 0; data_index 11; roll_shift 11; + ); +} diff --git a/ripemd320/src/lib.rs b/ripemd320/src/lib.rs new file mode 100644 index 000000000..5ebb8a62f --- /dev/null +++ b/ripemd320/src/lib.rs @@ -0,0 +1,108 @@ +//! An implementation of the [RIPEMD-320][1] cryptographic hash. +//! +//! # Usage +//! +//! ```rust +//! # #[macro_use] extern crate hex_literal; +//! # extern crate ripemd320; +//! # fn main() { +//! use ripemd320::{Ripemd320, Digest}; +//! +//! // create a RIPEMD-320 hasher instance +//! let mut hasher = Ripemd320::new(); +//! +//! // process input message +//! hasher.input(b"Hello world!"); +//! +//! // acquire hash digest in the form of GenericArray, +//! // which in this case is equivalent to [u8; 40] +//! let expected = hex!("f1c1c231d301abcf2d7daae0269ff3e7bc68e623ad723aa068d316b056d26b7d1bb6f0cc0f28336d"); +//! let result = hasher.result(); +//! assert_eq!(&result[..], &expected[..]); +//! # } +//! ``` +//! +//! Also see [RustCrypto/hashes][2] readme. +//! +//! [1]: https://en.wikipedia.org/wiki/RIPEMD +//! [2]: https://github.com/RustCrypto/hashes +#![no_std] +#![doc(html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo_small.png")] +extern crate block_buffer; +#[macro_use] +extern crate opaque_debug; +#[macro_use] +pub extern crate digest; +#[cfg(feature = "std")] +extern crate std; + + +use block_buffer::byteorder::{ByteOrder, LE}; +use block_buffer::BlockBuffer; +use digest::generic_array::typenum::{U40, U64}; +use digest::generic_array::GenericArray; +pub use digest::Digest; +use digest::{BlockInput, FixedOutput, Input, Reset}; + +mod block; +use block::{process_msg_block, DIGEST_BUF_LEN, H0}; + +/// Structure representing the state of a ripemd320 computation +#[derive(Clone)] +pub struct Ripemd320 { + h: [u32; DIGEST_BUF_LEN], + len: u64, + buffer: BlockBuffer, +} + +impl Default for Ripemd320 { + fn default() -> Self { + Ripemd320 { + h: H0, + len: 0, + buffer: Default::default(), + } + } +} + +impl BlockInput for Ripemd320 { + type BlockSize = U64; +} + +impl Input for Ripemd320 { + fn input>(&mut self, input: B) { + let input = input.as_ref(); + // Assumes that input.len() can be converted to u64 without overflow + self.len += input.len() as u64; + let h = &mut self.h; + self.buffer.input(input, |b| process_msg_block(h, b)); + } +} + +impl FixedOutput for Ripemd320 { + type OutputSize = U40; + + fn fixed_result(mut self) -> GenericArray { + { + let h = &mut self.h; + let l = self.len << 3; + self.buffer + .len64_padding::(l, |b| process_msg_block(h, b)); + } + + let mut out = GenericArray::default(); + LE::write_u32_into(&self.h, &mut out[..]); + out + } +} + +impl Reset for Ripemd320 { + fn reset(&mut self) { + self.buffer.reset(); + self.len = 0; + self.h = H0; + } +} + +impl_opaque_debug!(Ripemd320); +impl_write!(Ripemd320); diff --git a/ripemd320/tests/data/one_million_a.bin b/ripemd320/tests/data/one_million_a.bin new file mode 100644 index 000000000..ebf119470 --- /dev/null +++ b/ripemd320/tests/data/one_million_a.bin @@ -0,0 +1 @@ +½î7ô7 dk‹ †-Ú)*ão@–^Œ… æ=½ÞÌP>+cë’E»f \ No newline at end of file diff --git a/ripemd320/tests/data/ripemd320.blb b/ripemd320/tests/data/ripemd320.blb new file mode 100644 index 000000000..2b5f1d993 Binary files /dev/null and b/ripemd320/tests/data/ripemd320.blb differ diff --git a/ripemd320/tests/lib.rs b/ripemd320/tests/lib.rs new file mode 100644 index 000000000..95eb98c99 --- /dev/null +++ b/ripemd320/tests/lib.rs @@ -0,0 +1,22 @@ +//! Test messages from FIPS 180-1 and from the RIPEMD-160 webpage[1] +//! [1] https://homes.esat.kuleuven.be/~bosselae/ripemd160.html +#![no_std] + +#[macro_use] +extern crate digest; +extern crate ripemd320; + +use digest::dev::{digest_test, one_million_a}; + +new_test!( + ripemd320_main, + "ripemd320", + ripemd320::Ripemd320, + digest_test +); + +#[test] +fn ripemd320_1million_a() { + let output = include_bytes!("data/one_million_a.bin"); + one_million_a::(&output[..]); +}