Skip to content

Commit eac5c29

Browse files
authored
Merge pull request #9455 from holtrop/rust-wc-init-cleanup
Rust wrapper: wrap wolfCrypt_Init() and wolfCrypt_Cleanup()
2 parents 4ccad17 + 09e223b commit eac5c29

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

wrapper/rust/wolfssl/src/wolfcrypt.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,47 @@ pub mod prf;
3131
pub mod random;
3232
pub mod rsa;
3333
pub mod sha;
34+
35+
use crate::sys;
36+
37+
/// Initialize resources used by wolfCrypt.
38+
///
39+
/// # Returns
40+
///
41+
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
42+
/// library error code value.
43+
///
44+
/// # Example
45+
///
46+
/// ```rust
47+
/// use wolfssl::wolfcrypt::*;
48+
/// wolfcrypt_init().expect("Error with wolfcrypt_init()");
49+
/// ```
50+
pub fn wolfcrypt_init() -> Result<(), i32> {
51+
let rc = unsafe { sys::wolfCrypt_Init() };
52+
if rc != 0 {
53+
return Err(rc);
54+
}
55+
Ok(())
56+
}
57+
58+
/// Clean up resources used by wolfCrypt.
59+
///
60+
/// # Returns
61+
///
62+
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
63+
/// library error code value.
64+
///
65+
/// # Example
66+
///
67+
/// ```rust
68+
/// use wolfssl::wolfcrypt::*;
69+
/// wolfcrypt_cleanup().expect("Error with wolfcrypt_cleanup()");
70+
/// ```
71+
pub fn wolfcrypt_cleanup() -> Result<(), i32> {
72+
let rc = unsafe { sys::wolfCrypt_Cleanup() };
73+
if rc != 0 {
74+
return Err(rc);
75+
}
76+
Ok(())
77+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use wolfssl::wolfcrypt::*;
2+
3+
#[test]
4+
fn test_wolfcrypt_init() {
5+
wolfcrypt_init().expect("Error with wolfcrypt_init()");
6+
}
7+
8+
#[test]
9+
fn test_wolfcrypt_cleanup() {
10+
wolfcrypt_cleanup().expect("Error with wolfcrypt_cleanup()");
11+
}

0 commit comments

Comments
 (0)