1use std::array::TryFromSliceError;
2
3use pqcrypto_falcon::ffi::PQCLEAN_FALCON1024_AVX2_CRYPTO_BYTES;
4
5pub type Result<T> = std::result::Result<T, CryptoError>;
6
7#[derive(Debug)]
8pub enum CryptoError {
9 InvalidSignature,
10 UnknownVerificationError,
11 PQCryptoError(pqcrypto_traits::Error),
12 InvalidKeyLength(crypto_common::InvalidLength),
13 ChaCha20Poly1305EncryptionError(chacha20poly1305::Error),
14 IncongruentLength(usize, usize),
15 FalconSignatureTooShort(usize),
16 SignatureVerificationFailed,
17 TryFromSliceError(TryFromSliceError),
18}
19
20impl std::fmt::Display for CryptoError {
21 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22 match self {
23 CryptoError::PQCryptoError(e) => e.fmt(f),
24 CryptoError::UnknownVerificationError => write!(f, "Unknown verification error"),
25 CryptoError::InvalidSignature => write!(f, "Invalid signature"),
26 CryptoError::InvalidKeyLength(e) => e.fmt(f),
27 CryptoError::ChaCha20Poly1305EncryptionError(e) => e.fmt(f),
28 CryptoError::IncongruentLength(expected, actual) => {
29 write!(
30 f,
31 "Incongruent length: expected {}, got {}",
32 expected, actual
33 )
34 }
35 CryptoError::SignatureVerificationFailed => {
36 write!(f, "Signature verification failed")
37 }
38 CryptoError::FalconSignatureTooShort(len) => {
39 write!(
40 f,
41 "Signature too short: expected at least {} bytes, got {}",
42 PQCLEAN_FALCON1024_AVX2_CRYPTO_BYTES, len
43 )
44 }
45 CryptoError::TryFromSliceError(e) => e.fmt(f),
46 }
47 }
48}
49
50impl std::error::Error for CryptoError {}
51
52impl From<pqcrypto_traits::Error> for CryptoError {
53 fn from(e: pqcrypto_traits::Error) -> Self {
54 CryptoError::PQCryptoError(e)
55 }
56}
57
58impl From<crypto_common::InvalidLength> for CryptoError {
59 fn from(e: crypto_common::InvalidLength) -> Self {
60 CryptoError::InvalidKeyLength(e)
61 }
62}
63
64impl From<chacha20poly1305::Error> for CryptoError {
65 fn from(e: chacha20poly1305::Error) -> Self {
66 CryptoError::ChaCha20Poly1305EncryptionError(e)
67 }
68}
69
70impl From<TryFromSliceError> for CryptoError {
71 fn from(e: TryFromSliceError) -> Self {
72 CryptoError::TryFromSliceError(e)
73 }
74}