Skip to content

Commit 7304d49

Browse files
committed
rust: mark rust_util_bytes as unsafe
The programmer needs to make sure the ptr/len point to a valid memory area.
1 parent 8ce40a7 commit 7304d49

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

src/rust/bitbox02-rust-c/src/p256.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ mod tests {
5858
let privkey = b"\x50\x3e\x32\xee\xb9\xca\xb8\x67\x3f\x78\x47\xc0\x47\xfa\x57\xad\x2b\xe0\x48\x5d\x07\x59\x94\x84\x13\xcc\x8c\x00\x2b\x52\x9f\xe4";
5959
let mut pubkey = [0u8; 64];
6060
rust_p256_pubkey(
61-
crate::util::rust_util_bytes(privkey.as_ptr(), privkey.len()),
61+
unsafe { crate::util::rust_util_bytes(privkey.as_ptr(), privkey.len()) },
6262
unsafe { crate::util::rust_util_bytes_mut(pubkey.as_mut_ptr(), pubkey.len()) },
6363
);
6464
}
@@ -75,8 +75,8 @@ mod tests {
7575
let msg = Sha256::digest(b"msg");
7676
let mut sig = [0u8; 64];
7777
rust_p256_sign(
78-
crate::util::rust_util_bytes(privkey.as_ptr(), privkey.len()),
79-
crate::util::rust_util_bytes(msg.as_ptr(), msg.len()),
78+
unsafe { crate::util::rust_util_bytes(privkey.as_ptr(), privkey.len()) },
79+
unsafe { crate::util::rust_util_bytes(msg.as_ptr(), msg.len()) },
8080
unsafe { crate::util::rust_util_bytes_mut(sig.as_mut_ptr(), sig.len()) },
8181
);
8282
assert_eq!(

src/rust/bitbox02-rust-c/src/util.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -101,27 +101,28 @@ impl AsMut<[u8]> for BytesMut {
101101
///
102102
/// * `buf` - Must be a valid pointer to an array of bytes
103103
/// * `len` - Length of buffer, `buf[len-1]` must be a valid dereference
104+
///
105+
/// SAFTEY: buf must not be NULL and point to a valid memory area of size `len`.
104106
#[no_mangle]
105-
pub extern "C" fn rust_util_bytes(buf: *const c_uchar, len: usize) -> Bytes {
107+
pub unsafe extern "C" fn rust_util_bytes(buf: *const c_uchar, len: usize) -> Bytes {
106108
Bytes { buf, len }
107109
}
108110

109111
/// Convert buffer to mutable slice
110112
///
111113
/// * `buf` - Must be a valid pointer to an array of bytes
112114
/// * `len` - Length of buffer, `buf[len-1]` must be a valid dereference
115+
///
116+
/// SAFTEY: buf must not be NULL and point to a valid memory area of size `len`.
113117
#[no_mangle]
114118
pub unsafe extern "C" fn rust_util_bytes_mut(buf: *mut c_uchar, len: usize) -> BytesMut {
115119
BytesMut { buf, len }
116120
}
117121

118122
/// Base58Check-encode the input.
119-
///
120-
/// #Safety
121-
/// buf and out must not be NULL and point to valid memory areas.
122123
#[cfg(feature = "c-unit-testing")]
123124
#[no_mangle]
124-
pub unsafe extern "C" fn rust_base58_encode_check(buf: Bytes, mut out: BytesMut) -> bool {
125+
pub extern "C" fn rust_base58_encode_check(buf: Bytes, mut out: BytesMut) -> bool {
125126
if buf.len == 0 {
126127
return false;
127128
}
@@ -168,36 +169,36 @@ mod tests {
168169
#[should_panic]
169170
fn create_invalid_bytes_ref() {
170171
// Calling `as_ref()` will panic because it tries to create an invalid rust slice.
171-
rust_util_bytes(core::ptr::null(), 1).as_ref();
172+
(unsafe { rust_util_bytes(core::ptr::null(), 1) }).as_ref();
172173
}
173174

174175
#[test]
175176
fn test_uint8_to_hex() {
176177
let buf = [1u8, 2, 3, 14, 15, 255];
177178
let mut string = String::from("xxxxxxxxxxxxx");
178-
rust_util_uint8_to_hex(rust_util_bytes(buf.as_ptr(), buf.len()), unsafe {
179-
rust_util_bytes_mut(string.as_mut_ptr(), string.len())
180-
});
179+
rust_util_uint8_to_hex(
180+
unsafe { rust_util_bytes(buf.as_ptr(), buf.len()) },
181+
unsafe { rust_util_bytes_mut(string.as_mut_ptr(), string.len()) },
182+
);
181183
assert_eq!(string, "0102030e0fff\0");
182184

183185
// Bigger buffer also works.
184186
let mut string = String::from("\0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
185-
rust_util_uint8_to_hex(rust_util_bytes(buf.as_ptr(), buf.len()), unsafe {
186-
rust_util_bytes_mut(string.as_mut_ptr(), string.len())
187-
});
187+
rust_util_uint8_to_hex(
188+
unsafe { rust_util_bytes(buf.as_ptr(), buf.len()) },
189+
unsafe { rust_util_bytes_mut(string.as_mut_ptr(), string.len()) },
190+
);
188191
assert_eq!(string, "0102030e0fff\0xxxxxxxxxxxxxxxxxxxxxxx");
189192
}
190193

191194
#[test]
192195
fn test_rust_base58_encode_check() {
193196
let buf = b"test";
194197
let mut result_buf = [0u8; 100];
195-
assert!(unsafe {
196-
rust_base58_encode_check(
197-
rust_util_bytes(buf.as_ptr(), buf.len()),
198-
rust_util_bytes_mut(result_buf.as_mut_ptr(), result_buf.len()),
199-
)
200-
});
198+
assert!(rust_base58_encode_check(
199+
unsafe { rust_util_bytes(buf.as_ptr(), buf.len()) },
200+
unsafe { rust_util_bytes_mut(result_buf.as_mut_ptr(), result_buf.len()) },
201+
));
201202
let expected = b"LUC1eAJa5jW\0";
202203
assert_eq!(&result_buf[..expected.len()], expected);
203204
}

0 commit comments

Comments
 (0)