defuse_webauthn/
p256.rs

1use defuse_crypto::VerifiableCurve;
2pub use defuse_crypto::{P256CompressedPublicKey, P256Signature};
3
4use crate::Algorithm;
5
6/// [COSE ES256 (-7) algorithm](https://www.iana.org/assignments/cose/cose.xhtml#algorithms):
7/// P256 (a.k.a secp256r1) over SHA-256
8#[derive(Debug, Clone)]
9pub struct P256;
10
11#[cfg(any(feature = "sha2", feature = "near-contract"))]
12impl Algorithm for P256 {
13    type PublicKey = P256CompressedPublicKey;
14    type Signature = P256Signature;
15
16    #[inline]
17    fn verify(msg: &[u8], public_key: &Self::PublicKey, signature: &Self::Signature) -> bool {
18        use defuse_digest::{Digest, Sha256};
19        // Use host impl of SHA-256 here to reduce gas consumption
20        let prehashed = Sha256::digest(msg).into();
21
22        defuse_crypto::P256::verify(&signature.0, &prehashed, &public_key.0).is_some()
23    }
24}