Skip to content

Commit 4102f82

Browse files
Rust wrapper: support optional heap and dev_id parameters
1 parent 2c47675 commit 4102f82

26 files changed

Lines changed: 1603 additions & 534 deletions

wrapper/rust/wolfssl/src/wolfcrypt/aes.rs

Lines changed: 100 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use wolfssl_sys as ws;
3434
/// # Example
3535
/// ```rust
3636
/// use wolfssl::wolfcrypt::aes::CBC;
37-
/// let mut cbc = CBC::new().expect("Failed to create CBC");
37+
/// let mut cbc = CBC::new(None, None).expect("Failed to create CBC");
3838
/// let key: &[u8; 16] = b"0123456789abcdef";
3939
/// let iv: &[u8; 16] = b"1234567890abcdef";
4040
/// let msg: [u8; 16] = [
@@ -60,12 +60,17 @@ pub struct CBC {
6060
impl CBC {
6161
/// Create a new `CBC` instance.
6262
///
63+
/// # Parameters
64+
///
65+
/// * `heap`: Optional heap hint.
66+
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
67+
///
6368
/// # Returns
6469
///
6570
/// A Result which is Ok(CBC) on success or an Err containing the wolfSSL
6671
/// library return code on failure.
67-
pub fn new() -> Result<Self, i32> {
68-
let ws_aes = new_ws_aes()?;
72+
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
73+
let ws_aes = new_ws_aes(heap, dev_id)?;
6974
let cbc = CBC {ws_aes};
7075
Ok(cbc)
7176
}
@@ -225,7 +230,7 @@ impl Drop for CBC {
225230
/// 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
226231
/// ];
227232
///
228-
/// let mut ccm = CCM::new().expect("Failed to create CCM");
233+
/// let mut ccm = CCM::new(None, None).expect("Failed to create CCM");
229234
/// ccm.init(&key).expect("Error with init()");
230235
/// let mut auth_tag_out: [u8; 8] = [0; 8];
231236
/// let mut cipher_out: [u8; 23] = [0; 23];
@@ -245,12 +250,17 @@ pub struct CCM {
245250
impl CCM {
246251
/// Create a new `CCM` instance.
247252
///
253+
/// # Parameters
254+
///
255+
/// * `heap`: Optional heap hint.
256+
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
257+
///
248258
/// # Returns
249259
///
250260
/// A Result which is Ok(CCM) on success or an Err containing the wolfSSL
251261
/// library return code on failure.
252-
pub fn new() -> Result<Self, i32> {
253-
let ws_aes = new_ws_aes()?;
262+
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
263+
let ws_aes = new_ws_aes(heap, dev_id)?;
254264
let ccm = CCM {ws_aes};
255265
Ok(ccm)
256266
}
@@ -380,7 +390,7 @@ impl Drop for CCM {
380390
/// # Example
381391
/// ```rust
382392
/// use wolfssl::wolfcrypt::aes::CFB;
383-
/// let mut cfb = CFB::new().expect("Failed to create CFB");
393+
/// let mut cfb = CFB::new(None, None).expect("Failed to create CFB");
384394
/// let key: [u8; 16] = [
385395
/// 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
386396
/// 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
@@ -421,12 +431,17 @@ pub struct CFB {
421431
impl CFB {
422432
/// Create a new `CFB` instance.
423433
///
434+
/// # Parameters
435+
///
436+
/// * `heap`: Optional heap hint.
437+
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
438+
///
424439
/// # Returns
425440
///
426441
/// A Result which is Ok(CFB) on success or an Err containing the wolfSSL
427442
/// library return code on failure.
428-
pub fn new() -> Result<Self, i32> {
429-
let ws_aes = new_ws_aes()?;
443+
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
444+
let ws_aes = new_ws_aes(heap, dev_id)?;
430445
let cfb = CFB {ws_aes};
431446
Ok(cfb)
432447
}
@@ -690,7 +705,7 @@ impl Drop for CFB {
690705
/// 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
691706
/// 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
692707
/// ];
693-
/// let mut ctr = CTR::new().expect("Failed to create CTR");
708+
/// let mut ctr = CTR::new(None, None).expect("Failed to create CTR");
694709
/// ctr.init(&key, &iv).expect("Error with init()");
695710
/// let mut outbuf: [u8; 64] = [0; 64];
696711
/// ctr.encrypt(&msg, &mut outbuf).expect("Error with encrypt()");
@@ -706,12 +721,17 @@ pub struct CTR {
706721
impl CTR {
707722
/// Create a new `CTR` instance.
708723
///
724+
/// # Parameters
725+
///
726+
/// * `heap`: Optional heap hint.
727+
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
728+
///
709729
/// # Returns
710730
///
711731
/// A Result which is Ok(CTR) on success or an Err containing the wolfSSL
712732
/// library return code on failure.
713-
pub fn new() -> Result<Self, i32> {
714-
let ws_aes = new_ws_aes()?;
733+
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
734+
let ws_aes = new_ws_aes(heap, dev_id)?;
715735
let ctr = CTR {ws_aes};
716736
Ok(ctr)
717737
}
@@ -948,7 +968,7 @@ impl EAX {
948968
/// # Example
949969
/// ```rust
950970
/// use wolfssl::wolfcrypt::aes::ECB;
951-
/// let mut ecb = ECB::new().expect("Failed to create ECB");
971+
/// let mut ecb = ECB::new(None, None).expect("Failed to create ECB");
952972
/// let key_128: &[u8; 16] = b"0123456789abcdef";
953973
/// let msg: [u8; 16] = [
954974
/// 0x6e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
@@ -973,12 +993,17 @@ pub struct ECB {
973993
impl ECB {
974994
/// Create a new `ECB` instance.
975995
///
996+
/// # Parameters
997+
///
998+
/// * `heap`: Optional heap hint.
999+
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
1000+
///
9761001
/// # Returns
9771002
///
9781003
/// A Result which is Ok(ECB) on success or an Err containing the wolfSSL
9791004
/// library return code on failure.
980-
pub fn new() -> Result<Self, i32> {
981-
let ws_aes = new_ws_aes()?;
1005+
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
1006+
let ws_aes = new_ws_aes(heap, dev_id)?;
9821007
let ecb = ECB {ws_aes};
9831008
Ok(ecb)
9841009
}
@@ -1137,7 +1162,7 @@ impl Drop for ECB {
11371162
/// 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
11381163
/// 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
11391164
/// ];
1140-
/// let mut gcm = GCM::new().expect("Failed to create GCM");
1165+
/// let mut gcm = GCM::new(None, None).expect("Failed to create GCM");
11411166
/// gcm.init(&key).expect("Error with init()");
11421167
/// let mut cipher: [u8; 32] = [0; 32];
11431168
/// let mut auth_tag: [u8; 16] = [0; 16];
@@ -1154,12 +1179,17 @@ pub struct GCM {
11541179
impl GCM {
11551180
/// Create a new `GCM` instance.
11561181
///
1182+
/// # Parameters
1183+
///
1184+
/// * `heap`: Optional heap hint.
1185+
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
1186+
///
11571187
/// # Returns
11581188
///
11591189
/// A Result which is Ok(GCM) on success or an Err containing the wolfSSL
11601190
/// library return code on failure.
1161-
pub fn new() -> Result<Self, i32> {
1162-
let ws_aes = new_ws_aes()?;
1191+
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
1192+
let ws_aes = new_ws_aes(heap, dev_id)?;
11631193
let gcm = GCM {ws_aes};
11641194
Ok(gcm)
11651195
}
@@ -1333,7 +1363,7 @@ impl Drop for GCM {
13331363
/// 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
13341364
/// 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
13351365
/// ];
1336-
/// let mut gcmstream = GCMStream::new().expect("Failed to create GCMStream");
1366+
/// let mut gcmstream = GCMStream::new(None, None).expect("Failed to create GCMStream");
13371367
/// for chunk_size in 1..=auth.len() {
13381368
/// gcmstream.init(&key, &iv).expect("Error with init()");
13391369
/// let mut cipher: [u8; 60] = [0; 60];
@@ -1367,12 +1397,17 @@ pub struct GCMStream {
13671397
impl GCMStream {
13681398
/// Create a new `GCMStream` instance.
13691399
///
1400+
/// # Parameters
1401+
///
1402+
/// * `heap`: Optional heap hint.
1403+
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
1404+
///
13701405
/// # Returns
13711406
///
13721407
/// A Result which is Ok(GCMStream) on success or an Err containing the
13731408
/// wolfSSL library return code on failure.
1374-
pub fn new() -> Result<Self, i32> {
1375-
let ws_aes = new_ws_aes()?;
1409+
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
1410+
let ws_aes = new_ws_aes(heap, dev_id)?;
13761411
let gcmstream = GCMStream {ws_aes};
13771412
Ok(gcmstream)
13781413
}
@@ -1584,7 +1619,7 @@ impl Drop for GCMStream {
15841619
/// 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
15851620
/// 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
15861621
/// ];
1587-
/// let mut ofb = OFB::new().expect("Failed to create OFB");
1622+
/// let mut ofb = OFB::new(None, None).expect("Failed to create OFB");
15881623
/// ofb.init(&key, &iv).expect("Error with init()");
15891624
/// let mut cipher: [u8; 48] = [0; 48];
15901625
/// ofb.encrypt(&plain, &mut cipher).expect("Error with encrypt()");
@@ -1600,12 +1635,17 @@ pub struct OFB {
16001635
impl OFB {
16011636
/// Create a new `OFB` instance.
16021637
///
1638+
/// # Parameters
1639+
///
1640+
/// * `heap`: Optional heap hint.
1641+
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
1642+
///
16031643
/// # Returns
16041644
///
16051645
/// A Result which is Ok(OFB) on success or an Err containing the wolfSSL
16061646
/// library return code on failure.
1607-
pub fn new() -> Result<Self, i32> {
1608-
let ws_aes = new_ws_aes()?;
1647+
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
1648+
let ws_aes = new_ws_aes(heap, dev_id)?;
16091649
let ofb = OFB {ws_aes};
16101650
Ok(ofb)
16111651
}
@@ -1749,7 +1789,7 @@ impl Drop for OFB {
17491789
/// 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a
17501790
/// ];
17511791
///
1752-
/// let mut xts = XTS::new().expect("Failed to create XTS");
1792+
/// let mut xts = XTS::new(None, None).expect("Failed to create XTS");
17531793
/// xts.init_encrypt(&key).expect("Error with init_encrypt()");
17541794
/// let mut cipher: [u8; 16] = [0; 16];
17551795
/// xts.encrypt(&plain, &mut cipher, &tweak).expect("Error with encrypt()");
@@ -1774,12 +1814,17 @@ pub struct XTS {
17741814
impl XTS {
17751815
/// Create a new `XTS` instance.
17761816
///
1817+
/// # Parameters
1818+
///
1819+
/// * `heap`: Optional heap hint.
1820+
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
1821+
///
17771822
/// # Returns
17781823
///
17791824
/// A Result which is Ok(XTS) on success or an Err containing the wolfSSL
17801825
/// library return code on failure.
1781-
pub fn new() -> Result<Self, i32> {
1782-
let ws_xtsaes = new_ws_xtsaes()?;
1826+
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
1827+
let ws_xtsaes = new_ws_xtsaes(heap, dev_id)?;
17831828
let xts = XTS {ws_xtsaes};
17841829
Ok(xts)
17851830
}
@@ -2100,7 +2145,7 @@ impl Drop for XTS {
21002145
/// 0xB5, 0x5A, 0xDD, 0xCB, 0x80, 0xE0, 0xFC, 0xCD
21012146
/// ];
21022147
///
2103-
/// let mut xtsstream = XTSStream::new().expect("Failed to create XTSStream");
2148+
/// let mut xtsstream = XTSStream::new(None, None).expect("Failed to create XTSStream");
21042149
/// xtsstream.init_encrypt(&keys, &tweak).expect("Error with init_encrypt()");
21052150
/// let mut cipher: [u8; 40] = [0; 40];
21062151
/// xtsstream.encrypt_update(&plain[0..16], &mut cipher[0..16]).expect("Error with encrypt_update()");
@@ -2120,12 +2165,17 @@ pub struct XTSStream {
21202165
impl XTSStream {
21212166
/// Create a new `XTSStream` instance.
21222167
///
2168+
/// # Parameters
2169+
///
2170+
/// * `heap`: Optional heap hint.
2171+
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
2172+
///
21232173
/// # Returns
21242174
///
21252175
/// A Result which is Ok(XTSStream) on success or an Err containing the
21262176
/// wolfSSL library return code on failure.
2127-
pub fn new() -> Result<Self, i32> {
2128-
let ws_xtsaes = new_ws_xtsaes()?;
2177+
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
2178+
let ws_xtsaes = new_ws_xtsaes(heap, dev_id)?;
21292179
let ws_xtsaesstreamdata: MaybeUninit<ws::XtsAesStreamData> = MaybeUninit::uninit();
21302180
let ws_xtsaesstreamdata = unsafe { ws_xtsaesstreamdata.assume_init() };
21312181
let xtsstream = XTSStream {ws_xtsaes, ws_xtsaesstreamdata};
@@ -2353,10 +2403,18 @@ impl Drop for XTSStream {
23532403
}
23542404
}
23552405

2356-
fn new_ws_aes() -> Result<ws::Aes, i32> {
2406+
fn new_ws_aes(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<ws::Aes, i32> {
2407+
let heap = match heap {
2408+
Some(heap) => heap,
2409+
None => core::ptr::null_mut(),
2410+
};
2411+
let dev_id = match dev_id {
2412+
Some(dev_id) => dev_id,
2413+
None => ws::INVALID_DEVID,
2414+
};
23572415
let mut ws_aes: MaybeUninit<ws::Aes> = MaybeUninit::uninit();
23582416
let rc = unsafe {
2359-
ws::wc_AesInit(ws_aes.as_mut_ptr(), core::ptr::null_mut(), ws::INVALID_DEVID)
2417+
ws::wc_AesInit(ws_aes.as_mut_ptr(), heap, dev_id)
23602418
};
23612419
if rc != 0 {
23622420
return Err(rc);
@@ -2365,10 +2423,18 @@ fn new_ws_aes() -> Result<ws::Aes, i32> {
23652423
Ok(ws_aes)
23662424
}
23672425

2368-
fn new_ws_xtsaes() -> Result<ws::XtsAes, i32> {
2426+
fn new_ws_xtsaes(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<ws::XtsAes, i32> {
2427+
let heap = match heap {
2428+
Some(heap) => heap,
2429+
None => core::ptr::null_mut(),
2430+
};
2431+
let dev_id = match dev_id {
2432+
Some(dev_id) => dev_id,
2433+
None => ws::INVALID_DEVID,
2434+
};
23692435
let mut ws_xtsaes: MaybeUninit<ws::XtsAes> = MaybeUninit::uninit();
23702436
let rc = unsafe {
2371-
ws::wc_AesXtsInit(ws_xtsaes.as_mut_ptr(), core::ptr::null_mut(), ws::INVALID_DEVID)
2437+
ws::wc_AesXtsInit(ws_xtsaes.as_mut_ptr(), heap, dev_id)
23722438
};
23732439
if rc != 0 {
23742440
return Err(rc);

0 commit comments

Comments
 (0)