Skip to content

Commit

Permalink
RSA PSS tests
Browse files Browse the repository at this point in the history
  • Loading branch information
franziskuskiefer committed Dec 20, 2024
1 parent a0130bf commit f3b9143
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
12 changes: 12 additions & 0 deletions rsa/src/impl_hacl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ use crate::Error;
#[derive(Debug)]
pub struct Signature<const LEN: usize>([u8; LEN]);

impl<const LEN: usize> Signature<LEN> {
pub fn new() -> Self {
Self([0u8; LEN])
}
}

impl<const LEN: usize> From<[u8; LEN]> for Signature<LEN> {
fn from(value: [u8; LEN]) -> Self {
Self(value)
}
}

/// An RSA Public Key that is `LEN` bytes long.
#[derive(Debug, Clone)]
pub struct PublicKey<const LEN: usize> {
Expand Down
129 changes: 129 additions & 0 deletions rsa/tests/rsa_pss.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
use libcrux_rsa::{sign_2048, verify_2048, DigestAlgorithm, PrivateKey, PublicKey, Signature};

const MODULUS: [u8; 256] = [
0xd2, 0x78, 0x16, 0xcb, 0x72, 0xbb, 0x6e, 0x27, 0xdb, 0x10, 0x1a, 0x6f, 0x3e, 0x64, 0x62, 0x93,
0xd9, 0xec, 0xa7, 0xb3, 0x98, 0xe3, 0x36, 0x6c, 0x9e, 0x69, 0x31, 0xc4, 0x5d, 0xd7, 0x24, 0xd3,
0xf8, 0x90, 0xb0, 0xd0, 0x57, 0x78, 0x3e, 0xdd, 0xee, 0xf0, 0xc9, 0x0e, 0x98, 0x6d, 0xad, 0xe9,
0x46, 0x47, 0xc5, 0xcb, 0x4d, 0xa4, 0xc6, 0x9c, 0x83, 0x1a, 0x13, 0x9f, 0xb7, 0x8d, 0xe7, 0xe3,
0x79, 0x97, 0xf2, 0x9e, 0x36, 0x5c, 0x96, 0xaa, 0xf6, 0x29, 0xfe, 0x6e, 0x3c, 0x0d, 0xb0, 0xcb,
0x04, 0x7d, 0x35, 0xd3, 0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 0x99,
0x8b, 0x78, 0x7a, 0xd5, 0xec, 0x93, 0x53, 0x5a, 0x84, 0x43, 0x47, 0x1a, 0xaf, 0x68, 0xa7, 0x5f,
0x4e, 0x62, 0xe5, 0xcf, 0x07, 0xc9, 0x2b, 0x67, 0x34, 0x82, 0x27, 0xf6, 0xe0, 0x6d, 0x51, 0xca,
0x21, 0xea, 0xfa, 0x32, 0xf0, 0x9f, 0x84, 0xb4, 0xfb, 0xaf, 0x25, 0x1e, 0x91, 0x08, 0x94, 0x5e,
0x83, 0x7f, 0x0f, 0x6a, 0x86, 0x98, 0x77, 0xb8, 0xb0, 0xca, 0xd0, 0x34, 0x10, 0x69, 0x59, 0x21,
];

#[test]
fn self_test_rsa_pss() {
let pk = PublicKey::from(MODULUS);
let sk = PrivateKey::from_components(MODULUS, PRIVATE_EXPONENT);
let salt = [1, 2, 3, 4, 5];
let msg = [7, 8, 9, 10];
let mut signature = Signature::new();
sign_2048(DigestAlgorithm::Sha2_256, &sk, &msg, &salt, &mut signature).unwrap();
eprintln!("signature: {:x?}", signature);
verify_2048(
DigestAlgorithm::Sha2_256,
&pk,
&msg,
salt.len() as u32,
&signature,
)
.expect("Error verifying signature");
}

const N: [u8; 256] = [
0xa2, 0xb4, 0x51, 0xa0, 0x7d, 0x0a, 0xa5, 0xf9, 0x6e, 0x45, 0x56, 0x71, 0x51, 0x35, 0x50, 0x51,
0x4a, 0x8a, 0x5b, 0x46, 0x2e, 0xbe, 0xf7, 0x17, 0x09, 0x4f, 0xa1, 0xfe, 0xe8, 0x22, 0x24, 0xe6,
0x37, 0xf9, 0x74, 0x6d, 0x3f, 0x7c, 0xaf, 0xd3, 0x18, 0x78, 0xd8, 0x03, 0x25, 0xb6, 0xef, 0x5a,
0x17, 0x00, 0xf6, 0x59, 0x03, 0xb4, 0x69, 0x42, 0x9e, 0x89, 0xd6, 0xea, 0xc8, 0x84, 0x50, 0x97,
0xb5, 0xab, 0x39, 0x31, 0x89, 0xdb, 0x92, 0x51, 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[test]
fn wycheproof_single_test() {
let pk = PublicKey::from(N);
let msg: [u8; 0] = [];
let signature: [u8; 256] = [
0x20, 0x08, 0x1f, 0x88, 0x94, 0xa1, 0x33, 0x0c, 0x4d, 0x50, 0x3f, 0x64, 0x28, 0x80, 0xe3,
0xc3, 0x0e, 0x39, 0x8f, 0xc6, 0x23, 0x5c, 0x24, 0xf1, 0xbe, 0x75, 0x2e, 0x2d, 0x49, 0xcd,
0x94, 0x93, 0xac, 0x0c, 0xf9, 0x99, 0xe2, 0x75, 0xc4, 0xf8, 0x9f, 0xf0, 0x8f, 0x0d, 0x9b,
0xa4, 0xe2, 0x64, 0xa3, 0x32, 0x52, 0x5a, 0x61, 0x6d, 0x33, 0x6b, 0xd9, 0xe8, 0x22, 0xf4,
0x1a, 0xb3, 0xf4, 0xfa, 0xe2, 0xf4, 0x8e, 0xc6, 0x6c, 0x2e, 0x52, 0x64, 0x2e, 0xd9, 0x3b,
0x7c, 0xb9, 0x44, 0x39, 0x6f, 0xba, 0xa7, 0x27, 0xcb, 0xfd, 0xfc, 0x1f, 0x20, 0xaa, 0xce,
0x99, 0xa6, 0xf2, 0xa7, 0x44, 0x75, 0xc3, 0x38, 0xf8, 0xd9, 0xf2, 0x2a, 0x38, 0xcb, 0x5b,
0xc5, 0x17, 0x52, 0x07, 0x65, 0x03, 0xb3, 0xae, 0xf1, 0xe6, 0x5e, 0x5a, 0x8f, 0x85, 0x83,
0xd9, 0xae, 0x73, 0x78, 0xde, 0xd0, 0x38, 0xcf, 0x51, 0x68, 0x98, 0xad, 0x06, 0xbe, 0xb9,
0x0a, 0x42, 0xb8, 0x57, 0x64, 0x52, 0x6f, 0xce, 0xa4, 0x4f, 0x74, 0x25, 0x8f, 0xa4, 0xef,
0xb1, 0xda, 0x25, 0x3d, 0x33, 0x7f, 0x65, 0x61, 0x91, 0x81, 0xce, 0xb8, 0x32, 0xdf, 0xe2,
0x85, 0xce, 0x78, 0xae, 0x6b, 0x15, 0xf2, 0x04, 0xe2, 0x3b, 0xab, 0x27, 0x4e, 0x87, 0x44,
0x5d, 0x9f, 0x5d, 0xf9, 0x7f, 0x41, 0xdc, 0x8e, 0x3a, 0x97, 0x73, 0x6b, 0x62, 0x59, 0x1d,
0x07, 0x57, 0x44, 0xb2, 0x55, 0x2f, 0x90, 0xbc, 0xf1, 0xb1, 0x39, 0x3e, 0x1e, 0x76, 0x27,
0xef, 0x1f, 0x98, 0x5f, 0x2b, 0xba, 0xbd, 0x52, 0xe4, 0x3a, 0x35, 0xd0, 0xdd, 0xf4, 0xc6,
0x71, 0x26, 0xe3, 0x91, 0xf9, 0x22, 0xef, 0x7b, 0x1b, 0xb1, 0x91, 0x1c, 0xd6, 0xe1, 0xb3,
0x03, 0xcb, 0x29, 0x10, 0xdd, 0x70, 0x67, 0x2b, 0xbf, 0xb6, 0x2e, 0xa4, 0xea, 0xad, 0x72,
0x5c,
];
verify_2048(DigestAlgorithm::Sha2_256, &pk, &msg, 0, &signature.into())
.expect("Error verifying signature");

let msg = [0x33, 0x32, 0x32, 0x32, 0x30, 0x34, 0x31, 0x30, 0x34, 0x36];
let signature: [u8; 256] = [
0x6b, 0x9a, 0x45, 0xec, 0x51, 0x7b, 0xf8, 0x38, 0xa5, 0x35, 0xed, 0x93, 0xfb, 0x36, 0xbb,
0x02, 0x74, 0x71, 0xb1, 0x1d, 0x20, 0xd0, 0xbf, 0x6b, 0x1e, 0xda, 0xae, 0xef, 0xa2, 0x5b,
0xcc, 0xdf, 0x58, 0x81, 0xf3, 0x44, 0x09, 0xa0, 0x42, 0xda, 0xdb, 0xe4, 0xa7, 0xb3, 0xc4,
0x1b, 0xf2, 0x4a, 0xbe, 0x66, 0xc1, 0xf7, 0xf3, 0x7b, 0x92, 0xb9, 0x65, 0x8f, 0x59, 0xc5,
0x5f, 0xa8, 0x8a, 0xa1, 0x63, 0x24, 0x65, 0xba, 0x82, 0x45, 0xc5, 0xf0, 0xc9, 0x8d, 0x08,
0x2f, 0xca, 0xbb, 0x5e,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signature.into())
.expect("Error verifying signature");
}

0 comments on commit f3b9143

Please sign in to comment.