mini_sign/
secret_key.rs

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
use std::fmt::Display;

use crate::{
    util::raw_scrypt_params, Result, SError, ALG_SIZE, CHK_ALG, CHK_SIZE, COMPONENT_SIZE, KDF_ALG,
    KDF_LIMIT_SIZE, KDF_SALT_SIZE, KEYNUM_SK_SIZE, KEY_SIG_ALG, KID_SIZE, MEMLIMIT, N_LOG2_MAX,
    OPSLIMIT,
};
use base64::Engine;
use blake2::{Blake2b, Digest};
use ed25519_dalek::{
    ed25519::{self, ComponentBytes},
    Signer,
};
use scrypt::password_hash::rand_core::{self, RngCore};
use zeroize::{Zeroize, ZeroizeOnDrop};

/// A `SecretKeyBox` represents a minisign secret key.
///
/// also can be output to a string and parse from a str.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SecretKeyBox<'s> {
    pub(crate) untrusted_comment: Option<&'s str>,
    pub(crate) secret_key: SecretKey,
}
impl Display for SecretKeyBox<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut s = String::new();
        s.push_str("untrusted comment: ");
        if let Some(c) = self.untrusted_comment {
            s.push_str(c);
        }
        s.push('\n');
        let encoder = base64::engine::general_purpose::STANDARD;
        let mut sk_format = vec![];
        sk_format.extend_from_slice(&self.secret_key.sig_alg);
        sk_format.extend_from_slice(&self.secret_key.kdf_alg);
        sk_format.extend_from_slice(&self.secret_key.cksum_alg);
        sk_format.extend_from_slice(&self.secret_key.kdf_salt);
        sk_format.extend_from_slice(&self.secret_key.kdf_opslimit.to_le_bytes());
        sk_format.extend_from_slice(&self.secret_key.kdf_memlimit.to_le_bytes());
        sk_format.extend_from_slice(&self.secret_key.keynum_sk);
        let sk = encoder.encode(&sk_format);
        s.push_str(&sk);
        s.push('\n');

        write!(f, "{}", s)
    }
}
type Blake2b256 = Blake2b<blake2::digest::consts::U32>;
impl<'s> SecretKeyBox<'s> {
    fn new(untrusted_comment: Option<&'s str>, secret_key: SecretKey) -> Self {
        Self {
            untrusted_comment,
            secret_key,
        }
    }
    pub fn from_signing_key(
        signing_key: ed25519_dalek::SigningKey,
        kid: &[u8; KID_SIZE],
        password: Option<&[u8]>,
        untrusted_comment: Option<&'s str>,
    ) -> Result<Self> {
        let sk = signing_key.to_bytes();
        let pk = signing_key.verifying_key().to_bytes();
        let mut dest = [0u8; KDF_SALT_SIZE];
        rand_core::OsRng.try_fill_bytes(&mut dest)?;

        let mut hash = Blake2b256::new();
        hash.update(KEY_SIG_ALG);
        hash.update(kid);
        hash.update(sk);
        hash.update(pk);
        let mut kdf_buf = kdf(password, &dest, OPSLIMIT, MEMLIMIT)?;
        let keynum_sk = KeynumSK {
            key_id: *kid,
            sec_key: RawSk(sk),
            pub_key: pk,
            checksum: hash.finalize().to_vec().try_into().unwrap(),
        };
        kdf_buf = keynum_sk.to_bytes(kdf_buf);
        let secret_key = SecretKey {
            sig_alg: KEY_SIG_ALG,
            kdf_alg: KDF_ALG,
            cksum_alg: CHK_ALG,
            kdf_salt: dest,
            kdf_opslimit: OPSLIMIT,
            kdf_memlimit: MEMLIMIT,
            keynum_sk: kdf_buf,
        };

        Ok(Self::new(untrusted_comment, secret_key))
    }
    pub(crate) fn sign(
        &self,
        message: &[u8],
        password: Option<&[u8]>,
    ) -> Result<ed25519::Signature> {
        self.secret_key.sign(message, password)
    }
    pub(crate) fn xor_keynum_sk(&self, password: Option<&[u8]>) -> Result<KeynumSK> {
        self.secret_key.xor_keynum_sk(password)
    }
    /// Parse a `SecretKeyBox` from str.
    ///
    /// as it store in a file.
    #[allow(clippy::should_implement_trait)]
    pub fn from_str(s: &'s str) -> Result<Self> {
        parse_secret_key(s)
    }
    /// Get the untrusted comment.
    pub fn untrusted_comment(&self) -> Option<&'s str> {
        self.untrusted_comment
    }
}
fn parse_secret_key(s: &str) -> Result<SecretKeyBox> {
    let mut lines = s.lines();
    if let Some(c) = lines.next() {
        let untrusted_comment = c.strip_prefix("untrusted comment: ");
        let secret_key = lines
            .next()
            .ok_or_else(|| SError::new(crate::ErrorKind::SecretKey, "missing secret key"))?;
        let decoder = base64::engine::general_purpose::STANDARD;
        let sk_format = decoder
            .decode(secret_key.as_bytes())
            .map_err(|e| SError::new(crate::ErrorKind::SecretKey, e))?;
        if sk_format.len()
            != ALG_SIZE
                + ALG_SIZE
                + ALG_SIZE
                + KDF_SALT_SIZE
                + KDF_LIMIT_SIZE
                + KDF_LIMIT_SIZE
                + KEYNUM_SK_SIZE
        {
            return Err(SError::new(
                crate::ErrorKind::SecretKey,
                "invalid secret key length",
            ));
        }
        let sig_alg = &sk_format[..ALG_SIZE];
        let kdf_alg = &sk_format[ALG_SIZE..ALG_SIZE + ALG_SIZE];
        let cksum_alg = &sk_format[ALG_SIZE + ALG_SIZE..ALG_SIZE + ALG_SIZE + ALG_SIZE];
        let kdf_salt = &sk_format
            [ALG_SIZE + ALG_SIZE + ALG_SIZE..ALG_SIZE + ALG_SIZE + ALG_SIZE + KDF_SALT_SIZE];
        let kdf_opslimit = u64::from_le_bytes(
            sk_format[ALG_SIZE + ALG_SIZE + ALG_SIZE + KDF_SALT_SIZE
                ..ALG_SIZE + ALG_SIZE + ALG_SIZE + KDF_SALT_SIZE + KDF_LIMIT_SIZE]
                .try_into()
                .unwrap(),
        );
        let kdf_memlimit = u64::from_le_bytes(
            sk_format[ALG_SIZE + ALG_SIZE + ALG_SIZE + KDF_SALT_SIZE + KDF_LIMIT_SIZE
                ..ALG_SIZE + ALG_SIZE + ALG_SIZE + KDF_SALT_SIZE + KDF_LIMIT_SIZE + KDF_LIMIT_SIZE]
                .try_into()
                .unwrap(),
        );

        let secret_key = SecretKey {
            sig_alg: sig_alg.try_into().unwrap(),
            kdf_alg: kdf_alg.try_into().unwrap(),
            cksum_alg: cksum_alg.try_into().unwrap(),
            kdf_salt: kdf_salt.try_into().unwrap(),
            kdf_opslimit,
            kdf_memlimit,
            keynum_sk: sk_format[ALG_SIZE
                + ALG_SIZE
                + ALG_SIZE
                + KDF_SALT_SIZE
                + KDF_LIMIT_SIZE
                + KDF_LIMIT_SIZE..]
                .try_into()
                .unwrap(),
        };
        Ok(SecretKeyBox::new(untrusted_comment, secret_key))
    } else {
        Err(SError::new(
            crate::ErrorKind::SecretKey,
            "missing untrusted comment",
        ))
    }
}

