libsignify 0.6.0

Create cryptographic signatures for files and verify them
Documentation
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
use crate::consts::{KeyNumber, FULL_KEY_LEN, KDFALG, PKGALG, PUBLIC_KEY_LEN, SIG_LEN};
use crate::errors::Error;

use alloc::string::String;
use core::convert::TryInto;
use rand_core::{CryptoRng, RngCore};
use sha2::{Digest, Sha512};
use zeroize::{Zeroize, Zeroizing};

/// The public half of a keypair.
///
/// You will need this if you are trying to verify a signature.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct PublicKey {
    pub(crate) keynum: KeyNumber,
    pub(crate) key: [u8; PUBLIC_KEY_LEN],
}

impl PublicKey {
    /// The public key's bytes.
    pub fn key(&self) -> [u8; PUBLIC_KEY_LEN] {
        self.key
    }

    /// The public key's identifying number.
    pub fn keynum(&self) -> KeyNumber {
        self.keynum
    }
}

/// Key derivation options available when creating a new key.
#[derive(Clone)]
pub enum NewKeyOpts {
    /// Don't encrypt the secret key.
    NoEncryption,
    /// Encrypt the secret key with a passphrase.
    Encrypted {
        /// Passphrase to encrypt the key with.
        passphrase: String,
        /// The number of key derivation rounds to apply to the password.
        ///
        /// If you're unsure of what this should be set to, use [the default].
        ///
        /// [the default]: crate::consts::DEFAULT_KDF_ROUNDS
        kdf_rounds: u32,
    },
}

impl Drop for NewKeyOpts {
    fn drop(&mut self) {
        if let NewKeyOpts::Encrypted { passphrase, .. } = self {
            passphrase.zeroize();
        }
    }
}

impl core::fmt::Debug for NewKeyOpts {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::NoEncryption => f.write_str("NoEncryption"),
            Self::Encrypted { kdf_rounds, .. } => f
                .debug_struct("Encrypted")
                .field("passphrase", &"<concealed>")
                .field("kdf_rounds", kdf_rounds)
                .finish(),
        }
    }
}

struct UnencryptedKey(Zeroizing<[u8; FULL_KEY_LEN]>);

/// The full secret keypair.
///
/// You will need this if you want to create signatures.
#[derive(Clone)]
#[cfg_attr(test, derive(Debug, PartialEq, Eq))] // Makes the encoding tests nicer.
pub struct PrivateKey {
    pub(crate) public_key_alg: [u8; 2],
    pub(crate) kdf_alg: [u8; 2],
    pub(crate) kdf_rounds: u32,
    pub(crate) salt: [u8; 16],
    pub(crate) checksum: [u8; 8],
    pub(super) keynum: KeyNumber,
    pub(super) complete_key: Zeroizing<[u8; FULL_KEY_LEN]>,
}

impl PrivateKey {
    /// Generates a new random secret (private) key with the provided options.
    ///
    /// # Errors
    ///
    /// This only returns an error if the provided password was empty.
    pub fn generate<R: CryptoRng + RngCore>(
        rng: &mut R,
        derivation_info: NewKeyOpts,
    ) -> Result<Self, Error> {
        let keynum = KeyNumber::generate(rng);

        let key_pair = ed25519_dalek::SigningKey::generate(rng);

        let mut complete_key = UnencryptedKey(Zeroizing::new(key_pair.to_keypair_bytes()));

        let checksum = Self::calculate_checksum(&complete_key);

        let mut salt = [0; 16];
        rng.fill_bytes(&mut salt);

        let kdf_rounds = if let NewKeyOpts::Encrypted {
            passphrase,
            kdf_rounds,
        } = &derivation_info
        {
            let kdf_rounds = *kdf_rounds;
            Self::inner_kdf_mix(&mut complete_key.0[..32], kdf_rounds, &salt, passphrase)?;
            kdf_rounds
        } else {
            0
        };

        Ok(Self {
            public_key_alg: PKGALG,
            kdf_alg: KDFALG,
            kdf_rounds,
            salt,
            checksum,
            keynum,
            // The state of the key doesn't matter at this stage.
            complete_key: complete_key.0,
        })
    }

