Skip to content

Commit 85d39bc

Browse files
committed
Add aws-lc-rs as optional dependency
Export SignatureVerificationAlgorithms backed by it in webpki::aws_lc_rs
1 parent bdbd388 commit 85d39bc

File tree

4 files changed

+209
-2
lines changed

4 files changed

+209
-2
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ include = [
2929
"/LICENSE",
3030
"README.md",
3131

32+
"src/aws_lc_rs_algs.rs",
3233
"src/calendar.rs",
3334
"src/cert.rs",
3435
"src/crl/mod.rs",
@@ -66,11 +67,13 @@ name = "webpki"
6667

6768
[features]
6869
default = ["std", "ring"]
69-
ring = ["dep:ring"]
70+
aws_lc_rs = ["dep:aws-lc-rs"]
7071
alloc = ["ring?/alloc", "pki-types/alloc"]
72+
ring = ["dep:ring"]
7173
std = ["alloc"]
7274

7375
[dependencies]
76+
aws-lc-rs = { version = "1.0.0", optional = true }
7477
pki-types = { package = "rustls-pki-types", version = "0.2.1", default-features = false }
7578
ring = { version = "0.16.19", default-features = false, optional = true }
7679
untrusted = "0.7.1"

src/alg_tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1313
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1414

15+
#![allow(clippy::duplicate_mod)]
16+
1517
use base64::{engine::general_purpose, Engine as _};
1618

1719
use crate::error::{DerTypeId, Error};

src/aws_lc_rs_algs.rs

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
use aws_lc_rs::signature;
2+
use pki_types::{AlgorithmIdentifier, InvalidSignature, SignatureVerificationAlgorithm};
3+
4+
use crate::signed_data::alg_id;
5+
6+
// nb. aws-lc-rs has an API that is broadly compatible with *ring*,
7+
// so this is very similar to ring_algs.rs.
8+
9+
/// A `SignatureVerificationAlgorithm` implemented using aws-lc-rs.
10+
struct AwsLcRsAlgorithm {
11+
public_key_alg_id: AlgorithmIdentifier,
12+
signature_alg_id: AlgorithmIdentifier,
13+
verification_alg: &'static dyn signature::VerificationAlgorithm,
14+
}
15+
16+
impl SignatureVerificationAlgorithm for AwsLcRsAlgorithm {
17+
fn public_key_alg_id(&self) -> AlgorithmIdentifier {
18+
self.public_key_alg_id
19+
}
20+
21+
fn signature_alg_id(&self) -> AlgorithmIdentifier {
22+
self.signature_alg_id
23+
}
24+
25+
fn verify_signature(
26+
&self,
27+
public_key: &[u8],
28+
message: &[u8],
29+
signature: &[u8],
30+
) -> Result<(), InvalidSignature> {
31+
signature::UnparsedPublicKey::new(self.verification_alg, public_key)
32+
.verify(message, signature)
33+
.map_err(|_| InvalidSignature)
34+
}
35+
}
36+
37+
/// ECDSA signatures using the P-256 curve and SHA-256.
38+
pub static ECDSA_P256_SHA256: &dyn SignatureVerificationAlgorithm = &AwsLcRsAlgorithm {
39+
public_key_alg_id: alg_id::ECDSA_P256,
40+
signature_alg_id: alg_id::ECDSA_SHA256,
41+
verification_alg: &signature::ECDSA_P256_SHA256_ASN1,
42+
};
43+
44+
/// ECDSA signatures using the P-256 curve and SHA-384. Deprecated.
45+
pub static ECDSA_P256_SHA384: &dyn SignatureVerificationAlgorithm = &AwsLcRsAlgorithm {
46+
public_key_alg_id: alg_id::ECDSA_P256,
47+
signature_alg_id: alg_id::ECDSA_SHA384,
48+
verification_alg: &signature::ECDSA_P256_SHA384_ASN1,
49+
};
50+
51+
/// ECDSA signatures using the P-384 curve and SHA-256. Deprecated.
52+
pub static ECDSA_P384_SHA256: &dyn SignatureVerificationAlgorithm = &AwsLcRsAlgorithm {
53+
public_key_alg_id: alg_id::ECDSA_P384,
54+
signature_alg_id: alg_id::ECDSA_SHA256,
55+
verification_alg: &signature::ECDSA_P384_SHA256_ASN1,
56+
};
57+
58+
/// ECDSA signatures using the P-384 curve and SHA-384.
59+
pub static ECDSA_P384_SHA384: &dyn SignatureVerificationAlgorithm = &AwsLcRsAlgorithm {
60+
public_key_alg_id: alg_id::ECDSA_P384,
61+
signature_alg_id: alg_id::ECDSA_SHA384,
62+
verification_alg: &signature::ECDSA_P384_SHA384_ASN1,
63+
};
64+
65+
/// RSA PKCS#1 1.5 signatures using SHA-256 for keys of 2048-8192 bits.
66+
pub static RSA_PKCS1_2048_8192_SHA256: &dyn SignatureVerificationAlgorithm = &AwsLcRsAlgorithm {
67+
public_key_alg_id: alg_id::RSA_ENCRYPTION,
68+
signature_alg_id: alg_id::RSA_PKCS1_SHA256,
69+
verification_alg: &signature::RSA_PKCS1_2048_8192_SHA256,
70+
};
71+
72+
/// RSA PKCS#1 1.5 signatures using SHA-384 for keys of 2048-8192 bits.
73+
pub static RSA_PKCS1_2048_8192_SHA384: &dyn SignatureVerificationAlgorithm = &AwsLcRsAlgorithm {
74+
public_key_alg_id: alg_id::RSA_ENCRYPTION,
75+
signature_alg_id: alg_id::RSA_PKCS1_SHA384,
76+
verification_alg: &signature::RSA_PKCS1_2048_8192_SHA384,
77+
};
78+
79+
/// RSA PKCS#1 1.5 signatures using SHA-512 for keys of 2048-8192 bits.
80+
pub static RSA_PKCS1_2048_8192_SHA512: &dyn SignatureVerificationAlgorithm = &AwsLcRsAlgorithm {
81+
public_key_alg_id: alg_id::RSA_ENCRYPTION,
82+
signature_alg_id: alg_id::RSA_PKCS1_SHA512,
83+
verification_alg: &signature::RSA_PKCS1_2048_8192_SHA512,
84+
};
85+
86+
/// RSA PKCS#1 1.5 signatures using SHA-384 for keys of 3072-8192 bits.
87+
pub static RSA_PKCS1_3072_8192_SHA384: &dyn SignatureVerificationAlgorithm = &AwsLcRsAlgorithm {
88+
public_key_alg_id: alg_id::RSA_ENCRYPTION,
89+
signature_alg_id: alg_id::RSA_PKCS1_SHA384,
90+
verification_alg: &signature::RSA_PKCS1_3072_8192_SHA384,
91+
};
92+
93+
/// RSA PSS signatures using SHA-256 for keys of 2048-8192 bits and of
94+
/// type rsaEncryption; see [RFC 4055 Section 1.2].
95+
///
96+
/// [RFC 4055 Section 1.2]: https://tools.ietf.org/html/rfc4055#section-1.2
97+
pub static RSA_PSS_2048_8192_SHA256_LEGACY_KEY: &dyn SignatureVerificationAlgorithm =
98+
&AwsLcRsAlgorithm {
99+
public_key_alg_id: alg_id::RSA_ENCRYPTION,
100+
signature_alg_id: alg_id::RSA_PSS_SHA256,
101+
verification_alg: &signature::RSA_PSS_2048_8192_SHA256,
102+
};
103+
104+
/// RSA PSS signatures using SHA-384 for keys of 2048-8192 bits and of
105+
/// type rsaEncryption; see [RFC 4055 Section 1.2].
106+
///
107+
/// [RFC 4055 Section 1.2]: https://tools.ietf.org/html/rfc4055#section-1.2
108+
pub static RSA_PSS_2048_8192_SHA384_LEGACY_KEY: &dyn SignatureVerificationAlgorithm =
109+
&AwsLcRsAlgorithm {
110+
public_key_alg_id: alg_id::RSA_ENCRYPTION,
111+
signature_alg_id: alg_id::RSA_PSS_SHA384,
112+
verification_alg: &signature::RSA_PSS_2048_8192_SHA384,
113+
};
114+
115+
/// RSA PSS signatures using SHA-512 for keys of 2048-8192 bits and of
116+
/// type rsaEncryption; see [RFC 4055 Section 1.2].
117+
///
118+
/// [RFC 4055 Section 1.2]: https://tools.ietf.org/html/rfc4055#section-1.2
119+
pub static RSA_PSS_2048_8192_SHA512_LEGACY_KEY: &dyn SignatureVerificationAlgorithm =
120+
&AwsLcRsAlgorithm {
121+
public_key_alg_id: alg_id::RSA_ENCRYPTION,
122+
signature_alg_id: alg_id::RSA_PSS_SHA512,
123+
verification_alg: &signature::RSA_PSS_2048_8192_SHA512,
124+
};
125+
126+
/// ED25519 signatures according to RFC 8410
127+
pub static ED25519: &dyn SignatureVerificationAlgorithm = &AwsLcRsAlgorithm {
128+
public_key_alg_id: alg_id::ED25519,
129+
signature_alg_id: alg_id::ED25519,
130+
verification_alg: &signature::ED25519,
131+
};
132+
133+
#[cfg(test)]
134+
#[path = "."]
135+
mod tests {
136+
use crate::Error;
137+
138+
static SUPPORTED_ALGORITHMS_IN_TESTS: &[&dyn super::SignatureVerificationAlgorithm] = &[
139+
// Reasonable algorithms.
140+
super::ECDSA_P256_SHA256,
141+
super::ECDSA_P384_SHA384,
142+
super::ED25519,
143+
super::RSA_PKCS1_2048_8192_SHA256,
144+
super::RSA_PKCS1_2048_8192_SHA384,
145+
super::RSA_PKCS1_2048_8192_SHA512,
146+
super::RSA_PKCS1_3072_8192_SHA384,
147+
super::RSA_PSS_2048_8192_SHA256_LEGACY_KEY,
148+
super::RSA_PSS_2048_8192_SHA384_LEGACY_KEY,
149+
super::RSA_PSS_2048_8192_SHA512_LEGACY_KEY,
150+
// Algorithms deprecated because they are nonsensical combinations.
151+
super::ECDSA_P256_SHA384, // Truncates digest.
152+
super::ECDSA_P384_SHA256, // Digest is unnecessarily short.
153+
];
154+
155+
const UNSUPPORTED_SIGNATURE_ALGORITHM_FOR_RSA_KEY: Error =
156+
Error::UnsupportedSignatureAlgorithmForPublicKey;
157+
158+
const INVALID_SIGNATURE_FOR_RSA_KEY: Error = Error::InvalidSignatureForPublicKey;
159+
160+
const OK_IF_RSA_AVAILABLE: Result<(), Error> = Ok(());
161+
162+
#[path = "alg_tests.rs"]
163+
mod alg_tests;
164+
}

src/lib.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
//! | `alloc` | Enable features that require use of the heap. Currently all RSA signature algorithms require this feature. |
2525
//! | `std` | Enable features that require libstd. Implies `alloc`. |
2626
//! | `ring` | Enable use of the *ring* crate for cryptography. |
27+
//! | `aws_lc_rs` | Enable use of the aws-lc-rs crate for cryptography. |
2728
2829
#![cfg_attr(not(feature = "std"), no_std)]
2930
#![warn(unreachable_pub)]
@@ -46,6 +47,8 @@ extern crate alloc;
4647
#[macro_use]
4748
mod der;
4849

50+
#[cfg(feature = "aws_lc_rs")]
51+
mod aws_lc_rs_algs;
4952
mod cert;
5053
mod end_entity;
5154
mod error;
@@ -104,9 +107,20 @@ pub mod ring {
104107
};
105108
}
106109

