-
Notifications
You must be signed in to change notification settings - Fork 295
Decoupled crypto backends (the rest) #428
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
Open
sulami
wants to merge
41
commits into
Keats:master
Choose a base branch
from
sulami:decoupled-crypto-backends
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
33d2c21
support wasm
itanxiao b116e44
upgrade dependency
itanxiao e2a203f
remove ring
itanxiao 5bb1d75
Merge branch 'master' into wasm_support
itanxiao d80832c
fix wasm test failed
itanxiao 69cbfa3
fix cargo format
itanxiao a66864f
change ci branch
itanxiao 485fb9f
fix ci
itanxiao 490564c
fix ci
itanxiao a672130
fix ci
itanxiao 73ea869
remove ci branch
itanxiao a41c817
fix examples ed25519 test failed
itanxiao 6f9b553
Next version
Keats e061882
Remove downgrade steps
Keats e5ba5a1
Merge branch 'master' into new-backends
Keats 502faa4
feat(encoder): Add encoder builder
sidrubs a701547
feat(encoder): Convert to dynamic dispatch
sidrubs 6a41ba7
feat(decoder): Create decoder
sidrubs 66d54be
test: Get HMAC tests passing
sidrubs 0886064
docs: Neaten up docstrings
sidrubs 337f9ed
feat(crypto): Implement JwtSigner and JwtVerifier for aws-lc-rs
sidrubs a0431d8
feat: Remove builder style implementation
sidrubs 4225e1f
feat: Use original encoding and decoding key structs
sidrubs 78e84c1
feat(crypto): Add RSA family
sidrubs 2a50e0d
Add ECDSA via AWS-LC
sulami 19ee8f3
Implement EdDSA through AWS-LC
sulami 765c04c
Verify ES and ED keys are of the right type
sulami e1305ef
Implement RSA-PSS via AWS-LC
sulami e37ebee
Implement EdDSA via RustCrypto/Dalek
sulami 25f84c3
Implement EcDSA through RustCrypto
sulami 4b7fdfb
Implement RSA via RustCrypto
sulami 1f49f43
Clean up optional dependencies
sulami 6062c96
Fix all test-breaking issues with the RustCrypto versions
sulami 492760f
Re-add the crypto::{sign, verify} convenience functions
sulami eed1c1c
Require at least one crypto backend to be enabled
sulami 8f60acf
Merge remote-tracking branch 'upstream/master' into decoupled-crypto-…
sulami 660d89e
Ensure tests pass without use_pem feature as well
sulami 9adb7da
Fix dependency features & clippy lints
sulami 01eca4c
Reduce code duplication in crypto impls through macros
sulami 903ecff
Move try_get_hmac_secret directly into en-/decoding key impls
sulami 4664843
Re-enable the HMAC sign & verify test cases
sulami File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# Changelog | ||
|
||
## 10.0.0 (unreleased) | ||
|
||
## 9.3.1 (2024-02-06) | ||
|
||
- Update base64 | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,83 @@ | ||
//! Implementations of the [`JwtSigner`] and [`JwtVerifier`] traits for the | ||
//! ECDSA family of algorithms using [`aws_lc_rs`] | ||
|
||
use crate::algorithms::AlgorithmFamily; | ||
use crate::crypto::{JwtSigner, JwtVerifier}; | ||
use crate::errors::{new_error, ErrorKind, Result}; | ||
use crate::{Algorithm, DecodingKey, EncodingKey}; | ||
use aws_lc_rs::rand::SystemRandom; | ||
use aws_lc_rs::signature::{ | ||
EcdsaKeyPair, VerificationAlgorithm, ECDSA_P256_SHA256_FIXED, ECDSA_P256_SHA256_FIXED_SIGNING, | ||
ECDSA_P384_SHA384_FIXED, ECDSA_P384_SHA384_FIXED_SIGNING, | ||
}; | ||
use signature::{Error, Signer, Verifier}; | ||
|
||
macro_rules! define_ecdsa_signer { | ||
($name:ident, $alg:expr, $signing_alg:expr) => { | ||
pub struct $name(EcdsaKeyPair); | ||
|
||
impl $name { | ||
pub(crate) fn new(encoding_key: &EncodingKey) -> Result<Self> { | ||
if encoding_key.family != AlgorithmFamily::Ec { | ||
return Err(new_error(ErrorKind::InvalidKeyFormat)); | ||
} | ||
|
||
Ok(Self( | ||
EcdsaKeyPair::from_pkcs8($signing_alg, encoding_key.inner()) | ||
.map_err(|_| ErrorKind::InvalidEcdsaKey)?, | ||
)) | ||
} | ||
} | ||
|
||
impl Signer<Vec<u8>> for $name { | ||
fn try_sign(&self, msg: &[u8]) -> std::result::Result<Vec<u8>, Error> { | ||
let rng = SystemRandom::new(); | ||
let signature = self.0.sign(&rng, msg).map_err(Error::from_source)?; | ||
Ok(signature.as_ref().to_vec()) | ||
} | ||
} | ||
|
||
impl JwtSigner for $name { | ||
fn algorithm(&self) -> Algorithm { | ||
$alg | ||
} | ||
} | ||
}; | ||
} | ||
|
||
macro_rules! define_ecdsa_verifier { | ||
($name:ident, $alg:expr, $verification_alg:expr) => { | ||
pub struct $name(DecodingKey); | ||
|
||
impl $name { | ||
pub(crate) fn new(decoding_key: &DecodingKey) -> Result<Self> { | ||
if decoding_key.family != AlgorithmFamily::Ec { | ||
return Err(new_error(ErrorKind::InvalidKeyFormat)); | ||
} | ||
|
||
Ok(Self(decoding_key.clone())) | ||
} | ||
} | ||
|
||
impl Verifier<Vec<u8>> for $name { | ||
fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), Error> { | ||
$verification_alg | ||
.verify_sig(self.0.as_bytes(), msg, signature) | ||
.map_err(Error::from_source)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl JwtVerifier for $name { | ||
fn algorithm(&self) -> Algorithm { | ||
$alg | ||
} | ||
} | ||
}; | ||
} | ||
|
||
define_ecdsa_signer!(Es256Signer, Algorithm::ES256, &ECDSA_P256_SHA256_FIXED_SIGNING); | ||
define_ecdsa_verifier!(Es256Verifier, Algorithm::ES256, ECDSA_P256_SHA256_FIXED); | ||
|
||
define_ecdsa_signer!(Es384Signer, Algorithm::ES384, &ECDSA_P384_SHA384_FIXED_SIGNING); | ||
define_ecdsa_verifier!(Es384Verifier, Algorithm::ES384, ECDSA_P384_SHA384_FIXED); |
This file contains hidden or 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,60 @@ | ||
//! Implementations of the [`JwtSigner`] and [`JwtVerifier`] traits for EdDSA using AWS-LC-RS. | ||
|
||
use crate::algorithms::AlgorithmFamily; | ||
use crate::crypto::{JwtSigner, JwtVerifier}; | ||
use crate::errors::{new_error, ErrorKind, Result}; | ||
use crate::{Algorithm, DecodingKey, EncodingKey}; | ||
use aws_lc_rs::signature::{Ed25519KeyPair, VerificationAlgorithm, ED25519}; | ||
use signature::{Error, Signer, Verifier}; | ||
|
||
pub struct EdDSASigner(Ed25519KeyPair); | ||
|
||
impl EdDSASigner { | ||
pub(crate) fn new(encoding_key: &EncodingKey) -> Result<Self> { | ||
if encoding_key.family != AlgorithmFamily::Ed { | ||
return Err(new_error(ErrorKind::InvalidKeyFormat)); | ||
} | ||
|
||
Ok(Self( | ||
Ed25519KeyPair::from_pkcs8(encoding_key.inner()) | ||
.map_err(|_| ErrorKind::InvalidEddsaKey)?, | ||
)) | ||
} | ||
} | ||
|
||
impl Signer<Vec<u8>> for EdDSASigner { | ||
fn try_sign(&self, msg: &[u8]) -> std::result::Result<Vec<u8>, Error> { | ||
Ok(self.0.sign(msg).as_ref().to_vec()) | ||
} | ||
} | ||
|
||
impl JwtSigner for EdDSASigner { | ||
fn algorithm(&self) -> Algorithm { | ||
Algorithm::EdDSA | ||
} | ||
} | ||
|
||
pub struct EdDSAVerifier(DecodingKey); | ||
|
||
impl EdDSAVerifier { | ||
pub(crate) fn new(decoding_key: &DecodingKey) -> Result<Self> { | ||
if decoding_key.family != AlgorithmFamily::Ed { | ||
return Err(new_error(ErrorKind::InvalidKeyFormat)); | ||
} | ||
|
||
Ok(Self(decoding_key.clone())) | ||
} | ||
} | ||
|
||
impl Verifier<Vec<u8>> for EdDSAVerifier { | ||
fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), Error> { | ||
ED25519.verify_sig(self.0.as_bytes(), msg, signature).map_err(Error::from_source)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl JwtVerifier for EdDSAVerifier { | ||
fn algorithm(&self) -> Algorithm { | ||
Algorithm::EdDSA | ||
} | ||
} |
This file contains hidden or 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,69 @@ | ||
//! 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, DecodingKey, EncodingKey}; | ||
|
||
macro_rules! define_hmac_signer { | ||
($name:ident, $alg:expr, $hmac_alg:expr) => { | ||
pub struct $name(hmac::Key); | ||
|
||
impl $name { | ||
pub(crate) fn new(encoding_key: &EncodingKey) -> Result<Self> { | ||
Ok(Self(hmac::Key::new($hmac_alg, encoding_key.try_get_hmac_secret()?))) | ||
} | ||
} | ||
|
||
impl Signer<Vec<u8>> for $name { | ||
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 $name { | ||
fn algorithm(&self) -> Algorithm { | ||
$alg | ||
} | ||
} | ||
}; | ||
} | ||
|
||
macro_rules! define_hmac_verifier { | ||
($name:ident, $alg:expr, $hmac_alg:expr) => { | ||
pub struct $name(hmac::Key); | ||
|
||
impl $name { | ||
pub(crate) fn new(decoding_key: &DecodingKey) -> Result<Self> { | ||
Ok(Self(hmac::Key::new($hmac_alg, decoding_key.try_get_hmac_secret()?))) | ||
} | ||
} | ||
|
||
impl Verifier<Vec<u8>> for $name { | ||
fn verify( | ||
&self, | ||
msg: &[u8], | ||
signature: &Vec<u8>, | ||
) -> std::result::Result<(), signature::Error> { | ||
hmac::verify(&self.0, msg, signature).map_err(signature::Error::from_source) | ||
} | ||
} | ||
|
||
impl JwtVerifier for $name { | ||
fn algorithm(&self) -> Algorithm { | ||
$alg | ||
} | ||
} | ||
}; | ||
} | ||
|
||
define_hmac_signer!(Hs256Signer, Algorithm::HS256, hmac::HMAC_SHA256); | ||
define_hmac_signer!(Hs384Signer, Algorithm::HS384, hmac::HMAC_SHA384); | ||
define_hmac_signer!(Hs512Signer, Algorithm::HS512, hmac::HMAC_SHA512); | ||
|
||
define_hmac_verifier!(Hs256Verifier, Algorithm::HS256, hmac::HMAC_SHA256); | ||
define_hmac_verifier!(Hs384Verifier, Algorithm::HS384, hmac::HMAC_SHA384); | ||
define_hmac_verifier!(Hs512Verifier, Algorithm::HS512, hmac::HMAC_SHA512); |
This file contains hidden or 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,4 @@ | ||
pub(crate) mod ecdsa; | ||
pub(crate) mod eddsa; | ||
pub(crate) mod hmac; | ||
pub(crate) mod rsa; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we have a default crypto backend or let users pick the one they want?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, that's up to you. I personally think it's nice to have a reasonable default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aws_lc_rs would be a good default