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
use crate::AgentId;

use super::{
    custodian::{CustodialAgentKey, KeyMask, UserAuthKey},
    AgentIdentity,
};
use ed25519_dalek::Keypair;
use hmac::{Hmac, Mac, NewMac};
use rand::rngs::OsRng;
use scrypt::{scrypt, ScryptParams};
use serde::{Deserialize, Serialize};
use sha2::Sha512Trunc256;
use zeroize::Zeroize;

// dalek::Keypair already derives Zeroize
#[derive(Debug, Serialize, Deserialize)]
pub struct AgentKey {
    pub keypair: Keypair,
    pub email: Option<String>,
}

// impl Eq for AgentKey{
//     fn assert_receiver_is_total_eq(&self) {}
// }
impl PartialEq for AgentKey {
    fn eq(&self, other: &Self) -> bool {
        self.keypair.secret.as_bytes().eq(other.keypair.secret.as_bytes())
            && self.keypair.public.as_bytes().eq(other.keypair.public.as_bytes())
    }
}

impl AgentKey {
    pub fn create(email: Option<String>) -> AgentKey {
        let mut csprng = OsRng {};
        let keypair = Keypair::generate(&mut csprng);

        // TODO 2 - Reconcile differences between the above
        // and the old code removed from minbase core (below)
        // let mut csprng: OsRng = OsRng::new().unwrap();
        // let keypair: Keypair = Keypair::generate::<Sha512>(&mut csprng);

        AgentKey { keypair, email }
    }
    pub fn hmac(&self) -> [u8; 32] {
        let mut mac = Hmac::<Sha512Trunc256>::new_varkey(b"agentkey").unwrap();
        mac.update(self.keypair.secret.as_bytes());
        mac.update(self.keypair.public.as_bytes());
        let result = mac.finalize();

        result.into_bytes().into()
    }
    pub fn id(&self) -> AgentId {
        let pubkey = self.keypair.public.as_bytes().clone();
        AgentId { pubkey }
    }
    pub fn identity(&self) -> AgentIdentity {
        let pubkey = self.keypair.public.as_bytes().clone();
        AgentIdentity {
            pubkey,
            email: self.email.clone(),
        }
    }
    pub fn pubkey(&self) -> [u8; 32] {
        self.keypair.public.as_bytes().clone()
    }
    pub fn keymask(&self, passkey: &PassKey) -> KeyMask {
        // use std::convert::TryInto;

        let mut mask = [0u8; 32];

        self.keypair
            .secret
            .as_bytes()
            .iter()
            .zip(passkey.c.iter())
            .enumerate()
            .for_each(|(i, (bk, bc))| mask[i] = bk ^ bc);

        KeyMask { mask }
    }
    pub fn custodial_key(&self, passkey: PassKey) -> CustodialAgentKey {
        // Consume the PassKey to discourage the implementer from storing it

        CustodialAgentKey {
            pubkey: self.keypair.public.as_bytes().clone(),
            mask: self.keymask(&passkey),
            check: self.hmac(),
            email: self.email.clone(),
        }
    }
    pub fn from_custodial_key(custodial_key: CustodialAgentKey, passkey: PassKey) -> Result<Self, crate::Error> {
        // Consume the PassKey to discourage the implementer from storing it

        let mut secret = [0u8; 32];

        custodial_key
            .mask
            .as_bytes()
            .iter()
            .zip(passkey.c.iter())
            .enumerate()
            .for_each(|(i, (m, p))| secret[i] = m ^ p);

        let mut mac = Hmac::<Sha512Trunc256>::new_varkey(b"agentkey").unwrap();
        mac.update(&secret);
        mac.update(&custodial_key.pubkey);

        mac.verify(&custodial_key.check)?;

        Ok(Self {
            keypair: Keypair {
                secret: ed25519_dalek::SecretKey::from_bytes(&secret)?,
                public: ed25519_dalek::PublicKey::from_bytes(&custodial_key.pubkey)?,
            },
            email: custodial_key.email.clone(),
        })
    }
}

// DO NOT ALLOW SERIALIZATION
#[derive(Zeroize)]
#[zeroize(drop)]
pub struct PassKey {
    c: [u8; 32],
}

impl PassKey {
    pub fn new(passphrase: &str) -> PassKey {
        let salt = b"mindbase passkey";
        let params = ScryptParams::recommended();
        let mut dk = [0u8; 32];
        scrypt(passphrase.as_bytes(), salt, &params, &mut dk).expect("32 bytes always satisfy output length requirements");

        PassKey { c: dk }
    }
    // Use this to authenticate with the server
    pub fn auth(&self) -> UserAuthKey {
        let salt = b"mindbase authkey";
        let params = ScryptParams::recommended();

        let mut auth = [0u8; 32];
        scrypt(&self.c, salt, &params, &mut auth).expect("32 bytes always satisfy output length requirements");

        UserAuthKey { auth }
    }
}