    /// Decrypts a secret key that was stored in encrypted form with the passphrase.
    ///
    /// # Errors
    ///
    /// This returns an error if the provided password was empty or if it failed to decrypt the key.
    pub fn decrypt_with_password(&mut self, passphrase: &str) -> Result<(), Error> {
        let mut encrypted_key = self.complete_key.clone(); // Cheap :)

        match Self::inner_kdf_mix(
            &mut encrypted_key[..32],
            self.kdf_rounds,
            &self.salt,
            passphrase,
        ) {
            Ok(_) => {
                // Since the decryption worked, its now "unencrypted", even if the passphrase was wrong
                // and the value is garbage.
                let decrypted_key = UnencryptedKey(encrypted_key);
                let current_checksum = Self::calculate_checksum(&decrypted_key);

                // Non-constant time is fine since checksum is public.
                if current_checksum != self.checksum {
                    return Err(Error::BadPassword);
                }

                // Confirmed the decryption worked and the keys are matching.
                PrivateKey::from_key_bytes(&decrypted_key.0).map(drop)?;

                self.complete_key = decrypted_key.0;

                Ok(())
            }
            Err(e) => Err(e),
        }
    }

    /// Creates a well-typed signing keypair from raw bytes.
    ///
    /// This also validates the provided public/verifying key matches the
    /// private/signing key.
    pub(crate) fn from_key_bytes(
        complete_key: &[u8; FULL_KEY_LEN],
    ) -> Result<ed25519_dalek::SigningKey, Error> {
        ed25519_dalek::SigningKey::from_keypair_bytes(complete_key).map_err(|_| Error::WrongKey)
    }

    fn calculate_checksum(complete_key: &UnencryptedKey) -> [u8; 8] {
        let digest = Sha512::digest(complete_key.0.as_ref());
        let mut checksum = [0; 8];
        checksum.copy_from_slice(&digest.as_slice()[0..8]);
        checksum
    }

    fn inner_kdf_mix(
        secret_key: &mut [u8],
        rounds: u32,
        salt: &[u8],
        passphrase: &str,
    ) -> Result<(), Error> {
        if rounds == 0 {
            return Ok(());
        }

        let mut xorkey = Zeroizing::new([0; FULL_KEY_LEN]);
        let mut workspace = Zeroizing::new([0; FULL_KEY_LEN]);

        bcrypt_pbkdf::bcrypt_pbkdf_with_memory(
            passphrase,
            salt,
            rounds,
            xorkey.as_mut_slice(),
            workspace.as_mut_slice(),
        )
        .map_err(|_| Error::BadPassword)?;

        for (prv, xor) in secret_key.iter_mut().zip(xorkey.iter()) {
            *prv ^= xor;
        }

        Ok(())
    }

    /// Returns the public half of this secret keypair.
    pub fn public(&self) -> PublicKey {
        // This `unwrap()` gets erased in release mode.
        PublicKey {
            key: self.complete_key[32..].try_into().unwrap(),
            keynum: self.keynum,
        }
    }

    /// Returns if this key was stored encrypted.
    pub fn is_encrypted(&self) -> bool {
        self.kdf_rounds != 0
    }
}

/// A signature
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct Signature {
    pub(crate) keynum: KeyNumber,
    pub(crate) sig: [u8; SIG_LEN],
}

impl Signature {
    /// The ID of the keypair which created this signature.
    ///
    /// This is useful to determine if you have the right key to verify a signature.
    pub fn signer_keynum(&self) -> KeyNumber {
        self.keynum
    }

    /// Returns the signature's raw bytes.
    pub fn signature(&self) -> [u8; SIG_LEN] {
        self.sig
    }

    pub(super) fn new(keynum: KeyNumber, sig: [u8; SIG_LEN]) -> Self {
        Self { keynum, sig }
    }
}

#[cfg(test)]
mod tests {
    use crate::consts::DEFAULT_KDF_ROUNDS;
    use crate::test_utils::StepperRng;

    use super::*;
    use alloc::string::ToString;
    use core::fmt::Debug;
    use core::hash::Hash;
    use static_assertions::assert_impl_all;

    assert_impl_all!(
        PublicKey: Clone,
        Copy,
        Debug,
        Eq,
        PartialEq,
        Hash,
        Send,
        Sync
    );

    assert_impl_all!(PrivateKey: Clone, Send, Sync);

    assert_impl_all!(NewKeyOpts: Clone, Debug, Send, Sync);

    assert_impl_all!(
        Signature: Clone,
        Copy,
        Debug,
        Eq,
        PartialEq,
        Hash,
        Send,
        Sync
    );

    const PASSPHRASE: &str = "muchsecret";

    #[test]
    fn check_key_generation_passphrase_concealment() {
        let new_opts = NewKeyOpts::Encrypted {
            passphrase: PASSPHRASE.to_string(),
            kdf_rounds: DEFAULT_KDF_ROUNDS,
        };
        let debug_output = alloc::format!("{:?}", new_opts);
        assert!(!debug_output.contains(PASSPHRASE));
    }

