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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use cfg_if::cfg_if;
use serde::{Deserialize, Serialize};
use zeroize::Zeroize;
pub const CURVE25519_SECRET_LENGTH: usize = 32;
pub const CURVE25519_PUBLIC_LENGTH: usize = 32;
pub const P256_SECRET_LENGTH: usize = 32;
pub const P256_PUBLIC_LENGTH: usize = 65;
pub const AES256_SECRET_LENGTH: usize = 32;
pub const AES128_SECRET_LENGTH: usize = 16;
cfg_if! {
if #[cfg(not(feature = "alloc"))] {
pub type SecretKeyVec = heapless::Vec<u8, 32>;
pub type PublicKeyVec = heapless::Vec<u8, 65>;
pub type SmallBuffer<T> = heapless::Vec<T, 4>;
pub type Buffer<T> = heapless::Vec<T, 512>;
pub type KeyId = heapless::String<64>;
pub type SignatureVec = heapless::Vec<u8, 112>;
impl From<&str> for KeyId {
fn from(s: &str) -> Self {
heapless::String::from(s)
}
}
}
else {
use alloc::vec::Vec;
use alloc::string::String;
pub type SecretKeyVec = Vec<u8>;
pub type PublicKeyVec = Vec<u8>;
pub type SmallBuffer<T> = Vec<T>;
pub type Buffer<T> = Vec<T>;
pub type KeyId = String;
pub type SignatureVec = Vec<u8>;
}
}
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Zeroize)]
#[zeroize(drop)]
pub struct SecretKey(SecretKeyVec);
impl SecretKey {
pub fn new(data: SecretKeyVec) -> Self {
Self(data)
}
}
impl AsRef<[u8]> for SecretKey {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Zeroize)]
#[zeroize(drop)]
pub struct PublicKey(PublicKeyVec);
impl PublicKey {
pub fn new(data: PublicKeyVec) -> Self {
Self(data)
}
}
impl AsRef<[u8]> for PublicKey {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Zeroize)]
#[zeroize(drop)]
pub struct Signature(SignatureVec);
impl Signature {
pub fn new(data: SignatureVec) -> Self {
Self(data)
}
}
impl AsRef<[u8]> for Signature {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
#[derive(Serialize, Deserialize, Copy, Clone, Debug, Eq, PartialEq)]
pub enum SecretType {
Buffer,
Aes,
Curve25519,
P256,
#[cfg(feature = "bls")]
Bls,
}
#[derive(Serialize, Deserialize, Copy, Clone, Debug, Eq, PartialEq)]
pub enum SecretPersistence {
Ephemeral,
Persistent,
}
#[derive(Serialize, Deserialize, Copy, Clone, Debug, Eq, PartialEq)]
pub struct SecretAttributes {
stype: SecretType,
persistence: SecretPersistence,
length: usize,
}
impl SecretAttributes {
pub fn stype(&self) -> SecretType {
self.stype
}
pub fn persistence(&self) -> SecretPersistence {
self.persistence
}
pub fn length(&self) -> usize {
self.length
}
}
impl SecretAttributes {
pub fn new(stype: SecretType, persistence: SecretPersistence, length: usize) -> Self {
SecretAttributes {
stype,
persistence,
length,
}
}
}