Skip to content

Commit e78752f

Browse files
authored
Merge pull request wolfSSL#9407 from holtrop/rust-heap-devid-cleanup
Rust wrapper: support optional heap and dev_id parameters
2 parents 7cfffd5 + 40c471e commit e78752f

File tree

19 files changed

+2484
-218
lines changed

19 files changed

+2484
-218
lines changed

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

Lines changed: 180 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,22 @@ impl CBC {
6565
/// A Result which is Ok(CBC) on success or an Err containing the wolfSSL
6666
/// library return code on failure.
6767
pub fn new() -> Result<Self, i32> {
68-
let ws_aes = new_ws_aes()?;
68+
Self::new_ex(None, None)
69+
}
70+
71+
/// Create a new `CBC` instance with optional heap and device ID.
72+
///
73+
/// # Parameters
74+
///
75+
/// * `heap`: Optional heap hint.
76+
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
77+
///
78+
/// # Returns
79+
///
80+
/// A Result which is Ok(CBC) on success or an Err containing the wolfSSL
81+
/// library return code on failure.
82+
pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
83+
let ws_aes = new_ws_aes(heap, dev_id)?;
6984
let cbc = CBC {ws_aes};
7085
Ok(cbc)
7186
}
@@ -250,7 +265,22 @@ impl CCM {
250265
/// A Result which is Ok(CCM) on success or an Err containing the wolfSSL
251266
/// library return code on failure.
252267
pub fn new() -> Result<Self, i32> {
253-
let ws_aes = new_ws_aes()?;
268+
Self::new_ex(None, None)
269+
}
270+
271+
/// Create a new `CCM` instance with optional heap and device ID.
272+
///
273+
/// # Parameters
274+
///
275+
/// * `heap`: Optional heap hint.
276+
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
277+
///
278+
/// # Returns
279+
///
280+
/// A Result which is Ok(CCM) on success or an Err containing the wolfSSL
281+
/// library return code on failure.
282+
pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
283+
let ws_aes = new_ws_aes(heap, dev_id)?;
254284
let ccm = CCM {ws_aes};
255285
Ok(ccm)
256286
}
@@ -426,7 +456,22 @@ impl CFB {
426456
/// A Result which is Ok(CFB) on success or an Err containing the wolfSSL
427457
/// library return code on failure.
428458
pub fn new() -> Result<Self, i32> {
429-
let ws_aes = new_ws_aes()?;
459+
Self::new_ex(None, None)
460+
}
461+
462+
/// Create a new `CFB` instance with optional heap and device ID.
463+
///
464+
/// # Parameters
465+
///
466+
/// * `heap`: Optional heap hint.
467+
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
468+
///
469+
/// # Returns
470+
///
471+
/// A Result which is Ok(CFB) on success or an Err containing the wolfSSL
472+
/// library return code on failure.
473+
pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
474+
let ws_aes = new_ws_aes(heap, dev_id)?;
430475
let cfb = CFB {ws_aes};
431476
Ok(cfb)
432477
}
@@ -711,7 +756,22 @@ impl CTR {
711756
/// A Result which is Ok(CTR) on success or an Err containing the wolfSSL
712757
/// library return code on failure.
713758
pub fn new() -> Result<Self, i32> {
714-
let ws_aes = new_ws_aes()?;
759+
Self::new_ex(None, None)
760+
}
761+
762+
/// Create a new `CTR` instance with optional heap and device ID.
763+
///
764+
/// # Parameters
765+
///
766+
/// * `heap`: Optional heap hint.
767+
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
768+
///
769+
/// # Returns
770+
///
771+
/// A Result which is Ok(CTR) on success or an Err containing the wolfSSL
772+
/// library return code on failure.
773+
pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
774+
let ws_aes = new_ws_aes(heap, dev_id)?;
715775
let ctr = CTR {ws_aes};
716776
Ok(ctr)
717777
}
@@ -978,7 +1038,22 @@ impl ECB {
9781038
/// A Result which is Ok(ECB) on success or an Err containing the wolfSSL
9791039
/// library return code on failure.
9801040
pub fn new() -> Result<Self, i32> {
981-
let ws_aes = new_ws_aes()?;
1041+
Self::new_ex(None, None)
1042+
}
1043+
1044+
/// Create a new `ECB` instance with optional heap and device ID.
1045+
///
1046+
/// # Parameters
1047+
///
1048+
/// * `heap`: Optional heap hint.
1049+
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
1050+
///
1051+
/// # Returns
1052+
///
1053+
/// A Result which is Ok(ECB) on success or an Err containing the wolfSSL
1054+
/// library return code on failure.
1055+
pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
1056+
let ws_aes = new_ws_aes(heap, dev_id)?;
9821057
let ecb = ECB {ws_aes};
9831058
Ok(ecb)
9841059
}
@@ -1159,7 +1234,22 @@ impl GCM {
11591234
/// A Result which is Ok(GCM) on success or an Err containing the wolfSSL
11601235
/// library return code on failure.
11611236
pub fn new() -> Result<Self, i32> {
1162-
let ws_aes = new_ws_aes()?;
1237+
Self::new_ex(None, None)
1238+
}
1239+
1240+
/// Create a new `GCM` instance with optional heap and device ID.
1241+
///
1242+
/// # Parameters
1243+
///
1244+
/// * `heap`: Optional heap hint.
1245+
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
1246+
///
1247+
/// # Returns
1248+
///
1249+
/// A Result which is Ok(GCM) on success or an Err containing the wolfSSL
1250+
/// library return code on failure.
1251+
pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
1252+
let ws_aes = new_ws_aes(heap, dev_id)?;
11631253
let gcm = GCM {ws_aes};
11641254
Ok(gcm)
11651255
}
@@ -1372,7 +1462,22 @@ impl GCMStream {
13721462
/// A Result which is Ok(GCMStream) on success or an Err containing the
13731463
/// wolfSSL library return code on failure.
13741464
pub fn new() -> Result<Self, i32> {
1375-
let ws_aes = new_ws_aes()?;
1465+
Self::new_ex(None, None)
1466+
}
1467+
1468+
/// Create a new `GCMStream` instance with heap and device ID.
1469+
///
1470+
/// # Parameters
1471+
///
1472+
/// * `heap`: Optional heap hint.
1473+
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
1474+
///
1475+
/// # Returns
1476+
///
1477+
/// A Result which is Ok(GCMStream) on success or an Err containing the
1478+
/// wolfSSL library return code on failure.
1479+
pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
1480+
let ws_aes = new_ws_aes(heap, dev_id)?;
13761481
let gcmstream = GCMStream {ws_aes};
13771482
Ok(gcmstream)
13781483
}
@@ -1605,7 +1710,22 @@ impl OFB {
16051710
/// A Result which is Ok(OFB) on success or an Err containing the wolfSSL
16061711
/// library return code on failure.
16071712
pub fn new() -> Result<Self, i32> {
1608-
let ws_aes = new_ws_aes()?;
1713+
Self::new_ex(None, None)
1714+
}
1715+
1716+
/// Create a new `OFB` instance with optional heap and device ID.
1717+
///
1718+
/// # Parameters
1719+
///
1720+
/// * `heap`: Optional heap hint.
1721+
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
1722+
///
1723+
/// # Returns
1724+
///
1725+
/// A Result which is Ok(OFB) on success or an Err containing the wolfSSL
1726+
/// library return code on failure.
1727+
pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
1728+
let ws_aes = new_ws_aes(heap, dev_id)?;
16091729
let ofb = OFB {ws_aes};
16101730
Ok(ofb)
16111731
}
@@ -1779,7 +1899,22 @@ impl XTS {
17791899
/// A Result which is Ok(XTS) on success or an Err containing the wolfSSL
17801900
/// library return code on failure.
17811901
pub fn new() -> Result<Self, i32> {
1782-
let ws_xtsaes = new_ws_xtsaes()?;
1902+
Self::new_ex(None, None)
1903+
}
1904+
1905+
/// Create a new `XTS` instance with optional heap and device ID.
1906+
///
1907+
/// # Parameters
1908+
///
1909+
/// * `heap`: Optional heap hint.
1910+
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
1911+
///
1912+
/// # Returns
1913+
///
1914+
/// A Result which is Ok(XTS) on success or an Err containing the wolfSSL
1915+
/// library return code on failure.
1916+
pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
1917+
let ws_xtsaes = new_ws_xtsaes(heap, dev_id)?;
17831918
let xts = XTS {ws_xtsaes};
17841919
Ok(xts)
17851920
}
@@ -2125,7 +2260,22 @@ impl XTSStream {
21252260
/// A Result which is Ok(XTSStream) on success or an Err containing the
21262261
/// wolfSSL library return code on failure.
21272262
pub fn new() -> Result<Self, i32> {
2128-
let ws_xtsaes = new_ws_xtsaes()?;
2263+
Self::new_ex(None, None)
2264+
}
2265+
2266+
/// Create a new `XTSStream` instance with optional heap and device ID.
2267+
///
2268+
/// # Parameters
2269+
///
2270+
/// * `heap`: Optional heap hint.
2271+
/// * `dev_id` Optional device ID to use with crypto callbacks or async hardware.
2272+
///
2273+
/// # Returns
2274+
///
2275+
/// A Result which is Ok(XTSStream) on success or an Err containing the
2276+
/// wolfSSL library return code on failure.
2277+
pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
2278+
let ws_xtsaes = new_ws_xtsaes(heap, dev_id)?;
21292279
let ws_xtsaesstreamdata: MaybeUninit<ws::XtsAesStreamData> = MaybeUninit::uninit();
21302280
let ws_xtsaesstreamdata = unsafe { ws_xtsaesstreamdata.assume_init() };
21312281
let xtsstream = XTSStream {ws_xtsaes, ws_xtsaesstreamdata};
@@ -2353,10 +2503,18 @@ impl Drop for XTSStream {
23532503
}
23542504
}
23552505

