2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2016-06-20 10:06:49 +02:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2016-06-20 10:06:49 +02:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2016-06-20 10:06:49 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-06-20 10:06:49 +02:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
use super::{Bytes, Error};
|
|
|
|
use serde::{
|
|
|
|
de::{Error as SerdeError, Visitor},
|
|
|
|
Deserialize, Deserializer, Serialize, Serializer,
|
|
|
|
};
|
|
|
|
use std::{fmt, num::NonZeroU32};
|
2016-06-20 00:10:34 +02:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum KdfSer {
|
2020-08-05 06:08:03 +02:00
|
|
|
Pbkdf2,
|
|
|
|
Scrypt,
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for KdfSer {
|
2020-08-05 06:08:03 +02:00
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
match *self {
|
|
|
|
KdfSer::Pbkdf2 => serializer.serialize_str("pbkdf2"),
|
|
|
|
KdfSer::Scrypt => serializer.serialize_str("scrypt"),
|
|
|
|
}
|
|
|
|
}
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
2017-07-06 11:36:15 +02:00
|
|
|
impl<'a> Deserialize<'a> for KdfSer {
|
2020-08-05 06:08:03 +02:00
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'a>,
|
|
|
|
{
|
|
|
|
deserializer.deserialize_any(KdfSerVisitor)
|
|
|
|
}
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct KdfSerVisitor;
|
|
|
|
|
2017-07-06 11:36:15 +02:00
|
|
|
impl<'a> Visitor<'a> for KdfSerVisitor {
|
2020-08-05 06:08:03 +02:00
|
|
|
type Value = KdfSer;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(formatter, "a kdf algorithm identifier")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: SerdeError,
|
|
|
|
{
|
|
|
|
match value {
|
|
|
|
"pbkdf2" => Ok(KdfSer::Pbkdf2),
|
|
|
|
"scrypt" => Ok(KdfSer::Scrypt),
|
|
|
|
_ => Err(SerdeError::custom(Error::UnsupportedKdf)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: SerdeError,
|
|
|
|
{
|
|
|
|
self.visit_str(value.as_ref())
|
|
|
|
}
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum Prf {
|
2020-08-05 06:08:03 +02:00
|
|
|
HmacSha256,
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for Prf {
|
2020-08-05 06:08:03 +02:00
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
match *self {
|
|
|
|
Prf::HmacSha256 => serializer.serialize_str("hmac-sha256"),
|
|
|
|
}
|
|
|
|
}
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
2017-07-06 11:36:15 +02:00
|
|
|
impl<'a> Deserialize<'a> for Prf {
|
2020-08-05 06:08:03 +02:00
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'a>,
|
|
|
|
{
|
|
|
|
deserializer.deserialize_any(PrfVisitor)
|
|
|
|
}
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct PrfVisitor;
|
|
|
|
|
2017-07-06 11:36:15 +02:00
|
|
|
impl<'a> Visitor<'a> for PrfVisitor {
|
2020-08-05 06:08:03 +02:00
|
|
|
type Value = Prf;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(formatter, "a prf algorithm identifier")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: SerdeError,
|
|
|
|
{
|
|
|
|
match value {
|
|
|
|
"hmac-sha256" => Ok(Prf::HmacSha256),
|
|
|
|
_ => Err(SerdeError::custom(Error::InvalidPrf)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
|
|
|
|
where
|
|
|
|
E: SerdeError,
|
|
|
|
{
|
|
|
|
self.visit_str(value.as_ref())
|
|
|
|
}
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Pbkdf2 {
|
2020-08-05 06:08:03 +02:00
|
|
|
pub c: NonZeroU32,
|
|
|
|
pub dklen: u32,
|
|
|
|
pub prf: Prf,
|
|
|
|
pub salt: Bytes,
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Scrypt {
|
2020-08-05 06:08:03 +02:00
|
|
|
pub dklen: u32,
|
|
|
|
pub p: u32,
|
|
|
|
pub n: u32,
|
|
|
|
pub r: u32,
|
|
|
|
pub salt: Bytes,
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum KdfSerParams {
|
2020-08-05 06:08:03 +02:00
|
|
|
Pbkdf2(Pbkdf2),
|
|
|
|
Scrypt(Scrypt),
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for KdfSerParams {
|
2020-08-05 06:08:03 +02:00
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
match *self {
|
|
|
|
KdfSerParams::Pbkdf2(ref params) => params.serialize(serializer),
|
|
|
|
KdfSerParams::Scrypt(ref params) => params.serialize(serializer),
|
|
|
|
}
|
|
|
|
}
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
2017-07-06 11:36:15 +02:00
|
|
|
impl<'a> Deserialize<'a> for KdfSerParams {
|
2020-08-05 06:08:03 +02:00
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'a>,
|
|
|
|
{
|
|
|
|
use serde_json::{from_value, Value};
|
|
|
|
|
|
|
|
let v: Value = Deserialize::deserialize(deserializer)?;
|
|
|
|
|
|
|
|
from_value(v.clone())
|
|
|
|
.map(KdfSerParams::Pbkdf2)
|
|
|
|
.or_else(|_| from_value(v).map(KdfSerParams::Scrypt))
|
|
|
|
.map_err(|_| D::Error::custom("Invalid KDF algorithm"))
|
|
|
|
}
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum Kdf {
|
2020-08-05 06:08:03 +02:00
|
|
|
Pbkdf2(Pbkdf2),
|
|
|
|
Scrypt(Scrypt),
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|