parity_crypto/
error.rs

1// Copyright 2020 Parity Technologies
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use std::{error::Error as StdError, fmt, result};
10
11#[derive(Debug)]
12pub enum Error {
13	Scrypt(ScryptError),
14	Symm(SymmError),
15}
16
17#[derive(Debug)]
18pub enum ScryptError {
19	// log(N) < r / 16
20	InvalidN,
21	// p <= (2^31-1 * 32)/(128 * r)
22	InvalidP,
23	ScryptParam(scrypt::errors::InvalidParams),
24	ScryptLength(scrypt::errors::InvalidOutputLen),
25}
26
27#[derive(Debug)]
28pub struct SymmError(PrivSymmErr);
29
30#[derive(Debug)]
31enum PrivSymmErr {
32	BlockMode(block_modes::BlockModeError),
33	KeyStream(aes_ctr::cipher::stream::LoopError),
34	InvalidKeyLength(block_modes::InvalidKeyIvLength),
35}
36
37impl StdError for Error {
38	fn source(&self) -> Option<&(dyn StdError + 'static)> {
39		match self {
40			Error::Scrypt(scrypt_err) => Some(scrypt_err),
41			Error::Symm(symm_err) => Some(symm_err),
42		}
43	}
44}
45
46impl StdError for ScryptError {
47	fn source(&self) -> Option<&(dyn StdError + 'static)> {
48		match self {
49			ScryptError::ScryptParam(err) => Some(err),
50			ScryptError::ScryptLength(err) => Some(err),
51			_ => None,
52		}
53	}
54}
55
56impl StdError for SymmError {
57	fn source(&self) -> Option<&(dyn StdError + 'static)> {
58		match &self.0 {
59			PrivSymmErr::BlockMode(err) => Some(err),
60			PrivSymmErr::InvalidKeyLength(err) => Some(err),
61			_ => None,
62		}
63	}
64}
65
66impl fmt::Display for Error {
67	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> result::Result<(), fmt::Error> {
68		match self {
69			Error::Scrypt(err) => write!(f, "scrypt error: {}", err),
70			Error::Symm(err) => write!(f, "symm error: {}", err),
71		}
72	}
73}
74
75impl fmt::Display for ScryptError {
76	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> result::Result<(), fmt::Error> {
77		match self {
78			ScryptError::InvalidN => write!(f, "invalid n argument"),
79			ScryptError::InvalidP => write!(f, "invalid p argument"),
80			ScryptError::ScryptParam(err) => write!(f, "invalid params: {}", err),
81			ScryptError::ScryptLength(err) => write!(f, "invalid output length: {}", err),
82		}
83	}
84}
85
86impl fmt::Display for SymmError {
87	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> result::Result<(), fmt::Error> {
88		match self {
89			SymmError(PrivSymmErr::BlockMode(err)) => write!(f, "block cipher error: {}", err),
90			SymmError(PrivSymmErr::KeyStream(err)) => write!(f, "ctr key stream ended: {}", err),
91			SymmError(PrivSymmErr::InvalidKeyLength(err)) => write!(f, "block cipher key length: {}", err),
92		}
93	}
94}
95
96impl Into<std::io::Error> for Error {
97	fn into(self) -> std::io::Error {
98		std::io::Error::new(std::io::ErrorKind::Other, format!("Crypto error: {}", self))
99	}
100}
101
102impl From<block_modes::BlockModeError> for SymmError {
103	fn from(e: block_modes::BlockModeError) -> SymmError {
104		SymmError(PrivSymmErr::BlockMode(e))
105	}
106}
107
108impl From<block_modes::InvalidKeyIvLength> for SymmError {
109	fn from(e: block_modes::InvalidKeyIvLength) -> SymmError {
110		SymmError(PrivSymmErr::InvalidKeyLength(e))
111	}
112}
113
114impl From<aes_ctr::cipher::stream::LoopError> for SymmError {
115	fn from(e: aes_ctr::cipher::stream::LoopError) -> SymmError {
116		SymmError(PrivSymmErr::KeyStream(e))
117	}
118}
119
120impl From<scrypt::errors::InvalidParams> for ScryptError {
121	fn from(e: scrypt::errors::InvalidParams) -> ScryptError {
122		ScryptError::ScryptParam(e)
123	}
124}
125
126impl From<scrypt::errors::InvalidOutputLen> for ScryptError {
127	fn from(e: scrypt::errors::InvalidOutputLen) -> ScryptError {
128		ScryptError::ScryptLength(e)
129	}
130}
131
132impl From<ScryptError> for Error {
133	fn from(e: ScryptError) -> Error {
134		Error::Scrypt(e)
135	}
136}
137
138impl From<SymmError> for Error {
139	fn from(e: SymmError) -> Error {
140		Error::Symm(e)
141	}
142}