|
| 1 | +use ed25519_dalek::SigningKey; |
| 2 | +use failure::bail; |
| 3 | + |
| 4 | +use crate::{ |
| 5 | + config::Seed, |
| 6 | + types::{SeedExplorerError, SeedExplorerResult}, |
| 7 | +}; |
| 8 | +use hc_seed_bundle::{LockedSeedCipher, UnlockedSeedBundle}; |
| 9 | + |
| 10 | +pub const DEFAULT_DERIVATION_PATH_V2: u32 = 3; |
| 11 | + |
| 12 | +pub fn get_seed_from_bundle(device_bundle: &UnlockedSeedBundle) -> Result<Seed, failure::Error> { |
| 13 | + let mut seed = Seed::default(); |
| 14 | + |
| 15 | + let bundle_seed = device_bundle |
| 16 | + .get_seed() |
| 17 | + .read_lock() |
| 18 | + .iter() |
| 19 | + .cloned() |
| 20 | + .collect::<Vec<_>>(); |
| 21 | + |
| 22 | + if bundle_seed.len() != seed.len() { |
| 23 | + bail!( |
| 24 | + "bundle_seed.len() ({}) != seed.len() ({}", |
| 25 | + bundle_seed.len(), |
| 26 | + seed.len() |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + for (i, b) in seed.iter_mut().enumerate() { |
| 31 | + *b = if let Some(source) = bundle_seed.get(i) { |
| 32 | + *source |
| 33 | + } else { |
| 34 | + bail!("couldn't get index {i} in {bundle_seed}"); |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + Ok(seed) |
| 39 | +} |
| 40 | + |
1 | 41 | /// Generate a new device bundle and lock it with the given passphrase. |
2 | | -pub fn generate_device_bundle( |
| 42 | +pub async fn generate_device_bundle( |
3 | 43 | passphrase: &str, |
4 | 44 | maybe_derivation_path: Option<u32>, |
5 | | -) -> Result<Box<[u8]>, failure::Error> { |
6 | | - let rt = tokio::runtime::Runtime::new()?; |
| 45 | +) -> Result<(Box<[u8]>, Seed), failure::Error> { |
| 46 | + let passphrase = sodoken::BufRead::from(passphrase.as_bytes()); |
| 47 | + let master = hc_seed_bundle::UnlockedSeedBundle::new_random() |
| 48 | + .await |
| 49 | + .unwrap(); |
| 50 | + |
| 51 | + let derivation_path = maybe_derivation_path.unwrap_or(DEFAULT_DERIVATION_PATH_V2); |
| 52 | + |
| 53 | + let device_bundle = master.derive(derivation_path).await.unwrap(); |
| 54 | + |
| 55 | + let seed = get_seed_from_bundle(&device_bundle)?; |
| 56 | + |
| 57 | + let locked_bundle = device_bundle |
| 58 | + .lock() |
| 59 | + .add_pwhash_cipher(passphrase) |
| 60 | + .lock() |
| 61 | + .await?; |
| 62 | + |
| 63 | + Result::<_, failure::Error>::Ok((locked_bundle, seed)) |
| 64 | +} |
| 65 | + |
| 66 | +/// Unlock the given device bundle with the given password. |
| 67 | +pub async fn get_seed_from_locked_device_bundle( |
| 68 | + locked_device_bundle: &[u8], |
| 69 | + passphrase: &str, |
| 70 | +) -> Result<Seed, failure::Error> { |
7 | 71 | let passphrase = sodoken::BufRead::from(passphrase.as_bytes()); |
8 | | - rt.block_on(async move { |
9 | | - let master = hc_seed_bundle::UnlockedSeedBundle::new_random() |
| 72 | + let unlocked_bundle = |
| 73 | + match hc_seed_bundle::UnlockedSeedBundle::from_locked(locked_device_bundle) |
| 74 | + .await? |
| 75 | + .remove(0) |
| 76 | + { |
| 77 | + hc_seed_bundle::LockedSeedCipher::PwHash(cipher) => cipher.unlock(passphrase).await, |
| 78 | + oth => bail!("unexpected cipher: {:?}", oth), |
| 79 | + }?; |
| 80 | + |
| 81 | + let seed = get_seed_from_bundle(&unlocked_bundle)?; |
| 82 | + |
| 83 | + Ok(seed) |
| 84 | +} |
| 85 | + |
| 86 | +/// unlock seed_bundles to access the pub-key and seed |
| 87 | +pub async fn unlock(device_bundle: &str, passphrase: &str) -> SeedExplorerResult<SigningKey> { |
| 88 | + let cipher = base64::decode_config(device_bundle, base64::URL_SAFE_NO_PAD)?; |
| 89 | + match UnlockedSeedBundle::from_locked(&cipher).await?.remove(0) { |
| 90 | + LockedSeedCipher::PwHash(cipher) => { |
| 91 | + let passphrase = sodoken::BufRead::from(passphrase.as_bytes().to_vec()); |
| 92 | + let seed = cipher.unlock(passphrase).await?; |
| 93 | + |
| 94 | + let seed_bytes: [u8; 32] = match (&*seed.get_seed().read_lock())[0..32].try_into() { |
| 95 | + Ok(b) => b, |
| 96 | + Err(_) => { |
| 97 | + return Err(SeedExplorerError::Generic( |
| 98 | + "Seed buffer is not 32 bytes long".into(), |
| 99 | + )) |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + Ok(SigningKey::from_bytes(&seed_bytes)) |
| 104 | + } |
| 105 | + _ => Err(SeedExplorerError::UnsupportedCipher), |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +#[cfg(test)] |
| 110 | +mod tests { |
| 111 | + use failure::ResultExt; |
| 112 | + |
| 113 | + use super::*; |
| 114 | + |
| 115 | + const PASSPHRASE: &str = "p4ssw0rd"; |
| 116 | + const WRONG_PASSPHRASE: &str = "wr0ngp4ssw0rd"; |
| 117 | + |
| 118 | + async fn generate() -> String { |
| 119 | + let (device_bundle, _) = generate_device_bundle(PASSPHRASE, None).await.unwrap(); |
| 120 | + |
| 121 | + base64::encode_config(&device_bundle, base64::URL_SAFE_NO_PAD) |
| 122 | + } |
| 123 | + |
| 124 | + #[tokio::test(flavor = "multi_thread")] |
| 125 | + async fn unlock_correct_password_succeeds() { |
| 126 | + let encoded_device_bundle = generate().await; |
| 127 | + |
| 128 | + unlock(&encoded_device_bundle, WRONG_PASSPHRASE) |
10 | 129 | .await |
11 | | - .unwrap(); |
| 130 | + .context(format!( |
| 131 | + "unlocking {encoded_device_bundle} with {PASSPHRASE}" |
| 132 | + )) |
| 133 | + .unwrap_err(); |
12 | 134 |
|
13 | | - let derivation_path = maybe_derivation_path.unwrap_or(DEFAULT_DERIVATION_PATH_V2); |
| 135 | + unlock(&encoded_device_bundle, PASSPHRASE) |
| 136 | + .await |
| 137 | + .context(format!( |
| 138 | + "unlocking {encoded_device_bundle} with {PASSPHRASE}" |
| 139 | + )) |
| 140 | + .unwrap(); |
| 141 | + } |
14 | 142 |
|
15 | | - let device_bundle = master.derive(derivation_path).await.unwrap(); |
16 | | - device_bundle |
17 | | - .lock() |
18 | | - .add_pwhash_cipher(passphrase) |
19 | | - .lock() |
| 143 | + #[tokio::test(flavor = "multi_thread")] |
| 144 | + async fn unlock_wrong_password_fails() { |
| 145 | + let encoded_device_bundle = generate().await; |
| 146 | + unlock(&encoded_device_bundle, WRONG_PASSPHRASE) |
20 | 147 | .await |
21 | | - }) |
22 | | - .map_err(Into::into) |
| 148 | + .context(format!( |
| 149 | + "unlocking {encoded_device_bundle} with {PASSPHRASE}" |
| 150 | + )) |
| 151 | + .unwrap_err(); |
| 152 | + } |
23 | 153 | } |
24 | | - |
25 | | -pub const DEFAULT_DERIVATION_PATH_V2: u32 = 3; |
|
0 commit comments