Skip to content

Commit b00b194

Browse files
committed
Merge #483: move some unsafe code inside an unsafe{} boundary
0f29348 move some unsafe code inside an unsafe{} boundary (Andrew Poelstra) Pull request description: An internal function had a non-unsafe signature but could be called with data that would cause it to exhibit UB. Move the unsafety inside of the function so that the function signature now enforces soundness. Fixes #481 Top commit has no ACKs. Tree-SHA512: b1ffc643aa11e9c8d0b7a32965a1504da14f6ac3f9e0aa175d2c09d7d7b6bf84e228f64e1f57800d75500e2c65066a4991f0070a3a1d0a19c1bd84ca0dd44363
2 parents 89670c7 + 0f29348 commit b00b194

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/ecdsa/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,14 @@ impl<C: Signing> Secp256k1<C> {
257257
&self,
258258
msg: &Message,
259259
sk: &SecretKey,
260-
noncedata_ptr: *const ffi::types::c_void,
260+
noncedata: Option<&[u8; 32]>,
261261
) -> Signature {
262262
unsafe {
263263
let mut ret = ffi::Signature::new();
264+
let noncedata_ptr = match noncedata {
265+
Some(arr) => arr.as_c_ptr() as *const _,
266+
None => ptr::null(),
267+
};
264268
// We can assume the return value because it's not possible to construct
265269
// an invalid signature from a valid `Message` and `SecretKey`
266270
assert_eq!(ffi::secp256k1_ecdsa_sign(self.ctx, &mut ret, msg.as_c_ptr(),
@@ -273,7 +277,7 @@ impl<C: Signing> Secp256k1<C> {
273277
/// Constructs a signature for `msg` using the secret key `sk` and RFC6979 nonce
274278
/// Requires a signing-capable context.
275279
pub fn sign_ecdsa(&self, msg: &Message, sk: &SecretKey) -> Signature {
276-
self.sign_ecdsa_with_noncedata_pointer(msg, sk, ptr::null())
280+
self.sign_ecdsa_with_noncedata_pointer(msg, sk, None)
277281
}
278282

279283
/// Constructs a signature for `msg` using the secret key `sk` and RFC6979 nonce
@@ -287,8 +291,7 @@ impl<C: Signing> Secp256k1<C> {
287291
sk: &SecretKey,
288292
noncedata: &[u8; 32],
289293
) -> Signature {
290-
let noncedata_ptr = noncedata.as_ptr() as *const ffi::types::c_void;
291-
self.sign_ecdsa_with_noncedata_pointer(msg, sk, noncedata_ptr)
294+
self.sign_ecdsa_with_noncedata_pointer(msg, sk, Some(noncedata))
292295
}
293296

294297
fn sign_grind_with_check(

0 commit comments

Comments
 (0)