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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! 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: `xwing` needs the `suite-xwing`
/// cargo feature, `pqbox-max` the `suite-max` feature and `pqbox-paranoid` the `paranoid`
/// feature.
#[error("suite {0} is not enabled in this build (cargo feature `{feature}`)", feature = suite_feature(.0))]
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,
}
/// The cargo feature that compiles the suite named `suite`.
fn suite_feature(suite: &str) -> &'static str {
match suite {
"xwing" => "suite-xwing",
"pqbox-max" => "suite-max",
_ => "paranoid",
}
}
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",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn suite_not_enabled_names_the_feature() {
for (suite, feature) in [
("xwing", "suite-xwing"),
("pqbox-max", "suite-max"),
("pqbox-paranoid", "paranoid"),
] {
assert_eq!(
Error::SuiteNotEnabled(suite).to_string(),
format!("suite {suite} is not enabled in this build (cargo feature `{feature}`)")
);
}
}
}