diff --git a/hight/Cargo.toml b/hight/Cargo.toml new file mode 100644 index 00000000..b5d27243 --- /dev/null +++ b/hight/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "hight" +version = "0.1.0" +edition = "2021" +description = "HIGHT - Block Cipher Suitable for Low-Resource Device" +authors = ["Arul Sujith S"] +license = "MIT OR Apache-2.0" +rust-version = "1.80" +readme = "README.md" +repository = "https://github.com/RustCrypto/block-ciphers" +keywords = ["crypto", "hight", "block-cipher"] +categories = ["cryptography", "no-std"] + +[dependencies] +hex-literal = "0.3" + diff --git a/hight/LICENSE-APACHE b/hight/LICENSE-APACHE new file mode 100644 index 00000000..573db22f --- /dev/null +++ b/hight/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 2017 Gulshan Singh + +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/hight/LICENSE-MIT b/hight/LICENSE-MIT new file mode 100644 index 00000000..bbfaff8d --- /dev/null +++ b/hight/LICENSE-MIT @@ -0,0 +1,26 @@ +Copyright (c) 2017-2024 The RustCrypto Project Developers +Copyright (c) 2017 Gulshan Singh, Antoni Boucher, 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/hight/README.md b/hight/README.md new file mode 100644 index 00000000..65a164d6 --- /dev/null +++ b/hight/README.md @@ -0,0 +1,54 @@ +# RustCrypto: HIGHT - (High Security and Light Weight) + +![Apache2/MIT licensed][license-image] +![Rust Version][rustc-image] +[![HAZMAT][hazmat-image]][hazmat-link] + +Pure Rust implementation of the [HIGHT Cipher][1] + + +## ⚠️ Security Warning: [Hazmat!][hazmat-link] + +This crate does not ensure ciphertexts are authentic (i.e. by using a MAC to +verify ciphertext integrity), which can lead to serious vulnerabilities +if used incorrectly! + +No security audits of this crate have ever been performed, and it has not been +thoroughly assessed to ensure its operation is constant-time on common CPU +architectures. + +USE AT YOUR OWN RISK! + +## Minimum Supported Rust Version + +Rust **1.80** or higher. + +Minimum supported Rust version can be changed in the future, but it will be +done with a minor version bump. + +## SemVer Policy + +- All on-by-default features of this library are covered by SemVer +- MSRV is considered exempt from SemVer as noted above + +## License + +Licensed under either of: + + * [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0) + * [MIT license](http://opensource.org/licenses/MIT) + +at your option. + +### Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in the work by you, as defined in the Apache-2.0 license, shall be +dual licensed as above, without any additional terms or conditions. + +[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg +[rustc-image]: https://img.shields.io/badge/rustc-1.80+-blue.svg +[hazmat-image]: https://img.shields.io/badge/crypto-hazmat%E2%9A%A0-red.svg +[hazmat-link]: https://github.com/RustCrypto/meta/blob/master/HAZMAT.md + +[1]: https://www.iacr.org/archive/ches2006/04/04.pdf diff --git a/hight/docs/01.+HIGHT+Algorithm+Specification.pdf b/hight/docs/01.+HIGHT+Algorithm+Specification.pdf new file mode 100644 index 00000000..f537bd6c Binary files /dev/null and b/hight/docs/01.+HIGHT+Algorithm+Specification.pdf differ diff --git a/hight/docs/03.+HIGHT+Algorithm-Test+vector.pdf b/hight/docs/03.+HIGHT+Algorithm-Test+vector.pdf new file mode 100644 index 00000000..6d13f4d9 Binary files /dev/null and b/hight/docs/03.+HIGHT+Algorithm-Test+vector.pdf differ diff --git a/hight/docs/04.pdf b/hight/docs/04.pdf new file mode 100644 index 00000000..43e85131 Binary files /dev/null and b/hight/docs/04.pdf differ diff --git a/hight/docs/HIGHT_A_new_block_cipher_suitable_for_low-resource.pdf b/hight/docs/HIGHT_A_new_block_cipher_suitable_for_low-resource.pdf new file mode 100644 index 00000000..3f79ef31 Binary files /dev/null and b/hight/docs/HIGHT_A_new_block_cipher_suitable_for_low-resource.pdf differ diff --git a/hight/src/consts.rs b/hight/src/consts.rs new file mode 100644 index 00000000..c55ad892 --- /dev/null +++ b/hight/src/consts.rs @@ -0,0 +1,18 @@ +/// HIGHT round constants, initial state = 1011010 and primitive polynomial = x^7 + x^3 + 1 +pub(crate) const DELTA: [u8; 128] = [ + 0x5a, 0x6d, 0x36, 0x1b, 0x0d, 0x06, 0x03, 0x41, + 0x60, 0x30, 0x18, 0x4c, 0x66, 0x33, 0x59, 0x2c, + 0x56, 0x2b, 0x15, 0x4a, 0x65, 0x72, 0x39, 0x1c, + 0x4e, 0x67, 0x73, 0x79, 0x3c, 0x5e, 0x6f, 0x37, + 0x5b, 0x2d, 0x16, 0x0b, 0x05, 0x42, 0x21, 0x50, + 0x28, 0x54, 0x2a, 0x55, 0x6a, 0x75, 0x7a, 0x7d, + 0x3e, 0x5f, 0x2f, 0x17, 0x4b, 0x25, 0x52, 0x29, + 0x14, 0x0a, 0x45, 0x62, 0x31, 0x58, 0x6c, 0x76, + 0x3b, 0x1d, 0x0e, 0x47, 0x63, 0x71, 0x78, 0x7c, + 0x7e, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x43, 0x61, + 0x70, 0x38, 0x5c, 0x6e, 0x77, 0x7b, 0x3d, 0x1e, + 0x4f, 0x27, 0x53, 0x69, 0x34, 0x1a, 0x4d, 0x26, + 0x13, 0x49, 0x24, 0x12, 0x09, 0x04, 0x02, 0x01, + 0x40, 0x20, 0x10, 0x08, 0x44, 0x22, 0x11, 0x48, + 0x64, 0x32, 0x19, 0x0c, 0x46, 0x23, 0x51, 0x68, + 0x74, 0x3a, 0x5d, 0x2e, 0x57, 0x6b, 0x35, 0x5a ]; diff --git a/hight/src/dec.rs b/hight/src/dec.rs new file mode 100644 index 00000000..27311d60 --- /dev/null +++ b/hight/src/dec.rs @@ -0,0 +1,50 @@ +use crate::primitives::{f0, f1}; +use crate::key_schedule::keys; + +/// Decryption Initial Transformation +fn dec_initial_transformation(mut cipher: Vec, wk: &[u8]) -> Vec { + cipher[0] = cipher[0].wrapping_sub(wk[4]); + cipher[2] ^= wk[5]; + cipher[4] = cipher[4].wrapping_sub(wk[6]); + cipher[6] ^= wk[7]; + cipher +} + +/// Decryption Final Transformation +fn dec_final_transformation(mut cipher: Vec, wk: &[u8]) -> Vec { + cipher[0] = cipher[0].wrapping_sub(wk[0]); + cipher[2] ^= wk[1]; + cipher[4] = cipher[4].wrapping_sub(wk[2]); + cipher[6] ^= wk[3]; + cipher +} + +/// Decryption +fn decryption(mut cipher: Vec, sk: &[u8]) -> Vec { + for i in 0..32 { + if i == 0 { + cipher[1] = cipher[1].wrapping_sub(f1(cipher[0]) ^ sk[4 * i + 3]); + cipher[3] = cipher[3] ^ (f0(cipher[2]).wrapping_add(sk[4 * i + 2])); + cipher[5] = cipher[5].wrapping_sub(f1(cipher[4]) ^ sk[4 * i + 1]); + cipher[7] = cipher[7] ^ (f0(cipher[6]).wrapping_add(sk[4 * i])); + } else { + cipher = vec![ + cipher[1], cipher[2].wrapping_sub(f1(cipher[1]) ^ sk[4 * i + 3]), + cipher[3], cipher[4] ^ (f0(cipher[3]).wrapping_add(sk[4 * i + 2])), + cipher[5], cipher[6].wrapping_sub(f1(cipher[5]) ^ sk[4 * i + 1]), + cipher[7], cipher[0] ^ (f0(cipher[7]).wrapping_add(sk[4 * i])), + ]; + } + } + cipher +} + +pub fn decrypt(ciphertext: &[u8], key: &[u8]) -> Vec { + let (wk, mut sk) = keys(key); + sk.reverse(); + let transformed = dec_initial_transformation(ciphertext.to_vec(), &wk); + let mut plaintext = decryption(transformed, &sk); + plaintext = dec_final_transformation(plaintext, &wk); + plaintext.reverse(); + plaintext +} diff --git a/hight/src/enc.rs b/hight/src/enc.rs new file mode 100644 index 00000000..843eecb6 --- /dev/null +++ b/hight/src/enc.rs @@ -0,0 +1,50 @@ +use crate::primitives::{f0, f1}; +use crate::key_schedule::keys; + +/// Encryption Initial Transformation +fn enc_initial_transformation(pt: &[u8], wk: &[u8]) -> Vec { + vec![ + pt[7].wrapping_add(wk[0]), + pt[6], + pt[5] ^ wk[1], + pt[4], + pt[3].wrapping_add(wk[2]), + pt[2], + pt[1] ^ wk[3], + pt[0] + ] +} + +/// Encryption Final Transformation +fn enc_final_transformation(mut cipher: Vec, wk: &[u8]) -> Vec { + cipher[0] = cipher[0].wrapping_add(wk[4]); + cipher[2] ^= wk[5]; + cipher[4] = cipher[4].wrapping_add(wk[6]); + cipher[6] ^= wk[7]; + cipher +} + +/// Encryption +fn encryption(mut cipher: Vec, sk: &[u8]) -> Vec { + for i in 0..32 { + let t0 = cipher[1].wrapping_add(f1(cipher[0]) ^ sk[4 * i]); + let t1 = cipher[3] ^ (f0(cipher[2]).wrapping_add(sk[4 * i + 1])); + let t2 = cipher[5].wrapping_add(f1(cipher[4]) ^ sk[4 * i + 2]); + let t3 = cipher[7] ^ (f0(cipher[6]).wrapping_add(sk[4 * i + 3])); + + if i == 31 { + cipher = vec![cipher[0], t0, cipher[2], t1, cipher[4], t2, cipher[6], t3]; + } else { + cipher = vec![t3, cipher[0], t0, cipher[2], t1, cipher[4], t2, cipher[6]]; + } + } + cipher +} + +pub fn encrypt(plaintext: &[u8], key: &[u8]) -> Vec { + let (wk, sk) = keys(key); + let transformed = enc_initial_transformation(plaintext, &wk); + let cipher = encryption(transformed, &sk); + let final_cipher = enc_final_transformation(cipher, &wk); + final_cipher +} diff --git a/hight/src/key_schedule.rs b/hight/src/key_schedule.rs new file mode 100644 index 00000000..1a61be64 --- /dev/null +++ b/hight/src/key_schedule.rs @@ -0,0 +1,21 @@ +use crate::consts; + +pub fn keys(mk: &[u8]) -> (Vec, Vec) { + + let delta = &consts::DELTA; + let mut sk = vec![0u8; 128]; + + for i in 0usize..8 { + for j in 0usize..16 { + let mk_index = (j.wrapping_sub(i)) % 8 + (j / 8) * 8; + + let a = mk[15 - mk_index]; + let b = delta[16 * i + j]; + + sk[16 * i + j] = a.wrapping_add(b); + } + } + + let wk: Vec = (0..8).map(|i| mk[if i <= 3 { 3 - i } else { 19 - i }]).collect(); + (wk, sk) +} diff --git a/hight/src/lib.rs b/hight/src/lib.rs new file mode 100644 index 00000000..01b70800 --- /dev/null +++ b/hight/src/lib.rs @@ -0,0 +1,5 @@ +pub mod consts; +pub mod enc; +pub mod dec; +pub mod key_schedule; +pub mod primitives; diff --git a/hight/src/primitives.rs b/hight/src/primitives.rs new file mode 100644 index 00000000..c53990e9 --- /dev/null +++ b/hight/src/primitives.rs @@ -0,0 +1,9 @@ +#[inline] +pub fn f0(x: u8) -> u8 { + x.rotate_left(1) ^ x.rotate_left(2) ^ x.rotate_left(7) +} + +#[inline] +pub fn f1(x: u8) -> u8 { + x.rotate_left(3) ^ x.rotate_left(4) ^ x.rotate_left(6) +} diff --git a/hight/tests/mod.rs b/hight/tests/mod.rs new file mode 100644 index 00000000..e731c115 --- /dev/null +++ b/hight/tests/mod.rs @@ -0,0 +1,36 @@ +use new::{enc, dec}; +use hex_literal::hex; + +const KEYS: [[u8; 16]; 4] = [ + hex!("00112233445566778899aabbccddeeff"), + hex!("ffeeddccbbaa99887766554433221100"), + hex!("000102030405060708090a0b0c0d0e0f"), + hex!("28dbc3bc49ffd87dcfa509b11d422be7"), +]; + +const PTEXTS: [[u8; 8]; 4] = [ + hex!("0000000000000000"), + hex!("0011223344556677"), + hex!("0123456789abcdef"), + hex!("b41e6be2eba84a14"), +]; + +const CTEXTS: [[u8; 8]; 4] = [ + hex!("f2034fd9ae18f400"), + hex!("d8e643e5729fce23"), + hex!("66f4238da2b26f7a"), + hex!("c61f9c20757a04cc"), +]; + +#[test] +fn test_vectors() { + for i in 0..KEYS.len() { + let ciphertext = enc::encrypt(&PTEXTS[i], &KEYS[i]); + + assert_eq!(ciphertext, CTEXTS[i], "Encryption failed at test vector {}", i); + + let decrypted = dec::decrypt(&ciphertext, &KEYS[i]); + + assert_eq!(decrypted, PTEXTS[i], "Decryption failed at test vector {}", i); + } +}