1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//! Errors returned by every fallible operation.
/// A `Result` with this crate's [`Error`].
pub type Result<T> = core::result::Result<T, Error>;
/// Everything that can go wrong. Messages never include secret material.
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
/// The input does not start with the `PQB` magic.
#[error("not a pqbox object: missing \"PQB\" magic bytes")]
BadMagic,
/// The input uses a format version this build cannot read.
#[error("unsupported pqbox format version {0} (this build reads version 2)")]
UnsupportedVersion(u8),
/// The object type byte is unknown.
#[error("unknown pqbox object type 0x{0:02x}")]
UnknownKind(u8),
/// The suite id is unknown.
#[error("unknown pqbox suite 0x{0:02x}")]
UnknownSuite(u8),
/// The suite exists but is not compiled into this build (`pqbox-paranoid` needs the
/// `paranoid` cargo feature).
#[error("suite {0} is not enabled in this build (cargo feature `paranoid`)")]
SuiteNotEnabled(&'static str),
/// The suite is valid but the [`crate::SuitePolicy`] in effect does not allow it. By default
/// only `pqbox-max` is accepted; `xwing` and `pqbox-paranoid` must be allowed explicitly.
#[error("suite {0} is not allowed here (allow it explicitly)")]
SuiteNotAllowed(&'static str),
/// Two objects of different suites were used together (for example, a `pqbox-max` ciphertext
/// with an `xwing` key).
#[error("suite mismatch: expected {expected}, found {found}")]
SuiteMismatch {
/// The suite of the key.
expected: &'static str,
/// The suite of the other object.
found: &'static str,
},
/// The object is valid but of another type than expected.
#[error("wrong pqbox object type: expected {expected}, found {found}")]
WrongKind {
/// The type the caller asked for.
expected: &'static str,
/// The type found in the header.
found: &'static str,
},
/// The input has the wrong length for its type.
#[error("invalid length for {0}")]
InvalidLength(&'static str),
/// A key is malformed (for example, an Ed25519 public key that is not a curve point, or a
/// low-order X25519 public key).
#[error("invalid key encoding")]
InvalidKey,
/// Authenticated decryption failed: the data was tampered with, or the key is wrong.
#[error("decryption failed: the data was tampered with or the key is wrong")]
Decrypt,
/// A signature did not verify (either half of a hybrid signature failing is enough).
#[error("signature verification failed")]
BadSignature,
/// A parameter is out of range.
#[error("invalid parameter: {0}")]
InvalidParams(&'static str),
/// A password hash string could not be parsed, or uses unsupported parameters.
#[error("invalid password hash string")]
InvalidPasswordHash,
/// The password does not match the hash.
#[error("password does not match")]
PasswordMismatch,
}
impl Error {
/// A stable, machine-readable name for the error (used by the language bindings).
#[must_use]
pub fn code(&self) -> &'static str {
match self {
Self::BadMagic => "BadMagic",
Self::UnsupportedVersion(_) => "UnsupportedVersion",
Self::UnknownKind(_) => "UnknownKind",
Self::UnknownSuite(_) => "UnknownSuite",
Self::SuiteNotEnabled(_) => "SuiteNotEnabled",
Self::SuiteNotAllowed(_) => "SuiteNotAllowed",
Self::SuiteMismatch { .. } => "SuiteMismatch",
Self::WrongKind { .. } => "WrongKind",
Self::InvalidLength(_) => "InvalidLength",
Self::InvalidKey => "InvalidKey",
Self::Decrypt => "Decrypt",
Self::BadSignature => "BadSignature",
Self::InvalidParams(_) => "InvalidParams",
Self::InvalidPasswordHash => "InvalidPasswordHash",
Self::PasswordMismatch => "PasswordMismatch",
}
}
}