Skip to content

Commit

Permalink
Add rsa2048_oaep_sha1 operation to asymmetric
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton committed Jan 12, 2024
1 parent 65c309a commit a5fe051
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
10 changes: 9 additions & 1 deletion crates/bitwarden/src/crypto/enc_string/asymmetric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rsa::Oaep;
use serde::Deserialize;

use crate::{
crypto::{AsymmetricCryptoKey, KeyDecryptable},
crypto::{rsa::encrypt_rsa2048_oaep_sha1, AsymmetricCryptoKey, KeyDecryptable},
error::{CryptoError, EncStringParseError, Error, Result},
};

Expand Down Expand Up @@ -137,6 +137,14 @@ impl serde::Serialize for AsymmEncString {
}

impl AsymmEncString {
pub(crate) fn encrypt_rsa2048_oaep_sha1(
data_dec: &[u8],
key: AsymmetricCryptoKey,
) -> Result<AsymmEncString> {
let enc = encrypt_rsa2048_oaep_sha1(key.key, data_dec)?;
Ok(AsymmEncString::Rsa2048_OaepSha1_B64 { data: enc })
}

/// The numerical representation of the encryption type of the [AsymmEncString].
const fn enc_type(&self) -> u8 {
match self {
Expand Down
16 changes: 15 additions & 1 deletion crates/bitwarden/src/crypto/rsa.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use base64::{engine::general_purpose::STANDARD, Engine};
use rsa::{
pkcs8::{EncodePrivateKey, EncodePublicKey},
RsaPrivateKey, RsaPublicKey,
Oaep, RsaPrivateKey, RsaPublicKey,
};
use sha1::Sha1;

use crate::{
crypto::{EncString, SymmetricCryptoKey},
Expand Down Expand Up @@ -38,6 +39,19 @@ pub(super) fn make_key_pair(key: &SymmetricCryptoKey) -> Result<RsaKeyPair> {
})
}

pub(super) fn encrypt_rsa2048_oaep_sha1(
private_key: RsaPrivateKey,
data: &[u8],
) -> Result<Vec<u8>> {
let mut rng = rand::thread_rng();

let padding = Oaep::new::<Sha1>();
private_key
.to_public_key()
.encrypt(&mut rng, padding, data)
.map_err(|e| e.to_string().into())
}

// TODO: Move this to AsymmCryptoKey
/// Generate a new random AsymmetricCryptoKey (RSA-2048)
pub(crate) fn generate_rsa() -> RsaPrivateKey {
Expand Down

0 comments on commit a5fe051

Please sign in to comment.