/*
 * Copyright (C) 2025 - 2026, Stephan Mueller <smueller@chronox.de>
 *
 * License: see LICENSE file in root directory
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
 * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */

use leancrypto_sys::lcr_hash::lcr_hash_type;
use leancrypto_sys::lcr_kbkdf::{lcr_kbkdf_ctr, lcr_kbkdf_fb};

#[test]
fn lc_rust_ctr_kdf_tester() {
    let key: [u8; 32] = [
        0xdd, 0x1d, 0x91, 0xb7, 0xd9, 0x0b, 0x2b, 0xd3, 0x13, 0x85, 0x33, 0xce,
        0x92, 0xb2, 0x72, 0xfb, 0xf8, 0xa3, 0x69, 0x31, 0x6a, 0xef, 0xe2, 0x42,
        0xe6, 0x59, 0xcc, 0x0a, 0xe2, 0x38, 0xaf, 0xe0,
    ];
    let label: [u8; 60] = [
        0x01, 0x32, 0x2b, 0x96, 0xb3, 0x0a, 0xcd, 0x19, 0x79, 0x79, 0x44, 0x4e,
        0x46, 0x8e, 0x1c, 0x5c, 0x68, 0x59, 0xbf, 0x1b, 0x1c, 0xf9, 0x51, 0xb7,
        0xe7, 0x25, 0x30, 0x3e, 0x23, 0x7e, 0x46, 0xb8, 0x64, 0xa1, 0x45, 0xfa,
        0xb2, 0x5e, 0x51, 0x7b, 0x08, 0xf8, 0x68, 0x3d, 0x03, 0x15, 0xbb, 0x29,
        0x11, 0xd8, 0x0a, 0x0e, 0x8a, 0xba, 0x17, 0xf3, 0xb4, 0x13, 0xfa, 0xac,
    ];
    let exp: [u8; 16] = [
        0x10, 0x62, 0x13, 0x42, 0xbf, 0xb0, 0xfd, 0x40, 0x04, 0x6c, 0x0e, 0x29,
        0xf2, 0xcf, 0xdb, 0xf0,
    ];

    let mut kbkdf = lcr_kbkdf_ctr::new(lcr_hash_type::lcr_sha2_256);
    let mut act = vec![0u8; exp.len()];
    let result = kbkdf.derive(&key, &label, &mut act);
    assert_eq!(result, Ok(()));
    assert_eq!(act, &exp[..]);
}

#[test]
fn lc_rust_fb_kdf_tester() {
    let key: [u8; 32] = [
        0x93, 0xf6, 0x98, 0xe8, 0x42, 0xee, 0xd7, 0x53, 0x94, 0xd6, 0x29, 0xd9,
        0x57, 0xe2, 0xe8, 0x9c, 0x6e, 0x74, 0x1f, 0x81, 0x0b, 0x62, 0x3c, 0x8b,
        0x90, 0x1e, 0x38, 0x37, 0x6d, 0x06, 0x8e, 0x7b,
    ];
    let iv: [u8; 32] = [
        0x9f, 0x57, 0x5d, 0x90, 0x59, 0xd3, 0xe0, 0xc0, 0x80, 0x3f, 0x08, 0x11,
        0x2f, 0x8a, 0x80, 0x6d, 0xe3, 0xc3, 0x47, 0x19, 0x12, 0xcd, 0xf4, 0x2b,
        0x09, 0x53, 0x88, 0xb1, 0x4b, 0x33, 0x50, 0x8e,
    ];
    let label: [u8; 51] = [
        0x53, 0xb8, 0x9c, 0x18, 0x69, 0x0e, 0x20, 0x57, 0xa1, 0xd1, 0x67, 0x82,
        0x2e, 0x63, 0x6d, 0xe5, 0x0b, 0xe0, 0x01, 0x85, 0x32, 0xc4, 0x31, 0xf7,
        0xf5, 0xe3, 0x7f, 0x77, 0x13, 0x92, 0x20, 0xd5, 0xe0, 0x42, 0x59, 0x9e,
        0xbe, 0x26, 0x6a, 0xf5, 0x76, 0x7e, 0xe1, 0x8c, 0xd2, 0xc5, 0xc1, 0x9a,
        0x1f, 0x0f, 0x80,
    ];
    let exp: [u8; 64] = [
        0xbd, 0x14, 0x76, 0xf4, 0x3a, 0x4e, 0x31, 0x57, 0x47, 0xcf, 0x59, 0x18,
        0xe0, 0xea, 0x5b, 0xc0, 0xd9, 0x87, 0x69, 0x45, 0x74, 0x77, 0xc3, 0xab,
        0x18, 0xb7, 0x42, 0xde, 0xf0, 0xe0, 0x79, 0xa9, 0x33, 0xb7, 0x56, 0x36,
        0x5a, 0xfb, 0x55, 0x41, 0xf2, 0x53, 0xfe, 0xe4, 0x3c, 0x6f, 0xd7, 0x88,
        0xa4, 0x40, 0x41, 0x03, 0x85, 0x09, 0xe9, 0xee, 0xb6, 0x8f, 0x7d, 0x65,
        0xff, 0xbb, 0x5f, 0x95,
    ];

    let mut kbkdf = lcr_kbkdf_fb::new(lcr_hash_type::lcr_sha2_256);
    let mut act = vec![0u8; exp.len()];
    let result = kbkdf.derive(&key, &iv, &label, &mut act);
    assert_eq!(result, Ok(()));
    assert_eq!(act, &exp[..]);
}
