purecrypto 0.6.23

A pure-Rust cryptography toolkit with no foreign-code dependencies, from constant-time primitives up to keys, X.509 and TLS.
Documentation
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! Unified [`key`](crate::key) facade impls for the elliptic-curve keys.
//!
//! Each key implements [`PrivateKey`]/[`PublicKey`] directly for the operations
//! it supports; unsupported operations fall through to the facade defaults
//! ([`Error::Unsupported`](crate::key::Error)). Per-call parameters are read
//! through the consume-tracking [`SignParamsReader`](crate::key::SignParamsReader)
//! so that any parameter the algorithm does not honour is rejected loudly.

use alloc::boxed::Box;
use alloc::vec::Vec;

use crate::key::{
    Algorithm, CryptParams, Error, Hash, PrivateKey, PublicKey, Secret, SigEncoding, SignParams,
    downcast_peer,
};
use crate::rng::CryptoRngCore;

use super::ecdh::EcdhPrivateKey;
use super::ecdsa::{EcdsaPrivateKey, EcdsaPublicKey, Signature};
use super::ed448::{Ed448PrivateKey, Ed448PublicKey, Ed448Signature};
use super::ed25519::{Ed25519PrivateKey, Ed25519PublicKey, Ed25519Signature};
use super::sm2::{Sm2PrivateKey, Sm2PublicKey, Sm2Signature};
use super::x448::{X448PrivateKey, X448PublicKey};
use super::x25519::{X25519PrivateKey, X25519PublicKey};

/// Runs `$body` with `$d` aliased to the concrete digest named by `$h`
/// (a [`Hash`]). Bridges the runtime hash selector into the generic
/// `sign::<D>` / `verify::<D>` methods.
macro_rules! dispatch_hash {
    ($h:expr, |$d:ident| $body:block) => {
        match $h {
            Hash::Sha256 => {
                type $d = crate::hash::Sha256;
                $body
            }
            Hash::Sha384 => {
                type $d = crate::hash::Sha384;
                $body
            }
            Hash::Sha512 => {
                type $d = crate::hash::Sha512;
                $body
            }
            Hash::Sha1 => {
                type $d = crate::hash::Sha1;
                $body
            }
        }
    };
}

// ----------------------------------------------------------------------------
// Ed25519 — fixes its own hash, no params honoured
// ----------------------------------------------------------------------------

impl PrivateKey for Ed25519PrivateKey {
    fn algorithm(&self) -> Algorithm {
        Algorithm::Ed25519
    }
    fn public_key(&self) -> Result<Box<dyn PublicKey>, Error> {
        Ok(Box::new(self.public_key()))
    }
    fn sign(
        &self,
        msg: &[u8],
        params: &SignParams<'_>,
        _rng: &mut dyn CryptoRngCore,
    ) -> Result<Vec<u8>, Error> {
        params.reader().finish()?;
        Ok(self.sign(msg).to_bytes().to_vec())
    }
}

impl PublicKey for Ed25519PublicKey {
    fn algorithm(&self) -> Algorithm {
        Algorithm::Ed25519
    }
    fn as_any(&self) -> &dyn core::any::Any {
        self
    }
    fn verify(&self, msg: &[u8], sig: &[u8], params: &SignParams<'_>) -> Result<(), Error> {
        params.reader().finish()?;
        let bytes: [u8; 64] = sig.try_into().map_err(|_| Error::Signature)?;
        self.verify(msg, &Ed25519Signature::from_bytes(bytes))
            .map_err(|_| Error::Signature)
    }
}

// ----------------------------------------------------------------------------
// Ed448 — honours `context`
// ----------------------------------------------------------------------------

impl PrivateKey for Ed448PrivateKey {
    fn algorithm(&self) -> Algorithm {
        Algorithm::Ed448
    }
    fn public_key(&self) -> Result<Box<dyn PublicKey>, Error> {
        Ok(Box::new(self.public_key()))
    }
    fn sign(
        &self,
        msg: &[u8],
        params: &SignParams<'_>,
        _rng: &mut dyn CryptoRngCore,
    ) -> Result<Vec<u8>, Error> {
        let mut p = params.reader();
        let context = p.context();
        p.finish()?;
        let sig = if context.is_empty() {
            self.sign(msg)
        } else {
            self.sign_ctx(msg, context)
        };
        Ok(sig.to_bytes().to_vec())
    }
}

