1use crate::common;
2use crate::common::check_tag_len;
3use crate::{
4 ActiveKeyExchange, AeadAlgorithm, AeadCipher, BlockCipherAlgorithm, CbcAlgorithm, CbcCipher,
5 CryptoAlgorithm, CryptoError, HEADER_MASK_LEN, HEADER_MASK_SAMPLE_LEN, HashAlgorithm,
6 HeaderMask, HeaderMaskAlgorithm, HmacAlgorithm, KeyExchangeAlgorithm, Mac, PublicKey,
7 PublicKeyEncoding, RTCCrypto, RTCCryptoProvider, RTCRandom, SecretVec, SignatureScheme,
8 SigningKey, StreamCipher, StreamCipherAlgorithm, constant_time_eq,
9};
10use ::hmac::Mac as RustCryptoMac;
11use ring::aead;
12use ring::agreement;
13use ring::digest;
14use ring::hmac;
15use ring::rand::SystemRandom;
16use ring::signature::{self, KeyPair};
17use sha1::Sha1;
18use std::sync::Arc;
19
20#[derive(Default)]
22pub struct RingProvider {
23 crypto: RingCrypto,
24 random: RingRandom,
25}
26
27impl RingProvider {
28 #[must_use]
30 pub const fn new() -> Self {
31 Self {
32 crypto: RingCrypto,
33 random: RingRandom,
34 }
35 }
36}
37
38impl RTCCryptoProvider for RingProvider {
39 fn name(&self) -> &'static str {
40 "ring"
41 }
42
43 fn crypto(&self) -> &dyn RTCCrypto {
44 &self.crypto
45 }
46
47 fn random(&self) -> &dyn RTCRandom {
48 &self.random
49 }
50}
51
52#[derive(Default)]
55pub struct RingCrypto;
56
57#[derive(Default)]
59pub struct RingRandom;
60
61impl RTCRandom for RingRandom {
62 fn fill(&self, output: &mut [u8]) -> Result<(), CryptoError> {
63 common::fill_random(output)
64 }
65}
66
67impl RTCCrypto for RingCrypto {
68 fn supports(&self, algorithm: CryptoAlgorithm) -> bool {
69 matches!(
70 algorithm,
71 CryptoAlgorithm::Hash(
72 HashAlgorithm::Md5
73 | HashAlgorithm::Sha256
74 | HashAlgorithm::Sha1
75 | HashAlgorithm::Sha224
76 | HashAlgorithm::Sha384
77 | HashAlgorithm::Sha512
78 ) | CryptoAlgorithm::Hmac(
79 HmacAlgorithm::Sha1
80 | HmacAlgorithm::Sha256
81 | HmacAlgorithm::Sha384
82 | HmacAlgorithm::Sha512
83 ) | CryptoAlgorithm::HeaderMask(
84 HeaderMaskAlgorithm::Aes128
85 | HeaderMaskAlgorithm::Aes256
86 | HeaderMaskAlgorithm::ChaCha20
87 ) | CryptoAlgorithm::Aead(
88 AeadAlgorithm::Aes128Gcm
89 | AeadAlgorithm::Aes256Gcm
90 | AeadAlgorithm::Aes128Ccm
91 | AeadAlgorithm::Aes128Ccm8
92 | AeadAlgorithm::ChaCha20Poly1305,
93 ) | CryptoAlgorithm::StreamCipher(
94 StreamCipherAlgorithm::Aes128Ctr
95 | StreamCipherAlgorithm::Aes256Ctr
96 | StreamCipherAlgorithm::Aes192Ctr,
97 ) | CryptoAlgorithm::BlockCipher(
98 BlockCipherAlgorithm::Aes128
99 | BlockCipherAlgorithm::Aes256
100 | BlockCipherAlgorithm::Aes192,
101 ) | CryptoAlgorithm::Cbc(CbcAlgorithm::Aes256Cbc)
102 | CryptoAlgorithm::KeyExchange(
103 KeyExchangeAlgorithm::P256
104 | KeyExchangeAlgorithm::P384
105 | KeyExchangeAlgorithm::X25519,
106 )
107 | CryptoAlgorithm::Signature(
108 SignatureScheme::Ed25519
109 | SignatureScheme::EcdsaP256Sha256
110 | SignatureScheme::EcdsaP384Sha256
111 | SignatureScheme::EcdsaP384Sha384
112 | SignatureScheme::RsaPkcs1Sha1
113 | SignatureScheme::RsaPkcs1Sha256
114 | SignatureScheme::RsaPkcs1Sha384
115 | SignatureScheme::RsaPkcs1Sha512
116 | SignatureScheme::RsaPssSha256
117 | SignatureScheme::RsaPssSha384
118 | SignatureScheme::RsaPssSha512,
119 )
120 | CryptoAlgorithm::SigningKeyGeneration(
121 SignatureScheme::Ed25519 | SignatureScheme::EcdsaP256Sha256,
122 )
123 | CryptoAlgorithm::SigningKeyImport(
124 SignatureScheme::Ed25519
125 | SignatureScheme::EcdsaP256Sha256
126 | SignatureScheme::RsaPkcs1Sha256,
127 )
128 )
129 }
130
131 fn hash(&self, algorithm: HashAlgorithm, data: &[u8]) -> Result<Vec<u8>, CryptoError> {
132 match algorithm {
133 HashAlgorithm::Md5 => Ok(common::md5(data)),
134 HashAlgorithm::Sha256 => Ok(digest::digest(&digest::SHA256, data).as_ref().to_vec()),
135 HashAlgorithm::Sha1 => Ok(digest::digest(&digest::SHA1_FOR_LEGACY_USE_ONLY, data)
136 .as_ref()
137 .to_vec()),
138 HashAlgorithm::Sha224 => Ok(common::sha224(data)),
140 HashAlgorithm::Sha384 => Ok(digest::digest(&digest::SHA384, data).as_ref().to_vec()),
141 HashAlgorithm::Sha512 => Ok(digest::digest(&digest::SHA512, data).as_ref().to_vec()),
142 }
143 }
144
145 fn new_hmac(&self, algorithm: HmacAlgorithm, key: &[u8]) -> Result<Box<dyn Mac>, CryptoError> {
146 match algorithm {
147 HmacAlgorithm::Sha1 => Ok(Box::new(RustCryptoHmacSha1::new(key))),
151 HmacAlgorithm::Sha256 | HmacAlgorithm::Sha384 | HmacAlgorithm::Sha512 => {
152 Ok(Box::new(RingHmac {
153 key: hmac::Key::new(hmac_algorithm(algorithm), key),
154 output_len: algorithm.output_len(),
155 }))
156 }
157 }
158 }
159
160 fn block_encrypt(
161 &self,
162 algorithm: BlockCipherAlgorithm,
163 key: &[u8],
164 block: &mut [u8],
165 ) -> Result<(), CryptoError> {
166 common::block_encrypt(algorithm, key, block)
167 }
168
169 fn new_stream_cipher(
170 &self,
171 algorithm: StreamCipherAlgorithm,
172 key: &[u8],
173 ) -> Result<Box<dyn StreamCipher>, CryptoError> {
174 common::new_stream_cipher(algorithm, key)
175 }
176
177 fn new_aead(
178 &self,
179 algorithm: AeadAlgorithm,
180 key: &[u8],
181 ) -> Result<Box<dyn AeadCipher>, CryptoError> {
182 match algorithm {
183 AeadAlgorithm::Aes128Ccm | AeadAlgorithm::Aes128Ccm8 => common::new_ccm(algorithm, key),
184 AeadAlgorithm::Aes128Gcm => RingAead::create(&aead::AES_128_GCM, key),
185 AeadAlgorithm::Aes256Gcm => RingAead::create(&aead::AES_256_GCM, key),
186 AeadAlgorithm::ChaCha20Poly1305 => RingAead::create(&aead::CHACHA20_POLY1305, key),
187 }
188 }
189
190 fn new_header_mask(
191 &self,
192 algorithm: HeaderMaskAlgorithm,
193 key: &[u8],
194 ) -> Result<Box<dyn HeaderMask>, CryptoError> {
195 common::check_key_len(algorithm.key_len(), key.len())?;
196 let backend_algorithm = match algorithm {
197 HeaderMaskAlgorithm::Aes128 => &aead::quic::AES_128,
198 HeaderMaskAlgorithm::Aes256 => &aead::quic::AES_256,
199 HeaderMaskAlgorithm::ChaCha20 => &aead::quic::CHACHA20,
200 };
201 let key = aead::quic::HeaderProtectionKey::new(backend_algorithm, key)
202 .map_err(|_| CryptoError::InvalidPrivateKey)?;
203 Ok(Box::new(RingHeaderMask { key }))
204 }
205
206 fn new_cbc(
207 &self,
208 algorithm: CbcAlgorithm,
209 key: &[u8],
210 ) -> Result<Box<dyn CbcCipher>, CryptoError> {
211 common::new_cbc(algorithm, key)
212 }
213
214 fn start_key_exchange(
215 &self,
216 algorithm: KeyExchangeAlgorithm,
217 ) -> Result<Box<dyn ActiveKeyExchange>, CryptoError> {
218 RingKeyExchange::start(algorithm)
219 }
220
221 fn generate_signing_key(
222 &self,
223 scheme: SignatureScheme,
224 ) -> Result<Arc<dyn SigningKey>, CryptoError> {
225 RingSigningKey::generate(scheme)
226 }
227
228 fn import_signing_key(
229 &self,
230 scheme: SignatureScheme,
231 pkcs8_der: &[u8],
232 ) -> Result<Arc<dyn SigningKey>, CryptoError> {
233 RingSigningKey::import(scheme, pkcs8_der)
234 }
235
236 fn verify_signature(
237 &self,
238 scheme: SignatureScheme,
239 public_key: PublicKey<'_>,
240 message: &[u8],
241 signature: &[u8],
242 ) -> Result<(), CryptoError> {
243 verify_public_key_encoding(scheme, public_key.encoding)?;
244 signature::UnparsedPublicKey::new(verification_algorithm(scheme), public_key.bytes)
245 .verify(message, signature)
246 .map_err(|_| CryptoError::InvalidSignature)
247 }
248}
249
250struct RingHmac {
255 key: hmac::Key,
256 output_len: usize,
257}
258
259impl Mac for RingHmac {
260 fn output_len(&self) -> usize {
261 self.output_len
262 }
263
264 fn sign(&mut self, input: &[&[u8]], output: &mut [u8]) -> Result<(), CryptoError> {
265 common::check_tag_len(self.output_len, output.len())?;
266 let mut context = hmac::Context::with_key(&self.key);
267 for part in input {
268 context.update(part);
269 }
270 output.copy_from_slice(context.sign().as_ref());
271 Ok(())
272 }
273
274 fn verify(&mut self, input: &[&[u8]], expected: &[u8]) -> Result<(), CryptoError> {
275 common::check_tag_len(self.output_len, expected.len())?;
276 let mut actual = vec![0; self.output_len];
277 self.sign(input, &mut actual)?;
278 if constant_time_eq(&actual, expected) {
279 Ok(())
280 } else {
281 Err(CryptoError::AuthenticationFailed)
282 }
283 }
284}
285
286fn hmac_algorithm(algorithm: HmacAlgorithm) -> hmac::Algorithm {
287 match algorithm {
288 HmacAlgorithm::Sha1 => hmac::HMAC_SHA1_FOR_LEGACY_USE_ONLY,
289 HmacAlgorithm::Sha256 => hmac::HMAC_SHA256,
290 HmacAlgorithm::Sha512 => hmac::HMAC_SHA512,
291 HmacAlgorithm::Sha384 => hmac::HMAC_SHA384,
292 }
293}
294
295struct RingHeaderMask {
299 key: aead::quic::HeaderProtectionKey,
300}
301
302impl HeaderMask for RingHeaderMask {
303 fn mask(
304 &mut self,
305 sample: &[u8; HEADER_MASK_SAMPLE_LEN],
306 ) -> Result<[u8; HEADER_MASK_LEN], CryptoError> {
307 self.key
308 .new_mask(sample)
309 .map_err(|_| CryptoError::Provider("header mask generation failed".into()))
310 }
311}
312
313struct RingAead {
314 key: aead::LessSafeKey,
315}
316
317impl RingAead {
318 fn create(
319 algorithm: &'static aead::Algorithm,
320 key: &[u8],
321 ) -> Result<Box<dyn AeadCipher>, CryptoError> {
322 common::check_key_len(algorithm.key_len(), key.len())?;
323 let key = aead::UnboundKey::new(algorithm, key)
324 .map(aead::LessSafeKey::new)
325 .map_err(|_| CryptoError::InvalidPrivateKey)?;
326 Ok(Box::new(Self { key }))
327 }
328}
329
330impl AeadCipher for RingAead {
331 fn tag_len(&self) -> usize {
332 16
333 }
334
335 fn seal_in_place(
336 &mut self,
337 nonce: &[u8],
338 aad: &[u8],
339 plaintext_and_ciphertext: &mut [u8],
340 tag_out: &mut [u8],
341 ) -> Result<(), CryptoError> {
342 common::check_nonce_len(12, nonce.len())?;
343 common::check_tag_len(self.tag_len(), tag_out.len())?;
344 let nonce = aead::Nonce::try_assume_unique_for_key(nonce).map_err(|_| {
345 CryptoError::InvalidNonceLength {
346 expected: 12,
347 actual: nonce.len(),
348 }
349 })?;
350 let tag = self
351 .key
352 .seal_in_place_separate_tag(nonce, aead::Aad::from(aad), plaintext_and_ciphertext)
353 .map_err(|_| CryptoError::AuthenticationFailed)?;
354 tag_out.copy_from_slice(tag.as_ref());
355 Ok(())
356 }
357
358 fn open_in_place(
359 &mut self,
360 nonce: &[u8],
361 aad: &[u8],
362 ciphertext_and_plaintext: &mut [u8],
363 tag: &[u8],
364 ) -> Result<(), CryptoError> {
365 common::check_nonce_len(12, nonce.len())?;
366 common::check_tag_len(self.tag_len(), tag.len())?;
367 let nonce = aead::Nonce::try_assume_unique_for_key(nonce).map_err(|_| {
368 CryptoError::InvalidNonceLength {
369 expected: 12,
370 actual: nonce.len(),
371 }
372 })?;
373 let tag = aead::Tag::try_from(tag).map_err(|_| CryptoError::InvalidTagLength {
374 expected: self.tag_len(),
375 actual: tag.len(),
376 })?;
377 self.key
378 .open_in_place_separate_tag(
379 nonce,
380 aead::Aad::from(aad),
381 tag,
382 ciphertext_and_plaintext,
383 0..,
384 )
385 .map(|_| ())
386 .map_err(|_| CryptoError::AuthenticationFailed)
387 }
388}
389
390struct RingKeyExchange {
391 algorithm: KeyExchangeAlgorithm,
392 backend_algorithm: &'static agreement::Algorithm,
393 private_key: agreement::EphemeralPrivateKey,
394 public_key: Vec<u8>,
395}
396
397impl RingKeyExchange {
398 fn start(algorithm: KeyExchangeAlgorithm) -> Result<Box<dyn ActiveKeyExchange>, CryptoError> {
399 let backend_algorithm = agreement_algorithm(algorithm)?;
400 let private_key =
401 agreement::EphemeralPrivateKey::generate(backend_algorithm, &SystemRandom::new())
402 .map_err(|_| CryptoError::RandomnessFailed)?;
403 let public_key = private_key
404 .compute_public_key()
405 .map_err(|_| CryptoError::Provider("key exchange public-key generation failed".into()))?
406 .as_ref()
407 .to_vec();
408 Ok(Box::new(Self {
409 algorithm,
410 backend_algorithm,
411 private_key,
412 public_key,
413 }))
414 }
415}
416
417impl ActiveKeyExchange for RingKeyExchange {
418 fn algorithm(&self) -> KeyExchangeAlgorithm {
419 self.algorithm
420 }
421
422 fn public_key(&self) -> &[u8] {
423 &self.public_key
424 }
425
426 fn complete(self: Box<Self>, peer_public_key: &[u8]) -> Result<SecretVec, CryptoError> {
427 let peer = agreement::UnparsedPublicKey::new(self.backend_algorithm, peer_public_key);
428 agreement::agree_ephemeral(self.private_key, &peer, |secret| {
429 SecretVec::new(secret.to_vec())
430 })
431 .map_err(|_| CryptoError::InvalidPublicKey)
432 }
433}
434
435enum RingSigningKeyKind {
436 Ed25519(signature::Ed25519KeyPair),
437 EcdsaP256(signature::EcdsaKeyPair),
438 Rsa(signature::RsaKeyPair),
439}
440
441struct RingSigningKey {
442 scheme: SignatureScheme,
443 kind: RingSigningKeyKind,
444 public_key: Vec<u8>,
445 public_key_encoding: PublicKeyEncoding,
446 pkcs8_der: SecretVec,
447}
448
449impl RingSigningKey {
450 fn generate(scheme: SignatureScheme) -> Result<Arc<dyn SigningKey>, CryptoError> {
451 let rng = SystemRandom::new();
452 let pkcs8 = match scheme {
453 SignatureScheme::Ed25519 => signature::Ed25519KeyPair::generate_pkcs8(&rng),
454 SignatureScheme::EcdsaP256Sha256 => signature::EcdsaKeyPair::generate_pkcs8(
455 &signature::ECDSA_P256_SHA256_ASN1_SIGNING,
456 &rng,
457 ),
458 _ => {
459 return Err(CryptoError::UnsupportedAlgorithm(
460 CryptoAlgorithm::SigningKeyGeneration(scheme),
461 ));
462 }
463 }
464 .map_err(|_| CryptoError::RandomnessFailed)?;
465 Self::import(scheme, pkcs8.as_ref())
466 }
467
468 fn import(
469 scheme: SignatureScheme,
470 pkcs8_der: &[u8],
471 ) -> Result<Arc<dyn SigningKey>, CryptoError> {
472 let rng = SystemRandom::new();
473 let (kind, public_key, public_key_encoding) = match scheme {
474 SignatureScheme::Ed25519 => {
475 let key = signature::Ed25519KeyPair::from_pkcs8_maybe_unchecked(pkcs8_der)
476 .map_err(|_| CryptoError::InvalidPrivateKey)?;
477 let public = key.public_key().as_ref().to_vec();
478 (
479 RingSigningKeyKind::Ed25519(key),
480 public,
481 PublicKeyEncoding::Ed25519Raw,
482 )
483 }
484 SignatureScheme::EcdsaP256Sha256 => {
485 let key = signature::EcdsaKeyPair::from_pkcs8(
486 &signature::ECDSA_P256_SHA256_ASN1_SIGNING,
487 pkcs8_der,
488 &rng,
489 )
490 .map_err(|_| CryptoError::InvalidPrivateKey)?;
491 let public = key.public_key().as_ref().to_vec();
492 (
493 RingSigningKeyKind::EcdsaP256(key),
494 public,
495 PublicKeyEncoding::EcUncompressedPoint,
496 )
497 }
498 SignatureScheme::RsaPkcs1Sha256 => {
499 let key = signature::RsaKeyPair::from_pkcs8(pkcs8_der)
500 .map_err(|_| CryptoError::InvalidPrivateKey)?;
501 let public = key.public().as_ref().to_vec();
502 (
503 RingSigningKeyKind::Rsa(key),
504 public,
505 PublicKeyEncoding::RsaPkcs1Der,
506 )
507 }
508 _ => {
509 return Err(CryptoError::UnsupportedAlgorithm(
510 CryptoAlgorithm::SigningKeyImport(scheme),
511 ));
512 }
513 };
514 Ok(Arc::new(Self {
515 scheme,
516 kind,
517 public_key,
518 public_key_encoding,
519 pkcs8_der: SecretVec::new(pkcs8_der.to_vec()),
520 }))
521 }
522}
523
524impl SigningKey for RingSigningKey {
525 fn supports(&self, scheme: SignatureScheme) -> bool {
526 self.scheme == scheme
529 || (matches!(self.kind, RingSigningKeyKind::Rsa(_))
530 && matches!(
531 scheme,
532 SignatureScheme::RsaPssSha256
533 | SignatureScheme::RsaPssSha384
534 | SignatureScheme::RsaPssSha512
535 ))
536 }
537
538 fn public_key(&self) -> PublicKey<'_> {
539 PublicKey {
540 encoding: self.public_key_encoding,
541 bytes: &self.public_key,
542 }
543 }
544
545 fn sign(&self, scheme: SignatureScheme, message: &[u8]) -> Result<Vec<u8>, CryptoError> {
546 if !self.supports(scheme) {
547 return Err(CryptoError::UnsupportedAlgorithm(
548 CryptoAlgorithm::Signature(scheme),
549 ));
550 }
551 match &self.kind {
552 RingSigningKeyKind::Ed25519(key) => Ok(key.sign(message).as_ref().to_vec()),
553 RingSigningKeyKind::EcdsaP256(key) => key
554 .sign(&SystemRandom::new(), message)
555 .map(|signature| signature.as_ref().to_vec())
556 .map_err(|_| CryptoError::Provider("signature generation failed".into())),
557 RingSigningKeyKind::Rsa(key) => {
558 let encoding: &'static dyn signature::RsaEncoding = match scheme {
559 SignatureScheme::RsaPssSha256 => &signature::RSA_PSS_SHA256,
560 SignatureScheme::RsaPssSha384 => &signature::RSA_PSS_SHA384,
561 SignatureScheme::RsaPssSha512 => &signature::RSA_PSS_SHA512,
562 _ => &signature::RSA_PKCS1_SHA256,
563 };
564 let mut signature = vec![0; key.public().modulus_len()];
565 key.sign(encoding, &SystemRandom::new(), message, &mut signature)
566 .map_err(|_| CryptoError::Provider("signature generation failed".into()))?;
567 Ok(signature)
568 }
569 }
570 }
571
572 fn to_pkcs8_der(&self) -> Result<Option<SecretVec>, CryptoError> {
573 Ok(Some(self.pkcs8_der.clone()))
574 }
575}
576
577fn agreement_algorithm(
578 algorithm: KeyExchangeAlgorithm,
579) -> Result<&'static agreement::Algorithm, CryptoError> {
580 match algorithm {
581 KeyExchangeAlgorithm::P256 => Ok(&agreement::ECDH_P256),
582 KeyExchangeAlgorithm::P384 => Ok(&agreement::ECDH_P384),
583 KeyExchangeAlgorithm::X25519 => Ok(&agreement::X25519),
584 KeyExchangeAlgorithm::X25519MlKem768 => Err(CryptoError::UnsupportedAlgorithm(
586 CryptoAlgorithm::KeyExchange(algorithm),
587 )),
588 }
589}
590
591fn verification_algorithm(
592 scheme: SignatureScheme,
593) -> &'static dyn signature::VerificationAlgorithm {
594 match scheme {
595 SignatureScheme::Ed25519 => &signature::ED25519,
596 SignatureScheme::EcdsaP256Sha256 => &signature::ECDSA_P256_SHA256_ASN1,
597 SignatureScheme::EcdsaP384Sha256 => &signature::ECDSA_P384_SHA256_ASN1,
598 SignatureScheme::EcdsaP384Sha384 => &signature::ECDSA_P384_SHA384_ASN1,
599 SignatureScheme::RsaPkcs1Sha1 => &signature::RSA_PKCS1_1024_8192_SHA1_FOR_LEGACY_USE_ONLY,
600 SignatureScheme::RsaPkcs1Sha256 => {
601 &signature::RSA_PKCS1_1024_8192_SHA256_FOR_LEGACY_USE_ONLY
602 }
603 SignatureScheme::RsaPkcs1Sha384 => &signature::RSA_PKCS1_2048_8192_SHA384,
604 SignatureScheme::RsaPkcs1Sha512 => {
605 &signature::RSA_PKCS1_1024_8192_SHA512_FOR_LEGACY_USE_ONLY
606 }
607 SignatureScheme::RsaPssSha256 => &signature::RSA_PSS_2048_8192_SHA256,
608 SignatureScheme::RsaPssSha384 => &signature::RSA_PSS_2048_8192_SHA384,
609 SignatureScheme::RsaPssSha512 => &signature::RSA_PSS_2048_8192_SHA512,
610 }
611}
612
613fn verify_public_key_encoding(
614 scheme: SignatureScheme,
615 encoding: PublicKeyEncoding,
616) -> Result<(), CryptoError> {
617 let valid = matches!(
618 (scheme, encoding),
619 (SignatureScheme::Ed25519, PublicKeyEncoding::Ed25519Raw)
620 | (
621 SignatureScheme::EcdsaP256Sha256
622 | SignatureScheme::EcdsaP384Sha256
623 | SignatureScheme::EcdsaP384Sha384,
624 PublicKeyEncoding::EcUncompressedPoint
625 )
626 | (
627 SignatureScheme::RsaPkcs1Sha1
628 | SignatureScheme::RsaPkcs1Sha256
629 | SignatureScheme::RsaPkcs1Sha384
630 | SignatureScheme::RsaPkcs1Sha512
631 | SignatureScheme::RsaPssSha256
632 | SignatureScheme::RsaPssSha384
633 | SignatureScheme::RsaPssSha512,
634 PublicKeyEncoding::RsaPkcs1Der
635 )
636 );
637 if valid {
638 Ok(())
639 } else {
640 Err(CryptoError::InvalidPublicKey)
641 }
642}
643
644type HmacSha1 = ::hmac::Hmac<Sha1>;
645
646pub(crate) struct RustCryptoHmacSha1 {
655 keyed: HmacSha1,
656}
657
658impl RustCryptoHmacSha1 {
659 pub(crate) fn new(key: &[u8]) -> Self {
660 Self {
661 keyed: <HmacSha1 as RustCryptoMac>::new_from_slice(key)
663 .expect("HMAC accepts keys of any length"),
664 }
665 }
666}
667
668impl Mac for RustCryptoHmacSha1 {
669 fn output_len(&self) -> usize {
670 20
671 }
672
673 fn sign(&mut self, input: &[&[u8]], output: &mut [u8]) -> Result<(), CryptoError> {
674 check_tag_len(20, output.len())?;
675 let mut mac = self.keyed.clone();
676 for part in input {
677 mac.update(part);
678 }
679 output.copy_from_slice(&mac.finalize().into_bytes());
680 Ok(())
681 }
682
683 fn verify(&mut self, input: &[&[u8]], expected: &[u8]) -> Result<(), CryptoError> {
684 check_tag_len(20, expected.len())?;
685 let mut actual = [0u8; 20];
686 self.sign(input, &mut actual)?;
687 if crate::constant_time_eq(&actual, expected) {
688 Ok(())
689 } else {
690 Err(CryptoError::AuthenticationFailed)
691 }
692 }
693}