ockam_vault_core/
types.rs1use cfg_if::cfg_if;
2use serde::{Deserialize, Serialize};
3use zeroize::Zeroize;
4
5pub const CURVE25519_SECRET_LENGTH: usize = 32;
7pub const CURVE25519_PUBLIC_LENGTH: usize = 32;
9pub const P256_SECRET_LENGTH: usize = 32;
11pub const P256_PUBLIC_LENGTH: usize = 65;
13pub const AES256_SECRET_LENGTH: usize = 32;
15pub const AES128_SECRET_LENGTH: usize = 16;
17
18cfg_if! {
19 if #[cfg(not(feature = "alloc"))] {
20 pub type SecretKeyVec = heapless::Vec<u8, 32>;
22 pub type PublicKeyVec = heapless::Vec<u8, 65>;
24 pub type SmallBuffer<T> = heapless::Vec<T, 4>;
26 pub type Buffer<T> = heapless::Vec<T, 512>;
28 pub type KeyId = heapless::String<64>;
29 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 pub type SecretKeyVec = Vec<u8>;
43 pub type PublicKeyVec = Vec<u8>;
45 pub type SmallBuffer<T> = Vec<T>;
47 pub type Buffer<T> = Vec<T>;
49 pub type KeyId = String;
51 pub type SignatureVec = Vec<u8>;
53 }
54}
55
56#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Zeroize)]
58#[zeroize(drop)]
59pub struct SecretKey(SecretKeyVec);
60
61impl SecretKey {
62 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#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Zeroize)]
76#[zeroize(drop)]
77pub struct PublicKey(PublicKeyVec);
78
79impl PublicKey {
80 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#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Zeroize)]
94#[zeroize(drop)]
95pub struct Signature(SignatureVec);
96
97impl Signature {
98 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#[derive(Serialize, Deserialize, Copy, Clone, Debug, Eq, PartialEq)]
112pub enum SecretType {
113 Buffer,
115 Aes,
117 Curve25519,
119 P256,
121 #[cfg(feature = "bls")]
123 Bls,
124}
125
126#[derive(Serialize, Deserialize, Copy, Clone, Debug, Eq, PartialEq)]
128pub enum SecretPersistence {
129 Ephemeral,
131 Persistent,
133}
134
135#[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 pub fn stype(&self) -> SecretType {
146 self.stype
147 }
148 pub fn persistence(&self) -> SecretPersistence {
150 self.persistence
151 }
152 pub fn length(&self) -> usize {
154 self.length
155 }
156}
157
158impl SecretAttributes {
159 pub fn new(stype: SecretType, persistence: SecretPersistence, length: usize) -> Self {
161 SecretAttributes {
162 stype,
163 persistence,
164 length,
165 }
166 }
167}