Skip to content

Commit 337f9ed

Browse files
committed
feat(crypto): Implement JwtSigner and JwtVerifier for aws-lc-rs
1 parent 0886064 commit 337f9ed

File tree

13 files changed

+290
-362
lines changed

13 files changed

+290
-362
lines changed

Cargo.toml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ base64 = "0.22"
2727
pem = { version = "3", optional = true }
2828
simple_asn1 = { version = "0.6", optional = true }
2929

30-
hmac = "0.12.1"
3130
rsa = "0.9.6"
3231
sha2 = { version = "0.10.7", features = ["oid"] }
3332
getrandom = { version = "0.2.10", features = ["js"] }
@@ -37,6 +36,13 @@ p256 = { version = "0.13.2", features = ["ecdsa"] }
3736
p384 = { version = "0.13.0", features = ["ecdsa"] }
3837
rand_core = "0.6.4"
3938
signature = "2.2.0"
39+
40+
# "rust_crypto" feature
41+
hmac = { version = "0.12.1", optional = true }
42+
43+
# "aws_lc_rs" feature
44+
aws-lc-rs = { version = "1.10.0", optional = true }
45+
4046
[target.'cfg(target_arch = "wasm32")'.dependencies]
4147
js-sys = "0.3"
4248

@@ -54,8 +60,10 @@ time = { version = "0.3", features = ["wasm-bindgen"] }
5460
criterion = { version = "0.4", default-features = false }
5561

5662
[features]
57-
default = ["use_pem"]
63+
default = ["use_pem", "rust_crypto"]
5864
use_pem = ["pem", "simple_asn1", 'p256/pem', 'p384/pem']
65+
rust_crypto = ["hmac"]
66+
aws_lc_rs = ["aws-lc-rs"]
5967

6068
[[bench]]
6169
name = "jwt"

src/crypto/aws_lc/hmac.rs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
}

src/crypto/aws_lc/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub(crate) mod hmac;

src/crypto/ecdsa.rs

Lines changed: 0 additions & 74 deletions
This file was deleted.

src/crypto/eddsa.rs

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)