    #[test]
    fn check_simple_private_key_getters() {
        let mut rng = StepperRng::default();
        let unencrypted_key = PrivateKey::generate(&mut rng, NewKeyOpts::NoEncryption).unwrap();

        assert_eq!(
            unencrypted_key.public().key(),
            unencrypted_key.complete_key[32..]
        ); // Ed25519 keys are private || public.

        assert!(!unencrypted_key.is_encrypted());

        let encrypted_key = PrivateKey::generate(
            &mut rng,
            NewKeyOpts::Encrypted {
                passphrase: PASSPHRASE.to_string(),
                kdf_rounds: DEFAULT_KDF_ROUNDS,
            },
        )
        .unwrap();
        assert!(encrypted_key.is_encrypted());
    }

    #[test]
    fn check_key_generation_opts() {
        let mut rng = StepperRng::default();
        let unencrypted_key = PrivateKey::generate(&mut rng, NewKeyOpts::NoEncryption).unwrap();
        assert_eq!(unencrypted_key.kdf_rounds, 0); // `0` represents not encrypted.
        assert_eq!(unencrypted_key.kdf_alg, KDFALG);
        assert_eq!(unencrypted_key.public_key_alg, PKGALG);

        let encrypted_key_1 = PrivateKey::generate(
            &mut rng,
            NewKeyOpts::Encrypted {
                passphrase: PASSPHRASE.to_string(),
                kdf_rounds: DEFAULT_KDF_ROUNDS,
            },
        )
        .unwrap();
        assert_eq!(encrypted_key_1.kdf_rounds, DEFAULT_KDF_ROUNDS);
        assert_eq!(encrypted_key_1.kdf_alg, KDFALG);
        assert_eq!(encrypted_key_1.public_key_alg, PKGALG);

        // Check non-standard KDF rounds are respected.
        let encrypted_key_2 = PrivateKey::generate(
            &mut rng,
            NewKeyOpts::Encrypted {
                passphrase: PASSPHRASE.to_string(),
                kdf_rounds: 7,
            },
        )
        .unwrap();
        assert_eq!(encrypted_key_2.kdf_rounds, 7);
        assert_eq!(encrypted_key_1.kdf_alg, KDFALG);
        assert_eq!(encrypted_key_2.public_key_alg, PKGALG);

        // Salts should be random.
        assert_ne!(encrypted_key_1.salt, encrypted_key_2.salt);
        // Key numbers should be unique.
        assert_ne!(encrypted_key_1.keynum, encrypted_key_2.keynum);
        // The keys themselves should be random and unique.
        assert_ne!(encrypted_key_1.complete_key, encrypted_key_2.complete_key);
        assert_ne!(encrypted_key_1.checksum, encrypted_key_2.checksum);
    }

    struct ConstantRng;

    impl ConstantRng {
        const VALUE: u8 = 3;
    }

    impl rand_core::RngCore for ConstantRng {
        fn next_u32(&mut self) -> u32 {
            Self::VALUE.into()
        }

        fn next_u64(&mut self) -> u64 {
            Self::VALUE.into()
        }

        fn fill_bytes(&mut self, dest: &mut [u8]) {
            for b in dest {
                *b = Self::VALUE;
            }
        }

        fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
            for b in dest {
                *b = Self::VALUE;
            }

            Ok(())
        }
    }

    impl rand_core::CryptoRng for ConstantRng {}

    #[test]
    fn check_key_encryption_roundtrip() {
        const ACTUAL_KEY: [u8; 32] = [ConstantRng::VALUE; 32];
        let mut rng = ConstantRng;

        let mut encrypted_key = PrivateKey::generate(
            &mut rng,
            NewKeyOpts::Encrypted {
                passphrase: PASSPHRASE.to_string(),
                kdf_rounds: DEFAULT_KDF_ROUNDS,
            },
        )
        .unwrap();

        // Easy check that its actually being encrypted when requested.
        assert_ne!(encrypted_key.complete_key.as_ref()[..32], ACTUAL_KEY);

        // ... and then make sure it properly decrypts.
        encrypted_key.decrypt_with_password(PASSPHRASE).unwrap();
        assert_eq!(encrypted_key.complete_key.as_ref()[..32], ACTUAL_KEY);
    }

    #[test]
    fn check_wrong_passphrase_errors() {
        let mut rng = StepperRng::default();
        let mut encrypted_key = PrivateKey::generate(
            &mut rng,
            NewKeyOpts::Encrypted {
                passphrase: PASSPHRASE.to_string(),
                kdf_rounds: DEFAULT_KDF_ROUNDS,
            },
        )
        .unwrap();

        assert!(matches!(
            encrypted_key.decrypt_with_password("wrong"),
            Err(Error::BadPassword)
        ));
    }
}