secretstore 0.9.0

Store a secret (such as a private key) in an encrypted file
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
use crate::encrypt_chacha as chacha;
use crate::encrypt_common::{EncryptionAuxData, EncryptionVersion, Encryptor};
use crate::encrypt_scrypt as scrypt;
use crate::encrypt_xor as xor;
use bitcoin_hashes::Sha256d;
use hex_conservative::{DisplayHex, FromHex};
use std::fs;
#[cfg(feature = "unixfilepermissions")]
use std::os::unix::fs::PermissionsExt;
use zeroize::{Zeroize, ZeroizeOnDrop};

/// Minimum accepted password length
pub const PASSWORD_MIN_LEN: usize = 7;
pub const SECRET_DATA_MIN_LEN: usize = 1;
pub const SECRET_DATA_MAX_LEN: usize = 65535;
pub const NONSECRET_DATA_MAX_LEN: usize = 255;

const MAGIC_BYTES_LEN: usize = 2;
const MAGIC_BYTES_STR: &str = "5353"; // "SS" from SeedStore; dec 83, 83
const CHECKSUM_LEN: usize = 4;
const BYTE_MAX: u8 = 255;

/// Store a secret data in an encrypred file.
/// Also store some nonsecret data.
/// Can be loaded from an encrypted file.
/// The secret is stored in memory scrambled (using an ephemeral scrambling key).
/// Secret data length can be between 1 and 65535 bytes.
/// See also [`SecretStoreCreator`].
pub struct SecretStore {
    /// The format version used.
    format_version: FormatVersion,
    /// The encryption version used.
    encryption_version: EncryptionVersion,
    /// Secret data, scrambled with the ephemeral key.
    scrambled_secret_data: Vec<u8>,
    /// The non-secret part of the data
    nonsecret_data: Vec<u8>,
    /// Auxiliary encryption data, such as salt using for encryption.
    encryption_aux_data: EncryptionAuxData,
    /// Scrambling key used to scramble the secret data in memory.
    ephemeral_scrambling_key: xor::EncryptionKey,
}

/// Helper class for creating the store from given data.
/// Should be used only by the utility that creates the encrypted file.
/// See also [`SecretStore`].
pub struct SecretStoreCreator {}

#[derive(Clone, Copy)]
enum FormatVersion {
    One = 1,
}

const FORMAT_VERSION_LATEST: FormatVersion = FormatVersion::One;
const FORMAT_VERSION_OLDEST: FormatVersion = FormatVersion::One;

impl SecretStore {
    /// Load the secret from a password-protected secret file.
    pub fn new_from_encrypted_file(
        path_for_secret_file: &str,
        encryption_password: &str,
    ) -> Result<Self, String> {
        let secret_payload = read_payload_from_file(path_for_secret_file)?;
        Self::new_from_payload(&secret_payload, encryption_password)
    }

    /// Load the secret store from encrypted data.
    /// Typically the data is stored in a file, but this method takes the contents directly.
    pub fn new_from_payload(
        secret_payload: &Vec<u8>,
        encryption_password: &str,
    ) -> Result<Self, String> {
        let (
            format_version,
            nonsecret_data,
            encryption_version,
            mut encrypted_secret_data,
            encryption_aux_data,
        ) = parse_payload(&secret_payload)?;
        let ephemeral_scrambling_key = xor::generate_key();
        let _res = scramble_encrypted_secret_data(
            encryption_version,
            &mut encrypted_secret_data,
            encryption_password,
            &encryption_aux_data,
            &ephemeral_scrambling_key,
        )?;
        Ok(Self {
            format_version,
            encryption_version,
            scrambled_secret_data: encrypted_secret_data,
            nonsecret_data,
            encryption_aux_data,
            ephemeral_scrambling_key,
        })
    }

    pub fn nonsecret_data(&self) -> &Vec<u8> {
        &self.nonsecret_data
    }