impl PublicKey for Ed448PublicKey {
    fn algorithm(&self) -> Algorithm {
        Algorithm::Ed448
    }
    fn as_any(&self) -> &dyn core::any::Any {
        self
    }
    fn verify(&self, msg: &[u8], sig: &[u8], params: &SignParams<'_>) -> Result<(), Error> {
        let mut p = params.reader();
        let context = p.context();
        p.finish()?;
        let bytes: [u8; 114] = sig.try_into().map_err(|_| Error::Signature)?;
        let signature = Ed448Signature::from_bytes(bytes);
        let res = if context.is_empty() {
            self.verify(msg, &signature)
        } else {
            self.verify_ctx(msg, &signature, context)
        };
        res.map_err(|_| Error::Signature)
    }
}

// ----------------------------------------------------------------------------
// ECDSA over P-256 (fixed) — honours hash, prehashed, sig_encoding
// ----------------------------------------------------------------------------

impl PrivateKey for EcdsaPrivateKey {
    fn algorithm(&self) -> Algorithm {
        Algorithm::P256
    }
    fn public_key(&self) -> Result<Box<dyn PublicKey>, Error> {
        Ok(Box::new(self.public_key()))
    }
    fn sign(
        &self,
        msg: &[u8],
        params: &SignParams<'_>,
        _rng: &mut dyn CryptoRngCore,
    ) -> Result<Vec<u8>, Error> {
        let mut p = params.reader();
        let hash = p.hash();
        let prehashed = p.prehashed();
        let enc = p.sig_encoding();
        p.finish()?;
        let sig = dispatch_hash!(hash, |D| {
            if prehashed {
                self.sign_prehash::<D>(msg)
            } else {
                self.sign::<D>(msg)
            }
        })
        .map_err(|_| Error::Signature)?;
        Ok(match enc {
            SigEncoding::Raw => sig.to_bytes().to_vec(),
            SigEncoding::Der => sig.to_der(),
        })
    }
}

impl PublicKey for EcdsaPublicKey {
    fn algorithm(&self) -> Algorithm {
        Algorithm::P256
    }
    fn as_any(&self) -> &dyn core::any::Any {
        self
    }
    fn verify(&self, msg: &[u8], sig: &[u8], params: &SignParams<'_>) -> Result<(), Error> {
        let mut p = params.reader();
        let hash = p.hash();
        let prehashed = p.prehashed();
        let enc = p.sig_encoding();
        p.finish()?;
        let signature = match enc {
            SigEncoding::Raw => {
                let bytes: [u8; 64] = sig.try_into().map_err(|_| Error::Signature)?;
                Signature::from_bytes(&bytes)
            }
            SigEncoding::Der => Signature::from_der(sig).map_err(|_| Error::Signature)?,
        };
        if prehashed {
            self.verify_prehash(msg, &signature)
        } else {
            dispatch_hash!(hash, |D| { self.verify::<D>(msg, &signature) })
        }
        .map_err(|_| Error::Signature)
    }
}

// ----------------------------------------------------------------------------
// ECDH over P-256 (fixed)
// ----------------------------------------------------------------------------

impl PrivateKey for EcdhPrivateKey {
    fn algorithm(&self) -> Algorithm {
        Algorithm::P256
    }
    fn public_key(&self) -> Result<Box<dyn PublicKey>, Error> {
        Ok(Box::new(self.public_key()))
    }
    fn agree(&self, peer: &dyn PublicKey) -> Result<Secret, Error> {
        let peer = downcast_peer::<EcdsaPublicKey>(peer, Algorithm::P256)?;
        let shared = self.diffie_hellman(peer).map_err(|_| Error::KeyAgreement)?;
        Ok(Secret::from_bytes(shared.to_vec()))
    }
}

// ----------------------------------------------------------------------------
// X25519
// ----------------------------------------------------------------------------

impl PrivateKey for X25519PrivateKey {
    fn algorithm(&self) -> Algorithm {
        Algorithm::X25519
    }
    fn public_key(&self) -> Result<Box<dyn PublicKey>, Error> {
        Ok(Box::new(X25519PublicKey::from_bytes(self.public_key())))
    }
    fn agree(&self, peer: &dyn PublicKey) -> Result<Secret, Error> {
        let peer = downcast_peer::<X25519PublicKey>(peer, Algorithm::X25519)?;
        let shared = self
            .diffie_hellman(peer.as_bytes())
            .map_err(|_| Error::KeyAgreement)?;
        Ok(Secret::from_bytes(shared.to_vec()))
    }
}

