ecqv 0.1.0

SEC 4 ECQV implicit certificates on P-256: reconstruct a public key from a certificate and the issuer's key, with no signature to carry
Documentation
//! Errors, phrased so that a caller can fix the problem from the message
//! alone.

use core::fmt;

use crate::encoding;

/// Errors produced by the ECQV operations.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
    /// The request public key R_U is not a valid P-256 point.
    ///
    /// The request did not come from a P-256 key pair, or was corrupted in
    /// transit. Check that the requester used the same curve.
    MalformedRequest,

    /// The reconstruction value P_U is not on the P-256 curve.
    ///
    /// The certificate is corrupt, or was issued for a different curve.
    InvalidReconstructionPoint,

    /// The derived private key does not match the reconstructed public key.
    ///
    /// The certificate was not issued by the CA public key you supplied, or
    /// the certificate or reconstruction value was altered.
    SelfCheckFailed,

    /// A signature did not verify with the reconstructed key.
    ///
    /// Either the certificate was forged (reconstruction succeeded but the
    /// key is wrong), or the message or signature was altered.
    SignatureVerification,

    /// A scalar value was outside the valid range.
    ///
    /// A secret or reconstruction value was zero or >= the group order.
    InvalidScalar,

    /// The certificate bytes could not be parsed.
    ///
    /// The certificate is truncated, or the field lengths are inconsistent.
    MalformedCertificate,

    /// The certificate's reconstruction point is not a valid P-256 point.
    ///
    /// The certificate is corrupt, or was issued for a different curve.
    InvalidCertificatePoint,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::MalformedRequest => f.write_str(
                "the request public key R_U is not a valid P-256 point; check that the requester used the same curve",
            ),
            Error::InvalidReconstructionPoint => f.write_str(
                "the reconstruction value P_U is not on the P-256 curve; the certificate is corrupt or was issued for a different curve",
            ),
            Error::SelfCheckFailed => f.write_str(
                "the derived private key does not match the reconstructed public key; the certificate was not issued by this CA or was altered",
            ),
            Error::SignatureVerification => f.write_str(
                "the signature did not verify with the reconstructed key; the certificate is forged or the message was altered",
            ),
            Error::InvalidScalar => f.write_str(
                "a secret or reconstruction value was zero or >= the group order",
            ),
            Error::MalformedCertificate => f.write_str(
                "the certificate bytes could not be parsed; it is truncated or the field lengths are inconsistent",
            ),
            Error::InvalidCertificatePoint => f.write_str(
                "the certificate's reconstruction point is not a valid P-256 point; the certificate is corrupt or was issued for a different curve",
            ),
        }
    }
}

impl core::error::Error for Error {}

impl From<encoding::DecodeError> for Error {
    fn from(e: encoding::DecodeError) -> Self {
        match e {
            encoding::DecodeError::Truncated => Error::MalformedCertificate,
            encoding::DecodeError::InvalidPoint => Error::InvalidCertificatePoint,
        }
    }
}