use aws_lc_rs::signature::ECDSA_P256_SHA256_ASN1;
use crate::{Signature, keys::KeyId};
#[derive(Debug, thiserror::Error)]
#[error("signature verification failed")]
pub struct SignatureVerifyErr;
#[derive(Debug)]
pub struct PublicKey<'a>(&'a [u8]);
impl<'a> PublicKey<'a> {
pub(crate) fn new(v: &'a [u8]) -> Self {
Self(v)
}
pub fn verify(&self, data: &[u8], sig: &Signature) -> Result<(), SignatureVerifyErr> {
aws_lc_rs::signature::UnparsedPublicKey::new(&ECDSA_P256_SHA256_ASN1, self.0)
.verify(data, sig.as_ref())
.map_err(|_| SignatureVerifyErr)
}
pub fn key_id(&self) -> KeyId {
KeyId::from(self)
}
}
impl<'a> rcgen::PublicKeyData for PublicKey<'a> {
fn der_bytes(&self) -> &[u8] {
self.0
}
fn algorithm(&self) -> &'static rcgen::SignatureAlgorithm {
&rcgen::PKCS_ECDSA_P256_SHA256
}
}
#[cfg(test)]
mod tests {
use crate::{
Signature, Signer,
keys::{PrivateKey, tests::fixture_key},
};
#[test]
fn test_verify_fixture() {
const SIG: &[u8] = &[
48, 70, 2, 33, 0, 159, 76, 25, 247, 14, 167, 0, 24, 61, 234, 149, 155, 10, 245, 27,
172, 116, 5, 107, 196, 201, 234, 169, 89, 6, 10, 214, 0, 134, 101, 141, 210, 2, 33, 0,
208, 252, 87, 7, 41, 104, 204, 68, 230, 200, 114, 145, 230, 146, 74, 188, 121, 72, 16,
186, 227, 169, 81, 231, 126, 133, 63, 65, 174, 55, 181, 207,
];
let key = fixture_key();
let sig = Signature::try_from(SIG).expect("valid signature");
assert!(key.public_key().verify("bananas".as_bytes(), &sig).is_ok());
}
#[test]
fn test_non_deterministic_signatures() {
const DATA: [u8; 4] = [0x00, 0xCA, 0xFE, 0x42];
let key = PrivateKey::new();
let a = key.sign(&DATA);
let b = key.sign(&DATA);
assert_ne!(a, b);
}
}