Skip to main content

define_rsa_keypair

Macro define_rsa_keypair 

Source
macro_rules! define_rsa_keypair {
    ($(#[ $docs_and_attrs:meta ])*
     $vis:vis $base_name:ident) => { ... };
}
Expand description

Create an RSA keypair wrapper given a visibility and a struct name.

§Syntax:

define_rsa_keypair(<visibility> <prefix>)

This macro creates a struct tuple named <prefix>Keypair which contains the lower-level cryptographic keypair for an RSA keypair. It derives the deftly RsaKeypair template which in turn creates <prefix>PublicKey along a series of useful methods.

The keypair is NOT clonable by design in order to avoid duplicating secret key material.

§Example:

use tor_key_forge::define_rsa_keypair;

define_rsa_keypair!(NonPublicSigning);
define_rsa_keypair!(pub PublicSigning);
define_rsa_keypair!(pub(crate) CratePublicSigning);

The above results in NonPublicSigningKeypair and NonPublicSigningPublicKey struct being created and usable with a series of useful methods. Same for the other defines.

You can then use these objects like so:

use rand::Rng;
use tor_key_forge::Keygen;
use tor_key_forge::define_rsa_keypair;
use tor_llcrypto::pk::ValidatableSignature;
use tor_llcrypto::pk::rsa::KeyPair;

define_rsa_keypair!(
    /// Our signing key.
    MyRsa
);

let signing_kp = MyRsaKeypair::generate(&mut rng).expect("Invalid keygen");
let signing_pubkey = signing_kp.public();
// Lets sign this wonderful message.
let message = "Workers want rights, not your opinion".as_bytes();
let sig = signing_kp.sign(&message).expect("Error signing message");

// You can then verify either directly with the keypair or the public key.
assert!(signing_kp.verify(&sig, &message));
assert!(signing_pubkey.verify(&sig, &message));