From 801b42b04f7495be0d44731456acbbc95256e740 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Mon, 20 Nov 2017 10:29:47 +0300 Subject: [PATCH 01/10] pmac draft --- Cargo.toml | 1 + pmac/Cargo.toml | 17 ++++ pmac/LICENSE-APACHE | 201 ++++++++++++++++++++++++++++++++++++++++++++ pmac/LICENSE-MIT | 25 ++++++ pmac/src/double.rs | 98 +++++++++++++++++++++ pmac/src/lib.rs | 197 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 539 insertions(+) create mode 100644 pmac/Cargo.toml create mode 100644 pmac/LICENSE-APACHE create mode 100644 pmac/LICENSE-MIT create mode 100644 pmac/src/double.rs create mode 100644 pmac/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 551d24c..040c8ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "cmac", "hmac", + "pmac", ] [patch.crates-io] diff --git a/pmac/Cargo.toml b/pmac/Cargo.toml new file mode 100644 index 0000000..fa96d6e --- /dev/null +++ b/pmac/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "pmac" +version = "0.1.0" +authors = ["RustCrypto Developers"] +license = "MIT/Apache-2.0" +description = "Generic implementation of Parallelizable Message Authentication Code" +documentation = "https://docs.rs/cmac" +repository = "https://github.com/RustCrypto/MACs" +keywords = ["crypto", "mac", "pmac"] + +[dependencies] +crypto-mac = "0.5" +block-cipher-trait = "0.5" + +[dev-dependencies] +crypto-mac = { version = "0.5.1", features = ["dev"] } +aesni = "0.2" diff --git a/pmac/LICENSE-APACHE b/pmac/LICENSE-APACHE new file mode 100644 index 0000000..78173fa --- /dev/null +++ b/pmac/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/pmac/LICENSE-MIT b/pmac/LICENSE-MIT new file mode 100644 index 0000000..8dcb85b --- /dev/null +++ b/pmac/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright (c) 2017 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/pmac/src/double.rs b/pmac/src/double.rs new file mode 100644 index 0000000..594bae5 --- /dev/null +++ b/pmac/src/double.rs @@ -0,0 +1,98 @@ +use block_cipher_trait::generic_array::typenum::{U8, U16, U32}; +use super::Block; + +use core::mem; + +/// Block doublable over Galois Field. +/// +/// This trait is implemented for 64, 128 and 256 bit block sizes. +pub trait Doublable { + /// Return doubled value of the block. + /// + /// If most significant bit of the block equals to zero will return + /// `block<<1`, otherwise `(block<<1)^C`, where `C` is the non-leading + /// coefficients of the lexicographically first irreducible degree-b binary + /// polynomial with the minimal number of ones. + fn double(self) -> Self; + + fn inv_double(self) -> Self; +} + +impl Doublable for Block { + fn double(self) -> Self { + let mut val: u64 = unsafe { mem::transmute(self) }; + val = val.to_be(); + val = if val >> 63 == 1 { + (val << 1) ^ 0b11011 + } else { + val << 1 + }; + unsafe { mem::transmute(val.to_be()) } + } + + fn inv_double(self) -> Self { + self + } +} + +#[inline(always)] +fn to_be(val: &mut [u64]) { + for v in val.iter_mut() { + *v = v.to_be(); + } +} + +impl Doublable for Block { + fn double(self) -> Self { + let mut val: [u64; 2] = unsafe { mem::transmute(self) }; + to_be(&mut val); + + let mut flag = false; + + for v in val.iter_mut().rev() { + let mut t = *v << 1; + if flag { t += 1; } + + flag = (*v >> 63) == 1; + *v = t; + } + + if flag { + val[1] ^= 0b10000111; + } + + to_be(&mut val); + unsafe { mem::transmute(val) } + } + + fn inv_double(self) -> Self { + self + } +} + +impl Doublable for Block { + fn double(self) -> Self { + let mut val: [u64; 4] = unsafe { mem::transmute(self) }; + to_be(&mut val); + + let mut flag = false; + for v in val.iter_mut().rev() { + let mut t = *v << 1; + if flag { t += 1; } + + flag = (*v >> 63) == 1; + *v = t; + } + + if flag { + val[3] ^= 0b100_0010_0101; + } + + to_be(&mut val); + unsafe { mem::transmute(val) } + } + + fn inv_double(self) -> Self { + self + } +} diff --git a/pmac/src/lib.rs b/pmac/src/lib.rs new file mode 100644 index 0000000..9c22b5e --- /dev/null +++ b/pmac/src/lib.rs @@ -0,0 +1,197 @@ +//! Generic implementation of Parallelizable Message Authentication Code (PMAC), +//! otherwise known as OMAC1. +//! +//! # Usage +//! We will use AES-128 block cipher from [aesni](https://docs.rs/aesni) crate. +//! +//! To get the authentication code: +//! +//! ```rust +//! extern crate pmac; +//! extern crate aesni; +//! +//! use aesni::Aes128; +//! use pmac::{Pmac, Mac}; +//! +//! # fn main() { +//! // Create `Mac` trait implementation, namely PMAC-AES128 +//! let mut mac = Pmac::::new(b"very secret key.").unwrap(); +//! mac.input(b"input message"); +//! +//! // `result` has type `MacResult` which is a thin wrapper around array of +//! // bytes for providing constant time equality check +//! let result = mac.result(); +//! // To get underlying array use `code` method, but be carefull, since +//! // incorrect use of the code value may permit timing attacks which defeat +//! // the security provided by the `MacResult` +//! let code_bytes = result.code(); +//! # } +//! ``` +//! +//! To verify the message: +//! +//! ```rust +//! # extern crate pmac; +//! # extern crate aesni; +//! # use aesni::Aes128; +//! # use pmac::{Pmac, Mac}; +//! # fn main() { +//! let mut mac = Pmac::::new(b"very secret key.").unwrap(); +//! +//! mac.input(b"input message"); +//! +//! # let mac2 = mac.clone(); +//! # let code_bytes = mac2.result().code(); +//! // `verify` will return `Ok(())` if code is correct, `Err(MacError)` otherwise +//! mac.verify(&code_bytes).unwrap(); +//! # } +//! ``` +#![no_std] +extern crate block_cipher_trait; +pub extern crate crypto_mac; + +pub use crypto_mac::Mac; +use crypto_mac::{InvalidKeyLength, MacResult}; +use block_cipher_trait::{BlockCipher, NewVarKey}; +use block_cipher_trait::generic_array::{GenericArray, ArrayLength}; +use block_cipher_trait::generic_array::typenum::Unsigned; + +use core::slice; + +mod double; + +pub use double::Doublable; + +type Block = GenericArray; +type ParBlocks = GenericArray, M>; + +/// Generic CMAC instance +#[derive(Clone)] +pub struct Pmac { + cipher: C, + l_inv: Block, + l: Block, + buffer: ParBlocks, + tag: Block, + pos: usize, +} + +#[inline(always)] +fn xor>(buf: &mut Block, data: &Block) { + for i in 0..L::to_usize() { + buf[i] ^= data[i]; + } +} + +impl Pmac + where C: BlockCipher + NewVarKey, Block: Doublable +{ + /// Process full buffer and update tag + fn process_buffer(&mut self) { + // generate L values for xoring with buffer + let ls = { + let mut buf = ParBlocks::::default(); + let mut l_temp = self.l.clone(); + for val in buf.iter_mut() { + *val = l_temp.clone(); + l_temp = l_temp.double(); + } + self.l = l_temp; + buf + }; + // Create local buffer copy and xor Ls into it + let mut buf = self.buffer.clone(); + for (a, b) in buf.iter_mut().zip(ls.iter()) { + xor(a, b); + } + // encrypt blocks in the buffer + self.cipher.encrypt_blocks(&mut buf); + // and xor them into tag + for val in buf.iter() { + xor(&mut self.tag, val); + } + } + + /// Represent internall buffer as bytes slice (hopefully in future we will + /// be able to switch `&mut [u8]` to `&mut [u8; BlockSize*ParBlocks]`) + #[inline(always)] + fn as_mut_bytes(&mut self) -> &mut [u8] { + unsafe { + slice::from_raw_parts_mut( + &mut self.buffer + as *mut ParBlocks + as *mut u8, + C::BlockSize::to_usize()*C::ParBlocks::to_usize(), + ) + } + } +} + +impl Mac for Pmac + where C: BlockCipher + NewVarKey, Block: Doublable +{ + type OutputSize = C::BlockSize; + + #[inline] + fn new(key: &[u8]) -> Result { + let cipher = C::new(key).map_err(|_| InvalidKeyLength)?; + + let mut l = Default::default(); + cipher.encrypt_block(&mut l); + let l_inv = l.clone().inv_double(); + + Ok(Self { + cipher, l_inv, l, + buffer: Default::default(), tag: Default::default(), pos: 0, + }) + } + + #[inline] + fn input(&mut self, mut data: &[u8]) { + let n = C::BlockSize::to_usize()*C::ParBlocks::to_usize(); + + let p = self.pos; + let rem = n - p; + if data.len() >= rem { + let (l, r) = data.split_at(rem); + data = r; + self.as_mut_bytes()[p..].clone_from_slice(l); + self.pos = 0; + self.process_buffer(); + } else { + self.as_mut_bytes()[p..p+data.len()] + .clone_from_slice(data); + self.pos += data.len(); + return; + } + + while data.len() >= n { + let (l, r) = data.split_at(n); + self.as_mut_bytes().clone_from_slice(l); + data = r; + + self.process_buffer(); + } + + if data.len() != 0 { + self.as_mut_bytes()[..data.len()].clone_from_slice(data); + self.pos = data.len(); + } + } + + #[inline] + fn result(mut self) -> MacResult { + let n = self.pos/C::BlockSize::to_usize(); + assert!(n <= C::ParBlocks::to_usize(), + "invalid buffer positions"); + for i in 0..n { + let mut buf = self.buffer[i].clone(); + xor(&mut buf, &self.l); + self.l = self.l.double(); + self.cipher.encrypt_block(&mut buf); + xor(&mut self.tag, &buf); + } + + MacResult::new(self.tag) + } +} From c0b66f5cd1b7872364201601f834a749e48a3b62 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Mon, 20 Nov 2017 15:27:12 +0300 Subject: [PATCH 02/10] working pmac impl --- pmac/src/double.rs | 108 +++++++++++++++++++--------- pmac/src/lib.rs | 106 ++++++++++++++++++++------- pmac/tests/data/aes128/1.input.bin | 0 pmac/tests/data/aes128/1.key.bin | Bin 0 -> 16 bytes pmac/tests/data/aes128/1.output.bin | 1 + pmac/tests/data/aes128/2.input.bin | Bin 0 -> 3 bytes pmac/tests/data/aes128/2.key.bin | Bin 0 -> 16 bytes pmac/tests/data/aes128/2.output.bin | 1 + pmac/tests/data/aes128/3.input.bin | Bin 0 -> 16 bytes pmac/tests/data/aes128/3.key.bin | Bin 0 -> 16 bytes pmac/tests/data/aes128/3.output.bin | 1 + pmac/tests/data/aes128/4.input.bin | Bin 0 -> 20 bytes pmac/tests/data/aes128/4.key.bin | Bin 0 -> 16 bytes pmac/tests/data/aes128/4.output.bin | 1 + pmac/tests/data/aes128/5.input.bin | Bin 0 -> 32 bytes pmac/tests/data/aes128/5.key.bin | Bin 0 -> 16 bytes pmac/tests/data/aes128/5.output.bin | 1 + pmac/tests/data/aes128/6.input.bin | Bin 0 -> 34 bytes pmac/tests/data/aes128/6.key.bin | Bin 0 -> 16 bytes pmac/tests/data/aes128/6.output.bin | 1 + pmac/tests/data/aes128/7.input.bin | Bin 0 -> 1000 bytes pmac/tests/data/aes128/7.key.bin | Bin 0 -> 16 bytes pmac/tests/data/aes128/7.output.bin | 1 + pmac/tests/data/aes192/1.input.bin | 0 pmac/tests/data/aes192/1.key.bin | Bin 0 -> 24 bytes pmac/tests/data/aes192/1.output.bin | 1 + pmac/tests/data/aes192/2.input.bin | Bin 0 -> 3 bytes pmac/tests/data/aes192/2.key.bin | Bin 0 -> 24 bytes pmac/tests/data/aes192/2.output.bin | 1 + pmac/tests/data/aes192/3.input.bin | Bin 0 -> 16 bytes pmac/tests/data/aes192/3.key.bin | Bin 0 -> 24 bytes pmac/tests/data/aes192/3.output.bin | Bin 0 -> 16 bytes pmac/tests/data/aes192/4.input.bin | Bin 0 -> 20 bytes pmac/tests/data/aes192/4.key.bin | Bin 0 -> 24 bytes pmac/tests/data/aes192/4.output.bin | 1 + pmac/tests/data/aes192/5.input.bin | Bin 0 -> 32 bytes pmac/tests/data/aes192/5.key.bin | Bin 0 -> 24 bytes pmac/tests/data/aes192/5.output.bin | 1 + pmac/tests/data/aes192/6.input.bin | Bin 0 -> 34 bytes pmac/tests/data/aes192/6.key.bin | Bin 0 -> 24 bytes pmac/tests/data/aes192/6.output.bin | 1 + pmac/tests/data/aes192/7.input.bin | Bin 0 -> 1000 bytes pmac/tests/data/aes192/7.key.bin | Bin 0 -> 24 bytes pmac/tests/data/aes192/7.output.bin | 1 + pmac/tests/data/aes256/1.input.bin | 0 pmac/tests/data/aes256/1.key.bin | Bin 0 -> 32 bytes pmac/tests/data/aes256/1.output.bin | 1 + pmac/tests/data/aes256/2.input.bin | Bin 0 -> 3 bytes pmac/tests/data/aes256/2.key.bin | Bin 0 -> 32 bytes pmac/tests/data/aes256/2.output.bin | 1 + pmac/tests/data/aes256/3.input.bin | Bin 0 -> 16 bytes pmac/tests/data/aes256/3.key.bin | Bin 0 -> 32 bytes pmac/tests/data/aes256/3.output.bin | 1 + pmac/tests/data/aes256/4.input.bin | Bin 0 -> 20 bytes pmac/tests/data/aes256/4.key.bin | Bin 0 -> 32 bytes pmac/tests/data/aes256/4.output.bin | 1 + pmac/tests/data/aes256/5.input.bin | Bin 0 -> 32 bytes pmac/tests/data/aes256/5.key.bin | Bin 0 -> 32 bytes pmac/tests/data/aes256/5.output.bin | 1 + pmac/tests/data/aes256/6.input.bin | Bin 0 -> 34 bytes pmac/tests/data/aes256/6.key.bin | Bin 0 -> 32 bytes pmac/tests/data/aes256/6.output.bin | 1 + pmac/tests/data/aes256/7.input.bin | Bin 0 -> 1000 bytes pmac/tests/data/aes256/7.key.bin | Bin 0 -> 32 bytes pmac/tests/data/aes256/7.output.bin | 1 + pmac/tests/lib.rs | 39 ++++++++++ 66 files changed, 216 insertions(+), 57 deletions(-) create mode 100644 pmac/tests/data/aes128/1.input.bin create mode 100644 pmac/tests/data/aes128/1.key.bin create mode 100644 pmac/tests/data/aes128/1.output.bin create mode 100644 pmac/tests/data/aes128/2.input.bin create mode 100644 pmac/tests/data/aes128/2.key.bin create mode 100644 pmac/tests/data/aes128/2.output.bin create mode 100644 pmac/tests/data/aes128/3.input.bin create mode 100644 pmac/tests/data/aes128/3.key.bin create mode 100644 pmac/tests/data/aes128/3.output.bin create mode 100644 pmac/tests/data/aes128/4.input.bin create mode 100644 pmac/tests/data/aes128/4.key.bin create mode 100644 pmac/tests/data/aes128/4.output.bin create mode 100644 pmac/tests/data/aes128/5.input.bin create mode 100644 pmac/tests/data/aes128/5.key.bin create mode 100644 pmac/tests/data/aes128/5.output.bin create mode 100644 pmac/tests/data/aes128/6.input.bin create mode 100644 pmac/tests/data/aes128/6.key.bin create mode 100644 pmac/tests/data/aes128/6.output.bin create mode 100644 pmac/tests/data/aes128/7.input.bin create mode 100644 pmac/tests/data/aes128/7.key.bin create mode 100644 pmac/tests/data/aes128/7.output.bin create mode 100644 pmac/tests/data/aes192/1.input.bin create mode 100644 pmac/tests/data/aes192/1.key.bin create mode 100644 pmac/tests/data/aes192/1.output.bin create mode 100644 pmac/tests/data/aes192/2.input.bin create mode 100644 pmac/tests/data/aes192/2.key.bin create mode 100644 pmac/tests/data/aes192/2.output.bin create mode 100644 pmac/tests/data/aes192/3.input.bin create mode 100644 pmac/tests/data/aes192/3.key.bin create mode 100644 pmac/tests/data/aes192/3.output.bin create mode 100644 pmac/tests/data/aes192/4.input.bin create mode 100644 pmac/tests/data/aes192/4.key.bin create mode 100644 pmac/tests/data/aes192/4.output.bin create mode 100644 pmac/tests/data/aes192/5.input.bin create mode 100644 pmac/tests/data/aes192/5.key.bin create mode 100644 pmac/tests/data/aes192/5.output.bin create mode 100644 pmac/tests/data/aes192/6.input.bin create mode 100644 pmac/tests/data/aes192/6.key.bin create mode 100644 pmac/tests/data/aes192/6.output.bin create mode 100644 pmac/tests/data/aes192/7.input.bin create mode 100644 pmac/tests/data/aes192/7.key.bin create mode 100644 pmac/tests/data/aes192/7.output.bin create mode 100644 pmac/tests/data/aes256/1.input.bin create mode 100644 pmac/tests/data/aes256/1.key.bin create mode 100644 pmac/tests/data/aes256/1.output.bin create mode 100644 pmac/tests/data/aes256/2.input.bin create mode 100644 pmac/tests/data/aes256/2.key.bin create mode 100644 pmac/tests/data/aes256/2.output.bin create mode 100644 pmac/tests/data/aes256/3.input.bin create mode 100644 pmac/tests/data/aes256/3.key.bin create mode 100644 pmac/tests/data/aes256/3.output.bin create mode 100644 pmac/tests/data/aes256/4.input.bin create mode 100644 pmac/tests/data/aes256/4.key.bin create mode 100644 pmac/tests/data/aes256/4.output.bin create mode 100644 pmac/tests/data/aes256/5.input.bin create mode 100644 pmac/tests/data/aes256/5.key.bin create mode 100644 pmac/tests/data/aes256/5.output.bin create mode 100644 pmac/tests/data/aes256/6.input.bin create mode 100644 pmac/tests/data/aes256/6.key.bin create mode 100644 pmac/tests/data/aes256/6.output.bin create mode 100644 pmac/tests/data/aes256/7.input.bin create mode 100644 pmac/tests/data/aes256/7.key.bin create mode 100644 pmac/tests/data/aes256/7.output.bin create mode 100644 pmac/tests/lib.rs diff --git a/pmac/src/double.rs b/pmac/src/double.rs index 594bae5..7e4d2e6 100644 --- a/pmac/src/double.rs +++ b/pmac/src/double.rs @@ -3,11 +3,16 @@ use super::Block; use core::mem; -/// Block doublable over Galois Field. +const C64: u64 = 0b1_1011; +const C128: u64 = 0b1000_0111; +const C256: u64 = 0b100_0010_0101; + +/// Some block operations over Galois Field. /// /// This trait is implemented for 64, 128 and 256 bit block sizes. +/// IMPORTANT: block must be aligned pub trait Doublable { - /// Return doubled value of the block. + /// Multiply block by x /// /// If most significant bit of the block equals to zero will return /// `block<<1`, otherwise `(block<<1)^C`, where `C` is the non-leading @@ -15,6 +20,10 @@ pub trait Doublable { /// polynomial with the minimal number of ones. fn double(self) -> Self; + /// Divide block by x + /// + /// If least significant bit of the block equals to zero will return + /// `block>>1`, otherwise `(block>>1)^(1<>1)` fn inv_double(self) -> Self; } @@ -22,16 +31,21 @@ impl Doublable for Block { fn double(self) -> Self { let mut val: u64 = unsafe { mem::transmute(self) }; val = val.to_be(); - val = if val >> 63 == 1 { - (val << 1) ^ 0b11011 - } else { - val << 1 - }; + let a = val >> 63; + val <<= 1; + val ^= a * C64; unsafe { mem::transmute(val.to_be()) } } fn inv_double(self) -> Self { - self + let mut val: u64 = unsafe { mem::transmute(self) }; + val = val.to_be(); + + let a = val & 1; + val >>= 1; + val ^= a*((1 << 63) ^ (C64 >> 1)); + + unsafe { mem::transmute(val.to_be()) } } } @@ -47,26 +61,33 @@ impl Doublable for Block { let mut val: [u64; 2] = unsafe { mem::transmute(self) }; to_be(&mut val); - let mut flag = false; + let b = val[1] >> 63; + let a = val[0] >> 63; - for v in val.iter_mut().rev() { - let mut t = *v << 1; - if flag { t += 1; } - - flag = (*v >> 63) == 1; - *v = t; - } - - if flag { - val[1] ^= 0b10000111; - } + val[0] <<= 1; + val[0] ^= b; + val[1] <<= 1; + val[1] ^= a*C128; to_be(&mut val); unsafe { mem::transmute(val) } } fn inv_double(self) -> Self { - self + let mut val: [u64; 2] = unsafe { mem::transmute(self) }; + to_be(&mut val); + + let a = (val[0] & 1) << 63; + let b = val[1] & 1; + + val[0] >>= 1; + val[1] >>= 1; + val[1] ^= a; + val[0] ^= b*(1 << 63); + val[1] ^= b*(C128 >> 1); + + to_be(&mut val); + unsafe { mem::transmute(val) } } } @@ -75,24 +96,45 @@ impl Doublable for Block { let mut val: [u64; 4] = unsafe { mem::transmute(self) }; to_be(&mut val); - let mut flag = false; - for v in val.iter_mut().rev() { - let mut t = *v << 1; - if flag { t += 1; } + let a = val[0] >> 63; + let b = val[1] >> 63; + let c = val[2] >> 63; + let d = val[3] >> 63; - flag = (*v >> 63) == 1; - *v = t; - } - - if flag { - val[3] ^= 0b100_0010_0101; - } + val[0] <<= 1; + val[0] ^= b; + val[1] <<= 1; + val[1] ^= c; + val[2] <<= 1; + val[2] ^= d; + val[3] <<= 1; + val[3] ^= a*C256; to_be(&mut val); unsafe { mem::transmute(val) } } fn inv_double(self) -> Self { - self + let mut val: [u64; 4] = unsafe { mem::transmute(self) }; + to_be(&mut val); + + let a = (val[0] & 1) << 63; + let b = (val[1] & 1) << 63; + let c = (val[2] & 1) << 63; + let d = val[3] & 1; + + val[0] >>= 1; + val[1] >>= 1; + val[2] >>= 1; + val[3] >>= 1; + val[1] ^= a; + val[2] ^= b; + val[3] ^= c; + + val[0] ^= d*(1 << 63); + val[3] ^= d*(C256 >> 1); + + to_be(&mut val); + unsafe { mem::transmute(val) } } } diff --git a/pmac/src/lib.rs b/pmac/src/lib.rs index 9c22b5e..b34143a 100644 --- a/pmac/src/lib.rs +++ b/pmac/src/lib.rs @@ -65,15 +65,20 @@ pub use double::Doublable; type Block = GenericArray; type ParBlocks = GenericArray, M>; +// in future it can be parameter of Pmac +const LC_SIZE: usize = 16; + /// Generic CMAC instance #[derive(Clone)] pub struct Pmac { cipher: C, l_inv: Block, - l: Block, + l_cache: [Block; LC_SIZE], buffer: ParBlocks, tag: Block, + offset: Block, pos: usize, + counter: usize, } #[inline(always)] @@ -87,21 +92,22 @@ impl Pmac where C: BlockCipher + NewVarKey, Block: Doublable { /// Process full buffer and update tag + #[inline(always)] fn process_buffer(&mut self) { - // generate L values for xoring with buffer - let ls = { + // generate values for xoring with buffer + let t = { let mut buf = ParBlocks::::default(); - let mut l_temp = self.l.clone(); for val in buf.iter_mut() { - *val = l_temp.clone(); - l_temp = l_temp.double(); + let l = self.get_l(self.counter); + xor(&mut self.offset, &l); + *val = self.offset.clone(); + self.counter += 1; } - self.l = l_temp; buf }; // Create local buffer copy and xor Ls into it let mut buf = self.buffer.clone(); - for (a, b) in buf.iter_mut().zip(ls.iter()) { + for (a, b) in buf.iter_mut().zip(t.iter()) { xor(a, b); } // encrypt blocks in the buffer @@ -125,6 +131,20 @@ impl Pmac ) } } + + #[inline(always)] + fn get_l(&self, counter: usize) -> Block { + let ntz = counter.trailing_zeros() as usize; + if ntz < LC_SIZE { + self.l_cache[ntz].clone() + } else { + let mut block = self.l_cache[LC_SIZE-1].clone(); + for _ in LC_SIZE-1..ntz { + block = block.double(); + } + block + } + } } impl Mac for Pmac @@ -136,13 +156,22 @@ impl Mac for Pmac fn new(key: &[u8]) -> Result { let cipher = C::new(key).map_err(|_| InvalidKeyLength)?; - let mut l = Default::default(); - cipher.encrypt_block(&mut l); - let l_inv = l.clone().inv_double(); + let mut l0 = Default::default(); + cipher.encrypt_block(&mut l0); + + let mut l_cache: [Block; LC_SIZE] = Default::default(); + l_cache[0] = l0.clone(); + for i in 1..LC_SIZE { + l_cache[i] = l_cache[i-1].clone().double(); + } + + let l_inv = l0.clone().inv_double(); Ok(Self { - cipher, l_inv, l, - buffer: Default::default(), tag: Default::default(), pos: 0, + cipher, l_inv, l_cache, + buffer: Default::default(), tag: Default::default(), + offset: Default::default(), + pos: 0, counter: 1, }) } @@ -156,8 +185,6 @@ impl Mac for Pmac let (l, r) = data.split_at(rem); data = r; self.as_mut_bytes()[p..].clone_from_slice(l); - self.pos = 0; - self.process_buffer(); } else { self.as_mut_bytes()[p..p+data.len()] .clone_from_slice(data); @@ -166,14 +193,17 @@ impl Mac for Pmac } while data.len() >= n { + self.process_buffer(); + let (l, r) = data.split_at(n); self.as_mut_bytes().clone_from_slice(l); data = r; - - self.process_buffer(); } + self.pos = n; + if data.len() != 0 { + self.process_buffer(); self.as_mut_bytes()[..data.len()].clone_from_slice(data); self.pos = data.len(); } @@ -181,17 +211,45 @@ impl Mac for Pmac #[inline] fn result(mut self) -> MacResult { - let n = self.pos/C::BlockSize::to_usize(); - assert!(n <= C::ParBlocks::to_usize(), - "invalid buffer positions"); + let mut tag = self.tag.clone(); + // Special case for empty input + if self.pos == 0 { + tag[0] = 0x80; + self.cipher.encrypt_block(&mut tag); + return MacResult::new(tag); + } + + let bs = C::BlockSize::to_usize(); + let k = self.pos % bs; + let is_full = k == 0; + // number of full blocks excluding last + let n = if is_full { (self.pos/bs) - 1 } else { self.pos/bs }; + assert!(n < C::ParBlocks::to_usize(), "invalid buffer positions"); + for i in 0..n { let mut buf = self.buffer[i].clone(); - xor(&mut buf, &self.l); - self.l = self.l.double(); + + let l = self.get_l(self.counter); + xor(&mut self.offset, &l); + xor(&mut buf, &self.offset); self.cipher.encrypt_block(&mut buf); - xor(&mut self.tag, &buf); + + xor(&mut tag, &buf); + self.counter += 1; + } + + if is_full { + xor(&mut tag, &self.buffer[n]); + xor(&mut tag, &self.l_inv); + } else { + let mut block = self.buffer[n].clone(); + block[k] = 0x80; + for v in block[k+1..].iter_mut() { *v = 0; } + xor(&mut tag, &block); } - MacResult::new(self.tag) + self.cipher.encrypt_block(&mut tag); + + MacResult::new(tag) } } diff --git a/pmac/tests/data/aes128/1.input.bin b/pmac/tests/data/aes128/1.input.bin new file mode 100644 index 0000000..e69de29 diff --git a/pmac/tests/data/aes128/1.key.bin b/pmac/tests/data/aes128/1.key.bin new file mode 100644 index 0000000000000000000000000000000000000000..b66efb8adab7795606f4ebbc70be4c0a1d047a52 GIT binary patch literal 16 XcmZQzWMXDvWn<^yLnc0=NKq literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes128/1.output.bin b/pmac/tests/data/aes128/1.output.bin new file mode 100644 index 0000000..e366486 --- /dev/null +++ b/pmac/tests/data/aes128/1.output.bin @@ -0,0 +1 @@ +CW,SAXv \ No newline at end of file diff --git a/pmac/tests/data/aes128/2.input.bin b/pmac/tests/data/aes128/2.input.bin new file mode 100644 index 0000000000000000000000000000000000000000..8352675d67aed6625ece79af41c27fdb4ee2e867 GIT binary patch literal 3 KcmZQzWC8#H2LJ>B literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes128/2.key.bin b/pmac/tests/data/aes128/2.key.bin new file mode 100644 index 0000000000000000000000000000000000000000..b66efb8adab7795606f4ebbc70be4c0a1d047a52 GIT binary patch literal 16 XcmZQzWMXDvWn<^yLnc0=NKq literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes128/2.output.bin b/pmac/tests/data/aes128/2.output.bin new file mode 100644 index 0000000..36ec338 --- /dev/null +++ b/pmac/tests/data/aes128/2.output.bin @@ -0,0 +1 @@ +%k<M8' \ No newline at end of file diff --git a/pmac/tests/data/aes128/3.input.bin b/pmac/tests/data/aes128/3.input.bin new file mode 100644 index 0000000000000000000000000000000000000000..b66efb8adab7795606f4ebbc70be4c0a1d047a52 GIT binary patch literal 16 XcmZQzWMXDvWn<^yLnc0=NKq literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes128/3.key.bin b/pmac/tests/data/aes128/3.key.bin new file mode 100644 index 0000000000000000000000000000000000000000..b66efb8adab7795606f4ebbc70be4c0a1d047a52 GIT binary patch literal 16 XcmZQzWMXDvWn<^yLnc0=NKq literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes128/3.output.bin b/pmac/tests/data/aes128/3.output.bin new file mode 100644 index 0000000..c4a2117 --- /dev/null +++ b/pmac/tests/data/aes128/3.output.bin @@ -0,0 +1 @@ +뽂/X}c8 \ No newline at end of file diff --git a/pmac/tests/data/aes128/4.input.bin b/pmac/tests/data/aes128/4.input.bin new file mode 100644 index 0000000000000000000000000000000000000000..dc8cabcb1f324b0841ba2624c4e06bdf1d78c325 GIT binary patch literal 20 bcmZQzWMXDvWn<^yMC+6cPpi1x5hB literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes128/4.key.bin b/pmac/tests/data/aes128/4.key.bin new file mode 100644 index 0000000000000000000000000000000000000000..b66efb8adab7795606f4ebbc70be4c0a1d047a52 GIT binary patch literal 16 XcmZQzWMXDvWn<^yLnc0=NKq literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes128/4.output.bin b/pmac/tests/data/aes128/4.output.bin new file mode 100644 index 0000000..0c74fff --- /dev/null +++ b/pmac/tests/data/aes128/4.output.bin @@ -0,0 +1 @@ + yu?U \ No newline at end of file diff --git a/pmac/tests/data/aes128/5.input.bin b/pmac/tests/data/aes128/5.input.bin new file mode 100644 index 0000000000000000000000000000000000000000..fefa1cc823e0ec463b7923972b03901b52808fc1 GIT binary patch literal 32 ncmZQzWMXDvWn<^yMC+6cQE@6%&_`l#-T_m6Hbm6>tIZ literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes128/5.key.bin b/pmac/tests/data/aes128/5.key.bin new file mode 100644 index 0000000000000000000000000000000000000000..b66efb8adab7795606f4ebbc70be4c0a1d047a52 GIT binary patch literal 16 XcmZQzWMXDvWn<^yLnc0=NKq literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes128/5.output.bin b/pmac/tests/data/aes128/5.output.bin new file mode 100644 index 0000000..2f8db71 --- /dev/null +++ b/pmac/tests/data/aes128/5.output.bin @@ -0,0 +1 @@ +zN^3SUtu \ No newline at end of file diff --git a/pmac/tests/data/aes128/6.input.bin b/pmac/tests/data/aes128/6.input.bin new file mode 100644 index 0000000000000000000000000000000000000000..6fd520cbb33d353a80127aba5cc52cf1656c17c0 GIT binary patch literal 34 pcmZQzWMXDvWn<^yMC+6cQE@6%&_`l#-T_m6KOc1OOSc0x|#q literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes128/6.key.bin b/pmac/tests/data/aes128/6.key.bin new file mode 100644 index 0000000000000000000000000000000000000000..b66efb8adab7795606f4ebbc70be4c0a1d047a52 GIT binary patch literal 16 XcmZQzWMXDvWn<^yLnc0=NKq literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes128/6.output.bin b/pmac/tests/data/aes128/6.output.bin new file mode 100644 index 0000000..b0c7a4c --- /dev/null +++ b/pmac/tests/data/aes128/6.output.bin @@ -0,0 +1 @@ +\}^O|F=U \ No newline at end of file diff --git a/pmac/tests/data/aes128/7.input.bin b/pmac/tests/data/aes128/7.input.bin new file mode 100644 index 0000000000000000000000000000000000000000..012b3279398166a8f9e06174a33624048581648a GIT binary patch literal 1000 ScmZQz7zLvtFd72GJp=#)=l}r# literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes128/7.key.bin b/pmac/tests/data/aes128/7.key.bin new file mode 100644 index 0000000000000000000000000000000000000000..b66efb8adab7795606f4ebbc70be4c0a1d047a52 GIT binary patch literal 16 XcmZQzWMXDvWn<^yLnc0=NKq literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes128/7.output.bin b/pmac/tests/data/aes128/7.output.bin new file mode 100644 index 0000000..5206c10 --- /dev/null +++ b/pmac/tests/data/aes128/7.output.bin @@ -0,0 +1 @@ +ү \ No newline at end of file diff --git a/pmac/tests/data/aes192/1.input.bin b/pmac/tests/data/aes192/1.input.bin new file mode 100644 index 0000000..e69de29 diff --git a/pmac/tests/data/aes192/1.key.bin b/pmac/tests/data/aes192/1.key.bin new file mode 100644 index 0000000000000000000000000000000000000000..6cf04740a9306bb0d3548bd0d741c6f1f2ee9ba8 GIT binary patch literal 24 fcmZQzWMXDvWn<^yMC+6cQE@6%z*l2^0Yp literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes192/1.output.bin b/pmac/tests/data/aes192/1.output.bin new file mode 100644 index 0000000..9ad6809 --- /dev/null +++ b/pmac/tests/data/aes192/1.output.bin @@ -0,0 +1 @@ + cvޓ~6ھI \ No newline at end of file diff --git a/pmac/tests/data/aes192/2.input.bin b/pmac/tests/data/aes192/2.input.bin new file mode 100644 index 0000000000000000000000000000000000000000..8352675d67aed6625ece79af41c27fdb4ee2e867 GIT binary patch literal 3 KcmZQzWC8#H2LJ>B literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes192/2.key.bin b/pmac/tests/data/aes192/2.key.bin new file mode 100644 index 0000000000000000000000000000000000000000..6cf04740a9306bb0d3548bd0d741c6f1f2ee9ba8 GIT binary patch literal 24 fcmZQzWMXDvWn<^yMC+6cQE@6%z*l2^0Yp literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes192/2.output.bin b/pmac/tests/data/aes192/2.output.bin new file mode 100644 index 0000000..b135459 --- /dev/null +++ b/pmac/tests/data/aes192/2.output.bin @@ -0,0 +1 @@ +[C@u'B؂z} \ No newline at end of file diff --git a/pmac/tests/data/aes192/3.input.bin b/pmac/tests/data/aes192/3.input.bin new file mode 100644 index 0000000000000000000000000000000000000000..b66efb8adab7795606f4ebbc70be4c0a1d047a52 GIT binary patch literal 16 XcmZQzWMXDvWn<^yLnc0=NKq literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes192/3.key.bin b/pmac/tests/data/aes192/3.key.bin new file mode 100644 index 0000000000000000000000000000000000000000..6cf04740a9306bb0d3548bd0d741c6f1f2ee9ba8 GIT binary patch literal 24 fcmZQzWMXDvWn<^yMC+6cQE@6%z*l2^0Yp literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes192/3.output.bin b/pmac/tests/data/aes192/3.output.bin new file mode 100644 index 0000000000000000000000000000000000000000..e2b1e24fcd87b0fabb6609cbb4751648511deb92 GIT binary patch literal 16 YcmZQ?cMLb5G5g@6hbkv0K4EwW06ZcH7ytkO literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes192/4.input.bin b/pmac/tests/data/aes192/4.input.bin new file mode 100644 index 0000000000000000000000000000000000000000..dc8cabcb1f324b0841ba2624c4e06bdf1d78c325 GIT binary patch literal 20 bcmZQzWMXDvWn<^yMC+6cPpi1x5hB literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes192/4.key.bin b/pmac/tests/data/aes192/4.key.bin new file mode 100644 index 0000000000000000000000000000000000000000..6cf04740a9306bb0d3548bd0d741c6f1f2ee9ba8 GIT binary patch literal 24 fcmZQzWMXDvWn<^yMC+6cQE@6%z*l2^0Yp literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes192/4.output.bin b/pmac/tests/data/aes192/4.output.bin new file mode 100644 index 0000000..fd8ccf2 --- /dev/null +++ b/pmac/tests/data/aes192/4.output.bin @@ -0,0 +1 @@ +j|!s1Zƻ \ No newline at end of file diff --git a/pmac/tests/data/aes192/5.input.bin b/pmac/tests/data/aes192/5.input.bin new file mode 100644 index 0000000000000000000000000000000000000000..fefa1cc823e0ec463b7923972b03901b52808fc1 GIT binary patch literal 32 ncmZQzWMXDvWn<^yMC+6cQE@6%&_`l#-T_m6Hbm6>tIZ literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes192/5.key.bin b/pmac/tests/data/aes192/5.key.bin new file mode 100644 index 0000000000000000000000000000000000000000..6cf04740a9306bb0d3548bd0d741c6f1f2ee9ba8 GIT binary patch literal 24 fcmZQzWMXDvWn<^yMC+6cQE@6%z*l2^0Yp literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes192/5.output.bin b/pmac/tests/data/aes192/5.output.bin new file mode 100644 index 0000000..100835a --- /dev/null +++ b/pmac/tests/data/aes192/5.output.bin @@ -0,0 +1 @@ +eJYܗڟh1 p \ No newline at end of file diff --git a/pmac/tests/data/aes192/6.input.bin b/pmac/tests/data/aes192/6.input.bin new file mode 100644 index 0000000000000000000000000000000000000000..6fd520cbb33d353a80127aba5cc52cf1656c17c0 GIT binary patch literal 34 pcmZQzWMXDvWn<^yMC+6cQE@6%&_`l#-T_m6KOc1OOSc0x|#q literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes192/6.key.bin b/pmac/tests/data/aes192/6.key.bin new file mode 100644 index 0000000000000000000000000000000000000000..6cf04740a9306bb0d3548bd0d741c6f1f2ee9ba8 GIT binary patch literal 24 fcmZQzWMXDvWn<^yMC+6cQE@6%z*l2^0Yp literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes192/6.output.bin b/pmac/tests/data/aes192/6.output.bin new file mode 100644 index 0000000..ee1fa7b --- /dev/null +++ b/pmac/tests/data/aes192/6.output.bin @@ -0,0 +1 @@ + D8$ \ No newline at end of file diff --git a/pmac/tests/data/aes192/7.input.bin b/pmac/tests/data/aes192/7.input.bin new file mode 100644 index 0000000000000000000000000000000000000000..012b3279398166a8f9e06174a33624048581648a GIT binary patch literal 1000 ScmZQz7zLvtFd72GJp=#)=l}r# literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes192/7.key.bin b/pmac/tests/data/aes192/7.key.bin new file mode 100644 index 0000000000000000000000000000000000000000..6cf04740a9306bb0d3548bd0d741c6f1f2ee9ba8 GIT binary patch literal 24 fcmZQzWMXDvWn<^yMC+6cQE@6%z*l2^0Yp literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes192/7.output.bin b/pmac/tests/data/aes192/7.output.bin new file mode 100644 index 0000000..85cb47d --- /dev/null +++ b/pmac/tests/data/aes192/7.output.bin @@ -0,0 +1 @@ +Ӯ6))7sP \ No newline at end of file diff --git a/pmac/tests/data/aes256/1.input.bin b/pmac/tests/data/aes256/1.input.bin new file mode 100644 index 0000000..e69de29 diff --git a/pmac/tests/data/aes256/1.key.bin b/pmac/tests/data/aes256/1.key.bin new file mode 100644 index 0000000000000000000000000000000000000000..fefa1cc823e0ec463b7923972b03901b52808fc1 GIT binary patch literal 32 ncmZQzWMXDvWn<^yMC+6cQE@6%&_`l#-T_m6Hbm6>tIZ literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes256/1.output.bin b/pmac/tests/data/aes256/1.output.bin new file mode 100644 index 0000000..eb9c651 --- /dev/null +++ b/pmac/tests/data/aes256/1.output.bin @@ -0,0 +1 @@ + /[u$= \ No newline at end of file diff --git a/pmac/tests/data/aes256/2.input.bin b/pmac/tests/data/aes256/2.input.bin new file mode 100644 index 0000000000000000000000000000000000000000..8352675d67aed6625ece79af41c27fdb4ee2e867 GIT binary patch literal 3 KcmZQzWC8#H2LJ>B literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes256/2.key.bin b/pmac/tests/data/aes256/2.key.bin new file mode 100644 index 0000000000000000000000000000000000000000..fefa1cc823e0ec463b7923972b03901b52808fc1 GIT binary patch literal 32 ncmZQzWMXDvWn<^yMC+6cQE@6%&_`l#-T_m6Hbm6>tIZ literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes256/2.output.bin b/pmac/tests/data/aes256/2.output.bin new file mode 100644 index 0000000..43d2458 --- /dev/null +++ b/pmac/tests/data/aes256/2.output.bin @@ -0,0 +1 @@ +$,+T 3< \ No newline at end of file diff --git a/pmac/tests/data/aes256/3.input.bin b/pmac/tests/data/aes256/3.input.bin new file mode 100644 index 0000000000000000000000000000000000000000..b66efb8adab7795606f4ebbc70be4c0a1d047a52 GIT binary patch literal 16 XcmZQzWMXDvWn<^yLnc0=NKq literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes256/3.key.bin b/pmac/tests/data/aes256/3.key.bin new file mode 100644 index 0000000000000000000000000000000000000000..fefa1cc823e0ec463b7923972b03901b52808fc1 GIT binary patch literal 32 ncmZQzWMXDvWn<^yMC+6cQE@6%&_`l#-T_m6Hbm6>tIZ literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes256/3.output.bin b/pmac/tests/data/aes256/3.output.bin new file mode 100644 index 0000000..01e9771 --- /dev/null +++ b/pmac/tests/data/aes256/3.output.bin @@ -0,0 +1 @@ +?68 idw \ No newline at end of file diff --git a/pmac/tests/data/aes256/4.input.bin b/pmac/tests/data/aes256/4.input.bin new file mode 100644 index 0000000000000000000000000000000000000000..dc8cabcb1f324b0841ba2624c4e06bdf1d78c325 GIT binary patch literal 20 bcmZQzWMXDvWn<^yMC+6cPpi1x5hB literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes256/4.key.bin b/pmac/tests/data/aes256/4.key.bin new file mode 100644 index 0000000000000000000000000000000000000000..fefa1cc823e0ec463b7923972b03901b52808fc1 GIT binary patch literal 32 ncmZQzWMXDvWn<^yMC+6cQE@6%&_`l#-T_m6Hbm6>tIZ literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes256/4.output.bin b/pmac/tests/data/aes256/4.output.bin new file mode 100644 index 0000000..0b869ee --- /dev/null +++ b/pmac/tests/data/aes256/4.output.bin @@ -0,0 +1 @@ +w9_R \ No newline at end of file diff --git a/pmac/tests/data/aes256/5.input.bin b/pmac/tests/data/aes256/5.input.bin new file mode 100644 index 0000000000000000000000000000000000000000..fefa1cc823e0ec463b7923972b03901b52808fc1 GIT binary patch literal 32 ncmZQzWMXDvWn<^yMC+6cQE@6%&_`l#-T_m6Hbm6>tIZ literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes256/5.key.bin b/pmac/tests/data/aes256/5.key.bin new file mode 100644 index 0000000000000000000000000000000000000000..fefa1cc823e0ec463b7923972b03901b52808fc1 GIT binary patch literal 32 ncmZQzWMXDvWn<^yMC+6cQE@6%&_`l#-T_m6Hbm6>tIZ literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes256/5.output.bin b/pmac/tests/data/aes256/5.output.bin new file mode 100644 index 0000000..e88de99 --- /dev/null +++ b/pmac/tests/data/aes256/5.output.bin @@ -0,0 +1 @@ +%†xM810e>w \ No newline at end of file diff --git a/pmac/tests/data/aes256/6.input.bin b/pmac/tests/data/aes256/6.input.bin new file mode 100644 index 0000000000000000000000000000000000000000..6fd520cbb33d353a80127aba5cc52cf1656c17c0 GIT binary patch literal 34 pcmZQzWMXDvWn<^yMC+6cQE@6%&_`l#-T_m6KOc1OOSc0x|#q literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes256/6.key.bin b/pmac/tests/data/aes256/6.key.bin new file mode 100644 index 0000000000000000000000000000000000000000..fefa1cc823e0ec463b7923972b03901b52808fc1 GIT binary patch literal 32 ncmZQzWMXDvWn<^yMC+6cQE@6%&_`l#-T_m6Hbm6>tIZ literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes256/6.output.bin b/pmac/tests/data/aes256/6.output.bin new file mode 100644 index 0000000..251d81f --- /dev/null +++ b/pmac/tests/data/aes256/6.output.bin @@ -0,0 +1 @@ +ؠ_KfvONá \ No newline at end of file diff --git a/pmac/tests/data/aes256/7.input.bin b/pmac/tests/data/aes256/7.input.bin new file mode 100644 index 0000000000000000000000000000000000000000..012b3279398166a8f9e06174a33624048581648a GIT binary patch literal 1000 ScmZQz7zLvtFd72GJp=#)=l}r# literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes256/7.key.bin b/pmac/tests/data/aes256/7.key.bin new file mode 100644 index 0000000000000000000000000000000000000000..fefa1cc823e0ec463b7923972b03901b52808fc1 GIT binary patch literal 32 ncmZQzWMXDvWn<^yMC+6cQE@6%&_`l#-T_m6Hbm6>tIZ literal 0 HcmV?d00001 diff --git a/pmac/tests/data/aes256/7.output.bin b/pmac/tests/data/aes256/7.output.bin new file mode 100644 index 0000000..db47d3c --- /dev/null +++ b/pmac/tests/data/aes256/7.output.bin @@ -0,0 +1 @@ +iw1 `V)n \ No newline at end of file diff --git a/pmac/tests/lib.rs b/pmac/tests/lib.rs new file mode 100644 index 0000000..0cd187e --- /dev/null +++ b/pmac/tests/lib.rs @@ -0,0 +1,39 @@ +//! Tests from NIST SP 800-38B: +//! https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/ +#![no_std] +#[macro_use] +extern crate crypto_mac; +extern crate aesni; +extern crate pmac; + +use pmac::Pmac; +use pmac::crypto_mac::dev::{mac_test, Test}; +use aesni::{Aes128, Aes192, Aes256}; + + +#[test] +fn pmac_aes128() { + let tests = new_mac_tests!( + "aes128/1", "aes128/2", "aes128/3", "aes128/4", + "aes128/5", "aes128/6", "aes128/7" + ); + mac_test::>(&tests); +} + +#[test] +fn pmac_aes192() { + let tests = new_mac_tests!( + "aes192/1", "aes192/2", "aes192/3", "aes192/4", + "aes192/5", "aes192/6", "aes192/7" + ); + mac_test::>(&tests); +} + +#[test] +fn pmac_aes256() { + let tests = new_mac_tests!( + "aes256/1", "aes256/2", "aes256/3", "aes256/4", + "aes256/5", "aes256/6", "aes256/7" + ); + mac_test::>(&tests); +} From 73bd51ff3b5a9a28392283afbc16d29832d68c12 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Mon, 20 Nov 2017 15:29:55 +0300 Subject: [PATCH 03/10] fixed pmac tests link --- pmac/tests/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pmac/tests/lib.rs b/pmac/tests/lib.rs index 0cd187e..63dec8d 100644 --- a/pmac/tests/lib.rs +++ b/pmac/tests/lib.rs @@ -1,5 +1,4 @@ -//! Tests from NIST SP 800-38B: -//! https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/ +//! Tests taken from: http://web.cs.ucdavis.edu/~rogaway/ocb/pmac-test.htm #![no_std] #[macro_use] extern crate crypto_mac; From d0d6fd48bd8b74e075695557c81414ab05a18e3a Mon Sep 17 00:00:00 2001 From: newpavlov Date: Mon, 20 Nov 2017 15:52:50 +0300 Subject: [PATCH 04/10] added pmac benchmark --- pmac/Cargo.toml | 2 +- pmac/benches/aes128_pmac.rs | 7 +++++++ pmac/src/double.rs | 2 ++ pmac/src/lib.rs | 2 -- 4 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 pmac/benches/aes128_pmac.rs diff --git a/pmac/Cargo.toml b/pmac/Cargo.toml index fa96d6e..30c8a95 100644 --- a/pmac/Cargo.toml +++ b/pmac/Cargo.toml @@ -13,5 +13,5 @@ crypto-mac = "0.5" block-cipher-trait = "0.5" [dev-dependencies] -crypto-mac = { version = "0.5.1", features = ["dev"] } +crypto-mac = { version = "0.5.2", features = ["dev"] } aesni = "0.2" diff --git a/pmac/benches/aes128_pmac.rs b/pmac/benches/aes128_pmac.rs new file mode 100644 index 0000000..e67afe5 --- /dev/null +++ b/pmac/benches/aes128_pmac.rs @@ -0,0 +1,7 @@ +#![feature(test)] +#[macro_use] +extern crate crypto_mac; +extern crate pmac; +extern crate aesni; + +bench!(pmac::Pmac::, 16); diff --git a/pmac/src/double.rs b/pmac/src/double.rs index 7e4d2e6..31f336c 100644 --- a/pmac/src/double.rs +++ b/pmac/src/double.rs @@ -28,6 +28,7 @@ pub trait Doublable { } impl Doublable for Block { + #[inline(always)] fn double(self) -> Self { let mut val: u64 = unsafe { mem::transmute(self) }; val = val.to_be(); @@ -37,6 +38,7 @@ impl Doublable for Block { unsafe { mem::transmute(val.to_be()) } } + #[inline(always)] fn inv_double(self) -> Self { let mut val: u64 = unsafe { mem::transmute(self) }; val = val.to_be(); diff --git a/pmac/src/lib.rs b/pmac/src/lib.rs index b34143a..a3402f4 100644 --- a/pmac/src/lib.rs +++ b/pmac/src/lib.rs @@ -152,7 +152,6 @@ impl Mac for Pmac { type OutputSize = C::BlockSize; - #[inline] fn new(key: &[u8]) -> Result { let cipher = C::new(key).map_err(|_| InvalidKeyLength)?; @@ -209,7 +208,6 @@ impl Mac for Pmac } } - #[inline] fn result(mut self) -> MacResult { let mut tag = self.tag.clone(); // Special case for empty input From 034797b941c3559c703c8a89e3ad4340cf066584 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Mon, 20 Nov 2017 16:40:28 +0300 Subject: [PATCH 05/10] added pmac-aes256 becnhmark --- pmac/benches/aes256_pmac.rs | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 pmac/benches/aes256_pmac.rs diff --git a/pmac/benches/aes256_pmac.rs b/pmac/benches/aes256_pmac.rs new file mode 100644 index 0000000..46bcc04 --- /dev/null +++ b/pmac/benches/aes256_pmac.rs @@ -0,0 +1,7 @@ +#![feature(test)] +#[macro_use] +extern crate crypto_mac; +extern crate pmac; +extern crate aesni; + +bench!(pmac::Pmac::, 32); From a016ddbea17b463e5a42e0286ec88164e6712a50 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Mon, 20 Nov 2017 17:52:49 +0300 Subject: [PATCH 06/10] pmac optimizations --- pmac/src/double.rs | 2 -- pmac/src/lib.rs | 36 +++++++++++++++++------------------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/pmac/src/double.rs b/pmac/src/double.rs index 31f336c..7e4d2e6 100644 --- a/pmac/src/double.rs +++ b/pmac/src/double.rs @@ -28,7 +28,6 @@ pub trait Doublable { } impl Doublable for Block { - #[inline(always)] fn double(self) -> Self { let mut val: u64 = unsafe { mem::transmute(self) }; val = val.to_be(); @@ -38,7 +37,6 @@ impl Doublable for Block { unsafe { mem::transmute(val.to_be()) } } - #[inline(always)] fn inv_double(self) -> Self { let mut val: u64 = unsafe { mem::transmute(self) }; val = val.to_be(); diff --git a/pmac/src/lib.rs b/pmac/src/lib.rs index a3402f4..21b854e 100644 --- a/pmac/src/lib.rs +++ b/pmac/src/lib.rs @@ -94,22 +94,18 @@ impl Pmac /// Process full buffer and update tag #[inline(always)] fn process_buffer(&mut self) { - // generate values for xoring with buffer - let t = { - let mut buf = ParBlocks::::default(); - for val in buf.iter_mut() { - let l = self.get_l(self.counter); - xor(&mut self.offset, &l); - *val = self.offset.clone(); - self.counter += 1; - } - buf - }; - // Create local buffer copy and xor Ls into it + let mut offset = self.offset.clone(); + let mut counter = self.counter; let mut buf = self.buffer.clone(); - for (a, b) in buf.iter_mut().zip(t.iter()) { - xor(a, b); + for val in buf.iter_mut() { + let l = self.get_l(counter); + xor(&mut offset, &l); + counter += 1; + xor(val, &offset); } + self.counter = counter; + self.offset = offset; + // encrypt blocks in the buffer self.cipher.encrypt_blocks(&mut buf); // and xor them into tag @@ -208,7 +204,7 @@ impl Mac for Pmac } } - fn result(mut self) -> MacResult { + fn result(self) -> MacResult { let mut tag = self.tag.clone(); // Special case for empty input if self.pos == 0 { @@ -224,16 +220,18 @@ impl Mac for Pmac let n = if is_full { (self.pos/bs) - 1 } else { self.pos/bs }; assert!(n < C::ParBlocks::to_usize(), "invalid buffer positions"); + let mut offset = self.offset.clone(); + let mut counter = self.counter; for i in 0..n { let mut buf = self.buffer[i].clone(); - let l = self.get_l(self.counter); - xor(&mut self.offset, &l); - xor(&mut buf, &self.offset); + let l = self.get_l(counter); + xor(&mut offset, &l); + xor(&mut buf, &offset); self.cipher.encrypt_block(&mut buf); xor(&mut tag, &buf); - self.counter += 1; + counter += 1; } if is_full { From 74605dd0918218b0d402617c352807b619355914 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Mon, 20 Nov 2017 18:04:57 +0300 Subject: [PATCH 07/10] changed L cache table size --- pmac/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pmac/src/lib.rs b/pmac/src/lib.rs index 21b854e..f564f60 100644 --- a/pmac/src/lib.rs +++ b/pmac/src/lib.rs @@ -65,8 +65,9 @@ pub use double::Doublable; type Block = GenericArray; type ParBlocks = GenericArray, M>; -// in future it can be parameter of Pmac -const LC_SIZE: usize = 16; +/// Will use only cached table up to 16*2^20 = 16 MB of input data. +/// In future it can be parameter of Pmac +const LC_SIZE: usize = 20; /// Generic CMAC instance #[derive(Clone)] From 33cdfc1a9d16e278d01cd6c967b83cca6fc979b9 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Mon, 20 Nov 2017 18:22:09 +0300 Subject: [PATCH 08/10] more precise doc --- pmac/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pmac/src/lib.rs b/pmac/src/lib.rs index f564f60..1471230 100644 --- a/pmac/src/lib.rs +++ b/pmac/src/lib.rs @@ -66,7 +66,7 @@ type Block = GenericArray; type ParBlocks = GenericArray, M>; /// Will use only cached table up to 16*2^20 = 16 MB of input data. -/// In future it can be parameter of Pmac +/// (for 128 bit cipher) In future it can be parameter of Pmac const LC_SIZE: usize = 20; /// Generic CMAC instance From 900126b057a3ae27dd2371234b293502b850e143 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Mon, 20 Nov 2017 19:27:41 +0300 Subject: [PATCH 09/10] use dbl crate --- Cargo.toml | 1 + pmac/Cargo.toml | 1 + pmac/src/double.rs | 140 --------------------------------------------- pmac/src/lib.rs | 23 ++++---- 4 files changed, 13 insertions(+), 152 deletions(-) delete mode 100644 pmac/src/double.rs diff --git a/Cargo.toml b/Cargo.toml index 040c8ca..37beaab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ members = [ [patch.crates-io] block-cipher-trait = { git = "https://github.com/RustCrypto/traits" } aesni = { git = "https://github.com/RustCrypto/block-ciphers" } +dbl = { git = "https://github.com/RustCrypto/utils" } diff --git a/pmac/Cargo.toml b/pmac/Cargo.toml index 30c8a95..c72aaa2 100644 --- a/pmac/Cargo.toml +++ b/pmac/Cargo.toml @@ -11,6 +11,7 @@ keywords = ["crypto", "mac", "pmac"] [dependencies] crypto-mac = "0.5" block-cipher-trait = "0.5" +dbl = "0.1" [dev-dependencies] crypto-mac = { version = "0.5.2", features = ["dev"] } diff --git a/pmac/src/double.rs b/pmac/src/double.rs deleted file mode 100644 index 7e4d2e6..0000000 --- a/pmac/src/double.rs +++ /dev/null @@ -1,140 +0,0 @@ -use block_cipher_trait::generic_array::typenum::{U8, U16, U32}; -use super::Block; - -use core::mem; - -const C64: u64 = 0b1_1011; -const C128: u64 = 0b1000_0111; -const C256: u64 = 0b100_0010_0101; - -/// Some block operations over Galois Field. -/// -/// This trait is implemented for 64, 128 and 256 bit block sizes. -/// IMPORTANT: block must be aligned -pub trait Doublable { - /// Multiply block by x - /// - /// If most significant bit of the block equals to zero will return - /// `block<<1`, otherwise `(block<<1)^C`, where `C` is the non-leading - /// coefficients of the lexicographically first irreducible degree-b binary - /// polynomial with the minimal number of ones. - fn double(self) -> Self; - - /// Divide block by x - /// - /// If least significant bit of the block equals to zero will return - /// `block>>1`, otherwise `(block>>1)^(1<>1)` - fn inv_double(self) -> Self; -} - -impl Doublable for Block { - fn double(self) -> Self { - let mut val: u64 = unsafe { mem::transmute(self) }; - val = val.to_be(); - let a = val >> 63; - val <<= 1; - val ^= a * C64; - unsafe { mem::transmute(val.to_be()) } - } - - fn inv_double(self) -> Self { - let mut val: u64 = unsafe { mem::transmute(self) }; - val = val.to_be(); - - let a = val & 1; - val >>= 1; - val ^= a*((1 << 63) ^ (C64 >> 1)); - - unsafe { mem::transmute(val.to_be()) } - } -} - -#[inline(always)] -fn to_be(val: &mut [u64]) { - for v in val.iter_mut() { - *v = v.to_be(); - } -} - -impl Doublable for Block { - fn double(self) -> Self { - let mut val: [u64; 2] = unsafe { mem::transmute(self) }; - to_be(&mut val); - - let b = val[1] >> 63; - let a = val[0] >> 63; - - val[0] <<= 1; - val[0] ^= b; - val[1] <<= 1; - val[1] ^= a*C128; - - to_be(&mut val); - unsafe { mem::transmute(val) } - } - - fn inv_double(self) -> Self { - let mut val: [u64; 2] = unsafe { mem::transmute(self) }; - to_be(&mut val); - - let a = (val[0] & 1) << 63; - let b = val[1] & 1; - - val[0] >>= 1; - val[1] >>= 1; - val[1] ^= a; - val[0] ^= b*(1 << 63); - val[1] ^= b*(C128 >> 1); - - to_be(&mut val); - unsafe { mem::transmute(val) } - } -} - -impl Doublable for Block { - fn double(self) -> Self { - let mut val: [u64; 4] = unsafe { mem::transmute(self) }; - to_be(&mut val); - - let a = val[0] >> 63; - let b = val[1] >> 63; - let c = val[2] >> 63; - let d = val[3] >> 63; - - val[0] <<= 1; - val[0] ^= b; - val[1] <<= 1; - val[1] ^= c; - val[2] <<= 1; - val[2] ^= d; - val[3] <<= 1; - val[3] ^= a*C256; - - to_be(&mut val); - unsafe { mem::transmute(val) } - } - - fn inv_double(self) -> Self { - let mut val: [u64; 4] = unsafe { mem::transmute(self) }; - to_be(&mut val); - - let a = (val[0] & 1) << 63; - let b = (val[1] & 1) << 63; - let c = (val[2] & 1) << 63; - let d = val[3] & 1; - - val[0] >>= 1; - val[1] >>= 1; - val[2] >>= 1; - val[3] >>= 1; - val[1] ^= a; - val[2] ^= b; - val[3] ^= c; - - val[0] ^= d*(1 << 63); - val[3] ^= d*(C256 >> 1); - - to_be(&mut val); - unsafe { mem::transmute(val) } - } -} diff --git a/pmac/src/lib.rs b/pmac/src/lib.rs index 1471230..9c575e6 100644 --- a/pmac/src/lib.rs +++ b/pmac/src/lib.rs @@ -48,6 +48,7 @@ //! ``` #![no_std] extern crate block_cipher_trait; +extern crate dbl; pub extern crate crypto_mac; pub use crypto_mac::Mac; @@ -55,21 +56,19 @@ use crypto_mac::{InvalidKeyLength, MacResult}; use block_cipher_trait::{BlockCipher, NewVarKey}; use block_cipher_trait::generic_array::{GenericArray, ArrayLength}; use block_cipher_trait::generic_array::typenum::Unsigned; +use dbl::Dbl; use core::slice; -mod double; - -pub use double::Doublable; - type Block = GenericArray; type ParBlocks = GenericArray, M>; -/// Will use only cached table up to 16*2^20 = 16 MB of input data. -/// (for 128 bit cipher) In future it can be parameter of Pmac +/// Will use only precomputed table up to 16*2^20 = 16 MB of input data +/// (for 128 bit cipher), after that will dynamically calculate L value if +/// needed. In future it can become parameter of `Pmac`. const LC_SIZE: usize = 20; -/// Generic CMAC instance +/// Generic PMAC instance #[derive(Clone)] pub struct Pmac { cipher: C, @@ -90,7 +89,7 @@ fn xor>(buf: &mut Block, data: &Block) { } impl Pmac - where C: BlockCipher + NewVarKey, Block: Doublable + where C: BlockCipher + NewVarKey, Block: Dbl { /// Process full buffer and update tag #[inline(always)] @@ -137,7 +136,7 @@ impl Pmac } else { let mut block = self.l_cache[LC_SIZE-1].clone(); for _ in LC_SIZE-1..ntz { - block = block.double(); + block = block.dbl(); } block } @@ -145,7 +144,7 @@ impl Pmac } impl Mac for Pmac - where C: BlockCipher + NewVarKey, Block: Doublable + where C: BlockCipher + NewVarKey, Block: Dbl { type OutputSize = C::BlockSize; @@ -158,10 +157,10 @@ impl Mac for Pmac let mut l_cache: [Block; LC_SIZE] = Default::default(); l_cache[0] = l0.clone(); for i in 1..LC_SIZE { - l_cache[i] = l_cache[i-1].clone().double(); + l_cache[i] = l_cache[i-1].clone().dbl(); } - let l_inv = l0.clone().inv_double(); + let l_inv = l0.clone().inv_dbl(); Ok(Self { cipher, l_inv, l_cache, From 18fdae1fa3259aebee1dbd4dc204baa3787caab8 Mon Sep 17 00:00:00 2001 From: newpavlov Date: Mon, 20 Nov 2017 19:31:27 +0300 Subject: [PATCH 10/10] use dbl from crates.io --- Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 37beaab..040c8ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,3 @@ members = [ [patch.crates-io] block-cipher-trait = { git = "https://github.com/RustCrypto/traits" } aesni = { git = "https://github.com/RustCrypto/block-ciphers" } -dbl = { git = "https://github.com/RustCrypto/utils" }