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
use prost::Message;
use tink_core::{utils::wrap_err, TinkError};
pub const ED25519_SIGNER_KEY_VERSION: u32 = 0;
pub const ED25519_SIGNER_TYPE_URL: &str =
"type.googleapis.com/google.crypto.tink.Ed25519PrivateKey";
#[derive(Default)]
pub(crate) struct Ed25519SignerKeyManager {}
impl tink_core::registry::KeyManager for Ed25519SignerKeyManager {
fn primitive(&self, serialized_key: &[u8]) -> Result<tink_core::Primitive, TinkError> {
if serialized_key.is_empty() {
return Err("Ed25519SignerKeyManager: invalid key".into());
}
let key = tink_proto::Ed25519PrivateKey::decode(serialized_key)
.map_err(|e| wrap_err("Ed25519SignerKeyManager: invalid key", e))?;
validate_key(&key)?;
match crate::subtle::Ed25519Signer::new(&key.key_value) {
Ok(p) => Ok(tink_core::Primitive::Signer(Box::new(p))),
Err(e) => Err(wrap_err("Ed25519SignerKeyManager: invalid key", e)),
}
}
fn new_key(&self, _serialized_key_format: &[u8]) -> Result<Vec<u8>, TinkError> {
let mut csprng = rand::rngs::OsRng {};
let keypair = ed25519_dalek::Keypair::generate(&mut csprng);
let public_proto = tink_proto::Ed25519PublicKey {
version: ED25519_SIGNER_KEY_VERSION,
key_value: keypair.public.as_bytes().to_vec(),
};
let key = tink_proto::Ed25519PrivateKey {
version: ED25519_SIGNER_KEY_VERSION,
public_key: Some(public_proto),
key_value: keypair.secret.as_bytes().to_vec(),
};
let mut sk = Vec::new();
key.encode(&mut sk)
.map_err(|e| wrap_err("Ed25519SignerKeyManager: failed to encode new key", e))?;
Ok(sk)
}
fn type_url(&self) -> &'static str {
ED25519_SIGNER_TYPE_URL
}
fn key_material_type(&self) -> tink_proto::key_data::KeyMaterialType {
tink_proto::key_data::KeyMaterialType::AsymmetricPrivate
}
fn supports_private_keys(&self) -> bool {
true
}
fn public_key_data(
&self,
serialized_priv_key: &[u8],
) -> Result<tink_proto::KeyData, TinkError> {
let key = tink_proto::Ed25519PrivateKey::decode(serialized_priv_key)
.map_err(|e| wrap_err("Ed25519SignerKeyManager: invalid key", e))?;
let mut serialized_pub_key = Vec::new();
key.public_key
.ok_or_else(|| TinkError::new("Ed25519SignerKeyManager: invalid key"))?
.encode(&mut serialized_pub_key)
.map_err(|e| wrap_err("Ed25519SignerKeyManager: invalid key", e))?;
Ok(tink_proto::KeyData {
type_url: crate::ED25519_VERIFIER_TYPE_URL.to_string(),
value: serialized_pub_key,
key_material_type: tink_proto::key_data::KeyMaterialType::AsymmetricPublic as i32,
})
}
}
fn validate_key(key: &tink_proto::Ed25519PrivateKey) -> Result<(), TinkError> {
tink_core::keyset::validate_key_version(key.version, ED25519_SIGNER_KEY_VERSION)
.map_err(|e| wrap_err("Ed25519SignerKeyManager", e))?;
if key.key_value.len() != ed25519_dalek::SECRET_KEY_LENGTH {
return Err(format!(
"Ed25519SignerKeyManager: invalid key length: {}",
key.key_value.len()
)
.into());
}
let pub_key = key
.public_key
.as_ref()
.ok_or_else(|| TinkError::new("Ed25519SignerKeyManager: no public key"))?;
crate::validate_ed25519_public_key(pub_key)
}