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

feat: add support for SHA-256 RSA PSS signatures #9

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions lib/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "noir_rsa"
type = "lib"
authors = [""]
compiler_version = ">=0.34.0"
compiler_version = ">=0.35.0"

[dependencies]
bignum = {tag = "v0.3.3", git = "https://github.com/noir-lang/noir-bignum"}
bignum = {tag = "v0.3.6", git = "https://github.com/noir-lang/noir-bignum"}
281 changes: 253 additions & 28 deletions lib/src/rsa.nr

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions lib/src/types.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,38 @@ use dep::bignum::BigNum;
use dep::bignum::runtime_bignum::BigNumInstance;
use dep::bignum::fields::Params2048;
use dep::bignum::fields::Params1024;
use dep::bignum::fields::Params4096;
use dep::bignum::runtime_bignum::BigNumParamsTrait;

struct RSA<BN, BNInstance, let NumBytes: u32>{}

struct Params1025 {}
impl BigNumParamsTrait<9> for Params1025 {
fn modulus_bits() -> u32 {
1025
}
}

struct Params1964 {}
impl BigNumParamsTrait<17> for Params1964 {
fn modulus_bits() -> u32 {
1964
}
}

type BN1024 = BigNum<9, Params1024>;
type BN1025 = BigNum<9, Params1025>;
type BN1964 = BigNum<17, Params1964>;
type BN2048 = BigNum<18, Params2048>;
type BN4096 = BigNum<35, Params4096>;
type BNInst1024 = BigNumInstance<9, Params1024>;
type BNInst1025 = BigNumInstance<9, Params1025>;
type BNInst1964 = BigNumInstance<17, Params1964>;
type BNInst2048 = BigNumInstance<18, Params2048>;
type BNInst4096 = BigNumInstance<35, Params4096>;

type RSA1024 = RSA<BN1024, BNInst1024, 128>;
type RSA1025 = RSA<BN1025, BNInst1025, 129>;
type RSA1964 = RSA<BN1964, BNInst1964, 246>;
type RSA2048 = RSA<BN2048, BNInst2048, 256>;
type RSA4096 = RSA<BN4096, BNInst4096, 512>;
6 changes: 3 additions & 3 deletions signature_gen/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion signature_gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
[dependencies]
clap = "2.33"
toml = "0.5"
noir-bignum-paramgen = { version = "0.1.2" }
noir-bignum-paramgen = { version = "0.1.4" }
hex = { version = "0.4" }
rsa = { git = "https://github.com/RustCrypto/RSA" } # from online repo
rand = { version = "0.8.5" }
Expand Down
28 changes: 21 additions & 7 deletions signature_gen/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use num_bigint::BigUint;
use rsa::pkcs1v15::Signature;
use rsa::{RsaPrivateKey, RsaPublicKey};
use signature::Keypair;
use signature::RandomizedSignerMut;
use std::env;
use toml::Value;

use rsa::signature::{SignatureEncoding, Signer};
Expand Down Expand Up @@ -28,7 +31,7 @@ fn format_limbs_as_toml_value(limbs: &Vec<BigUint>) -> Vec<Value> {
.collect()
}

fn generate_2048_bit_signature_parameters(msg: &str, as_toml: bool, exponent: u32) {
fn generate_2048_bit_signature_parameters(msg: &str, as_toml: bool, exponent: u32, pss: bool) {
let mut hasher = Sha256::new();
hasher.update(msg.as_bytes());
let hashed_message = hasher.finalize();
Expand All @@ -46,12 +49,16 @@ fn generate_2048_bit_signature_parameters(msg: &str, as_toml: bool, exponent: u3
.expect("failed to generate a key");
let pub_key: RsaPublicKey = priv_key.clone().into();

let signing_key = rsa::pkcs1v15::SigningKey::<Sha256>::new(priv_key);
let sig: Vec<u8> = signing_key.sign(msg.as_bytes()).to_vec();

let sig_bytes = &Signature::try_from(sig.as_slice()).unwrap().to_bytes();
let sig_bytes = if pss {
let mut signing_key = rsa::pss::BlindedSigningKey::<Sha256>::new(priv_key);
let sig = signing_key.sign_with_rng(&mut rng, msg.as_bytes());
sig.to_vec()
} else {
let signing_key = rsa::pkcs1v15::SigningKey::<Sha256>::new(priv_key);
signing_key.sign(msg.as_bytes()).to_vec()
};

let sig_uint: BigUint = BigUint::from_bytes_be(sig_bytes);
let sig_uint: BigUint = BigUint::from_bytes_be(&sig_bytes);

let sig_str = bn_limbs(sig_uint.clone(), 2048);

Expand Down Expand Up @@ -104,6 +111,12 @@ fn main() {
.long("toml")
.help("Print output in TOML format"),
)
.arg(
Arg::with_name("pss")
.short("p")
.long("pss")
.help("Use RSA PSS"),
)
.arg(
Arg::with_name("exponent")
.short("e")
Expand All @@ -116,9 +129,10 @@ fn main() {

let msg = matches.value_of("msg").unwrap();
let as_toml = matches.is_present("toml");
let pss = matches.is_present("pss");
let e: u32 = matches.value_of("exponent").unwrap().parse().unwrap();

generate_2048_bit_signature_parameters(msg, as_toml, e);
generate_2048_bit_signature_parameters(msg, as_toml, e, pss);
}

#[cfg(test)]
Expand Down