Skip to content

Commit

Permalink
Diverse Imports und Formatierungen korrigiert.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan-H11 committed Feb 16, 2024
1 parent 4af8105 commit efc78d5
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 44 deletions.
21 changes: 7 additions & 14 deletions src/api/endpoints/rsa/create_key_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use actix_web::{HttpResponse, Responder};
use log::info;
use serde::Deserialize;

use crate::api::serializable_models::{KeyPair, SingleStringResponse, UseFastQuery};
use crate::api::serializable_models::{KeyPair, UseFastQuery};
use crate::encryption::asymmetric_encryption_types::{AsymmetricKeyPair, KeyGenerator};
use crate::encryption::rsa::rsa_scheme::{RsaKeyGenConfig, RsaScheme};
use crate::math_core::block_chiffre::determine_block_size;
Expand Down Expand Up @@ -52,22 +52,15 @@ pub(crate) async fn create_key_pair(

let key_pair = RsaScheme::generate_keypair(&config);

let public_key= key_pair.public();
let public_key = key_pair.public();
let private_key = key_pair.private();

let block_size_pub = determine_block_size(
&public_key.n,
&req_body.number_system_base.into(),
true,
)
.to_string();
let block_size_pub =
determine_block_size(&public_key.n, &req_body.number_system_base.into(), true).to_string();

let block_size_priv = determine_block_size(
&private_key.n,
&req_body.number_system_base.into(),
false,
)
.to_string();
let block_size_priv =
determine_block_size(&private_key.n, &req_body.number_system_base.into(), false)
.to_string();

let key_pair_response = KeyPair {
modulus: public_key.n.to_str_radix(10),
Expand Down
4 changes: 3 additions & 1 deletion src/api/endpoints/rsa/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ pub(crate) async fn decrypt(
};

let rsa_service =
crate::encryption::rsa::rsa_with_string_service::RsaWithStringService::new(number_theory_service);
crate::encryption::rsa::rsa_with_string_service::RsaWithStringService::new(
number_theory_service,
);

let plaintext = rsa_service.decrypt(&ciphertext, number_system_base, &private_key);
let response = SingleStringResponse { message: plaintext };
Expand Down
4 changes: 3 additions & 1 deletion src/api/endpoints/rsa/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ pub(crate) async fn encrypt(
};

let rsa_service =
crate::encryption::rsa::rsa_with_string_service::RsaWithStringService::new(number_theory_service);
crate::encryption::rsa::rsa_with_string_service::RsaWithStringService::new(
number_theory_service,
);

let ciphertext = rsa_service.encrypt(&plaintext, number_system_base, &public_key);
let response = SingleStringResponse {
Expand Down
8 changes: 5 additions & 3 deletions src/api/endpoints/rsa/multiplication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::str::FromStr;
use crate::api::serializable_models::{KeyPair, SingleStringResponse, UseFastQuery};
use crate::encryption::asymmetric_encryption_types::{Decryptor, Encryptor};
use crate::encryption::rsa::rsa_scheme::RsaScheme;
use crate::encryption::rsa::rsa_with_string_service::RsaWithStringService;

use crate::math_core::number_theory::number_theory_service::NumberTheoryService;
use crate::math_core::number_theory::number_theory_service::NumberTheoryServiceSpeed::{
Fast, Slow,
Expand Down Expand Up @@ -55,8 +55,10 @@ pub(crate) async fn multiplication(
let public_key = req_body.key_pair.to_public_key()?;
let private_key = req_body.key_pair.to_private_key()?;

let encrypted_factor_one = RsaScheme::encrypt(&public_key, &factor_one, number_theory_service);
let encrypted_factor_two = RsaScheme::encrypt(&public_key, &factor_two, number_theory_service);
let encrypted_factor_one =
RsaScheme::encrypt(&public_key, &factor_one, number_theory_service);
let encrypted_factor_two =
RsaScheme::encrypt(&public_key, &factor_two, number_theory_service);

let encrypted_result = &encrypted_factor_one * &encrypted_factor_two;

Expand Down
4 changes: 3 additions & 1 deletion src/api/endpoints/rsa/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ pub(crate) async fn sign(
};

let rsa_service =
crate::encryption::rsa::rsa_with_string_service::RsaWithStringService::new(number_theory_service);
crate::encryption::rsa::rsa_with_string_service::RsaWithStringService::new(
number_theory_service,
);

let signature = rsa_service.sign(&plaintext, &private_key, g_base);
let response = SingleStringResponse { message: signature };
Expand Down
4 changes: 3 additions & 1 deletion src/api/endpoints/rsa/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ pub(crate) async fn verify(
};

let rsa_service =
crate::encryption::rsa::rsa_with_string_service::RsaWithStringService::new(number_theory_service);
crate::encryption::rsa::rsa_with_string_service::RsaWithStringService::new(
number_theory_service,
);

let plaintext = rsa_service.verify(&signature, &plaintext, &public_key, g_base);
let response = SingleStringResponse {
Expand Down
8 changes: 4 additions & 4 deletions src/encryption/asymmetric_encryption_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ pub trait VerificationKey<T: AsymmetricEncryptionScheme>: PublicKey<T> {}
/// * `public` - Gibt den öffentlichen Schlüssel zurück.
/// * `private` - Gibt den privaten Schlüssel zurück.
pub trait AsymmetricKeyPair<Public, Private, Scheme>
where
Public: EncryptionKey<Scheme>,
Private: DecryptionKey<Scheme>,
Scheme: AsymmetricEncryptionScheme,
where
Public: EncryptionKey<Scheme>,
Private: DecryptionKey<Scheme>,
Scheme: AsymmetricEncryptionScheme,
{
fn public(&self) -> Public;
fn private(&self) -> Private;
Expand Down
8 changes: 3 additions & 5 deletions src/encryption/rsa/keys.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use crate::encryption::asymmetric_encryption_types::{
AsymmetricEncryptionScheme, AsymmetricKey, AsymmetricKeyPair, DecryptionKey, EncryptionKey,
PrivateKey, PublicKey, SignatureKey, VerificationKey,
AsymmetricKey, AsymmetricKeyPair, DecryptionKey, EncryptionKey, PrivateKey, PublicKey,
SignatureKey, VerificationKey,
};
use crate::encryption::rsa::rsa_scheme::RsaScheme;
use crate::math_core::number_theory::number_theory_service::{
NumberTheoryService, NumberTheoryServiceTrait,
};

use bigdecimal::num_bigint::BigInt;

#[derive(Clone, Debug)]
Expand Down
44 changes: 30 additions & 14 deletions src/encryption/rsa/rsa_with_string_service.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
use bigdecimal::num_bigint::{BigInt, Sign};
use log::{debug, info};
use sha2::{Digest, Sha256};
use crate::encryption::asymmetric_encryption_types::{Decryptor, Encryptor, Signer};
use crate::encryption::rsa::keys::{RsaPrivateKey, RsaPublicKey};
use crate::encryption::rsa::rsa_scheme::RsaScheme;
use bigdecimal::num_bigint::{BigInt, Sign};
use log::{debug, info};
use sha2::{Digest, Sha256};

use crate::math_core::block_chiffre::{
create_string_from_blocks_decrypt, create_string_from_blocks_encrypt, encode_string_to_blocks,
};
use crate::math_core::number_theory::number_theory_service::{
NumberTheoryService, NumberTheoryServiceTrait,
};
use crate::math_core::number_theory::number_theory_service::NumberTheoryService;
use crate::math_core::traits::logarithm::Logarithm;

pub struct RsaWithStringService {
number_theory_service: NumberTheoryService,
}

impl RsaWithStringService { // TODO: Interface extrahieren, wenn klar ist, wie ElGamal mit dem blockChiffre funktioniert.
impl RsaWithStringService {
// TODO: Interface extrahieren, wenn klar ist, wie ElGamal mit dem blockChiffre funktioniert.
pub fn new(number_theory_service: NumberTheoryService) -> RsaWithStringService {
RsaWithStringService {
number_theory_service,
Expand All @@ -40,8 +39,10 @@ impl RsaWithStringService { // TODO: Interface extrahieren, wenn klar ist, wie E
info!("Verschlüsseln mit blockgröße {}", block_size);

let chunks = encode_string_to_blocks(message, block_size, g_base);
let encrypted_chunks = chunks.iter().map(|chunk| RsaScheme::encrypt(key, chunk, self.number_theory_service)).collect();

let encrypted_chunks = chunks
.iter()
.map(|chunk| RsaScheme::encrypt(key, chunk, self.number_theory_service))
.collect();

// Die Größe der verschlüsselten Blöcke ist immer um 1 größer als die Klartextgröße.
create_string_from_blocks_encrypt(encrypted_chunks, block_size + 1, g_base)
Expand All @@ -63,7 +64,10 @@ impl RsaWithStringService { // TODO: Interface extrahieren, wenn klar ist, wie E
info!("Entschlüsseln mit blockgröße {}", block_size);

let chunks = encode_string_to_blocks(message, block_size, g_base);
let decrypted_chunks = chunks.iter().map(|chunk| RsaScheme::decrypt(key, chunk, self.number_theory_service)).collect();
let decrypted_chunks = chunks
.iter()
.map(|chunk| RsaScheme::decrypt(key, chunk, self.number_theory_service))
.collect();

create_string_from_blocks_decrypt(decrypted_chunks, g_base)
}
Expand All @@ -83,7 +87,10 @@ impl RsaWithStringService { // TODO: Interface extrahieren, wenn klar ist, wie E

let block_size = key.n.log(&g_base.into());
let chunks = encode_string_to_blocks(&hashed_message, block_size, g_base);
let encrypted_chunks = chunks.iter().map(|chunk| RsaScheme::sign(key, chunk, self.number_theory_service)).collect();
let encrypted_chunks = chunks
.iter()
.map(|chunk| RsaScheme::sign(key, chunk, self.number_theory_service))
.collect();

// Die Größe der verschlüsselten Blöcke ist immer um 1 größer als die Klartextgröße.
create_string_from_blocks_encrypt(encrypted_chunks, block_size + 1, g_base)
Expand All @@ -99,7 +106,13 @@ impl RsaWithStringService { // TODO: Interface extrahieren, wenn klar ist, wie E
///
/// # Rückgabe
/// * `bool` - Gibt an, ob die Verifizierung erfolgreich war.
pub(crate) fn verify(&self, signature: &str, message: &str, key: &RsaPublicKey, g_base: u32) -> bool {
pub(crate) fn verify(
&self,
signature: &str,
message: &str,
key: &RsaPublicKey,
g_base: u32,
) -> bool {
info!(
"Verifizieren der Nachricht {} mit Signatur {}",
message, signature
Expand All @@ -113,9 +126,12 @@ impl RsaWithStringService { // TODO: Interface extrahieren, wenn klar ist, wie E
// Ja, da steht encrypt. Das ist aber korrekt, weil dahinter auch nur eine Expnentiation steckt und die Typen aktuell noch nicht stimmen.
// Das umstellen auf Verify komm mit dem Refactoring dieses Services hier.
// Dafür ist es nötig, dass Signatur und Nachricht beide als BigInt vorliegen und nicht als String.
let decrypted_chunks = chunks.iter().map(|chunk| RsaScheme::encrypt(key, chunk, self.number_theory_service)).collect();
let decrypted_chunks = chunks
.iter()
.map(|chunk| RsaScheme::encrypt(key, chunk, self.number_theory_service))
.collect();

let verification =create_string_from_blocks_decrypt(decrypted_chunks, g_base);
let verification = create_string_from_blocks_decrypt(decrypted_chunks, g_base);

verification == message_big_int
}
Expand Down
1 change: 1 addition & 0 deletions src/shared/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl std::error::Error for ArithmeticError {}
#[derive(Debug, Clone)]
pub enum RsaError {
/// Wird geworfen, wenn die Schlüsselerzeugung fehlschlägt.
#[allow(dead_code)] // TODO: Wieder einbauen
KeyGenerationError,
}

Expand Down

0 comments on commit efc78d5

Please sign in to comment.