Skip to content

Commit 01eca4c

Browse files
committed
Reduce code duplication in crypto impls through macros
1 parent 9adb7da commit 01eca4c

File tree

6 files changed

+370
-1039
lines changed

6 files changed

+370
-1039
lines changed

src/crypto/aws_lc/ecdsa.rs

Lines changed: 54 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -12,115 +12,72 @@ use aws_lc_rs::signature::{
1212
};
1313
use signature::{Error, Signer, Verifier};
1414

15-
pub struct Es256Signer(EcdsaKeyPair);
16-
17-
impl Es256Signer {
18-
pub(crate) fn new(encoding_key: &EncodingKey) -> Result<Self> {
19-
if encoding_key.family != AlgorithmFamily::Ec {
20-
return Err(new_error(ErrorKind::InvalidKeyFormat));
15+
macro_rules! define_ecdsa_signer {
16+
($name:ident, $alg:expr, $signing_alg:expr) => {
17+
pub struct $name(EcdsaKeyPair);
18+
19+
impl $name {
20+
pub(crate) fn new(encoding_key: &EncodingKey) -> Result<Self> {
21+
if encoding_key.family != AlgorithmFamily::Ec {
22+
return Err(new_error(ErrorKind::InvalidKeyFormat));
23+
}
24+
25+
Ok(Self(
26+
EcdsaKeyPair::from_pkcs8($signing_alg, encoding_key.inner())
27+
.map_err(|_| ErrorKind::InvalidEcdsaKey)?,
28+
))
29+
}
2130
}
2231

23-
Ok(Self(
24-
EcdsaKeyPair::from_pkcs8(&ECDSA_P256_SHA256_FIXED_SIGNING, encoding_key.inner())
25-
.map_err(|_| ErrorKind::InvalidEcdsaKey)?,
26-
))
27-
}
28-
}
29-
30-
impl Signer<Vec<u8>> for Es256Signer {
31-
fn try_sign(&self, msg: &[u8]) -> std::result::Result<Vec<u8>, Error> {
32-
let rng = SystemRandom::new();
33-
let signature = self.0.sign(&rng, msg).map_err(Error::from_source)?;
34-
Ok(signature.as_ref().to_vec())
35-
}
36-
}
37-
38-
impl JwtSigner for Es256Signer {
39-
fn algorithm(&self) -> Algorithm {
40-
Algorithm::ES256
41-
}
42-
}
43-
44-
pub struct Es256Verifier(DecodingKey);
45-
46-
impl Es256Verifier {
47-
pub(crate) fn new(decoding_key: &DecodingKey) -> Result<Self> {
48-
if decoding_key.family != AlgorithmFamily::Ec {
49-
return Err(new_error(ErrorKind::InvalidKeyFormat));
32+
impl Signer<Vec<u8>> for $name {
33+
fn try_sign(&self, msg: &[u8]) -> std::result::Result<Vec<u8>, Error> {
34+
let rng = SystemRandom::new();
35+
let signature = self.0.sign(&rng, msg).map_err(Error::from_source)?;
36+
Ok(signature.as_ref().to_vec())
37+
}
5038
}
5139

52-
Ok(Self(decoding_key.clone()))
53-
}
54-
}
55-
56-
impl Verifier<Vec<u8>> for Es256Verifier {
57-
fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), Error> {
58-
ECDSA_P256_SHA256_FIXED
59-
.verify_sig(self.0.as_bytes(), msg, signature)
60-
.map_err(Error::from_source)?;
61-
Ok(())
62-
}
63-
}
64-
65-
impl JwtVerifier for Es256Verifier {
66-
fn algorithm(&self) -> Algorithm {
67-
Algorithm::ES256
68-
}
69-
}
70-
71-
pub struct Es384Signer(EcdsaKeyPair);
72-
73-
impl Es384Signer {
74-
pub(crate) fn new(encoding_key: &EncodingKey) -> Result<Self> {
75-
if encoding_key.family != AlgorithmFamily::Ec {
76-
return Err(new_error(ErrorKind::InvalidKeyFormat));
40+
impl JwtSigner for $name {
41+
fn algorithm(&self) -> Algorithm {
42+
$alg
43+
}
7744
}
78-
79-
Ok(Self(
80-
EcdsaKeyPair::from_pkcs8(&ECDSA_P384_SHA384_FIXED_SIGNING, encoding_key.inner())
81-
.map_err(|_| crate::errors::ErrorKind::InvalidEcdsaKey)?,
82-
))
83-
}
45+
};
8446
}
8547

86-
impl Signer<Vec<u8>> for Es384Signer {
87-
fn try_sign(&self, msg: &[u8]) -> std::result::Result<Vec<u8>, Error> {
88-
let rng = SystemRandom::new();
89-
let signature = self.0.sign(&rng, msg).map_err(Error::from_source)?;
90-
Ok(signature.as_ref().to_vec())
91-
}
92-
}
48+
macro_rules! define_ecdsa_verifier {
49+
($name:ident, $alg:expr, $verification_alg:expr) => {
50+
pub struct $name(DecodingKey);
9351

94-
impl JwtSigner for Es384Signer {
95-
fn algorithm(&self) -> Algorithm {
96-
Algorithm::ES384
97-
}
98-
}
52+
impl $name {
53+
pub(crate) fn new(decoding_key: &DecodingKey) -> Result<Self> {
54+
if decoding_key.family != AlgorithmFamily::Ec {
55+
return Err(new_error(ErrorKind::InvalidKeyFormat));
56+
}
9957

100-
pub struct Es384Verifier(DecodingKey);
58+
Ok(Self(decoding_key.clone()))
59+
}
60+
}
10161

102-
impl Es384Verifier {
103-
pub(crate) fn new(decoding_key: &DecodingKey) -> Result<Self> {
104-
if decoding_key.family != AlgorithmFamily::Ec {
105-
return Err(new_error(ErrorKind::InvalidKeyFormat));
62+
impl Verifier<Vec<u8>> for $name {
63+
fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), Error> {
64+
$verification_alg
65+
.verify_sig(self.0.as_bytes(), msg, signature)
66+
.map_err(Error::from_source)?;
67+
Ok(())
68+
}
10669
}
10770

108-
Ok(Self(decoding_key.clone()))
109-
}
71+
impl JwtVerifier for $name {
72+
fn algorithm(&self) -> Algorithm {
73+
$alg
74+
}
75+
}
76+
};
11077
}
11178

112-
impl Verifier<Vec<u8>> for Es384Verifier {
113-
fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), Error> {
114-
ECDSA_P384_SHA384_FIXED
115-
.verify_sig(self.0.as_bytes(), msg, signature)
116-
.map_err(Error::from_source)?;
79+
define_ecdsa_signer!(Es256Signer, Algorithm::ES256, &ECDSA_P256_SHA256_FIXED_SIGNING);
80+
define_ecdsa_verifier!(Es256Verifier, Algorithm::ES256, ECDSA_P256_SHA256_FIXED);
11781

118-
Ok(())
119-
}
120-
}
121-
122-
impl JwtVerifier for Es384Verifier {
123-
fn algorithm(&self) -> Algorithm {
124-
Algorithm::ES384
125-
}
126-
}
82+
define_ecdsa_signer!(Es384Signer, Algorithm::ES384, &ECDSA_P384_SHA384_FIXED_SIGNING);
83+
define_ecdsa_verifier!(Es384Verifier, Algorithm::ES384, ECDSA_P384_SHA384_FIXED);

src/crypto/aws_lc/hmac.rs

Lines changed: 65 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -11,140 +11,68 @@ use crate::crypto::{JwtSigner, JwtVerifier};
1111
use crate::errors::Result;
1212
use crate::{Algorithm, DecodingKey, EncodingKey};
1313

14-
pub struct Hs256Signer(hmac::Key);
15-
16-
impl Hs256Signer {
17-
pub(crate) fn new(encoding_key: &EncodingKey) -> Result<Self> {
18-
Ok(Self(hmac::Key::new(
19-
hmac::HMAC_SHA256,
20-
try_get_hmac_secret_from_encoding_key(encoding_key)?,
21-
)))
22-
}
23-
}
24-
25-
impl Signer<Vec<u8>> for Hs256Signer {
26-
fn try_sign(&self, msg: &[u8]) -> std::result::Result<Vec<u8>, signature::Error> {
27-
Ok(hmac::sign(&self.0, msg).as_ref().to_vec())
28-
}
29-
}
30-
31-
impl JwtSigner for Hs256Signer {
32-
fn algorithm(&self) -> Algorithm {
33-
Algorithm::HS256
34-
}
35-
}
36-
37-
pub struct Hs256Verifier(hmac::Key);
38-
39-
impl Hs256Verifier {
40-
pub(crate) fn new(decoding_key: &DecodingKey) -> Result<Self> {
41-
Ok(Self(hmac::Key::new(
42-
hmac::HMAC_SHA256,
43-
try_get_hmac_secret_from_decoding_key(decoding_key)?,
44-
)))
45-
}
46-
}
47-
48-
impl Verifier<Vec<u8>> for Hs256Verifier {
49-
fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), signature::Error> {
50-
hmac::verify(&self.0, msg, signature).map_err(signature::Error::from_source)
51-
}
52-
}
53-
54-
impl JwtVerifier for Hs256Verifier {
55-
fn algorithm(&self) -> Algorithm {
56-
Algorithm::HS256
57-
}
58-
}
59-
60-
pub struct Hs384Signer(hmac::Key);
61-
62-
impl Hs384Signer {
63-
pub(crate) fn new(encoding_key: &EncodingKey) -> Result<Self> {
64-
Ok(Self(hmac::Key::new(
65-
hmac::HMAC_SHA384,
66-
try_get_hmac_secret_from_encoding_key(encoding_key)?,
67-
)))
68-
}
69-
}
70-
71-
impl Signer<Vec<u8>> for Hs384Signer {
72-
fn try_sign(&self, msg: &[u8]) -> std::result::Result<Vec<u8>, signature::Error> {
73-
Ok(hmac::sign(&self.0, msg).as_ref().to_vec())
74-
}
75-
}
76-
77-
impl JwtSigner for Hs384Signer {
78-
fn algorithm(&self) -> Algorithm {
79-
Algorithm::HS384
80-
}
81-
}
82-
83-
pub struct Hs384Verifier(hmac::Key);
84-
85-
impl Hs384Verifier {
86-
pub(crate) fn new(decoding_key: &DecodingKey) -> Result<Self> {
87-
Ok(Self(hmac::Key::new(
88-
hmac::HMAC_SHA384,
89-
try_get_hmac_secret_from_decoding_key(decoding_key)?,
90-
)))
91-
}
92-
}
93-
94-
impl Verifier<Vec<u8>> for Hs384Verifier {
95-
fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), signature::Error> {
96-
hmac::verify(&self.0, msg, signature).map_err(signature::Error::from_source)
97-
}
98-
}
99-
100-
impl JwtVerifier for Hs384Verifier {
101-
fn algorithm(&self) -> Algorithm {
102-
Algorithm::HS384
103-
}
104-
}
105-
106-
pub struct Hs512Signer(hmac::Key);
107-
108-
impl Hs512Signer {
109-
pub(crate) fn new(encoding_key: &EncodingKey) -> Result<Self> {
110-
Ok(Self(hmac::Key::new(
111-
hmac::HMAC_SHA512,
112-
try_get_hmac_secret_from_encoding_key(encoding_key)?,
113-
)))
114-
}
115-
}
116-
117-
impl Signer<Vec<u8>> for Hs512Signer {
118-
fn try_sign(&self, msg: &[u8]) -> std::result::Result<Vec<u8>, signature::Error> {
119-
Ok(hmac::sign(&self.0, msg).as_ref().to_vec())
120-
}
121-
}
122-
123-
impl JwtSigner for Hs512Signer {
124-
fn algorithm(&self) -> Algorithm {
125-
Algorithm::HS512
126-
}
127-
}
128-
129-
pub struct Hs512Verifier(hmac::Key);
130-
131-
impl Hs512Verifier {
132-
pub(crate) fn new(decoding_key: &DecodingKey) -> Result<Self> {
133-
Ok(Self(hmac::Key::new(
134-
hmac::HMAC_SHA512,
135-
try_get_hmac_secret_from_decoding_key(decoding_key)?,
136-
)))
137-
}
138-
}
139-
140-
impl Verifier<Vec<u8>> for Hs512Verifier {
141-
fn verify(&self, msg: &[u8], signature: &Vec<u8>) -> std::result::Result<(), signature::Error> {
142-
hmac::verify(&self.0, msg, signature).map_err(signature::Error::from_source)
143-
}
144-
}
145-
146-
impl JwtVerifier for Hs512Verifier {
147-
fn algorithm(&self) -> Algorithm {
148-
Algorithm::HS512
149-
}
150-
}
14+
macro_rules! define_hmac_signer {
15+
($name:ident, $alg:expr, $hmac_alg:expr) => {
16+
pub struct $name(hmac::Key);
17+
18+
impl $name {
19+
pub(crate) fn new(encoding_key: &EncodingKey) -> Result<Self> {
20+
Ok(Self(hmac::Key::new(
21+
$hmac_alg,
22+
try_get_hmac_secret_from_encoding_key(encoding_key)?,
23+
)))
24+
}
25+
}
26+
27+
impl Signer<Vec<u8>> for $name {
28+
fn try_sign(&self, msg: &[u8]) -> std::result::Result<Vec<u8>, signature::Error> {
29+
Ok(hmac::sign(&self.0, msg).as_ref().to_vec())
30+
}
31+
}
32+
33+
impl JwtSigner for $name {
34+
fn algorithm(&self) -> Algorithm {
35+
$alg
36+
}
37+
}
38+
};
39+
}
40+
41+
macro_rules! define_hmac_verifier {
42+
($name:ident, $alg:expr, $hmac_alg:expr) => {
43+
pub struct $name(hmac::Key);
44+
45+
impl $name {
46+
pub(crate) fn new(decoding_key: &DecodingKey) -> Result<Self> {
47+
Ok(Self(hmac::Key::new(
48+
$hmac_alg,
49+
try_get_hmac_secret_from_decoding_key(decoding_key)?,
50+
)))
51+
}
52+
}
53+
54+
impl Verifier<Vec<u8>> for $name {
55+
fn verify(
56+
&self,
57+
msg: &[u8],
58+
signature: &Vec<u8>,
59+
) -> std::result::Result<(), signature::Error> {
60+
hmac::verify(&self.0, msg, signature).map_err(signature::Error::from_source)
61+
}
62+
}
63+
64+
impl JwtVerifier for $name {
65+
fn algorithm(&self) -> Algorithm {
66+
$alg
67+
}
68+
}
69+
};
70+
}
71+
72+
define_hmac_signer!(Hs256Signer, Algorithm::HS256, hmac::HMAC_SHA256);
73+
define_hmac_signer!(Hs384Signer, Algorithm::HS384, hmac::HMAC_SHA384);
74+
define_hmac_signer!(Hs512Signer, Algorithm::HS512, hmac::HMAC_SHA512);
75+
76+
define_hmac_verifier!(Hs256Verifier, Algorithm::HS256, hmac::HMAC_SHA256);
77+
define_hmac_verifier!(Hs384Verifier, Algorithm::HS384, hmac::HMAC_SHA384);
78+
define_hmac_verifier!(Hs512Verifier, Algorithm::HS512, hmac::HMAC_SHA512);

0 commit comments

Comments
 (0)