|
| 1 | +//! Implementations of the [`JwtSigner`] and [`JwtVerifier`] traits for the |
| 2 | +//! HMAC family of algorithms using [`aws_lc_rs`] |
| 3 | +
|
| 4 | +use aws_lc_rs::hmac; |
| 5 | +use signature::{Signer, Verifier}; |
| 6 | + |
| 7 | +use crate::crypto::{JwtSigner, JwtVerifier}; |
| 8 | +use crate::errors::Result; |
| 9 | +use crate::{Algorithm, HmacSecret}; |
| 10 | + |
| 11 | +pub struct Hs256(hmac::Key); |
| 12 | + |
| 13 | +impl Hs256 { |
| 14 | + pub(crate) fn new(secret: HmacSecret) -> Result<Self> { |
| 15 | + Ok(Self(hmac::Key::new(hmac::HMAC_SHA256, &secret))) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +impl Signer<Vec<u8>> for Hs256 { |
| 20 | + fn try_sign(&self, msg: &[u8]) -> std::result::Result<Vec<u8>, signature::Error> { |
| 21 | + Ok(hmac::sign(&self.0, msg).as_ref().to_vec()) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +impl JwtSigner for Hs256 { |
| 26 | + fn algorithm(&self) -> Algorithm { |
| 27 | + Algorithm::HS256 |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl Verifier<Vec<u8>> for Hs256 { |
| 32 | + fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), signature::Error> { |
| 33 | + hmac::verify(&self.0, msg, &signature).map_err(|err| signature::Error::from_source(err)) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl JwtVerifier for Hs256 { |
| 38 | + fn algorithm(&self) -> Algorithm { |
| 39 | + Algorithm::HS256 |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +pub struct Hs384(hmac::Key); |
| 44 | + |
| 45 | +impl Hs384 { |
| 46 | + pub(crate) fn new(secret: HmacSecret) -> Result<Self> { |
| 47 | + Ok(Self(hmac::Key::new(hmac::HMAC_SHA384, &secret))) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl Signer<Vec<u8>> for Hs384 { |
| 52 | + fn try_sign(&self, msg: &[u8]) -> std::result::Result<Vec<u8>, signature::Error> { |
| 53 | + Ok(hmac::sign(&self.0, msg).as_ref().to_vec()) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl JwtSigner for Hs384 { |
| 58 | + fn algorithm(&self) -> Algorithm { |
| 59 | + Algorithm::HS384 |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl Verifier<Vec<u8>> for Hs384 { |
| 64 | + fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), signature::Error> { |
| 65 | + hmac::verify(&self.0, msg, &signature).map_err(|err| signature::Error::from_source(err)) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl JwtVerifier for Hs384 { |
| 70 | + fn algorithm(&self) -> Algorithm { |
| 71 | + Algorithm::HS384 |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +pub struct Hs512(hmac::Key); |
| 76 | + |
| 77 | +impl Hs512 { |
| 78 | + pub(crate) fn new(secret: HmacSecret) -> Result<Self> { |
| 79 | + Ok(Self(hmac::Key::new(hmac::HMAC_SHA512, &secret))) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl Signer<Vec<u8>> for Hs512 { |
| 84 | + fn try_sign(&self, msg: &[u8]) -> std::result::Result<Vec<u8>, signature::Error> { |
| 85 | + Ok(hmac::sign(&self.0, msg).as_ref().to_vec()) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl JwtSigner for Hs512 { |
| 90 | + fn algorithm(&self) -> Algorithm { |
| 91 | + Algorithm::HS512 |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl Verifier<Vec<u8>> for Hs512 { |
| 96 | + fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), signature::Error> { |
| 97 | + hmac::verify(&self.0, msg, &signature).map_err(|err| signature::Error::from_source(err)) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +impl JwtVerifier for Hs512 { |
| 102 | + fn algorithm(&self) -> Algorithm { |
| 103 | + Algorithm::HS512 |
| 104 | + } |
| 105 | +} |
0 commit comments