Skip to content

Commit

Permalink
feat: add sha-384 hashing
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Zak <richard@profian.com>
  • Loading branch information
rjzak committed Oct 7, 2022
1 parent 54330b7 commit 043238f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 2 additions & 0 deletions implementations/hostcalls/rust/src/symmetric/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ pub enum SymmetricAlgorithm {
HkdfSha256Expand,
HkdfSha512Expand,
Sha256,
Sha384,
Sha512,
Sha512_256,
Aes128Gcm,
Expand All @@ -162,6 +163,7 @@ impl TryFrom<&str> for SymmetricAlgorithm {
"HMAC/SHA-256" => Ok(SymmetricAlgorithm::HmacSha256),
"HMAC/SHA-512" => Ok(SymmetricAlgorithm::HmacSha512),
"SHA-256" => Ok(SymmetricAlgorithm::Sha256),
"SHA-384" => Ok(SymmetricAlgorithm::Sha384),
"SHA-512" => Ok(SymmetricAlgorithm::Sha512),
"SHA-512/256" => Ok(SymmetricAlgorithm::Sha512_256),
"AES-128-GCM" => Ok(SymmetricAlgorithm::Aes128Gcm),
Expand Down
6 changes: 5 additions & 1 deletion implementations/hostcalls/rust/src/symmetric/sha2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ::sha2::{Digest, Sha256, Sha512, Sha512_256};
use ::sha2::{Digest, Sha256, Sha384, Sha512, Sha512_256};

use super::state::*;
use super::*;
Expand All @@ -7,6 +7,7 @@ use super::*;
#[derive(Debug, Clone)]
enum HashVariant {
Sha256(Sha256),
Sha384(Sha384),
Sha512(Sha512),
Sha512_256(Sha512_256),
}
Expand All @@ -32,6 +33,7 @@ impl Sha2SymmetricState {
}
let ctx = match alg {
SymmetricAlgorithm::Sha256 => HashVariant::Sha256(Sha256::new()),
SymmetricAlgorithm::Sha384 => HashVariant::Sha384(Sha384::new()),
SymmetricAlgorithm::Sha512 => HashVariant::Sha512(Sha512::new()),
SymmetricAlgorithm::Sha512_256 => HashVariant::Sha512_256(Sha512_256::new()),
_ => bail!(CryptoError::UnsupportedAlgorithm),
Expand Down Expand Up @@ -71,6 +73,7 @@ impl SymmetricStateLike for Sha2SymmetricState {
fn absorb_unchecked(&mut self, data: &[u8]) -> Result<(), CryptoError> {
match &mut self.ctx {
HashVariant::Sha256(x) => x.update(data),
HashVariant::Sha384(x) => x.update(data),
HashVariant::Sha512(x) => x.update(data),
HashVariant::Sha512_256(x) => x.update(data),
};
Expand All @@ -80,6 +83,7 @@ impl SymmetricStateLike for Sha2SymmetricState {
fn squeeze_unchecked(&mut self, out: &mut [u8]) -> Result<(), CryptoError> {
let raw = match &self.ctx {
HashVariant::Sha256(x) => x.clone().finalize().to_vec(),
HashVariant::Sha384(x) => x.clone().finalize().to_vec(),
HashVariant::Sha512(x) => x.clone().finalize().to_vec(),
HashVariant::Sha512_256(x) => x.clone().finalize().to_vec(),
};
Expand Down

0 comments on commit 043238f

Please sign in to comment.