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
//! `objects` provides multiple data objects used to generate `KeySecure` data format
use rst_common::standard::serde::{self, Deserialize, Serialize};

use crate::keysecure::types::constants::*;
use crate::passphrase::kdf_params::KdfParams as PassphraseKDFParams;

/// `KeySecureCryptoParams` store a single field for the `nonce`
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(crate = "self::serde")]
pub struct KeySecureCryptoParams {
    pub nonce: String,
}

/// `KeySecureCrypto` will be used to store the encrypted data including for
/// it's supported components
///
/// This data will consists of:
/// - cipher
/// - cipher_text
/// - cipher_params
/// - kdf
/// - kdf_params
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(crate = "self::serde")]
pub struct KeySecureCrypto {
    pub cipher: String,

    #[serde(rename = "cipherText")]
    pub cipher_text: String,

    #[serde(rename = "cipherParams")]
    pub cipher_params: KeySecureCryptoParams,

    pub kdf: String,

    #[serde(rename = "kdfParams")]
    pub kdf_params: KdfParams,
}

impl KeySecureCrypto {
    pub fn new(nonce: String, ciphertext: String, kdf_params: KdfParams) -> Self {
        let params = KeySecureCryptoParams { nonce };
        Self {
            cipher: CRYPTO_CIPHER_ALGO.to_string(),
            cipher_text: ciphertext,
            cipher_params: params,
            kdf: KDF_ALGO.to_string(),
            kdf_params,
        }
    }
}

/// `KdfParams` used to store passphrase kdf params and it's salt
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(crate = "self::serde")]
pub struct KdfParams {
    pub params: PassphraseKDFParams,
    pub salt: String,
}

impl KdfParams {
    pub fn new(params: PassphraseKDFParams, salt: String) -> Self {
        Self { params, salt }
    }
}