diff --git a/.github/workflows/bake-kdf.yml b/.github/workflows/bake-kdf.yml new file mode 100644 index 0000000..099f577 --- /dev/null +++ b/.github/workflows/bake-kdf.yml @@ -0,0 +1,53 @@ +name: bake-kdf + +on: + pull_request: + paths: + - "bake-kdf/**" + - "Cargo.*" + push: + branches: master + +defaults: + run: + working-directory: bake-kdf + +env: + CARGO_INCREMENTAL: 0 + RUSTFLAGS: "-Dwarnings" + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + rust: + - 1.81.0 # MSRV + - stable + target: + - thumbv7em-none-eabi + - wasm32-unknown-unknown + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ matrix.rust }} + targets: ${{ matrix.target }} + - run: cargo build --no-default-features --target ${{ matrix.target }} + + test: + runs-on: ubuntu-latest + strategy: + matrix: + rust: + - 1.81.0 # MSRV + - stable + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ matrix.rust }} + - run: cargo check --all-features + - run: cargo test --no-default-features + - run: cargo test + - run: cargo test --all-features diff --git a/Cargo.lock b/Cargo.lock index 958128c..96fb10b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,30 @@ dependencies = [ "sha2", ] +[[package]] +name = "bake-kdf" +version = "0.1.0" +dependencies = [ + "belt-hash", + "hex-literal", +] + +[[package]] +name = "belt-block" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9aa1eef3994e2ccd304a78fe3fea4a73e5792007f85f09b79bb82143ca5f82b" + +[[package]] +name = "belt-hash" +version = "0.2.0-pre.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee5982dbf7d2f719b4237cd796ee600e9dcbef1eef460ece65380f9192a54ab5" +dependencies = [ + "belt-block", + "digest", +] + [[package]] name = "blobby" version = "0.3.1" diff --git a/Cargo.toml b/Cargo.toml index 00e0ff2..90507cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,10 @@ [workspace] resolver = "2" members = [ + "bake-kdf", "hkdf", - "concat-kdf", "ansi-x963-kdf", + "concat-kdf", + "ansi-x963-kdf", ] [profile.dev] diff --git a/README.md b/README.md index bc1888e..93bee84 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,9 @@ Collection of [Key Derivation Functions][KDF] (KDF) written in pure Rust. ## Supported Algorithms -| Algorithm | Crate | Crates.io | Documentation | MSRV | -|--------------|----------------|:---------------------------------------------------------------------------------------------------:|:------------------------------------------------------------------------------------:|:-----------------------:| +| Algorithm | Crate | Crates.io | Documentation | MSRV | +|--------------|-------|:---------:|:-------------:|:----:| +| [bake-kdf] | [`bake-kdf`] | [![crates.io](https://img.shields.io/crates/v/bake-kdf.svg)](https://crates.io/crates/bake-kdf) | [![Documentation](https://docs.rs/bake-kdf/badge.svg)](https://docs.rs/bake-kdf) | ![MSRV 1.81][msrv-1.81] | | [HKDF] | [`hkdf`] | [![crates.io](https://img.shields.io/crates/v/hkdf.svg)](https://crates.io/crates/hkdf) | [![Documentation](https://docs.rs/hkdf/badge.svg)](https://docs.rs/hkdf) | ![MSRV 1.81][msrv-1.81] | | [Concat-KDF] | [`concat-kdf`] | [![crates.io](https://img.shields.io/crates/v/concat-kdf.svg)](https://crates.io/crates/concat-kdf) | [![Documentation](https://docs.rs/concat-kdf/badge.svg)](https://docs.rs/concat-kdf) | ![MSRV 1.81][msrv-1.81] | | [ANSI-X9.63-KDF] | [`ansi-x963-kdf`] | [![crates.io](https://img.shields.io/crates/v/ansi-x963-kdf.svg)](https://crates.io/crates/ansi-x963-kdf) | [![Documentation](https://docs.rs/ansi-x963-kdf/badge.svg)](https://docs.rs/ansi-x963-kdf) | ![MSRV 1.81][msrv-1.81] | diff --git a/bake-kdf/CHANGELOG.md b/bake-kdf/CHANGELOG.md new file mode 100644 index 0000000..b7ad69b --- /dev/null +++ b/bake-kdf/CHANGELOG.md @@ -0,0 +1,8 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## 0.1.0 (xxxx-xx-xx) +- Initial release diff --git a/bake-kdf/Cargo.toml b/bake-kdf/Cargo.toml new file mode 100644 index 0000000..d20f65f --- /dev/null +++ b/bake-kdf/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "bake-kdf" +version = "0.1.0" +description = "bake-kdf function (STB 34.101.66-2014)" +authors = ["RustCrypto Developers"] +license = "MIT OR Apache-2.0" +readme = "README.md" +edition = "2021" +rust-version = "1.72" +documentation = "https://docs.rs/bake-kdf" +repository = "https://github.com/RustCrypto/KDFs" +keywords = ["crypto", "bake", "stb", "kdf"] +categories = ["cryptography", "no-std"] + +[dependencies] +belt-hash = { version = "0.2.0-pre.4", default-features = false } + +[dev-dependencies] +hex-literal = "0.4.1" + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = ["--cfg", "docsrs"] diff --git a/bake-kdf/LICENSE-APACHE b/bake-kdf/LICENSE-APACHE new file mode 100644 index 0000000..53b7ccd --- /dev/null +++ b/bake-kdf/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. \ No newline at end of file diff --git a/bake-kdf/LICENSE-MIT b/bake-kdf/LICENSE-MIT new file mode 100644 index 0000000..e7068ac --- /dev/null +++ b/bake-kdf/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright (c) 2024 RustCrypto Developers + +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/bake-kdf/README.md b/bake-kdf/README.md new file mode 100644 index 0000000..e2d813e --- /dev/null +++ b/bake-kdf/README.md @@ -0,0 +1,39 @@ +# RustCrypto: bake-kdf + +[![crate][crate-image]][crate-link] +[![Docs][docs-image]][docs-link] +![Apache2/MIT licensed][license-image] +![Rust Version][rustc-image] +[![Project Chat][chat-image]][chat-link] +[![Build Status][build-image]][build-link] + +Pure Rust implementation of the [bake-kdf][1] function. + +[1]: https://apmi.bsu.by/assets/files/std/bake-spec19.pdf + +# Examples + +```rust +use bake_kdf::bake_kdf; +use hex_literal::hex; +let x = [0x42; 32]; +let s = [0x24; 8]; +let c = 0x00; +let key = bake_kdf(&x, &s, c); + +assert_eq!(key, hex!("bbd7ece0080bee33c776a140f8d807a113a119a4e4d4270f9f2018fbd5e6292e")); +``` + + +[//]: # (badges) + +[crate-image]: https://img.shields.io/crates/v/bake-kdf.svg +[crate-link]: https://crates.io/crates/bake-kdf +[docs-image]: https://docs.rs/bake-kdf/badge.svg +[docs-link]: https://docs.rs/bake-kdf/ +[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg +[rustc-image]: https://img.shields.io/badge/rustc-1.41+-blue.svg +[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg +[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260043-KDFs +[build-image]: https://github.com/RustCrypto/KDFs/workflows/bake-kdf/badge.svg?branch=master&event=push +[build-link]: https://github.com/RustCrypto/KDFs/actions?query=workflow:bake-kdf diff --git a/bake-kdf/benches/benchmark.rs b/bake-kdf/benches/benchmark.rs new file mode 100644 index 0000000..3cf2dec --- /dev/null +++ b/bake-kdf/benches/benchmark.rs @@ -0,0 +1,16 @@ +#![feature(test)] +extern crate test; + +use bake_kdf::bake_kdf; +use test::Bencher; + +#[bench] +fn bake_kdf_benchmark(b: &mut Bencher) { + let secret = [0u8; 32]; + let iv = [0u8; 64]; + let c = 0u128; + b.iter(|| { + let (secret, iv, c) = test::black_box((&secret, &iv, c)); + test::black_box(bake_kdf(secret, iv, c)); + }); +} diff --git a/bake-kdf/src/lib.rs b/bake-kdf/src/lib.rs new file mode 100644 index 0000000..279b0ab --- /dev/null +++ b/bake-kdf/src/lib.rs @@ -0,0 +1,100 @@ +#![no_std] +#![doc( + html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg", + html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg", + html_root_url = "https://docs.rs/bake-kdf/0.0.0" +)] +#![doc = include_str!("../README.md")] +#![cfg_attr(docsrs, feature(doc_auto_cfg))] +#![forbid(unsafe_code)] +#![warn(missing_docs, rust_2018_idioms)] + +use belt_hash::digest::FixedOutput; +use belt_hash::{belt_compress, BeltHash, Digest}; + +/// `belt-keyexpand` key expansion algorithm described in STB 34.101.34-2020 8.1.2. +/// +/// # Panics +/// If `N` is not equal to 16, 24, or 32. +// TODO: use compile-time checks for `N` +#[inline] +pub fn belt_keyexpand(k: &[u8; N]) -> [u32; 8] { + let mut t = [0u32; 8]; + // TODO: move this conversion into `belt_keyrep` when we will be able + // to use generic paramaters as `[u32; N / 4]`. + for (src, dst) in k.chunks_exact(4).zip(t.iter_mut()) { + *dst = u32::from_le_bytes(src.try_into().unwrap()); + } + match N { + 16 => { + t[4] = t[0]; + t[5] = t[1]; + t[6] = t[2]; + t[7] = t[3]; + } + 24 => { + t[6] = t[0] ^ t[1] ^ t[2]; + t[7] = t[3] ^ t[4] ^ t[5]; + } + 32 => {} + _ => panic!("Invalid key size n={N}. Expected 16, 24, or 32."), + } + t +} + +/// `belt-keyrep` key repetition algorithm described in STB 34.101.34-2020 8.1.3. +/// +/// # Panics +/// If `(N, M)` is not equal to `(16, 16)`, `(24, 16)`, `(24, 24)`, +/// `(32, 16)`, `(32, 24)`, or `(32, 32)`. +// TODO: use compile-time check for `N` and `M` +#[inline] +pub fn belt_keyrep( + x: &[u8; N], + d: &[u8; 12], + i: &[u8; 16], +) -> [u8; M] { + let r: u32 = match (N, M) { + (16, 16) => 0xC8BA94B1, + (24, 16) => 0x12D6E35B, + (24, 24) => 0xFFC0B05C, + (32, 16) => 0x1ADC2BE1, + (32, 24) => 0x3876ABC1, + (32, 32) => 0x7B653CF3, + _ => panic!("belt-keyrep: invalid combination of N ({N}) and M ({M})"), + }; + + let s = belt_keyexpand(x); + + let d = [ + u32::from_le_bytes(d[..4].try_into().unwrap()), + u32::from_le_bytes(d[4..][..4].try_into().unwrap()), + u32::from_le_bytes(d[8..][..4].try_into().unwrap()), + ]; + + let i = [ + u32::from_le_bytes(i[0..][..4].try_into().unwrap()), + u32::from_le_bytes(i[4..][..4].try_into().unwrap()), + u32::from_le_bytes(i[8..][..4].try_into().unwrap()), + u32::from_le_bytes(i[12..][..4].try_into().unwrap()), + ]; + + let (_, s) = belt_compress([r, d[0], d[1], d[2]], i, s); + + let mut y = [0u8; M]; + for (src, dst) in s.iter().zip(y.chunks_exact_mut(4)) { + dst.copy_from_slice(&src.to_le_bytes()); + } + y +} + +/// `bake-kdf` key derivation algorithm described in STB 34.101.66-2014 8.1.4. +#[inline] +pub fn bake_kdf(x: &[u8], s: &[u8], c: u128) -> [u8; 32] { + let mut hasher = BeltHash::default(); + hasher.update(x); + hasher.update(s); + let y = hasher.finalize_fixed().0; + + belt_keyrep(&y, &[0xFF; 12], &c.to_le_bytes()) +} diff --git a/bake-kdf/tests/tests.rs b/bake-kdf/tests/tests.rs new file mode 100644 index 0000000..e2bbcee --- /dev/null +++ b/bake-kdf/tests/tests.rs @@ -0,0 +1,74 @@ +use bake_kdf::{bake_kdf, belt_keyexpand, belt_keyrep}; +use hex_literal::hex; + +#[test] +fn test_keyexpand() { + let k_u32: [u32; 6] = [ + 0xE9DEE72C, 0x8F0C0FA6, 0x2DDB49F4, 0x6F739647, 0x06075316, 0xED247A37, + ]; + let mut k = [0u8; 24]; + for (src, dst) in k_u32.iter().zip(k.chunks_exact_mut(4)) { + dst.copy_from_slice(&src.to_le_bytes()); + } + + let k1 = belt_keyexpand::<16>(&k[..16].try_into().unwrap()); + let k2 = belt_keyexpand(&k); + + assert_eq!( + k1, + [ + 0xE9DEE72C, 0x8F0C0FA6, 0x2DDB49F4, 0x6F739647, 0xE9DEE72C, 0x8F0C0FA6, 0x2DDB49F4, + 0x6F739647 + ] + ); + assert_eq!( + k2, + [ + 0xE9DEE72C, 0x8F0C0FA6, 0x2DDB49F4, 0x6F739647, 0x06075316, 0xED247A37, 0x4B09A17E, + 0x8450BF66 + ] + ); +} + +#[test] +fn test_keyrep() { + let x: [u8; 32] = + hex!("E9DEE72C 8F0C0FA6 2DDB49F4 6F739647 06075316 ED247A37 39CBA383 03A98BF6"); + let d: [u8; 12] = hex!("01000000 00000000 00000000"); + let i: [u8; 16] = hex!("5BE3D612 17B96181 FE6786AD 716B890B"); + + let out: [u8; 16] = belt_keyrep(&x, &d, &i); + assert_eq!(out, hex!("6BBBC233 6670D31A B83DAA90 D52C0541")); + + let out: [u8; 24] = belt_keyrep(&x, &d, &i); + assert_eq!( + out, + hex!("9A2532A1 8CBAF145 398D5A95 FEEA6C82 5B9C1971 56A00275"), + ); + + let out: [u8; 32] = belt_keyrep(&x, &d, &i); + assert_eq!( + out, + hex!("76E166E6 AB21256B 6739397B 672B8796 14B81CF0 5955FC3A B09343A7 45C48F77"), + ); +} + +#[test] +fn test_kdf() { + let secret = hex!("723356E3 35ED7062 0FFB1842 752092C3 2603EB66 60409205 87D80057 5BECFC42"); + let iv = hex!( + "6B13ACBB 086FB876 18BCC2EF 20A3FA89 475654CB 367E670A 2441730B 24B8AB31" + "CD3D6487 DC4EEB23 45697818 6A069C71 375D75C2 DF198BAD 1E61EEA0 DBBFF737" + ); + let key = bake_kdf(&secret, &iv, 0); + assert_eq!( + key, + hex!("DAC4D8F4 11F9C523 D28BBAAB 32A5270E 4DFA1F0F 757EF8E0 F30AF08F BDE1E7F4"), + ); + + let key = bake_kdf(&secret, &iv, 1); + assert_eq!( + key, + hex!("54AC0582 84D679CF 4C47D3D7 2651F3E4 EF0D61D1 D0ED5BAF 8FF30B89 24E599D8"), + ); +}