#[cfg(test)]
#[test]
fn test_parse_secret_key() {
    use crate::KeyPairBox;
    let password = b"password";
    let k = KeyPairBox::generate(Some(password), None, None).unwrap();
    let file = k.secret_key_box.to_string();
    let sk = parse_secret_key(&file).unwrap();
    assert_eq!(file, sk.to_string());
}
/// A `SecretKey` is used to sign messages.
#[derive(Clone, Debug, ZeroizeOnDrop, PartialEq, Eq)]
pub(crate) struct SecretKey {
    pub sig_alg: [u8; ALG_SIZE],
    kdf_alg: [u8; ALG_SIZE],
    cksum_alg: [u8; ALG_SIZE],
    kdf_salt: [u8; KDF_SALT_SIZE],
    kdf_opslimit: u64,
    kdf_memlimit: u64,
    keynum_sk: [u8; KEYNUM_SK_SIZE],
}
#[derive(Clone, Debug, ZeroizeOnDrop)]
pub struct KeynumSK {
    pub(crate) key_id: [u8; KID_SIZE],
    sec_key: RawSk,
    pub pub_key: ComponentBytes,
    checksum: [u8; CHK_SIZE],
}
impl KeynumSK {
    fn to_bytes(&self, mut kdf_buf: [u8; KEYNUM_SK_SIZE]) -> [u8; KEYNUM_SK_SIZE] {
        for (i, item) in kdf_buf.iter_mut().enumerate().take(KID_SIZE) {
            *item ^= self.key_id[i];
        }
        for i in 0..COMPONENT_SIZE {
            kdf_buf[KID_SIZE + i] ^= self.sec_key.0[i];
        }
        for i in 0..COMPONENT_SIZE {
            kdf_buf[KID_SIZE + COMPONENT_SIZE + i] ^= self.pub_key[i];
        }
        for i in 0..CHK_SIZE {
            kdf_buf[KID_SIZE + 2 * COMPONENT_SIZE + i] ^= self.checksum[i];
        }
        kdf_buf
    }
    fn from_bytes(keynum_sk: &[u8; KEYNUM_SK_SIZE], mut kdf_buf: [u8; KEYNUM_SK_SIZE]) -> Self {
        // let mut kdf_buf = [0u8; KEYNUM_SK_SIZE];
        for i in 0..KEYNUM_SK_SIZE {
            kdf_buf[i] ^= keynum_sk[i];
        }
        Self {
            key_id: kdf_buf[0..KID_SIZE].try_into().unwrap(),
            sec_key: RawSk(
                kdf_buf[KID_SIZE..KID_SIZE + COMPONENT_SIZE]
                    .try_into()
                    .unwrap(),
            ),
            pub_key: kdf_buf[KID_SIZE + COMPONENT_SIZE..KID_SIZE + 2 * COMPONENT_SIZE]
                .try_into()
                .unwrap(),
            checksum: kdf_buf[KID_SIZE + 2 * COMPONENT_SIZE..KEYNUM_SK_SIZE]
                .try_into()
                .unwrap(),
        }
    }
}
#[derive(Debug, Clone, ZeroizeOnDrop, Zeroize)]
struct RawSk(ComponentBytes);
impl Signer<ed25519::Signature> for RawSk {
    fn try_sign(&self, msg: &[u8]) -> std::result::Result<ed25519::Signature, ed25519::Error> {
        let sk = ed25519_dalek::SigningKey::from_bytes(&self.0);
        Ok(sk.sign(msg))
    }
}
fn kdf(
    password: Option<&[u8]>,
    salt: &[u8; KDF_SALT_SIZE],
    opslimit: u64,
    memlimit: u64,
) -> Result<[u8; KEYNUM_SK_SIZE]> {
    let params = raw_scrypt_params(memlimit as usize, opslimit, N_LOG2_MAX)?;
    let mut stream = [0u8; KEYNUM_SK_SIZE];
    scrypt::scrypt(password.unwrap_or(&[]), salt, &params, &mut stream)?;
    Ok(stream)
}
impl SecretKey {
    pub fn sign(&self, message: &[u8], password: Option<&[u8]>) -> Result<ed25519::Signature> {
        let keynum_sk = self.xor_keynum_sk(password);
        Ok(keynum_sk?.sec_key.sign(message))
    }
    pub(crate) fn xor_keynum_sk(&self, password: Option<&[u8]>) -> Result<KeynumSK> {
        let stream = kdf(
            password,
            &self.kdf_salt,
            self.kdf_opslimit,
            self.kdf_memlimit,
        )?;

        let keynum_sk = KeynumSK::from_bytes(&self.keynum_sk, stream);

        let mut hash = Blake2b256::new();
        hash.update(self.sig_alg);
        hash.update(&keynum_sk.key_id);
        hash.update(&keynum_sk.sec_key.0);
        hash.update(&keynum_sk.pub_key);
        if hash.finalize().to_vec() != keynum_sk.checksum {
            return Err(SError::new(
                crate::ErrorKind::SecretKey,
                "checksum mismatch, invalid password",
            ));
        }
        Ok(keynum_sk)
    }
}
#[cfg(test)]
#[test]
fn test_sign() {
    use crate::{pub_key_from_sec_key, KeyPairBox};
    let password = b"password";
    let k = KeyPairBox::generate(Some(password), None, None).unwrap();
    let s = k.secret_key_box.to_string();
    let sk = parse_secret_key(&s).unwrap();
    let msg = b"hello world";
    let sig = sk.sign(msg, Some(password)).unwrap();
    let pk = pub_key_from_sec_key(&sk, Some(password)).unwrap();
    let v = pk.public_key.key.verify(msg, &sig);
    assert_eq!(v.unwrap(), true);
}