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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use boringssl::{ed25519_keypair, ed25519_keypair_from_seed, ed25519_sign, ed25519_verify};
use public::{PrivateKey, PublicKey, Signature};
use util::Sealed;
use Error;
const ED25519_PUBLIC_KEY_LEN: usize = ::boringssl::ED25519_PUBLIC_KEY_LEN as usize;
const ED25519_PRIVATE_KEY_LEN: usize = ::boringssl::ED25519_PRIVATE_KEY_LEN as usize;
const ED25519_SIGNATURE_LEN: usize = ::boringssl::ED25519_SIGNATURE_LEN as usize;
const ED25519_PUBLIC_KEY_OFFSET: usize = ED25519_PRIVATE_KEY_LEN - ED25519_PUBLIC_KEY_LEN;
pub struct Ed25519PubKey {
key: [u8; ED25519_PUBLIC_KEY_LEN],
}
impl Ed25519PubKey {
#[must_use]
pub fn from_bytes(bytes: [u8; ED25519_PUBLIC_KEY_LEN]) -> Ed25519PubKey {
Ed25519PubKey { key: bytes }
}
#[must_use]
pub fn bytes(&self) -> &[u8; ED25519_PUBLIC_KEY_LEN] {
&self.key
}
}
impl_debug!(Ed25519PubKey, "Ed25519PubKey");
impl Sealed for Ed25519PubKey {}
impl PublicKey for Ed25519PubKey {
type Private = Ed25519PrivKey;
}
pub struct Ed25519PrivKey {
key: [u8; ED25519_PRIVATE_KEY_LEN],
}
impl_debug!(Ed25519PrivKey, "Ed25519PrivKey");
impl Ed25519PrivKey {
#[must_use]
pub fn generate() -> Ed25519PrivKey {
Ed25519PrivKey { key: ed25519_keypair() }
}
#[must_use]
pub fn from_key_pair_bytes(private: [u8; 32], public: &Ed25519PubKey) -> Ed25519PrivKey {
let mut key = [0u8; ED25519_PRIVATE_KEY_LEN];
(&mut key[..32]).copy_from_slice(&private);
(&mut key[ED25519_PUBLIC_KEY_OFFSET..]).copy_from_slice(&public.key);
Ed25519PrivKey { key }
}
#[must_use]
pub fn from_private_key_bytes(private: [u8; 32]) -> Ed25519PrivKey {
let (_, key) = ed25519_keypair_from_seed(&private);
Ed25519PrivKey { key }
}
#[must_use]
pub fn bytes(&self) -> &[u8; ED25519_PRIVATE_KEY_LEN] {
&self.key
}
}
impl Sealed for Ed25519PrivKey {}
impl PrivateKey for Ed25519PrivKey {
type Public = Ed25519PubKey;
fn public(&self) -> Ed25519PubKey {
let mut public = [0u8; ED25519_PUBLIC_KEY_LEN];
(&mut public[..]).copy_from_slice(&self.key[ED25519_PUBLIC_KEY_OFFSET..]);
Ed25519PubKey { key: public }
}
}
#[must_use]
pub struct Ed25519Signature {
sig: [u8; ED25519_SIGNATURE_LEN],
}
impl_debug!(Ed25519Signature, "Ed25519Signature");
impl Ed25519Signature {
#[must_use]
pub fn from_bytes(bytes: [u8; ED25519_SIGNATURE_LEN]) -> Ed25519Signature {
Ed25519Signature { sig: bytes }
}
#[must_use]
pub fn bytes(&self) -> &[u8; ED25519_SIGNATURE_LEN] {
&self.sig
}
#[must_use]
pub fn sign_ed25519(key: &Ed25519PrivKey, message: &[u8]) -> Ed25519Signature {
Ed25519Signature {
sig: ed25519_sign(message, &key.key).unwrap(),
}
}
}
impl Sealed for Ed25519Signature {}
impl Signature for Ed25519Signature {
type PrivateKey = Ed25519PrivKey;
fn sign(key: &Ed25519PrivKey, message: &[u8]) -> Result<Ed25519Signature, Error> {
Ok(Ed25519Signature::sign_ed25519(key, message))
}
fn is_valid(&self, key: &Ed25519PubKey, message: &[u8]) -> bool {
ed25519_verify(message, &self.sig, &key.key)
}
}
#[cfg(test)]
mod tests {
use super::*;
use public::testutil::test_signature_smoke;
#[test]
fn test_priv_key_constructors() {
let key = Ed25519PrivKey::generate();
let mut private = [0u8; 32];
(&mut private[..]).copy_from_slice(&key.key[..32]);
let key2 = Ed25519PrivKey::from_private_key_bytes(private);
assert_eq!(&key.key[..], &key2.key[..]);
let mut private = [0u8; 32];
let mut public = [0u8; 32];
let bytes = *key.bytes();
(&mut private[..]).copy_from_slice(&bytes[..32]);
(&mut public[..]).copy_from_slice(&bytes[32..]);
let key2 = Ed25519PrivKey::from_key_pair_bytes(private, &Ed25519PubKey::from_bytes(public));
assert_eq!(&key.key[..], &key2.key[..]);
}
#[test]
fn test_smoke() {
let key = Ed25519PrivKey::generate();
let from_bytes = |bytes: &[u8]| {
let mut sig = [0u8; ED25519_SIGNATURE_LEN];
let len = ::std::cmp::min(sig.len(), bytes.len());
(&mut sig[..len]).copy_from_slice(&bytes[..len]);
Ed25519Signature::from_bytes(sig)
};
fn to_bytes(sig: &Ed25519Signature) -> &[u8] {
&sig.bytes()[..]
}
test_signature_smoke(&key, from_bytes, to_bytes);
}
}