2356-
fn new_ws_aes() -> Result<ws::Aes, i32> {
2506+
fn new_ws_aes(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<ws::Aes, i32> {
2507+
let heap = match heap {
2508+
Some(heap) => heap,
2509+
None => core::ptr::null_mut(),
2510+
};
2511+
let dev_id = match dev_id {
2512+
Some(dev_id) => dev_id,
2513+
None => ws::INVALID_DEVID,
2514+
};
23572515
let mut ws_aes: MaybeUninit<ws::Aes> = MaybeUninit::uninit();
23582516
let rc = unsafe {
2359-
ws::wc_AesInit(ws_aes.as_mut_ptr(), core::ptr::null_mut(), ws::INVALID_DEVID)
2517+
ws::wc_AesInit(ws_aes.as_mut_ptr(), heap, dev_id)
23602518
};
23612519
if rc != 0 {
23622520
return Err(rc);
@@ -2365,10 +2523,18 @@ fn new_ws_aes() -> Result<ws::Aes, i32> {
23652523
Ok(ws_aes)
23662524
}
23672525

2368-
fn new_ws_xtsaes() -> Result<ws::XtsAes, i32> {
2526+
fn new_ws_xtsaes(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<ws::XtsAes, i32> {
2527+
let heap = match heap {
2528+
Some(heap) => heap,
2529+
None => core::ptr::null_mut(),
2530+
};
2531+
let dev_id = match dev_id {
2532+
Some(dev_id) => dev_id,
2533+
None => ws::INVALID_DEVID,
2534+
};
23692535
let mut ws_xtsaes: MaybeUninit<ws::XtsAes> = MaybeUninit::uninit();
23702536
let rc = unsafe {
2371-
ws::wc_AesXtsInit(ws_xtsaes.as_mut_ptr(), core::ptr::null_mut(), ws::INVALID_DEVID)
2537+
ws::wc_AesXtsInit(ws_xtsaes.as_mut_ptr(), heap, dev_id)
23722538
};
23732539
if rc != 0 {
23742540
return Err(rc);

0 commit comments

Comments
 (0)