110+
#[cfg(feature = "aws_lc_rs")]
111+
/// Signature verification algorithm implementations using the aws-lc-rs crypto library.
112+
pub mod aws_lc_rs {
113+
pub use super::aws_lc_rs_algs::{
114+
ECDSA_P256_SHA256, ECDSA_P256_SHA384, ECDSA_P384_SHA256, ECDSA_P384_SHA384, ED25519,
115+
RSA_PKCS1_2048_8192_SHA256, RSA_PKCS1_2048_8192_SHA384, RSA_PKCS1_2048_8192_SHA512,
116+
RSA_PKCS1_3072_8192_SHA384, RSA_PSS_2048_8192_SHA256_LEGACY_KEY,
117+
RSA_PSS_2048_8192_SHA384_LEGACY_KEY, RSA_PSS_2048_8192_SHA512_LEGACY_KEY,
118+
};
119+
}
120+
107121
/// An array of all the verification algorithms exported by this crate.
108122
///
109-
/// This will be empty if the crate is built without the `ring` feature.
123+
/// This will be empty if the crate is built without the `ring` and `aws_lc_rs` features.
110124
pub static ALL_VERIFICATION_ALGS: &[&dyn types::SignatureVerificationAlgorithm] = &[
111125
#[cfg(feature = "ring")]
112126
ring::ECDSA_P256_SHA256,
@@ -132,4 +146,28 @@ pub static ALL_VERIFICATION_ALGS: &[&dyn types::SignatureVerificationAlgorithm]
132146
ring::RSA_PSS_2048_8192_SHA384_LEGACY_KEY,
133147
#[cfg(all(feature = "ring", feature = "alloc"))]
134148
ring::RSA_PSS_2048_8192_SHA512_LEGACY_KEY,
149+
#[cfg(feature = "aws_lc_rs")]
150+
aws_lc_rs::ECDSA_P256_SHA256,
151+
#[cfg(feature = "aws_lc_rs")]
152+
aws_lc_rs::ECDSA_P256_SHA384,
153+
#[cfg(feature = "aws_lc_rs")]
154+
aws_lc_rs::ECDSA_P384_SHA256,
155+
#[cfg(feature = "aws_lc_rs")]
156+
aws_lc_rs::ECDSA_P384_SHA384,
157+
#[cfg(feature = "aws_lc_rs")]
158+
aws_lc_rs::ED25519,
159+
#[cfg(feature = "aws_lc_rs")]
160+
aws_lc_rs::RSA_PKCS1_2048_8192_SHA256,
161+
#[cfg(feature = "aws_lc_rs")]
162+
aws_lc_rs::RSA_PKCS1_2048_8192_SHA384,
163+
#[cfg(feature = "aws_lc_rs")]
164+
aws_lc_rs::RSA_PKCS1_2048_8192_SHA512,
165+
#[cfg(feature = "aws_lc_rs")]
166+
aws_lc_rs::RSA_PKCS1_3072_8192_SHA384,
167+
#[cfg(feature = "aws_lc_rs")]
168+
aws_lc_rs::RSA_PSS_2048_8192_SHA256_LEGACY_KEY,
169+
#[cfg(feature = "aws_lc_rs")]
170+
aws_lc_rs::RSA_PSS_2048_8192_SHA384_LEGACY_KEY,
171+
#[cfg(feature = "aws_lc_rs")]
172+
aws_lc_rs::RSA_PSS_2048_8192_SHA512_LEGACY_KEY,
135173
];

0 commit comments

Comments
 (0)