From bfce1718364fdcc75e92086bc9f41d1eff94d28e Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 21 Nov 2025 13:34:33 -0500 Subject: [PATCH] Rust wrapper: enable cargo clippy and fix several clippy warnings --- wrapper/rust/wolfssl/Makefile | 1 + wrapper/rust/wolfssl/build.rs | 7 +- wrapper/rust/wolfssl/src/wolfcrypt/aes.rs | 514 +++++++++---------- wrapper/rust/wolfssl/src/wolfcrypt/ecc.rs | 1 + wrapper/rust/wolfssl/src/wolfcrypt/kdf.rs | 9 +- wrapper/rust/wolfssl/src/wolfcrypt/random.rs | 22 +- wrapper/rust/wolfssl/src/wolfcrypt/rsa.rs | 79 ++- wrapper/rust/wolfssl/src/wolfcrypt/sha.rs | 4 +- 8 files changed, 283 insertions(+), 354 deletions(-) diff --git a/wrapper/rust/wolfssl/Makefile b/wrapper/rust/wolfssl/Makefile index 8bc689515..3944a1971 100644 --- a/wrapper/rust/wolfssl/Makefile +++ b/wrapper/rust/wolfssl/Makefile @@ -1,6 +1,7 @@ .PHONY: all all: cargo build + cargo clippy cargo doc .PHONY: test diff --git a/wrapper/rust/wolfssl/build.rs b/wrapper/rust/wolfssl/build.rs index 7da0f4605..e07d608b6 100644 --- a/wrapper/rust/wolfssl/build.rs +++ b/wrapper/rust/wolfssl/build.rs @@ -55,15 +55,12 @@ fn generate_bindings() -> Result<()> { .clang_arg(format!("-I{}", wolfssl_base_dir()?)) .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) .generate() - .map_err(|_| io::Error::new(io::ErrorKind::Other, "Failed to generate bindings"))?; + .map_err(|_| io::Error::other("Failed to generate bindings"))?; bindings .write_to_file(bindings_path()) .map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!("Couldn't write bindings: {}", e), - ) + io::Error::other(format!("Couldn't write bindings: {}", e)) }) } diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/aes.rs b/wrapper/rust/wolfssl/src/wolfcrypt/aes.rs index 02f83bb2a..168b1c88b 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/aes.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/aes.rs @@ -26,7 +26,7 @@ Encryption Standard (AES) functionality. #![cfg(aes)] use crate::sys; -use std::mem::{size_of, MaybeUninit}; +use std::mem::{size_of_val, MaybeUninit}; /// AES Cipher Block Chaining (CBC) mode. /// @@ -90,14 +90,13 @@ impl CBC { } fn init(&mut self, key: &[u8], iv: &[u8], dir: i32) -> Result<(), i32> { - let key_ptr = key.as_ptr() as *const u8; let key_size = key.len() as u32; - let iv_ptr = iv.as_ptr() as *const u8; if iv.len() as u32 != sys::WC_AES_BLOCK_SIZE { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - sys::wc_AesSetKey(&mut self.ws_aes, key_ptr, key_size, iv_ptr, dir) + sys::wc_AesSetKey(&mut self.ws_aes, key.as_ptr(), key_size, + iv.as_ptr(), dir) }; if rc != 0 { return Err(rc); @@ -112,16 +111,16 @@ impl CBC { /// # Parameters /// /// * `key`: A slice containing the encryption key to use. The key must be - /// 16, 24, or 32 bytes in length. + /// 16, 24, or 32 bytes in length. /// * `iv`: A slice containing the initialization vector (IV) to use. The - /// IV must be 16 bytes in length. + /// IV must be 16 bytes in length. /// /// # Returns /// /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn init_encrypt(&mut self, key: &[u8], iv: &[u8]) -> Result<(), i32> { - return self.init(key, iv, sys::AES_ENCRYPTION as i32); + self.init(key, iv, sys::AES_ENCRYPTION as i32) } /// Initialize a CBC instance for decryption. @@ -131,16 +130,16 @@ impl CBC { /// # Parameters /// /// * `key`: A slice containing the decryption key to use. The key must be - /// 16, 24, or 32 bytes in length. + /// 16, 24, or 32 bytes in length. /// * `iv`: A slice containing the initialization vector (IV) to use. The - /// IV must be 16 bytes in length. + /// IV must be 16 bytes in length. /// /// # Returns /// /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn init_decrypt(&mut self, key: &[u8], iv: &[u8]) -> Result<(), i32> { - return self.init(key, iv, sys::AES_DECRYPTION as i32); + self.init(key, iv, sys::AES_DECRYPTION as i32) } /// Encrypt data. @@ -150,9 +149,9 @@ impl CBC { /// # Parameters /// /// * `din`: Data to encrypt. The size of the data must be a multiple of - /// 16 bytes. + /// 16 bytes. /// * `dout`: Buffer in which to store the encrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// /// # Returns /// @@ -160,9 +159,9 @@ impl CBC { /// library return code on failure. pub fn encrypt(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } @@ -182,9 +181,9 @@ impl CBC { /// # Parameters /// /// * `din`: Data to decrypt. The size of the data must be a multiple of - /// 16 bytes. + /// 16 bytes. /// * `dout`: Buffer in which to store the decrypted data. The size of - /// the data must match that of the `din` slice. + /// the data must match that of the `din` slice. /// /// # Returns /// @@ -192,9 +191,9 @@ impl CBC { /// library return code on failure. pub fn decrypt(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } @@ -301,17 +300,16 @@ impl CCM { /// # Parameters /// /// * `key`: A slice containing the encryption key to use. The key must be - /// 16, 24, or 32 bytes in length. + /// 16, 24, or 32 bytes in length. /// /// # Returns /// /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn init(&mut self, key: &[u8]) -> Result<(), i32> { - let key_ptr = key.as_ptr() as *const u8; let key_size = key.len() as u32; let rc = unsafe { - sys::wc_AesCcmSetKey(&mut self.ws_aes, key_ptr, key_size) + sys::wc_AesCcmSetKey(&mut self.ws_aes, key.as_ptr(), key_size) }; if rc != 0 { return Err(rc); @@ -327,7 +325,7 @@ impl CCM { /// /// * `din`: Data to encrypt. /// * `dout`: Buffer in which to store the encrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// * `nonce`: Nonce (number used once). /// * `auth`: Authentication data input. /// * `auth_tag`: Buffer in which to store the authentication tag. @@ -338,23 +336,21 @@ impl CCM { /// library return code on failure. pub fn encrypt(&mut self, din: &[I], dout: &mut [O], nonce: &[N], auth: &[A], auth_tag: &mut [A]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; let nonce_ptr = nonce.as_ptr() as *const u8; - let nonce_size = (nonce.len() * size_of::()) as u32; + let nonce_size = size_of_val(nonce) as u32; let auth_ptr = auth.as_ptr() as *const u8; - let auth_size = (auth.len() * size_of::()) as u32; - let auth_tag_ptr = auth_tag.as_ptr() as *mut u8; - let auth_tag_size = (auth_tag.len() * size_of::()) as u32; + let auth_size = size_of_val(auth) as u32; + let auth_tag_ptr = auth_tag.as_mut_ptr() as *mut u8; + let auth_tag_size = size_of_val(auth_tag) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - sys::wc_AesCcmEncrypt(&mut self.ws_aes, out_ptr, - in_ptr, in_size, - nonce_ptr, nonce_size, - auth_tag_ptr, auth_tag_size, + sys::wc_AesCcmEncrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size, + nonce_ptr, nonce_size, auth_tag_ptr, auth_tag_size, auth_ptr, auth_size) }; if rc != 0 { @@ -371,7 +367,7 @@ impl CCM { /// /// * `din`: Data to decrypt. /// * `dout`: Buffer in which to store the decrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// * `nonce`: Nonce (number used once). /// * `auth`: Authentication data input. /// * `auth_tag`: Authentication tag input to verify. @@ -382,23 +378,21 @@ impl CCM { /// library return code on failure. pub fn decrypt(&mut self, din: &[I], dout: &mut [O], nonce: &[N], auth: &[A], auth_tag: &[A]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; let nonce_ptr = nonce.as_ptr() as *const u8; - let nonce_size = (nonce.len() * size_of::()) as u32; + let nonce_size = size_of_val(nonce) as u32; let auth_ptr = auth.as_ptr() as *const u8; - let auth_size = (auth.len() * size_of::()) as u32; + let auth_size = size_of_val(auth) as u32; let auth_tag_ptr = auth_tag.as_ptr() as *const u8; - let auth_tag_size = (auth_tag.len() * size_of::()) as u32; + let auth_tag_size = size_of_val(auth_tag) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - sys::wc_AesCcmDecrypt(&mut self.ws_aes, out_ptr, - in_ptr, in_size, - nonce_ptr, nonce_size, - auth_tag_ptr, auth_tag_size, + sys::wc_AesCcmDecrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size, + nonce_ptr, nonce_size, auth_tag_ptr, auth_tag_size, auth_ptr, auth_size) }; if rc != 0 { @@ -502,24 +496,22 @@ impl CFB { /// # Parameters /// /// * `key`: A slice containing the encryption key to use. The key must be - /// 16, 24, or 32 bytes in length. + /// 16, 24, or 32 bytes in length. /// * `iv`: A slice containing the initialization vector (IV) to use. The - /// IV must be 16 bytes in length. + /// IV must be 16 bytes in length. /// /// # Returns /// /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn init(&mut self, key: &[u8], iv: &[u8]) -> Result<(), i32> { - let key_ptr = key.as_ptr() as *const u8; let key_size = key.len() as u32; - let iv_ptr = iv.as_ptr() as *const u8; if iv.len() as u32 != sys::WC_AES_BLOCK_SIZE { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - sys::wc_AesSetKey(&mut self.ws_aes, key_ptr, key_size, - iv_ptr, sys::AES_ENCRYPTION as i32) + sys::wc_AesSetKey(&mut self.ws_aes, key.as_ptr(), key_size, + iv.as_ptr(), sys::AES_ENCRYPTION as i32) }; if rc != 0 { return Err(rc); @@ -535,7 +527,7 @@ impl CFB { /// /// * `din`: Data to encrypt. /// * `dout`: Buffer in which to store the encrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// /// # Returns /// @@ -543,9 +535,9 @@ impl CFB { /// library return code on failure. pub fn encrypt(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } @@ -566,7 +558,7 @@ impl CFB { /// /// * `din`: Data to encrypt. /// * `dout`: Buffer in which to store the encrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// /// # Returns /// @@ -574,9 +566,9 @@ impl CFB { /// library return code on failure. pub fn encrypt1(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } @@ -597,7 +589,7 @@ impl CFB { /// /// * `din`: Data to encrypt. /// * `dout`: Buffer in which to store the encrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// /// # Returns /// @@ -605,9 +597,9 @@ impl CFB { /// library return code on failure. pub fn encrypt8(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } @@ -628,7 +620,7 @@ impl CFB { /// /// * `din`: Data to decrypt. /// * `dout`: Buffer in which to store the decrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// /// # Returns /// @@ -637,9 +629,9 @@ impl CFB { #[cfg(aes_decrypt)] pub fn decrypt(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } @@ -660,7 +652,7 @@ impl CFB { /// /// * `din`: Data to decrypt. /// * `dout`: Buffer in which to store the decrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// /// # Returns /// @@ -669,9 +661,9 @@ impl CFB { #[cfg(aes_decrypt)] pub fn decrypt1(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } @@ -692,7 +684,7 @@ impl CFB { /// /// * `din`: Data to decrypt. /// * `dout`: Buffer in which to store the decrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// /// # Returns /// @@ -701,9 +693,9 @@ impl CFB { #[cfg(aes_decrypt)] pub fn decrypt8(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } @@ -810,24 +802,22 @@ impl CTR { /// # Parameters /// /// * `key`: A slice containing the encryption key to use. The key must be - /// 16, 24, or 32 bytes in length. + /// 16, 24, or 32 bytes in length. /// * `iv`: A slice containing the initialization vector (IV) to use. The - /// IV must be 16 bytes in length. + /// IV must be 16 bytes in length. /// /// # Returns /// /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn init(&mut self, key: &[u8], iv: &[u8]) -> Result<(), i32> { - let key_ptr = key.as_ptr() as *const u8; let key_size = key.len() as u32; - let iv_ptr = iv.as_ptr() as *const u8; if iv.len() as u32 != sys::WC_AES_BLOCK_SIZE { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - sys::wc_AesSetKeyDirect(&mut self.ws_aes, key_ptr, key_size, - iv_ptr, sys::AES_ENCRYPTION as i32) + sys::wc_AesSetKeyDirect(&mut self.ws_aes, key.as_ptr(), key_size, + iv.as_ptr(), sys::AES_ENCRYPTION as i32) }; if rc != 0 { return Err(rc); @@ -837,9 +827,9 @@ impl CTR { fn encrypt_decrypt(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } @@ -860,14 +850,14 @@ impl CTR { /// /// * `din`: Data to encrypt. /// * `dout`: Buffer in which to store the encrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// /// # Returns /// /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn encrypt(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { - return self.encrypt_decrypt(din, dout); + self.encrypt_decrypt(din, dout) } /// Decrypt data. @@ -878,14 +868,14 @@ impl CTR { /// /// * `din`: Data to decrypt. /// * `dout`: Buffer in which to store the decrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// /// # Returns /// /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn decrypt(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { - return self.encrypt_decrypt(din, dout); + self.encrypt_decrypt(din, dout) } } #[cfg(aes_ctr)] @@ -949,9 +939,9 @@ impl EAX { /// /// * `din`: Data to encrypt. /// * `dout`: Buffer in which to store the encrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// * `key`: Encryption key to use. The key size must be 16, 24, or 32 - /// bytes. + /// bytes. /// * `nonce`: Nonce (number used once). /// * `auth`: Authentication data input. /// * `auth_tag`: Buffer in which to store the authentication tag. @@ -962,26 +952,21 @@ impl EAX { /// library return code on failure. pub fn encrypt(din: &[I], dout: &mut [O], key: &[u8], nonce: &[u8], auth: &[u8], auth_tag: &mut [u8]) -> Result<(), i32> { - let key_ptr = key.as_ptr() as *const u8; - let key_size = key.len() as u32; - let nonce_ptr = nonce.as_ptr() as *const u8; - let nonce_size = nonce.len() as u32; - let auth_ptr = auth.as_ptr() as *const u8; - let auth_size = auth.len() as u32; - let auth_tag_ptr = auth_tag.as_ptr() as *mut u8; - let auth_tag_size = auth_tag.len() as u32; let in_ptr = din.as_ptr() as *const u8; - let in_size = din.len() as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = dout.len() as u32; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; + let key_size = key.len() as u32; + let nonce_size = nonce.len() as u32; + let auth_size = auth.len() as u32; + let auth_tag_size = auth_tag.len() as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - sys::wc_AesEaxEncryptAuth(key_ptr, key_size, out_ptr, - in_ptr, in_size, nonce_ptr, nonce_size, - auth_tag_ptr, auth_tag_size, - auth_ptr, auth_size) + sys::wc_AesEaxEncryptAuth(key.as_ptr(), key_size, out_ptr, + in_ptr, in_size, nonce.as_ptr(), nonce_size, + auth_tag.as_mut_ptr(), auth_tag_size, auth.as_ptr(), auth_size) }; if rc != 0 { return Err(rc); @@ -995,9 +980,9 @@ impl EAX { /// /// * `din`: Data to decrypt. /// * `dout`: Buffer in which to store the decrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// * `key`: Decryption key to use. The key size must be 16, 24, or 32 - /// bytes. + /// bytes. /// * `nonce`: Nonce (number used once). /// * `auth`: Authentication data input. /// * `auth_tag`: Authentication tag input to verify. @@ -1008,26 +993,21 @@ impl EAX { /// library return code on failure. pub fn decrypt(din: &[I], dout: &mut [O], key: &[u8], nonce: &[u8], auth: &[u8], auth_tag: &[u8]) -> Result<(), i32> { - let key_ptr = key.as_ptr() as *const u8; - let key_size = key.len() as u32; - let nonce_ptr = nonce.as_ptr() as *const u8; - let nonce_size = nonce.len() as u32; - let auth_ptr = auth.as_ptr() as *const u8; - let auth_size = auth.len() as u32; - let auth_tag_ptr = auth_tag.as_ptr() as *const u8; - let auth_tag_size = auth_tag.len() as u32; let in_ptr = din.as_ptr() as *const u8; - let in_size = din.len() as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = dout.len() as u32; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; + let key_size = key.len() as u32; + let nonce_size = nonce.len() as u32; + let auth_size = auth.len() as u32; + let auth_tag_size = auth_tag.len() as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - sys::wc_AesEaxDecryptAuth(key_ptr, key_size, out_ptr, - in_ptr, in_size, nonce_ptr, nonce_size, - auth_tag_ptr, auth_tag_size, - auth_ptr, auth_size) + sys::wc_AesEaxDecryptAuth(key.as_ptr(), key_size, out_ptr, + in_ptr, in_size, nonce.as_ptr(), nonce_size, + auth_tag.as_ptr(), auth_tag_size, auth.as_ptr(), auth_size) }; if rc != 0 { return Err(rc); @@ -1097,10 +1077,9 @@ impl ECB { } fn init(&mut self, key: &[u8], dir: i32) -> Result<(), i32> { - let key_ptr = key.as_ptr() as *const u8; let key_size = key.len() as u32; let rc = unsafe { - sys::wc_AesSetKey(&mut self.ws_aes, key_ptr, key_size, + sys::wc_AesSetKey(&mut self.ws_aes, key.as_ptr(), key_size, core::ptr::null(), dir) }; if rc != 0 { @@ -1116,14 +1095,14 @@ impl ECB { /// # Parameters /// /// * `key`: A slice containing the encryption key to use. The key must be - /// 16, 24, or 32 bytes in length. + /// 16, 24, or 32 bytes in length. /// /// # Returns /// /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn init_encrypt(&mut self, key: &[u8]) -> Result<(), i32> { - return self.init(key, sys::AES_ENCRYPTION as i32); + self.init(key, sys::AES_ENCRYPTION as i32) } /// Initialize a ECB instance for decryption. @@ -1133,14 +1112,14 @@ impl ECB { /// # Parameters /// /// * `key`: A slice containing the decryption key to use. The key must be - /// 16, 24, or 32 bytes in length. + /// 16, 24, or 32 bytes in length. /// /// # Returns /// /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn init_decrypt(&mut self, key: &[u8]) -> Result<(), i32> { - return self.init(key, sys::AES_DECRYPTION as i32); + self.init(key, sys::AES_DECRYPTION as i32) } /// Encrypt data. @@ -1150,9 +1129,9 @@ impl ECB { /// # Parameters /// /// * `din`: Data to encrypt. The size of the data must be a multiple of - /// 16 bytes. + /// 16 bytes. /// * `dout`: Buffer in which to store the encrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// /// # Returns /// @@ -1160,9 +1139,9 @@ impl ECB { /// library return code on failure. pub fn encrypt(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } @@ -1182,9 +1161,9 @@ impl ECB { /// # Parameters /// /// * `din`: Data to decrypt. The size of the data must be a multiple of - /// 16 bytes. + /// 16 bytes. /// * `dout`: Buffer in which to store the decrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// /// # Returns /// @@ -1192,9 +1171,9 @@ impl ECB { /// library return code on failure. pub fn decrypt(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } @@ -1305,17 +1284,16 @@ impl GCM { /// # Parameters /// /// * `key`: A slice containing the encryption key to use. The key must be - /// 16, 24, or 32 bytes in length. + /// 16, 24, or 32 bytes in length. /// /// # Returns /// /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn init(&mut self, key: &[u8]) -> Result<(), i32> { - let key_ptr = key.as_ptr() as *const u8; let key_size = key.len() as u32; let rc = unsafe { - sys::wc_AesGcmSetKey(&mut self.ws_aes, key_ptr, key_size) + sys::wc_AesGcmSetKey(&mut self.ws_aes, key.as_ptr(), key_size) }; if rc != 0 { return Err(rc); @@ -1331,7 +1309,7 @@ impl GCM { /// /// * `din`: Data to encrypt. /// * `dout`: Buffer in which to store the encrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// * `iv`: Initialization vector to use for the encryption operation. /// * `auth`: Authentication data input. /// * `auth_tag`: Buffer in which to store the authentication tag. @@ -1343,24 +1321,19 @@ impl GCM { pub fn encrypt(&mut self, din: &[I], dout: &mut [O], iv: &[u8], auth: &[u8], auth_tag: &mut [u8]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; - let iv_ptr = iv.as_ptr() as *const u8; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; let iv_size = iv.len() as u32; - let auth_ptr = auth.as_ptr() as *const u8; let auth_size = auth.len() as u32; - let auth_tag_ptr = auth_tag.as_ptr() as *mut u8; let auth_tag_size = auth_tag.len() as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - sys::wc_AesGcmEncrypt(&mut self.ws_aes, out_ptr, - in_ptr, in_size, - iv_ptr, iv_size, - auth_tag_ptr, auth_tag_size, - auth_ptr, auth_size) + sys::wc_AesGcmEncrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size, + iv.as_ptr(), iv_size, auth_tag.as_mut_ptr(), auth_tag_size, + auth.as_ptr(), auth_size) }; if rc != 0 { return Err(rc); @@ -1376,7 +1349,7 @@ impl GCM { /// /// * `din`: Data to decrypt. /// * `dout`: Buffer in which to store the decrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// * `iv`: Initialization vector to use for the decryption operation. /// * `auth`: Authentication data input. /// * `auth_tag`: Authentication tag input to verify. @@ -1388,24 +1361,19 @@ impl GCM { pub fn decrypt(&mut self, din: &[I], dout: &mut [O], iv: &[u8], auth: &[u8], auth_tag: &[u8]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; - let iv_ptr = iv.as_ptr() as *const u8; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; let iv_size = iv.len() as u32; - let auth_ptr = auth.as_ptr() as *const u8; let auth_size = auth.len() as u32; - let auth_tag_ptr = auth_tag.as_ptr() as *const u8; let auth_tag_size = auth_tag.len() as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - sys::wc_AesGcmDecrypt(&mut self.ws_aes, out_ptr, - in_ptr, in_size, - iv_ptr, iv_size, - auth_tag_ptr, auth_tag_size, - auth_ptr, auth_size) + sys::wc_AesGcmDecrypt(&mut self.ws_aes, out_ptr, in_ptr, in_size, + iv.as_ptr(), iv_size, auth_tag.as_ptr(), auth_tag_size, + auth.as_ptr(), auth_size) }; if rc != 0 { return Err(rc); @@ -1540,7 +1508,7 @@ impl GCMStream { /// # Parameters /// /// * `key`: A slice containing the encryption key to use. The key must be - /// 16, 24, or 32 bytes in length. + /// 16, 24, or 32 bytes in length. /// * `iv`: A slice containing the initialization vector (IV) to use. /// /// # Returns @@ -1548,12 +1516,11 @@ impl GCMStream { /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn init(&mut self, key: &[u8], iv: &[u8]) -> Result<(), i32> { - let key_ptr = key.as_ptr() as *const u8; let key_size = key.len() as u32; - let iv_ptr = iv.as_ptr() as *const u8; let iv_size = iv.len() as u32; let rc = unsafe { - sys::wc_AesGcmInit(&mut self.ws_aes, key_ptr, key_size, iv_ptr, iv_size) + sys::wc_AesGcmInit(&mut self.ws_aes, key.as_ptr(), key_size, + iv.as_ptr(), iv_size) }; if rc != 0 { return Err(rc); @@ -1575,7 +1542,7 @@ impl GCMStream { /// /// * `din`: Data to encrypt. /// * `dout`: Buffer in which to store the encrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// * `auth`: Authentication data input. /// /// # Returns @@ -1585,18 +1552,16 @@ impl GCMStream { pub fn encrypt_update(&mut self, din: &[I], dout: &mut [O], auth: &[u8]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; - let auth_ptr = auth.as_ptr() as *const u8; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; let auth_size = auth.len() as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { sys::wc_AesGcmEncryptUpdate(&mut self.ws_aes, out_ptr, - in_ptr, in_size, - auth_ptr, auth_size) + in_ptr, in_size, auth.as_ptr(), auth_size) }; if rc != 0 { return Err(rc); @@ -1620,10 +1585,10 @@ impl GCMStream { /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn encrypt_final(&mut self, auth_tag: &mut [u8]) -> Result<(), i32> { - let auth_tag_ptr = auth_tag.as_ptr() as *mut u8; let auth_tag_size = auth_tag.len() as u32; let rc = unsafe { - sys::wc_AesGcmEncryptFinal(&mut self.ws_aes, auth_tag_ptr, auth_tag_size) + sys::wc_AesGcmEncryptFinal(&mut self.ws_aes, + auth_tag.as_mut_ptr(), auth_tag_size) }; if rc != 0 { return Err(rc); @@ -1645,7 +1610,7 @@ impl GCMStream { /// /// * `din`: Data to encrypt. /// * `dout`: Buffer in which to store the decrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// * `auth`: Authentication data input. /// /// # Returns @@ -1655,18 +1620,16 @@ impl GCMStream { pub fn decrypt_update(&mut self, din: &[I], dout: &mut [O], auth: &[u8]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; - let auth_ptr = auth.as_ptr() as *const u8; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; let auth_size = auth.len() as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { sys::wc_AesGcmDecryptUpdate(&mut self.ws_aes, out_ptr, - in_ptr, in_size, - auth_ptr, auth_size) + in_ptr, in_size, auth.as_ptr(), auth_size) }; if rc != 0 { return Err(rc); @@ -1690,10 +1653,10 @@ impl GCMStream { /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn decrypt_final(&mut self, auth_tag: &[u8]) -> Result<(), i32> { - let auth_tag_ptr = auth_tag.as_ptr() as *const u8; let auth_tag_size = auth_tag.len() as u32; let rc = unsafe { - sys::wc_AesGcmDecryptFinal(&mut self.ws_aes, auth_tag_ptr, auth_tag_size) + sys::wc_AesGcmDecryptFinal(&mut self.ws_aes, + auth_tag.as_ptr(), auth_tag_size) }; if rc != 0 { return Err(rc); @@ -1796,24 +1759,22 @@ impl OFB { /// # Parameters /// /// * `key`: A slice containing the encryption key to use. The key must be - /// 16, 24, or 32 bytes in length. + /// 16, 24, or 32 bytes in length. /// * `iv`: A slice containing the initialization vector (IV) to use. The - /// IV must be 16 bytes in length. + /// IV must be 16 bytes in length. /// /// # Returns /// /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn init(&mut self, key: &[u8], iv: &[u8]) -> Result<(), i32> { - let key_ptr = key.as_ptr() as *const u8; let key_size = key.len() as u32; - let iv_ptr = iv.as_ptr() as *const u8; if iv.len() as u32 != sys::WC_AES_BLOCK_SIZE { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - sys::wc_AesSetKey(&mut self.ws_aes, key_ptr, key_size, iv_ptr, - sys::AES_ENCRYPTION as i32) + sys::wc_AesSetKey(&mut self.ws_aes, key.as_ptr(), + key_size, iv.as_ptr(), sys::AES_ENCRYPTION as i32) }; if rc != 0 { return Err(rc); @@ -1829,7 +1790,7 @@ impl OFB { /// /// * `din`: Data to encrypt. /// * `dout`: Buffer in which to store the encrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// /// # Returns /// @@ -1837,9 +1798,9 @@ impl OFB { /// library return code on failure. pub fn encrypt(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } @@ -1860,7 +1821,7 @@ impl OFB { /// /// * `din`: Data to decrypt. /// * `dout`: Buffer in which to store the decrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// /// # Returns /// @@ -1869,9 +1830,9 @@ impl OFB { #[cfg(aes_decrypt)] pub fn decrypt(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } @@ -1986,11 +1947,10 @@ impl XTS { } fn init(&mut self, key: &[u8], dir: i32) -> Result<(), i32> { - let key_ptr = key.as_ptr() as *const u8; let key_size = key.len() as u32; let rc = unsafe { - sys::wc_AesXtsSetKeyNoInit(&mut self.ws_xtsaes, key_ptr, key_size, - dir) + sys::wc_AesXtsSetKeyNoInit(&mut self.ws_xtsaes, + key.as_ptr(), key_size, dir) }; if rc != 0 { return Err(rc); @@ -2005,14 +1965,14 @@ impl XTS { /// # Parameters /// /// * `key`: A slice containing the encryption key to use. The key must be - /// 16, 24, or 32 bytes in length. + /// 16, 24, or 32 bytes in length. /// /// # Returns /// /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn init_encrypt(&mut self, key: &[u8]) -> Result<(), i32> { - return self.init(key, sys::AES_ENCRYPTION as i32); + self.init(key, sys::AES_ENCRYPTION as i32) } /// Initialize a XTS instance for decryption. @@ -2022,14 +1982,14 @@ impl XTS { /// # Parameters /// /// * `key`: A slice containing the decryption key to use. The key must be - /// 16, 24, or 32 bytes in length. + /// 16, 24, or 32 bytes in length. /// /// # Returns /// /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn init_decrypt(&mut self, key: &[u8]) -> Result<(), i32> { - return self.init(key, sys::AES_DECRYPTION as i32); + self.init(key, sys::AES_DECRYPTION as i32) } /// Encrypt data. @@ -2040,7 +2000,7 @@ impl XTS { /// /// * `din`: Data to encrypt. /// * `dout`: Buffer in which to store the encrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// * `tweak`: Tweak value to use for the encryption operation. /// /// # Returns @@ -2049,18 +2009,16 @@ impl XTS { /// library return code on failure. pub fn encrypt(&mut self, din: &[I], dout: &mut [O], tweak: &[u8]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; - let tweak_ptr = tweak.as_ptr() as *const u8; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; let tweak_size = tweak.len() as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { sys::wc_AesXtsEncrypt(&mut self.ws_xtsaes, out_ptr, - in_ptr, in_size, - tweak_ptr, tweak_size) + in_ptr, in_size, tweak.as_ptr(), tweak_size) }; if rc != 0 { return Err(rc); @@ -2080,9 +2038,9 @@ impl XTS { /// /// * `din`: Data to encrypt. /// * `dout`: Buffer in which to store the encrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// * `sector`: Sector number to use for encryption operation. This value - /// is expanded into a tweak value. + /// is expanded into a tweak value. /// /// # Returns /// @@ -2090,9 +2048,9 @@ impl XTS { /// library return code on failure. pub fn encrypt_sector(&mut self, din: &[I], dout: &mut [O], sector: u64) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } @@ -2117,11 +2075,11 @@ impl XTS { /// /// * `din`: Data to encrypt. /// * `dout`: Buffer in which to store the encrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// * `sector`: Sector number to use for encryption operation. This value - /// is expanded into a tweak value. + /// is expanded into a tweak value. /// * `sector_size`: Sector size. The `sector` value is internally - /// incremented every `sector_size` bytes. + /// incremented every `sector_size` bytes. /// /// # Returns /// @@ -2130,15 +2088,15 @@ impl XTS { pub fn encrypt_consecutive_sectors(&mut self, din: &[I], dout: &mut [O], sector: u64, sector_size: u32) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - sys::wc_AesXtsEncryptConsecutiveSectors(&mut self.ws_xtsaes, out_ptr, - in_ptr, in_size, sector, sector_size) + sys::wc_AesXtsEncryptConsecutiveSectors(&mut self.ws_xtsaes, + out_ptr, in_ptr, in_size, sector, sector_size) }; if rc != 0 { return Err(rc); @@ -2154,7 +2112,7 @@ impl XTS { /// /// * `din`: Data to decrypt. /// * `dout`: Buffer in which to store the decrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// * `tweak`: Tweak value to use for the decryption operation. /// /// # Returns @@ -2163,18 +2121,16 @@ impl XTS { /// library return code on failure. pub fn decrypt(&mut self, din: &[I], dout: &mut [O], tweak: &[u8]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; - let tweak_ptr = tweak.as_ptr() as *const u8; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; let tweak_size = tweak.len() as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { sys::wc_AesXtsDecrypt(&mut self.ws_xtsaes, out_ptr, - in_ptr, in_size, - tweak_ptr, tweak_size) + in_ptr, in_size, tweak.as_ptr(), tweak_size) }; if rc != 0 { return Err(rc); @@ -2194,9 +2150,9 @@ impl XTS { /// /// * `din`: Data to decrypt. /// * `dout`: Buffer in which to store the decrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// * `sector`: Sector number to use for decryption operation. This value - /// is expanded into a tweak value. + /// is expanded into a tweak value. /// /// # Returns /// @@ -2204,9 +2160,9 @@ impl XTS { /// library return code on failure. pub fn decrypt_sector(&mut self, din: &[I], dout: &mut [O], sector: u64) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } @@ -2231,11 +2187,11 @@ impl XTS { /// /// * `din`: Data to decrypt. /// * `dout`: Buffer in which to store the decrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// * `sector`: Sector number to use for decryption operation. This value - /// is expanded into a tweak value. + /// is expanded into a tweak value. /// * `sector_size`: Sector size. The `sector` value is internally - /// incremented every `sector_size` bytes. + /// incremented every `sector_size` bytes. /// /// # Returns /// @@ -2244,15 +2200,15 @@ impl XTS { pub fn decrypt_consecutive_sectors(&mut self, din: &[I], dout: &mut [O], sector: u64, sector_size: u32) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; - let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; + let out_ptr = dout.as_mut_ptr() as *mut u8; + let out_size = size_of_val(dout) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } let rc = unsafe { - sys::wc_AesXtsDecryptConsecutiveSectors(&mut self.ws_xtsaes, out_ptr, - in_ptr, in_size, sector, sector_size) + sys::wc_AesXtsDecryptConsecutiveSectors(&mut self.ws_xtsaes, + out_ptr, in_ptr, in_size, sector, sector_size) }; if rc != 0 { return Err(rc); @@ -2361,7 +2317,7 @@ impl XTSStream { /// # Parameters /// /// * `key`: A slice containing the encryption key to use. The key must be - /// 16, 24, or 32 bytes in length. + /// 16, 24, or 32 bytes in length. /// * `tweak`: Tweak value to use for the encryption operation. /// /// # Returns @@ -2369,20 +2325,18 @@ impl XTSStream { /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn init_encrypt(&mut self, key: &[u8], tweak: &[u8]) -> Result<(), i32> { - let key_ptr = key.as_ptr() as *const u8; let key_size = key.len() as u32; let rc = unsafe { - sys::wc_AesXtsSetKeyNoInit(&mut self.ws_xtsaes, key_ptr, key_size, - sys::AES_ENCRYPTION as i32) + sys::wc_AesXtsSetKeyNoInit(&mut self.ws_xtsaes, + key.as_ptr(), key_size, sys::AES_ENCRYPTION as i32) }; if rc != 0 { return Err(rc); } - let tweak_ptr = tweak.as_ptr() as *const u8; let tweak_size = tweak.len() as u32; let rc = unsafe { - sys::wc_AesXtsEncryptInit(&mut self.ws_xtsaes, tweak_ptr, tweak_size, - &mut self.ws_xtsaesstreamdata) + sys::wc_AesXtsEncryptInit(&mut self.ws_xtsaes, + tweak.as_ptr(), tweak_size, &mut self.ws_xtsaesstreamdata) }; if rc != 0 { return Err(rc); @@ -2397,7 +2351,7 @@ impl XTSStream { /// # Parameters /// /// * `key`: A slice containing the decryption key to use. The key must be - /// 16, 24, or 32 bytes in length. + /// 16, 24, or 32 bytes in length. /// * `tweak`: Tweak value to use for the decryption operation. /// /// # Returns @@ -2405,20 +2359,18 @@ impl XTSStream { /// A Result which is Ok(()) on success or an Err containing the wolfSSL /// library return code on failure. pub fn init_decrypt(&mut self, key: &[u8], tweak: &[u8]) -> Result<(), i32> { - let key_ptr = key.as_ptr() as *const u8; let key_size = key.len() as u32; let rc = unsafe { - sys::wc_AesXtsSetKeyNoInit(&mut self.ws_xtsaes, key_ptr, key_size, - sys::AES_DECRYPTION as i32) + sys::wc_AesXtsSetKeyNoInit(&mut self.ws_xtsaes, + key.as_ptr(), key_size, sys::AES_DECRYPTION as i32) }; if rc != 0 { return Err(rc); } - let tweak_ptr = tweak.as_ptr() as *const u8; let tweak_size = tweak.len() as u32; let rc = unsafe { - sys::wc_AesXtsDecryptInit(&mut self.ws_xtsaes, tweak_ptr, tweak_size, - &mut self.ws_xtsaesstreamdata) + sys::wc_AesXtsDecryptInit(&mut self.ws_xtsaes, + tweak.as_ptr(), tweak_size, &mut self.ws_xtsaesstreamdata) }; if rc != 0 { return Err(rc); @@ -2435,10 +2387,10 @@ impl XTSStream { /// # Parameters /// /// * `din`: Data to encrypt. The size of the data must be a multiple of - /// 16 bytes. A final chunk of data that is not a multiple of 16 bytes can - /// be passed in to `encrypt_final()`. + /// 16 bytes. A final chunk of data that is not a multiple of 16 bytes + /// can be passed in to `encrypt_final()`. /// * `dout`: Buffer in which to store the encrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// /// # Returns /// @@ -2446,9 +2398,9 @@ impl XTSStream { /// library return code on failure. pub fn encrypt_update(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let out_size = size_of_val(dout) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } @@ -2471,9 +2423,9 @@ impl XTSStream { /// # Parameters /// /// * `din`: Data to encrypt. The size of the data must be 0 or at least - /// 16 bytes. It does not need to be a multiple of 16 bytes. + /// 16 bytes. It does not need to be a multiple of 16 bytes. /// * `dout`: Buffer in which to store the encrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// /// # Returns /// @@ -2481,9 +2433,9 @@ impl XTSStream { /// library return code on failure. pub fn encrypt_final(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let out_size = size_of_val(dout) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } @@ -2506,10 +2458,10 @@ impl XTSStream { /// # Parameters /// /// * `din`: Data to decrypt. The size of the data must be a multiple of - /// 16 bytes. A final chunk of data that is not a multiple of 16 bytes can - /// be passed in to `decrypt_final()`. + /// 16 bytes. A final chunk of data that is not a multiple of 16 bytes + /// can be passed in to `decrypt_final()`. /// * `dout`: Buffer in which to store the decrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// /// # Returns /// @@ -2517,9 +2469,9 @@ impl XTSStream { /// library return code on failure. pub fn decrypt_update(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let out_size = size_of_val(dout) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } @@ -2542,9 +2494,9 @@ impl XTSStream { /// # Parameters /// /// * `din`: Data to decrypt. The size of the data must be 0 or at least - /// 16 bytes. It does not need to be a multiple of 16 bytes. + /// 16 bytes. It does not need to be a multiple of 16 bytes. /// * `dout`: Buffer in which to store the decrypted data. The size of - /// the buffer must match that of the `din` buffer. + /// the buffer must match that of the `din` buffer. /// /// # Returns /// @@ -2552,9 +2504,9 @@ impl XTSStream { /// library return code on failure. pub fn decrypt_final(&mut self, din: &[I], dout: &mut [O]) -> Result<(), i32> { let in_ptr = din.as_ptr() as *const u8; - let in_size = (din.len() * size_of::()) as u32; + let in_size = size_of_val(din) as u32; let out_ptr = dout.as_ptr() as *mut u8; - let out_size = (dout.len() * size_of::()) as u32; + let out_size = size_of_val(dout) as u32; if in_size != out_size { return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG); } diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/ecc.rs b/wrapper/rust/wolfssl/src/wolfcrypt/ecc.rs index f65129fef..68e2cb90d 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/ecc.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/ecc.rs @@ -1362,6 +1362,7 @@ impl ECC { /// } /// ``` #[cfg(ecc_import)] + #[allow(clippy::too_many_arguments)] pub fn export_ex(&mut self, qx: &mut [u8], qx_len: &mut u32, qy: &mut [u8], qy_len: &mut u32, d: &mut [u8], d_len: &mut u32, hex: bool) -> Result<(), i32> { diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/kdf.rs b/wrapper/rust/wolfssl/src/wolfcrypt/kdf.rs index ba434024a..534e8f72b 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/kdf.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/kdf.rs @@ -339,11 +339,9 @@ pub fn tls13_hkdf_extract_ex(typ: i32, salt: Option<&[u8]>, key: Option<&mut [u8 let mut ikm_buf = [0u8; sys::WC_MAX_DIGEST_SIZE as usize]; let mut ikm_ptr = ikm_buf.as_mut_ptr(); let mut ikm_size = 0u32; - if let Some(key) = key { - if key.len() > 0 { - ikm_ptr = key.as_mut_ptr(); - ikm_size = key.len() as u32; - } + if let Some(key) = key && !key.is_empty() { + ikm_ptr = key.as_mut_ptr(); + ikm_size = key.len() as u32; } if out.len() != HMAC::get_hmac_size_by_type(typ)? { return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E); @@ -473,6 +471,7 @@ pub fn tls13_hkdf_expand_label(typ: i32, key: &[u8], protocol: &[u8], label: &[u /// } /// ``` #[cfg(kdf_tls13)] +#[allow(clippy::too_many_arguments)] pub fn tls13_hkdf_expand_label_ex(typ: i32, key: &[u8], protocol: &[u8], label: &[u8], info: &[u8], out: &mut [u8], heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result<(), i32> { let key_size = key.len() as u32; let protocol_size = protocol.len() as u32; diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/random.rs b/wrapper/rust/wolfssl/src/wolfcrypt/random.rs index a4f9b408d..7600f2276 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/random.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/random.rs @@ -30,22 +30,20 @@ wolfSSL `WC_RNG` object. It ensures proper initialization and deallocation. ```rust use wolfssl::wolfcrypt::random::RNG; -fn main() { - // Create a RNG instance. - let mut rng = RNG::new().expect("Failed to create RNG"); +// Create a RNG instance. +let mut rng = RNG::new().expect("Failed to create RNG"); - // Generate a single random byte value. - let byte = rng.generate_byte().expect("Failed to generate a single byte"); +// Generate a single random byte value. +let byte = rng.generate_byte().expect("Failed to generate a single byte"); - // Generate a random block. - let mut buffer = [0u32; 8]; - rng.generate_block(&mut buffer).expect("Failed to generate a block"); -} +// Generate a random block. +let mut buffer = [0u32; 8]; +rng.generate_block(&mut buffer).expect("Failed to generate a block"); ``` */ use crate::sys; -use std::mem::{size_of, MaybeUninit}; +use std::mem::{size_of_val, MaybeUninit}; /// A cryptographically secure random number generator based on the wolfSSL /// library. @@ -138,7 +136,7 @@ impl RNG { /// library return code on failure. pub fn new_with_nonce_ex(nonce: &mut [T], heap: Option<*mut std::os::raw::c_void>, dev_id: Option) -> Result { let ptr = nonce.as_mut_ptr() as *mut u8; - let size: u32 = (nonce.len() * size_of::()) as u32; + let size: u32 = size_of_val(nonce) as u32; let mut rng: MaybeUninit = MaybeUninit::uninit(); let heap = match heap { Some(heap) => heap, @@ -320,7 +318,7 @@ impl RNG { /// library return code on failure. pub fn generate_block(&mut self, buf: &mut [T]) -> Result<(), i32> { let ptr = buf.as_mut_ptr() as *mut u8; - let size: u32 = (buf.len() * size_of::()) as u32; + let size: u32 = size_of_val(buf) as u32; let rc = unsafe { sys::wc_RNG_GenerateBlock(&mut self.wc_rng, ptr, size) }; if rc == 0 { Ok(()) diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/rsa.rs b/wrapper/rust/wolfssl/src/wolfcrypt/rsa.rs index e548a54d3..ab36152e5 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/rsa.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/rsa.rs @@ -215,11 +215,10 @@ impl RSA { return Err(rc); } let mut wc_rsakey = unsafe { wc_rsakey.assume_init() }; - let der_ptr = der.as_ptr() as *const u8; let der_size = der.len() as u32; let mut idx: u32 = 0; let rc = unsafe { - sys::wc_RsaPrivateKeyDecode(der_ptr, &mut idx, &mut wc_rsakey, der_size) + sys::wc_RsaPrivateKeyDecode(der.as_ptr(), &mut idx, &mut wc_rsakey, der_size) }; if rc != 0 { unsafe { sys::wc_FreeRsaKey(&mut wc_rsakey); } @@ -325,11 +324,10 @@ impl RSA { return Err(rc); } let mut wc_rsakey = unsafe { wc_rsakey.assume_init() }; - let der_ptr = der.as_ptr() as *const u8; let der_size = der.len() as u32; let mut idx: u32 = 0; let rc = unsafe { - sys::wc_RsaPublicKeyDecode(der_ptr, &mut idx, &mut wc_rsakey, der_size) + sys::wc_RsaPublicKeyDecode(der.as_ptr(), &mut idx, &mut wc_rsakey, der_size) }; if rc != 0 { unsafe { sys::wc_FreeRsaKey(&mut wc_rsakey); } @@ -496,25 +494,25 @@ impl RSA { /// &mut d, &mut d_size, &mut p, &mut p_size, &mut q, &mut q_size).expect("Error with export_key()"); /// } /// ``` + #[allow(clippy::too_many_arguments)] pub fn export_key(&mut self, e: &mut [u8], e_size: &mut u32, n: &mut [u8], n_size: &mut u32, d: &mut [u8], d_size: &mut u32, p: &mut [u8], p_size: &mut u32, q: &mut [u8], q_size: &mut u32) -> Result<(), i32> { - let e_ptr = e.as_ptr() as *mut u8; *e_size = e.len() as u32; - let n_ptr = n.as_ptr() as *mut u8; *n_size = n.len() as u32; - let d_ptr = d.as_ptr() as *mut u8; *d_size = d.len() as u32; - let p_ptr = p.as_ptr() as *mut u8; *p_size = p.len() as u32; - let q_ptr = q.as_ptr() as *mut u8; *q_size = q.len() as u32; let rc = unsafe { - sys::wc_RsaExportKey(&mut self.wc_rsakey, e_ptr, e_size, - n_ptr, n_size, d_ptr, d_size, p_ptr, p_size, q_ptr, q_size) + sys::wc_RsaExportKey(&self.wc_rsakey, + e.as_mut_ptr(), e_size, + n.as_mut_ptr(), n_size, + d.as_mut_ptr(), d_size, + p.as_mut_ptr(), p_size, + q.as_mut_ptr(), q_size) }; if rc != 0 { return Err(rc); @@ -556,13 +554,11 @@ impl RSA { pub fn export_public_key(&mut self, e: &mut [u8], e_size: &mut u32, n: &mut [u8], n_size: &mut u32) -> Result<(), i32> { - let e_ptr = e.as_ptr() as *mut u8; *e_size = e.len() as u32; - let n_ptr = n.as_ptr() as *mut u8; *n_size = n.len() as u32; let rc = unsafe { - sys::wc_RsaFlattenPublicKey(&mut self.wc_rsakey, e_ptr, e_size, - n_ptr, n_size) + sys::wc_RsaFlattenPublicKey(&self.wc_rsakey, + e.as_mut_ptr(), e_size, n.as_mut_ptr(), n_size) }; if rc != 0 { return Err(rc); @@ -670,13 +666,12 @@ impl RSA { /// assert_eq!(plain_out[0..dec_len], *plain); /// ``` pub fn public_encrypt(&mut self, din: &[u8], dout: &mut [u8], rng: &mut RNG) -> Result { - let din_ptr = din.as_ptr() as *const u8; let din_size = din.len() as u32; - let dout_ptr = dout.as_ptr() as *mut u8; let dout_size = dout.len() as u32; let rc = unsafe { - sys::wc_RsaPublicEncrypt(din_ptr, din_size, dout_ptr, dout_size, - &mut self.wc_rsakey, &mut rng.wc_rng) + sys::wc_RsaPublicEncrypt(din.as_ptr(), din_size, + dout.as_mut_ptr(), dout_size, &mut self.wc_rsakey, + &mut rng.wc_rng) }; if rc < 0 { return Err(rc); @@ -725,13 +720,11 @@ impl RSA { /// assert_eq!(plain_out[0..dec_len], *plain); /// ``` pub fn private_decrypt(&mut self, din: &[u8], dout: &mut [u8]) -> Result { - let din_ptr = din.as_ptr() as *const u8; let din_size = din.len() as u32; - let dout_ptr = dout.as_ptr() as *mut u8; let dout_size = dout.len() as u32; let rc = unsafe { - sys::wc_RsaPrivateDecrypt(din_ptr, din_size, dout_ptr, dout_size, - &mut self.wc_rsakey) + sys::wc_RsaPrivateDecrypt(din.as_ptr(), din_size, + dout.as_mut_ptr(), dout_size, &mut self.wc_rsakey) }; if rc < 0 { return Err(rc); @@ -789,12 +782,10 @@ impl RSA { /// rsa.pss_verify_check(signature, &mut verify_out, msg, RSA::HASH_TYPE_SHA256, RSA::MGF1SHA256).expect("Error with pss_verify_check()"); /// ``` pub fn pss_sign(&mut self, din: &[u8], dout: &mut [u8], hash_algo: u32, mgf: i32, rng: &mut RNG) -> Result { - let din_ptr = din.as_ptr() as *const u8; let din_size = din.len() as u32; - let dout_ptr = dout.as_ptr() as *mut u8; let dout_size = dout.len() as u32; let rc = unsafe { - sys::wc_RsaPSS_Sign(din_ptr, din_size, dout_ptr, dout_size, + sys::wc_RsaPSS_Sign(din.as_ptr(), din_size, dout.as_mut_ptr(), dout_size, hash_algo, mgf, &mut self.wc_rsakey, &mut rng.wc_rng) }; if rc < 0 { @@ -850,13 +841,11 @@ impl RSA { /// rsa.pss_verify_check(signature, &mut verify_out, msg, RSA::HASH_TYPE_SHA256, RSA::MGF1SHA256).expect("Error with pss_verify_check()"); /// ``` pub fn pss_check_padding(&mut self, din: &[u8], sig: &[u8], hash_algo: u32) -> Result<(), i32> { - let din_ptr = din.as_ptr() as *const u8; let din_size = din.len() as u32; - let sig_ptr = sig.as_ptr() as *const u8; let sig_size = sig.len() as u32; let rc = unsafe { - sys::wc_RsaPSS_CheckPadding(din_ptr, din_size, sig_ptr, sig_size, - hash_algo) + sys::wc_RsaPSS_CheckPadding(din.as_ptr(), din_size, + sig.as_ptr(), sig_size, hash_algo) }; if rc != 0 { return Err(rc); @@ -914,12 +903,11 @@ impl RSA { /// rsa.pss_verify_check(signature, &mut verify_out, msg, RSA::HASH_TYPE_SHA256, RSA::MGF1SHA256).expect("Error with pss_verify_check()"); /// ``` pub fn pss_verify(&mut self, din: &[u8], dout: &mut [u8], hash_algo: u32, mgf: i32) -> Result { - let din_ptr = din.as_ptr() as *const u8; let din_size = din.len() as u32; - let dout_ptr = dout.as_ptr() as *mut u8; let dout_size = dout.len() as u32; let rc = unsafe { - sys::wc_RsaPSS_Verify(din_ptr, din_size, dout_ptr, dout_size, + sys::wc_RsaPSS_Verify(din.as_ptr(), din_size, + dout.as_mut_ptr(), dout_size, hash_algo, mgf, &mut self.wc_rsakey) }; if rc < 0 { @@ -982,15 +970,13 @@ impl RSA { /// rsa.pss_verify_check(signature, &mut verify_out, msg, RSA::HASH_TYPE_SHA256, RSA::MGF1SHA256).expect("Error with pss_verify_check()"); /// ``` pub fn pss_verify_check(&mut self, din: &[u8], dout: &mut [u8], digest: &[u8], hash_algo: u32, mgf: i32) -> Result { - let din_ptr = din.as_ptr() as *const u8; let din_size = din.len() as u32; - let dout_ptr = dout.as_ptr() as *mut u8; let dout_size = dout.len() as u32; - let digest_ptr = digest.as_ptr() as *const u8; let digest_size = digest.len() as u32; let rc = unsafe { - sys::wc_RsaPSS_VerifyCheck(din_ptr, din_size, dout_ptr, dout_size, - digest_ptr, digest_size, hash_algo, mgf, &mut self.wc_rsakey) + sys::wc_RsaPSS_VerifyCheck(din.as_ptr(), din_size, + dout.as_mut_ptr(), dout_size, digest.as_ptr(), digest_size, + hash_algo, mgf, &mut self.wc_rsakey) }; if rc < 0 { return Err(rc); @@ -1047,12 +1033,11 @@ impl RSA { /// ``` #[cfg(rsa_direct)] pub fn rsa_direct(&mut self, din: &[u8], dout: &mut [u8], typ: i32, rng: &mut RNG) -> Result { - let din_ptr = din.as_ptr() as *const u8; let din_size = din.len() as u32; - let dout_ptr = dout.as_ptr() as *mut u8; let mut dout_size = dout.len() as u32; let rc = unsafe { - sys::wc_RsaDirect(din_ptr, din_size, dout_ptr, &mut dout_size, + sys::wc_RsaDirect(din.as_ptr(), din_size, + dout.as_mut_ptr(), &mut dout_size, &mut self.wc_rsakey, typ, &mut rng.wc_rng) }; if rc < 0 { @@ -1093,7 +1078,6 @@ impl RSA { /// let mut enc: [u8; 512] = [0; 512]; /// let enc_len = rsa.public_encrypt(plain, &mut enc, &mut rng).expect("Error with public_encrypt()"); /// assert!(enc_len > 0 && enc_len <= 512); - /// let key_path = "../../../certs/client-key.der"; /// let der: Vec = fs::read(key_path).expect("Error reading key file"); /// let mut rsa = RSA::new_from_der(&der).expect("Error with new_from_der()"); @@ -1156,12 +1140,11 @@ impl RSA { /// assert!(verify_out_size > 0 && verify_out_size <= 512); /// ``` pub fn ssl_sign(&mut self, din: &[u8], dout: &mut [u8], rng: &mut RNG) -> Result { - let din_ptr = din.as_ptr() as *const u8; let din_size = din.len() as u32; - let dout_ptr = dout.as_ptr() as *mut u8; let dout_size = dout.len() as u32; let rc = unsafe { - sys::wc_RsaSSL_Sign(din_ptr, din_size, dout_ptr, dout_size, + sys::wc_RsaSSL_Sign(din.as_ptr(), din_size, + dout.as_mut_ptr(), dout_size, &mut self.wc_rsakey, &mut rng.wc_rng) }; if rc < 0 { @@ -1214,13 +1197,11 @@ impl RSA { /// assert!(verify_out_size > 0 && verify_out_size <= 512); /// ``` pub fn ssl_verify(&mut self, din: &[u8], dout: &mut [u8]) -> Result { - let din_ptr = din.as_ptr() as *const u8; let din_size = din.len() as u32; - let dout_ptr = dout.as_ptr() as *mut u8; let dout_size = dout.len() as u32; let rc = unsafe { - sys::wc_RsaSSL_Verify(din_ptr, din_size, dout_ptr, dout_size, - &mut self.wc_rsakey) + sys::wc_RsaSSL_Verify(din.as_ptr(), din_size, + dout.as_mut_ptr(), dout_size, &mut self.wc_rsakey) }; if rc < 0 { return Err(rc); diff --git a/wrapper/rust/wolfssl/src/wolfcrypt/sha.rs b/wrapper/rust/wolfssl/src/wolfcrypt/sha.rs index 63696d4c4..a4873454f 100644 --- a/wrapper/rust/wolfssl/src/wolfcrypt/sha.rs +++ b/wrapper/rust/wolfssl/src/wolfcrypt/sha.rs @@ -2098,7 +2098,7 @@ impl SHAKE128 { if dout_size % (Self::SQUEEZE_BLOCK_SIZE as u32) != 0 { return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E); } - let n_blocks = (dout_size / (Self::SQUEEZE_BLOCK_SIZE as u32)) as u32; + let n_blocks = dout_size / (Self::SQUEEZE_BLOCK_SIZE as u32); let rc = unsafe { sys::wc_Shake128_SqueezeBlocks(&mut self.wc_shake, dout.as_mut_ptr(), n_blocks) }; @@ -2368,7 +2368,7 @@ impl SHAKE256 { if dout_size % (Self::SQUEEZE_BLOCK_SIZE as u32) != 0 { return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E); } - let n_blocks = (dout_size / (Self::SQUEEZE_BLOCK_SIZE as u32)) as u32; + let n_blocks = dout_size / (Self::SQUEEZE_BLOCK_SIZE as u32); let rc = unsafe { sys::wc_Shake256_SqueezeBlocks(&mut self.wc_shake, dout.as_mut_ptr(), n_blocks) };