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
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity Ethereum.

// Parity Ethereum 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 Ethereum 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 Ethereum.  If not, see <http://www.gnu.org/licenses/>.

use super::{Bytes, Error};
use serde::{
    de::{Error as SerdeError, Visitor},
    Deserialize, Deserializer, Serialize, Serializer,
};
use std::fmt;

#[derive(Debug, PartialEq)]
pub enum KdfSer {
    Pbkdf2,
    Scrypt,
}

impl Serialize for KdfSer {
    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"),
        }
    }
}

impl<'a> Deserialize<'a> for KdfSer {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where D: Deserializer<'a> {
        deserializer.deserialize_any(KdfSerVisitor)
    }
}

struct KdfSerVisitor;

impl<'a> Visitor<'a> for KdfSerVisitor {
    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())
    }
}

#[derive(Debug, PartialEq)]
pub enum Prf {
    HmacSha256,
}

impl Serialize for Prf {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where S: Serializer {
        match *self {
            Prf::HmacSha256 => serializer.serialize_str("hmac-sha256"),
        }
    }
}

impl<'a> Deserialize<'a> for Prf {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where D: Deserializer<'a> {
        deserializer.deserialize_any(PrfVisitor)
    }
}

struct PrfVisitor;

impl<'a> Visitor<'a> for PrfVisitor {
    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())
    }
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Pbkdf2 {
    pub c: u32,
    pub dklen: u32,
    pub prf: Prf,
    pub salt: Bytes,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Scrypt {
    pub dklen: u32,
    pub p: u32,
    pub n: u32,
    pub r: u32,
    pub salt: Bytes,
}

#[derive(Debug, PartialEq)]
pub enum KdfSerParams {
    Pbkdf2(Pbkdf2),
    Scrypt(Scrypt),
}

impl Serialize for KdfSerParams {
    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),
        }
    }
}

impl<'a> Deserialize<'a> for KdfSerParams {
    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"))
    }
}

#[derive(Debug, PartialEq)]
pub enum Kdf {
    Pbkdf2(Pbkdf2),
    Scrypt(Scrypt),
}