ed25519_dalek/errors.rs
1// -*- mode: rust; -*-
2//
3// This file is part of ed25519-dalek.
4// Copyright (c) 2017-2019 isis lovecruft
5// See LICENSE for licensing information.
6//
7// Authors:
8// - isis agora lovecruft <isis@patternsinthevoid.net>
9
10//! Errors which may occur when parsing keys and/or signatures to or from wire formats.
11
12// rustc seems to think the typenames in match statements (e.g. in
13// Display) should be snake cased, for some reason.
14#![allow(non_snake_case)]
15
16use core::fmt;
17use core::fmt::Display;
18
19#[cfg(feature = "std")]
20use std::error::Error;
21
22/// Internal errors. Most application-level developers will likely not
23/// need to pay any attention to these.
24#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
25pub(crate) enum InternalError {
26 PointDecompression,
27 ScalarFormat,
28 /// An error in the length of bytes handed to a constructor.
29 ///
30 /// To use this, pass a string specifying the `name` of the type which is
31 /// returning the error, and the `length` in bytes which its constructor
32 /// expects.
33 BytesLength {
34 name: &'static str,
35 length: usize,
36 },
37 /// The verification equation wasn't satisfied
38 Verify,
39 /// Two arrays did not match in size, making the called signature
40 /// verification method impossible.
41 #[cfg(feature = "batch")]
42 ArrayLength {
43 name_a: &'static str,
44 length_a: usize,
45 name_b: &'static str,
46 length_b: usize,
47 name_c: &'static str,
48 length_c: usize,
49 },
50 /// An ed25519ph signature can only take up to 255 octets of context.
51 #[cfg(feature = "digest")]
52 PrehashedContextLength,
53 /// A mismatched (public, secret) key pair.
54 MismatchedKeypair,
55}
56
57impl Display for InternalError {
58 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59 match *self {
60 InternalError::PointDecompression => write!(f, "Cannot decompress Edwards point"),
61 InternalError::ScalarFormat => write!(f, "Cannot use scalar with high-bit set"),
62 InternalError::BytesLength { name: n, length: l } => {
63 write!(f, "{} must be {} bytes in length", n, l)
64 }
65 InternalError::Verify => write!(f, "Verification equation was not satisfied"),
66 #[cfg(feature = "batch")]
67 InternalError::ArrayLength {
68 name_a: na,
69 length_a: la,
70 name_b: nb,
71 length_b: lb,
72 name_c: nc,
73 length_c: lc,
74 } => write!(
75 f,
76 "Arrays must be the same length: {} has length {},
77 {} has length {}, {} has length {}.",
78 na, la, nb, lb, nc, lc
79 ),
80 #[cfg(feature = "digest")]
81 InternalError::PrehashedContextLength => write!(
82 f,
83 "An ed25519ph signature can only take up to 255 octets of context"
84 ),
85 InternalError::MismatchedKeypair => write!(f, "Mismatched Keypair detected"),
86 }
87 }
88}
89
90#[cfg(feature = "std")]
91impl Error for InternalError {}
92
93/// Errors which may occur while processing signatures and keypairs.
94///
95/// This error may arise due to:
96///
97/// * Being given bytes with a length different to what was expected.
98///
99/// * A problem decompressing `r`, a curve point, in the `Signature`, or the
100/// curve point for a `PublicKey`.
101///
102/// * A problem with the format of `s`, a scalar, in the `Signature`. This
103/// is only raised if the high-bit of the scalar was set. (Scalars must
104/// only be constructed from 255-bit integers.)
105///
106/// * Failure of a signature to satisfy the verification equation.
107pub type SignatureError = ed25519::signature::Error;
108
109impl From<InternalError> for SignatureError {
110 #[cfg(not(feature = "std"))]
111 fn from(_err: InternalError) -> SignatureError {
112 SignatureError::new()
113 }
114
115 #[cfg(feature = "std")]
116 fn from(err: InternalError) -> SignatureError {
117 SignatureError::from_source(err)
118 }
119}