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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
use std::fmt;
use crate::Error;
use crate::Result;
use crate::types::{Curve, HashAlgorithm};
use crate::crypto::mpis::{MPI, PublicKey};
/// A proprietary, protocol agnostic identifier for public keys.
///
/// This is defined and used by GnuPG.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Keygrip([u8; 20]);
impl fmt::Debug for Keygrip {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for b in self.0.iter() {
write!(f, "{:02X}", *b)?;
}
Ok(())
}
}
impl fmt::Display for Keygrip {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for b in self.0.iter() {
write!(f, "{:02X}", *b)?;
}
Ok(())
}
}
impl std::str::FromStr for Keygrip {
type Err = failure::Error;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
Self::from_hex(s)
}
}
impl Keygrip {
/// Parses a keygrip.
pub fn from_hex(hex: &str) -> Result<Self> {
let bytes = crate::fmt::from_hex(hex, true)?;
if bytes.len() != 20 {
return Err(Error::InvalidArgument(
format!("Expected 20 bytes, got {}", bytes.len())).into());
}
let mut digest = [0; 20];
&mut digest[..].copy_from_slice(&bytes[..]);
Ok(Keygrip(digest))
}
}
impl PublicKey {
/// Computes the keygrip.
pub fn keygrip(&self) -> Result<Keygrip> {
use crate::crypto::hash;
use std::io::Write;
use self::PublicKey::*;
let mut hash = HashAlgorithm::SHA1.context().unwrap();
fn hash_sexp_mpi(hash: &mut hash::Context, kind: char, prefix: &[u8],
mpi: &MPI)
{
write!(hash, "(1:{}{}:",
kind, mpi.value().len() + prefix.len()).unwrap();
hash.update(prefix);
hash.update(mpi.value());
write!(hash, ")").unwrap();
}
fn hash_ecc(hash: &mut hash::Context, curve: &Curve, q: &MPI)
{
for (i, name) in "pabgnhq".chars().enumerate() {
if i == 5 {
continue; // Skip cofactor.
}
let mut m =
if i == 6 { q.clone() } else { ecc_param(curve, i) };
// Opaque encoding?
if m.value()[0] == 0x40 {
// Drop the prefix!
let mut p = Vec::from(m.value());
p.remove(0);
m = p.into();
}
hash_sexp_mpi(hash, name, &[], &m);
}
}
match self {
// From libgcrypt/cipher/rsa.c:
//
// PKCS-15 says that for RSA only the modulus should be
// hashed - however, it is not clear whether this is meant
// to use the raw bytes (assuming this is an unsigned
// integer) or whether the DER required 0 should be
// prefixed. We hash the raw bytes.
&RSA { ref n, .. } => {
// Contrary to the comment reproduced above,
// overwhelming empirical evidence suggest that we
// need to prepend a 0.
hash.update(&[0]);
hash.update(n.value());
},
&DSA { ref p, ref q, ref g, ref y } => {
// Empirical evidence suggest that we need to prepend
// a 0 to some parameters.
hash_sexp_mpi(&mut hash, 'p', b"\x00", p);
hash_sexp_mpi(&mut hash, 'q', b"\x00", q);
hash_sexp_mpi(&mut hash, 'g', b"", g);
hash_sexp_mpi(&mut hash, 'y', b"", y);
},
&Elgamal { ref p, ref g, ref y } => {
hash_sexp_mpi(&mut hash, 'p', b"\x00", p);
hash_sexp_mpi(&mut hash, 'g', b"", g);
hash_sexp_mpi(&mut hash, 'y', b"", y);
},
&EdDSA { ref curve, ref q } => hash_ecc(&mut hash, curve, q),
&ECDSA { ref curve, ref q } => hash_ecc(&mut hash, curve, q),
&ECDH { ref curve, ref q, .. } => hash_ecc(&mut hash, curve, q),
&Unknown { .. } =>
return Err(Error::InvalidOperation(
"Keygrip not defined for this kind of public key".into())
.into()),
}
let mut digest = [0; 20];
hash.digest(&mut digest);
Ok(Keygrip(digest))
}
}
/// Returns curve parameters.
///
/// These parameters are a courtesy of libgcrypt.
fn ecc_param(curve: &Curve, i: usize) -> MPI {
use self::Curve::*;
assert!(i < 6);
let hex = match (curve, i) {
(NistP256, 0) => "0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff",
(NistP256, 1) => "0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc",
(NistP256, 2) => "0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b",
(NistP256, 4)
|