impl PublicKey for X25519PublicKey {
    fn algorithm(&self) -> Algorithm {
        Algorithm::X25519
    }
    fn as_any(&self) -> &dyn core::any::Any {
        self
    }
}

// ----------------------------------------------------------------------------
// X448
// ----------------------------------------------------------------------------

impl PrivateKey for X448PrivateKey {
    fn algorithm(&self) -> Algorithm {
        Algorithm::X448
    }
    fn public_key(&self) -> Result<Box<dyn PublicKey>, Error> {
        Ok(Box::new(X448PublicKey::from_bytes(self.public_key())))
    }
    fn agree(&self, peer: &dyn PublicKey) -> Result<Secret, Error> {
        let peer = downcast_peer::<X448PublicKey>(peer, Algorithm::X448)?;
        let shared = self
            .diffie_hellman(peer.as_bytes())
            .map_err(|_| Error::KeyAgreement)?;
        Ok(Secret::from_bytes(shared.to_vec()))
    }
}

impl PublicKey for X448PublicKey {
    fn algorithm(&self) -> Algorithm {
        Algorithm::X448
    }
    fn as_any(&self) -> &dyn core::any::Any {
        self
    }
}

// ----------------------------------------------------------------------------
// SM2 — honours `context` (signer ID) and `sig_encoding`
// ----------------------------------------------------------------------------

impl PrivateKey for Sm2PrivateKey {
    fn algorithm(&self) -> Algorithm {
        Algorithm::Sm2
    }
    fn public_key(&self) -> Result<Box<dyn PublicKey>, Error> {
        Ok(Box::new(self.public_key()))
    }
    fn sign(
        &self,
        msg: &[u8],
        params: &SignParams<'_>,
        rng: &mut dyn CryptoRngCore,
    ) -> Result<Vec<u8>, Error> {
        let mut p = params.reader();
        let id = sm2_id(p.context());
        let enc = p.sig_encoding();
        p.finish()?;
        let mut rng = rng;
        let sig = self.sign(msg, id, &mut rng).map_err(|_| Error::Signature)?;
        Ok(match enc {
            SigEncoding::Raw => sig.to_bytes(),
            SigEncoding::Der => sig.to_der(),
        })
    }
    fn decrypt(&self, ct: &[u8], params: &CryptParams<'_>) -> Result<Secret, Error> {
        params.reader().finish()?;
        let pt = self.decrypt(ct).map_err(|_| Error::Decryption)?;
        Ok(Secret::from_bytes(pt))
    }
}

impl PublicKey for Sm2PublicKey {
    fn algorithm(&self) -> Algorithm {
        Algorithm::Sm2
    }
    fn as_any(&self) -> &dyn core::any::Any {
        self
    }
    fn verify(&self, msg: &[u8], sig: &[u8], params: &SignParams<'_>) -> Result<(), Error> {
        let mut p = params.reader();
        let id = sm2_id(p.context());
        let enc = p.sig_encoding();
        p.finish()?;
        let signature = match enc {
            SigEncoding::Raw => Sm2Signature::from_bytes(sig).map_err(|_| Error::Signature)?,
            SigEncoding::Der => Sm2Signature::from_der(sig).map_err(|_| Error::Signature)?,
        };
        self.verify(msg, &signature, id)
            .map_err(|_| Error::Signature)
    }
    fn encrypt(
        &self,
        pt: &[u8],
        params: &CryptParams<'_>,
        rng: &mut dyn CryptoRngCore,
    ) -> Result<Vec<u8>, Error> {
        params.reader().finish()?;
        let mut rng = rng;
        self.encrypt(pt, &mut rng).map_err(|_| Error::Encryption)
    }
}

// ----------------------------------------------------------------------------
// Runtime-curve ("boxed") ECDSA / ECDH
//
// `curve_alg` maps the four supported curves to `Algorithm`; an unsupported
// curve (e.g. Brainpool or the SM2 curve carried as ECDSA) returns `None`, so
// the capability ops reject it up front while `algorithm()` falls back to P256.
// ----------------------------------------------------------------------------

use super::boxed::{
    BoxedEcdhPrivateKey, BoxedEcdsaPrivateKey, BoxedEcdsaPublicKey, BoxedEcdsaSignature,
};
use super::curves::CurveId;
use crate::bignum::BoxedUint;

