#[cfg(test)]
pub mod tests {
use crate::kcapi_handle;
use std::ffi::CString;
const privkey: [u8; 547] = [
0x30, 0x82, 0x2, 0x1f, 0x2, 0x1, 0x1, 0x2, 0x82, 0x1, 0x0, 0xdb, 0x10, 0x1a, 0xc2, 0xa3,
0xf1, 0xdc, 0xff, 0x13, 0x6b, 0xed, 0x44, 0xdf, 0xf0, 0x2, 0x6d, 0x13, 0xc7, 0x88, 0xda,
0x70, 0x6b, 0x54, 0xf1, 0xe8, 0x27, 0xdc, 0xc3, 0xf, 0x99, 0x6a, 0xfa, 0xc6, 0x67, 0xff,
0x1d, 0x1e, 0x3c, 0x1d, 0xc1, 0xb5, 0x5f, 0x6c, 0xc0, 0xb2, 0x7, 0x3a, 0x6d, 0x41, 0xe4,
0x25, 0x99, 0xac, 0xfc, 0xd2, 0xf, 0x2, 0xd3, 0xd1, 0x54, 0x6, 0x1a, 0x51, 0x77, 0xbd,
0xb6, 0xbf, 0xea, 0xa7, 0x5c, 0x6, 0xa9, 0x5d, 0x69, 0x84, 0x45, 0xd7, 0xf5, 0x5, 0xba,
0x47, 0xf0, 0x1b, 0xd7, 0x2b, 0x24, 0xec, 0xcb, 0x9b, 0x1b, 0x10, 0x8d, 0x81, 0xa0, 0xbe,
0xb1, 0x8c, 0x33, 0xe4, 0x36, 0xb8, 0x43, 0xeb, 0x19, 0x2a, 0x81, 0x8d, 0xde, 0x81, 0xa,
0x99, 0x48, 0xb6, 0xf6, 0xbc, 0xcd, 0x49, 0x34, 0x3a, 0x8f, 0x26, 0x94, 0xe3, 0x28, 0x82,
0x1a, 0x7c, 0x8f, 0x59, 0x9f, 0x45, 0xe8, 0x5d, 0x1a, 0x45, 0x76, 0x4, 0x56, 0x5, 0xa1,
0xd0, 0x1b, 0x8c, 0x77, 0x6d, 0xaf, 0x53, 0xfa, 0x71, 0xe2, 0x67, 0xe0, 0x9a, 0xfe, 0x3,
0xa9, 0x85, 0xd2, 0xc9, 0xaa, 0xba, 0x2a, 0xbc, 0x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0xce,
0x24, 0x2e, 0xf7, 0x95, 0xb7, 0x7e, 0x86, 0x23, 0x30, 0x7b, 0xd, 0x58, 0x5f, 0x48, 0x5e,
0x1e, 0x23, 0x5c, 0x5e, 0x3c, 0x8a, 0x4c, 0x84, 0xcc, 0xb6, 0x3b, 0xab, 0x4d, 0x8c, 0xdd,
0x69, 0x86, 0x20, 0x34, 0xfb, 0x3e, 0xa6, 0x83, 0x33, 0xab, 0x9a, 0xdb, 0x59, 0x2d, 0xf2,
0x9f, 0xd7, 0x64, 0x78, 0x2d, 0x80, 0xa9, 0xad, 0x58, 0x5b, 0xc4, 0xe, 0x1d, 0x7e, 0x73,
0xf7,
];
#[test]
#[ignore]
fn test_akcipher_init() {
unsafe {
let mut handle =
Box::into_raw(Box::new(kcapi_handle { _unused: [0u8; 0] })) as *mut kcapi_handle;
let alg = CString::new("rsa").expect("Failed to create CString");
let mut ret = crate::kcapi_akcipher_init(&mut handle as *mut _, alg.as_ptr(), 0);
assert_eq!(ret, 0);
ret = crate::kcapi_akcipher_setkey(handle, privkey.as_ptr(), privkey.len() as u32);
assert_eq!(ret, 256);
ret = crate::kcapi_akcipher_setpubkey(handle, pubkey.as_ptr(), pubkey.len() as u32);
assert_eq!(ret, 256);
}
}
#[test]
#[ignore]
fn test_ackipher_enc() {
unsafe {
let mut handle =
Box::into_raw(Box::new(kcapi_handle { _unused: [0u8; 0] })) as *mut kcapi_handle;
let alg = CString::new("rsa").expect("Failed to create CString");
let mut ret = crate::kcapi_akcipher_init(&mut handle as *mut _, alg.as_ptr(), 0);
assert_eq!(ret, 0);
ret = crate::kcapi_akcipher_setpubkey(handle, pubkey.as_ptr(), pubkey.len() as u32);
assert_eq!(ret, 256);
let mut out = [0u8; ct.len()];
let inp = pt.clone();
let ret64 = crate::kcapi_akcipher_encrypt(
handle,
inp.as_ptr(),
inp.len() as crate::size_t,
out.as_mut_ptr(),
out.len() as crate::size_t,
crate::KCAPI_ACCESS_HEURISTIC as ::std::os::raw::c_int,
);
assert_eq!(out, ct);
assert_eq!(ret64, ct.len() as i64);
}
}
#[test]
#[ignore]
fn test_akcipher_dec() {
unsafe {
let mut handle =
Box::into_raw(Box::new(kcapi_handle { _unused: [0u8; 0] })) as *mut kcapi_handle;
let alg = CString::new("rsa").expect("Failed to create CString");
let mut ret = crate::kcapi_akcipher_init(&mut handle as *mut _, alg.as_ptr(), 0);
assert_eq!(ret, 0);
ret = crate::kcapi_akcipher_setkey(handle, privkey.as_ptr(), privkey.len() as u32);
assert_eq!(ret, 256);
let mut out = [0u8; ct.len()];
let inp = ct.clone();
let ret64 = crate::kcapi_akcipher_decrypt(
handle,
inp.as_ptr(),
inp.len() as crate::size_t,
out.as_mut_ptr(),
out.len() as crate::size_t,
crate::KCAPI_ACCESS_HEURISTIC as ::std::os::raw::c_int,
);
let mut pt_out = vec![0u8; pt.len()];
pt_out.clone_from_slice(&out[out.len() - pt.len()..]);
assert_eq!(pt_out, pt);
assert_eq!(ret64, ct.len() as i64);
}
}
#[test]
#[ignore]
fn test_akcipher_sign() {
unsafe {
let mut handle =
Box::into_raw(Box::new(kcapi_handle { _unused: [0u8; 0] })) as *mut kcapi_handle;
let alg =
CString::new("pkcs1pad(rsa-generic,sha256)").expect("Failed to create CString");
let mut ret = crate::kcapi_akcipher_init(&mut handle as *mut _, alg.as_ptr(), 0);
assert_eq!(ret, 0);
let mut hash = [0u8; 32];
let mut ret64 = crate::kcapi_md_sha256(
pt.as_ptr(),
pt.len() as crate::size_t,
hash.as_mut_ptr(),
hash.len() as crate::size_t,
);
assert_eq!(hash.len(), ret64 as usize);
ret = crate::kcapi_akcipher_setkey(handle, privkey.as_ptr(), privkey.len() as u32);
assert_eq!(ret, 256);
let mut out = [0u8; ct.len()];
ret64 = crate::kcapi_akcipher_sign(
handle,
hash.as_ptr(),
hash.len() as crate::size_t,
out.as_mut_ptr(),
out.len() as crate::size_t,
crate::KCAPI_ACCESS_HEURISTIC as ::std::os::raw::c_int,
);
assert_eq!(sig, out);
assert_eq!(out.len(), ret64 as usize);
}
}
#[test]
#[ignore]
fn test_akcipher_verify() {
unsafe {
let mut handle =
Box::into_raw(Box::new(kcapi_handle { _unused: [0u8; 0] })) as *mut kcapi_handle;
let alg =
CString::new("pkcs1pad(rsa-generic,sha256)").expect("Failed to create CString");
let mut ret = crate::kcapi_akcipher_init(&mut handle as *mut _, alg.as_ptr(), 0);
assert_eq!(ret, 0);
let mut hash = [0u8; 32];
let mut ret64 = crate::kcapi_md_sha256(
pt.as_ptr(),
pt.len() as crate::size_t,
hash.as_mut_ptr(),
hash.len() as crate::size_t,
);
assert_eq!(hash.len(), ret64 as usize);
ret = crate::kcapi_akcipher_setpubkey(handle, pubkey.as_ptr(), pubkey.len() as u32);
assert_eq!(ret, 256);
let mut inp = Vec::new();
inp.extend(sig.iter().copied());
inp.extend(hash.iter().copied());
let mut out = [0u8; sig.len()];
ret64 = crate::kcapi_akcipher_verify(
handle,
inp.as_ptr(),
inp.len() as crate::size_t,
out.as_mut_ptr(),
out.len() as crate::size_t,
crate::KCAPI_ACCESS_HEURISTIC as ::std::os::raw::c_int,
);
assert_eq!(ret64, 0);
}
}
#[test]
#[ignore]
fn test_akcipher_verify_sig_fail() {
unsafe {
let mut handle =
Box::into_raw(Box::new(kcapi_handle { _unused: [0u8; 0] })) as *mut kcapi_handle;
let alg =
CString::new("pkcs1pad(rsa-generic,sha256)").expect("Failed to create CString");
let mut ret = crate::kcapi_akcipher_init(&mut handle as *mut _, alg.as_ptr(), 0);
assert_eq!(ret, 0);
let mut hash = [0u8; 32];
let mut ret64 = crate::kcapi_md_sha256(
pt.as_ptr(),
pt.len() as crate::size_t,
hash.as_mut_ptr(),
hash.len() as crate::size_t,
);
assert_eq!(hash.len(), ret64 as usize);
ret = crate::kcapi_akcipher_setpubkey(handle, pubkey.as_ptr(), pubkey.len() as u32);
assert_eq!(ret, 256);
let mut inp = Vec::new();
inp.extend(sig.iter().copied());
inp[sig.len() - 1] ^= 0x01;
inp.extend(hash.iter().copied());
let mut out = [0u8; sig.len()];
ret64 = crate::kcapi_akcipher_verify(
handle,
inp.as_ptr(),
inp.len() as crate::size_t,
out.as_mut_ptr(),
out.len() as crate::size_t,
crate::KCAPI_ACCESS_HEURISTIC as ::std::os::raw::c_int,
);
assert!(ret64 < 0);
}
}
#[test]
#[ignore]
fn test_akcipher_verify_digest_fail() {
unsafe {
let mut handle =
Box::into_raw(Box::new(kcapi_handle { _unused: [0u8; 0] })) as *mut kcapi_handle;
let alg =
CString::new("pkcs1pad(rsa-generic,sha256)").expect("Failed to create CString");
let mut ret = crate::kcapi_akcipher_init(&mut handle as *mut _, alg.as_ptr(), 0);
assert_eq!(ret, 0);
let mut hash = [0u8; 32];
let mut ret64 = crate::kcapi_md_sha256(
pt.as_ptr(),
pt.len() as crate::size_t,
hash.as_mut_ptr(),
hash.len() as crate::size_t,
);
assert_eq!(hash.len(), ret64 as usize);
ret = crate::kcapi_akcipher_setpubkey(handle, pubkey.as_ptr(), pubkey.len() as u32);
assert_eq!(ret, 256);
let mut inp = Vec::new();
inp.extend(sig.iter().copied());
inp.extend(hash.iter().copied());
let inp_len: usize = inp.len();
inp[inp_len - 1] ^= 0x01;
let mut out = [0u8; sig.len()];
ret64 = crate::kcapi_akcipher_verify(
handle,
inp.as_ptr(),
inp.len() as crate::size_t,
out.as_mut_ptr(),
out.len() as crate::size_t,
crate::KCAPI_ACCESS_HEURISTIC as ::std::os::raw::c_int,
);
assert!(ret64 < 0);
}
}
#[test]
#[ignore]
fn test_akcipher_verify_key_fail() {
unsafe {
let mut handle =
Box::into_raw(Box::new(kcapi_handle { _unused: [0u8; 0] })) as *mut kcapi_handle;
let alg =
CString::new("pkcs1pad(rsa-generic,sha256)").expect("Failed to create CString");
let mut ret = crate::kcapi_akcipher_init(&mut handle as *mut _, alg.as_ptr(), 0);
assert_eq!(ret, 0);
let mut hash = [0u8; 32];
let mut ret64 = crate::kcapi_md_sha256(
pt.as_ptr(),
pt.len() as crate::size_t,
hash.as_mut_ptr(),
hash.len() as crate::size_t,
);
assert_eq!(hash.len(), ret64 as usize);
let mut pubkey_corrupt = pubkey.clone();
pubkey_corrupt[pubkey.len() - pubkey.len() / 2] ^= 0x01;
ret = crate::kcapi_akcipher_setpubkey(
handle,
pubkey_corrupt.as_ptr(),
pubkey_corrupt.len() as u32,
);
assert_eq!(ret, 256);
let mut inp = Vec::new();
inp.extend(sig.iter().copied());
inp.extend(hash.iter().copied());
let mut out = [0u8; sig.len()];
ret64 = crate::kcapi_akcipher_verify(
handle,
inp.as_ptr(),
inp.len() as crate::size_t,
out.as_mut_ptr(),
out.len() as crate::size_t,
crate::KCAPI_ACCESS_HEURISTIC as ::std::os::raw::c_int,
);
assert!(ret64 < 0);
}
}
}