11//! BIP38 encryption support
22
3- use std:: ffi:: { CStr , CString } ;
3+ use std:: ffi:: CStr ;
44use std:: os:: raw:: c_char;
55use std:: ptr;
66
77use crate :: error:: { FFIError , FFIErrorCode } ;
88use crate :: types:: FFINetwork ;
99
1010/// Encrypt a private key with BIP38
11+ ///
12+ /// # Safety
13+ ///
14+ /// This function is unsafe because it dereferences raw pointers:
15+ /// - `private_key` must be a valid, null-terminated C string
16+ /// - `passphrase` must be a valid, null-terminated C string
17+ /// - `error` must be a valid pointer to an FFIError or null
1118#[ no_mangle]
12- pub extern "C" fn bip38_encrypt_private_key (
19+ pub unsafe extern "C" fn bip38_encrypt_private_key (
1320 private_key : * const c_char ,
1421 passphrase : * const c_char ,
15- network : FFINetwork ,
22+ _network : FFINetwork ,
1623 error : * mut FFIError ,
1724) -> * mut c_char {
1825 #[ cfg( feature = "bip38" ) ]
@@ -26,31 +33,27 @@ pub extern "C" fn bip38_encrypt_private_key(
2633 return ptr:: null_mut ( ) ;
2734 }
2835
29- let privkey_str = unsafe {
30- match CStr :: from_ptr ( private_key) . to_str ( ) {
31- Ok ( s) => s,
32- Err ( _) => {
33- FFIError :: set_error (
34- error,
35- FFIErrorCode :: InvalidInput ,
36- "Invalid UTF-8 in private key" . to_string ( ) ,
37- ) ;
38- return ptr:: null_mut ( ) ;
39- }
36+ let _privkey_str = match CStr :: from_ptr ( private_key) . to_str ( ) {
37+ Ok ( s) => s,
38+ Err ( _) => {
39+ FFIError :: set_error (
40+ error,
41+ FFIErrorCode :: InvalidInput ,
42+ "Invalid UTF-8 in private key" . to_string ( ) ,
43+ ) ;
44+ return ptr:: null_mut ( ) ;
4045 }
4146 } ;
4247
43- let passphrase_str = unsafe {
44- match CStr :: from_ptr ( passphrase) . to_str ( ) {
45- Ok ( s) => s,
46- Err ( _) => {
47- FFIError :: set_error (
48- error,
49- FFIErrorCode :: InvalidInput ,
50- "Invalid UTF-8 in passphrase" . to_string ( ) ,
51- ) ;
52- return ptr:: null_mut ( ) ;
53- }
48+ let _passphrase_str = match CStr :: from_ptr ( passphrase) . to_str ( ) {
49+ Ok ( s) => s,
50+ Err ( _) => {
51+ FFIError :: set_error (
52+ error,
53+ FFIErrorCode :: InvalidInput ,
54+ "Invalid UTF-8 in passphrase" . to_string ( ) ,
55+ ) ;
56+ return ptr:: null_mut ( ) ;
5457 }
5558 } ;
5659
@@ -75,8 +78,15 @@ pub extern "C" fn bip38_encrypt_private_key(
7578}
7679
7780/// Decrypt a BIP38 encrypted private key
81+ ///
82+ /// # Safety
83+ ///
84+ /// This function is unsafe because it dereferences raw pointers:
85+ /// - `encrypted_key` must be a valid, null-terminated C string
86+ /// - `passphrase` must be a valid, null-terminated C string
87+ /// - `error` must be a valid pointer to an FFIError or null
7888#[ no_mangle]
79- pub extern "C" fn bip38_decrypt_private_key (
89+ pub unsafe extern "C" fn bip38_decrypt_private_key (
8090 encrypted_key : * const c_char ,
8191 passphrase : * const c_char ,
8292 error : * mut FFIError ,
@@ -92,31 +102,27 @@ pub extern "C" fn bip38_decrypt_private_key(
92102 return ptr:: null_mut ( ) ;
93103 }
94104
95- let encrypted_str = unsafe {
96- match CStr :: from_ptr ( encrypted_key) . to_str ( ) {
97- Ok ( s) => s,
98- Err ( _) => {
99- FFIError :: set_error (
100- error,
101- FFIErrorCode :: InvalidInput ,
102- "Invalid UTF-8 in encrypted key" . to_string ( ) ,
103- ) ;
104- return ptr:: null_mut ( ) ;
105- }
105+ let _encrypted_str = match CStr :: from_ptr ( encrypted_key) . to_str ( ) {
106+ Ok ( s) => s,
107+ Err ( _) => {
108+ FFIError :: set_error (
109+ error,
110+ FFIErrorCode :: InvalidInput ,
111+ "Invalid UTF-8 in encrypted key" . to_string ( ) ,
112+ ) ;
113+ return ptr:: null_mut ( ) ;
106114 }
107115 } ;
108116
109- let passphrase_str = unsafe {
110- match CStr :: from_ptr ( passphrase) . to_str ( ) {
111- Ok ( s) => s,
112- Err ( _) => {
113- FFIError :: set_error (
114- error,
115- FFIErrorCode :: InvalidInput ,
116- "Invalid UTF-8 in passphrase" . to_string ( ) ,
117- ) ;
118- return ptr:: null_mut ( ) ;
119- }
117+ let _passphrase_str = match CStr :: from_ptr ( passphrase) . to_str ( ) {
118+ Ok ( s) => s,
119+ Err ( _) => {
120+ FFIError :: set_error (
121+ error,
122+ FFIErrorCode :: InvalidInput ,
123+ "Invalid UTF-8 in passphrase" . to_string ( ) ,
124+ ) ;
125+ return ptr:: null_mut ( ) ;
120126 }
121127 } ;
122128
0 commit comments