fn curve_alg(curve: CurveId) -> Option<Algorithm> {
    match curve {
        CurveId::P256 => Some(Algorithm::P256),
        CurveId::P384 => Some(Algorithm::P384),
        CurveId::P521 => Some(Algorithm::P521),
        CurveId::Secp256k1 => Some(Algorithm::Secp256k1),
        _ => None,
    }
}

impl PrivateKey for BoxedEcdsaPrivateKey {
    fn algorithm(&self) -> Algorithm {
        curve_alg(self.curve()).unwrap_or(Algorithm::P256)
    }
    fn public_key(&self) -> Result<Box<dyn PublicKey>, Error> {
        Ok(Box::new(self.public_key()))
    }
    fn sign(
        &self,
        msg: &[u8],
        params: &SignParams<'_>,
        _rng: &mut dyn CryptoRngCore,
    ) -> Result<Vec<u8>, Error> {
        let curve = self.curve();
        curve_alg(curve).ok_or(Error::InvalidParams)?;
        let mut p = params.reader();
        let hash = p.hash();
        let prehashed = p.prehashed();
        let enc = p.sig_encoding();
        p.finish()?;
        let sig = dispatch_hash!(hash, |D| {
            if prehashed {
                self.sign_prehash::<D>(msg)
            } else {
                self.sign::<D>(msg)
            }
        })
        .map_err(|_| Error::Signature)?;
        Ok(match enc {
            SigEncoding::Raw => sig.to_bytes(curve),
            SigEncoding::Der => sig.to_der(curve),
        })
    }
}

impl PublicKey for BoxedEcdsaPublicKey {
    fn algorithm(&self) -> Algorithm {
        curve_alg(self.curve()).unwrap_or(Algorithm::P256)
    }
    fn as_any(&self) -> &dyn core::any::Any {
        self
    }
    fn verify(&self, msg: &[u8], sig: &[u8], params: &SignParams<'_>) -> Result<(), Error> {
        let curve = self.curve();
        curve_alg(curve).ok_or(Error::InvalidParams)?;
        let mut p = params.reader();
        let hash = p.hash();
        let prehashed = p.prehashed();
        let enc = p.sig_encoding();
        p.finish()?;
        let signature = match enc {
            // No `from_bytes` on BoxedEcdsaSignature: split raw `r||s` halves of
            // `field_len()` -> BoxedUint -> from_components.
            SigEncoding::Raw => {
                let flen = curve.field_len();
                if sig.len() != 2 * flen {
                    return Err(Error::Signature);
                }
                let r = BoxedUint::from_be_bytes(&sig[..flen]);
                let s = BoxedUint::from_be_bytes(&sig[flen..]);
                BoxedEcdsaSignature::from_components(r, s)
            }
            SigEncoding::Der => BoxedEcdsaSignature::from_der(sig).map_err(|_| Error::Signature)?,
        };
        if prehashed {
            self.verify_prehash(msg, &signature)
        } else {
            dispatch_hash!(hash, |D| { self.verify::<D>(msg, &signature) })
        }
        .map_err(|_| Error::Signature)
    }
}

impl PrivateKey for BoxedEcdhPrivateKey {
    fn algorithm(&self) -> Algorithm {
        curve_alg(self.public_key().curve()).unwrap_or(Algorithm::P256)
    }
    fn public_key(&self) -> Result<Box<dyn PublicKey>, Error> {
        Ok(Box::new(self.public_key()))
    }
    fn agree(&self, peer: &dyn PublicKey) -> Result<Secret, Error> {
        let alg = curve_alg(self.public_key().curve()).ok_or(Error::InvalidParams)?;
        let peer = downcast_peer::<BoxedEcdsaPublicKey>(peer, alg)?;
        let shared = self.diffie_hellman(peer).map_err(|_| Error::KeyAgreement)?;
        Ok(Secret::from_bytes(shared))
    }
}

// ----------------------------------------------------------------------------
// helpers
// ----------------------------------------------------------------------------

/// The SM2 signer ID: the supplied context, or [`super::sm2::DEFAULT_ID`] when
/// empty.
fn sm2_id(context: &[u8]) -> &[u8] {
    if context.is_empty() {
        super::sm2::DEFAULT_ID
    } else {
        context
    }
}