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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//! Possible errors in the library.

use std;
use std::error::Error as _;
use std::fmt;
use std::io::Error;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::JsValue;

use block_modes::{BlockModeError, InvalidKeyIvLength};
use hmac::crypto_mac::InvalidKeyLength;
use hmac::crypto_mac::MacError;
use rand;

/// The enum containing the various error types.
#[derive(Debug)]
pub enum DevoCryptoError {
    /// The provided data has an invalid length. Error code: -1
    InvalidLength,
    /// The key length is invalid. Error code: -2
    InvalidKeyLength,
    /// The length of the FFI output buffer is invalid. Error code: -3
    InvalidOutputLength,
    /// The signature of the data blob does not match 0x0d0c. Error code: -11
    InvalidSignature,
    /// The MAC is invalid. Error code: -12
    InvalidMac,
    /// The operation cannot be done with this type. Error code: -13
    InvalidDataType,
    /// The data type is unknown. Error code: -21
    UnknownType,
    /// The data subtype is unknown. Error code: -22
    UnknownSubtype,
    /// The data type version is unknown. Error code: -23
    UnknownVersion,
    /// A null pointer has been passed to the FFI interface. Error code: -31
    NullPointer,
    /// A cryptographic error occurred. Error code: -32
    CryptoError,
    /// An error with the Random Number Generator occurred. Error code: -33
    RandomError,
    /// A generic IO error has occurred. Error code: -34
    IoError(Error),
}

impl DevoCryptoError {
    /// Returns the error code associated with the error.
    /// This is useful for passing the exception type across a language boundary.
    pub fn error_code(&self) -> i64 {
        match *self {
            DevoCryptoError::InvalidLength => -1,
            DevoCryptoError::InvalidKeyLength => -2,
            DevoCryptoError::InvalidOutputLength => -3,
            DevoCryptoError::InvalidSignature => -11,
            DevoCryptoError::InvalidMac => -12,
            DevoCryptoError::InvalidDataType => -13,
            DevoCryptoError::UnknownType => -21,
            DevoCryptoError::UnknownSubtype => -22,
            DevoCryptoError::UnknownVersion => -23,
            DevoCryptoError::NullPointer => -31,
            DevoCryptoError::CryptoError => -32,
            DevoCryptoError::RandomError => -33,
            DevoCryptoError::IoError(_) => -34,
        }
    }
}

#[cfg(target_arch = "wasm32")]
impl DevoCryptoError {
    // Returns the error name for JavaScript error handling
    pub fn name(&self) -> &str {
        match *self {
            DevoCryptoError::InvalidLength => "InvalidLength",
            DevoCryptoError::InvalidKeyLength => "InvalidKeyLength",
            DevoCryptoError::InvalidOutputLength => "InvalidOutputLength",
            DevoCryptoError::InvalidSignature => "InvalidSignature",
            DevoCryptoError::InvalidMac => "InvalidMac",
            DevoCryptoError::InvalidDataType => "InvalidDataType",
            DevoCryptoError::UnknownType => "UnknownType",
            DevoCryptoError::UnknownSubtype => "UnknownSubtype",
            DevoCryptoError::UnknownVersion => "UnknownVersion",
            DevoCryptoError::NullPointer => "NullPointer",
            DevoCryptoError::CryptoError => "CryptoError",
            DevoCryptoError::RandomError => "RandomError",
            DevoCryptoError::IoError(_) => "IoError",
        }
    }
}

impl fmt::Display for DevoCryptoError {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match *self {
            DevoCryptoError::IoError(ref error) => error.fmt(f),
            _ => write!(f, "Error {}: {}", self.error_code(), self.description()),
        }
    }
}

impl std::error::Error for DevoCryptoError {
    fn description(&self) -> &str {
        match *self {
            DevoCryptoError::InvalidLength => "The provided data has an invalid length.",
            DevoCryptoError::InvalidKeyLength => "The key length is invalid.",
            DevoCryptoError::InvalidOutputLength => {
                "The length of the FFI output buffer is invalid."
            }
            DevoCryptoError::InvalidSignature => {
                "The signature of the data blob does not match 0x0d0c."
            }
            DevoCryptoError::InvalidMac => "The MAC is invalid.",
            DevoCryptoError::InvalidDataType => "The operation cannot be done with this type.",
            DevoCryptoError::UnknownType => "The data type is unknown.",
            DevoCryptoError::UnknownSubtype => "The data subtype is unknown.",
            DevoCryptoError::UnknownVersion => "The data type version is unknown.",
            DevoCryptoError::NullPointer => "A null pointer has been passed to the FFI interface.",
            DevoCryptoError::CryptoError => "A cryptographic error occurred.",
            DevoCryptoError::RandomError => "An error with the Random Number Generator occurred.",
            DevoCryptoError::IoError(ref error) => error.description(),
        }
    }
}

impl From<InvalidKeyLength> for DevoCryptoError {
    fn from(_error: InvalidKeyLength) -> DevoCryptoError {
        DevoCryptoError::InvalidKeyLength
    }
}

impl From<MacError> for DevoCryptoError {
    fn from(_error: MacError) -> DevoCryptoError {
        DevoCryptoError::InvalidMac
    }
}

impl From<InvalidKeyIvLength> for DevoCryptoError {
    fn from(_error: InvalidKeyIvLength) -> DevoCryptoError {
        DevoCryptoError::InvalidKeyLength
    }
}

impl From<BlockModeError> for DevoCryptoError {
    fn from(_error: BlockModeError) -> DevoCryptoError {
        DevoCryptoError::CryptoError
    }
}

impl From<aead::Error> for DevoCryptoError {
    fn from(_error: aead::Error) -> DevoCryptoError {
        DevoCryptoError::CryptoError
    }
}

impl From<rand::Error> for DevoCryptoError {
    fn from(_error: rand::Error) -> DevoCryptoError {
        DevoCryptoError::RandomError
    }
}

impl From<std::io::Error> for DevoCryptoError {
    fn from(_error: std::io::Error) -> DevoCryptoError {
        DevoCryptoError::RandomError
    }
}

#[cfg(target_arch = "wasm32")]
impl From<DevoCryptoError> for JsValue {
    fn from(error: DevoCryptoError) -> JsValue {
        let js_error = js_sys::Error::new(error.description());

        js_error.set_name(error.name());
        js_error.into()
    }
}