From 01bfe227cc257abd0d98d727d22efb5b7ccef0b8 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 5 Nov 2018 19:48:17 +0100 Subject: aquatic-prime: Try to encrypt input data string Take the data string we created and feed it through RSA encryption. Basically following the steps used by the Aquatic Prime CLI program written in C: https://github.com/bdrister/AquaticPrime/blob/4f86666/Source/PHP/AquaticPrimeCLI.c At first I tried using a `Signer`, but it looks like I'm supposed to be encrypting the data instead. Unfortunately, I'm now getting this error when calling `private_encrypt()`: 'failed to encrypt: ErrorStack([Error { code: 67526721, library: "rsa routines", function: "RSA_EAY_PRIVATE_ENCRYPT", reason: "malloc failure", file: "rsa_eay.c", line: 368 }])', libcore/result.rs:945:5 Not sure what the problem is yet. --- license-generator/aquatic-prime/src/lib.rs | 40 +++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) (limited to 'license-generator') diff --git a/license-generator/aquatic-prime/src/lib.rs b/license-generator/aquatic-prime/src/lib.rs index f92ad30..80db363 100644 --- a/license-generator/aquatic-prime/src/lib.rs +++ b/license-generator/aquatic-prime/src/lib.rs @@ -2,6 +2,14 @@ extern crate openssl; use std::collections::HashMap; +use openssl::bn::BigNum; +use openssl::hash::MessageDigest; +use openssl::rsa::Padding; +use openssl::pkey::PKey; +use openssl::rsa::RsaPrivateKeyBuilder; +use openssl::sha::sha1; +use openssl::sign::Signer; + struct AquaticPrime<'a> { public_key: &'a str, private_key: &'a str, @@ -20,7 +28,37 @@ impl<'a> AquaticPrime<'a> { .collect::>() .concat(); - println!("{:?}", data); + let public_key = BigNum::from_hex_str(self.public_key).unwrap(); + let private_key = BigNum::from_hex_str(self.private_key).unwrap(); + let rsa_e = BigNum::from_u32(3).unwrap(); + + if public_key.num_bits() != 1024 { + // TODO: Return Err + } + + let digest = sha1(data.as_bytes()); + + let keypair = RsaPrivateKeyBuilder::new( + public_key, + rsa_e, + private_key, + ).expect("failed to build RSA key").build(); + + let mut signature = [0; 128]; + keypair.private_encrypt( + &digest, + &mut signature, + Padding::PKCS1, + ).expect("failed to encrypt"); + + + // let keypair = PKey::from_rsa(keypair).unwrap(); + // + // let mut signer = Signer::new(MessageDigest::sha1(), &keypair).unwrap(); + // signer.update(data.as_bytes()); + // let signature = signer.sign_to_vec(); + + println!("{:?}", String::from_utf8_lossy(&signature)); String::new() } -- cgit v1.2.3