2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-06-20 10:06:49 +02:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// 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
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-02-13 16:38:47 +01:00
|
|
|
use std::fmt;
|
|
|
|
use serde::{Serialize, Serializer, Deserialize, Deserializer};
|
|
|
|
use serde::de::{Visitor, Error as SerdeError};
|
2016-06-20 00:10:34 +02:00
|
|
|
use super::{Error, H256};
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum KdfSer {
|
|
|
|
Pbkdf2,
|
|
|
|
Scrypt,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for KdfSer {
|
2017-02-13 16:38:47 +01:00
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
2016-06-20 00:10:34 +02:00
|
|
|
where S: Serializer {
|
|
|
|
match *self {
|
|
|
|
KdfSer::Pbkdf2 => serializer.serialize_str("pbkdf2"),
|
|
|
|
KdfSer::Scrypt => serializer.serialize_str("scrypt"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deserialize for KdfSer {
|
2017-02-13 16:38:47 +01:00
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
2016-06-20 00:10:34 +02:00
|
|
|
where D: Deserializer {
|
|
|
|
deserializer.deserialize(KdfSerVisitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct KdfSerVisitor;
|
|
|
|
|
|
|
|
impl Visitor for KdfSerVisitor {
|
|
|
|
type Value = KdfSer;
|
|
|
|
|
2017-02-13 16:38:47 +01:00
|
|
|
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 {
|
2016-06-20 00:10:34 +02:00
|
|
|
match value {
|
|
|
|
"pbkdf2" => Ok(KdfSer::Pbkdf2),
|
|
|
|
"scrypt" => Ok(KdfSer::Scrypt),
|
|
|
|
_ => Err(SerdeError::custom(Error::UnsupportedKdf))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 16:38:47 +01:00
|
|
|
fn visit_string<E>(self, value: String) -> Result<Self::Value, E> where E: SerdeError {
|
2016-06-20 00:10:34 +02:00
|
|
|
self.visit_str(value.as_ref())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum Prf {
|
|
|
|
HmacSha256,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for Prf {
|
2017-02-13 16:38:47 +01:00
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
2016-06-20 00:10:34 +02:00
|
|
|
where S: Serializer {
|
|
|
|
match *self {
|
|
|
|
Prf::HmacSha256 => serializer.serialize_str("hmac-sha256"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deserialize for Prf {
|
2017-02-13 16:38:47 +01:00
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
2016-06-20 00:10:34 +02:00
|
|
|
where D: Deserializer {
|
|
|
|
deserializer.deserialize(PrfVisitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PrfVisitor;
|
|
|
|
|
|
|
|
impl Visitor for PrfVisitor {
|
|
|
|
type Value = Prf;
|
|
|
|
|
2017-02-13 16:38:47 +01:00
|
|
|
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 {
|
2016-06-20 00:10:34 +02:00
|
|
|
match value {
|
|
|
|
"hmac-sha256" => Ok(Prf::HmacSha256),
|
|
|
|
_ => Err(SerdeError::custom(Error::InvalidPrf)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 16:38:47 +01:00
|
|
|
fn visit_string<E>(self, value: String) -> Result<Self::Value, E> where E: SerdeError {
|
2016-06-20 00:10:34 +02:00
|
|
|
self.visit_str(value.as_ref())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Pbkdf2 {
|
|
|
|
pub c: u32,
|
|
|
|
pub dklen: u32,
|
|
|
|
pub prf: Prf,
|
|
|
|
pub salt: H256,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct Scrypt {
|
|
|
|
pub dklen: u32,
|
|
|
|
pub p: u32,
|
|
|
|
pub n: u32,
|
|
|
|
pub r: u32,
|
|
|
|
pub salt: H256,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum KdfSerParams {
|
|
|
|
Pbkdf2(Pbkdf2),
|
|
|
|
Scrypt(Scrypt),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for KdfSerParams {
|
2017-02-13 16:38:47 +01:00
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
2016-06-20 00:10:34 +02:00
|
|
|
where S: Serializer {
|
|
|
|
match *self {
|
|
|
|
KdfSerParams::Pbkdf2(ref params) => params.serialize(serializer),
|
|
|
|
KdfSerParams::Scrypt(ref params) => params.serialize(serializer),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deserialize for KdfSerParams {
|
2017-02-13 16:38:47 +01:00
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
2016-06-20 00:10:34 +02:00
|
|
|
where D: Deserializer {
|
2017-02-13 16:38:47 +01:00
|
|
|
use serde_json::{Value, from_value};
|
|
|
|
|
|
|
|
let v: Value = Deserialize::deserialize(deserializer)?;
|
2016-06-20 00:10:34 +02:00
|
|
|
|
2017-02-13 16:38:47 +01:00
|
|
|
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 {
|
|
|
|
Pbkdf2(Pbkdf2),
|
|
|
|
Scrypt(Scrypt),
|
|
|
|
}
|