Skip to content

Commit df99227

Browse files
Rust wrapper: use _ex APIs for heap and dev_id variants
1 parent 4102f82 commit df99227

18 files changed

Lines changed: 1526 additions & 327 deletions

File tree

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

Lines changed: 120 additions & 20 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(None, None).expect("Failed to create CBC");
37+
/// let mut cbc = CBC::new().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,6 +60,16 @@ pub struct CBC {
6060
impl CBC {
6161
/// Create a new `CBC` instance.
6262
///
63+
/// # Returns
64+
///
65+
/// A Result which is Ok(CBC) on success or an Err containing the wolfSSL
66+
/// library return code on failure.
67+
pub fn new() -> Result<Self, i32> {
68+
Self::new_ex(None, None)
69+
}
70+
71+
/// Create a new `CBC` instance with optional heap and device ID.
72+
///
6373
/// # Parameters
6474
///
6575
/// * `heap`: Optional heap hint.
@@ -69,7 +79,7 @@ impl CBC {
6979
///
7080
/// A Result which is Ok(CBC) on success or an Err containing the wolfSSL
7181
/// library return code on failure.
72-
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
82+
pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
7383
let ws_aes = new_ws_aes(heap, dev_id)?;
7484
let cbc = CBC {ws_aes};
7585
Ok(cbc)
@@ -230,7 +240,7 @@ impl Drop for CBC {
230240
/// 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
231241
/// ];
232242
///
233-
/// let mut ccm = CCM::new(None, None).expect("Failed to create CCM");
243+
/// let mut ccm = CCM::new().expect("Failed to create CCM");
234244
/// ccm.init(&key).expect("Error with init()");
235245
/// let mut auth_tag_out: [u8; 8] = [0; 8];
236246
/// let mut cipher_out: [u8; 23] = [0; 23];
@@ -250,6 +260,16 @@ pub struct CCM {
250260
impl CCM {
251261
/// Create a new `CCM` instance.
252262
///
263+
/// # Returns
264+
///
265+
/// A Result which is Ok(CCM) on success or an Err containing the wolfSSL
266+
/// library return code on failure.
267+
pub fn new() -> Result<Self, i32> {
268+
Self::new_ex(None, None)
269+
}
270+
271+
/// Create a new `CCM` instance with optional heap and device ID.
272+
///
253273
/// # Parameters
254274
///
255275
/// * `heap`: Optional heap hint.
@@ -259,7 +279,7 @@ impl CCM {
259279
///
260280
/// A Result which is Ok(CCM) on success or an Err containing the wolfSSL
261281
/// library return code on failure.
262-
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
282+
pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
263283
let ws_aes = new_ws_aes(heap, dev_id)?;
264284
let ccm = CCM {ws_aes};
265285
Ok(ccm)
@@ -390,7 +410,7 @@ impl Drop for CCM {
390410
/// # Example
391411
/// ```rust
392412
/// use wolfssl::wolfcrypt::aes::CFB;
393-
/// let mut cfb = CFB::new(None, None).expect("Failed to create CFB");
413+
/// let mut cfb = CFB::new().expect("Failed to create CFB");
394414
/// let key: [u8; 16] = [
395415
/// 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
396416
/// 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
@@ -431,6 +451,16 @@ pub struct CFB {
431451
impl CFB {
432452
/// Create a new `CFB` instance.
433453
///
454+
/// # Returns
455+
///
456+
/// A Result which is Ok(CFB) on success or an Err containing the wolfSSL
457+
/// library return code on failure.
458+
pub fn new() -> Result<Self, i32> {
459+
Self::new_ex(None, None)
460+
}
461+
462+
/// Create a new `CFB` instance with optional heap and device ID.
463+
///
434464
/// # Parameters
435465
///
436466
/// * `heap`: Optional heap hint.
@@ -440,7 +470,7 @@ impl CFB {
440470
///
441471
/// A Result which is Ok(CFB) on success or an Err containing the wolfSSL
442472
/// library return code on failure.
443-
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
473+
pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
444474
let ws_aes = new_ws_aes(heap, dev_id)?;
445475
let cfb = CFB {ws_aes};
446476
Ok(cfb)
@@ -705,7 +735,7 @@ impl Drop for CFB {
705735
/// 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
706736
/// 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
707737
/// ];
708-
/// let mut ctr = CTR::new(None, None).expect("Failed to create CTR");
738+
/// let mut ctr = CTR::new().expect("Failed to create CTR");
709739
/// ctr.init(&key, &iv).expect("Error with init()");
710740
/// let mut outbuf: [u8; 64] = [0; 64];
711741
/// ctr.encrypt(&msg, &mut outbuf).expect("Error with encrypt()");
@@ -721,6 +751,16 @@ pub struct CTR {
721751
impl CTR {
722752
/// Create a new `CTR` instance.
723753
///
754+
/// # Returns
755+
///
756+
/// A Result which is Ok(CTR) on success or an Err containing the wolfSSL
757+
/// library return code on failure.
758+
pub fn new() -> Result<Self, i32> {
759+
Self::new_ex(None, None)
760+
}
761+
762+
/// Create a new `CTR` instance with optional heap and device ID.
763+
///
724764
/// # Parameters
725765
///
726766
/// * `heap`: Optional heap hint.
@@ -730,7 +770,7 @@ impl CTR {
730770
///
731771
/// A Result which is Ok(CTR) on success or an Err containing the wolfSSL
732772
/// library return code on failure.
733-
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
773+
pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
734774
let ws_aes = new_ws_aes(heap, dev_id)?;
735775
let ctr = CTR {ws_aes};
736776
Ok(ctr)
@@ -968,7 +1008,7 @@ impl EAX {
9681008
/// # Example
9691009
/// ```rust
9701010
/// use wolfssl::wolfcrypt::aes::ECB;
971-
/// let mut ecb = ECB::new(None, None).expect("Failed to create ECB");
1011+
/// let mut ecb = ECB::new().expect("Failed to create ECB");
9721012
/// let key_128: &[u8; 16] = b"0123456789abcdef";
9731013
/// let msg: [u8; 16] = [
9741014
/// 0x6e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
@@ -993,6 +1033,16 @@ pub struct ECB {
9931033
impl ECB {
9941034
/// Create a new `ECB` instance.
9951035
///
1036+
/// # Returns
1037+
///
1038+
/// A Result which is Ok(ECB) on success or an Err containing the wolfSSL
1039+
/// library return code on failure.
1040+
pub fn new() -> Result<Self, i32> {
1041+
Self::new_ex(None, None)
1042+
}
1043+
1044+
/// Create a new `ECB` instance with optional heap and device ID.
1045+
///
9961046
/// # Parameters
9971047
///
9981048
/// * `heap`: Optional heap hint.
@@ -1002,7 +1052,7 @@ impl ECB {
10021052
///
10031053
/// A Result which is Ok(ECB) on success or an Err containing the wolfSSL
10041054
/// library return code on failure.
1005-
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
1055+
pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
10061056
let ws_aes = new_ws_aes(heap, dev_id)?;
10071057
let ecb = ECB {ws_aes};
10081058
Ok(ecb)
@@ -1162,7 +1212,7 @@ impl Drop for ECB {
11621212
/// 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
11631213
/// 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
11641214
/// ];
1165-
/// let mut gcm = GCM::new(None, None).expect("Failed to create GCM");
1215+
/// let mut gcm = GCM::new().expect("Failed to create GCM");
11661216
/// gcm.init(&key).expect("Error with init()");
11671217
/// let mut cipher: [u8; 32] = [0; 32];
11681218
/// let mut auth_tag: [u8; 16] = [0; 16];
@@ -1179,6 +1229,16 @@ pub struct GCM {
11791229
impl GCM {
11801230
/// Create a new `GCM` instance.
11811231
///
1232+
/// # Returns
1233+
///
1234+
/// A Result which is Ok(GCM) on success or an Err containing the wolfSSL
1235+
/// library return code on failure.
1236+
pub fn new() -> Result<Self, i32> {
1237+
Self::new_ex(None, None)
1238+
}
1239+
1240+
/// Create a new `GCM` instance with optional heap and device ID.
1241+
///
11821242
/// # Parameters
11831243
///
11841244
/// * `heap`: Optional heap hint.
@@ -1188,7 +1248,7 @@ impl GCM {
11881248
///
11891249
/// A Result which is Ok(GCM) on success or an Err containing the wolfSSL
11901250
/// library return code on failure.
1191-
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
1251+
pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
11921252
let ws_aes = new_ws_aes(heap, dev_id)?;
11931253
let gcm = GCM {ws_aes};
11941254
Ok(gcm)
@@ -1363,7 +1423,7 @@ impl Drop for GCM {
13631423
/// 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
13641424
/// 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
13651425
/// ];
1366-
/// let mut gcmstream = GCMStream::new(None, None).expect("Failed to create GCMStream");
1426+
/// let mut gcmstream = GCMStream::new().expect("Failed to create GCMStream");
13671427
/// for chunk_size in 1..=auth.len() {
13681428
/// gcmstream.init(&key, &iv).expect("Error with init()");
13691429
/// let mut cipher: [u8; 60] = [0; 60];
@@ -1397,6 +1457,16 @@ pub struct GCMStream {
13971457
impl GCMStream {
13981458
/// Create a new `GCMStream` instance.
13991459
///
1460+
/// # Returns
1461+
///
1462+
/// A Result which is Ok(GCMStream) on success or an Err containing the
1463+
/// wolfSSL library return code on failure.
1464+
pub fn new() -> Result<Self, i32> {
1465+
Self::new_ex(None, None)
1466+
}
1467+
1468+
/// Create a new `GCMStream` instance with heap and device ID.
1469+
///
14001470
/// # Parameters
14011471
///
14021472
/// * `heap`: Optional heap hint.
@@ -1406,7 +1476,7 @@ impl GCMStream {
14061476
///
14071477
/// A Result which is Ok(GCMStream) on success or an Err containing the
14081478
/// wolfSSL library return code on failure.
1409-
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
1479+
pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
14101480
let ws_aes = new_ws_aes(heap, dev_id)?;
14111481
let gcmstream = GCMStream {ws_aes};
14121482
Ok(gcmstream)
@@ -1619,7 +1689,7 @@ impl Drop for GCMStream {
16191689
/// 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
16201690
/// 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
16211691
/// ];
1622-
/// let mut ofb = OFB::new(None, None).expect("Failed to create OFB");
1692+
/// let mut ofb = OFB::new().expect("Failed to create OFB");
16231693
/// ofb.init(&key, &iv).expect("Error with init()");
16241694
/// let mut cipher: [u8; 48] = [0; 48];
16251695
/// ofb.encrypt(&plain, &mut cipher).expect("Error with encrypt()");
@@ -1635,6 +1705,16 @@ pub struct OFB {
16351705
impl OFB {
16361706
/// Create a new `OFB` instance.
16371707
///
1708+
/// # Returns
1709+
///
1710+
/// A Result which is Ok(OFB) on success or an Err containing the wolfSSL
1711+
/// library return code on failure.
1712+
pub fn new() -> Result<Self, i32> {
1713+
Self::new_ex(None, None)
1714+
}
1715+
1716+
/// Create a new `OFB` instance with optional heap and device ID.
1717+
///
16381718
/// # Parameters
16391719
///
16401720
/// * `heap`: Optional heap hint.
@@ -1644,7 +1724,7 @@ impl OFB {
16441724
///
16451725
/// A Result which is Ok(OFB) on success or an Err containing the wolfSSL
16461726
/// library return code on failure.
1647-
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
1727+
pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
16481728
let ws_aes = new_ws_aes(heap, dev_id)?;
16491729
let ofb = OFB {ws_aes};
16501730
Ok(ofb)
@@ -1789,7 +1869,7 @@ impl Drop for OFB {
17891869
/// 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a
17901870
/// ];
17911871
///
1792-
/// let mut xts = XTS::new(None, None).expect("Failed to create XTS");
1872+
/// let mut xts = XTS::new().expect("Failed to create XTS");
17931873
/// xts.init_encrypt(&key).expect("Error with init_encrypt()");
17941874
/// let mut cipher: [u8; 16] = [0; 16];
17951875
/// xts.encrypt(&plain, &mut cipher, &tweak).expect("Error with encrypt()");
@@ -1814,6 +1894,16 @@ pub struct XTS {
18141894
impl XTS {
18151895
/// Create a new `XTS` instance.
18161896
///
1897+
/// # Returns
1898+
///
1899+
/// A Result which is Ok(XTS) on success or an Err containing the wolfSSL
1900+
/// library return code on failure.
1901+
pub fn new() -> Result<Self, i32> {
1902+
Self::new_ex(None, None)
1903+
}
1904+
1905+
/// Create a new `XTS` instance with optional heap and device ID.
1906+
///
18171907
/// # Parameters
18181908
///
18191909
/// * `heap`: Optional heap hint.
@@ -1823,7 +1913,7 @@ impl XTS {
18231913
///
18241914
/// A Result which is Ok(XTS) on success or an Err containing the wolfSSL
18251915
/// library return code on failure.
1826-
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
1916+
pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
18271917
let ws_xtsaes = new_ws_xtsaes(heap, dev_id)?;
18281918
let xts = XTS {ws_xtsaes};
18291919
Ok(xts)
@@ -2145,7 +2235,7 @@ impl Drop for XTS {
21452235
/// 0xB5, 0x5A, 0xDD, 0xCB, 0x80, 0xE0, 0xFC, 0xCD
21462236
/// ];
21472237
///
2148-
/// let mut xtsstream = XTSStream::new(None, None).expect("Failed to create XTSStream");
2238+
/// let mut xtsstream = XTSStream::new().expect("Failed to create XTSStream");
21492239
/// xtsstream.init_encrypt(&keys, &tweak).expect("Error with init_encrypt()");
21502240
/// let mut cipher: [u8; 40] = [0; 40];
21512241
/// xtsstream.encrypt_update(&plain[0..16], &mut cipher[0..16]).expect("Error with encrypt_update()");
@@ -2165,6 +2255,16 @@ pub struct XTSStream {
21652255
impl XTSStream {
21662256
/// Create a new `XTSStream` instance.
21672257
///
2258+
/// # Returns
2259+
///
2260+
/// A Result which is Ok(XTSStream) on success or an Err containing the
2261+
/// wolfSSL library return code on failure.
2262+
pub fn new() -> Result<Self, i32> {
2263+
Self::new_ex(None, None)
2264+
}
2265+
2266+
/// Create a new `XTSStream` instance with optional heap and device ID.
2267+
///
21682268
/// # Parameters
21692269
///
21702270
/// * `heap`: Optional heap hint.
@@ -2174,7 +2274,7 @@ impl XTSStream {
21742274
///
21752275
/// A Result which is Ok(XTSStream) on success or an Err containing the
21762276
/// wolfSSL library return code on failure.
2177-
pub fn new(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
2277+
pub fn new_ex(heap: Option<*mut std::os::raw::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
21782278
let ws_xtsaes = new_ws_xtsaes(heap, dev_id)?;
21792279
let ws_xtsaesstreamdata: MaybeUninit<ws::XtsAesStreamData> = MaybeUninit::uninit();
21802280
let ws_xtsaesstreamdata = unsafe { ws_xtsaesstreamdata.assume_init() };

0 commit comments

Comments
 (0)