Skip to content

Commit d6b80b0

Browse files
Refactor ECDSA implementation by consolidating curve and hash algorithm definitions using a macro for improved maintainability
1 parent f32f5cd commit d6b80b0

File tree

1 file changed

+48
-104
lines changed

1 file changed

+48
-104
lines changed

src/verify/ecdsa/nist.rs

Lines changed: 48 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -105,110 +105,54 @@ where
105105
}
106106
}
107107

108-
#[cfg(feature = "ecdsa-p256")]
109-
impl EcdsaCurveAlgId for ::p256::NistP256 {
110-
const PUBLIC_KEY_ALG_ID: AlgorithmIdentifier = alg_id::ECDSA_P256;
111-
}
112-
113-
#[cfg(feature = "ecdsa-p384")]
114-
impl EcdsaCurveAlgId for ::p384::NistP384 {
115-
const PUBLIC_KEY_ALG_ID: AlgorithmIdentifier = alg_id::ECDSA_P384;
116-
}
117-
118-
#[cfg(feature = "ecdsa-p521")]
119-
impl EcdsaCurveAlgId for ::p521::NistP521 {
120-
const PUBLIC_KEY_ALG_ID: AlgorithmIdentifier = alg_id::ECDSA_P521;
121-
}
122-
123-
#[cfg(feature = "hash-sha256")]
124-
impl EcdsaHashAlgId for ::sha2::Sha256 {
125-
const SIGNATURE_ALG_ID: AlgorithmIdentifier = alg_id::ECDSA_SHA256;
126-
}
127-
128-
#[cfg(feature = "hash-sha384")]
129-
impl EcdsaHashAlgId for ::sha2::Sha384 {
130-
const SIGNATURE_ALG_ID: AlgorithmIdentifier = alg_id::ECDSA_SHA384;
131-
}
132-
133-
#[cfg(feature = "hash-sha512")]
134-
impl EcdsaHashAlgId for ::sha2::Sha512 {
135-
const SIGNATURE_ALG_ID: AlgorithmIdentifier = alg_id::ECDSA_SHA512;
136-
}
137-
138-
/// Macro to generate ECDSA verifier constants
139-
macro_rules! ecdsa_const {
140-
($name:ident, $curve:path, $hash:path, $ecdsa_feat:literal, $hash_feat:literal) => {
141-
#[cfg(all(feature = $ecdsa_feat, feature = $hash_feat))]
142-
pub const $name: &dyn SignatureVerificationAlgorithm =
143-
&EcdsaVerifier::<$curve, $hash>::DEFAULT;
108+
/// Macro to generate all ECDSA hash impls, curve impls, and constants
109+
macro_rules! ecdsa_setup {
110+
(
111+
hashes: $( ($hash_ty:ty, $hash_alg:expr, $hash_feat:literal) ),* $(,)? ;
112+
curves: $( ($curve_ty:ty, $curve_alg:expr, $curve_feat:literal, $( ($const_name:ident, $hash_for_const:ty, $hash_feat_for_const:literal) ),* $(,)? ) ),* $(,)?
113+
) => {
114+
$(
115+
#[cfg(feature = $hash_feat)]
116+
impl EcdsaHashAlgId for $hash_ty {
117+
const SIGNATURE_ALG_ID: AlgorithmIdentifier = $hash_alg;
118+
}
119+
)*
120+
121+
$(
122+
#[cfg(feature = $curve_feat)]
123+
impl EcdsaCurveAlgId for $curve_ty {
124+
const PUBLIC_KEY_ALG_ID: AlgorithmIdentifier = $curve_alg;
125+
}
126+
127+
$(
128+
#[cfg(all(feature = $curve_feat, feature = $hash_feat_for_const))]
129+
pub const $const_name: &dyn SignatureVerificationAlgorithm =
130+
&EcdsaVerifier::<$curve_ty, $hash_for_const>::DEFAULT;
131+
)*
132+
)*
144133
};
145134
}
146135

147-
// P-256 curve constants
148-
ecdsa_const!(
149-
ECDSA_P256_SHA256,
150-
::p256::NistP256,
151-
::sha2::Sha256,
152-
"ecdsa-p256",
153-
"hash-sha256"
154-
);
155-
ecdsa_const!(
156-
ECDSA_P256_SHA384,
157-
::p256::NistP256,
158-
::sha2::Sha384,
159-
"ecdsa-p256",
160-
"hash-sha384"
161-
);
162-
ecdsa_const!(
163-
ECDSA_P256_SHA512,
164-
::p256::NistP256,
165-
::sha2::Sha512,
166-
"ecdsa-p256",
167-
"hash-sha512"
168-
);
169-
170-
// P-384 curve constants
171-
ecdsa_const!(
172-
ECDSA_P384_SHA256,
173-
::p384::NistP384,
174-
::sha2::Sha256,
175-
"ecdsa-p384",
176-
"hash-sha256"
177-
);
178-
ecdsa_const!(
179-
ECDSA_P384_SHA384,
180-
::p384::NistP384,
181-
::sha2::Sha384,
182-
"ecdsa-p384",
183-
"hash-sha384"
184-
);
185-
ecdsa_const!(
186-
ECDSA_P384_SHA512,
187-
::p384::NistP384,
188-
::sha2::Sha512,
189-
"ecdsa-p384",
190-
"hash-sha512"
191-
);
192-
193-
// P-521 curve constants
194-
ecdsa_const!(
195-
ECDSA_P521_SHA256,
196-
::p521::NistP521,
197-
::sha2::Sha256,
198-
"ecdsa-p521",
199-
"hash-sha256"
200-
);
201-
ecdsa_const!(
202-
ECDSA_P521_SHA384,
203-
::p521::NistP521,
204-
::sha2::Sha384,
205-
"ecdsa-p521",
206-
"hash-sha384"
207-
);
208-
ecdsa_const!(
209-
ECDSA_P521_SHA512,
210-
::p521::NistP521,
211-
::sha2::Sha512,
212-
"ecdsa-p521",
213-
"hash-sha512"
214-
);
136+
ecdsa_setup! {
137+
hashes:
138+
(::sha2::Sha256, alg_id::ECDSA_SHA256, "hash-sha256"),
139+
(::sha2::Sha384, alg_id::ECDSA_SHA384, "hash-sha384"),
140+
(::sha2::Sha512, alg_id::ECDSA_SHA512, "hash-sha512");
141+
142+
curves:
143+
(::p256::NistP256, alg_id::ECDSA_P256, "ecdsa-p256",
144+
(ECDSA_P256_SHA256, ::sha2::Sha256, "hash-sha256"),
145+
(ECDSA_P256_SHA384, ::sha2::Sha384, "hash-sha384"),
146+
(ECDSA_P256_SHA512, ::sha2::Sha512, "hash-sha512")
147+
),
148+
(::p384::NistP384, alg_id::ECDSA_P384, "ecdsa-p384",
149+
(ECDSA_P384_SHA256, ::sha2::Sha256, "hash-sha256"),
150+
(ECDSA_P384_SHA384, ::sha2::Sha384, "hash-sha384"),
151+
(ECDSA_P384_SHA512, ::sha2::Sha512, "hash-sha512")
152+
),
153+
(::p521::NistP521, alg_id::ECDSA_P521, "ecdsa-p521",
154+
(ECDSA_P521_SHA256, ::sha2::Sha256, "hash-sha256"),
155+
(ECDSA_P521_SHA384, ::sha2::Sha384, "hash-sha384"),
156+
(ECDSA_P521_SHA512, ::sha2::Sha512, "hash-sha512")
157+
)
158+
}

0 commit comments

Comments
 (0)