Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(crypto): use ring for asm implementations of sha256/sha512 #27885

Merged
merged 5 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions ext/node/ops/crypto/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use digest::DynDigest;
use digest::ExtendableOutput;
use digest::Update;

mod ring_sha2;

pub struct Hasher {
pub hash: Rc<RefCell<Option<Hash>>>,
}
Expand Down Expand Up @@ -200,6 +202,24 @@ impl Hash {
match algorithm_name {
"shake128" => return Ok(Shake128(Default::default(), output_length)),
"shake256" => return Ok(Shake256(Default::default(), output_length)),
"sha256" => {
let digest = ring_sha2::RingSha256::new();
if let Some(length) = output_length {
if length != digest.output_size() {
return Err(HashError::OutputLengthMismatch);
}
}
return Ok(Hash::FixedSize(Box::new(digest)));
}
"sha512" => {
let digest = ring_sha2::RingSha512::new();
if let Some(length) = output_length {
if length != digest.output_size() {
return Err(HashError::OutputLengthMismatch);
}
}
return Ok(Hash::FixedSize(Box::new(digest)));
}
_ => {}
}

Expand Down
82 changes: 82 additions & 0 deletions ext/node/ops/crypto/digest/ring_sha2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2018-2025 the Deno authors. MIT license.

use std::marker::PhantomData;

use digest::generic_array::ArrayLength;

pub trait RingDigestAlgo {
fn algorithm() -> &'static ring::digest::Algorithm;
type OutputSize: ArrayLength<u8> + 'static;
}

pub struct RingDigest<Algo: RingDigestAlgo> {
context: ring::digest::Context,
_phantom: PhantomData<Algo>,
}

impl<Algo: RingDigestAlgo> Clone for RingDigest<Algo> {
fn clone(&self) -> Self {
Self {
context: self.context.clone(),
_phantom: self._phantom,
}
}
}

impl<Algo: RingDigestAlgo> digest::HashMarker for RingDigest<Algo> {}
impl<Algo: RingDigestAlgo> Default for RingDigest<Algo> {
fn default() -> Self {
Self {
context: ring::digest::Context::new(Algo::algorithm()),
_phantom: PhantomData,
}
}
}
impl<Algo: RingDigestAlgo> digest::Reset for RingDigest<Algo> {
fn reset(&mut self) {
self.context = ring::digest::Context::new(Algo::algorithm())
}
}
impl<Algo: RingDigestAlgo> digest::Update for RingDigest<Algo> {
fn update(&mut self, data: &[u8]) {
self.context.update(data);
}
}
impl<Algo: RingDigestAlgo> digest::OutputSizeUser for RingDigest<Algo> {
type OutputSize = Algo::OutputSize;
}
impl<Algo: RingDigestAlgo> digest::FixedOutput for RingDigest<Algo> {
fn finalize_into(self, out: &mut digest::Output<Self>) {
let result = self.context.finish();
out.copy_from_slice(result.as_ref());
}
}
impl<Algo: RingDigestAlgo> digest::FixedOutputReset for RingDigest<Algo> {
fn finalize_into_reset(&mut self, out: &mut digest::Output<Self>) {
let context = std::mem::replace(
&mut self.context,
ring::digest::Context::new(Algo::algorithm()),
);
out.copy_from_slice(context.finish().as_ref());
}
}

pub struct RingSha256Algo;
impl RingDigestAlgo for RingSha256Algo {
fn algorithm() -> &'static ring::digest::Algorithm {
&ring::digest::SHA256
}

type OutputSize = digest::typenum::U32;
}
pub struct RingSha512Algo;
impl RingDigestAlgo for RingSha512Algo {
fn algorithm() -> &'static ring::digest::Algorithm {
&ring::digest::SHA512
}

type OutputSize = digest::typenum::U64;
}

pub type RingSha256 = RingDigest<RingSha256Algo>;
pub type RingSha512 = RingDigest<RingSha512Algo>;