    /// Caution: unencrypted secret is returned in copy.
    #[cfg(test)]
    pub(crate) fn secret_data(&self) -> Result<Vec<u8>, String> {
        // Caution: unencrypted secret
        let mut decrypted = self.scrambled_secret_data.clone();
        let _res = descramble_secret(&mut decrypted, &self.ephemeral_scrambling_key)?;
        Ok(decrypted)
    }

    /// Invokes a processor function on the unencrypted secret data.
    /// Caution: unencrypted secret is made available in a user processor method.
    pub fn processed_secret_data<F, R>(&self, f: F) -> Result<R, String>
    where
        F: Fn(&Vec<u8>) -> Result<R, String>,
    {
        // Caution: unencrypted secret
        let mut decrypted = self.scrambled_secret_data.clone();
        let _res = descramble_secret(&mut decrypted, &self.ephemeral_scrambling_key)?;
        let res = f(&decrypted)?;
        decrypted.zeroize();
        drop(decrypted);
        Ok(res)
    }

    /// Write out secret content to a file.
    /// Use it through [`SecretStoreCreator`]
    pub(crate) fn write_to_file(
        &self,
        path_for_secret_file: &str,
        encryption_password: &str,
    ) -> Result<(), String> {
        let file_exists = fs::exists(path_for_secret_file).map_err(|e| {
            format!(
                "Could not check existence of secret file ({} {})",
                path_for_secret_file,
                e.to_string()
            )
        })?;
        if file_exists {
            return Err(format!(
                "A secret file already exisits, refusing to overwrite ({})",
                path_for_secret_file
            ));
        }

        // First create empty file
        let f = fs::File::create(path_for_secret_file).map_err(|e| {
            format!(
                "Error writing to file {}, {}",
                path_for_secret_file,
                e.to_string()
            )
        })?;

        // Create contents
        let encrypted_payload = self.assemble_encrypted_payload(encryption_password)?;

        // Set restricted permissions
        #[cfg(feature = "unixfilepermissions")]
        {
            let metadata = f.metadata().map_err(|e| {
                format!(
                    "Error getting file metadata {}, {}",
                    path_for_secret_file,
                    e.to_string()
                )
            })?;
            let mut permissions = metadata.permissions();

            permissions.set_mode(0o600); // Read/write for owner, no read for others.
            assert_eq!(permissions.mode(), 0o600);

            let _res = f.set_permissions(permissions).map_err(|e| {
                format!(
                    "Error removing permsissions from file {}, {}",
                    path_for_secret_file,
                    e.to_string()
                )
            })?;
        }

        // Write out contents
        let _res = fs::write(path_for_secret_file, encrypted_payload).map_err(|e| {
            format!(
                "Error writing to file {}, {}",
                path_for_secret_file,
                e.to_string()
            )
        })?;
        Ok(())
    }

    pub fn assemble_encrypted_payload(&self, encryption_password: &str) -> Result<Vec<u8>, String> {
        let mut encrypted = self.scrambled_secret_data.clone();
        let _res = encrypt_scrambled_secret_data(
            &mut encrypted,
            &self.ephemeral_scrambling_key,
            self.encryption_version,
            encryption_password,
            &self.encryption_aux_data,
        )?;
        assemble_payload(
            self.format_version,
            &self.nonsecret_data,
            self.encryption_version,
            &encrypted,
            &self.encryption_aux_data,
        )
    }

    /// Scramble arbitrary data in-place, with the ephemeral scrambling key.
    pub fn scramble_data(&self, data: &mut Vec<u8>) -> Result<(), String> {
        let _res = xor::XorEncryptor::encrypt_with_key(data, &self.ephemeral_scrambling_key)?;
        Ok(())
    }

    /// Descramble arbitrary data in-place, with the ephemeral scrambling key.
    pub fn descramble_data(&self, scrambled_data: &mut Vec<u8>) -> Result<(), String> {
        let _res =
            xor::XorEncryptor::encrypt_with_key(scrambled_data, &self.ephemeral_scrambling_key)?;
        Ok(())
    }
}

