ockam_vault_core/
types.rs

1use cfg_if::cfg_if;
2use serde::{Deserialize, Serialize};
3use zeroize::Zeroize;
4
5/// Curve25519 private key length
6pub const CURVE25519_SECRET_LENGTH: usize = 32;
7/// Curve25519 public key length
8pub const CURVE25519_PUBLIC_LENGTH: usize = 32;
9/// P256 private key length
10pub const P256_SECRET_LENGTH: usize = 32;
11/// P256 public key length
12pub const P256_PUBLIC_LENGTH: usize = 65;
13/// AES256 private key length
14pub const AES256_SECRET_LENGTH: usize = 32;
15/// AES128 private key length
16pub const AES128_SECRET_LENGTH: usize = 16;
17
18cfg_if! {
19    if #[cfg(not(feature = "alloc"))] {
20        /// Secret Key Vector
21        pub type SecretKeyVec = heapless::Vec<u8, 32>;
22        /// Public Key Vector
23        pub type PublicKeyVec = heapless::Vec<u8, 65>;
24        /// Bufer for small vectors (e.g. array of attributes). Max size - 4
25        pub type SmallBuffer<T> = heapless::Vec<T, 4>;
26        /// Buffer for large binaries (e.g. encrypted data). Max size - 512
27        pub type Buffer<T> = heapless::Vec<T, 512>;
28        pub type KeyId = heapless::String<64>;
29        /// Signature Vector. Max size - 112
30        pub type SignatureVec = heapless::Vec<u8, 112>;
31
32        impl From<&str> for KeyId {
33            fn from(s: &str) -> Self {
34                heapless::String::from(s)
35            }
36        }
37    }
38    else {
39        use alloc::vec::Vec;
40        use alloc::string::String;
41        /// Secret Key Vector
42        pub type SecretKeyVec = Vec<u8>;
43        /// Public Key Vector
44        pub type PublicKeyVec = Vec<u8>;
45        /// Buffer for small vectors (e.g. array of attributes)
46        pub type SmallBuffer<T> = Vec<T>;
47        /// Buffer for large binaries (e.g. encrypted data)
48        pub type Buffer<T> = Vec<T>;
49        /// ID of a Key
50        pub type KeyId = String;
51        ///Signature Vector
52        pub type SignatureVec = Vec<u8>;
53    }
54}
55
56/// Binary representation of a Secret.
57#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Zeroize)]
58#[zeroize(drop)]
59pub struct SecretKey(SecretKeyVec);
60
61impl SecretKey {
62    /// Create a new secret key
63    pub fn new(data: SecretKeyVec) -> Self {
64        Self(data)
65    }
66}
67
68impl AsRef<[u8]> for SecretKey {
69    fn as_ref(&self) -> &[u8] {
70        &self.0
71    }
72}
73
74/// A public key
75#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Zeroize)]
76#[zeroize(drop)]
77pub struct PublicKey(PublicKeyVec);
78
79impl PublicKey {
80    /// Create a new public key
81    pub fn new(data: PublicKeyVec) -> Self {
82        Self(data)
83    }
84}
85
86impl AsRef<[u8]> for PublicKey {
87    fn as_ref(&self) -> &[u8] {
88        &self.0
89    }
90}
91
92///Binary representation of Signature
93#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Zeroize)]
94#[zeroize(drop)]
95pub struct Signature(SignatureVec);
96
97impl Signature {
98    /// Create a new signature
99    pub fn new(data: SignatureVec) -> Self {
100        Self(data)
101    }
102}
103
104impl AsRef<[u8]> for Signature {
105    fn as_ref(&self) -> &[u8] {
106        &self.0
107    }
108}
109
110/// All possible [`SecretType`]s
111#[derive(Serialize, Deserialize, Copy, Clone, Debug, Eq, PartialEq)]
112pub enum SecretType {
113    /// Secret buffer
114    Buffer,
115    /// AES key
116    Aes,
117    /// Curve 22519 key
118    Curve25519,
119    /// P256 key
120    P256,
121    /// BLS key
122    #[cfg(feature = "bls")]
123    Bls,
124}
125
126/// Possible [`SecretKey`]'s persistence
127#[derive(Serialize, Deserialize, Copy, Clone, Debug, Eq, PartialEq)]
128pub enum SecretPersistence {
129    /// An ephemeral/temporary secret
130    Ephemeral,
131    /// A persistent secret
132    Persistent,
133}
134
135/// Attributes for a specific vault [`SecretKey`]
136#[derive(Serialize, Deserialize, Copy, Clone, Debug, Eq, PartialEq)]
137pub struct SecretAttributes {
138    stype: SecretType,
139    persistence: SecretPersistence,
140    length: usize,
141}
142
143impl SecretAttributes {
144    /// Return the type of secret
145    pub fn stype(&self) -> SecretType {
146        self.stype
147    }
148    /// Return the persistence of the secret
149    pub fn persistence(&self) -> SecretPersistence {
150        self.persistence
151    }
152    /// Return the length of the secret
153    pub fn length(&self) -> usize {
154        self.length
155    }
156}
157
158impl SecretAttributes {
159    /// Create a new secret attribute
160    pub fn new(stype: SecretType, persistence: SecretPersistence, length: usize) -> Self {
161        SecretAttributes {
162            stype,
163            persistence,
164            length,
165        }
166    }
167}