Rust wrapper: support optional heap and dev_id parameters
This commit is contained in:
@@ -34,7 +34,7 @@ use wolfssl_sys as ws;
|
||||
/// # Example
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::aes::CBC;
|
||||
/// let mut cbc = CBC::new().expect("Failed to create CBC");
|
||||
/// let mut cbc = CBC::new(None, None).expect("Failed to create CBC");
|
||||
/// let key: &[u8; 16] = b"0123456789abcdef";
|
||||
/// let iv: &[u8; 16] = b"1234567890abcdef";
|
||||
/// let msg: [u8; 16] = [
|
||||
@@ -60,12 +60,17 @@ pub struct CBC {
|
||||
impl CBC {
|
||||
/// Create a new `CBC` instance.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A Result which is Ok(CBC) on success or an Err containing the wolfSSL
|
||||
/// library return code on failure.
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
let ws_aes = new_ws_aes()?;
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let ws_aes = new_ws_aes(heap, dev_id)?;
|
||||
let cbc = CBC {ws_aes};
|
||||
Ok(cbc)
|
||||
}
|
||||
@@ -225,7 +230,7 @@ impl Drop for CBC {
|
||||
/// 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
|
||||
/// ];
|
||||
///
|
||||
/// let mut ccm = CCM::new().expect("Failed to create CCM");
|
||||
/// let mut ccm = CCM::new(None, None).expect("Failed to create CCM");
|
||||
/// ccm.init(&key).expect("Error with init()");
|
||||
/// let mut auth_tag_out: [u8; 8] = [0; 8];
|
||||
/// let mut cipher_out: [u8; 23] = [0; 23];
|
||||
@@ -245,12 +250,17 @@ pub struct CCM {
|
||||
impl CCM {
|
||||
/// Create a new `CCM` instance.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A Result which is Ok(CCM) on success or an Err containing the wolfSSL
|
||||
/// library return code on failure.
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
let ws_aes = new_ws_aes()?;
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let ws_aes = new_ws_aes(heap, dev_id)?;
|
||||
let ccm = CCM {ws_aes};
|
||||
Ok(ccm)
|
||||
}
|
||||
@@ -380,7 +390,7 @@ impl Drop for CCM {
|
||||
/// # Example
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::aes::CFB;
|
||||
/// let mut cfb = CFB::new().expect("Failed to create CFB");
|
||||
/// let mut cfb = CFB::new(None, None).expect("Failed to create CFB");
|
||||
/// let key: [u8; 16] = [
|
||||
/// 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
|
||||
/// 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
|
||||
@@ -421,12 +431,17 @@ pub struct CFB {
|
||||
impl CFB {
|
||||
/// Create a new `CFB` instance.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A Result which is Ok(CFB) on success or an Err containing the wolfSSL
|
||||
/// library return code on failure.
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
let ws_aes = new_ws_aes()?;
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let ws_aes = new_ws_aes(heap, dev_id)?;
|
||||
let cfb = CFB {ws_aes};
|
||||
Ok(cfb)
|
||||
}
|
||||
@@ -690,7 +705,7 @@ impl Drop for CFB {
|
||||
/// 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
|
||||
/// 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
|
||||
/// ];
|
||||
/// let mut ctr = CTR::new().expect("Failed to create CTR");
|
||||
/// let mut ctr = CTR::new(None, None).expect("Failed to create CTR");
|
||||
/// ctr.init(&key, &iv).expect("Error with init()");
|
||||
/// let mut outbuf: [u8; 64] = [0; 64];
|
||||
/// ctr.encrypt(&msg, &mut outbuf).expect("Error with encrypt()");
|
||||
@@ -706,12 +721,17 @@ pub struct CTR {
|
||||
impl CTR {
|
||||
/// Create a new `CTR` instance.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A Result which is Ok(CTR) on success or an Err containing the wolfSSL
|
||||
/// library return code on failure.
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
let ws_aes = new_ws_aes()?;
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let ws_aes = new_ws_aes(heap, dev_id)?;
|
||||
let ctr = CTR {ws_aes};
|
||||
Ok(ctr)
|
||||
}
|
||||
@@ -948,7 +968,7 @@ impl EAX {
|
||||
/// # Example
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::aes::ECB;
|
||||
/// let mut ecb = ECB::new().expect("Failed to create ECB");
|
||||
/// let mut ecb = ECB::new(None, None).expect("Failed to create ECB");
|
||||
/// let key_128: &[u8; 16] = b"0123456789abcdef";
|
||||
/// let msg: [u8; 16] = [
|
||||
/// 0x6e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
|
||||
@@ -973,12 +993,17 @@ pub struct ECB {
|
||||
impl ECB {
|
||||
/// Create a new `ECB` instance.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A Result which is Ok(ECB) on success or an Err containing the wolfSSL
|
||||
/// library return code on failure.
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
let ws_aes = new_ws_aes()?;
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let ws_aes = new_ws_aes(heap, dev_id)?;
|
||||
let ecb = ECB {ws_aes};
|
||||
Ok(ecb)
|
||||
}
|
||||
@@ -1137,7 +1162,7 @@ impl Drop for ECB {
|
||||
/// 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
|
||||
/// 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
|
||||
/// ];
|
||||
/// let mut gcm = GCM::new().expect("Failed to create GCM");
|
||||
/// let mut gcm = GCM::new(None, None).expect("Failed to create GCM");
|
||||
/// gcm.init(&key).expect("Error with init()");
|
||||
/// let mut cipher: [u8; 32] = [0; 32];
|
||||
/// let mut auth_tag: [u8; 16] = [0; 16];
|
||||
@@ -1154,12 +1179,17 @@ pub struct GCM {
|
||||
impl GCM {
|
||||
/// Create a new `GCM` instance.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A Result which is Ok(GCM) on success or an Err containing the wolfSSL
|
||||
/// library return code on failure.
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
let ws_aes = new_ws_aes()?;
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let ws_aes = new_ws_aes(heap, dev_id)?;
|
||||
let gcm = GCM {ws_aes};
|
||||
Ok(gcm)
|
||||
}
|
||||
@@ -1333,7 +1363,7 @@ impl Drop for GCM {
|
||||
/// 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
|
||||
/// 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
|
||||
/// ];
|
||||
/// let mut gcmstream = GCMStream::new().expect("Failed to create GCMStream");
|
||||
/// let mut gcmstream = GCMStream::new(None, None).expect("Failed to create GCMStream");
|
||||
/// for chunk_size in 1..=auth.len() {
|
||||
/// gcmstream.init(&key, &iv).expect("Error with init()");
|
||||
/// let mut cipher: [u8; 60] = [0; 60];
|
||||
@@ -1367,12 +1397,17 @@ pub struct GCMStream {
|
||||
impl GCMStream {
|
||||
/// Create a new `GCMStream` instance.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A Result which is Ok(GCMStream) on success or an Err containing the
|
||||
/// wolfSSL library return code on failure.
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
let ws_aes = new_ws_aes()?;
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let ws_aes = new_ws_aes(heap, dev_id)?;
|
||||
let gcmstream = GCMStream {ws_aes};
|
||||
Ok(gcmstream)
|
||||
}
|
||||
@@ -1584,7 +1619,7 @@ impl Drop for GCMStream {
|
||||
/// 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
|
||||
/// 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
|
||||
/// ];
|
||||
/// let mut ofb = OFB::new().expect("Failed to create OFB");
|
||||
/// let mut ofb = OFB::new(None, None).expect("Failed to create OFB");
|
||||
/// ofb.init(&key, &iv).expect("Error with init()");
|
||||
/// let mut cipher: [u8; 48] = [0; 48];
|
||||
/// ofb.encrypt(&plain, &mut cipher).expect("Error with encrypt()");
|
||||
@@ -1600,12 +1635,17 @@ pub struct OFB {
|
||||
impl OFB {
|
||||
/// Create a new `OFB` instance.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A Result which is Ok(OFB) on success or an Err containing the wolfSSL
|
||||
/// library return code on failure.
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
let ws_aes = new_ws_aes()?;
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let ws_aes = new_ws_aes(heap, dev_id)?;
|
||||
let ofb = OFB {ws_aes};
|
||||
Ok(ofb)
|
||||
}
|
||||
@@ -1749,7 +1789,7 @@ impl Drop for OFB {
|
||||
/// 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a
|
||||
/// ];
|
||||
///
|
||||
/// let mut xts = XTS::new().expect("Failed to create XTS");
|
||||
/// let mut xts = XTS::new(None, None).expect("Failed to create XTS");
|
||||
/// xts.init_encrypt(&key).expect("Error with init_encrypt()");
|
||||
/// let mut cipher: [u8; 16] = [0; 16];
|
||||
/// xts.encrypt(&plain, &mut cipher, &tweak).expect("Error with encrypt()");
|
||||
@@ -1774,12 +1814,17 @@ pub struct XTS {
|
||||
impl XTS {
|
||||
/// Create a new `XTS` instance.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A Result which is Ok(XTS) on success or an Err containing the wolfSSL
|
||||
/// library return code on failure.
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
let ws_xtsaes = new_ws_xtsaes()?;
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let ws_xtsaes = new_ws_xtsaes(heap, dev_id)?;
|
||||
let xts = XTS {ws_xtsaes};
|
||||
Ok(xts)
|
||||
}
|
||||
@@ -2100,7 +2145,7 @@ impl Drop for XTS {
|
||||
/// 0xB5, 0x5A, 0xDD, 0xCB, 0x80, 0xE0, 0xFC, 0xCD
|
||||
/// ];
|
||||
///
|
||||
/// let mut xtsstream = XTSStream::new().expect("Failed to create XTSStream");
|
||||
/// let mut xtsstream = XTSStream::new(None, None).expect("Failed to create XTSStream");
|
||||
/// xtsstream.init_encrypt(&keys, &tweak).expect("Error with init_encrypt()");
|
||||
/// let mut cipher: [u8; 40] = [0; 40];
|
||||
/// xtsstream.encrypt_update(&plain[0..16], &mut cipher[0..16]).expect("Error with encrypt_update()");
|
||||
@@ -2120,12 +2165,17 @@ pub struct XTSStream {
|
||||
impl XTSStream {
|
||||
/// Create a new `XTSStream` instance.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A Result which is Ok(XTSStream) on success or an Err containing the
|
||||
/// wolfSSL library return code on failure.
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
let ws_xtsaes = new_ws_xtsaes()?;
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let ws_xtsaes = new_ws_xtsaes(heap, dev_id)?;
|
||||
let ws_xtsaesstreamdata: MaybeUninit<ws::XtsAesStreamData> = MaybeUninit::uninit();
|
||||
let ws_xtsaesstreamdata = unsafe { ws_xtsaesstreamdata.assume_init() };
|
||||
let xtsstream = XTSStream {ws_xtsaes, ws_xtsaesstreamdata};
|
||||
@@ -2353,10 +2403,18 @@ impl Drop for XTSStream {
|
||||
}
|
||||
}
|
||||
|
||||
fn new_ws_aes() -> Result<ws::Aes, i32> {
|
||||
fn new_ws_aes(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<ws::Aes, i32> {
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let mut ws_aes: MaybeUninit<ws::Aes> = MaybeUninit::uninit();
|
||||
let rc = unsafe {
|
||||
ws::wc_AesInit(ws_aes.as_mut_ptr(), core::ptr::null_mut(), ws::INVALID_DEVID)
|
||||
ws::wc_AesInit(ws_aes.as_mut_ptr(), heap, dev_id)
|
||||
};
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
@@ -2365,10 +2423,18 @@ fn new_ws_aes() -> Result<ws::Aes, i32> {
|
||||
Ok(ws_aes)
|
||||
}
|
||||
|
||||
fn new_ws_xtsaes() -> Result<ws::XtsAes, i32> {
|
||||
fn new_ws_xtsaes(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<ws::XtsAes, i32> {
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let mut ws_xtsaes: MaybeUninit<ws::XtsAes> = MaybeUninit::uninit();
|
||||
let rc = unsafe {
|
||||
ws::wc_AesXtsInit(ws_xtsaes.as_mut_ptr(), core::ptr::null_mut(), ws::INVALID_DEVID)
|
||||
ws::wc_AesXtsInit(ws_xtsaes.as_mut_ptr(), heap, dev_id)
|
||||
};
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
|
||||
@@ -45,6 +45,8 @@ impl CMAC {
|
||||
/// * `key`: Key to use for CMAC generation.
|
||||
/// * `data`: CMAC input data.
|
||||
/// * `dout`: Output buffer where CMAC is written.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -86,6 +88,8 @@ impl CMAC {
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `key`: Key to use for CMAC generation.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -100,15 +104,23 @@ impl CMAC {
|
||||
/// 0x2bu8, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
|
||||
/// 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
|
||||
/// ];
|
||||
/// let mut cmac = CMAC::new(&key).expect("Error with new()");
|
||||
/// let mut cmac = CMAC::new(&key, None, None).expect("Error with new()");
|
||||
/// ```
|
||||
pub fn new(key: &[u8]) -> Result<Self, i32> {
|
||||
pub fn new(key: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let key_size = key.len() as u32;
|
||||
let mut ws_cmac: MaybeUninit<ws::Cmac> = MaybeUninit::uninit();
|
||||
let typ = ws::CmacType_WC_CMAC_AES as i32;
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe {
|
||||
ws::wc_InitCmac(ws_cmac.as_mut_ptr(), key.as_ptr(), key_size,
|
||||
typ, core::ptr::null_mut())
|
||||
ws::wc_InitCmac_ex(ws_cmac.as_mut_ptr(), key.as_ptr(), key_size,
|
||||
typ, core::ptr::null_mut(), heap, dev_id)
|
||||
};
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
@@ -164,6 +176,61 @@ impl CMAC {
|
||||
Ok(rc == 0)
|
||||
}
|
||||
|
||||
/// One-shot CMAC generation function (with heap and device ID).
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `key`: Key to use for CMAC generation.
|
||||
/// * `data`: CMAC input data.
|
||||
/// * `dout`: Output buffer where CMAC is written.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
|
||||
/// library error code value.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::cmac::CMAC;
|
||||
/// let key = [
|
||||
/// 0x2bu8, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
|
||||
/// 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
|
||||
/// ];
|
||||
/// let message = [
|
||||
/// 0x6bu8, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
|
||||
/// 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
|
||||
/// ];
|
||||
/// let mut generate_out = [0u8; 16];
|
||||
/// let mut cmac = CMAC::new(&key, None, None).expect("Error with new()");
|
||||
/// cmac.generate_ex(&key, &message, &mut generate_out, None, None).expect("Error with generate_ex()");
|
||||
/// ```
|
||||
pub fn generate_ex(&mut self, key: &[u8], data: &[u8], dout: &mut [u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<(), i32> {
|
||||
let key_size = key.len() as u32;
|
||||
let data_size = data.len() as u32;
|
||||
let mut dout_size = dout.len() as u32;
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe {
|
||||
ws::wc_AesCmacGenerate_ex(&mut self.ws_cmac,
|
||||
dout.as_mut_ptr(), &mut dout_size,
|
||||
data.as_ptr(), data_size,
|
||||
key.as_ptr(), key_size, heap, dev_id)
|
||||
};
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Add CMAC input data.
|
||||
///
|
||||
/// # Parameters
|
||||
@@ -187,7 +254,7 @@ impl CMAC {
|
||||
/// 0x6bu8, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
|
||||
/// 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
|
||||
/// ];
|
||||
/// let mut cmac = CMAC::new(&key).expect("Error with new()");
|
||||
/// let mut cmac = CMAC::new(&key, None, None).expect("Error with new()");
|
||||
/// cmac.update(&message).expect("Error with update()");
|
||||
/// ```
|
||||
pub fn update(&mut self, data: &[u8]) -> Result<(), i32> {
|
||||
@@ -227,7 +294,7 @@ impl CMAC {
|
||||
/// 0x6bu8, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
|
||||
/// 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
|
||||
/// ];
|
||||
/// let mut cmac = CMAC::new(&key).expect("Error with new()");
|
||||
/// let mut cmac = CMAC::new(&key, None, None).expect("Error with new()");
|
||||
/// cmac.update(&message).expect("Error with update()");
|
||||
/// let mut finalize_out = [0u8; 16];
|
||||
/// cmac.finalize(&mut finalize_out).expect("Error with finalize()");
|
||||
@@ -243,6 +310,64 @@ impl CMAC {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// One-shot CMAC verification function (with optional heap and device ID).
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `key`: Key to use for CMAC generation.
|
||||
/// * `data`: CMAC input data.
|
||||
/// * `check`: CMAC value to compare to.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(valid) (with valid indicating if the CMAC passed in
|
||||
/// is correct or not) on success or Err(e) containing the wolfSSL library
|
||||
/// error code value.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::cmac::CMAC;
|
||||
/// let key = [
|
||||
/// 0x2bu8, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
|
||||
/// 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
|
||||
/// ];
|
||||
/// let message = [
|
||||
/// 0x6bu8, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
|
||||
/// 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
|
||||
/// ];
|
||||
/// let mut generate_out = [0u8; 16];
|
||||
/// CMAC::generate(&key, &message, &mut generate_out).expect("Error with generate()");
|
||||
/// let mut cmac = CMAC::new(&key, None, None).expect("Error with new()");
|
||||
/// let valid = cmac.verify_ex(&key, &message, &generate_out, None, None).expect("Error with verify_ex()");
|
||||
/// assert!(valid);
|
||||
/// ```
|
||||
pub fn verify_ex(&mut self, key: &[u8], data: &[u8], check: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<bool, i32> {
|
||||
let key_size = key.len() as u32;
|
||||
let data_size = data.len() as u32;
|
||||
let check_size = check.len() as u32;
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe {
|
||||
ws::wc_AesCmacVerify_ex(&mut self.ws_cmac,
|
||||
check.as_ptr(), check_size,
|
||||
data.as_ptr(), data_size,
|
||||
key.as_ptr(), key_size, heap, dev_id)
|
||||
};
|
||||
if rc < 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
Ok(rc == 0)
|
||||
}
|
||||
}
|
||||
impl Drop for CMAC {
|
||||
/// Safely free the wolfSSL resources.
|
||||
|
||||
@@ -73,7 +73,7 @@ impl DH {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::dh::DH;
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let mut dh = DH::new_named(DH::FFDHE_2048).expect("Error with new_named()");
|
||||
/// let mut dh = DH::new_named(DH::FFDHE_2048, None, None).expect("Error with new_named()");
|
||||
/// let mut private = [0u8; 256];
|
||||
/// let mut private_size = 0u32;
|
||||
/// let mut public = [0u8; 256];
|
||||
@@ -121,7 +121,7 @@ impl DH {
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::dh::DH;
|
||||
/// let mut dh = DH::new_named(DH::FFDHE_2048).expect("Error with new_named()");
|
||||
/// let mut dh = DH::new_named(DH::FFDHE_2048, None, None).expect("Error with new_named()");
|
||||
/// let mut p = [0u8; 256];
|
||||
/// let mut q = [0u8; 256];
|
||||
/// let mut g = [0u8; 256];
|
||||
@@ -159,6 +159,8 @@ impl DH {
|
||||
///
|
||||
/// * `rng`: `RNG` struct instance to use for random number generation.
|
||||
/// * `modulus_size`: Modulus size in bits.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -171,11 +173,19 @@ impl DH {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::dh::DH;
|
||||
/// let mut rng = RNG::new().expect("Error with RNG::new()");
|
||||
/// let mut dh = DH::generate(&mut rng, 2048).expect("Error with generate()");
|
||||
/// let mut dh = DH::generate(&mut rng, 2048, None, None).expect("Error with generate()");
|
||||
/// ```
|
||||
pub fn generate(rng: &mut RNG, modulus_size: i32) -> Result<Self, i32> {
|
||||
pub fn generate(rng: &mut RNG, modulus_size: i32, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_dhkey: MaybeUninit<ws::DhKey> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_InitDhKey(wc_dhkey.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitDhKey_ex(wc_dhkey.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -242,14 +252,32 @@ impl DH {
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `name`: DH parameters name, one of DH::FFDHE_*.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(dh) containing the DH struct instance or Err(e)
|
||||
/// containing the wolfSSL library error code value.
|
||||
pub fn new_named(name: i32) -> Result<Self, i32> {
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::dh::DH;
|
||||
/// let mut dh = DH::new_named(DH::FFDHE_2048, None, None).expect("Error with new_named()");
|
||||
/// ```
|
||||
pub fn new_named(name: i32, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_dhkey: MaybeUninit<ws::DhKey> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_InitDhKey(wc_dhkey.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitDhKey_ex(wc_dhkey.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -268,6 +296,8 @@ impl DH {
|
||||
///
|
||||
/// * `p`: DH 'p' parameter value.
|
||||
/// * `g`: DH 'g' parameter value.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -347,13 +377,21 @@ impl DH {
|
||||
/// 0x75, 0x52, 0x9d, 0x42, 0x51, 0x78, 0x62, 0x68,
|
||||
/// 0x05, 0x45, 0x15, 0xf8, 0xa2, 0x4e, 0xf3, 0x0b
|
||||
/// ];
|
||||
/// let dh = DH::new_from_pg(&p, &g).expect("Error with new_from_pg()");
|
||||
/// let dh = DH::new_from_pg(&p, &g, None, None).expect("Error with new_from_pg()");
|
||||
/// ```
|
||||
pub fn new_from_pg(p: &[u8], g: &[u8]) -> Result<Self, i32> {
|
||||
pub fn new_from_pg(p: &[u8], g: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let p_size = p.len() as u32;
|
||||
let g_size = g.len() as u32;
|
||||
let mut wc_dhkey: MaybeUninit<ws::DhKey> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_InitDhKey(wc_dhkey.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitDhKey_ex(wc_dhkey.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -375,6 +413,8 @@ impl DH {
|
||||
/// * `p`: DH 'p' parameter value.
|
||||
/// * `g`: DH 'g' parameter value.
|
||||
/// * `q`: DH 'q' parameter value.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -460,14 +500,22 @@ impl DH {
|
||||
/// 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
|
||||
/// 0x40, 0x52, 0xed, 0x41
|
||||
/// ];
|
||||
/// let dh = DH::new_from_pgq(&p, &g, &q).expect("Error with new_from_pgq()");
|
||||
/// let dh = DH::new_from_pgq(&p, &g, &q, None, None).expect("Error with new_from_pgq()");
|
||||
/// ```
|
||||
pub fn new_from_pgq(p: &[u8], g: &[u8], q: &[u8]) -> Result<Self, i32> {
|
||||
pub fn new_from_pgq(p: &[u8], g: &[u8], q: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let p_size = p.len() as u32;
|
||||
let g_size = g.len() as u32;
|
||||
let q_size = q.len() as u32;
|
||||
let mut wc_dhkey: MaybeUninit<ws::DhKey> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_InitDhKey(wc_dhkey.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitDhKey_ex(wc_dhkey.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -492,6 +540,8 @@ impl DH {
|
||||
/// * `trusted`: Whether to skip the prime check for `p` parameter and mark
|
||||
/// the DH context as trusted.
|
||||
/// * `rng`: `RNG` instance to use for random number generation.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -578,14 +628,22 @@ impl DH {
|
||||
/// 0x40, 0x52, 0xed, 0x41
|
||||
/// ];
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let dh = DH::new_from_pgq_with_check(&p, &g, &q, 0, &mut rng).expect("Error with new_from_pgq_with_check()");
|
||||
/// let dh = DH::new_from_pgq_with_check(&p, &g, &q, 0, &mut rng, None, None).expect("Error with new_from_pgq_with_check()");
|
||||
/// ```
|
||||
pub fn new_from_pgq_with_check(p: &[u8], g: &[u8], q: &[u8], trusted: i32, rng: &mut RNG) -> Result<Self, i32> {
|
||||
pub fn new_from_pgq_with_check(p: &[u8], g: &[u8], q: &[u8], trusted: i32, rng: &mut RNG, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let p_size = p.len() as u32;
|
||||
let g_size = g.len() as u32;
|
||||
let q_size = q.len() as u32;
|
||||
let mut wc_dhkey: MaybeUninit<ws::DhKey> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_InitDhKey(wc_dhkey.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitDhKey_ex(wc_dhkey.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -619,7 +677,7 @@ impl DH {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::dh::DH;
|
||||
/// let mut rng = RNG::new().expect("Error with RNG::new()");
|
||||
/// let mut dh = DH::new_named(DH::FFDHE_2048).expect("Error with new_named()");
|
||||
/// let mut dh = DH::new_named(DH::FFDHE_2048, None, None).expect("Error with new_named()");
|
||||
/// let mut private = [0u8; 256];
|
||||
/// let mut private_size = 0u32;
|
||||
/// let mut public = [0u8; 256];
|
||||
@@ -662,7 +720,7 @@ impl DH {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::dh::DH;
|
||||
/// let mut rng = RNG::new().expect("Error with RNG::new()");
|
||||
/// let mut dh = DH::new_named(DH::FFDHE_2048).expect("Error with new_named()");
|
||||
/// let mut dh = DH::new_named(DH::FFDHE_2048, None, None).expect("Error with new_named()");
|
||||
/// let mut private = [0u8; 256];
|
||||
/// let mut private_size = 0u32;
|
||||
/// let mut public = [0u8; 256];
|
||||
@@ -779,7 +837,7 @@ impl DH {
|
||||
/// 0x40, 0x52, 0xed, 0x41
|
||||
/// ];
|
||||
/// let mut rng = RNG::new().expect("Error with RNG::new()");
|
||||
/// let mut dh = DH::new_from_pgq_with_check(&p, &g, &q, 0, &mut rng).expect("Error with new_from_pgq()");
|
||||
/// let mut dh = DH::new_from_pgq_with_check(&p, &g, &q, 0, &mut rng, None, None).expect("Error with new_from_pgq()");
|
||||
/// let mut private = [0u8; 256];
|
||||
/// let mut private_size = 0u32;
|
||||
/// let mut public = [0u8; 256];
|
||||
@@ -826,7 +884,7 @@ impl DH {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::dh::DH;
|
||||
/// let mut rng = RNG::new().expect("Error with RNG::new()");
|
||||
/// let mut dh = DH::new_named(DH::FFDHE_2048).expect("Error with new_named()");
|
||||
/// let mut dh = DH::new_named(DH::FFDHE_2048, None, None).expect("Error with new_named()");
|
||||
/// let mut private = [0u8; 256];
|
||||
/// let mut private_size = 0u32;
|
||||
/// let mut public = [0u8; 256];
|
||||
@@ -947,7 +1005,7 @@ impl DH {
|
||||
/// 0x40, 0x52, 0xed, 0x41
|
||||
/// ];
|
||||
/// let mut rng = RNG::new().expect("Error with RNG::new()");
|
||||
/// let mut dh = DH::new_from_pgq_with_check(&p, &g, &q, 0, &mut rng).expect("Error with new_from_pgq()");
|
||||
/// let mut dh = DH::new_from_pgq_with_check(&p, &g, &q, 0, &mut rng, None, None).expect("Error with new_from_pgq()");
|
||||
/// let mut private = [0u8; 256];
|
||||
/// let mut private_size = 0u32;
|
||||
/// let mut public = [0u8; 256];
|
||||
@@ -1025,7 +1083,7 @@ impl DH {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::dh::DH;
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let mut dh = DH::new_named(DH::FFDHE_2048).expect("Error with new_named()");
|
||||
/// let mut dh = DH::new_named(DH::FFDHE_2048, None, None).expect("Error with new_named()");
|
||||
/// let mut private = [0u8; 256];
|
||||
/// let mut private_size = 0u32;
|
||||
/// let mut public = [0u8; 256];
|
||||
@@ -1067,7 +1125,7 @@ impl DH {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::dh::DH;
|
||||
/// let mut rng = RNG::new().expect("Error with RNG::new()");
|
||||
/// let mut dh = DH::new_named(DH::FFDHE_2048).expect("Error with new_named()");
|
||||
/// let mut dh = DH::new_named(DH::FFDHE_2048, None, None).expect("Error with new_named()");
|
||||
/// let mut private0 = [0u8; 256];
|
||||
/// let mut private0_size = 0u32;
|
||||
/// let mut public0 = [0u8; 256];
|
||||
|
||||
@@ -37,6 +37,7 @@ use crate::wolfcrypt::random::RNG;
|
||||
/// Rust wrapper for wolfSSL `ecc_point` object.
|
||||
pub struct ECCPoint {
|
||||
wc_ecc_point: *mut ws::ecc_point,
|
||||
heap: *mut std::os::raw::c_void,
|
||||
}
|
||||
|
||||
impl ECCPoint {
|
||||
@@ -46,21 +47,41 @@ impl ECCPoint {
|
||||
///
|
||||
/// * `din`: DER-formatted buffer.
|
||||
/// * `curve_id`: Curve ID, e.g. ECC::SECP256R1.
|
||||
/// * `heap`: Optional heap hint.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(ECCPoint) containing the ECCPoint struct instance or
|
||||
/// Err(e) containing the wolfSSL library error code value.
|
||||
pub fn import_der(din: &[u8], curve_id: i32) -> Result<Self, i32> {
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ecc::{ECC,ECCPoint};
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let curve_id = ECC::SECP256R1;
|
||||
/// let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
|
||||
/// let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id, None, None).expect("Error with generate()");
|
||||
/// let ecc_point = ecc.make_pub_to_point(Some(&mut rng), None).expect("Error with make_pub_to_point()");
|
||||
/// let mut der = [0u8; 128];
|
||||
/// let size = ecc_point.export_der(&mut der, curve_id).expect("Error with export_der()");
|
||||
/// ECCPoint::import_der(&der[0..size], curve_id, None).expect("Error with import_der()");
|
||||
/// ```
|
||||
pub fn import_der(din: &[u8], curve_id: i32, heap: Option<*mut std::os::raw::c_void>) -> Result<Self, i32> {
|
||||
let curve_idx = unsafe { ws::wc_ecc_get_curve_idx(curve_id) };
|
||||
if curve_idx < 0 {
|
||||
return Err(curve_idx);
|
||||
}
|
||||
let wc_ecc_point = unsafe { ws::wc_ecc_new_point() };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let wc_ecc_point = unsafe { ws::wc_ecc_new_point_h(heap) };
|
||||
if wc_ecc_point.is_null() {
|
||||
return Err(ws::wolfCrypt_ErrorCodes_MEMORY_E);
|
||||
}
|
||||
let eccpoint = ECCPoint { wc_ecc_point };
|
||||
let eccpoint = ECCPoint { wc_ecc_point, heap };
|
||||
let din_size = din.len() as u32;
|
||||
let rc = unsafe {
|
||||
ws::wc_ecc_import_point_der(din.as_ptr(), din_size, curve_idx,
|
||||
@@ -80,6 +101,7 @@ impl ECCPoint {
|
||||
/// * `curve_id`: Curve ID, e.g. ECC::SECP256R1.
|
||||
/// * `short_key_size`: if shortKeySize != 0 then key size is always
|
||||
/// (din.len() - 1) / 2.
|
||||
/// * `heap`: Optional heap hint.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -94,22 +116,26 @@ impl ECCPoint {
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let curve_id = ECC::SECP256R1;
|
||||
/// let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
|
||||
/// let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id).expect("Error with generate()");
|
||||
/// let ecc_point = ecc.make_pub_to_point(Some(&mut rng)).expect("Error with make_pub_to_point()");
|
||||
/// let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id, None, None).expect("Error with generate()");
|
||||
/// let ecc_point = ecc.make_pub_to_point(Some(&mut rng), None).expect("Error with make_pub_to_point()");
|
||||
/// let mut der = [0u8; 128];
|
||||
/// let size = ecc_point.export_der_compressed(&mut der, curve_id).expect("Error with export_der_compressed()");
|
||||
/// ECCPoint::import_der_ex(&der[0..size], curve_id, 1).expect("Error with import_der_ex()");
|
||||
/// ECCPoint::import_der_ex(&der[0..size], curve_id, 1, None).expect("Error with import_der_ex()");
|
||||
/// ```
|
||||
pub fn import_der_ex(din: &[u8], curve_id: i32, short_key_size: i32) -> Result<Self, i32> {
|
||||
pub fn import_der_ex(din: &[u8], curve_id: i32, short_key_size: i32, heap: Option<*mut std::os::raw::c_void>) -> Result<Self, i32> {
|
||||
let curve_idx = unsafe { ws::wc_ecc_get_curve_idx(curve_id) };
|
||||
if curve_idx < 0 {
|
||||
return Err(curve_idx);
|
||||
}
|
||||
let wc_ecc_point = unsafe { ws::wc_ecc_new_point() };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let wc_ecc_point = unsafe { ws::wc_ecc_new_point_h(heap) };
|
||||
if wc_ecc_point.is_null() {
|
||||
return Err(ws::wolfCrypt_ErrorCodes_MEMORY_E);
|
||||
}
|
||||
let eccpoint = ECCPoint { wc_ecc_point };
|
||||
let eccpoint = ECCPoint { wc_ecc_point, heap };
|
||||
let din_size = din.len() as u32;
|
||||
let rc = unsafe {
|
||||
ws::wc_ecc_import_point_der_ex(din.as_ptr(), din_size, curve_idx,
|
||||
@@ -141,12 +167,12 @@ impl ECCPoint {
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let curve_id = ECC::SECP256R1;
|
||||
/// let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
|
||||
/// let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id).expect("Error with generate()");
|
||||
/// let ecc_point = ecc.make_pub_to_point(Some(&mut rng)).expect("Error with make_pub_to_point()");
|
||||
/// let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id, None, None).expect("Error with generate()");
|
||||
/// let ecc_point = ecc.make_pub_to_point(Some(&mut rng), None).expect("Error with make_pub_to_point()");
|
||||
/// let mut der = [0u8; 128];
|
||||
/// let size = ecc_point.export_der(&mut der, curve_id).expect("Error with export_der()");
|
||||
/// assert!(size > 0 && size <= der.len());
|
||||
/// ECCPoint::import_der(&der[0..size], curve_id).expect("Error with import_der()");
|
||||
/// ECCPoint::import_der(&der[0..size], curve_id, None).expect("Error with import_der()");
|
||||
/// ```
|
||||
pub fn export_der(&self, dout: &mut [u8], curve_id: i32) -> Result<usize, i32> {
|
||||
let curve_idx = unsafe { ws::wc_ecc_get_curve_idx(curve_id) };
|
||||
@@ -184,11 +210,11 @@ impl ECCPoint {
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let curve_id = ECC::SECP256R1;
|
||||
/// let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
|
||||
/// let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id).expect("Error with generate()");
|
||||
/// let ecc_point = ecc.make_pub_to_point(Some(&mut rng)).expect("Error with make_pub_to_point()");
|
||||
/// let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id, None, None).expect("Error with generate()");
|
||||
/// let ecc_point = ecc.make_pub_to_point(Some(&mut rng), None).expect("Error with make_pub_to_point()");
|
||||
/// let mut der = [0u8; 128];
|
||||
/// let size = ecc_point.export_der_compressed(&mut der, curve_id).expect("Error with export_der_compressed()");
|
||||
/// ECCPoint::import_der_ex(&der[0..size], curve_id, 1).expect("Error with import_der_ex()");
|
||||
/// ECCPoint::import_der_ex(&der[0..size], curve_id, 1, None).expect("Error with import_der_ex()");
|
||||
/// ```
|
||||
pub fn export_der_compressed(&self, dout: &mut [u8], curve_id: i32) -> Result<usize, i32> {
|
||||
let curve_idx = unsafe { ws::wc_ecc_get_curve_idx(curve_id) };
|
||||
@@ -214,8 +240,8 @@ impl ECCPoint {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ecc::ECC;
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
/// let mut ecc_point = ecc.make_pub_to_point(Some(&mut rng)).expect("Error with make_pub_to_point()");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut ecc_point = ecc.make_pub_to_point(Some(&mut rng), None).expect("Error with make_pub_to_point()");
|
||||
/// ecc_point.forcezero();
|
||||
/// ```
|
||||
pub fn forcezero(&mut self) {
|
||||
@@ -226,13 +252,13 @@ impl ECCPoint {
|
||||
impl Drop for ECCPoint {
|
||||
/// Safely free the underlying wolfSSL ecc_point context.
|
||||
///
|
||||
/// This calls the `wc_ecc_del_point()` wolfssl library function.
|
||||
/// This calls the `wc_ecc_del_point_h()` wolfssl library function.
|
||||
///
|
||||
/// The Rust Drop trait guarantees that this method is called when the
|
||||
/// ECCPoint struct instance goes out of scope, automatically cleaning up
|
||||
/// resources and preventing memory leaks.
|
||||
fn drop(&mut self) {
|
||||
unsafe { ws::wc_ecc_del_point(self.wc_ecc_point); }
|
||||
unsafe { ws::wc_ecc_del_point_h(self.wc_ecc_point, self.heap); }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -295,6 +321,8 @@ impl ECC {
|
||||
/// * `size`: Desired key length in bytes.
|
||||
/// * `rng`: Reference to a `RNG` struct to use for random number
|
||||
/// generation while making the key.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -307,12 +335,20 @@ impl ECC {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ecc::ECC;
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
/// ecc.check().expect("Error with check()");
|
||||
/// ```
|
||||
pub fn generate(size: i32, rng: &mut RNG) -> Result<Self, i32> {
|
||||
pub fn generate(size: i32, rng: &mut RNG, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_ecc_init(wc_ecc_key.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -336,6 +372,8 @@ impl ECC {
|
||||
/// * `rng`: Reference to a `RNG` struct to use for random number
|
||||
/// generation while making the key.
|
||||
/// * `curve_id`: Curve ID, e.g. ECC::SECP256R1.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -350,12 +388,20 @@ impl ECC {
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let curve_id = ECC::SECP256R1;
|
||||
/// let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
|
||||
/// let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id).expect("Error with generate_ex()");
|
||||
/// let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id, None, None).expect("Error with generate_ex()");
|
||||
/// ecc.check().expect("Error with check()");
|
||||
/// ```
|
||||
pub fn generate_ex(size: i32, rng: &mut RNG, curve_id: i32) -> Result<Self, i32> {
|
||||
pub fn generate_ex(size: i32, rng: &mut RNG, curve_id: i32, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_ecc_init(wc_ecc_key.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -380,6 +426,8 @@ impl ECC {
|
||||
/// generation while making the key.
|
||||
/// * `curve_id`: Curve ID, e.g. ECC::SECP256R1.
|
||||
/// * `flags`: Flags for making the key.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -394,12 +442,20 @@ impl ECC {
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let curve_id = ECC::SECP256R1;
|
||||
/// let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
|
||||
/// let mut ecc = ECC::generate_ex2(curve_size, &mut rng, curve_id, ECC::FLAG_COFACTOR).expect("Error with generate_ex2()");
|
||||
/// let mut ecc = ECC::generate_ex2(curve_size, &mut rng, curve_id, ECC::FLAG_COFACTOR, None, None).expect("Error with generate_ex2()");
|
||||
/// ecc.check().expect("Error with check()");
|
||||
/// ```
|
||||
pub fn generate_ex2(size: i32, rng: &mut RNG, curve_id: i32, flags: i32) -> Result<Self, i32> {
|
||||
pub fn generate_ex2(size: i32, rng: &mut RNG, curve_id: i32, flags: i32, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_ecc_init(wc_ecc_key.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -434,7 +490,7 @@ impl ECC {
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let curve_id = ECC::SECP256R1;
|
||||
/// let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
|
||||
/// let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id).expect("Error with generate()");
|
||||
/// let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id, None, None).expect("Error with generate()");
|
||||
/// ecc.check().expect("Error with check()");
|
||||
/// ```
|
||||
pub fn get_curve_size_from_id(curve_id: i32) -> Result<i32, i32> {
|
||||
@@ -450,6 +506,8 @@ impl ECC {
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `der`: DER buffer containing the ECC public and private key pair.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -460,20 +518,24 @@ impl ECC {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ecc::{ECC,ECCPoint};
|
||||
/// use wolfssl::wolfcrypt::ecc::ECC;
|
||||
/// use std::fs;
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let curve_id = ECC::SECP256R1;
|
||||
/// let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
|
||||
/// let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id).expect("Error with generate()");
|
||||
/// let ecc_point = ecc.make_pub_to_point(Some(&mut rng)).expect("Error with make_pub_to_point()");
|
||||
/// let mut der = [0u8; 128];
|
||||
/// let size = ecc_point.export_der(&mut der, curve_id).expect("Error with export_der()");
|
||||
/// assert!(size > 0 && size <= der.len());
|
||||
/// ECCPoint::import_der(&der[0..size], curve_id).expect("Error with import_der()");
|
||||
/// let key_path = "../../../certs/ecc-client-key.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut ecc = ECC::import_der(&der, None, None).expect("Error with import_der()");
|
||||
/// ```
|
||||
pub fn import_der(der: &[u8]) -> Result<Self, i32> {
|
||||
pub fn import_der(der: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_ecc_init(wc_ecc_key.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -495,14 +557,44 @@ impl ECC {
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `der`: DER buffer containing the ECC public key.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(ECC) containing the ECC struct instance or Err(e)
|
||||
/// containing the wolfSSL library error code value.
|
||||
pub fn import_public_der(der: &[u8]) -> Result<Self, i32> {
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ecc::ECC;
|
||||
/// use std::fs;
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let key_path = "../../../certs/ecc-client-key.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut ecc = ECC::import_der(&der, None, None).expect("Error with import_der()");
|
||||
/// let hash = [0x42u8; 32];
|
||||
/// let mut signature = [0u8; 128];
|
||||
/// let signature_length = ecc.sign_hash(&hash, &mut signature, &mut rng).expect("Error with sign_hash()");
|
||||
/// assert!(signature_length > 0 && signature_length <= signature.len());
|
||||
/// let signature = &mut signature[0..signature_length];
|
||||
/// let key_path = "../../../certs/ecc-client-keyPub.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut ecc = ECC::import_public_der(&der, None, None).expect("Error with import_public_der()");
|
||||
/// ```
|
||||
pub fn import_public_der(der: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_ecc_init(wc_ecc_key.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -529,6 +621,8 @@ impl ECC {
|
||||
///
|
||||
/// * `priv_buf`: Buffer containing the raw private key.
|
||||
/// * `pub_buf`: Buffer containing the ANSI X9.63 formatted public key.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -541,7 +635,7 @@ impl ECC {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ecc::ECC;
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
/// let hash = [0x42u8; 32];
|
||||
/// let mut signature = [0u8; 128];
|
||||
/// let signature_length = ecc.sign_hash(&hash, &mut signature, &mut rng).expect("Error with sign_hash()");
|
||||
@@ -551,13 +645,21 @@ impl ECC {
|
||||
/// let mut x963 = [0u8; 128];
|
||||
/// let x963_size = ecc.export_x963(&mut x963).expect("Error with export_x963()");
|
||||
/// let x963 = &x963[0..x963_size];
|
||||
/// let mut ecc2 = ECC::import_private_key(&d, x963).expect("Error with import_private_key()");
|
||||
/// let mut ecc2 = ECC::import_private_key(&d, x963, None, None).expect("Error with import_private_key()");
|
||||
/// let valid = ecc2.verify_hash(&signature, &hash).expect("Error with verify_hash()");
|
||||
/// assert_eq!(valid, true);
|
||||
/// ```
|
||||
pub fn import_private_key(priv_buf: &[u8], pub_buf: &[u8]) -> Result<Self, i32> {
|
||||
pub fn import_private_key(priv_buf: &[u8], pub_buf: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_ecc_init(wc_ecc_key.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -586,6 +688,8 @@ impl ECC {
|
||||
/// * `priv_buf`: Buffer containing the raw private key.
|
||||
/// * `pub_buf`: Buffer containing the ANSI X9.63 formatted public key.
|
||||
/// * `curve_id`: Curve ID, e.g. ECC::SECP256R1.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -600,7 +704,7 @@ impl ECC {
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let curve_id = ECC::SECP256R1;
|
||||
/// let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
|
||||
/// let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id).expect("Error with generate_ex()");
|
||||
/// let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id, None, None).expect("Error with generate_ex()");
|
||||
/// let hash = [0x42u8; 32];
|
||||
/// let mut signature = [0u8; 128];
|
||||
/// let signature_length = ecc.sign_hash(&hash, &mut signature, &mut rng).expect("Error with sign_hash()");
|
||||
@@ -610,13 +714,21 @@ impl ECC {
|
||||
/// let mut x963 = [0u8; 128];
|
||||
/// let x963_size = ecc.export_x963(&mut x963).expect("Error with export_x963()");
|
||||
/// let x963 = &x963[0..x963_size];
|
||||
/// let mut ecc2 = ECC::import_private_key_ex(&d, x963, curve_id).expect("Error with import_private_key_ex()");
|
||||
/// let mut ecc2 = ECC::import_private_key_ex(&d, x963, curve_id, None, None).expect("Error with import_private_key_ex()");
|
||||
/// let valid = ecc2.verify_hash(&signature, &hash).expect("Error with verify_hash()");
|
||||
/// assert_eq!(valid, true);
|
||||
/// ```
|
||||
pub fn import_private_key_ex(priv_buf: &[u8], pub_buf: &[u8], curve_id: i32) -> Result<Self, i32> {
|
||||
pub fn import_private_key_ex(priv_buf: &[u8], pub_buf: &[u8], curve_id: i32, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_ecc_init(wc_ecc_key.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -643,6 +755,8 @@ impl ECC {
|
||||
/// * `qy`: Y component of public key as null terminated ASCII hex string.
|
||||
/// * `d`: Private key as null terminated ASCII hex string.
|
||||
/// * `curve_name`: Null terminated ASCII string containing the curve name.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -657,11 +771,19 @@ impl ECC {
|
||||
/// let qx = b"7a4e287890a1a47ad3457e52f2f76a83ce46cbc947616d0cbaa82323818a793d\0";
|
||||
/// let qy = b"eec4084f5b29ebf29c44cce3b3059610922f8b30ea6e8811742ac7238fe87308\0";
|
||||
/// let d = b"8c14b793cb19137e323a6d2e2a870bca2e7a493ec1153b3a95feb8a4873f8d08\0";
|
||||
/// ECC::import_raw(qx, qy, d, b"SECP256R1\0").expect("Error with import_raw()");
|
||||
/// ECC::import_raw(qx, qy, d, b"SECP256R1\0", None, None).expect("Error with import_raw()");
|
||||
/// ```
|
||||
pub fn import_raw(qx: &[u8], qy: &[u8], d: &[u8], curve_name: &[u8]) -> Result<Self, i32> {
|
||||
pub fn import_raw(qx: &[u8], qy: &[u8], d: &[u8], curve_name: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_ecc_init(wc_ecc_key.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -690,6 +812,8 @@ impl ECC {
|
||||
/// * `qy`: Y component of public key as null terminated ASCII hex string.
|
||||
/// * `d`: Private key as null terminated ASCII hex string.
|
||||
/// * `curve_id`: Curve ID, e.g. ECC::SECP256R1.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -704,11 +828,19 @@ impl ECC {
|
||||
/// let qx = b"7a4e287890a1a47ad3457e52f2f76a83ce46cbc947616d0cbaa82323818a793d\0";
|
||||
/// let qy = b"eec4084f5b29ebf29c44cce3b3059610922f8b30ea6e8811742ac7238fe87308\0";
|
||||
/// let d = b"8c14b793cb19137e323a6d2e2a870bca2e7a493ec1153b3a95feb8a4873f8d08\0";
|
||||
/// ECC::import_raw_ex(qx, qy, d, ECC::SECP256R1).expect("Error with import_raw_ex()");
|
||||
/// ECC::import_raw_ex(qx, qy, d, ECC::SECP256R1, None, None).expect("Error with import_raw_ex()");
|
||||
/// ```
|
||||
pub fn import_raw_ex(qx: &[u8], qy: &[u8], d: &[u8], curve_id: i32) -> Result<Self, i32> {
|
||||
pub fn import_raw_ex(qx: &[u8], qy: &[u8], d: &[u8], curve_id: i32, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_ecc_init(wc_ecc_key.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -736,6 +868,8 @@ impl ECC {
|
||||
/// * `qy`: Y component of public key in binary unsigned integer format.
|
||||
/// * `d`: Private key in binary unsigned integer format.
|
||||
/// * `curve_id`: Curve ID, e.g. ECC::SECP256R1.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -750,7 +884,7 @@ impl ECC {
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let curve_id = ECC::SECP256R1;
|
||||
/// let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
|
||||
/// let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id).expect("Error with generate()");
|
||||
/// let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id, None, None).expect("Error with generate()");
|
||||
/// let mut qx = [0u8; 32];
|
||||
/// let mut qx_len = 0u32;
|
||||
/// let mut qy = [0u8; 32];
|
||||
@@ -758,11 +892,19 @@ impl ECC {
|
||||
/// let mut d = [0u8; 32];
|
||||
/// let mut d_len = 0u32;
|
||||
/// ecc.export_ex(&mut qx, &mut qx_len, &mut qy, &mut qy_len, &mut d, &mut d_len, false).expect("Error with export_ex()");
|
||||
/// let mut ecc2 = ECC::import_unsigned(&qx, &qy, &d, curve_id).expect("Error with import_unsigned()");
|
||||
/// let mut ecc2 = ECC::import_unsigned(&qx, &qy, &d, curve_id, None, None).expect("Error with import_unsigned()");
|
||||
/// ```
|
||||
pub fn import_unsigned(qx: &[u8], qy: &[u8], d: &[u8], curve_id: i32) -> Result<Self, i32> {
|
||||
pub fn import_unsigned(qx: &[u8], qy: &[u8], d: &[u8], curve_id: i32, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_ecc_init(wc_ecc_key.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -786,6 +928,8 @@ impl ECC {
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `din`: Buffer containing the ECC key encoded in ANSI X9.63 format.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -798,16 +942,24 @@ impl ECC {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ecc::ECC;
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut x963 = [0u8; 128];
|
||||
/// let x963_size = ecc.export_x963(&mut x963).expect("Error with export_x963()");
|
||||
/// let x963 = &x963[0..x963_size];
|
||||
/// let _ecc2 = ECC::import_x963(x963).expect("Error with import_x963()");
|
||||
/// let _ecc2 = ECC::import_x963(x963, None, None).expect("Error with import_x963()");
|
||||
/// ```
|
||||
pub fn import_x963(din: &[u8]) -> Result<ECC, i32> {
|
||||
pub fn import_x963(din: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<ECC, i32> {
|
||||
let din_size = din.len() as u32;
|
||||
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_ecc_init(wc_ecc_key.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -834,6 +986,8 @@ impl ECC {
|
||||
///
|
||||
/// * `din`: Buffer containing the ECC key encoded in ANSI X9.63 format.
|
||||
/// * `curve_id`: Curve ID, e.g. ECC::SECP256R1.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -848,16 +1002,24 @@ impl ECC {
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let curve_id = ECC::SECP256R1;
|
||||
/// let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
|
||||
/// let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id).expect("Error with generate_ex()");
|
||||
/// let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id, None, None).expect("Error with generate_ex()");
|
||||
/// let mut x963 = [0u8; 128];
|
||||
/// let x963_size = ecc.export_x963(&mut x963).expect("Error with export_x963()");
|
||||
/// let x963 = &x963[0..x963_size];
|
||||
/// let _ecc2 = ECC::import_x963_ex(x963, curve_id).expect("Error with import_x963_ex()");
|
||||
/// let _ecc2 = ECC::import_x963_ex(x963, curve_id, None, None).expect("Error with import_x963_ex()");
|
||||
/// ```
|
||||
pub fn import_x963_ex(din: &[u8], curve_id: i32) -> Result<ECC, i32> {
|
||||
pub fn import_x963_ex(din: &[u8], curve_id: i32, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<ECC, i32> {
|
||||
let din_size = din.len() as u32;
|
||||
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_ecc_init(wc_ecc_key.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_ecc_init_ex(wc_ecc_key.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -906,7 +1068,7 @@ impl ECC {
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let key_path = "../../../certs/ecc-client-key.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut ecc = ECC::import_der(&der).expect("Error with import_der()");
|
||||
/// let mut ecc = ECC::import_der(&der, None, None).expect("Error with import_der()");
|
||||
/// let hash = [0x42u8; 32];
|
||||
/// let mut signature = [0u8; 128];
|
||||
/// let signature_length = ecc.sign_hash(&hash, &mut signature, &mut rng).expect("Error with sign_hash()");
|
||||
@@ -961,7 +1123,7 @@ impl ECC {
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let key_path = "../../../certs/ecc-client-key.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut ecc = ECC::import_der(&der).expect("Error with import_der()");
|
||||
/// let mut ecc = ECC::import_der(&der, None, None).expect("Error with import_der()");
|
||||
/// let hash = [0x42u8; 32];
|
||||
/// let mut signature = [0u8; 128];
|
||||
/// let signature_length = ecc.sign_hash(&hash, &mut signature, &mut rng).expect("Error with sign_hash()");
|
||||
@@ -1010,7 +1172,7 @@ impl ECC {
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let key_path = "../../../certs/ecc-client-key.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut ecc = ECC::import_der(&der).expect("Error with import_der()");
|
||||
/// let mut ecc = ECC::import_der(&der, None, None).expect("Error with import_der()");
|
||||
/// let hash = [0x42u8; 32];
|
||||
/// let mut signature = [0u8; 128];
|
||||
/// let signature_length = ecc.sign_hash(&hash, &mut signature, &mut rng).expect("Error with sign_hash()");
|
||||
@@ -1053,7 +1215,7 @@ impl ECC {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ecc::ECC;
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
/// ecc.check().expect("Error with check()");
|
||||
/// ```
|
||||
pub fn check(&mut self) -> Result<(), i32> {
|
||||
@@ -1086,7 +1248,7 @@ impl ECC {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ecc::ECC;
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut qx = [0u8; 32];
|
||||
/// let mut qx_len = 0u32;
|
||||
/// let mut qy = [0u8; 32];
|
||||
@@ -1137,7 +1299,7 @@ impl ECC {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ecc::ECC;
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut qx = [0u8; 32];
|
||||
/// let mut qx_len = 0u32;
|
||||
/// let mut qy = [0u8; 32];
|
||||
@@ -1188,7 +1350,7 @@ impl ECC {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ecc::ECC;
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut d = [0u8; 32];
|
||||
/// let d_size = ecc.export_private(&mut d).expect("Error with export_private()");
|
||||
/// assert_eq!(d_size, 32);
|
||||
@@ -1225,7 +1387,7 @@ impl ECC {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ecc::ECC;
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut qx = [0u8; 32];
|
||||
/// let mut qx_len = 0u32;
|
||||
/// let mut qy = [0u8; 32];
|
||||
@@ -1264,7 +1426,7 @@ impl ECC {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ecc::ECC;
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut x963 = [0u8; 128];
|
||||
/// let _x963_size = ecc.export_x963(&mut x963).expect("Error with export_x963()");
|
||||
/// ```
|
||||
@@ -1296,7 +1458,7 @@ impl ECC {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ecc::ECC;
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut x963 = [0u8; 128];
|
||||
/// let _x963_size = ecc.export_x963_compressed(&mut x963).expect("Error with export_x963_compressed()");
|
||||
/// ```
|
||||
@@ -1332,7 +1494,7 @@ impl ECC {
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let key_path = "../../../certs/ecc-client-key.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut ecc = ECC::import_der(&der).expect("Error with import_der()");
|
||||
/// let mut ecc = ECC::import_der(&der, None, None).expect("Error with import_der()");
|
||||
/// ecc.make_pub(Some(&mut rng)).expect("Error with make_pub()");
|
||||
/// ```
|
||||
pub fn make_pub(&mut self, rng: Option<&mut RNG>) -> Result<(), i32> {
|
||||
@@ -1355,6 +1517,7 @@ impl ECC {
|
||||
///
|
||||
/// * `rng`: RNG struct used to blind the private key value used in the
|
||||
/// computation.
|
||||
/// * `heap`: Optional heap hint.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -1370,19 +1533,23 @@ impl ECC {
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let key_path = "../../../certs/ecc-client-key.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut ecc = ECC::import_der(&der).expect("Error with import_der()");
|
||||
/// ecc.make_pub_to_point(Some(&mut rng)).expect("Error with make_pub_to_point()");
|
||||
/// let mut ecc = ECC::import_der(&der, None, None).expect("Error with import_der()");
|
||||
/// ecc.make_pub_to_point(Some(&mut rng), None).expect("Error with make_pub_to_point()");
|
||||
/// ```
|
||||
pub fn make_pub_to_point(&mut self, rng: Option<&mut RNG>) -> Result<ECCPoint, i32> {
|
||||
pub fn make_pub_to_point(&mut self, rng: Option<&mut RNG>, heap: Option<*mut std::os::raw::c_void>) -> Result<ECCPoint, i32> {
|
||||
let rng_ptr = match rng {
|
||||
Some(rng) => &mut rng.wc_rng,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let wc_ecc_point = unsafe { ws::wc_ecc_new_point() };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let wc_ecc_point = unsafe { ws::wc_ecc_new_point_h(heap) };
|
||||
if wc_ecc_point.is_null() {
|
||||
return Err(ws::wolfCrypt_ErrorCodes_MEMORY_E);
|
||||
}
|
||||
let ecc_point = ECCPoint { wc_ecc_point };
|
||||
let ecc_point = ECCPoint { wc_ecc_point, heap };
|
||||
let rc = unsafe {
|
||||
ws::wc_ecc_make_pub_ex(&mut self.wc_ecc_key, wc_ecc_point, rng_ptr)
|
||||
};
|
||||
@@ -1414,7 +1581,7 @@ impl ECC {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ecc::ECC;
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
/// ecc.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
/// ```
|
||||
pub fn set_rng(&mut self, rng: &mut RNG) -> Result<(), i32> {
|
||||
@@ -1446,8 +1613,8 @@ impl ECC {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ecc::ECC;
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let mut ecc0 = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
/// let mut ecc1 = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
/// let mut ecc0 = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut ecc1 = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut ss0 = [0u8; 128];
|
||||
/// let mut ss1 = [0u8; 128];
|
||||
/// let ss0_size = ecc0.shared_secret(&mut ecc1, &mut ss0).expect("Error with shared_secret()");
|
||||
@@ -1489,9 +1656,9 @@ impl ECC {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ecc::ECC;
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let mut ecc0 = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
/// let mut ecc1 = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
/// let ecc1_point = ecc1.make_pub_to_point(None).expect("Error with make_pub_to_point()");
|
||||
/// let mut ecc0 = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut ecc1 = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
/// let ecc1_point = ecc1.make_pub_to_point(None, None).expect("Error with make_pub_to_point()");
|
||||
/// let mut ss0 = [0u8; 128];
|
||||
/// let mut ss1 = [0u8; 128];
|
||||
/// let ss0_size = ecc0.shared_secret_ex(&ecc1_point, &mut ss0).expect("Error with shared_secret_ex()");
|
||||
@@ -1532,7 +1699,7 @@ impl ECC {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ecc::ECC;
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
/// let hash = [0x42u8; 32];
|
||||
/// let mut signature = [0u8; 128];
|
||||
/// let signature_length = ecc.sign_hash(&hash, &mut signature, &mut rng).expect("Error with sign_hash()");
|
||||
@@ -1571,7 +1738,7 @@ impl ECC {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ecc::ECC;
|
||||
/// let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
/// let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
/// let hash = [0x42u8; 32];
|
||||
/// let mut signature = [0u8; 128];
|
||||
/// let signature_length = ecc.sign_hash(&hash, &mut signature, &mut rng).expect("Error with sign_hash()");
|
||||
|
||||
@@ -59,6 +59,8 @@ impl Ed25519 {
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `rng`: `RNG` instance to use for random number generation.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -71,11 +73,19 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// ```
|
||||
pub fn generate(rng: &mut RNG) -> Result<Self, i32> {
|
||||
pub fn generate(rng: &mut RNG, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut ws_key: MaybeUninit<ws::ed25519_key> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_ed25519_init(ws_key.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_ed25519_init_ex(ws_key.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -96,6 +106,11 @@ impl Ed25519 {
|
||||
/// A key will not be present but can be imported with one of the import
|
||||
/// functions.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(ed25519) containing the Ed25519 struct instance or
|
||||
@@ -105,11 +120,19 @@ impl Ed25519 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let ed = Ed25519::new().expect("Error with new()");
|
||||
/// let ed = Ed25519::new(None, None).expect("Error with new()");
|
||||
/// ```
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut ws_key: MaybeUninit<ws::ed25519_key> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_ed25519_init(ws_key.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_ed25519_init_ex(ws_key.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -135,7 +158,7 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// ed.check_key().expect("Error with check_key()");
|
||||
/// ```
|
||||
pub fn check_key(&mut self) -> Result<(), i32> {
|
||||
@@ -166,7 +189,7 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut private = [0u8; Ed25519::PRV_KEY_SIZE];
|
||||
/// let mut public = [0u8; Ed25519::PUB_KEY_SIZE];
|
||||
/// ed.export_key(&mut private, &mut public).expect("Error with export_key()");
|
||||
@@ -204,7 +227,7 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut public = [0u8; Ed25519::PUB_KEY_SIZE];
|
||||
/// ed.export_public(&mut public).expect("Error with export_public()");
|
||||
/// ```
|
||||
@@ -239,7 +262,7 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut private = [0u8; Ed25519::PRV_KEY_SIZE];
|
||||
/// ed.export_private(&mut private).expect("Error with export_private()");
|
||||
/// ```
|
||||
@@ -274,7 +297,7 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut private_only = [0u8; Ed25519::KEY_SIZE];
|
||||
/// ed.export_private_only(&mut private_only).expect("Error with export_private_only()");
|
||||
/// ```
|
||||
@@ -311,11 +334,11 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut private = [0u8; Ed25519::PRV_KEY_SIZE];
|
||||
/// let mut public = [0u8; Ed25519::PUB_KEY_SIZE];
|
||||
/// ed.export_key(&mut private, &mut public).expect("Error with export_key()");
|
||||
/// let mut ed = Ed25519::new().expect("Error with new()");
|
||||
/// let mut ed = Ed25519::new(None, None).expect("Error with new()");
|
||||
/// ed.import_public(&public).expect("Error with import_public()");
|
||||
/// ```
|
||||
pub fn import_public(&mut self, public: &[u8]) -> Result<(), i32> {
|
||||
@@ -351,11 +374,11 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut private = [0u8; Ed25519::PRV_KEY_SIZE];
|
||||
/// let mut public = [0u8; Ed25519::PUB_KEY_SIZE];
|
||||
/// ed.export_key(&mut private, &mut public).expect("Error with export_key()");
|
||||
/// let mut ed = Ed25519::new().expect("Error with new()");
|
||||
/// let mut ed = Ed25519::new(None, None).expect("Error with new()");
|
||||
/// ed.import_public_ex(&public, false).expect("Error with import_public_ex()");
|
||||
/// ```
|
||||
pub fn import_public_ex(&mut self, public: &[u8], trusted: bool) -> Result<(), i32> {
|
||||
@@ -387,10 +410,10 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut private_only = [0u8; Ed25519::KEY_SIZE];
|
||||
/// ed.export_private_only(&mut private_only).expect("Error with export_private_only()");
|
||||
/// let mut ed = Ed25519::new().expect("Error with new()");
|
||||
/// let mut ed = Ed25519::new(None, None).expect("Error with new()");
|
||||
/// ed.import_private_only(&private_only).expect("Error with import_private_only()");
|
||||
/// ```
|
||||
pub fn import_private_only(&mut self, private: &[u8]) -> Result<(), i32> {
|
||||
@@ -427,11 +450,11 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut private = [0u8; Ed25519::PRV_KEY_SIZE];
|
||||
/// let mut public = [0u8; Ed25519::PUB_KEY_SIZE];
|
||||
/// ed.export_key(&mut private, &mut public).expect("Error with export_key()");
|
||||
/// let mut ed = Ed25519::new().expect("Error with new()");
|
||||
/// let mut ed = Ed25519::new(None, None).expect("Error with new()");
|
||||
/// ed.import_private_key(&private, Some(&public)).expect("Error with import_private_key()");
|
||||
/// ```
|
||||
pub fn import_private_key(&mut self, private: &[u8], public: Option<&[u8]>) -> Result<(), i32> {
|
||||
@@ -474,11 +497,11 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut private = [0u8; Ed25519::PRV_KEY_SIZE];
|
||||
/// let mut public = [0u8; Ed25519::PUB_KEY_SIZE];
|
||||
/// ed.export_key(&mut private, &mut public).expect("Error with export_key()");
|
||||
/// let mut ed = Ed25519::new().expect("Error with new()");
|
||||
/// let mut ed = Ed25519::new(None, None).expect("Error with new()");
|
||||
/// ed.import_private_key_ex(&private, Some(&public), false).expect("Error with import_private_key_ex()");
|
||||
/// ```
|
||||
pub fn import_private_key_ex(&mut self, private: &[u8], public: Option<&[u8]>, trusted: bool) -> Result<(), i32> {
|
||||
@@ -519,10 +542,10 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut private = [0u8; Ed25519::KEY_SIZE];
|
||||
/// ed.export_private_only(&mut private).expect("Error with export_private_only()");
|
||||
/// let mut ed = Ed25519::new().expect("Error with new()");
|
||||
/// let mut ed = Ed25519::new(None, None).expect("Error with new()");
|
||||
/// ed.import_private_only(&private).expect("Error with import_private_only()");
|
||||
/// let mut public = [0u8; Ed25519::KEY_SIZE];
|
||||
/// ed.make_public(&mut public).expect("Error with make_public()");
|
||||
@@ -558,7 +581,7 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let message = [0x42u8, 33, 55, 66];
|
||||
/// let mut signature = [0u8; Ed25519::SIG_SIZE];
|
||||
/// ed.sign_msg(&message, &mut signature).expect("Error with sign_msg()");
|
||||
@@ -598,7 +621,7 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let message = [0x42u8, 33, 55, 66];
|
||||
/// let context = b"context";
|
||||
/// let mut signature = [0u8; Ed25519::SIG_SIZE];
|
||||
@@ -642,7 +665,7 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let hash = [
|
||||
/// 0xddu8,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
|
||||
/// 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
|
||||
@@ -700,7 +723,7 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let message = [0x42u8, 33, 55, 66];
|
||||
/// let context = b"context";
|
||||
/// let mut signature = [0u8; Ed25519::SIG_SIZE];
|
||||
@@ -749,7 +772,7 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let message = [0x42u8, 33, 55, 66];
|
||||
/// let context = b"context";
|
||||
/// let mut signature = [0u8; Ed25519::SIG_SIZE];
|
||||
@@ -793,7 +816,7 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let message = [0x42u8, 33, 55, 66];
|
||||
/// let mut signature = [0u8; Ed25519::SIG_SIZE];
|
||||
/// ed.sign_msg(&message, &mut signature).expect("Error with sign_msg()");
|
||||
@@ -835,7 +858,7 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let message = b"Hello!";
|
||||
/// let context = b"context";
|
||||
/// let mut signature = [0u8; Ed25519::SIG_SIZE];
|
||||
@@ -882,7 +905,7 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let hash = [
|
||||
/// 0xddu8,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
|
||||
/// 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
|
||||
@@ -942,7 +965,7 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let message = [0x42u8, 33, 55, 66];
|
||||
/// let context = b"context";
|
||||
/// let mut signature = [0u8; Ed25519::SIG_SIZE];
|
||||
@@ -993,7 +1016,7 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let message = [0x42u8, 33, 55, 66];
|
||||
/// let context = b"context";
|
||||
/// let mut signature = [0u8; Ed25519::SIG_SIZE];
|
||||
@@ -1041,7 +1064,7 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let message = [0x42u8, 33, 55, 66];
|
||||
/// let mut signature = [0u8; Ed25519::SIG_SIZE];
|
||||
/// ed.sign_msg(&message, &mut signature).expect("Error with sign_msg()");
|
||||
@@ -1086,7 +1109,7 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let message = [0x42u8, 33, 55, 66];
|
||||
/// let mut signature = [0u8; Ed25519::SIG_SIZE];
|
||||
/// ed.sign_msg(&message, &mut signature).expect("Error with sign_msg()");
|
||||
@@ -1125,7 +1148,7 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let message = [0x42u8, 33, 55, 66];
|
||||
/// let mut signature = [0u8; Ed25519::SIG_SIZE];
|
||||
/// ed.sign_msg(&message, &mut signature).expect("Error with sign_msg()");
|
||||
@@ -1161,7 +1184,7 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let key_size = ed.size().expect("Error with size()");
|
||||
/// assert_eq!(key_size, Ed25519::KEY_SIZE);
|
||||
/// ```
|
||||
@@ -1186,7 +1209,7 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let priv_size = ed.priv_size().expect("Error with priv_size()");
|
||||
/// assert_eq!(priv_size, Ed25519::PRV_KEY_SIZE);
|
||||
/// ```
|
||||
@@ -1211,7 +1234,7 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let pub_size = ed.pub_size().expect("Error with pub_size()");
|
||||
/// assert_eq!(pub_size, Ed25519::PUB_KEY_SIZE);
|
||||
/// ```
|
||||
@@ -1236,7 +1259,7 @@ impl Ed25519 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed25519::Ed25519;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
/// let ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let sig_size = ed.sig_size().expect("Error with sig_size()");
|
||||
/// assert_eq!(sig_size, Ed25519::SIG_SIZE);
|
||||
/// ```
|
||||
|
||||
@@ -58,6 +58,8 @@ impl Ed448 {
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `rng`: `RNG` instance to use for random number generation.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -70,11 +72,19 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// ```
|
||||
pub fn generate(rng: &mut RNG) -> Result<Self, i32> {
|
||||
pub fn generate(rng: &mut RNG, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut ws_key: MaybeUninit<ws::ed448_key> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_ed448_init(ws_key.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_ed448_init_ex(ws_key.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -95,6 +105,11 @@ impl Ed448 {
|
||||
/// A key will not be present but can be imported with one of the import
|
||||
/// functions.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(ed448) containing the Ed448 struct instance or
|
||||
@@ -104,11 +119,19 @@ impl Ed448 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let ed = Ed448::new().expect("Error with new()");
|
||||
/// let ed = Ed448::new(None, None).expect("Error with new()");
|
||||
/// ```
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut ws_key: MaybeUninit<ws::ed448_key> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_ed448_init(ws_key.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_ed448_init_ex(ws_key.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -134,7 +157,7 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// ed.check_key().expect("Error with check_key()");
|
||||
/// ```
|
||||
pub fn check_key(&mut self) -> Result<(), i32> {
|
||||
@@ -165,7 +188,7 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut private = [0u8; Ed448::PRV_KEY_SIZE];
|
||||
/// let mut public = [0u8; Ed448::PUB_KEY_SIZE];
|
||||
/// ed.export_key(&mut private, &mut public).expect("Error with export_key()");
|
||||
@@ -202,7 +225,7 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut public = [0u8; Ed448::PUB_KEY_SIZE];
|
||||
/// ed.export_public(&mut public).expect("Error with export_public()");
|
||||
/// ```
|
||||
@@ -236,7 +259,7 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut private = [0u8; Ed448::PRV_KEY_SIZE];
|
||||
/// ed.export_private(&mut private).expect("Error with export_private()");
|
||||
/// ```
|
||||
@@ -270,7 +293,7 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut private_only = [0u8; Ed448::KEY_SIZE];
|
||||
/// ed.export_private_only(&mut private_only).expect("Error with export_private_only()");
|
||||
/// ```
|
||||
@@ -307,11 +330,11 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut private = [0u8; Ed448::PRV_KEY_SIZE];
|
||||
/// let mut public = [0u8; Ed448::PUB_KEY_SIZE];
|
||||
/// ed.export_key(&mut private, &mut public).expect("Error with export_key()");
|
||||
/// let mut ed = Ed448::new().expect("Error with new()");
|
||||
/// let mut ed = Ed448::new(None, None).expect("Error with new()");
|
||||
/// ed.import_public(&public).expect("Error with import_public()");
|
||||
/// ```
|
||||
pub fn import_public(&mut self, public: &[u8]) -> Result<(), i32> {
|
||||
@@ -347,11 +370,11 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut private = [0u8; Ed448::PRV_KEY_SIZE];
|
||||
/// let mut public = [0u8; Ed448::PUB_KEY_SIZE];
|
||||
/// ed.export_key(&mut private, &mut public).expect("Error with export_key()");
|
||||
/// let mut ed = Ed448::new().expect("Error with new()");
|
||||
/// let mut ed = Ed448::new(None, None).expect("Error with new()");
|
||||
/// ed.import_public_ex(&public, false).expect("Error with import_public_ex()");
|
||||
/// ```
|
||||
pub fn import_public_ex(&mut self, public: &[u8], trusted: bool) -> Result<(), i32> {
|
||||
@@ -383,10 +406,10 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut private_only = [0u8; Ed448::KEY_SIZE];
|
||||
/// ed.export_private_only(&mut private_only).expect("Error with export_private_only()");
|
||||
/// let mut ed = Ed448::new().expect("Error with new()");
|
||||
/// let mut ed = Ed448::new(None, None).expect("Error with new()");
|
||||
/// ed.import_private_only(&private_only).expect("Error with import_private_only()");
|
||||
/// ```
|
||||
pub fn import_private_only(&mut self, private: &[u8]) -> Result<(), i32> {
|
||||
@@ -423,11 +446,11 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut private = [0u8; Ed448::PRV_KEY_SIZE];
|
||||
/// let mut public = [0u8; Ed448::PUB_KEY_SIZE];
|
||||
/// ed.export_key(&mut private, &mut public).expect("Error with export_key()");
|
||||
/// let mut ed = Ed448::new().expect("Error with new()");
|
||||
/// let mut ed = Ed448::new(None, None).expect("Error with new()");
|
||||
/// ed.import_private_key(&private, Some(&public)).expect("Error with import_private_key()");
|
||||
/// ```
|
||||
pub fn import_private_key(&mut self, private: &[u8], public: Option<&[u8]>) -> Result<(), i32> {
|
||||
@@ -470,11 +493,11 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut private = [0u8; Ed448::PRV_KEY_SIZE];
|
||||
/// let mut public = [0u8; Ed448::PUB_KEY_SIZE];
|
||||
/// ed.export_key(&mut private, &mut public).expect("Error with export_key()");
|
||||
/// let mut ed = Ed448::new().expect("Error with new()");
|
||||
/// let mut ed = Ed448::new(None, None).expect("Error with new()");
|
||||
/// ed.import_private_key_ex(&private, Some(&public), false).expect("Error with import_private_key_ex()");
|
||||
/// ```
|
||||
pub fn import_private_key_ex(&mut self, private: &[u8], public: Option<&[u8]>, trusted: bool) -> Result<(), i32> {
|
||||
@@ -515,10 +538,10 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut private = [0u8; Ed448::KEY_SIZE];
|
||||
/// ed.export_private_only(&mut private).expect("Error with export_private_only()");
|
||||
/// let mut ed = Ed448::new().expect("Error with new()");
|
||||
/// let mut ed = Ed448::new(None, None).expect("Error with new()");
|
||||
/// ed.import_private_only(&private).expect("Error with import_private_only()");
|
||||
/// let mut public = [0u8; Ed448::KEY_SIZE];
|
||||
/// ed.make_public(&mut public).expect("Error with make_public()");
|
||||
@@ -557,7 +580,7 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let message = [0x42u8, 33, 55, 66];
|
||||
/// let context = b"context";
|
||||
/// let mut signature = [0u8; Ed448::SIG_SIZE];
|
||||
@@ -606,7 +629,7 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let hash = [
|
||||
/// 0xddu8,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
|
||||
/// 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
|
||||
@@ -664,7 +687,7 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let message = [0x42u8, 33, 55, 66];
|
||||
/// let context = b"context";
|
||||
/// let mut signature = [0u8; Ed448::SIG_SIZE];
|
||||
@@ -713,7 +736,7 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let message = [0x42u8, 33, 55, 66];
|
||||
/// let context = b"context";
|
||||
/// let mut signature = [0u8; Ed448::SIG_SIZE];
|
||||
@@ -760,7 +783,7 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let message = b"Hello!";
|
||||
/// let context = b"context";
|
||||
/// let mut signature = [0u8; Ed448::SIG_SIZE];
|
||||
@@ -812,7 +835,7 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let hash = [
|
||||
/// 0xddu8,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
|
||||
/// 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
|
||||
@@ -872,7 +895,7 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let message = [0x42u8, 33, 55, 66];
|
||||
/// let context = b"context";
|
||||
/// let mut signature = [0u8; Ed448::SIG_SIZE];
|
||||
@@ -923,7 +946,7 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let message = [0x42u8, 33, 55, 66];
|
||||
/// let context = b"context";
|
||||
/// let mut signature = [0u8; Ed448::SIG_SIZE];
|
||||
@@ -971,7 +994,7 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let message = [0x42u8, 33, 55, 66];
|
||||
/// let context = [0x42u8, 1, 2, 3];
|
||||
/// let mut signature = [0u8; Ed448::SIG_SIZE];
|
||||
@@ -1017,7 +1040,7 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let message = [0x42u8, 33, 55, 66];
|
||||
/// let context = [0x42u8, 1, 2, 3];
|
||||
/// let mut signature = [0u8; Ed448::SIG_SIZE];
|
||||
@@ -1057,7 +1080,7 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let mut ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let message = [0x42u8, 33, 55, 66];
|
||||
/// let context = [0x42u8, 1, 2, 3];
|
||||
/// let mut signature = [0u8; Ed448::SIG_SIZE];
|
||||
@@ -1094,7 +1117,7 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let key_size = ed.size().expect("Error with size()");
|
||||
/// assert_eq!(key_size, Ed448::KEY_SIZE);
|
||||
/// ```
|
||||
@@ -1119,7 +1142,7 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let priv_size = ed.priv_size().expect("Error with priv_size()");
|
||||
/// assert_eq!(priv_size, Ed448::PRV_KEY_SIZE);
|
||||
/// ```
|
||||
@@ -1144,7 +1167,7 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let pub_size = ed.pub_size().expect("Error with pub_size()");
|
||||
/// assert_eq!(pub_size, Ed448::PUB_KEY_SIZE);
|
||||
/// ```
|
||||
@@ -1169,7 +1192,7 @@ impl Ed448 {
|
||||
/// use wolfssl::wolfcrypt::random::RNG;
|
||||
/// use wolfssl::wolfcrypt::ed448::Ed448;
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
/// let ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
/// let sig_size = ed.sig_size().expect("Error with sig_size()");
|
||||
/// assert_eq!(sig_size, Ed448::SIG_SIZE);
|
||||
/// ```
|
||||
|
||||
@@ -59,6 +59,41 @@ use wolfssl_sys as ws;
|
||||
/// hkdf_extract(HMAC::TYPE_SHA256, Some(salt), ikm, &mut extract_out).expect("Error with hkdf_extract()");
|
||||
/// ```
|
||||
pub fn hkdf_extract(typ: i32, salt: Option<&[u8]>, key: &[u8], out: &mut [u8]) -> Result<(), i32> {
|
||||
hkdf_extract_ex(typ, salt, key, out, None, None)
|
||||
}
|
||||
|
||||
/// Perform HKDF-Extract operation (with optional heap and device ID).
|
||||
///
|
||||
/// This utilizes HMAC to convert `key`, with an optional `salt`, into a
|
||||
/// derived key which is written to `out`.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `typ`: Hash type, one of `HMAC::TYPE_*`.
|
||||
/// * `salt`: Salt value (optional).
|
||||
/// * `key`: Initial Key Material (IKM).
|
||||
/// * `out`: Output buffer to store HKDF-Extract result. The size of this
|
||||
/// buffer must match `HMAC::get_hmac_size_by_type(typ)`.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
|
||||
/// library error code value.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::hkdf::*;
|
||||
/// use wolfssl::wolfcrypt::hmac::HMAC;
|
||||
/// use wolfssl::wolfcrypt::sha::SHA256;
|
||||
/// let ikm = b"MyPassword0";
|
||||
/// let salt = b"12345678ABCDEFGH";
|
||||
/// let mut extract_out = [0u8; SHA256::DIGEST_SIZE];
|
||||
/// hkdf_extract_ex(HMAC::TYPE_SHA256, Some(salt), ikm, &mut extract_out, None, None).expect("Error with hkdf_extract_ex()");
|
||||
/// ```
|
||||
pub fn hkdf_extract_ex(typ: i32, salt: Option<&[u8]>, key: &[u8], out: &mut [u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<(), i32> {
|
||||
let mut salt_ptr = core::ptr::null();
|
||||
let mut salt_size = 0u32;
|
||||
if let Some(salt) = salt {
|
||||
@@ -69,9 +104,17 @@ pub fn hkdf_extract(typ: i32, salt: Option<&[u8]>, key: &[u8], out: &mut [u8]) -
|
||||
if out.len() != HMAC::get_hmac_size_by_type(typ)? {
|
||||
return Err(ws::wolfCrypt_ErrorCodes_BUFFER_E);
|
||||
}
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe {
|
||||
ws::wc_HKDF_Extract(typ, salt_ptr, salt_size,
|
||||
key.as_ptr(), key_size, out.as_mut_ptr())
|
||||
ws::wc_HKDF_Extract_ex(typ, salt_ptr, salt_size,
|
||||
key.as_ptr(), key_size, out.as_mut_ptr(), heap, dev_id)
|
||||
};
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
@@ -112,6 +155,44 @@ pub fn hkdf_extract(typ: i32, salt: Option<&[u8]>, key: &[u8], out: &mut [u8]) -
|
||||
/// hkdf_expand(HMAC::TYPE_SHA256, &extract_out, Some(info), &mut expand_out).expect("Error with hkdf_expand()");
|
||||
/// ```
|
||||
pub fn hkdf_expand(typ: i32, key: &[u8], info: Option<&[u8]>, out: &mut [u8]) -> Result<(), i32> {
|
||||
hkdf_expand_ex(typ, key, info, out, None, None)
|
||||
}
|
||||
|
||||
/// Perform HKDF-Expand operation (with optional heap and device ID).
|
||||
///
|
||||
/// This utilizes HMAC to convert `key`, with optional `info`, into a
|
||||
/// derived key which is written to `out`.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `typ`: Hash type, one of `HMAC::TYPE_*`.
|
||||
/// * `key`: Key to use for KDF (typically output of `hkdf_extract()`).
|
||||
/// * `info`: Optional buffer containing additional info.
|
||||
/// * `out`: Output buffer to store HKDF-Expand result. The buffer can be
|
||||
/// any size.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
|
||||
/// library error code value.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::hkdf::*;
|
||||
/// use wolfssl::wolfcrypt::hmac::HMAC;
|
||||
/// use wolfssl::wolfcrypt::sha::SHA256;
|
||||
/// let ikm = b"MyPassword0";
|
||||
/// let salt = b"12345678ABCDEFGH";
|
||||
/// let mut extract_out = [0u8; SHA256::DIGEST_SIZE];
|
||||
/// hkdf_extract(HMAC::TYPE_SHA256, Some(salt), ikm, &mut extract_out).expect("Error with hkdf_extract()");
|
||||
/// let info = b"0";
|
||||
/// let mut expand_out = [0u8; 16];
|
||||
/// hkdf_expand_ex(HMAC::TYPE_SHA256, &extract_out, Some(info), &mut expand_out, None, None).expect("Error with hkdf_expand_ex()");
|
||||
/// ```
|
||||
pub fn hkdf_expand_ex(typ: i32, key: &[u8], info: Option<&[u8]>, out: &mut [u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<(), i32> {
|
||||
let key_size = key.len() as u32;
|
||||
let mut info_ptr = core::ptr::null();
|
||||
let mut info_size = 0u32;
|
||||
@@ -120,9 +201,17 @@ pub fn hkdf_expand(typ: i32, key: &[u8], info: Option<&[u8]>, out: &mut [u8]) ->
|
||||
info_size = info.len() as u32;
|
||||
}
|
||||
let out_size = out.len() as u32;
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe {
|
||||
ws::wc_HKDF_Expand(typ, key.as_ptr(), key_size,
|
||||
info_ptr, info_size, out.as_mut_ptr(), out_size)
|
||||
ws::wc_HKDF_Expand_ex(typ, key.as_ptr(), key_size,
|
||||
info_ptr, info_size, out.as_mut_ptr(), out_size, heap, dev_id)
|
||||
};
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
|
||||
@@ -73,6 +73,8 @@ impl HMAC {
|
||||
///
|
||||
/// * `typ`: Hash type, one of `HMAC::TYPE_*`.
|
||||
/// * `key`: Encryption key.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -84,13 +86,21 @@ impl HMAC {
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::hmac::HMAC;
|
||||
/// let key = [0x42u8; 16];
|
||||
/// let mut hmac = HMAC::new(HMAC::TYPE_SHA256, &key).expect("Error with new()");
|
||||
/// let mut hmac = HMAC::new(HMAC::TYPE_SHA256, &key, None, None).expect("Error with new()");
|
||||
/// ```
|
||||
pub fn new(typ: i32, key: &[u8]) -> Result<Self, i32> {
|
||||
pub fn new(typ: i32, key: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let key_size = key.len() as u32;
|
||||
let mut wc_hmac: MaybeUninit<ws::Hmac> = MaybeUninit::uninit();
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe {
|
||||
ws::wc_HmacInit(wc_hmac.as_mut_ptr(), core::ptr::null_mut(), ws::INVALID_DEVID)
|
||||
ws::wc_HmacInit(wc_hmac.as_mut_ptr(), heap, dev_id)
|
||||
};
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
@@ -113,6 +123,8 @@ impl HMAC {
|
||||
///
|
||||
/// * `typ`: Hash type, one of `HMAC::TYPE_*`.
|
||||
/// * `key`: Encryption key.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -124,13 +136,21 @@ impl HMAC {
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::hmac::HMAC;
|
||||
/// let key = [0x42u8; 3];
|
||||
/// let mut hmac = HMAC::new_allow_short_key(HMAC::TYPE_SHA256, &key).expect("Error with new_allow_short_key()");
|
||||
/// let mut hmac = HMAC::new_allow_short_key(HMAC::TYPE_SHA256, &key, None, None).expect("Error with new_allow_short_key()");
|
||||
/// ```
|
||||
pub fn new_allow_short_key(typ: i32, key: &[u8]) -> Result<Self, i32> {
|
||||
pub fn new_allow_short_key(typ: i32, key: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let key_size = key.len() as u32;
|
||||
let mut wc_hmac: MaybeUninit<ws::Hmac> = MaybeUninit::uninit();
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe {
|
||||
ws::wc_HmacInit(wc_hmac.as_mut_ptr(), core::ptr::null_mut(), ws::INVALID_DEVID)
|
||||
ws::wc_HmacInit(wc_hmac.as_mut_ptr(), heap, dev_id)
|
||||
};
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
@@ -165,7 +185,7 @@ impl HMAC {
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::hmac::HMAC;
|
||||
/// let key = [0x42u8; 16];
|
||||
/// let mut hmac = HMAC::new(HMAC::TYPE_SHA256, &key).expect("Error with new()");
|
||||
/// let mut hmac = HMAC::new(HMAC::TYPE_SHA256, &key, None, None).expect("Error with new()");
|
||||
/// hmac.update(b"input").expect("Error with update()");
|
||||
/// ```
|
||||
pub fn update(&mut self, data: &[u8]) -> Result<(), i32> {
|
||||
@@ -196,7 +216,7 @@ impl HMAC {
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::hmac::HMAC;
|
||||
/// let key = [0x42u8; 16];
|
||||
/// let mut hmac = HMAC::new(HMAC::TYPE_SHA256, &key).expect("Error with new()");
|
||||
/// let mut hmac = HMAC::new(HMAC::TYPE_SHA256, &key, None, None).expect("Error with new()");
|
||||
/// hmac.update(b"input").expect("Error with update()");
|
||||
/// let hash_size = hmac.get_hmac_size().expect("Error with get_hmac_size()");
|
||||
/// let mut hash = vec![0u8; hash_size];
|
||||
@@ -235,7 +255,7 @@ impl HMAC {
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::hmac::HMAC;
|
||||
/// let key = [0x42u8; 16];
|
||||
/// let mut hmac = HMAC::new(HMAC::TYPE_SHA256, &key).expect("Error with new()");
|
||||
/// let mut hmac = HMAC::new(HMAC::TYPE_SHA256, &key, None, None).expect("Error with new()");
|
||||
/// hmac.update(b"input").expect("Error with update()");
|
||||
/// let hash_size = hmac.get_hmac_size().expect("Error with get_hmac_size()");
|
||||
/// let mut hash = vec![0u8; hash_size];
|
||||
|
||||
@@ -72,17 +72,7 @@ pub const SRTP_LABEL_HDR_SALT: u8 = ws::WC_SRTP_LABEL_HDR_SALT as u8;
|
||||
/// assert_eq!(keyout, expected_key);
|
||||
/// ```
|
||||
pub fn pbkdf2(password: &[u8], salt: &[u8], iterations: i32, typ: i32, out: &mut [u8]) -> Result<(), i32> {
|
||||
let password_size = password.len() as i32;
|
||||
let salt_size = salt.len() as i32;
|
||||
let out_size = out.len() as i32;
|
||||
let rc = unsafe {
|
||||
ws::wc_PBKDF2(out.as_mut_ptr(), password.as_ptr(), password_size,
|
||||
salt.as_ptr(), salt_size, iterations, out_size, typ)
|
||||
};
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
Ok(())
|
||||
pbkdf2_ex(password, salt, iterations, typ, None, None, out)
|
||||
}
|
||||
|
||||
/// Implement Password Based Key Derivation Function 2 (PBKDF2) converting an
|
||||
@@ -187,17 +177,7 @@ pub fn pbkdf2_ex(password: &[u8], salt: &[u8], iterations: i32, typ: i32, heap:
|
||||
/// assert_eq!(keyout, expected_key);
|
||||
/// ```
|
||||
pub fn pkcs12_pbkdf(password: &[u8], salt: &[u8], iterations: i32, typ: i32, id: i32, out: &mut [u8]) -> Result<(), i32> {
|
||||
let password_size = password.len() as i32;
|
||||
let salt_size = salt.len() as i32;
|
||||
let out_size = out.len() as i32;
|
||||
let rc = unsafe {
|
||||
ws::wc_PKCS12_PBKDF(out.as_mut_ptr(), password.as_ptr(), password_size,
|
||||
salt.as_ptr(), salt_size, iterations, out_size, typ, id)
|
||||
};
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
Ok(())
|
||||
pkcs12_pbkdf_ex(password, salt, iterations, typ, id, None, out)
|
||||
}
|
||||
|
||||
/// This function implements the Password Based Key Derivation Function
|
||||
@@ -289,6 +269,38 @@ pub fn pkcs12_pbkdf_ex(password: &[u8], salt: &[u8], iterations: i32, typ: i32,
|
||||
/// tls13_hkdf_extract(HMAC::TYPE_SHA256, None, None, &mut secret).expect("Error with tls13_hkdf_extract()");
|
||||
/// ```
|
||||
pub fn tls13_hkdf_extract(typ: i32, salt: Option<&[u8]>, key: Option<&mut [u8]>, out: &mut [u8]) -> Result<(), i32> {
|
||||
tls13_hkdf_extract_ex(typ, salt, key, out, None, None)
|
||||
}
|
||||
|
||||
/// Perform RFC 5869 HKDF-Extract operation for TLS v1.3 key derivation (with
|
||||
/// optional heap and device ID).
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `typ`: Hash type, one of `HMAC::TYPE_*`.
|
||||
/// * `salt`: Optional Salt value.
|
||||
/// * `key`: Optional Initial Key Material (IKM).
|
||||
/// * `out`: Output buffer to store TLS1.3 HKDF-Extract result (generated
|
||||
/// Pseudo-Random Key (PRK)). The size of this buffer must match
|
||||
/// `HMAC::get_hmac_size_by_type(typ)`.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
|
||||
/// library error code value.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::hmac::HMAC;
|
||||
/// use wolfssl::wolfcrypt::kdf::*;
|
||||
/// use wolfssl::wolfcrypt::sha::SHA256;
|
||||
/// let mut secret = [0u8; SHA256::DIGEST_SIZE];
|
||||
/// tls13_hkdf_extract_ex(HMAC::TYPE_SHA256, None, None, &mut secret, None, None).expect("Error with tls13_hkdf_extract_ex()");
|
||||
/// ```
|
||||
pub fn tls13_hkdf_extract_ex(typ: i32, salt: Option<&[u8]>, key: Option<&mut [u8]>, out: &mut [u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<(), i32> {
|
||||
let mut salt_ptr = core::ptr::null();
|
||||
let mut salt_size = 0u32;
|
||||
if let Some(salt) = salt {
|
||||
@@ -307,9 +319,17 @@ pub fn tls13_hkdf_extract(typ: i32, salt: Option<&[u8]>, key: Option<&mut [u8]>,
|
||||
if out.len() != HMAC::get_hmac_size_by_type(typ)? {
|
||||
return Err(ws::wolfCrypt_ErrorCodes_BUFFER_E);
|
||||
}
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe {
|
||||
ws::wc_Tls13_HKDF_Extract(out.as_mut_ptr(), salt_ptr, salt_size,
|
||||
ikm_ptr, ikm_size, typ)
|
||||
ws::wc_Tls13_HKDF_Extract_ex(out.as_mut_ptr(), salt_ptr, salt_size,
|
||||
ikm_ptr, ikm_size, typ, heap, dev_id)
|
||||
};
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
@@ -364,15 +384,77 @@ pub fn tls13_hkdf_extract(typ: i32, salt: Option<&[u8]>, key: Option<&mut [u8]>,
|
||||
/// &hash_hello1, &mut expand_out).expect("Error with tls13_hkdf_expand_label()");
|
||||
/// ```
|
||||
pub fn tls13_hkdf_expand_label(typ: i32, key: &[u8], protocol: &[u8], label: &[u8], info: &[u8], out: &mut [u8]) -> Result<(), i32> {
|
||||
tls13_hkdf_expand_label_ex(typ, key, protocol, label, info, out, None, None)
|
||||
}
|
||||
|
||||
/// Perform RFC 5869 HKDF-Expand operation for TLS v1.3 key derivation (with
|
||||
/// optional heap and device ID).
|
||||
///
|
||||
/// This utilizes HMAC to convert `key`, `label`, and `info` into a
|
||||
/// derived key which is written to `out`.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `typ`: Hash type, one of `HMAC::TYPE_*`.
|
||||
/// * `key`: Key to use for KDF (typically output of `tls13_hkdf_extract()`).
|
||||
/// * `protocol`: Buffer containing TLS protocol.
|
||||
/// * `label`: Buffer containing label.
|
||||
/// * `info`: Buffer containing additional info.
|
||||
/// * `out`: Output buffer to store TLS1.3 HKDF-Expand result. The buffer can be
|
||||
/// any size.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
|
||||
/// library error code value.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::hmac::HMAC;
|
||||
/// use wolfssl::wolfcrypt::kdf::*;
|
||||
/// use wolfssl::wolfcrypt::sha::SHA256;
|
||||
/// let hash_hello1 = [
|
||||
/// 0x63u8, 0x83, 0x58, 0xab, 0x36, 0xcd, 0x0c, 0xf3,
|
||||
/// 0x26, 0x07, 0xb5, 0x5f, 0x0b, 0x8b, 0x45, 0xd6,
|
||||
/// 0x7d, 0x5b, 0x42, 0xdc, 0xa8, 0xaa, 0x06, 0xfb,
|
||||
/// 0x20, 0xa5, 0xbb, 0x85, 0xdb, 0x54, 0xd8, 0x8b
|
||||
/// ];
|
||||
/// let client_early_traffic_secret = [
|
||||
/// 0x20u8, 0x18, 0x72, 0x7c, 0xde, 0x3a, 0x85, 0x17, 0x72, 0xdc, 0xd7, 0x72,
|
||||
/// 0xb0, 0xfc, 0x45, 0xd0, 0x62, 0xb9, 0xbb, 0x38, 0x69, 0x05, 0x7b, 0xb4,
|
||||
/// 0x5e, 0x58, 0x5d, 0xed, 0xcd, 0x0b, 0x96, 0xd3
|
||||
/// ];
|
||||
/// let mut secret = [0u8; SHA256::DIGEST_SIZE];
|
||||
/// tls13_hkdf_extract(HMAC::TYPE_SHA256, None, None, &mut secret).expect("Error with tls13_hkdf_extract()");
|
||||
/// let protocol_label = b"tls13 ";
|
||||
/// let ce_traffic_label = b"c e traffic";
|
||||
/// let mut expand_out = [0u8; SHA256::DIGEST_SIZE];
|
||||
/// tls13_hkdf_expand_label_ex(HMAC::TYPE_SHA256, &secret,
|
||||
/// protocol_label, ce_traffic_label,
|
||||
/// &hash_hello1, &mut expand_out, None, None).expect("Error with tls13_hkdf_expand_label_ex()");
|
||||
/// ```
|
||||
pub fn tls13_hkdf_expand_label_ex(typ: i32, key: &[u8], protocol: &[u8], label: &[u8], info: &[u8], out: &mut [u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<(), i32> {
|
||||
let key_size = key.len() as u32;
|
||||
let protocol_size = protocol.len() as u32;
|
||||
let label_size = label.len() as u32;
|
||||
let info_size = info.len() as u32;
|
||||
let out_size = out.len() as u32;
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe {
|
||||
ws::wc_Tls13_HKDF_Expand_Label(out.as_mut_ptr(), out_size,
|
||||
ws::wc_Tls13_HKDF_Expand_Label_ex(out.as_mut_ptr(), out_size,
|
||||
key.as_ptr(), key_size, protocol.as_ptr(), protocol_size,
|
||||
label.as_ptr(), label_size, info.as_ptr(), info_size, typ)
|
||||
label.as_ptr(), label_size, info.as_ptr(), info_size, typ,
|
||||
heap, dev_id)
|
||||
};
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
|
||||
@@ -46,9 +46,8 @@ pub const PRF_HASH_SM3: i32 = ws::wc_MACAlgorithm_sm3_mac as i32;
|
||||
/// * `secret`: Secret key.
|
||||
/// * `seed`: Seed.
|
||||
/// * `hash_type`: PRF Hash type, one of `PRF_HASH_*`.
|
||||
/// * `heap`: Heap hint.
|
||||
/// * `dev_id` Device ID to use with crypto callbacks or async hardware.
|
||||
/// Set to INVALID_DEVID (-2) if not used.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
/// * `dout`: Output buffer.
|
||||
///
|
||||
/// # Returns
|
||||
@@ -75,14 +74,20 @@ pub const PRF_HASH_SM3: i32 = ws::wc_MACAlgorithm_sm3_mac as i32;
|
||||
/// 0x91, 0xde, 0x5c, 0xc0, 0x47, 0x7c, 0xa8, 0xae, 0xcf,
|
||||
/// 0x5d, 0x93, 0x5f, 0x4c, 0x92, 0xcc, 0x98, 0x5b, 0x43];
|
||||
/// let mut out = [0u8; 12];
|
||||
/// prf(&secret, &seed, PRF_HASH_SHA384,
|
||||
/// core::ptr::null_mut(), ws::INVALID_DEVID,
|
||||
/// &mut out).expect("Error with prf()");
|
||||
/// prf(&secret, &seed, PRF_HASH_SHA384, None, None, &mut out).expect("Error with prf()");
|
||||
/// ```
|
||||
pub fn prf(secret: &[u8], seed: &[u8], hash_type: i32, heap: *mut ::std::os::raw::c_void, dev_id: i32, dout: &mut [u8]) -> Result<(), i32> {
|
||||
pub fn prf(secret: &[u8], seed: &[u8], hash_type: i32, heap: Option<*mut ::std::os::raw::c_void>, dev_id: Option<i32>, dout: &mut [u8]) -> Result<(), i32> {
|
||||
let secret_size = secret.len() as u32;
|
||||
let seed_size = seed.len() as u32;
|
||||
let dout_size = dout.len() as u32;
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe {
|
||||
ws::wc_PRF(dout.as_mut_ptr(), dout_size,
|
||||
secret.as_ptr(), secret_size,
|
||||
|
||||
@@ -72,8 +72,36 @@ impl RNG {
|
||||
/// A Result which is Ok(RNG) on success or an Err containing the wolfSSL
|
||||
/// library return code on failure.
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
RNG::new_ex(None, None)
|
||||
}
|
||||
|
||||
/// Initialize a new `RNG` instance with optional heap and device ID.
|
||||
///
|
||||
/// This function wraps the wolfssl library function `wc_InitRng`, which
|
||||
/// performs the necessary initialization for the RNG context.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A Result which is Ok(RNG) on success or an Err containing the wolfSSL
|
||||
/// library return code on failure.
|
||||
pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut rng: MaybeUninit<RNG> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_InitRng(&mut (*rng.as_mut_ptr()).wc_rng) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe {
|
||||
ws::wc_InitRng_ex(&mut (*rng.as_mut_ptr()).wc_rng, heap, dev_id)
|
||||
};
|
||||
if rc == 0 {
|
||||
let rng = unsafe { rng.assume_init() };
|
||||
Ok(rng)
|
||||
@@ -93,11 +121,38 @@ impl RNG {
|
||||
/// A Result which is Ok(RNG) on success or an Err containing the wolfSSL
|
||||
/// library return code on failure.
|
||||
pub fn new_with_nonce<T>(nonce: &mut [T]) -> Result<Self, i32> {
|
||||
RNG::new_with_nonce_ex(nonce, None, None)
|
||||
}
|
||||
|
||||
/// Initialize a new `RNG` instance and provide a nonce input.
|
||||
///
|
||||
/// This function wraps the wolfssl library function `wc_InitRngNonce`,
|
||||
/// which performs the necessary initialization for the RNG context and
|
||||
/// accepts a nonce input buffer.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A Result which is Ok(RNG) on success or an Err containing the wolfSSL
|
||||
/// library return code on failure.
|
||||
pub fn new_with_nonce_ex<T>(nonce: &mut [T], heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let ptr = nonce.as_mut_ptr() as *mut u8;
|
||||
let size: u32 = (nonce.len() * size_of::<T>()) as u32;
|
||||
let mut rng: MaybeUninit<RNG> = MaybeUninit::uninit();
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe {
|
||||
ws::wc_InitRngNonce(&mut (*rng.as_mut_ptr()).wc_rng, ptr, size)
|
||||
ws::wc_InitRngNonce_ex(&mut (*rng.as_mut_ptr()).wc_rng, ptr, size, heap, dev_id)
|
||||
};
|
||||
if rc == 0 {
|
||||
let rng = unsafe { rng.assume_init() };
|
||||
|
||||
@@ -38,7 +38,7 @@ use wolfssl::wolfcrypt::rsa::RSA;
|
||||
let mut rng = RNG::new().expect("Error creating RNG");
|
||||
let key_path = "../../../certs/client-keyPub.der";
|
||||
let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
let mut rsa = RSA::new_public_from_der(&der).expect("Error with new_public_from_der()");
|
||||
let mut rsa = RSA::new_public_from_der(&der, None, None).expect("Error with new_public_from_der()");
|
||||
rsa.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
let plain: &[u8] = b"Test message";
|
||||
let mut enc: [u8; 512] = [0; 512];
|
||||
@@ -47,7 +47,7 @@ assert!(enc_len > 0 && enc_len <= 512);
|
||||
|
||||
let key_path = "../../../certs/client-key.der";
|
||||
let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
let mut rsa = RSA::new_from_der(&der).expect("Error with new_from_der()");
|
||||
let mut rsa = RSA::new_from_der(&der, None, None).expect("Error with new_from_der()");
|
||||
rsa.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
let mut plain_out: [u8; 512] = [0; 512];
|
||||
let dec_len = rsa.private_decrypt(&enc[0..enc_len], &mut plain_out).expect("Error with private_decrypt()");
|
||||
@@ -112,6 +112,12 @@ impl RSA {
|
||||
|
||||
/// Load a public and private RSA keypair from DER-encoded buffer.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `der`: DER-encoded input buffer.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(RSA) containing the RSA struct instance or Err(e)
|
||||
@@ -127,7 +133,7 @@ impl RSA {
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let key_path = "../../../certs/client-keyPub.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_public_from_der(&der).expect("Error with new_public_from_der()");
|
||||
/// let mut rsa = RSA::new_public_from_der(&der, None, None).expect("Error with new_public_from_der()");
|
||||
/// rsa.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
/// let plain: &[u8] = b"Test message";
|
||||
/// let mut enc: [u8; 512] = [0; 512];
|
||||
@@ -136,16 +142,24 @@ impl RSA {
|
||||
///
|
||||
/// let key_path = "../../../certs/client-key.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_from_der(&der).expect("Error with new_from_der()");
|
||||
/// let mut rsa = RSA::new_from_der(&der, None, None).expect("Error with new_from_der()");
|
||||
/// rsa.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
/// let mut plain_out: [u8; 512] = [0; 512];
|
||||
/// let dec_len = rsa.private_decrypt(&enc[0..enc_len], &mut plain_out).expect("Error with private_decrypt()");
|
||||
/// assert!(dec_len as usize == plain.len());
|
||||
/// assert_eq!(plain_out[0..dec_len], *plain);
|
||||
/// ```
|
||||
pub fn new_from_der(der: &[u8]) -> Result<Self, i32> {
|
||||
pub fn new_from_der(der: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_rsakey: MaybeUninit<ws::RsaKey> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_InitRsaKey(wc_rsakey.as_mut_ptr(), core::ptr::null_mut()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitRsaKey_ex(wc_rsakey.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -166,6 +180,12 @@ impl RSA {
|
||||
|
||||
/// Load a public RSA key from DER-encoded buffer.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `der`: DER-encoded input buffer.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(RSA) containing the RSA struct instance or Err(e)
|
||||
@@ -181,7 +201,7 @@ impl RSA {
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let key_path = "../../../certs/client-keyPub.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_public_from_der(&der).expect("Error with new_public_from_der()");
|
||||
/// let mut rsa = RSA::new_public_from_der(&der, None, None).expect("Error with new_public_from_der()");
|
||||
/// rsa.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
/// let plain: &[u8] = b"Test message";
|
||||
/// let mut enc: [u8; 512] = [0; 512];
|
||||
@@ -190,16 +210,24 @@ impl RSA {
|
||||
///
|
||||
/// let key_path = "../../../certs/client-key.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_from_der(&der).expect("Error with new_from_der()");
|
||||
/// let mut rsa = RSA::new_from_der(&der, None, None).expect("Error with new_from_der()");
|
||||
/// rsa.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
/// let mut plain_out: [u8; 512] = [0; 512];
|
||||
/// let dec_len = rsa.private_decrypt(&enc[0..enc_len], &mut plain_out).expect("Error with private_decrypt()");
|
||||
/// assert!(dec_len as usize == plain.len());
|
||||
/// assert_eq!(plain_out[0..dec_len], *plain);
|
||||
/// ```
|
||||
pub fn new_public_from_der(der: &[u8]) -> Result<Self, i32> {
|
||||
pub fn new_public_from_der(der: &[u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_rsakey: MaybeUninit<ws::RsaKey> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_InitRsaKey(wc_rsakey.as_mut_ptr(), core::ptr::null_mut()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitRsaKey_ex(wc_rsakey.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -236,6 +264,8 @@ impl RSA {
|
||||
/// choice is 65537.
|
||||
/// * `rng`: Reference to a `RNG` struct to use for random number
|
||||
/// generation while making the key.
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -249,14 +279,22 @@ impl RSA {
|
||||
/// use wolfssl::wolfcrypt::rsa::RSA;
|
||||
///
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut rsa = RSA::generate(2048, 65537, &mut rng).expect("Error with generate()");
|
||||
/// let mut rsa = RSA::generate(2048, 65537, &mut rng, None, None).expect("Error with generate()");
|
||||
/// rsa.check().expect("Error with check()");
|
||||
/// let encrypt_size = rsa.get_encrypt_size().expect("Error with get_encrypt_size()");
|
||||
/// assert_eq!(encrypt_size, 256);
|
||||
/// ```
|
||||
pub fn generate(size: i32, e: i64, rng: &mut RNG) -> Result<Self, i32> {
|
||||
pub fn generate(size: i32, e: i64, rng: &mut RNG, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_rsakey: MaybeUninit<ws::RsaKey> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_InitRsaKey(wc_rsakey.as_mut_ptr(), core::ptr::null_mut()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitRsaKey_ex(wc_rsakey.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -299,7 +337,7 @@ impl RSA {
|
||||
/// use wolfssl::wolfcrypt::rsa::RSA;
|
||||
///
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut rsa = RSA::generate(2048, 65537, &mut rng).expect("Error with generate()");
|
||||
/// let mut rsa = RSA::generate(2048, 65537, &mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut e: [u8; 256] = [0; 256];
|
||||
/// let mut e_size: u32 = 0;
|
||||
/// let mut n: [u8; 256] = [0; 256];
|
||||
@@ -360,7 +398,7 @@ impl RSA {
|
||||
/// use wolfssl::wolfcrypt::rsa::RSA;
|
||||
///
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut rsa = RSA::generate(2048, 65537, &mut rng).expect("Error with generate()");
|
||||
/// let mut rsa = RSA::generate(2048, 65537, &mut rng, None, None).expect("Error with generate()");
|
||||
/// let mut e: [u8; 256] = [0; 256];
|
||||
/// let mut e_size: u32 = 0;
|
||||
/// let mut n: [u8; 256] = [0; 256];
|
||||
@@ -398,7 +436,7 @@ impl RSA {
|
||||
/// use wolfssl::wolfcrypt::rsa::RSA;
|
||||
///
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut rsa = RSA::generate(2048, 65537, &mut rng).expect("Error with generate()");
|
||||
/// let mut rsa = RSA::generate(2048, 65537, &mut rng, None, None).expect("Error with generate()");
|
||||
/// let encrypt_size = rsa.get_encrypt_size().expect("Error with get_encrypt_size()");
|
||||
/// assert_eq!(encrypt_size, 256);
|
||||
/// ```
|
||||
@@ -424,7 +462,7 @@ impl RSA {
|
||||
/// use wolfssl::wolfcrypt::rsa::RSA;
|
||||
///
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let mut rsa = RSA::generate(2048, 65537, &mut rng).expect("Error with generate()");
|
||||
/// let mut rsa = RSA::generate(2048, 65537, &mut rng, None, None).expect("Error with generate()");
|
||||
/// rsa.check().expect("Error with check()");
|
||||
/// ```
|
||||
pub fn check(&mut self) -> Result<(), i32> {
|
||||
@@ -461,7 +499,7 @@ impl RSA {
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let key_path = "../../../certs/client-keyPub.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_public_from_der(&der).expect("Error with new_public_from_der()");
|
||||
/// let mut rsa = RSA::new_public_from_der(&der, None, None).expect("Error with new_public_from_der()");
|
||||
/// rsa.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
/// let plain: &[u8] = b"Test message";
|
||||
/// let mut enc: [u8; 512] = [0; 512];
|
||||
@@ -470,7 +508,7 @@ impl RSA {
|
||||
///
|
||||
/// let key_path = "../../../certs/client-key.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_from_der(&der).expect("Error with new_from_der()");
|
||||
/// let mut rsa = RSA::new_from_der(&der, None, None).expect("Error with new_from_der()");
|
||||
/// rsa.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
/// let mut plain_out: [u8; 512] = [0; 512];
|
||||
/// let dec_len = rsa.private_decrypt(&enc[0..enc_len], &mut plain_out).expect("Error with private_decrypt()");
|
||||
@@ -516,7 +554,7 @@ impl RSA {
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let key_path = "../../../certs/client-keyPub.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_public_from_der(&der).expect("Error with new_public_from_der()");
|
||||
/// let mut rsa = RSA::new_public_from_der(&der, None, None).expect("Error with new_public_from_der()");
|
||||
/// rsa.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
/// let plain: &[u8] = b"Test message";
|
||||
/// let mut enc: [u8; 512] = [0; 512];
|
||||
@@ -525,7 +563,7 @@ impl RSA {
|
||||
///
|
||||
/// let key_path = "../../../certs/client-key.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_from_der(&der).expect("Error with new_from_der()");
|
||||
/// let mut rsa = RSA::new_from_der(&der, None, None).expect("Error with new_from_der()");
|
||||
/// rsa.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
/// let mut plain_out: [u8; 512] = [0; 512];
|
||||
/// let dec_len = rsa.private_decrypt(&enc[0..enc_len], &mut plain_out).expect("Error with private_decrypt()");
|
||||
@@ -577,7 +615,7 @@ impl RSA {
|
||||
///
|
||||
/// let key_path = "../../../certs/client-key.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_from_der(&der).expect("Error with new_from_der()");
|
||||
/// let mut rsa = RSA::new_from_der(&der, None, None).expect("Error with new_from_der()");
|
||||
/// let msg: &[u8] = b"This is the string to be signed!";
|
||||
/// let mut signature: [u8; 512] = [0; 512];
|
||||
/// let sig_len = rsa.pss_sign(msg, &mut signature, RSA::HASH_TYPE_SHA256, RSA::MGF1SHA256, &mut rng).expect("Error with pss_sign()");
|
||||
@@ -585,7 +623,7 @@ impl RSA {
|
||||
///
|
||||
/// let key_path = "../../../certs/client-keyPub.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_public_from_der(&der).expect("Error with new_public_from_der()");
|
||||
/// let mut rsa = RSA::new_public_from_der(&der, None, None).expect("Error with new_public_from_der()");
|
||||
/// rsa.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
/// let signature = &signature[0..sig_len];
|
||||
/// let mut verify_out: [u8; 512] = [0; 512];
|
||||
@@ -638,7 +676,7 @@ impl RSA {
|
||||
///
|
||||
/// let key_path = "../../../certs/client-key.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_from_der(&der).expect("Error with new_from_der()");
|
||||
/// let mut rsa = RSA::new_from_der(&der, None, None).expect("Error with new_from_der()");
|
||||
/// let msg: &[u8] = b"This is the string to be signed!";
|
||||
/// let mut signature: [u8; 512] = [0; 512];
|
||||
/// let sig_len = rsa.pss_sign(msg, &mut signature, RSA::HASH_TYPE_SHA256, RSA::MGF1SHA256, &mut rng).expect("Error with pss_sign()");
|
||||
@@ -646,7 +684,7 @@ impl RSA {
|
||||
///
|
||||
/// let key_path = "../../../certs/client-keyPub.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_public_from_der(&der).expect("Error with new_public_from_der()");
|
||||
/// let mut rsa = RSA::new_public_from_der(&der, None, None).expect("Error with new_public_from_der()");
|
||||
/// rsa.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
/// let signature = &signature[0..sig_len];
|
||||
/// let mut verify_out: [u8; 512] = [0; 512];
|
||||
@@ -702,7 +740,7 @@ impl RSA {
|
||||
///
|
||||
/// let key_path = "../../../certs/client-key.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_from_der(&der).expect("Error with new_from_der()");
|
||||
/// let mut rsa = RSA::new_from_der(&der, None, None).expect("Error with new_from_der()");
|
||||
/// let msg: &[u8] = b"This is the string to be signed!";
|
||||
/// let mut signature: [u8; 512] = [0; 512];
|
||||
/// let sig_len = rsa.pss_sign(msg, &mut signature, RSA::HASH_TYPE_SHA256, RSA::MGF1SHA256, &mut rng).expect("Error with pss_sign()");
|
||||
@@ -710,7 +748,7 @@ impl RSA {
|
||||
///
|
||||
/// let key_path = "../../../certs/client-keyPub.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_public_from_der(&der).expect("Error with new_public_from_der()");
|
||||
/// let mut rsa = RSA::new_public_from_der(&der, None, None).expect("Error with new_public_from_der()");
|
||||
/// rsa.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
/// let signature = &signature[0..sig_len];
|
||||
/// let mut verify_out: [u8; 512] = [0; 512];
|
||||
@@ -770,7 +808,7 @@ impl RSA {
|
||||
///
|
||||
/// let key_path = "../../../certs/client-key.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_from_der(&der).expect("Error with new_from_der()");
|
||||
/// let mut rsa = RSA::new_from_der(&der, None, None).expect("Error with new_from_der()");
|
||||
/// let msg: &[u8] = b"This is the string to be signed!";
|
||||
/// let mut signature: [u8; 512] = [0; 512];
|
||||
/// let sig_len = rsa.pss_sign(msg, &mut signature, RSA::HASH_TYPE_SHA256, RSA::MGF1SHA256, &mut rng).expect("Error with pss_sign()");
|
||||
@@ -778,7 +816,7 @@ impl RSA {
|
||||
///
|
||||
/// let key_path = "../../../certs/client-keyPub.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_public_from_der(&der).expect("Error with new_public_from_der()");
|
||||
/// let mut rsa = RSA::new_public_from_der(&der, None, None).expect("Error with new_public_from_der()");
|
||||
/// rsa.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
/// let signature = &signature[0..sig_len];
|
||||
/// let mut verify_out: [u8; 512] = [0; 512];
|
||||
@@ -838,7 +876,7 @@ impl RSA {
|
||||
///
|
||||
/// let key_path = "../../../certs/client-key.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_from_der(&der).expect("Error with new_from_der()");
|
||||
/// let mut rsa = RSA::new_from_der(&der, None, None).expect("Error with new_from_der()");
|
||||
/// let msg = b"A rsa_direct() test input string";
|
||||
/// let mut plain = [0u8; 256];
|
||||
/// plain[..msg.len()].copy_from_slice(msg);
|
||||
@@ -891,7 +929,7 @@ impl RSA {
|
||||
/// let mut rng = RNG::new().expect("Error creating RNG");
|
||||
/// let key_path = "../../../certs/client-keyPub.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_public_from_der(&der).expect("Error with new_public_from_der()");
|
||||
/// let mut rsa = RSA::new_public_from_der(&der, None, None).expect("Error with new_public_from_der()");
|
||||
/// rsa.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
/// let plain: &[u8] = b"Test message";
|
||||
/// let mut enc: [u8; 512] = [0; 512];
|
||||
@@ -900,7 +938,7 @@ impl RSA {
|
||||
|
||||
/// let key_path = "../../../certs/client-key.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_from_der(&der).expect("Error with new_from_der()");
|
||||
/// let mut rsa = RSA::new_from_der(&der, None, None).expect("Error with new_from_der()");
|
||||
/// rsa.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
/// let mut plain_out: [u8; 512] = [0; 512];
|
||||
/// let dec_len = rsa.private_decrypt(&enc[0..enc_len], &mut plain_out).expect("Error with private_decrypt()");
|
||||
@@ -944,7 +982,7 @@ impl RSA {
|
||||
///
|
||||
/// let key_path = "../../../certs/client-key.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_from_der(&der).expect("Error with new_from_der()");
|
||||
/// let mut rsa = RSA::new_from_der(&der, None, None).expect("Error with new_from_der()");
|
||||
/// let msg: &[u8] = b"This is the string to be signed!";
|
||||
/// let mut signature: [u8; 512] = [0; 512];
|
||||
/// let sig_len = rsa.ssl_sign(msg, &mut signature, &mut rng).expect("Error with ssl_sign()");
|
||||
@@ -952,7 +990,7 @@ impl RSA {
|
||||
///
|
||||
/// let key_path = "../../../certs/client-keyPub.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_public_from_der(&der).expect("Error with new_public_from_der()");
|
||||
/// let mut rsa = RSA::new_public_from_der(&der, None, None).expect("Error with new_public_from_der()");
|
||||
/// rsa.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
/// let signature = &signature[0..sig_len];
|
||||
/// let mut verify_out: [u8; 512] = [0; 512];
|
||||
@@ -1002,7 +1040,7 @@ impl RSA {
|
||||
///
|
||||
/// let key_path = "../../../certs/client-key.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_from_der(&der).expect("Error with new_from_der()");
|
||||
/// let mut rsa = RSA::new_from_der(&der, None, None).expect("Error with new_from_der()");
|
||||
/// let msg: &[u8] = b"This is the string to be signed!";
|
||||
/// let mut signature: [u8; 512] = [0; 512];
|
||||
/// let sig_len = rsa.ssl_sign(msg, &mut signature, &mut rng).expect("Error with ssl_sign()");
|
||||
@@ -1010,7 +1048,7 @@ impl RSA {
|
||||
///
|
||||
/// let key_path = "../../../certs/client-keyPub.der";
|
||||
/// let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
/// let mut rsa = RSA::new_public_from_der(&der).expect("Error with new_public_from_der()");
|
||||
/// let mut rsa = RSA::new_public_from_der(&der, None, None).expect("Error with new_public_from_der()");
|
||||
/// rsa.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
/// let signature = &signature[0..sig_len];
|
||||
/// let mut verify_out: [u8; 512] = [0; 512];
|
||||
|
||||
@@ -41,6 +41,11 @@ impl SHA {
|
||||
|
||||
/// Build a new SHA instance.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(sha) containing the SHA struct instance or Err(e)
|
||||
@@ -50,11 +55,19 @@ impl SHA {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA;
|
||||
/// let sha = SHA::new().expect("Error with new()");
|
||||
/// let sha = SHA::new(None, None).expect("Error with new()");
|
||||
/// ```
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_sha: MaybeUninit<ws::wc_Sha> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_InitSha(wc_sha.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitSha_ex(wc_sha.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -68,6 +81,11 @@ impl SHA {
|
||||
/// This does not need to be called after `new()`, but should be called
|
||||
/// after a hash calculation to prepare for a new calculation.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
|
||||
@@ -77,11 +95,19 @@ impl SHA {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA;
|
||||
/// let mut sha = SHA::new().expect("Error with new()");
|
||||
/// sha.init().expect("Error with init()");
|
||||
/// let mut sha = SHA::new(None, None).expect("Error with new()");
|
||||
/// sha.init(None, None).expect("Error with init()");
|
||||
/// ```
|
||||
pub fn init(&mut self) -> Result<(), i32> {
|
||||
let rc = unsafe { ws::wc_InitSha(&mut self.wc_sha) };
|
||||
pub fn init(&mut self, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<(), i32> {
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitSha_ex(&mut self.wc_sha, heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -103,7 +129,7 @@ impl SHA {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA;
|
||||
/// let mut sha = SHA::new().expect("Error with new()");
|
||||
/// let mut sha = SHA::new(None, None).expect("Error with new()");
|
||||
/// sha.update(b"input").expect("Error with update()");
|
||||
/// ```
|
||||
pub fn update(&mut self, data: &[u8]) -> Result<(), i32> {
|
||||
@@ -133,7 +159,7 @@ impl SHA {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA;
|
||||
/// let mut sha = SHA::new().expect("Error with new()");
|
||||
/// let mut sha = SHA::new(None, None).expect("Error with new()");
|
||||
/// sha.update(b"input").expect("Error with update()");
|
||||
/// let mut hash = [0u8; SHA::DIGEST_SIZE];
|
||||
/// sha.finalize(&mut hash).expect("Error with finalize()");
|
||||
@@ -176,6 +202,11 @@ impl SHA224 {
|
||||
|
||||
/// Build a new SHA224 instance.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(sha) containing the SHA224 struct instance or Err(e)
|
||||
@@ -185,11 +216,19 @@ impl SHA224 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA224;
|
||||
/// let sha = SHA224::new().expect("Error with new()");
|
||||
/// let sha = SHA224::new(None, None).expect("Error with new()");
|
||||
/// ```
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_sha224: MaybeUninit<ws::wc_Sha224> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_InitSha224(wc_sha224.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitSha224_ex(wc_sha224.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -203,6 +242,11 @@ impl SHA224 {
|
||||
/// This does not need to be called after `new()`, but should be called
|
||||
/// after a hash calculation to prepare for a new calculation.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
|
||||
@@ -212,11 +256,19 @@ impl SHA224 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA224;
|
||||
/// let mut sha = SHA224::new().expect("Error with new()");
|
||||
/// sha.init().expect("Error with init()");
|
||||
/// let mut sha = SHA224::new(None, None).expect("Error with new()");
|
||||
/// sha.init(None, None).expect("Error with init()");
|
||||
/// ```
|
||||
pub fn init(&mut self) -> Result<(), i32> {
|
||||
let rc = unsafe { ws::wc_InitSha224(&mut self.wc_sha224) };
|
||||
pub fn init(&mut self, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<(), i32> {
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitSha224_ex(&mut self.wc_sha224, heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -238,7 +290,7 @@ impl SHA224 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA224;
|
||||
/// let mut sha = SHA224::new().expect("Error with new()");
|
||||
/// let mut sha = SHA224::new(None, None).expect("Error with new()");
|
||||
/// sha.update(b"input").expect("Error with update()");
|
||||
/// ```
|
||||
pub fn update(&mut self, data: &[u8]) -> Result<(), i32> {
|
||||
@@ -268,7 +320,7 @@ impl SHA224 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA224;
|
||||
/// let mut sha = SHA224::new().expect("Error with new()");
|
||||
/// let mut sha = SHA224::new(None, None).expect("Error with new()");
|
||||
/// sha.update(b"input").expect("Error with update()");
|
||||
/// let mut hash = [0u8; SHA224::DIGEST_SIZE];
|
||||
/// sha.finalize(&mut hash).expect("Error with finalize()");
|
||||
@@ -311,6 +363,11 @@ impl SHA256 {
|
||||
|
||||
/// Build a new SHA256 instance.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(sha) containing the SHA256 struct instance or Err(e)
|
||||
@@ -320,11 +377,19 @@ impl SHA256 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA256;
|
||||
/// let sha = SHA256::new().expect("Error with new()");
|
||||
/// let sha = SHA256::new(None, None).expect("Error with new()");
|
||||
/// ```
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_sha256: MaybeUninit<ws::wc_Sha256> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_InitSha256(wc_sha256.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitSha256_ex(wc_sha256.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -338,6 +403,11 @@ impl SHA256 {
|
||||
/// This does not need to be called after `new()`, but should be called
|
||||
/// after a hash calculation to prepare for a new calculation.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
|
||||
@@ -347,11 +417,19 @@ impl SHA256 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA256;
|
||||
/// let mut sha = SHA256::new().expect("Error with new()");
|
||||
/// sha.init().expect("Error with init()");
|
||||
/// let mut sha = SHA256::new(None, None).expect("Error with new()");
|
||||
/// sha.init(None, None).expect("Error with init()");
|
||||
/// ```
|
||||
pub fn init(&mut self) -> Result<(), i32> {
|
||||
let rc = unsafe { ws::wc_InitSha256(&mut self.wc_sha256) };
|
||||
pub fn init(&mut self, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<(), i32> {
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitSha256_ex(&mut self.wc_sha256, heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -373,7 +451,7 @@ impl SHA256 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA256;
|
||||
/// let mut sha = SHA256::new().expect("Error with new()");
|
||||
/// let mut sha = SHA256::new(None, None).expect("Error with new()");
|
||||
/// sha.update(b"input").expect("Error with update()");
|
||||
/// ```
|
||||
pub fn update(&mut self, data: &[u8]) -> Result<(), i32> {
|
||||
@@ -403,7 +481,7 @@ impl SHA256 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA256;
|
||||
/// let mut sha = SHA256::new().expect("Error with new()");
|
||||
/// let mut sha = SHA256::new(None, None).expect("Error with new()");
|
||||
/// sha.update(b"input").expect("Error with update()");
|
||||
/// let mut hash = [0u8; SHA256::DIGEST_SIZE];
|
||||
/// sha.finalize(&mut hash).expect("Error with finalize()");
|
||||
@@ -446,6 +524,11 @@ impl SHA384 {
|
||||
|
||||
/// Build a new SHA384 instance.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(sha) containing the SHA384 struct instance or Err(e)
|
||||
@@ -455,11 +538,19 @@ impl SHA384 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA384;
|
||||
/// let sha = SHA384::new().expect("Error with new()");
|
||||
/// let sha = SHA384::new(None, None).expect("Error with new()");
|
||||
/// ```
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_sha384: MaybeUninit<ws::wc_Sha384> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_InitSha384(wc_sha384.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitSha384_ex(wc_sha384.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -473,6 +564,11 @@ impl SHA384 {
|
||||
/// This does not need to be called after `new()`, but should be called
|
||||
/// after a hash calculation to prepare for a new calculation.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
|
||||
@@ -482,11 +578,19 @@ impl SHA384 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA384;
|
||||
/// let mut sha = SHA384::new().expect("Error with new()");
|
||||
/// sha.init().expect("Error with init()");
|
||||
/// let mut sha = SHA384::new(None, None).expect("Error with new()");
|
||||
/// sha.init(None, None).expect("Error with init()");
|
||||
/// ```
|
||||
pub fn init(&mut self) -> Result<(), i32> {
|
||||
let rc = unsafe { ws::wc_InitSha384(&mut self.wc_sha384) };
|
||||
pub fn init(&mut self, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<(), i32> {
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitSha384_ex(&mut self.wc_sha384, heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -508,7 +612,7 @@ impl SHA384 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA384;
|
||||
/// let mut sha = SHA384::new().expect("Error with new()");
|
||||
/// let mut sha = SHA384::new(None, None).expect("Error with new()");
|
||||
/// sha.update(b"input").expect("Error with update()");
|
||||
/// ```
|
||||
pub fn update(&mut self, data: &[u8]) -> Result<(), i32> {
|
||||
@@ -538,7 +642,7 @@ impl SHA384 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA384;
|
||||
/// let mut sha = SHA384::new().expect("Error with new()");
|
||||
/// let mut sha = SHA384::new(None, None).expect("Error with new()");
|
||||
/// sha.update(b"input").expect("Error with update()");
|
||||
/// let mut hash = [0u8; SHA384::DIGEST_SIZE];
|
||||
/// sha.finalize(&mut hash).expect("Error with finalize()");
|
||||
@@ -581,6 +685,11 @@ impl SHA512 {
|
||||
|
||||
/// Build a new SHA512 instance.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(sha) containing the SHA512 struct instance or Err(e)
|
||||
@@ -590,11 +699,19 @@ impl SHA512 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA512;
|
||||
/// let sha = SHA512::new().expect("Error with new()");
|
||||
/// let sha = SHA512::new(None, None).expect("Error with new()");
|
||||
/// ```
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_sha512: MaybeUninit<ws::wc_Sha512> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_InitSha512(wc_sha512.as_mut_ptr()) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitSha512_ex(wc_sha512.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -608,6 +725,11 @@ impl SHA512 {
|
||||
/// This does not need to be called after `new()`, but should be called
|
||||
/// after a hash calculation to prepare for a new calculation.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
|
||||
@@ -617,11 +739,19 @@ impl SHA512 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA512;
|
||||
/// let mut sha = SHA512::new().expect("Error with new()");
|
||||
/// sha.init().expect("Error with init()");
|
||||
/// let mut sha = SHA512::new(None, None).expect("Error with new()");
|
||||
/// sha.init(None, None).expect("Error with init()");
|
||||
/// ```
|
||||
pub fn init(&mut self) -> Result<(), i32> {
|
||||
let rc = unsafe { ws::wc_InitSha512(&mut self.wc_sha512) };
|
||||
pub fn init(&mut self, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<(), i32> {
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitSha512_ex(&mut self.wc_sha512, heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -643,7 +773,7 @@ impl SHA512 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA512;
|
||||
/// let mut sha = SHA512::new().expect("Error with new()");
|
||||
/// let mut sha = SHA512::new(None, None).expect("Error with new()");
|
||||
/// sha.update(b"input").expect("Error with update()");
|
||||
/// ```
|
||||
pub fn update(&mut self, data: &[u8]) -> Result<(), i32> {
|
||||
@@ -673,7 +803,7 @@ impl SHA512 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA512;
|
||||
/// let mut sha = SHA512::new().expect("Error with new()");
|
||||
/// let mut sha = SHA512::new(None, None).expect("Error with new()");
|
||||
/// sha.update(b"input").expect("Error with update()");
|
||||
/// let mut hash = [0u8; SHA512::DIGEST_SIZE];
|
||||
/// sha.finalize(&mut hash).expect("Error with finalize()");
|
||||
@@ -716,6 +846,11 @@ impl SHA3_224 {
|
||||
|
||||
/// Build a new SHA3_224 instance.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(sha) containing the SHA3_224 struct instance or Err(e)
|
||||
@@ -725,11 +860,19 @@ impl SHA3_224 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA3_224;
|
||||
/// let sha = SHA3_224::new().expect("Error with new()");
|
||||
/// let sha = SHA3_224::new(None, None).expect("Error with new()");
|
||||
/// ```
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_sha3: MaybeUninit<ws::wc_Sha3> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_InitSha3_224(wc_sha3.as_mut_ptr(), core::ptr::null_mut(), ws::INVALID_DEVID) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitSha3_224(wc_sha3.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -743,6 +886,11 @@ impl SHA3_224 {
|
||||
/// This does not need to be called after `new()`, but should be called
|
||||
/// after a hash calculation to prepare for a new calculation.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
|
||||
@@ -752,11 +900,19 @@ impl SHA3_224 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA3_224;
|
||||
/// let mut sha = SHA3_224::new().expect("Error with new()");
|
||||
/// sha.init().expect("Error with init()");
|
||||
/// let mut sha = SHA3_224::new(None, None).expect("Error with new()");
|
||||
/// sha.init(None, None).expect("Error with init()");
|
||||
/// ```
|
||||
pub fn init(&mut self) -> Result<(), i32> {
|
||||
let rc = unsafe { ws::wc_InitSha3_224(&mut self.wc_sha3, core::ptr::null_mut(), ws::INVALID_DEVID) };
|
||||
pub fn init(&mut self, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<(), i32> {
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitSha3_224(&mut self.wc_sha3, heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -778,7 +934,7 @@ impl SHA3_224 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA3_224;
|
||||
/// let mut sha = SHA3_224::new().expect("Error with new()");
|
||||
/// let mut sha = SHA3_224::new(None, None).expect("Error with new()");
|
||||
/// sha.update(b"input").expect("Error with update()");
|
||||
/// ```
|
||||
pub fn update(&mut self, data: &[u8]) -> Result<(), i32> {
|
||||
@@ -808,7 +964,7 @@ impl SHA3_224 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA3_224;
|
||||
/// let mut sha = SHA3_224::new().expect("Error with new()");
|
||||
/// let mut sha = SHA3_224::new(None, None).expect("Error with new()");
|
||||
/// sha.update(b"input").expect("Error with update()");
|
||||
/// let mut hash = [0u8; SHA3_224::DIGEST_SIZE];
|
||||
/// sha.finalize(&mut hash).expect("Error with finalize()");
|
||||
@@ -851,6 +1007,11 @@ impl SHA3_256 {
|
||||
|
||||
/// Build a new SHA3_256 instance.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(sha) containing the SHA3_256 struct instance or Err(e)
|
||||
@@ -860,11 +1021,19 @@ impl SHA3_256 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA3_256;
|
||||
/// let sha = SHA3_256::new().expect("Error with new()");
|
||||
/// let sha = SHA3_256::new(None, None).expect("Error with new()");
|
||||
/// ```
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_sha3: MaybeUninit<ws::wc_Sha3> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_InitSha3_256(wc_sha3.as_mut_ptr(), core::ptr::null_mut(), ws::INVALID_DEVID) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitSha3_256(wc_sha3.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -878,6 +1047,11 @@ impl SHA3_256 {
|
||||
/// This does not need to be called after `new()`, but should be called
|
||||
/// after a hash calculation to prepare for a new calculation.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
|
||||
@@ -887,11 +1061,19 @@ impl SHA3_256 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA3_256;
|
||||
/// let mut sha = SHA3_256::new().expect("Error with new()");
|
||||
/// sha.init().expect("Error with init()");
|
||||
/// let mut sha = SHA3_256::new(None, None).expect("Error with new()");
|
||||
/// sha.init(None, None).expect("Error with init()");
|
||||
/// ```
|
||||
pub fn init(&mut self) -> Result<(), i32> {
|
||||
let rc = unsafe { ws::wc_InitSha3_256(&mut self.wc_sha3, core::ptr::null_mut(), ws::INVALID_DEVID) };
|
||||
pub fn init(&mut self, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<(), i32> {
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitSha3_256(&mut self.wc_sha3, heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -913,7 +1095,7 @@ impl SHA3_256 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA3_256;
|
||||
/// let mut sha = SHA3_256::new().expect("Error with new()");
|
||||
/// let mut sha = SHA3_256::new(None, None).expect("Error with new()");
|
||||
/// sha.update(b"input").expect("Error with update()");
|
||||
/// ```
|
||||
pub fn update(&mut self, data: &[u8]) -> Result<(), i32> {
|
||||
@@ -943,7 +1125,7 @@ impl SHA3_256 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA3_256;
|
||||
/// let mut sha = SHA3_256::new().expect("Error with new()");
|
||||
/// let mut sha = SHA3_256::new(None, None).expect("Error with new()");
|
||||
/// sha.update(b"input").expect("Error with update()");
|
||||
/// let mut hash = [0u8; SHA3_256::DIGEST_SIZE];
|
||||
/// sha.finalize(&mut hash).expect("Error with finalize()");
|
||||
@@ -986,6 +1168,11 @@ impl SHA3_384 {
|
||||
|
||||
/// Build a new SHA3_384 instance.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(sha) containing the SHA3_384 struct instance or Err(e)
|
||||
@@ -995,11 +1182,19 @@ impl SHA3_384 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA3_384;
|
||||
/// let sha = SHA3_384::new().expect("Error with new()");
|
||||
/// let sha = SHA3_384::new(None, None).expect("Error with new()");
|
||||
/// ```
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_sha3: MaybeUninit<ws::wc_Sha3> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_InitSha3_384(wc_sha3.as_mut_ptr(), core::ptr::null_mut(), ws::INVALID_DEVID) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitSha3_384(wc_sha3.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -1013,6 +1208,11 @@ impl SHA3_384 {
|
||||
/// This does not need to be called after `new()`, but should be called
|
||||
/// after a hash calculation to prepare for a new calculation.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
|
||||
@@ -1022,11 +1222,19 @@ impl SHA3_384 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA3_384;
|
||||
/// let mut sha = SHA3_384::new().expect("Error with new()");
|
||||
/// sha.init().expect("Error with init()");
|
||||
/// let mut sha = SHA3_384::new(None, None).expect("Error with new()");
|
||||
/// sha.init(None, None).expect("Error with init()");
|
||||
/// ```
|
||||
pub fn init(&mut self) -> Result<(), i32> {
|
||||
let rc = unsafe { ws::wc_InitSha3_384(&mut self.wc_sha3, core::ptr::null_mut(), ws::INVALID_DEVID) };
|
||||
pub fn init(&mut self, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<(), i32> {
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitSha3_384(&mut self.wc_sha3, heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -1048,7 +1256,7 @@ impl SHA3_384 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA3_384;
|
||||
/// let mut sha = SHA3_384::new().expect("Error with new()");
|
||||
/// let mut sha = SHA3_384::new(None, None).expect("Error with new()");
|
||||
/// sha.update(b"input").expect("Error with update()");
|
||||
/// ```
|
||||
pub fn update(&mut self, data: &[u8]) -> Result<(), i32> {
|
||||
@@ -1078,7 +1286,7 @@ impl SHA3_384 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA3_384;
|
||||
/// let mut sha = SHA3_384::new().expect("Error with new()");
|
||||
/// let mut sha = SHA3_384::new(None, None).expect("Error with new()");
|
||||
/// sha.update(b"input").expect("Error with update()");
|
||||
/// let mut hash = [0u8; SHA3_384::DIGEST_SIZE];
|
||||
/// sha.finalize(&mut hash).expect("Error with finalize()");
|
||||
@@ -1121,6 +1329,11 @@ impl SHA3_512 {
|
||||
|
||||
/// Build a new SHA3_512 instance.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(sha) containing the SHA3_512 struct instance or Err(e)
|
||||
@@ -1130,11 +1343,19 @@ impl SHA3_512 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA3_512;
|
||||
/// let sha = SHA3_512::new().expect("Error with new()");
|
||||
/// let sha = SHA3_512::new(None, None).expect("Error with new()");
|
||||
/// ```
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_sha3: MaybeUninit<ws::wc_Sha3> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_InitSha3_512(wc_sha3.as_mut_ptr(), core::ptr::null_mut(), ws::INVALID_DEVID) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitSha3_512(wc_sha3.as_mut_ptr(), heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -1148,6 +1369,11 @@ impl SHA3_512 {
|
||||
/// This does not need to be called after `new()`, but should be called
|
||||
/// after a hash calculation to prepare for a new calculation.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
|
||||
@@ -1157,11 +1383,19 @@ impl SHA3_512 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA3_512;
|
||||
/// let mut sha = SHA3_512::new().expect("Error with new()");
|
||||
/// sha.init().expect("Error with init()");
|
||||
/// let mut sha = SHA3_512::new(None, None).expect("Error with new()");
|
||||
/// sha.init(None, None).expect("Error with init()");
|
||||
/// ```
|
||||
pub fn init(&mut self) -> Result<(), i32> {
|
||||
let rc = unsafe { ws::wc_InitSha3_512(&mut self.wc_sha3, core::ptr::null_mut(), ws::INVALID_DEVID) };
|
||||
pub fn init(&mut self, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<(), i32> {
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe { ws::wc_InitSha3_512(&mut self.wc_sha3, heap, dev_id) };
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -1183,7 +1417,7 @@ impl SHA3_512 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA3_512;
|
||||
/// let mut sha = SHA3_512::new().expect("Error with new()");
|
||||
/// let mut sha = SHA3_512::new(None, None).expect("Error with new()");
|
||||
/// sha.update(b"input").expect("Error with update()");
|
||||
/// ```
|
||||
pub fn update(&mut self, data: &[u8]) -> Result<(), i32> {
|
||||
@@ -1213,7 +1447,7 @@ impl SHA3_512 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHA3_512;
|
||||
/// let mut sha = SHA3_512::new().expect("Error with new()");
|
||||
/// let mut sha = SHA3_512::new(None, None).expect("Error with new()");
|
||||
/// sha.update(b"input").expect("Error with update()");
|
||||
/// let mut hash = [0u8; SHA3_512::DIGEST_SIZE];
|
||||
/// sha.finalize(&mut hash).expect("Error with finalize()");
|
||||
@@ -1256,6 +1490,11 @@ impl SHAKE128 {
|
||||
|
||||
/// Build a new SHAKE128 instance.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(sha) containing the SHAKE128 struct instance or Err(e)
|
||||
@@ -1265,11 +1504,21 @@ impl SHAKE128 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHAKE128;
|
||||
/// let sha = SHAKE128::new().expect("Error with new()");
|
||||
/// let sha = SHAKE128::new(None, None).expect("Error with new()");
|
||||
/// ```
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_shake: MaybeUninit<ws::wc_Shake> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_InitShake128(wc_shake.as_mut_ptr(), core::ptr::null_mut(), ws::INVALID_DEVID) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe {
|
||||
ws::wc_InitShake128(wc_shake.as_mut_ptr(), heap, dev_id)
|
||||
};
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -1283,6 +1532,11 @@ impl SHAKE128 {
|
||||
/// This does not need to be called after `new()`, but should be called
|
||||
/// after a hash calculation to prepare for a new calculation.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
|
||||
@@ -1292,11 +1546,21 @@ impl SHAKE128 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHAKE128;
|
||||
/// let mut sha = SHAKE128::new().expect("Error with new()");
|
||||
/// sha.init().expect("Error with init()");
|
||||
/// let mut sha = SHAKE128::new(None, None).expect("Error with new()");
|
||||
/// sha.init(None, None).expect("Error with init()");
|
||||
/// ```
|
||||
pub fn init(&mut self) -> Result<(), i32> {
|
||||
let rc = unsafe { ws::wc_InitShake128(&mut self.wc_shake, core::ptr::null_mut(), ws::INVALID_DEVID) };
|
||||
pub fn init(&mut self, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<(), i32> {
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe {
|
||||
ws::wc_InitShake128(&mut self.wc_shake, heap, dev_id)
|
||||
};
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -1318,7 +1582,7 @@ impl SHAKE128 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHAKE128;
|
||||
/// let mut sha = SHAKE128::new().expect("Error with new()");
|
||||
/// let mut sha = SHAKE128::new(None, None).expect("Error with new()");
|
||||
/// sha.update(b"input").expect("Error with update()");
|
||||
/// ```
|
||||
pub fn update(&mut self, data: &[u8]) -> Result<(), i32> {
|
||||
@@ -1347,7 +1611,7 @@ impl SHAKE128 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHAKE128;
|
||||
/// let mut sha = SHAKE128::new().expect("Error with new()");
|
||||
/// let mut sha = SHAKE128::new(None, None).expect("Error with new()");
|
||||
/// sha.update(b"input").expect("Error with update()");
|
||||
/// let mut hash = [0u8; 32];
|
||||
/// sha.finalize(&mut hash).expect("Error with finalize()");
|
||||
@@ -1378,7 +1642,7 @@ impl SHAKE128 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHAKE128;
|
||||
/// let mut sha = SHAKE128::new().expect("Error with new()");
|
||||
/// let mut sha = SHAKE128::new(None, None).expect("Error with new()");
|
||||
/// sha.absorb(b"input").expect("Error with absorb()");
|
||||
/// ```
|
||||
pub fn absorb(&mut self, data: &[u8]) -> Result<(), i32> {
|
||||
@@ -1409,7 +1673,7 @@ impl SHAKE128 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHAKE128;
|
||||
/// let mut sha = SHAKE128::new().expect("Error with new()");
|
||||
/// let mut sha = SHAKE128::new(None, None).expect("Error with new()");
|
||||
/// sha.absorb(b"input").expect("Error with absorb()");
|
||||
/// let mut buffer = [0u8; 2 * SHAKE128::SQUEEZE_BLOCK_SIZE];
|
||||
/// sha.squeeze_blocks(&mut buffer).expect("Error with squeeze_blocks()");
|
||||
@@ -1454,6 +1718,11 @@ impl SHAKE256 {
|
||||
|
||||
/// Build a new SHAKE256 instance.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(sha) containing the SHAKE256 struct instance or Err(e)
|
||||
@@ -1463,11 +1732,21 @@ impl SHAKE256 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHAKE256;
|
||||
/// let sha = SHAKE256::new().expect("Error with new()");
|
||||
/// let sha = SHAKE256::new(None, None).expect("Error with new()");
|
||||
/// ```
|
||||
pub fn new() -> Result<Self, i32> {
|
||||
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
|
||||
let mut wc_shake: MaybeUninit<ws::wc_Shake> = MaybeUninit::uninit();
|
||||
let rc = unsafe { ws::wc_InitShake256(wc_shake.as_mut_ptr(), core::ptr::null_mut(), ws::INVALID_DEVID) };
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe {
|
||||
ws::wc_InitShake256(wc_shake.as_mut_ptr(), heap, dev_id)
|
||||
};
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -1481,6 +1760,11 @@ impl SHAKE256 {
|
||||
/// This does not need to be called after `new()`, but should be called
|
||||
/// after a hash calculation to prepare for a new calculation.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// * `heap`: Optional heap hint.
|
||||
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
|
||||
@@ -1490,11 +1774,21 @@ impl SHAKE256 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHAKE256;
|
||||
/// let mut sha = SHAKE256::new().expect("Error with new()");
|
||||
/// sha.init().expect("Error with init()");
|
||||
/// let mut sha = SHAKE256::new(None, None).expect("Error with new()");
|
||||
/// sha.init(None, None).expect("Error with init()");
|
||||
/// ```
|
||||
pub fn init(&mut self) -> Result<(), i32> {
|
||||
let rc = unsafe { ws::wc_InitShake256(&mut self.wc_shake, core::ptr::null_mut(), ws::INVALID_DEVID) };
|
||||
pub fn init(&mut self, heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<(), i32> {
|
||||
let heap = match heap {
|
||||
Some(heap) => heap,
|
||||
None => core::ptr::null_mut(),
|
||||
};
|
||||
let dev_id = match dev_id {
|
||||
Some(dev_id) => dev_id,
|
||||
None => ws::INVALID_DEVID,
|
||||
};
|
||||
let rc = unsafe {
|
||||
ws::wc_InitShake256(&mut self.wc_shake, heap, dev_id)
|
||||
};
|
||||
if rc != 0 {
|
||||
return Err(rc);
|
||||
}
|
||||
@@ -1516,7 +1810,7 @@ impl SHAKE256 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHAKE256;
|
||||
/// let mut sha = SHAKE256::new().expect("Error with new()");
|
||||
/// let mut sha = SHAKE256::new(None, None).expect("Error with new()");
|
||||
/// sha.update(b"input").expect("Error with update()");
|
||||
/// ```
|
||||
pub fn update(&mut self, data: &[u8]) -> Result<(), i32> {
|
||||
@@ -1545,7 +1839,7 @@ impl SHAKE256 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHAKE256;
|
||||
/// let mut sha = SHAKE256::new().expect("Error with new()");
|
||||
/// let mut sha = SHAKE256::new(None, None).expect("Error with new()");
|
||||
/// sha.update(b"input").expect("Error with update()");
|
||||
/// let mut hash = [0u8; 32];
|
||||
/// sha.finalize(&mut hash).expect("Error with finalize()");
|
||||
@@ -1576,7 +1870,7 @@ impl SHAKE256 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHAKE256;
|
||||
/// let mut sha = SHAKE256::new().expect("Error with new()");
|
||||
/// let mut sha = SHAKE256::new(None, None).expect("Error with new()");
|
||||
/// sha.absorb(b"input").expect("Error with absorb()");
|
||||
/// ```
|
||||
pub fn absorb(&mut self, data: &[u8]) -> Result<(), i32> {
|
||||
@@ -1607,7 +1901,7 @@ impl SHAKE256 {
|
||||
///
|
||||
/// ```rust
|
||||
/// use wolfssl::wolfcrypt::sha::SHAKE256;
|
||||
/// let mut sha = SHAKE256::new().expect("Error with new()");
|
||||
/// let mut sha = SHAKE256::new(None, None).expect("Error with new()");
|
||||
/// sha.absorb(b"input").expect("Error with absorb()");
|
||||
/// let mut buffer = [0u8; 2 * SHAKE256::SQUEEZE_BLOCK_SIZE];
|
||||
/// sha.squeeze_blocks(&mut buffer).expect("Error with squeeze_blocks()");
|
||||
|
||||
@@ -53,7 +53,7 @@ const BIG_MSG: [u8; 384] = [
|
||||
|
||||
#[test]
|
||||
fn test_cbc_encrypt_decrypt() {
|
||||
let mut cbc = CBC::new().expect("Failed to create CBC");
|
||||
let mut cbc = CBC::new(None, None).expect("Failed to create CBC");
|
||||
let key: &[u8; 16] = b"0123456789abcdef";
|
||||
let iv: &[u8; 16] = b"1234567890abcdef";
|
||||
let msg: [u8; 16] = [
|
||||
@@ -76,7 +76,7 @@ fn test_cbc_encrypt_decrypt() {
|
||||
|
||||
#[test]
|
||||
fn test_cbc_big_msg() {
|
||||
let mut cbc = CBC::new().expect("Failed to create CBC");
|
||||
let mut cbc = CBC::new(None, None).expect("Failed to create CBC");
|
||||
let big_key = b"0123456789abcdeffedcba9876543210";
|
||||
let iv: &[u8; 16] = b"1234567890abcdef";
|
||||
let mut big_cipher: [u8; 384] = [0; 384];
|
||||
@@ -146,7 +146,7 @@ fn test_ccm_encrypt_decrypt() {
|
||||
0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
|
||||
];
|
||||
|
||||
let mut ccm = CCM::new().expect("Failed to create CCM");
|
||||
let mut ccm = CCM::new(None, None).expect("Failed to create CCM");
|
||||
ccm.init(&key).expect("Error with init()");
|
||||
let mut auth_tag_out: [u8; 8] = [0; 8];
|
||||
let mut cipher_out: [u8; 23] = [0; 23];
|
||||
@@ -176,7 +176,7 @@ fn test_ccm_encrypt_decrypt() {
|
||||
|
||||
#[test]
|
||||
fn test_ccm_big_msg() {
|
||||
let mut ccm = CCM::new().expect("Failed to create CCM");
|
||||
let mut ccm = CCM::new(None, None).expect("Failed to create CCM");
|
||||
let big_key = b"0123456789abcdeffedcba9876543210";
|
||||
let nonce: [u8; 7] = [0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00];
|
||||
let auth_data: [u8; 8] = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
|
||||
@@ -194,7 +194,7 @@ fn test_ccm_big_msg() {
|
||||
|
||||
#[test]
|
||||
fn test_cfb_encrypt_decrypt() {
|
||||
let mut cfb = CFB::new().expect("Failed to create CFB");
|
||||
let mut cfb = CFB::new(None, None).expect("Failed to create CFB");
|
||||
let key: [u8; 16] = [
|
||||
0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
|
||||
0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
|
||||
@@ -233,7 +233,7 @@ fn test_cfb_encrypt_decrypt() {
|
||||
|
||||
#[test]
|
||||
fn test_cfb_big_msg() {
|
||||
let mut cfb = CFB::new().expect("Failed to create CFB");
|
||||
let mut cfb = CFB::new(None, None).expect("Failed to create CFB");
|
||||
let big_key = b"0123456789abcdeffedcba9876543210";
|
||||
let iv: [u8; 16] = [
|
||||
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
|
||||
@@ -278,7 +278,7 @@ fn test_ctr_encrypt_decrypt() {
|
||||
0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
|
||||
0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
|
||||
];
|
||||
let mut ctr = CTR::new().expect("Failed to create CTR");
|
||||
let mut ctr = CTR::new(None, None).expect("Failed to create CTR");
|
||||
ctr.init(&key, &iv).expect("Error with init()");
|
||||
let mut outbuf: [u8; 64] = [0; 64];
|
||||
ctr.encrypt(&msg, &mut outbuf).expect("Error with encrypt()");
|
||||
@@ -291,7 +291,7 @@ fn test_ctr_encrypt_decrypt() {
|
||||
|
||||
#[test]
|
||||
fn test_ctr_big_msg() {
|
||||
let mut ctr = CTR::new().expect("Failed to create CTR");
|
||||
let mut ctr = CTR::new(None, None).expect("Failed to create CTR");
|
||||
let big_key = b"0123456789abcdeffedcba9876543210";
|
||||
let iv: [u8; 16] = [
|
||||
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
|
||||
@@ -345,7 +345,7 @@ fn test_eax_one_shot_encrypt_decrypt() {
|
||||
|
||||
#[test]
|
||||
fn test_ecb_encrypt_decrypt() {
|
||||
let mut ecb = ECB::new().expect("Failed to create ECB");
|
||||
let mut ecb = ECB::new(None, None).expect("Failed to create ECB");
|
||||
let key_128: &[u8; 16] = b"0123456789abcdef";
|
||||
let msg: [u8; 16] = [
|
||||
0x6e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
|
||||
@@ -395,7 +395,7 @@ fn test_gcm_encrypt_decrypt() {
|
||||
0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
|
||||
0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
|
||||
];
|
||||
let mut gcm = GCM::new().expect("Failed to create GCM");
|
||||
let mut gcm = GCM::new(None, None).expect("Failed to create GCM");
|
||||
gcm.init(&key).expect("Error with init()");
|
||||
let mut cipher: [u8; 32] = [0; 32];
|
||||
let mut auth_tag: [u8; 16] = [0; 16];
|
||||
@@ -448,7 +448,7 @@ fn test_gcmstream_encrypt_decrypt() {
|
||||
0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
|
||||
0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
|
||||
];
|
||||
let mut gcmstream = GCMStream::new().expect("Failed to create GCMStream");
|
||||
let mut gcmstream = GCMStream::new(None, None).expect("Failed to create GCMStream");
|
||||
for chunk_size in 1..=auth.len() {
|
||||
gcmstream.init(&key, &iv).expect("Error with init()");
|
||||
let mut cipher: [u8; 60] = [0; 60];
|
||||
@@ -505,7 +505,7 @@ fn test_ofb_encrypt_decrypt() {
|
||||
0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
|
||||
0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
|
||||
];
|
||||
let mut ofb = OFB::new().expect("Failed to create OFB");
|
||||
let mut ofb = OFB::new(None, None).expect("Failed to create OFB");
|
||||
ofb.init(&key, &iv).expect("Error with init()");
|
||||
let mut cipher: [u8; 48] = [0; 48];
|
||||
ofb.encrypt(&plain, &mut cipher).expect("Error with encrypt()");
|
||||
@@ -547,7 +547,7 @@ fn test_xts_one_shot() {
|
||||
0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a
|
||||
];
|
||||
|
||||
let mut xts = XTS::new().expect("Failed to create XTS");
|
||||
let mut xts = XTS::new(None, None).expect("Failed to create XTS");
|
||||
xts.init_encrypt(&key).expect("Error with init_encrypt()");
|
||||
let mut cipher: [u8; 16] = [0; 16];
|
||||
xts.encrypt(&plain, &mut cipher, &tweak).expect("Error with encrypt()");
|
||||
@@ -585,7 +585,7 @@ fn test_xts_sector_128() {
|
||||
];
|
||||
let sector = 141;
|
||||
|
||||
let mut xts = XTS::new().expect("Failed to create XTS");
|
||||
let mut xts = XTS::new(None, None).expect("Failed to create XTS");
|
||||
xts.init_encrypt(&keys).expect("Error with init_encrypt()");
|
||||
let mut cipher: [u8; 16] = [0; 16];
|
||||
xts.encrypt_sector(&plain, &mut cipher, sector).expect("Error with encrypt_sector()");
|
||||
@@ -623,7 +623,7 @@ fn test_xts_sector_256() {
|
||||
];
|
||||
let sector = 187;
|
||||
|
||||
let mut xts = XTS::new().expect("Failed to create XTS");
|
||||
let mut xts = XTS::new(None, None).expect("Failed to create XTS");
|
||||
xts.init_encrypt(&keys).expect("Error with init_encrypt()");
|
||||
let mut cipher: [u8; 32] = [0; 32];
|
||||
xts.encrypt_sector(&plain, &mut cipher, sector).expect("Error with encrypt_sector()");
|
||||
@@ -745,7 +745,7 @@ fn test_xts_consecutive_sectors() {
|
||||
let sector = 0x000000ffffffffff;
|
||||
let sector_size = 512;
|
||||
|
||||
let mut xts = XTS::new().expect("Failed to create XTS");
|
||||
let mut xts = XTS::new(None, None).expect("Failed to create XTS");
|
||||
xts.init_encrypt(&keys).expect("Error with init_encrypt()");
|
||||
let mut cipher: [u8; 544] = [0; 544];
|
||||
xts.encrypt_consecutive_sectors(&plain, &mut cipher, sector, sector_size).expect("Error with encrypt_consecutive_sectors()");
|
||||
@@ -784,7 +784,7 @@ fn test_xtsstream() {
|
||||
0xB5, 0x5A, 0xDD, 0xCB, 0x80, 0xE0, 0xFC, 0xCD
|
||||
];
|
||||
|
||||
let mut xtsstream = XTSStream::new().expect("Failed to create XTSStream");
|
||||
let mut xtsstream = XTSStream::new(None, None).expect("Failed to create XTSStream");
|
||||
xtsstream.init_encrypt(&keys, &tweak).expect("Error with init_encrypt()");
|
||||
let mut cipher: [u8; 40] = [0; 40];
|
||||
xtsstream.encrypt_update(&plain[0..16], &mut cipher[0..16]).expect("Error with encrypt_update()");
|
||||
@@ -811,7 +811,7 @@ fn test_xtsstream_big_msg() {
|
||||
0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
|
||||
];
|
||||
|
||||
let mut xtsstream = XTSStream::new().expect("Failed to create XTSStream");
|
||||
let mut xtsstream = XTSStream::new(None, None).expect("Failed to create XTSStream");
|
||||
xtsstream.init_encrypt(&key, &tweak).expect("Error with init_encrypt()");
|
||||
let mut cipher: [u8; 384] = [0; 384];
|
||||
let mut i = 0;
|
||||
|
||||
@@ -14,7 +14,7 @@ fn test_cmac() {
|
||||
0x07u8, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
|
||||
0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
|
||||
];
|
||||
let mut cmac = CMAC::new(&key).expect("Error with new()");
|
||||
let mut cmac = CMAC::new(&key, None, None).expect("Error with new()");
|
||||
cmac.update(&message).expect("Error with update()");
|
||||
let mut finalize_out = [0u8; 16];
|
||||
cmac.finalize(&mut finalize_out).expect("Error with finalize()");
|
||||
@@ -25,4 +25,11 @@ fn test_cmac() {
|
||||
assert_eq!(generate_out, finalize_out);
|
||||
let valid = CMAC::verify(&key, &message, &generate_out).expect("Error with verify()");
|
||||
assert!(valid);
|
||||
|
||||
let mut cmac = CMAC::new(&key, None, None).expect("Error with new()");
|
||||
let mut generate_out = [0u8; 16];
|
||||
cmac.generate_ex(&key, &message, &mut generate_out, None, None).expect("Error with generate_ex()");
|
||||
assert_eq!(generate_out, finalize_out);
|
||||
let valid = cmac.verify_ex(&key, &message, &generate_out, None, None).expect("Error with verify_ex()");
|
||||
assert!(valid);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ fn test_dh_named_parameters() {
|
||||
|
||||
assert!(p_size > 0u32);
|
||||
assert!(g_size > 0u32);
|
||||
let mut dh = DH::new_named(DH::FFDHE_2048).expect("Error with new_named()");
|
||||
let mut dh = DH::new_named(DH::FFDHE_2048, None, None).expect("Error with new_named()");
|
||||
|
||||
let mut p = [0u8; 256];
|
||||
let mut q = [0u8; 256];
|
||||
@@ -27,7 +27,7 @@ fn test_dh_named_parameters() {
|
||||
#[test]
|
||||
fn test_generate_params() {
|
||||
let mut rng = RNG::new().expect("Error with RNG::new()");
|
||||
let mut dh = DH::generate(&mut rng, 2048).expect("Error with generate()");
|
||||
let mut dh = DH::generate(&mut rng, 2048, None, None).expect("Error with generate()");
|
||||
|
||||
let mut private = [0u8; 256];
|
||||
let mut private_size = 0u32;
|
||||
@@ -39,7 +39,7 @@ fn test_generate_params() {
|
||||
#[test]
|
||||
fn test_generate_key_pair() {
|
||||
let mut rng = RNG::new().expect("Error with RNG::new()");
|
||||
let mut dh = DH::new_named(DH::FFDHE_2048).expect("Error with new_named()");
|
||||
let mut dh = DH::new_named(DH::FFDHE_2048, None, None).expect("Error with new_named()");
|
||||
|
||||
let mut p = [0u8; 256];
|
||||
let mut q = [0u8; 256];
|
||||
@@ -150,9 +150,9 @@ fn test_dh_checks() {
|
||||
0x40, 0x52, 0xed, 0x41
|
||||
];
|
||||
let mut rng = RNG::new().expect("Error with RNG::new()");
|
||||
let _dh = DH::new_from_pg(&p, &g).expect("Error with new_from_pg()");
|
||||
let _dh = DH::new_from_pgq(&p, &g, &q).expect("Error with new_from_pgq()");
|
||||
let mut dh = DH::new_from_pgq_with_check(&p, &g, &q, 0, &mut rng).expect("Error with new_from_pgq()");
|
||||
let _dh = DH::new_from_pg(&p, &g, None, None).expect("Error with new_from_pg()");
|
||||
let _dh = DH::new_from_pgq(&p, &g, &q, None, None).expect("Error with new_from_pgq()");
|
||||
let mut dh = DH::new_from_pgq_with_check(&p, &g, &q, 0, &mut rng, None, None).expect("Error with new_from_pgq()");
|
||||
let mut private = [0u8; 256];
|
||||
let mut private_size = 0u32;
|
||||
let mut public = [0u8; 256];
|
||||
@@ -169,7 +169,7 @@ fn test_dh_checks() {
|
||||
#[test]
|
||||
fn test_dh_shared_secret() {
|
||||
let mut rng = RNG::new().expect("Error with RNG::new()");
|
||||
let mut dh = DH::new_named(DH::FFDHE_2048).expect("Error with new_named()");
|
||||
let mut dh = DH::new_named(DH::FFDHE_2048, None, None).expect("Error with new_named()");
|
||||
|
||||
let mut private0 = [0u8; 256];
|
||||
let mut private0_size = 0u32;
|
||||
|
||||
@@ -5,7 +5,7 @@ use wolfssl::wolfcrypt::random::RNG;
|
||||
#[test]
|
||||
fn test_ecc_generate() {
|
||||
let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
let mut ecc = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
ecc.check().expect("Error with check()");
|
||||
}
|
||||
|
||||
@@ -15,13 +15,13 @@ fn test_ecc_generate_ex() {
|
||||
let curve_id = ECC::SECP256R1;
|
||||
let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
|
||||
assert_eq!(curve_size, 32);
|
||||
let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id).expect("Error with generate_ex()");
|
||||
let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id, None, None).expect("Error with generate_ex()");
|
||||
ecc.check().expect("Error with check()");
|
||||
|
||||
let mut x963 = [0u8; 128];
|
||||
let x963_size = ecc.export_x963(&mut x963).expect("Error with export_x963()");
|
||||
let x963 = &x963[0..x963_size];
|
||||
let mut ecc = ECC::import_x963_ex(x963, ECC::SECP256R1).expect("Error with import_x963_ex");
|
||||
let mut ecc = ECC::import_x963_ex(x963, ECC::SECP256R1, None, None).expect("Error with import_x963_ex");
|
||||
ecc.check().expect("Error with check()");
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ fn test_ecc_generate_ex2() {
|
||||
let curve_id = ECC::SECP256R1;
|
||||
let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
|
||||
assert_eq!(curve_size, 32);
|
||||
let mut ecc = ECC::generate_ex2(curve_size, &mut rng, curve_id, ECC::FLAG_COFACTOR).expect("Error with generate_ex2()");
|
||||
let mut ecc = ECC::generate_ex2(curve_size, &mut rng, curve_id, ECC::FLAG_COFACTOR, None, None).expect("Error with generate_ex2()");
|
||||
ecc.check().expect("Error with check()");
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ fn test_ecc_import_export_sign_verify() {
|
||||
let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
let key_path = "../../../certs/ecc-client-key.der";
|
||||
let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
let mut ecc = ECC::import_der(&der).expect("Error with import_der()");
|
||||
let mut ecc = ECC::import_der(&der, None, None).expect("Error with import_der()");
|
||||
let hash = [0x42u8; 32];
|
||||
let mut signature = [0u8; 128];
|
||||
let signature_length = ecc.sign_hash(&hash, &mut signature, &mut rng).expect("Error with sign_hash()");
|
||||
@@ -58,21 +58,21 @@ fn test_ecc_import_export_sign_verify() {
|
||||
let signature = &mut signature[0..signature_length];
|
||||
let key_path = "../../../certs/ecc-client-keyPub.der";
|
||||
let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
let mut ecc = ECC::import_public_der(&der).expect("Error with import_public_der()");
|
||||
let mut ecc = ECC::import_public_der(&der, None, None).expect("Error with import_public_der()");
|
||||
let valid = ecc.verify_hash(&signature, &hash).expect("Error with verify_hash()");
|
||||
assert_eq!(valid, true);
|
||||
|
||||
let mut x963 = [0u8; 128];
|
||||
let x963_size = ecc.export_x963(&mut x963).expect("Error with export_x963()");
|
||||
let x963 = &x963[0..x963_size];
|
||||
let mut ecc = ECC::import_x963(x963).expect("Error with import_x963");
|
||||
let mut ecc = ECC::import_x963(x963, None, None).expect("Error with import_x963");
|
||||
let valid = ecc.verify_hash(&signature, &hash).expect("Error with verify_hash()");
|
||||
assert_eq!(valid, true);
|
||||
|
||||
let mut x963 = [0u8; 128];
|
||||
let x963_size = ecc.export_x963_compressed(&mut x963).expect("Error with export_x963_compressed()");
|
||||
let x963 = &x963[0..x963_size];
|
||||
let mut ecc = ECC::import_x963(x963).expect("Error with import_x963");
|
||||
let mut ecc = ECC::import_x963(x963, None, None).expect("Error with import_x963");
|
||||
let valid = ecc.verify_hash(&signature, &hash).expect("Error with verify_hash()");
|
||||
assert_eq!(valid, true);
|
||||
|
||||
@@ -106,8 +106,8 @@ fn test_ecc_import_export_sign_verify() {
|
||||
#[test]
|
||||
fn test_ecc_shared_secret() {
|
||||
let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
let mut ecc0 = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
let mut ecc1 = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
let mut ecc0 = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
let mut ecc1 = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
let mut ss0 = [0u8; 128];
|
||||
let mut ss1 = [0u8; 128];
|
||||
let ss0_size = ecc0.shared_secret(&mut ecc1, &mut ss0).expect("Error with shared_secret()");
|
||||
@@ -118,7 +118,7 @@ fn test_ecc_shared_secret() {
|
||||
assert_eq!(*ss0, *ss1);
|
||||
|
||||
let mut ss0 = [0u8; 128];
|
||||
let ecc_point = ecc1.make_pub_to_point(None).expect("Error with make_pub_to_point()");
|
||||
let ecc_point = ecc1.make_pub_to_point(None, None).expect("Error with make_pub_to_point()");
|
||||
let ss0_size = ecc0.shared_secret_ex(&ecc_point, &mut ss0).expect("Error with shared_secret_ex()");
|
||||
let ss0 = &ss0[0..ss0_size];
|
||||
assert_eq!(*ss0, *ss1);
|
||||
@@ -127,7 +127,7 @@ fn test_ecc_shared_secret() {
|
||||
#[test]
|
||||
fn test_ecc_export() {
|
||||
let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
let mut ecc = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
let mut qx = [0u8; 32];
|
||||
let mut qx_len = 0u32;
|
||||
let mut qy = [0u8; 32];
|
||||
@@ -140,7 +140,7 @@ fn test_ecc_export() {
|
||||
#[test]
|
||||
fn test_ecc_export_ex() {
|
||||
let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
let mut ecc = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
let mut qx = [0u8; 32];
|
||||
let mut qx_len = 0u32;
|
||||
let mut qy = [0u8; 32];
|
||||
@@ -153,7 +153,7 @@ fn test_ecc_export_ex() {
|
||||
#[test]
|
||||
fn test_ecc_import_export_private() {
|
||||
let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
let mut ecc = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
let hash = [0x42u8; 32];
|
||||
let mut signature = [0u8; 128];
|
||||
let signature_length = ecc.sign_hash(&hash, &mut signature, &mut rng).expect("Error with sign_hash()");
|
||||
@@ -166,7 +166,7 @@ fn test_ecc_import_export_private() {
|
||||
let x963_size = ecc.export_x963(&mut x963).expect("Error with export_x963()");
|
||||
let x963 = &x963[0..x963_size];
|
||||
|
||||
let mut ecc2 = ECC::import_private_key(&d, x963).expect("Error with import_private_key()");
|
||||
let mut ecc2 = ECC::import_private_key(&d, x963, None, None).expect("Error with import_private_key()");
|
||||
let valid = ecc2.verify_hash(&signature, &hash).expect("Error with verify_hash()");
|
||||
assert_eq!(valid, true);
|
||||
}
|
||||
@@ -176,7 +176,7 @@ fn test_ecc_import_export_private_ex() {
|
||||
let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
let curve_id = ECC::SECP256R1;
|
||||
let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
|
||||
let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id).expect("Error with generate_ex()");
|
||||
let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id, None, None).expect("Error with generate_ex()");
|
||||
let hash = [0x42u8; 32];
|
||||
let mut signature = [0u8; 128];
|
||||
let signature_length = ecc.sign_hash(&hash, &mut signature, &mut rng).expect("Error with sign_hash()");
|
||||
@@ -189,7 +189,7 @@ fn test_ecc_import_export_private_ex() {
|
||||
let x963_size = ecc.export_x963(&mut x963).expect("Error with export_x963()");
|
||||
let x963 = &x963[0..x963_size];
|
||||
|
||||
let mut ecc2 = ECC::import_private_key_ex(&d, x963, curve_id).expect("Error with import_private_key_ex()");
|
||||
let mut ecc2 = ECC::import_private_key_ex(&d, x963, curve_id, None, None).expect("Error with import_private_key_ex()");
|
||||
let valid = ecc2.verify_hash(&signature, &hash).expect("Error with verify_hash()");
|
||||
assert_eq!(valid, true);
|
||||
}
|
||||
@@ -197,7 +197,7 @@ fn test_ecc_import_export_private_ex() {
|
||||
#[test]
|
||||
fn test_ecc_export_public() {
|
||||
let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
let mut ecc = ECC::generate(32, &mut rng).expect("Error with generate()");
|
||||
let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
|
||||
let mut qx = [0u8; 32];
|
||||
let mut qx_len = 0u32;
|
||||
let mut qy = [0u8; 32];
|
||||
@@ -210,7 +210,7 @@ fn test_ecc_import_unsigned() {
|
||||
let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
let curve_id = ECC::SECP256R1;
|
||||
let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
|
||||
let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id).expect("Error with generate()");
|
||||
let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id, None, None).expect("Error with generate()");
|
||||
let mut qx = [0u8; 32];
|
||||
let mut qx_len = 0u32;
|
||||
let mut qy = [0u8; 32];
|
||||
@@ -219,7 +219,7 @@ fn test_ecc_import_unsigned() {
|
||||
let mut d_len = 0u32;
|
||||
ecc.export_ex(&mut qx, &mut qx_len, &mut qy, &mut qy_len, &mut d, &mut d_len, false).expect("Error with export_ex()");
|
||||
|
||||
let mut ecc2 = ECC::import_unsigned(&qx, &qy, &d, curve_id).expect("Error with import_unsigned()");
|
||||
let mut ecc2 = ECC::import_unsigned(&qx, &qy, &d, curve_id, None, None).expect("Error with import_unsigned()");
|
||||
|
||||
let hash = [0x42u8; 32];
|
||||
let mut signature = [0u8; 128];
|
||||
@@ -234,11 +234,11 @@ fn test_ecc_make_pub() {
|
||||
let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
let key_path = "../../../certs/ecc-client-key.der";
|
||||
let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
let mut ecc = ECC::import_der(&der).expect("Error with import_der()");
|
||||
let mut ecc = ECC::import_der(&der, None, None).expect("Error with import_der()");
|
||||
ecc.make_pub(Some(&mut rng)).expect("Error with make_pub()");
|
||||
ecc.make_pub(None).expect("Error with make_pub()");
|
||||
ecc.make_pub_to_point(Some(&mut rng)).expect("Error with make_pub_to_point()");
|
||||
ecc.make_pub_to_point(None).expect("Error with make_pub_to_point()");
|
||||
ecc.make_pub_to_point(Some(&mut rng), None).expect("Error with make_pub_to_point()");
|
||||
ecc.make_pub_to_point(None, None).expect("Error with make_pub_to_point()");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -246,14 +246,14 @@ fn test_ecc_point() {
|
||||
let mut rng = RNG::new().expect("Failed to create RNG");
|
||||
let curve_id = ECC::SECP256R1;
|
||||
let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
|
||||
let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id).expect("Error with generate()");
|
||||
let mut ecc_point = ecc.make_pub_to_point(Some(&mut rng)).expect("Error with make_pub_to_point()");
|
||||
let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id, None, None).expect("Error with generate()");
|
||||
let mut ecc_point = ecc.make_pub_to_point(Some(&mut rng), None).expect("Error with make_pub_to_point()");
|
||||
let mut der = [0u8; 128];
|
||||
let size = ecc_point.export_der(&mut der, curve_id).expect("Error with export_der()");
|
||||
assert!(size > 0 && size <= der.len());
|
||||
ECCPoint::import_der(&der[0..size], curve_id).expect("Error with import_der()");
|
||||
ECCPoint::import_der(&der[0..size], curve_id, None).expect("Error with import_der()");
|
||||
let size = ecc_point.export_der_compressed(&mut der, curve_id).expect("Error with export_der_compressed()");
|
||||
ECCPoint::import_der_ex(&der[0..size], curve_id, 1).expect("Error with import_der_ex()");
|
||||
ECCPoint::import_der_ex(&der[0..size], curve_id, 1, None).expect("Error with import_der_ex()");
|
||||
ecc_point.forcezero();
|
||||
}
|
||||
|
||||
@@ -262,6 +262,6 @@ fn test_ecc_import() {
|
||||
let qx = b"7a4e287890a1a47ad3457e52f2f76a83ce46cbc947616d0cbaa82323818a793d\0";
|
||||
let qy = b"eec4084f5b29ebf29c44cce3b3059610922f8b30ea6e8811742ac7238fe87308\0";
|
||||
let d = b"8c14b793cb19137e323a6d2e2a870bca2e7a493ec1153b3a95feb8a4873f8d08\0";
|
||||
ECC::import_raw(qx, qy, d, b"SECP256R1\0").expect("Error with import_raw()");
|
||||
ECC::import_raw_ex(qx, qy, d, ECC::SECP256R1).expect("Error with import_raw_ex()");
|
||||
ECC::import_raw(qx, qy, d, b"SECP256R1\0", None, None).expect("Error with import_raw()");
|
||||
ECC::import_raw_ex(qx, qy, d, ECC::SECP256R1, None, None).expect("Error with import_raw_ex()");
|
||||
}
|
||||
|
||||
@@ -4,10 +4,10 @@ use wolfssl::wolfcrypt::ed25519::*;
|
||||
#[test]
|
||||
fn test_make_public() {
|
||||
let mut rng = RNG::new().expect("Error creating RNG");
|
||||
let ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
let ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
let mut private = [0u8; Ed25519::KEY_SIZE];
|
||||
ed.export_private_only(&mut private).expect("Error with export_private_only()");
|
||||
let mut ed = Ed25519::new().expect("Error with new()");
|
||||
let mut ed = Ed25519::new(None, None).expect("Error with new()");
|
||||
ed.import_private_only(&private).expect("Error with import_private_only()");
|
||||
let mut public = [0u8; Ed25519::KEY_SIZE];
|
||||
ed.make_public(&mut public).expect("Error with make_public()");
|
||||
@@ -16,7 +16,7 @@ fn test_make_public() {
|
||||
#[test]
|
||||
fn test_check_key() {
|
||||
let mut rng = RNG::new().expect("Error creating RNG");
|
||||
let mut ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
let mut ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
ed.check_key().expect("Error with check_key()");
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ fn test_sign_verify() {
|
||||
0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
|
||||
];
|
||||
|
||||
let mut ed = Ed25519::new().expect("Error with new()");
|
||||
let mut ed = Ed25519::new(None, None).expect("Error with new()");
|
||||
ed.import_private_key(&private_key, Some(&public_key)).expect("Error with import_private_key()");
|
||||
|
||||
let mut signature = [0u8; Ed25519::SIG_SIZE];
|
||||
@@ -100,7 +100,7 @@ fn test_ctx_sign_verify() {
|
||||
0x1f,0x4b,0x88,0x8e,0x4e,0x7e,0xdb,0x0d
|
||||
];
|
||||
|
||||
let mut ed = Ed25519::new().expect("Error with new()");
|
||||
let mut ed = Ed25519::new(None, None).expect("Error with new()");
|
||||
ed.import_private_key(&private_key, Some(&public_key)).expect("Error with import_private_key()");
|
||||
|
||||
let mut signature = [0u8; Ed25519::SIG_SIZE];
|
||||
@@ -148,7 +148,7 @@ fn test_ph_sign_verify() {
|
||||
0x3f,0x73,0xe6,0xe3,0xba,0xa8,0x1c,0x0e
|
||||
];
|
||||
|
||||
let mut ed = Ed25519::new().expect("Error with new()");
|
||||
let mut ed = Ed25519::new(None, None).expect("Error with new()");
|
||||
ed.import_private_key(&private_key, Some(&public_key)).expect("Error with import_private_key()");
|
||||
|
||||
let mut signature = [0u8; Ed25519::SIG_SIZE];
|
||||
@@ -169,7 +169,7 @@ fn test_ph_sign_verify() {
|
||||
#[test]
|
||||
fn test_import_export() {
|
||||
let mut rng = RNG::new().expect("Error creating RNG");
|
||||
let ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
let ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
|
||||
let mut private = [0u8; Ed25519::PRV_KEY_SIZE];
|
||||
let mut public = [0u8; Ed25519::PUB_KEY_SIZE];
|
||||
@@ -186,10 +186,10 @@ fn test_import_export() {
|
||||
let mut private_only = [0u8; Ed25519::KEY_SIZE];
|
||||
ed.export_private_only(&mut private_only).expect("Error with export_private_only()");
|
||||
|
||||
let mut ed = Ed25519::new().expect("Error with new()");
|
||||
let mut ed = Ed25519::new(None, None).expect("Error with new()");
|
||||
ed.import_private_key_ex(&private, Some(&public), false).expect("Error with import_private_key_ex()");
|
||||
|
||||
let mut ed = Ed25519::new().expect("Error with new()");
|
||||
let mut ed = Ed25519::new(None, None).expect("Error with new()");
|
||||
ed.import_private_only(&private_only).expect("Error with import_private_only()");
|
||||
ed.import_public(&public).expect("Error with import_public()");
|
||||
ed.import_public_ex(&public, false).expect("Error with import_public_ex()");
|
||||
@@ -198,7 +198,7 @@ fn test_import_export() {
|
||||
#[test]
|
||||
fn test_sizes() {
|
||||
let mut rng = RNG::new().expect("Error creating RNG");
|
||||
let ed = Ed25519::generate(&mut rng).expect("Error with generate()");
|
||||
let ed = Ed25519::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
|
||||
let size = ed.size().expect("Error with size()");
|
||||
assert_eq!(size, Ed25519::KEY_SIZE);
|
||||
|
||||
@@ -4,10 +4,10 @@ use wolfssl::wolfcrypt::ed448::*;
|
||||
#[test]
|
||||
fn test_make_public() {
|
||||
let mut rng = RNG::new().expect("Error creating RNG");
|
||||
let ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
let ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
let mut private = [0u8; Ed448::KEY_SIZE];
|
||||
ed.export_private_only(&mut private).expect("Error with export_private_only()");
|
||||
let mut ed = Ed448::new().expect("Error with new()");
|
||||
let mut ed = Ed448::new(None, None).expect("Error with new()");
|
||||
ed.import_private_only(&private).expect("Error with import_private_only()");
|
||||
let mut public = [0u8; Ed448::KEY_SIZE];
|
||||
ed.make_public(&mut public).expect("Error with make_public()");
|
||||
@@ -16,7 +16,7 @@ fn test_make_public() {
|
||||
#[test]
|
||||
fn test_check_key() {
|
||||
let mut rng = RNG::new().expect("Error creating RNG");
|
||||
let mut ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
let mut ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
ed.check_key().expect("Error with check_key()");
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ fn test_sign_verify() {
|
||||
0x3c, 0x00
|
||||
];
|
||||
|
||||
let mut ed = Ed448::new().expect("Error with new()");
|
||||
let mut ed = Ed448::new(None, None).expect("Error with new()");
|
||||
ed.import_private_key(&private_key, Some(&public_key)).expect("Error with import_private_key()");
|
||||
|
||||
let mut signature = [0u8; Ed448::SIG_SIZE];
|
||||
@@ -137,7 +137,7 @@ fn test_ph_sign_verify() {
|
||||
0x21, 0x00
|
||||
];
|
||||
|
||||
let mut ed = Ed448::new().expect("Error with new()");
|
||||
let mut ed = Ed448::new(None, None).expect("Error with new()");
|
||||
ed.import_private_key(&private_key, Some(&public_key)).expect("Error with import_private_key()");
|
||||
|
||||
let mut signature = [0u8; Ed448::SIG_SIZE];
|
||||
@@ -158,7 +158,7 @@ fn test_ph_sign_verify() {
|
||||
#[test]
|
||||
fn test_import_export() {
|
||||
let mut rng = RNG::new().expect("Error creating RNG");
|
||||
let ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
let ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
|
||||
let mut private = [0u8; Ed448::PRV_KEY_SIZE];
|
||||
let mut public = [0u8; Ed448::PUB_KEY_SIZE];
|
||||
@@ -175,10 +175,10 @@ fn test_import_export() {
|
||||
let mut private_only = [0u8; Ed448::KEY_SIZE];
|
||||
ed.export_private_only(&mut private_only).expect("Error with export_private_only()");
|
||||
|
||||
let mut ed = Ed448::new().expect("Error with new()");
|
||||
let mut ed = Ed448::new(None, None).expect("Error with new()");
|
||||
ed.import_private_key_ex(&private, Some(&public), false).expect("Error with import_private_key_ex()");
|
||||
|
||||
let mut ed = Ed448::new().expect("Error with new()");
|
||||
let mut ed = Ed448::new(None, None).expect("Error with new()");
|
||||
ed.import_private_only(&private_only).expect("Error with import_private_only()");
|
||||
ed.import_public(&public).expect("Error with import_public()");
|
||||
ed.import_public_ex(&public, false).expect("Error with import_public_ex()");
|
||||
@@ -187,7 +187,7 @@ fn test_import_export() {
|
||||
#[test]
|
||||
fn test_sizes() {
|
||||
let mut rng = RNG::new().expect("Error creating RNG");
|
||||
let ed = Ed448::generate(&mut rng).expect("Error with generate()");
|
||||
let ed = Ed448::generate(&mut rng, None, None).expect("Error with generate()");
|
||||
|
||||
let size = ed.size().expect("Error with size()");
|
||||
assert_eq!(size, Ed448::KEY_SIZE);
|
||||
|
||||
@@ -8,10 +8,12 @@ fn test_hkdf_extract_expand() {
|
||||
let salt = b"12345678ABCDEFGH";
|
||||
let mut extract_out = [0u8; SHA256::DIGEST_SIZE];
|
||||
hkdf_extract(HMAC::TYPE_SHA256, Some(salt), ikm, &mut extract_out).expect("Error with hkdf_extract()");
|
||||
hkdf_extract_ex(HMAC::TYPE_SHA256, Some(salt), ikm, &mut extract_out, None, None).expect("Error with hkdf_extract_ex()");
|
||||
|
||||
let info = b"0";
|
||||
let mut expand_out = [0u8; 16];
|
||||
hkdf_expand(HMAC::TYPE_SHA256, &extract_out, Some(info), &mut expand_out).expect("Error with hkdf_expand()");
|
||||
hkdf_expand_ex(HMAC::TYPE_SHA256, &extract_out, Some(info), &mut expand_out, None, None).expect("Error with hkdf_expand_ex()");
|
||||
|
||||
let expected_key = [
|
||||
0x17, 0x5F, 0x24, 0xB3, 0x18, 0x20, 0xF3, 0xD4,
|
||||
|
||||
@@ -33,9 +33,9 @@ fn test_hmac_sha256() {
|
||||
for i in 0..keys.len() {
|
||||
let mut hmac =
|
||||
if keys[i].len() < 14 {
|
||||
HMAC::new_allow_short_key(HMAC::TYPE_SHA256, keys[i]).expect("Error with new_allow_short_key()")
|
||||
HMAC::new_allow_short_key(HMAC::TYPE_SHA256, keys[i], None, None).expect("Error with new_allow_short_key()")
|
||||
} else {
|
||||
HMAC::new(HMAC::TYPE_SHA256, keys[i]).expect("Error with new()")
|
||||
HMAC::new(HMAC::TYPE_SHA256, keys[i], None, None).expect("Error with new()")
|
||||
};
|
||||
let hmac_size = hmac.get_hmac_size().expect("Error with get_hmac_size()");
|
||||
assert_eq!(hmac_size, SHA256::DIGEST_SIZE);
|
||||
|
||||
@@ -58,6 +58,7 @@ fn test_tls13_hkdf_extract_expand() {
|
||||
let mut secret = [0u8; SHA256::DIGEST_SIZE];
|
||||
|
||||
tls13_hkdf_extract(HMAC::TYPE_SHA256, None, None, &mut secret).expect("Error with tls13_hkdf_extract()");
|
||||
tls13_hkdf_extract_ex(HMAC::TYPE_SHA256, None, None, &mut secret, None, None).expect("Error with tls13_hkdf_extract_ex()");
|
||||
|
||||
let protocol_label = b"tls13 ";
|
||||
let ce_traffic_label = b"c e traffic";
|
||||
@@ -66,6 +67,9 @@ fn test_tls13_hkdf_extract_expand() {
|
||||
tls13_hkdf_expand_label(HMAC::TYPE_SHA256, &secret,
|
||||
protocol_label, ce_traffic_label,
|
||||
&hash_hello1, &mut expand_out).expect("Error with tls13_hkdf_expand_label()");
|
||||
tls13_hkdf_expand_label_ex(HMAC::TYPE_SHA256, &secret,
|
||||
protocol_label, ce_traffic_label,
|
||||
&hash_hello1, &mut expand_out, None, None).expect("Error with tls13_hkdf_expand_label_ex()");
|
||||
|
||||
assert_eq!(expand_out, client_early_traffic_secret);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
use wolfssl::wolfcrypt::prf::*;
|
||||
use wolfssl_sys as ws;
|
||||
|
||||
#[test]
|
||||
fn test_kdf_prf() {
|
||||
@@ -23,9 +22,7 @@ fn test_kdf_prf() {
|
||||
|
||||
let mut out = [0u8; 12];
|
||||
|
||||
prf(&secret, &seed, PRF_HASH_SHA384,
|
||||
core::ptr::null_mut(), ws::INVALID_DEVID,
|
||||
&mut out).expect("Error with prf()");
|
||||
prf(&secret, &seed, PRF_HASH_SHA384, None, None, &mut out).expect("Error with prf()");
|
||||
|
||||
assert_eq!(out, expected);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,12 @@ fn test_rng_new_and_drop() {
|
||||
let _rng = RNG::new().expect("Failed to create RNG");
|
||||
}
|
||||
|
||||
// Test that RNG::new_ex() returns successfully and that drop() does not panic.
|
||||
#[test]
|
||||
fn test_rng_new_ex_and_drop() {
|
||||
let _rng = RNG::new_ex(None, None).expect("Failed to create RNG");
|
||||
}
|
||||
|
||||
// Test that RNG::new_with_nonce() returns successfully and that drop() does
|
||||
// not panic.
|
||||
#[test]
|
||||
@@ -14,6 +20,14 @@ fn test_rng_new_with_nonce_and_drop() {
|
||||
let _rng = RNG::new_with_nonce(&mut nonce).expect("Failed to create RNG");
|
||||
}
|
||||
|
||||
// Test that RNG::new_with_nonce_ex() returns successfully and that drop() does
|
||||
// not panic.
|
||||
#[test]
|
||||
fn test_rng_new_with_nonce_ex_and_drop() {
|
||||
let mut nonce = [1, 2, 3, 4];
|
||||
let _rng = RNG::new_with_nonce_ex(&mut nonce, None, None).expect("Failed to create RNG");
|
||||
}
|
||||
|
||||
// Test that generate_byte() returns random values.
|
||||
#[test]
|
||||
fn test_rng_generate_byte() {
|
||||
|
||||
@@ -5,7 +5,7 @@ use wolfssl::wolfcrypt::rsa::*;
|
||||
#[test]
|
||||
fn test_rsa_generate() {
|
||||
let mut rng = RNG::new().expect("Error creating RNG");
|
||||
let mut rsa = RSA::generate(2048, 65537, &mut rng).expect("Error with generate()");
|
||||
let mut rsa = RSA::generate(2048, 65537, &mut rng, None, None).expect("Error with generate()");
|
||||
rsa.check().expect("Error with check()");
|
||||
|
||||
let encrypt_size = rsa.get_encrypt_size().expect("Error with get_encrypt_size()");
|
||||
@@ -50,7 +50,7 @@ fn test_rsa_encrypt_decrypt() {
|
||||
let mut rng = RNG::new().expect("Error creating RNG");
|
||||
let key_path = "../../../certs/client-keyPub.der";
|
||||
let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
let mut rsa = RSA::new_public_from_der(&der).expect("Error with new_public_from_der()");
|
||||
let mut rsa = RSA::new_public_from_der(&der, None, None).expect("Error with new_public_from_der()");
|
||||
rsa.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
let plain: &[u8] = b"Test message";
|
||||
let mut enc: [u8; 512] = [0; 512];
|
||||
@@ -59,7 +59,7 @@ fn test_rsa_encrypt_decrypt() {
|
||||
|
||||
let key_path = "../../../certs/client-key.der";
|
||||
let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
let mut rsa = RSA::new_from_der(&der).expect("Error with new_from_der()");
|
||||
let mut rsa = RSA::new_from_der(&der, None, None).expect("Error with new_from_der()");
|
||||
rsa.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
let mut plain_out: [u8; 512] = [0; 512];
|
||||
let dec_len = rsa.private_decrypt(&enc[0..enc_len], &mut plain_out).expect("Error with private_decrypt()");
|
||||
@@ -73,7 +73,7 @@ fn test_rsa_pss() {
|
||||
|
||||
let key_path = "../../../certs/client-key.der";
|
||||
let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
let mut rsa = RSA::new_from_der(&der).expect("Error with new_from_der()");
|
||||
let mut rsa = RSA::new_from_der(&der, None, None).expect("Error with new_from_der()");
|
||||
let msg: &[u8] = b"This is the string to be signed!";
|
||||
let mut signature: [u8; 512] = [0; 512];
|
||||
let sig_len = rsa.pss_sign(msg, &mut signature, RSA::HASH_TYPE_SHA256, RSA::MGF1SHA256, &mut rng).expect("Error with pss_sign()");
|
||||
@@ -81,7 +81,7 @@ fn test_rsa_pss() {
|
||||
|
||||
let key_path = "../../../certs/client-keyPub.der";
|
||||
let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
let mut rsa = RSA::new_public_from_der(&der).expect("Error with new_public_from_der()");
|
||||
let mut rsa = RSA::new_public_from_der(&der, None, None).expect("Error with new_public_from_der()");
|
||||
rsa.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
let signature = &signature[0..sig_len];
|
||||
let mut verify_out: [u8; 512] = [0; 512];
|
||||
@@ -99,7 +99,7 @@ fn test_rsa_direct() {
|
||||
|
||||
let key_path = "../../../certs/client-key.der";
|
||||
let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
let mut rsa = RSA::new_from_der(&der).expect("Error with new_from_der()");
|
||||
let mut rsa = RSA::new_from_der(&der, None, None).expect("Error with new_from_der()");
|
||||
let msg = b"A rsa_direct() test input string";
|
||||
let mut plain = [0u8; 256];
|
||||
plain[..msg.len()].copy_from_slice(msg);
|
||||
@@ -118,7 +118,7 @@ fn test_rsa_ssl() {
|
||||
|
||||
let key_path = "../../../certs/client-key.der";
|
||||
let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
let mut rsa = RSA::new_from_der(&der).expect("Error with new_from_der()");
|
||||
let mut rsa = RSA::new_from_der(&der, None, None).expect("Error with new_from_der()");
|
||||
let msg: &[u8] = b"This is the string to be signed!";
|
||||
let mut signature: [u8; 512] = [0; 512];
|
||||
let sig_len = rsa.ssl_sign(msg, &mut signature, &mut rng).expect("Error with ssl_sign()");
|
||||
@@ -126,7 +126,7 @@ fn test_rsa_ssl() {
|
||||
|
||||
let key_path = "../../../certs/client-keyPub.der";
|
||||
let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
|
||||
let mut rsa = RSA::new_public_from_der(&der).expect("Error with new_public_from_der()");
|
||||
let mut rsa = RSA::new_public_from_der(&der, None, None).expect("Error with new_public_from_der()");
|
||||
rsa.set_rng(&mut rng).expect("Error with set_rng()");
|
||||
let signature = &signature[0..sig_len];
|
||||
let mut verify_out: [u8; 512] = [0; 512];
|
||||
|
||||
@@ -2,9 +2,9 @@ use wolfssl::wolfcrypt::sha::*;
|
||||
|
||||
#[test]
|
||||
fn test_sha() {
|
||||
let mut sha = SHA::new().expect("Error with new()");
|
||||
let mut sha = SHA::new(None, None).expect("Error with new()");
|
||||
fn test1(sha: &mut SHA, input: &[u8], expected_hash: &[u8]) {
|
||||
sha.init().expect("Error with init()");
|
||||
sha.init(None, None).expect("Error with init()");
|
||||
sha.update(input).expect("Error with update()");
|
||||
let mut hash = [0u8; SHA::DIGEST_SIZE];
|
||||
sha.finalize(&mut hash).expect("Error with finalize()");
|
||||
@@ -27,9 +27,9 @@ fn test_sha() {
|
||||
|
||||
#[test]
|
||||
fn test_sha224() {
|
||||
let mut sha = SHA224::new().expect("Error with new()");
|
||||
let mut sha = SHA224::new(None, None).expect("Error with new()");
|
||||
fn test1(sha: &mut SHA224, input: &[u8], expected_hash: &[u8]) {
|
||||
sha.init().expect("Error with init()");
|
||||
sha.init(None, None).expect("Error with init()");
|
||||
sha.update(input).expect("Error with update()");
|
||||
let mut hash = [0u8; SHA224::DIGEST_SIZE];
|
||||
sha.finalize(&mut hash).expect("Error with finalize()");
|
||||
@@ -46,9 +46,9 @@ fn test_sha224() {
|
||||
|
||||
#[test]
|
||||
fn test_sha256() {
|
||||
let mut sha = SHA256::new().expect("Error with new()");
|
||||
let mut sha = SHA256::new(None, None).expect("Error with new()");
|
||||
fn test1(sha: &mut SHA256, input: &[u8], expected_hash: &[u8]) {
|
||||
sha.init().expect("Error with init()");
|
||||
sha.init(None, None).expect("Error with init()");
|
||||
sha.update(input).expect("Error with update()");
|
||||
let mut hash = [0u8; SHA256::DIGEST_SIZE];
|
||||
sha.finalize(&mut hash).expect("Error with finalize()");
|
||||
@@ -71,9 +71,9 @@ fn test_sha256() {
|
||||
|
||||
#[test]
|
||||
fn test_sha384() {
|
||||
let mut sha = SHA384::new().expect("Error with new()");
|
||||
let mut sha = SHA384::new(None, None).expect("Error with new()");
|
||||
fn test1(sha: &mut SHA384, input: &[u8], expected_hash: &[u8]) {
|
||||
sha.init().expect("Error with init()");
|
||||
sha.init(None, None).expect("Error with init()");
|
||||
sha.update(input).expect("Error with update()");
|
||||
let mut hash = [0u8; SHA384::DIGEST_SIZE];
|
||||
sha.finalize(&mut hash).expect("Error with finalize()");
|
||||
@@ -93,9 +93,9 @@ fn test_sha384() {
|
||||
|
||||
#[test]
|
||||
fn test_sha512() {
|
||||
let mut sha = SHA512::new().expect("Error with new()");
|
||||
let mut sha = SHA512::new(None, None).expect("Error with new()");
|
||||
fn test1(sha: &mut SHA512, input: &[u8], expected_hash: &[u8]) {
|
||||
sha.init().expect("Error with init()");
|
||||
sha.init(None, None).expect("Error with init()");
|
||||
sha.update(input).expect("Error with update()");
|
||||
let mut hash = [0u8; SHA512::DIGEST_SIZE];
|
||||
sha.finalize(&mut hash).expect("Error with finalize()");
|
||||
@@ -115,9 +115,9 @@ fn test_sha512() {
|
||||
|
||||
#[test]
|
||||
fn test_sha3_224() {
|
||||
let mut sha = SHA3_224::new().expect("Error with new()");
|
||||
let mut sha = SHA3_224::new(None, None).expect("Error with new()");
|
||||
fn test1(sha: &mut SHA3_224, input: &[u8], expected_hash: &[u8]) {
|
||||
sha.init().expect("Error with init()");
|
||||
sha.init(None, None).expect("Error with init()");
|
||||
sha.update(input).expect("Error with update()");
|
||||
let mut hash = [0u8; SHA3_224::DIGEST_SIZE];
|
||||
sha.finalize(&mut hash).expect("Error with finalize()");
|
||||
@@ -137,9 +137,9 @@ fn test_sha3_224() {
|
||||
|
||||
#[test]
|
||||
fn test_sha3_256() {
|
||||
let mut sha = SHA3_256::new().expect("Error with new()");
|
||||
let mut sha = SHA3_256::new(None, None).expect("Error with new()");
|
||||
fn test1(sha: &mut SHA3_256, input: &[u8], expected_hash: &[u8]) {
|
||||
sha.init().expect("Error with init()");
|
||||
sha.init(None, None).expect("Error with init()");
|
||||
sha.update(input).expect("Error with update()");
|
||||
let mut hash = [0u8; SHA3_256::DIGEST_SIZE];
|
||||
sha.finalize(&mut hash).expect("Error with finalize()");
|
||||
@@ -159,9 +159,9 @@ fn test_sha3_256() {
|
||||
|
||||
#[test]
|
||||
fn test_sha3_384() {
|
||||
let mut sha = SHA3_384::new().expect("Error with new()");
|
||||
let mut sha = SHA3_384::new(None, None).expect("Error with new()");
|
||||
fn test1(sha: &mut SHA3_384, input: &[u8], expected_hash: &[u8]) {
|
||||
sha.init().expect("Error with init()");
|
||||
sha.init(None, None).expect("Error with init()");
|
||||
sha.update(input).expect("Error with update()");
|
||||
let mut hash = [0u8; SHA3_384::DIGEST_SIZE];
|
||||
sha.finalize(&mut hash).expect("Error with finalize()");
|
||||
@@ -185,9 +185,9 @@ fn test_sha3_384() {
|
||||
|
||||
#[test]
|
||||
fn test_sha3_512() {
|
||||
let mut sha = SHA3_512::new().expect("Error with new()");
|
||||
let mut sha = SHA3_512::new(None, None).expect("Error with new()");
|
||||
fn test1(sha: &mut SHA3_512, input: &[u8], expected_hash: &[u8]) {
|
||||
sha.init().expect("Error with init()");
|
||||
sha.init(None, None).expect("Error with init()");
|
||||
sha.update(input).expect("Error with update()");
|
||||
let mut hash = [0u8; SHA3_512::DIGEST_SIZE];
|
||||
sha.finalize(&mut hash).expect("Error with finalize()");
|
||||
@@ -207,9 +207,9 @@ fn test_sha3_512() {
|
||||
|
||||
#[test]
|
||||
fn test_shake128() {
|
||||
let mut sha = SHAKE128::new().expect("Error with new()");
|
||||
let mut sha = SHAKE128::new(None, None).expect("Error with new()");
|
||||
fn test1(sha: &mut SHAKE128, input: &[u8], expected_hash: &[u8]) {
|
||||
sha.init().expect("Error with init()");
|
||||
sha.init(None, None).expect("Error with init()");
|
||||
sha.update(input).expect("Error with update()");
|
||||
let mut hash = vec![0u8; expected_hash.len()];
|
||||
sha.finalize(&mut hash).expect("Error with finalize()");
|
||||
@@ -237,9 +237,9 @@ fn test_shake128() {
|
||||
|
||||
#[test]
|
||||
fn test_shake128_absorb_squeeze() {
|
||||
let mut sha = SHAKE128::new().expect("Error with new()");
|
||||
let mut sha = SHAKE128::new(None, None).expect("Error with new()");
|
||||
fn test1(sha: &mut SHAKE128, input: &[u8], expected_squeeze_out: &[u8]) {
|
||||
sha.init().expect("Error with init()");
|
||||
sha.init(None, None).expect("Error with init()");
|
||||
sha.absorb(input).expect("Error with absorb()");
|
||||
let mut squeeze_out = vec![0u8; expected_squeeze_out.len()];
|
||||
sha.squeeze_blocks(&mut squeeze_out).expect("Error with squeeze_blocks()");
|
||||
@@ -267,9 +267,9 @@ fn test_shake128_absorb_squeeze() {
|
||||
|
||||
#[test]
|
||||
fn test_shake256() {
|
||||
let mut sha = SHAKE256::new().expect("Error with new()");
|
||||
let mut sha = SHAKE256::new(None, None).expect("Error with new()");
|
||||
fn test1(sha: &mut SHAKE256, input: &[u8], expected_hash: &[u8]) {
|
||||
sha.init().expect("Error with init()");
|
||||
sha.init(None, None).expect("Error with init()");
|
||||
sha.update(input).expect("Error with update()");
|
||||
let mut hash = vec![0u8; expected_hash.len()];
|
||||
sha.finalize(&mut hash).expect("Error with finalize()");
|
||||
@@ -297,9 +297,9 @@ fn test_shake256() {
|
||||
|
||||
#[test]
|
||||
fn test_shake256_absorb_squeeze() {
|
||||
let mut sha = SHAKE256::new().expect("Error with new()");
|
||||
let mut sha = SHAKE256::new(None, None).expect("Error with new()");
|
||||
fn test1(sha: &mut SHAKE256, input: &[u8], expected_squeeze_out: &[u8]) {
|
||||
sha.init().expect("Error with init()");
|
||||
sha.init(None, None).expect("Error with init()");
|
||||
sha.absorb(input).expect("Error with absorb()");
|
||||
let mut squeeze_out = vec![0u8; expected_squeeze_out.len()];
|
||||
sha.squeeze_blocks(&mut squeeze_out).expect("Error with squeeze_blocks()");
|
||||
|
||||
Reference in New Issue
Block a user