impl Zeroize for SecretStore {
    fn zeroize(&mut self) {
        self.scrambled_secret_data.zeroize();
        self.encryption_aux_data.zeroize();
        self.ephemeral_scrambling_key.zeroize();
        self.nonsecret_data.zeroize();
        self.format_version = FORMAT_VERSION_OLDEST;
    }
}

impl ZeroizeOnDrop for SecretStore {}

impl SecretStoreCreator {
    /// Create a new store instance from given contained data.
    /// The store can be written out to file using [`write_to_file`]
    pub fn new_from_data(
        nonsecret_data: Vec<u8>,
        secret_data: &Vec<u8>,
    ) -> Result<SecretStore, String> {
        let format_version = FORMAT_VERSION_LATEST;
        if nonsecret_data.len() > NONSECRET_DATA_MAX_LEN {
            return Err(format!(
                "Non-secret data too long, {} vs {}",
                nonsecret_data.len(),
                NONSECRET_DATA_MAX_LEN
            ));
        }
        if secret_data.len() > SECRET_DATA_MAX_LEN {
            return Err(format!(
                "Secret data too long, {} vs {}",
                secret_data.len(),
                SECRET_DATA_MAX_LEN
            ));
        }
        if secret_data.len() < SECRET_DATA_MIN_LEN {
            return Err(format!(
                "Secret data too short, {} vs {}",
                secret_data.len(),
                SECRET_DATA_MIN_LEN
            ));
        }

        // Use default encryption version
        let encryption_version = EncryptionVersion::V2Scrypt;
        let encryption_aux_data = scrypt::ScryptEncryptor::generate_aux_data();

        let ephemeral_scrambling_key = xor::generate_key();
        let mut scrambled_secret_data = secret_data.clone();
        let _res = scramble_secret(&mut scrambled_secret_data, &ephemeral_scrambling_key)?;
        Ok(SecretStore {
            format_version,
            encryption_version,
            scrambled_secret_data,
            nonsecret_data,
            ephemeral_scrambling_key,
            encryption_aux_data,
        })
    }

    /// Write out the encrypted contents to a file.
    /// ['encryption_password']: The passowrd to be used for encryption, should be strong.
    /// Minimal length of password is checked.
    pub fn write_to_file(
        secretstore: &SecretStore,
        path_for_secret_file: &str,
        encryption_password: &str,
    ) -> Result<(), String> {
        secretstore.write_to_file(path_for_secret_file, encryption_password)
    }
}

impl FormatVersion {
    fn from_u8(byte: u8) -> Result<Self, String> {
        match byte {
            1 => Ok(Self::One),
            _ => Err(format!("Invalid format version {}", byte)),
        }
    }
}

fn read_payload_from_file(path_for_secret_file: &str) -> Result<Vec<u8>, String> {
    let contents = fs::read(path_for_secret_file).map_err(|e| {
        format!(
            "Could not read file '{}', {}",
            path_for_secret_file,
            e.to_string()
        )
    })?;
    Ok(contents)
}

fn verify_payload_len(payload_len: usize, min_len: usize) -> Result<(), String> {
    if payload_len < min_len {
        return Err(format!(
            "File content is too short, {} vs {}",
            payload_len, min_len,
        ));
    }
    Ok(())
}

fn get_next_payload_byte(payload: &Vec<u8>, pos: &mut usize) -> Result<u8, String> {
    let next_byte = *payload
        .get(*pos)
        .ok_or(format!("File content is too short, {}", pos))?;
    *(pos) += 1;
    Ok(next_byte)
}

fn u16_from_two_bytes(b1: u8, b2: u8) -> u16 {
    ((b2 as u16) << 8) + (b1 as u16)
}

fn two_bytes_from_u16(s: u16) -> (u8, u8) {
    ((s & 0x00ffu16) as u8, (s >> 8) as u8)
}

