Fix FFI slice usage

Use CSlice instead of a regular Rust slice when passing pointers to C
FFI.

Change-Id: Iccd827f4c6f005d860993e97fef5e9caf514885b
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/60525
Reviewed-by: David Benjamin <davidben@google.com>
Commit-Queue: Bob Beck <bbe@google.com>
chromium-stable
Maurice Lam 1 year ago committed by Boringssl LUCI CQ
parent 5d9ffb810b
commit 77d431746d
  1. 7
      rust/bssl-crypto/src/digest.rs
  2. 5
      rust/bssl-crypto/src/ed25519.rs

@ -15,7 +15,7 @@
use core::marker::PhantomData;
use crate::ForeignTypeRef;
use crate::{ForeignTypeRef, CSlice};
/// The SHA-256 digest algorithm.
#[derive(Clone)]
@ -110,10 +110,11 @@ impl<M: Md, const OUTPUT_SIZE: usize> Digest<M, OUTPUT_SIZE> {
/// Hashes the provided input into the current digest operation.
pub fn update(&mut self, data: &[u8]) {
let data_ffi = CSlice(data);
// Safety:
// - `data` is a slice from safe Rust.
// - `data` is a CSlice from safe Rust.
let result = unsafe {
bssl_sys::EVP_DigestUpdate(&mut self.0, data.as_ptr() as *const _, data.len())
bssl_sys::EVP_DigestUpdate(&mut self.0, data_ffi.as_ptr() as *const _, data_ffi.len())
};
assert_eq!(result, 1, "bssl_sys::EVP_DigestUpdate failed");
}

@ -89,14 +89,15 @@ impl PrivateKey {
pub fn sign(&self, msg: &[u8]) -> Signature {
let mut sig_bytes = [0u8; SIGNATURE_LENGTH];
let msg_ffi = CSlice(msg);
// Safety:
// - On allocation failure we panic.
// - Signature and private keys are always the correct length.
let result = unsafe {
bssl_sys::ED25519_sign(
sig_bytes.as_mut_ptr(),
msg.as_ptr(),
msg.len(),
msg_ffi.as_ptr(),
msg_ffi.len(),
self.0.as_ptr(),
)
};

Loading…
Cancel
Save