-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(crypto): Implement JwtSigner and JwtVerifier for aws-lc-rs
- Loading branch information
Showing
13 changed files
with
290 additions
and
362 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
//! Implementations of the [`JwtSigner`] and [`JwtVerifier`] traits for the | ||
//! HMAC family of algorithms using [`aws_lc_rs`] | ||
|
||
use aws_lc_rs::hmac; | ||
use signature::{Signer, Verifier}; | ||
|
||
use crate::crypto::{JwtSigner, JwtVerifier}; | ||
use crate::errors::Result; | ||
use crate::{Algorithm, HmacSecret}; | ||
|
||
pub struct Hs256(hmac::Key); | ||
|
||
impl Hs256 { | ||
pub(crate) fn new(secret: HmacSecret) -> Result<Self> { | ||
Ok(Self(hmac::Key::new(hmac::HMAC_SHA256, &secret))) | ||
} | ||
} | ||
|
||
impl Signer<Vec<u8>> for Hs256 { | ||
fn try_sign(&self, msg: &[u8]) -> std::result::Result<Vec<u8>, signature::Error> { | ||
Ok(hmac::sign(&self.0, msg).as_ref().to_vec()) | ||
} | ||
} | ||
|
||
impl JwtSigner for Hs256 { | ||
fn algorithm(&self) -> Algorithm { | ||
Algorithm::HS256 | ||
} | ||
} | ||
|
||
impl Verifier<Vec<u8>> for Hs256 { | ||
fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), signature::Error> { | ||
hmac::verify(&self.0, msg, &signature).map_err(|err| signature::Error::from_source(err)) | ||
} | ||
} | ||
|
||
impl JwtVerifier for Hs256 { | ||
fn algorithm(&self) -> Algorithm { | ||
Algorithm::HS256 | ||
} | ||
} | ||
|
||
pub struct Hs384(hmac::Key); | ||
|
||
impl Hs384 { | ||
pub(crate) fn new(secret: HmacSecret) -> Result<Self> { | ||
Ok(Self(hmac::Key::new(hmac::HMAC_SHA384, &secret))) | ||
} | ||
} | ||
|
||
impl Signer<Vec<u8>> for Hs384 { | ||
fn try_sign(&self, msg: &[u8]) -> std::result::Result<Vec<u8>, signature::Error> { | ||
Ok(hmac::sign(&self.0, msg).as_ref().to_vec()) | ||
} | ||
} | ||
|
||
impl JwtSigner for Hs384 { | ||
fn algorithm(&self) -> Algorithm { | ||
Algorithm::HS384 | ||
} | ||
} | ||
|
||
impl Verifier<Vec<u8>> for Hs384 { | ||
fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), signature::Error> { | ||
hmac::verify(&self.0, msg, &signature).map_err(|err| signature::Error::from_source(err)) | ||
} | ||
} | ||
|
||
impl JwtVerifier for Hs384 { | ||
fn algorithm(&self) -> Algorithm { | ||
Algorithm::HS384 | ||
} | ||
} | ||
|
||
pub struct Hs512(hmac::Key); | ||
|
||
impl Hs512 { | ||
pub(crate) fn new(secret: HmacSecret) -> Result<Self> { | ||
Ok(Self(hmac::Key::new(hmac::HMAC_SHA512, &secret))) | ||
} | ||
} | ||
|
||
impl Signer<Vec<u8>> for Hs512 { | ||
fn try_sign(&self, msg: &[u8]) -> std::result::Result<Vec<u8>, signature::Error> { | ||
Ok(hmac::sign(&self.0, msg).as_ref().to_vec()) | ||
} | ||
} | ||
|
||
impl JwtSigner for Hs512 { | ||
fn algorithm(&self) -> Algorithm { | ||
Algorithm::HS512 | ||
} | ||
} | ||
|
||
impl Verifier<Vec<u8>> for Hs512 { | ||
fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), signature::Error> { | ||
hmac::verify(&self.0, msg, &signature).map_err(|err| signature::Error::from_source(err)) | ||
} | ||
} | ||
|
||
impl JwtVerifier for Hs512 { | ||
fn algorithm(&self) -> Algorithm { | ||
Algorithm::HS512 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub(crate) mod hmac; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.