/// Method to parse an encrypted payload.
fn parse_payload(
    payload: &Vec<u8>,
) -> Result<
    (
        FormatVersion,
        Vec<u8>,
        EncryptionVersion,
        Vec<u8>,
        EncryptionAuxData,
    ),
    String,
> {
    let mut pos: usize = 0;

    let _res = verify_payload_len(payload.len(), pos + MAGIC_BYTES_LEN)?;
    let magic_bytes = &payload[pos..pos + MAGIC_BYTES_LEN];
    if magic_bytes.to_lower_hex_string() != MAGIC_BYTES_STR {
        return Err(format!(
            "Wrong magic byte ({}), check the secret file!",
            magic_bytes.to_lower_hex_string()
        ));
    }
    pos += MAGIC_BYTES_LEN;

    let format_version_byte = get_next_payload_byte(payload, &mut pos)?;
    let format_version = FormatVersion::from_u8(format_version_byte)?;

    let nonsecret_len = get_next_payload_byte(payload, &mut pos)? as usize;

    let _res = verify_payload_len(payload.len(), pos + nonsecret_len)?;

    let nonsecret_data = payload[pos..pos + nonsecret_len].to_vec();
    pos += nonsecret_len;

    let encryption_version_byte = get_next_payload_byte(payload, &mut pos)?;
    let encryption_version = EncryptionVersion::from_u8(encryption_version_byte)?;

    let (encryption_aux_data, encrypted_secret_data) = match encryption_version {
        EncryptionVersion::V3ChaCha => {
            let (encryption_aux_data, encrypted_secret_data) =
                parse_encrypted_data_v3_chacha(payload, &mut pos)?;
            (encryption_aux_data, encrypted_secret_data)
        }
        EncryptionVersion::V2Scrypt => {
            let (encryption_aux_data, encrypted_secret_data) =
                parse_encrypted_data_v2_scrypt(payload, &mut pos)?;
            (encryption_aux_data, encrypted_secret_data)
        }
        // V1Xor is deprecated, support reading though
        EncryptionVersion::V1Xor => {
            let (encryption_aux_data, encrypted_secret_data) =
                parse_encrypted_data_v1_xor(payload, &mut pos)?;
            (encryption_aux_data, encrypted_secret_data)
        }
    };

    let _res = verify_payload_len(payload.len(), pos + CHECKSUM_LEN)?;
    let checksum_parsed = payload[pos..pos + CHECKSUM_LEN].to_vec();
    // Compute and check checksum
    let checksum_computed = checksum_of_payload(&payload[0..pos])?;
    if checksum_parsed != checksum_computed {
        return Err(format!("Checksum mismatch, check the secret file!"));
    }
    pos += CHECKSUM_LEN;

    // Final check, for too long
    if payload.len() != pos {
        return Err(format!(
            "File content is too long, {} vs {}",
            payload.len(),
            pos
        ));
    }

    Ok((
        format_version,
        nonsecret_data,
        encryption_version,
        encrypted_secret_data,
        encryption_aux_data,
    ))
}

fn parse_encrypted_data_v3_chacha(
    payload: &Vec<u8>,
    pos: &mut usize,
) -> Result<(EncryptionAuxData, Vec<u8>), String> {
    let rounds = get_next_payload_byte(payload, pos)?;

    let _res = verify_payload_len(payload.len(), *pos + chacha::SALT_LEN)?;
    let encryption_salt_temp = &payload[(*pos)..(*pos + crate::encrypt_xor::SALT_LEN)];
    *pos += chacha::SALT_LEN;
    debug_assert_eq!(encryption_salt_temp.len(), chacha::SALT_LEN);
    let encryption_salt = <chacha::EncryptionSalt>::try_from(encryption_salt_temp)
        .map_err(|e| format!("Internal salt conversion error {}", e))?;

    let _res = verify_payload_len(payload.len(), *pos + chacha::NONCE_LEN)?;
    let encryption_nonce_temp = &payload[(*pos)..(*pos + chacha::NONCE_LEN)];
    *pos += chacha::NONCE_LEN;
    debug_assert_eq!(encryption_nonce_temp.len(), chacha::NONCE_LEN);
    let encryption_nonce = <chacha::EncryptionNonce>::try_from(encryption_nonce_temp)
        .map_err(|e| format!("Internal nonce conversion error {}", e))?;

    let encryption_aux_data =
        EncryptionAuxData::V3ChaCha((rounds, encryption_salt, encryption_nonce));

    let encrypted_secret_data = parse_encrypted_data(payload, pos)?;

    Ok((encryption_aux_data, encrypted_secret_data))
}

