2017-12-23 22:33:59 +00:00
|
|
|
// -*- mode: rust; -*-
|
|
|
|
|
//
|
|
|
|
|
// This file is part of ed25519-dalek.
|
2019-01-17 23:28:13 +00:00
|
|
|
// Copyright (c) 2017-2019 isis lovecruft
|
2017-12-23 22:33:59 +00:00
|
|
|
// See LICENSE for licensing information.
|
|
|
|
|
//
|
|
|
|
|
// Authors:
|
2019-01-17 23:28:13 +00:00
|
|
|
// - isis agora lovecruft <isis@patternsinthevoid.net>
|
2017-12-23 22:33:59 +00:00
|
|
|
|
|
|
|
|
//! Errors which may occur when parsing keys and/or signatures to or from wire formats.
|
|
|
|
|
|
|
|
|
|
// rustc seems to think the typenames in match statements (e.g. in
|
|
|
|
|
// Display) should be snake cased, for some reason.
|
|
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
|
|
|
|
|
use core::fmt;
|
|
|
|
|
use core::fmt::Display;
|
|
|
|
|
|
2019-10-26 04:16:33 +00:00
|
|
|
#[cfg(feature = "std")]
|
|
|
|
|
use std::error::Error;
|
|
|
|
|
|
2018-03-26 02:13:39 +00:00
|
|
|
/// Internal errors. Most application-level developers will likely not
|
2017-12-23 22:33:59 +00:00
|
|
|
/// need to pay any attention to these.
|
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
|
2018-12-30 04:10:59 +00:00
|
|
|
pub(crate) enum InternalError {
|
2022-12-18 20:56:41 +00:00
|
|
|
PointDecompression,
|
|
|
|
|
ScalarFormat,
|
2017-12-23 22:33:59 +00:00
|
|
|
/// An error in the length of bytes handed to a constructor.
|
|
|
|
|
///
|
|
|
|
|
/// To use this, pass a string specifying the `name` of the type which is
|
|
|
|
|
/// returning the error, and the `length` in bytes which its constructor
|
|
|
|
|
/// expects.
|
2022-12-18 20:56:41 +00:00
|
|
|
BytesLength {
|
2018-12-30 04:10:59 +00:00
|
|
|
name: &'static str,
|
|
|
|
|
length: usize,
|
|
|
|
|
},
|
2018-07-13 19:39:45 +00:00
|
|
|
/// The verification equation wasn't satisfied
|
2022-12-18 20:56:41 +00:00
|
|
|
Verify,
|
2019-10-16 20:57:31 +00:00
|
|
|
/// Two arrays did not match in size, making the called signature
|
|
|
|
|
/// verification method impossible.
|
2023-01-15 02:26:39 +00:00
|
|
|
#[cfg(feature = "batch")]
|
2022-12-18 20:56:41 +00:00
|
|
|
ArrayLength {
|
2022-12-08 07:39:48 +00:00
|
|
|
name_a: &'static str,
|
|
|
|
|
length_a: usize,
|
|
|
|
|
name_b: &'static str,
|
|
|
|
|
length_b: usize,
|
|
|
|
|
name_c: &'static str,
|
|
|
|
|
length_c: usize,
|
|
|
|
|
},
|
2020-07-14 00:25:40 +00:00
|
|
|
/// An ed25519ph signature can only take up to 255 octets of context.
|
2022-12-18 20:56:41 +00:00
|
|
|
PrehashedContextLength,
|
2022-10-15 19:04:03 +00:00
|
|
|
/// A mismatched (public, secret) key pair.
|
2022-12-18 20:56:41 +00:00
|
|
|
MismatchedKeypair,
|
2017-12-23 22:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Display for InternalError {
|
2018-12-30 03:13:13 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2017-12-23 22:33:59 +00:00
|
|
|
match *self {
|
2022-12-18 20:56:41 +00:00
|
|
|
InternalError::PointDecompression => write!(f, "Cannot decompress Edwards point"),
|
|
|
|
|
InternalError::ScalarFormat => write!(f, "Cannot use scalar with high-bit set"),
|
|
|
|
|
InternalError::BytesLength { name: n, length: l } => {
|
2022-12-08 07:39:48 +00:00
|
|
|
write!(f, "{} must be {} bytes in length", n, l)
|
|
|
|
|
}
|
2022-12-18 20:56:41 +00:00
|
|
|
InternalError::Verify => write!(f, "Verification equation was not satisfied"),
|
2023-01-15 02:26:39 +00:00
|
|
|
#[cfg(feature = "batch")]
|
2022-12-18 20:56:41 +00:00
|
|
|
InternalError::ArrayLength {
|
2022-12-08 07:39:48 +00:00
|
|
|
name_a: na,
|
|
|
|
|
length_a: la,
|
|
|
|
|
name_b: nb,
|
|
|
|
|
length_b: lb,
|
|
|
|
|
name_c: nc,
|
|
|
|
|
length_c: lc,
|
|
|
|
|
} => write!(
|
|
|
|
|
f,
|
|
|
|
|
"Arrays must be the same length: {} has length {},
|
|
|
|
|
{} has length {}, {} has length {}.",
|
|
|
|
|
na, la, nb, lb, nc, lc
|
|
|
|
|
),
|
2022-12-18 20:56:41 +00:00
|
|
|
InternalError::PrehashedContextLength => write!(
|
2022-12-08 07:39:48 +00:00
|
|
|
f,
|
|
|
|
|
"An ed25519ph signature can only take up to 255 octets of context"
|
|
|
|
|
),
|
2022-12-18 20:56:41 +00:00
|
|
|
InternalError::MismatchedKeypair => write!(f, "Mismatched Keypair detected"),
|
2017-12-23 22:33:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-26 04:16:33 +00:00
|
|
|
#[cfg(feature = "std")]
|
2022-12-08 07:39:48 +00:00
|
|
|
impl Error for InternalError {}
|
2017-12-23 22:33:59 +00:00
|
|
|
|
2018-07-13 19:39:45 +00:00
|
|
|
/// Errors which may occur while processing signatures and keypairs.
|
2017-12-23 22:33:59 +00:00
|
|
|
///
|
|
|
|
|
/// This error may arise due to:
|
|
|
|
|
///
|
2018-07-13 19:39:45 +00:00
|
|
|
/// * Being given bytes with a length different to what was expected.
|
|
|
|
|
///
|
2017-12-23 22:33:59 +00:00
|
|
|
/// * A problem decompressing `r`, a curve point, in the `Signature`, or the
|
|
|
|
|
/// curve point for a `PublicKey`.
|
2018-07-13 19:39:45 +00:00
|
|
|
///
|
2017-12-23 22:33:59 +00:00
|
|
|
/// * A problem with the format of `s`, a scalar, in the `Signature`. This
|
|
|
|
|
/// is only raised if the high-bit of the scalar was set. (Scalars must
|
|
|
|
|
/// only be constructed from 255-bit integers.)
|
2018-07-13 19:39:45 +00:00
|
|
|
///
|
|
|
|
|
/// * Failure of a signature to satisfy the verification equation.
|
2020-03-17 17:25:33 +00:00
|
|
|
pub type SignatureError = ed25519::signature::Error;
|
2017-12-23 22:33:59 +00:00
|
|
|
|
2020-03-17 17:25:33 +00:00
|
|
|
impl From<InternalError> for SignatureError {
|
|
|
|
|
#[cfg(not(feature = "std"))]
|
|
|
|
|
fn from(_err: InternalError) -> SignatureError {
|
|
|
|
|
SignatureError::new()
|
2017-12-23 22:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-17 17:25:33 +00:00
|
|
|
#[cfg(feature = "std")]
|
|
|
|
|
fn from(err: InternalError) -> SignatureError {
|
|
|
|
|
SignatureError::from_source(err)
|
2017-12-23 22:33:59 +00:00
|
|
|
}
|
|
|
|
|
}
|