|
| 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 | +} |
0 commit comments