aboutsummaryrefslogtreecommitdiff
path: root/rust
diff options
context:
space:
mode:
authorDavid Benjamin <davidben@google.com>2024-06-11 18:30:31 -0400
committerBoringssl LUCI CQ <boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com>2024-06-12 03:05:31 +0000
commit11acdc6abf13cc8139c30ac9455840a347793110 (patch)
tree3829fd1fbe2b2bbc79b9b20ae622b84053254086 /rust
parent962432c687f67f8df1aa6e3dd364fbc88fea4ed8 (diff)
downloadboringssl-11acdc6abf13cc8139c30ac9455840a347793110.zip
boringssl-11acdc6abf13cc8139c30ac9455840a347793110.tar.gz
boringssl-11acdc6abf13cc8139c30ac9455840a347793110.tar.bz2
Revert "Adds functionality for instantiating and using `Algorithm`" and friends.
This reverts the following commits: e1a860c3745c77cb83228dde1b73fa62eaf43930 a11277e187e407d0ef403b8a60d9a32eaab7d301 25cf1bb965ba9ae0302cbc6de4ff4dd6cdbbc016 There were a couple nuisances caused by this change, stemming from a Rust language deficiency. If you move a method from a type to a trait, this is a backwards-incompatible change that is visible to the caller. That is, Rust does not allow introducing abstractions in a backwars-compatible way! This meant you had to import Algorithm to use a hash. This made updating downstream code hard, but at least par for the course with Rust. It seems Rust just isn't a language where API stability and library evolution are possible. However, even after we pay the transition costs, downstream code needed to import WithOutputLength. This is too much of an implementation detail to leak into the public API, so revert it for now. We'll need to find some way to do this that keeps the public API reasonable. Change-Id: I82d00b47a77fe77b5893b1e9b15faef727ef9866 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/69227 Reviewed-by: Bob Beck <bbe@google.com> Auto-Submit: David Benjamin <davidben@google.com> Commit-Queue: David Benjamin <davidben@google.com> Commit-Queue: Bob Beck <bbe@google.com>
Diffstat (limited to 'rust')
-rw-r--r--rust/bssl-crypto/src/digest.rs26
-rw-r--r--rust/bssl-crypto/src/macros.rs44
2 files changed, 19 insertions, 51 deletions
diff --git a/rust/bssl-crypto/src/digest.rs b/rust/bssl-crypto/src/digest.rs
index d3d74c0..645ab80 100644
--- a/rust/bssl-crypto/src/digest.rs
+++ b/rust/bssl-crypto/src/digest.rs
@@ -16,7 +16,7 @@
//! Hash functions.
//!
//! ```
-//! use bssl_crypto::digest::{self, Algorithm, WithOutputLength};
+//! use bssl_crypto::digest;
//!
//! // One-shot hashing.
//! let digest: [u8; 32] = digest::Sha256::hash(b"hello");
@@ -45,8 +45,6 @@ unsafe impl ForeignTypeRef for MdRef {
pub trait Algorithm {
/// The size of the resulting digest.
const OUTPUT_LEN: usize;
- /// The block length (in bytes).
- const BLOCK_LEN: usize;
/// Gets a reference to a message digest algorithm to be used by the HKDF implementation.
#[doc(hidden)]
@@ -54,23 +52,6 @@ pub trait Algorithm {
/// Hashes a message.
fn hash_to_vec(input: &[u8]) -> Vec<u8>;
-
- /// Create a new context for incremental hashing.
- fn new() -> Self;
-
- /// Hash the contents of `input`.
- fn update(&mut self, input: &[u8]);
-
- /// Finish the hashing and return the digest.
- fn digest_to_vec(self) -> Vec<u8>;
-}
-
-/// Trait parameterized by the size of the output of the digest
-/// so that it can provide algorithm functions that depend on
-/// this parameter.
-pub trait WithOutputLength<const OUTPUT_LEN: usize> {
- /// Finish the hashing and return the digest.
- fn digest(self) -> [u8; OUTPUT_LEN];
}
/// The insecure SHA-1 hash algorithm.
@@ -86,7 +67,6 @@ pub struct InsecureSha1 {
unsafe_iuf_algo!(
InsecureSha1,
20,
- 64,
EVP_sha1,
SHA1,
SHA1_Init,
@@ -103,7 +83,6 @@ pub struct Sha256 {
unsafe_iuf_algo!(
Sha256,
32,
- 64,
EVP_sha256,
SHA256,
SHA256_Init,
@@ -120,7 +99,6 @@ pub struct Sha384 {
unsafe_iuf_algo!(
Sha384,
48,
- 128,
EVP_sha384,
SHA384,
SHA384_Init,
@@ -137,7 +115,6 @@ pub struct Sha512 {
unsafe_iuf_algo!(
Sha512,
64,
- 128,
EVP_sha512,
SHA512,
SHA512_Init,
@@ -154,7 +131,6 @@ pub struct Sha512_256 {
unsafe_iuf_algo!(
Sha512_256,
32,
- 128,
EVP_sha512_256,
SHA512_256,
SHA512_256_Init,
diff --git a/rust/bssl-crypto/src/macros.rs b/rust/bssl-crypto/src/macros.rs
index c9e6698..6ac3d37 100644
--- a/rust/bssl-crypto/src/macros.rs
+++ b/rust/bssl-crypto/src/macros.rs
@@ -22,10 +22,9 @@
// Safety: see the "Safety" sections within about the requirements for the
// functions named in the macro parameters.
macro_rules! unsafe_iuf_algo {
- ($name:ident, $output_len:expr, $block_len:expr, $evp_md:ident, $one_shot:ident, $init:ident, $update:ident, $final_func:ident) => {
+ ($name:ident, $output_len:expr, $evp_md:ident, $one_shot:ident, $init:ident, $update:ident, $final_func:ident) => {
impl Algorithm for $name {
const OUTPUT_LEN: usize = $output_len as usize;
- const BLOCK_LEN: usize = $block_len as usize;
fn get_md(_: sealed::Sealed) -> &'static MdRef {
// Safety:
@@ -36,9 +35,22 @@ macro_rules! unsafe_iuf_algo {
fn hash_to_vec(input: &[u8]) -> Vec<u8> {
Self::hash(input).as_slice().to_vec()
}
+ }
+
+ impl $name {
+ /// Digest `input` in a single operation.
+ pub fn hash(input: &[u8]) -> [u8; $output_len] {
+ // Safety: it is assumed that `$one_shot` indeed writes
+ // `$output_len` bytes.
+ unsafe {
+ crate::with_output_array(|out, _| {
+ bssl_sys::$one_shot(input.as_ffi_ptr(), input.len(), out);
+ })
+ }
+ }
/// Create a new context for incremental hashing.
- fn new() -> Self {
+ pub fn new() -> Self {
unsafe {
Self {
ctx: crate::initialized_struct(|ctx| {
@@ -51,7 +63,7 @@ macro_rules! unsafe_iuf_algo {
}
/// Hash the contents of `input`.
- fn update(&mut self, input: &[u8]) {
+ pub fn update(&mut self, input: &[u8]) {
// Safety: arguments point to a valid buffer.
unsafe {
bssl_sys::$update(&mut self.ctx, input.as_ffi_void_ptr(), input.len());
@@ -59,14 +71,7 @@ macro_rules! unsafe_iuf_algo {
}
/// Finish the hashing and return the digest.
- fn digest_to_vec(self) -> alloc::vec::Vec<u8> {
- WithOutputLength::<$output_len>::digest(self).to_vec()
- }
- }
-
- impl<const OUTPUT_LEN: usize> WithOutputLength<OUTPUT_LEN> for $name {
- /// Finish the hashing and return the digest.
- fn digest(mut self) -> [u8; OUTPUT_LEN] {
+ pub fn digest(mut self) -> [u8; $output_len] {
// Safety: it is assumed that `$final_func` indeed writes
// `$output_len` bytes.
unsafe {
@@ -77,19 +82,6 @@ macro_rules! unsafe_iuf_algo {
}
}
- impl $name {
- /// Digest `input` in a single operation.
- pub fn hash(input: &[u8]) -> [u8; $output_len] {
- // Safety: it is assumed that `$one_shot` indeed writes
- // `$output_len` bytes.
- unsafe {
- crate::with_output_array(|out, _| {
- bssl_sys::$one_shot(input.as_ffi_ptr(), input.len(), out);
- })
- }
- }
- }
-
impl From<$name> for [u8; $output_len] {
fn from(ctx: $name) -> [u8; $output_len] {
ctx.digest()
@@ -98,7 +90,7 @@ macro_rules! unsafe_iuf_algo {
impl From<$name> for alloc::vec::Vec<u8> {
fn from(ctx: $name) -> alloc::vec::Vec<u8> {
- ctx.digest_to_vec()
+ ctx.digest().into()
}
}