fn parse_encrypted_data_v2_scrypt(
    payload: &Vec<u8>,
    pos: &mut usize,
) -> Result<(EncryptionAuxData, Vec<u8>), String> {
    let rounds = get_next_payload_byte(payload, pos)?;

    let _res = verify_payload_len(payload.len(), *pos + chacha::SALT_LEN)?;
    let encryption_salt_temp = &payload[(*pos)..(*pos + crate::encrypt_xor::SALT_LEN)];
    *pos += chacha::SALT_LEN;
    debug_assert_eq!(encryption_salt_temp.len(), chacha::SALT_LEN);
    let encryption_salt = <chacha::EncryptionSalt>::try_from(encryption_salt_temp)
        .map_err(|e| format!("Internal salt conversion error {}", e))?;

    let encryption_aux_data = EncryptionAuxData::V2Scrypt((rounds, encryption_salt));

    let encrypted_secret_data = parse_encrypted_data(payload, pos)?;

    Ok((encryption_aux_data, encrypted_secret_data))
}

fn parse_encrypted_data_v1_xor(
    payload: &Vec<u8>,
    pos: &mut usize,
) -> Result<(EncryptionAuxData, Vec<u8>), String> {
    let _res = verify_payload_len(payload.len(), *pos + xor::SALT_LEN)?;
    let encryption_salt_temp = &payload[(*pos)..(*pos + xor::SALT_LEN)];
    *pos += xor::SALT_LEN;
    debug_assert_eq!(encryption_salt_temp.len(), xor::SALT_LEN);
    let encryption_salt = <xor::EncryptionSalt>::try_from(encryption_salt_temp)
        .map_err(|e| format!("Internal salt conversion error {}", e))?;
    let encryption_aux_data = EncryptionAuxData::V1Xor(encryption_salt);

    let encrypted_secret_data = parse_encrypted_data(payload, pos)?;

    Ok((encryption_aux_data, encrypted_secret_data))
}

fn parse_encrypted_data(payload: &Vec<u8>, pos: &mut usize) -> Result<Vec<u8>, String> {
    let el_b1 = get_next_payload_byte(payload, pos)?;
    let el_b2 = get_next_payload_byte(payload, pos)?;
    let encrypted_len = u16_from_two_bytes(el_b1, el_b2) as usize;

    let _res = verify_payload_len(payload.len(), *pos + encrypted_len)?;
    let encrypted_secret_data = payload[(*pos)..(*pos + encrypted_len)].to_vec();
    *pos += encrypted_len;
    Ok(encrypted_secret_data)
}

