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
// Copyright 2020 Parity Technologies
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::{error::Error as StdError, fmt, result};

#[derive(Debug)]
pub enum Error {
	Scrypt(ScryptError),
	Symm(SymmError),
}

#[derive(Debug)]
pub enum ScryptError {
	// log(N) < r / 16
	InvalidN,
	// p <= (2^31-1 * 32)/(128 * r)
	InvalidP,
	ScryptParam(scrypt::errors::InvalidParams),
	ScryptLength(scrypt::errors::InvalidOutputLen),
}

#[derive(Debug)]
pub struct SymmError(PrivSymmErr);

#[derive(Debug)]
enum PrivSymmErr {
	BlockMode(block_modes::BlockModeError),
	KeyStream(aes_ctr::cipher::stream::LoopError),
	InvalidKeyLength(block_modes::InvalidKeyIvLength),
}

impl StdError for Error {
	fn source(&self) -> Option<&(dyn StdError + 'static)> {
		match self {
			Error::Scrypt(scrypt_err) => Some(scrypt_err),
			Error::Symm(symm_err) => Some(symm_err),
		}
	}
}

impl StdError for ScryptError {
	fn source(&self) -> Option<&(dyn StdError + 'static)> {
		match self {
			ScryptError::ScryptParam(err) => Some(err),
			ScryptError::ScryptLength(err) => Some(err),
			_ => None,
		}
	}
}

impl StdError for SymmError {
	fn source(&self) -> Option<&(dyn StdError + 'static)> {
		match &self.0 {
			PrivSymmErr::BlockMode(err) => Some(err),
			PrivSymmErr::InvalidKeyLength(err) => Some(err),
			_ => None,
		}
	}
}

impl fmt::Display for Error {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> result::Result<(), fmt::Error> {
		match self {
			Error::Scrypt(err) => write!(f, "scrypt error: {}", err),
			Error::Symm(err) => write!(f, "symm error: {}", err),
		}
	}
}

impl fmt::Display for ScryptError {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> result::Result<(), fmt::Error> {
		match self {
			ScryptError::InvalidN => write!(f, "invalid n argument"),
			ScryptError::InvalidP => write!(f, "invalid p argument"),
			ScryptError::ScryptParam(err) => write!(f, "invalid params: {}", err),
			ScryptError::ScryptLength(err) => write!(f, "invalid output length: {}", err),
		}
	}
}

impl fmt::Display for SymmError {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> result::Result<(), fmt::Error> {
		match self {
			SymmError(PrivSymmErr::BlockMode(err)) => write!(f, "block cipher error: {}", err),
			SymmError(PrivSymmErr::KeyStream(err)) => write!(f, "ctr key stream ended: {}", err),
			SymmError(PrivSymmErr::InvalidKeyLength(err)) => write!(f, "block cipher key length: {}", err),
		}
	}
}

impl Into<std::io::Error> for Error {
	fn into(self) -> std::io::Error {
		std::io::Error::new(std::io::ErrorKind::Other, format!("Crypto error: {}", self))
	}
}

impl From<block_modes::BlockModeError> for SymmError {
	fn from(e: block_modes::BlockModeError) -> SymmError {
		SymmError(PrivSymmErr::BlockMode(e))
	}
}

impl From<block_modes::InvalidKeyIvLength> for SymmError {
	fn from(e: block_modes::InvalidKeyIvLength) -> SymmError {
		SymmError(PrivSymmErr::InvalidKeyLength(e))
	}
}

impl From<aes_ctr::cipher::stream::LoopError> for SymmError {
	fn from(e: aes_ctr::cipher::stream::LoopError) -> SymmError {
		SymmError(PrivSymmErr::KeyStream(e))
	}
}

impl From<scrypt::errors::InvalidParams> for ScryptError {
	fn from(e: scrypt::errors::InvalidParams) -> ScryptError {
		ScryptError::ScryptParam(e)
	}
}

impl From<scrypt::errors::InvalidOutputLen> for ScryptError {
	fn from(e: scrypt::errors::InvalidOutputLen) -> ScryptError {
		ScryptError::ScryptLength(e)
	}
}

impl From<ScryptError> for Error {
	fn from(e: ScryptError) -> Error {
		Error::Scrypt(e)
	}
}

impl From<SymmError> for Error {
	fn from(e: SymmError) -> Error {
		Error::Symm(e)
	}
}