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
#![warn(clippy::just_underscores_and_digits)]
/// Elliptic curve backend
use tiny_ed448_goldilocks::curve::{extended_edwards::ExtendedPoint, field::scalar::Scalar};

/// Module for sha3 primitives.
pub mod sha3 {
    pub mod aux_functions;
    pub mod keccakf;
    pub mod sponge;
}

pub mod aes {
    pub mod aes_constants;
    pub mod aes_functions;
}

/// Module for encrypt, decrypt, and sign functions.
pub mod ops;

#[derive(Debug)]
/// An object containing the necessary fields for Schnorr signatures.
pub struct Signature {
    /// keyed hash of signed message
    pub h: Vec<u8>,
    /// public nonce
    pub z: Scalar,
}

impl Clone for Signature {
    fn clone(&self) -> Signature {
        Signature {
            h: self.h.clone(),
            z: self.z,
        }
    }
}

#[derive(Debug)]
/// An object containing the fields necessary to represent an asymmetric keypair.
pub struct KeyPair {
    /// String indicating the owner of the key, can be arbitrary
    pub owner: String,
    /// Public encryption key
    pub pub_key: ExtendedPoint,
    /// value representing secret scalar, None if KeyType is PUBLIC
    pub priv_key: Vec<u8>,
    /// Date key was generated
    pub date_created: String,
}

impl Message {
    pub fn new(data: Vec<u8>) -> Message {
        Message {
            msg: Box::new(data),
            d: None,
            sym_nonce: None,
            asym_nonce: None,
            digest: None,
            op_result: None,
            sig: None,
        }
    }
}

#[derive(Debug)]
/// Message type for which cryptographic traits are defined.
pub struct Message {
    pub msg: Box<Vec<u8>>,
    pub d: Option<u64>,
    pub sym_nonce: Option<Vec<u8>>,
    pub asym_nonce: Option<ExtendedPoint>,
    pub digest: Option<Vec<u8>>,
    pub op_result: Option<bool>,
    pub sig: Option<Signature>,
}

pub trait AesEncryptable {
    fn aes_encrypt_cbc(&mut self, key: &[u8]);
    fn aes_decrypt_cbc(&mut self, key: &[u8]);
}

pub trait Hashable {
    fn compute_hash_sha3(&mut self, d: u64);
    fn compute_tagged_hash(&mut self, pw: &mut Vec<u8>, s: &str, d: u64);
}

pub trait PwEncryptable {
    fn pw_encrypt_sha3(&mut self, pw: &[u8], d: u64);
    fn pw_decrypt_sha3(&mut self, pw: &[u8]);
}

pub trait KeyEncryptable {
    fn key_encrypt(&mut self, pub_key: &ExtendedPoint, d: u64);
    fn key_decrypt(&mut self, pw: &[u8]);
}

pub trait Signable {
    fn sign(&mut self, key: &KeyPair, d: u64);
    fn verify(&mut self, pub_key: &ExtendedPoint);
}