|
4 | 4 | //! |
5 | 5 | //! See [code example in the toplevel rustdoc](../index.html#oaep-encryption). |
6 | 6 |
|
| 7 | +mod decrypting_key; |
| 8 | +mod encrypting_key; |
| 9 | + |
| 10 | +pub use self::{decrypting_key::DecryptingKey, encrypting_key::EncryptingKey}; |
| 11 | + |
7 | 12 | use alloc::boxed::Box; |
8 | 13 | use alloc::string::{String, ToString}; |
9 | 14 | use alloc::vec::Vec; |
10 | 15 | use core::fmt; |
11 | | -use core::marker::PhantomData; |
12 | 16 |
|
13 | 17 | use digest::{Digest, DynDigest, FixedOutputReset}; |
14 | 18 | use num_bigint::BigUint; |
15 | 19 | use rand_core::CryptoRngCore; |
16 | | -use zeroize::{ZeroizeOnDrop, Zeroizing}; |
| 20 | +use zeroize::Zeroizing; |
17 | 21 |
|
18 | 22 | use crate::algorithms::oaep::*; |
19 | 23 | use crate::algorithms::pad::{uint_to_be_pad, uint_to_zeroizing_be_pad}; |
20 | 24 | use crate::algorithms::rsa::{rsa_decrypt_and_check, rsa_encrypt}; |
21 | | -use crate::dummy_rng::DummyRng; |
22 | 25 | use crate::errors::{Error, Result}; |
23 | 26 | use crate::key::{self, RsaPrivateKey, RsaPublicKey}; |
24 | | -use crate::traits::PaddingScheme; |
25 | | -use crate::traits::{Decryptor, RandomizedDecryptor, RandomizedEncryptor}; |
26 | | -use crate::traits::PublicKeyParts; |
| 27 | +use crate::traits::{PaddingScheme, PublicKeyParts}; |
27 | 28 |
|
28 | 29 | /// Encryption and Decryption using [OAEP padding](https://datatracker.ietf.org/doc/html/rfc8017#section-7.1). |
29 | 30 | /// |
@@ -282,149 +283,12 @@ fn decrypt_digest<R: CryptoRngCore + ?Sized, D: Digest, MGD: Digest + FixedOutpu |
282 | 283 | oaep_decrypt_digest::<D, MGD>(&mut em, label, priv_key.size()) |
283 | 284 | } |
284 | 285 |
|
285 | | -/// Encryption key for PKCS#1 v1.5 encryption as described in [RFC8017 § 7.1]. |
286 | | -/// |
287 | | -/// [RFC8017 § 7.1]: https://datatracker.ietf.org/doc/html/rfc8017#section-7.1 |
288 | | -#[derive(Debug, Clone)] |
289 | | -pub struct EncryptingKey<D, MGD = D> |
290 | | -where |
291 | | - D: Digest, |
292 | | - MGD: Digest + FixedOutputReset, |
293 | | -{ |
294 | | - inner: RsaPublicKey, |
295 | | - label: Option<String>, |
296 | | - phantom: PhantomData<D>, |
297 | | - mg_phantom: PhantomData<MGD>, |
298 | | -} |
299 | | - |
300 | | -impl<D, MGD> EncryptingKey<D, MGD> |
301 | | -where |
302 | | - D: Digest, |
303 | | - MGD: Digest + FixedOutputReset, |
304 | | -{ |
305 | | - /// Create a new verifying key from an RSA public key. |
306 | | - pub fn new(key: RsaPublicKey) -> Self { |
307 | | - Self { |
308 | | - inner: key, |
309 | | - label: None, |
310 | | - phantom: Default::default(), |
311 | | - mg_phantom: Default::default(), |
312 | | - } |
313 | | - } |
314 | | - |
315 | | - /// Create a new verifying key from an RSA public key using provided label |
316 | | - pub fn new_with_label<S: AsRef<str>>(key: RsaPublicKey, label: S) -> Self { |
317 | | - Self { |
318 | | - inner: key, |
319 | | - label: Some(label.as_ref().to_string()), |
320 | | - phantom: Default::default(), |
321 | | - mg_phantom: Default::default(), |
322 | | - } |
323 | | - } |
324 | | -} |
325 | | - |
326 | | -impl<D, MGD> RandomizedEncryptor for EncryptingKey<D, MGD> |
327 | | -where |
328 | | - D: Digest, |
329 | | - MGD: Digest + FixedOutputReset, |
330 | | -{ |
331 | | - fn encrypt_with_rng<R: CryptoRngCore + ?Sized>( |
332 | | - &self, |
333 | | - rng: &mut R, |
334 | | - msg: &[u8], |
335 | | - ) -> Result<Vec<u8>> { |
336 | | - encrypt_digest::<_, D, MGD>(rng, &self.inner, msg, self.label.as_ref().cloned()) |
337 | | - } |
338 | | -} |
339 | | - |
340 | | -/// Decryption key for PKCS#1 v1.5 decryption as described in [RFC8017 § 7.1]. |
341 | | -/// |
342 | | -/// [RFC8017 § 7.1]: https://datatracker.ietf.org/doc/html/rfc8017#section-7.1 |
343 | | -#[derive(Debug, Clone)] |
344 | | -pub struct DecryptingKey<D, MGD = D> |
345 | | -where |
346 | | - D: Digest, |
347 | | - MGD: Digest + FixedOutputReset, |
348 | | -{ |
349 | | - inner: RsaPrivateKey, |
350 | | - label: Option<String>, |
351 | | - phantom: PhantomData<D>, |
352 | | - mg_phantom: PhantomData<MGD>, |
353 | | -} |
354 | | - |
355 | | -impl<D, MGD> DecryptingKey<D, MGD> |
356 | | -where |
357 | | - D: Digest, |
358 | | - MGD: Digest + FixedOutputReset, |
359 | | -{ |
360 | | - /// Create a new verifying key from an RSA public key. |
361 | | - pub fn new(key: RsaPrivateKey) -> Self { |
362 | | - Self { |
363 | | - inner: key, |
364 | | - label: None, |
365 | | - phantom: Default::default(), |
366 | | - mg_phantom: Default::default(), |
367 | | - } |
368 | | - } |
369 | | - |
370 | | - /// Create a new verifying key from an RSA public key using provided label |
371 | | - pub fn new_with_label<S: AsRef<str>>(key: RsaPrivateKey, label: S) -> Self { |
372 | | - Self { |
373 | | - inner: key, |
374 | | - label: Some(label.as_ref().to_string()), |
375 | | - phantom: Default::default(), |
376 | | - mg_phantom: Default::default(), |
377 | | - } |
378 | | - } |
379 | | -} |
380 | | - |
381 | | -impl<D, MGD> Decryptor for DecryptingKey<D, MGD> |
382 | | -where |
383 | | - D: Digest, |
384 | | - MGD: Digest + FixedOutputReset, |
385 | | -{ |
386 | | - fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>> { |
387 | | - decrypt_digest::<DummyRng, D, MGD>( |
388 | | - None, |
389 | | - &self.inner, |
390 | | - ciphertext, |
391 | | - self.label.as_ref().cloned(), |
392 | | - ) |
393 | | - } |
394 | | -} |
395 | | - |
396 | | -impl<D, MGD> RandomizedDecryptor for DecryptingKey<D, MGD> |
397 | | -where |
398 | | - D: Digest, |
399 | | - MGD: Digest + FixedOutputReset, |
400 | | -{ |
401 | | - fn decrypt_with_rng<R: CryptoRngCore + ?Sized>( |
402 | | - &self, |
403 | | - rng: &mut R, |
404 | | - ciphertext: &[u8], |
405 | | - ) -> Result<Vec<u8>> { |
406 | | - decrypt_digest::<_, D, MGD>( |
407 | | - Some(rng), |
408 | | - &self.inner, |
409 | | - ciphertext, |
410 | | - self.label.as_ref().cloned(), |
411 | | - ) |
412 | | - } |
413 | | -} |
414 | | - |
415 | | -impl<D, MGD> ZeroizeOnDrop for DecryptingKey<D, MGD> |
416 | | -where |
417 | | - D: Digest, |
418 | | - MGD: Digest + FixedOutputReset, |
419 | | -{ |
420 | | -} |
421 | | - |
422 | 286 | #[cfg(test)] |
423 | 287 | mod tests { |
424 | 288 | use crate::key::{RsaPrivateKey, RsaPublicKey}; |
425 | 289 | use crate::oaep::{DecryptingKey, EncryptingKey, Oaep}; |
426 | | - use crate::traits::{Decryptor, RandomizedDecryptor, RandomizedEncryptor}; |
427 | 290 | use crate::traits::PublicKeyParts; |
| 291 | + use crate::traits::{Decryptor, RandomizedDecryptor, RandomizedEncryptor}; |
428 | 292 |
|
429 | 293 | use alloc::string::String; |
430 | 294 | use digest::{Digest, DynDigest, FixedOutputReset}; |
|
0 commit comments