/// Method to assemble the encrypted payload.
fn assemble_payload(
    format_version: FormatVersion,
    nonsecret_data: &Vec<u8>,
    encryption_version: EncryptionVersion,
    encrypted_secret_data: &Vec<u8>,
    encryption_aux_data: &EncryptionAuxData,
) -> Result<Vec<u8>, String> {
    let _res = encryption_aux_data.check_version(encryption_version)?;

    let mut o: Vec<u8> = Vec::with_capacity(256);
    o.extend(Vec::from_hex(MAGIC_BYTES_STR).unwrap());
    let nonsecret_len = nonsecret_data.len();
    if nonsecret_len > NONSECRET_DATA_MAX_LEN {
        return Err(format!(
            "Nonsecret data too long ({} vs {})",
            nonsecret_len, NONSECRET_DATA_MAX_LEN
        ));
    }

    o.push(format_version as u8);

    debug_assert!(nonsecret_len <= BYTE_MAX as usize);
    o.push(nonsecret_len as u8);

    o.extend(nonsecret_data);

    o.push(encryption_version as u8);

    match encryption_aux_data {
        EncryptionAuxData::V3ChaCha((rounds, salt, nonce)) => {
            let _res = assemble_encrypted_data_v3_chacha(
                &mut o,
                *rounds,
                salt,
                nonce,
                encrypted_secret_data,
            )?;
        }
        EncryptionAuxData::V2Scrypt((rounds, salt)) => {
            let _res =
                assemble_encrypted_data_v2_scrypt(&mut o, *rounds, salt, encrypted_secret_data)?;
        }
        // V1Xor is DEPRECATED
        EncryptionAuxData::V1Xor(_salt) => {
            return Err("V1Xor is DEPRECATED".to_owned());
        }
    }

    // compute and add checksum
    let checksum = checksum_of_payload(&o)?;
    o.extend(&checksum);

    Ok(o)
}

fn assemble_encrypted_data_v3_chacha(
    o: &mut Vec<u8>,
    rounds: u8,
    salt: &chacha::EncryptionSalt,
    nonce: &chacha::EncryptionNonce,
    encrypted_secret_data: &Vec<u8>,
) -> Result<(), String> {
    o.push(rounds);
    o.extend(salt);
    o.extend(nonce);
    assemble_encrypted_data(o, encrypted_secret_data)
}

fn assemble_encrypted_data_v2_scrypt(
    o: &mut Vec<u8>,
    rounds: u8,
    salt: &chacha::EncryptionSalt,
    encrypted_secret_data: &Vec<u8>,
) -> Result<(), String> {
    o.push(rounds);
    o.extend(salt);
    assemble_encrypted_data(o, encrypted_secret_data)
}

#[allow(dead_code)]
fn assemble_encrypted_data_v1_xor(
    o: &mut Vec<u8>,
    salt: &xor::EncryptionSalt,
    encrypted_secret_data: &Vec<u8>,
) -> Result<(), String> {
    o.extend(salt);
    assemble_encrypted_data(o, encrypted_secret_data)
}

fn assemble_encrypted_data(o: &mut Vec<u8>, encrypted_secret_data: &Vec<u8>) -> Result<(), String> {
    let encrypted_data_len = encrypted_secret_data.len();
    if encrypted_data_len < SECRET_DATA_MIN_LEN {
        return Err(format!(
            "Secret data too short ({} vs {})",
            encrypted_data_len, SECRET_DATA_MIN_LEN
        ));
    }
    if encrypted_data_len > SECRET_DATA_MAX_LEN {
        return Err(format!(
            "Secret data too long ({} vs {})",
            encrypted_data_len, SECRET_DATA_MAX_LEN
        ));
    }
    debug_assert!(
        encrypted_data_len >= SECRET_DATA_MIN_LEN && encrypted_data_len <= SECRET_DATA_MAX_LEN
    );
    let (el_b1, el_b2) = two_bytes_from_u16(encrypted_data_len as u16);
    o.push(el_b1);
    o.push(el_b2);

    o.extend(encrypted_secret_data);

    Ok(())
}

/// Descramble scrambled secret, in-place.
/// Caution: unencrypted secret is made available
fn descramble_secret(
    encrypted: &mut Vec<u8>,
    scrambling_key: &xor::EncryptionKey,
) -> Result<(), String> {
    let _res = xor::XorEncryptor::decrypt_with_key(encrypted, &scrambling_key)?;
    Ok(())
}

