use cosmian_ffi_utils::{ffi_read_bytes, ffi_read_string, ffi_unwrap, ffi_write_bytes, ErrorCode};
use crate::get_alphabet;
#[allow(clippy::too_many_arguments)]
pub unsafe fn fpe(
output_ptr: *mut u8,
output_len: *mut i32,
input_ptr: *const i8,
alphabet_id_ptr: *const i8,
key_ptr: *const i8,
key_len: i32,
tweak_ptr: *const i8,
tweak_len: i32,
additional_characters_ptr: *const i8,
encrypt_flag: bool,
) -> i32 {
let key_bytes = ffi_read_bytes!("key", key_ptr, key_len);
let tweak_bytes = ffi_read_bytes!("tweak", tweak_ptr, tweak_len);
let input_str = ffi_read_string!("input", input_ptr);
let alphabet_id_str = ffi_read_string!("alphabet_id", alphabet_id_ptr);
let mut alphabet = ffi_unwrap!(
get_alphabet(&alphabet_id_str),
"Alphabet id not supported",
ErrorCode::Fpe
);
let additional_characters_str =
ffi_read_string!("additional_characters_ptr", additional_characters_ptr);
alphabet.extend_with(&additional_characters_str);
let output_str = if encrypt_flag {
ffi_unwrap!(
alphabet.encrypt(key_bytes, tweak_bytes, &input_str),
"fpe encryption process",
ErrorCode::Encryption
)
} else {
ffi_unwrap!(
alphabet.decrypt(key_bytes, tweak_bytes, &input_str),
"fpe decryption process",
ErrorCode::Decryption
)
};
ffi_write_bytes!("output_ptr", output_str.as_bytes(), output_ptr, output_len);
}
#[no_mangle]
pub unsafe extern "C" fn h_fpe_encrypt_alphabet(
plaintext_ptr: *mut u8,
plaintext_len: *mut i32,
alphabet_id_ptr: *const i8,
input_ptr: *const i8,
key_ptr: *const i8,
key_len: i32,
tweak_ptr: *const i8,
tweak_len: i32,
additional_characters_ptr: *const i8,
) -> i32 {
fpe(
plaintext_ptr,
plaintext_len,
input_ptr,
alphabet_id_ptr,
key_ptr,
key_len,
tweak_ptr,
tweak_len,
additional_characters_ptr,
true,
)
}
#[no_mangle]
pub unsafe extern "C" fn h_fpe_decrypt_alphabet(
ciphertext_ptr: *mut u8,
ciphertext_len: *mut i32,
alphabet_id_ptr: *const i8,
input_ptr: *const i8,
key_ptr: *const i8,
key_len: i32,
tweak_ptr: *const i8,
tweak_len: i32,
additional_characters_ptr: *const i8,
) -> i32 {
fpe(
ciphertext_ptr,
ciphertext_len,
input_ptr,
alphabet_id_ptr,
key_ptr,
key_len,
tweak_ptr,
tweak_len,
additional_characters_ptr,
false,
)
}