Skip to content

Commit a986b03

Browse files
Rust wrapper: Address code review feedback for ECC
1 parent 883da3d commit a986b03

2 files changed

Lines changed: 8 additions & 10 deletions

File tree

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl ECCPoint {
5959
}
6060
let wc_ecc_point = unsafe { ws::wc_ecc_new_point() };
6161
if wc_ecc_point.is_null() {
62-
return Err(0);
62+
return Err(ws::wolfCrypt_ErrorCodes_MEMORY_E);
6363
}
6464
let eccpoint = ECCPoint { wc_ecc_point };
6565
let din_size = din.len() as u32;
@@ -108,7 +108,7 @@ impl ECCPoint {
108108
}
109109
let wc_ecc_point = unsafe { ws::wc_ecc_new_point() };
110110
if wc_ecc_point.is_null() {
111-
return Err(0);
111+
return Err(ws::wolfCrypt_ErrorCodes_MEMORY_E);
112112
}
113113
let eccpoint = ECCPoint { wc_ecc_point };
114114
let din_size = din.len() as u32;
@@ -216,10 +216,10 @@ impl ECCPoint {
216216
/// use wolfssl::wolfcrypt::ecc::ECC;
217217
/// let mut rng = RNG::new().expect("Failed to create RNG");
218218
/// let mut ecc = ECC::generate(32, &mut rng).expect("Error with generate()");
219-
/// let ecc_point = ecc.make_pub_to_point(Some(&mut rng)).expect("Error with make_pub_to_point()");
219+
/// let mut ecc_point = ecc.make_pub_to_point(Some(&mut rng)).expect("Error with make_pub_to_point()");
220220
/// ecc_point.forcezero();
221221
/// ```
222-
pub fn forcezero(&self) {
222+
pub fn forcezero(&mut self) {
223223
unsafe { ws::wc_ecc_forcezero_point(self.wc_ecc_point) };
224224
}
225225
}
@@ -806,7 +806,6 @@ impl ECC {
806806
/// let _ecc2 = ECC::import_x963(x963).expect("Error with import_x963()");
807807
/// ```
808808
pub fn import_x963(din: &[u8]) -> Result<ECC, i32> {
809-
let din_ptr = din.as_ptr() as *const u8;
810809
let din_size = din.len() as u32;
811810
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
812811
let rc = unsafe { ws::wc_ecc_init(wc_ecc_key.as_mut_ptr()) };
@@ -815,7 +814,7 @@ impl ECC {
815814
}
816815
let mut wc_ecc_key = unsafe { wc_ecc_key.assume_init() };
817816
let rc = unsafe {
818-
ws::wc_ecc_import_x963(din_ptr, din_size, &mut wc_ecc_key)
817+
ws::wc_ecc_import_x963(din.as_ptr(), din_size, &mut wc_ecc_key)
819818
};
820819
if rc != 0 {
821820
unsafe { ws::wc_ecc_free(&mut wc_ecc_key); }
@@ -857,7 +856,6 @@ impl ECC {
857856
/// let _ecc2 = ECC::import_x963_ex(x963, curve_id).expect("Error with import_x963_ex()");
858857
/// ```
859858
pub fn import_x963_ex(din: &[u8], curve_id: i32) -> Result<ECC, i32> {
860-
let din_ptr = din.as_ptr() as *const u8;
861859
let din_size = din.len() as u32;
862860
let mut wc_ecc_key: MaybeUninit<ws::ecc_key> = MaybeUninit::uninit();
863861
let rc = unsafe { ws::wc_ecc_init(wc_ecc_key.as_mut_ptr()) };
@@ -866,7 +864,7 @@ impl ECC {
866864
}
867865
let mut wc_ecc_key = unsafe { wc_ecc_key.assume_init() };
868866
let rc = unsafe {
869-
ws::wc_ecc_import_x963_ex(din_ptr, din_size, &mut wc_ecc_key, curve_id)
867+
ws::wc_ecc_import_x963_ex(din.as_ptr(), din_size, &mut wc_ecc_key, curve_id)
870868
};
871869
if rc != 0 {
872870
unsafe { ws::wc_ecc_free(&mut wc_ecc_key); }
@@ -1385,7 +1383,7 @@ impl ECC {
13851383
};
13861384
let wc_ecc_point = unsafe { ws::wc_ecc_new_point() };
13871385
if wc_ecc_point.is_null() {
1388-
return Err(0);
1386+
return Err(ws::wolfCrypt_ErrorCodes_MEMORY_E);
13891387
}
13901388
let ecc_point = ECCPoint { wc_ecc_point };
13911389
let rc = unsafe {

wrapper/rust/wolfssl/tests/test_ecc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ fn test_ecc_point() {
247247
let curve_id = ECC::SECP256R1;
248248
let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
249249
let mut ecc = ECC::generate_ex(curve_size, &mut rng, curve_id).expect("Error with generate()");
250-
let ecc_point = ecc.make_pub_to_point(Some(&mut rng)).expect("Error with make_pub_to_point()");
250+
let mut ecc_point = ecc.make_pub_to_point(Some(&mut rng)).expect("Error with make_pub_to_point()");
251251
let mut der = [0u8; 128];
252252
let size = ecc_point.export_der(&mut der, curve_id).expect("Error with export_der()");
253253
assert!(size > 0 && size <= der.len());

0 commit comments

Comments
 (0)