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
use generic_array::typenum::Unsigned;
use p256::elliptic_curve;
use prost::Message;
use tink_core::{utils::wrap_err, TinkError};
use tink_proto::EllipticCurveType;
pub const ECDSA_SIGNER_KEY_VERSION: u32 = 0;
pub const ECDSA_SIGNER_TYPE_URL: &str = "type.googleapis.com/google.crypto.tink.EcdsaPrivateKey";
#[derive(Default)]
pub(crate) struct EcdsaSignerKeyManager {}
pub const ECDSA_UNCOMPRESSED_POINT_PREFIX: u8 = 0x04;
impl tink_core::registry::KeyManager for EcdsaSignerKeyManager {
fn primitive(&self, serialized_key: &[u8]) -> Result<tink_core::Primitive, TinkError> {
if serialized_key.is_empty() {
return Err("EcdsaSignerKeyManager: invalid key".into());
}
let key = tink_proto::EcdsaPrivateKey::decode(serialized_key)
.map_err(|e| wrap_err("EcdsaSignerKeyManager: invalid key", e))?;
let params = validate_key(&key)?;
let (hash, curve, encoding) = crate::get_ecdsa_param_ids(¶ms);
match crate::subtle::EcdsaSigner::new(hash, curve, encoding, &key.key_value) {
Ok(p) => Ok(tink_core::Primitive::Signer(Box::new(p))),
Err(e) => Err(wrap_err("EcdsaSignerKeyManager: invalid key", e)),
}
}
fn new_key(&self, serialized_key_format: &[u8]) -> Result<Vec<u8>, TinkError> {
if serialized_key_format.is_empty() {
return Err("EcdsaSignerKeyManager: invalid key format".into());
}
let key_format = tink_proto::EcdsaKeyFormat::decode(serialized_key_format)
.map_err(|e| wrap_err("EcdsaSignerKeyManager: invalid key", e))?;
let (params, curve) = validate_key_format(&key_format)?;
let mut csprng = rand::rngs::OsRng {};
let (secret_key_data, pub_x_data, pub_y_data) = match curve {
EllipticCurveType::NistP256 => {
let secret_key = p256::ecdsa::SigningKey::random(&mut csprng);
let public_key = p256::ecdsa::VerifyingKey::from(&secret_key);
let public_key_point = public_key.to_encoded_point( false);
let public_key_data = public_key_point.as_bytes();
let point_len = <p256::NistP256 as elliptic_curve::Curve>::FieldSize::to_usize();
if public_key_data.len() != 2 * point_len + 1
|| public_key_data[0] != ECDSA_UNCOMPRESSED_POINT_PREFIX
{
return Err("EcdsaSignerKeyManager: unexpected public key data format".into());
}
(
secret_key.to_bytes().to_vec(),
public_key_data[1..point_len + 1].to_vec(),
public_key_data[point_len + 1..].to_vec(),
)
}
_ => {
return Err(format!("EcdsaSignerKeyManager: unsupported curve {:?}", curve).into())
}
};
let pub_key = tink_proto::EcdsaPublicKey {
version: ECDSA_SIGNER_KEY_VERSION,
params: Some(params),
x: pub_x_data,
y: pub_y_data,
};
let priv_key = tink_proto::EcdsaPrivateKey {
version: ECDSA_SIGNER_KEY_VERSION,
public_key: Some(pub_key),
key_value: secret_key_data,
};
let mut sk = Vec::new();
priv_key
.encode(&mut sk)
.map_err(|e| wrap_err("EcdsaSignerKeyManager: failed to encode new key", e))?;
Ok(sk)
}
fn type_url(&self) -> &'static str {
ECDSA_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 priv_key = tink_proto::EcdsaPrivateKey::decode(serialized_priv_key)
.map_err(|e| wrap_err("EcdsaSignerKeyManager: invalid private key", e))?;
let mut serialized_pub_key = Vec::new();
priv_key
.public_key
.ok_or_else(|| TinkError::new("EcdsaSignerKeyManager: no public key"))?
.encode(&mut serialized_pub_key)
.map_err(|e| wrap_err("EcdsaSignerKeyManager: invalid public key", e))?;
Ok(tink_proto::KeyData {
type_url: crate::ECDSA_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::EcdsaPrivateKey) -> Result<tink_proto::EcdsaParams, TinkError> {
tink_core::keyset::validate_key_version(key.version, ECDSA_SIGNER_KEY_VERSION)
.map_err(|e| wrap_err("EcdsaSignerKeyManager", e))?;
let pub_key = key
.public_key
.as_ref()
.ok_or_else(|| TinkError::new("EcdsaSignerKeyManager: no public key"))?;
let params = crate::validate_ecdsa_public_key(pub_key)
.map_err(|e| wrap_err("EcdsaSignerKeyManager", e))?;
let (hash, curve, encoding) = crate::get_ecdsa_param_ids(¶ms);
crate::subtle::EcdsaVerifier::new(hash, curve, encoding, &pub_key.x, &pub_key.y)
.map_err(|e| wrap_err("EcdsaVerifierKeyManager: invalid key", e))?;
crate::subtle::validate_ecdsa_params(hash, curve, encoding)?;
Ok(params)
}
fn validate_key_format(
key_format: &tink_proto::EcdsaKeyFormat,
) -> Result<(tink_proto::EcdsaParams, tink_proto::EllipticCurveType), TinkError> {
let params = key_format
.params
.as_ref()
.ok_or_else(|| TinkError::new("no public key parameters"))?;
let (hash, curve, encoding) = crate::get_ecdsa_param_ids(¶ms);
crate::subtle::validate_ecdsa_params(hash, curve, encoding)?;
Ok((params.clone(), curve))
}