rsa_heapless 0.2.1

Pure Rust RSA implementation - heapless fork
Documentation
//! Host-side companion to the `rsa512_sha1` footprint fixture — the
//! lightest cell in our measurement matrix: RSA-512 + SHA-1 + e=3.
//! Intended only to exercise and demonstrate the generic public-key path on
//! a workload small enough to read end-to-end. Not for production use:
//! RSA-512 was factored in 1999 and SHA-1 is NIST-disallowed for signature
//! generation. The companion `rsa1024_verify` / `rsa2048_verify` /
//! `rsa3072_verify` examples show the SHA-256 path at realistic key sizes.

use fixed_bigint::FixedUInt;
use rsa::pkcs1v15::{GenericSignature, GenericVerifyingKey};
use rsa::signature::hazmat::PrehashVerifier;
use rsa::{
    modmath_support::{public_key_from_be_bytes, rsa_public_op},
    ModMathValue,
};
use sha1::Sha1;

fn main() {
    type U512 = FixedUInt<u8, 64>;

    let data = b"hello world!";
    let digest: [u8; 20] = [
        0x43, 0x0c, 0xe3, 0x4d, 0x02, 0x07, 0x24, 0xed, 0x75, 0xa1, 0x96, 0xdf, 0xc2, 0xad, 0x67,
        0xc7, 0x77, 0x72, 0xd1, 0x69,
    ];
    let modulus: [u8; 64] = [
        0x96, 0x9D, 0x03, 0xFF, 0xA9, 0x8D, 0x88, 0x8F, 0x3A, 0xA4, 0xF2, 0xFE, 0xD2, 0x32, 0xE6,
        0x1C, 0x4A, 0xCF, 0x06, 0x63, 0xA9, 0x2F, 0x99, 0x03, 0x4C, 0xF7, 0xB7, 0x24, 0x5A, 0x1A,
        0x1E, 0x5E, 0xAF, 0xA5, 0x65, 0xAF, 0xB9, 0x0B, 0xAB, 0x22, 0x85, 0x71, 0x2F, 0xAA, 0x50,
        0x39, 0x39, 0xA0, 0x65, 0xFB, 0x60, 0xDD, 0x08, 0x28, 0xA3, 0x84, 0xF2, 0x6D, 0x8A, 0xFC,
        0x28, 0x6D, 0xF6, 0xCF,
    ];
    let signature: [u8; 64] = [
        0x45, 0x53, 0xF3, 0xAF, 0x16, 0xAF, 0x63, 0x97, 0xB0, 0xD3, 0x2F, 0x8A, 0xEC, 0xD5, 0x4C,
        0xF1, 0xF3, 0xD0, 0x0C, 0x9F, 0x42, 0xDC, 0x68, 0xCB, 0xD7, 0x05, 0xCE, 0xA5, 0xA9, 0x70,
        0x95, 0x3E, 0xC0, 0xBC, 0x4A, 0x18, 0xED, 0x91, 0xA3, 0x5D, 0x66, 0xEC, 0xDA, 0x4A, 0x83,
        0x32, 0xCF, 0xC3, 0xA3, 0xAB, 0x21, 0xAD, 0x59, 0xB2, 0x2E, 0x87, 0xC2, 0x73, 0xFF, 0x08,
        0x88, 0xDD, 0x4D, 0xE0,
    ];

    let key = public_key_from_be_bytes::<U512>(&modulus, 3).expect("public key");
    let encoded_message = rsa_public_op(&key, &signature).expect("rsa public op");
    let verifying_key = GenericVerifyingKey::<Sha1, _, _>::new(key);
    let signature =
        GenericSignature::from(ModMathValue::from_inner(U512::from_be_bytes(&signature)));
    verifying_key
        .verify_prehash(&digest, &signature)
        .map_err(|_| rsa::Error::Verification)
        .expect("pkcs1v15 verify");

    println!("message: {}", core::str::from_utf8(data).unwrap());
    println!("raw decrypted signature block:");
    for byte in encoded_message.as_ref() {
        print!("{byte:02x}");
    }
    println!();
    println!("verification: ok");
}