/// Scramble secret, in-place.
fn scramble_secret(
    unencrypted: &mut Vec<u8>,
    scrambling_key: &xor::EncryptionKey,
) -> Result<(), String> {
    debug_assert!(unencrypted.len() <= SECRET_DATA_MAX_LEN);
    let _res = xor::XorEncryptor::encrypt_with_key(unencrypted, &scrambling_key)?;
    Ok(())
}

/// Take encrypted secret, and scramble it.
/// Caution: unencrypted secret is available internally for a short time.
fn scramble_encrypted_secret_data(
    encryption_version: EncryptionVersion,
    encrypted: &mut Vec<u8>,
    decryption_password: &str,
    aux_data: &EncryptionAuxData,
    scrambling_key: &xor::EncryptionKey,
) -> Result<(), String> {
    debug_assert!(encrypted.len() <= SECRET_DATA_MAX_LEN);

    // first decrypt
    let _res = aux_data.check_version(encryption_version)?;
    match encryption_version {
        EncryptionVersion::V3ChaCha => {
            let _res = chacha::ChaChaEncryptor::decrypt(encrypted, decryption_password, aux_data)?;
        }
        EncryptionVersion::V2Scrypt => {
            let _res = scrypt::ScryptEncryptor::decrypt(encrypted, decryption_password, aux_data)?;
        }
        EncryptionVersion::V1Xor => {
            let _res = xor::XorEncryptor::decrypt(encrypted, decryption_password, aux_data)?;
        }
    }

    // then scramble
    let _res = scramble_secret(encrypted, scrambling_key)?;
    Ok(())
}

fn validate_password(encryption_password: &str) -> Result<(), String> {
    if encryption_password.len() < PASSWORD_MIN_LEN {
        return Err(format!(
            "Password is too short! ({} vs {})",
            encryption_password.len(),
            PASSWORD_MIN_LEN
        ));
    }
    Ok(())
}

/// Take scrambled secret, and encrypt it.
/// Caution: unencrypted secret is available internally for a short time.
fn encrypt_scrambled_secret_data(
    scrambled: &mut Vec<u8>,
    scrambling_key: &xor::EncryptionKey,
    encryption_version: EncryptionVersion,
    encryption_password: &str,
    aux_data: &EncryptionAuxData,
) -> Result<(), String> {
    let _res = validate_password(encryption_password)?;

    // first de-scramble
    let _res = descramble_secret(scrambled, scrambling_key)?;

    // then encrypt
    let _res = aux_data.check_version(encryption_version)?;
    match encryption_version {
        EncryptionVersion::V3ChaCha => {
            let _res = chacha::ChaChaEncryptor::encrypt(scrambled, encryption_password, aux_data)?;
        }
        EncryptionVersion::V2Scrypt => {
            let _res = scrypt::ScryptEncryptor::encrypt(scrambled, encryption_password, aux_data)?;
        }
        EncryptionVersion::V1Xor => {
            return Err("V1Xor is DEPRECATED".to_owned());
        }
    }
    Ok(())
}

fn checksum_of_payload(payload: &[u8]) -> Result<Vec<u8>, String> {
    let checksum_truncated = Sha256d::hash(payload).as_byte_array()[0..CHECKSUM_LEN].to_vec();
    debug_assert_eq!(checksum_truncated.len(), CHECKSUM_LEN);
    Ok(checksum_truncated)
}

#[cfg(test)]
mod tests {
    use super::{two_bytes_from_u16, u16_from_two_bytes};

    #[test]
    fn parse_u16() {
        assert_eq!(u16_from_two_bytes(0x07, 0x03), 0x0307);

        assert_eq!(two_bytes_from_u16(0x0307), (0x07, 0x03));

        assert_eq!(two_bytes_from_u16(u16_from_two_bytes(201, 202)), (201, 202));

        let (b1, b2) = two_bytes_from_u16(0x7348);
        assert_eq!(u16_from_two_bytes(b1, b2), 0x7348)
    }
}