Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement RSA encryption (with update for current rewrite structure) #56

Draft
wants to merge 4 commits into
base: rewrite/v3
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ thiserror = "1.0.63"

# Cryptography
rsa = "0.9.6"
rand = "0.9.0-alpha.2"
rand = "0.8.5"
der = "0.7.9"

# Encoding/Serialization
serde = "1.0.210"
Expand Down
3 changes: 3 additions & 0 deletions src/lib/net/encryption/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ edition = "2021"

[dependencies]
thiserror = { workspace = true }
rsa = { workspace = true }
der = { workspace = true }
rand = { workspace = true }
10 changes: 8 additions & 2 deletions src/lib/net/encryption/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ use thiserror::Error;

#[derive(Debug, Clone, Error)]
pub enum NetEncryptionError {
#[error("Something failed lol")]
SomeError,
#[error("Error in encrypting data with RSA")]
RsaEncryptionError,
#[error("Error in decrypting data with RSA")]
RsaDecryptionError,
#[error("Error in generating RSA keypair")]
RsaKeyGenerationError,
#[error("Error in encoding public key to ASN.1 DER format")]
RsaKeyEncodingError,
}
2 changes: 1 addition & 1 deletion src/lib/net/encryption/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod errors;

pub mod rsa_enc;
pub fn add(left: u64, right: u64) -> u64 {
left + right
}
Expand Down
60 changes: 60 additions & 0 deletions src/lib/net/encryption/src/rsa_enc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use rsa::{RsaPublicKey, RsaPrivateKey, pkcs1::EncodeRsaPublicKey, Pkcs1v15Encrypt};
use rand::rngs::OsRng;
use crate::errors;

pub struct KeyPair {
//public key side of the keypair (for encryption)
pub public_key: RsaPublicKey,
//the public key encoded in ASN.1 DER format for publishing to the client
pub encoded_public_key: der::Document,
//private key side of the keypair (for decryption)
pub private_key: RsaPrivateKey,
}

impl KeyPair {
pub fn new() -> Result<KeyPair, errors::NetEncryptionError> {
let mut rng = OsRng;

let private_key = RsaPrivateKey::new(&mut rng, 1024)
.map_err(|_e| errors::NetEncryptionError::RsaKeyGenerationError)?;

let public_key = RsaPublicKey::from(&private_key);

let encoded_public_key = public_key.to_pkcs1_der()
.map_err(|_e| errors::NetEncryptionError::RsaKeyEncodingError)?;

Ok(Self {
public_key,
encoded_public_key,
private_key,
})
}

pub fn encrypt (&self, data: &[u8]) -> Result<Vec<u8>, errors::NetEncryptionError> {
let mut rng = OsRng;

self.public_key.encrypt(&mut rng, Pkcs1v15Encrypt, data)
.map_err(|_e| errors::NetEncryptionError::RsaEncryptionError)
}

pub fn decrypt (&self, data: &[u8]) -> Result<Vec<u8>, errors::NetEncryptionError> {
self.private_key.decrypt(Pkcs1v15Encrypt, data)
.map_err(|_e| errors::NetEncryptionError::RsaDecryptionError)
}
}

impl Default for KeyPair {
fn default() -> KeyPair {
Self::new().expect("Keypair generation failed")
}
}

#[test]
fn test_encrypt_decrypt() {
let keypair = KeyPair::new().expect("Failed keypair generation");
let msg = "Hello World!";
let msg_in_bytes: Vec<u8> = msg.to_string().into_bytes();
let encrypted_msg = keypair.encrypt(&msg_in_bytes).expect("Failed to encrypt");
let decrypted_msg = keypair.decrypt(&encrypted_msg).expect("Failed to decrypt");
assert_eq!(decrypted_msg, msg_in_bytes);
}