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