Expand description

The asymmetric encryption context.

Examples

Encrypt data with RSA

use openssl::rsa::Rsa;
use openssl::pkey::PKey;
use openssl::pkey_ctx::PkeyCtx;

let key = Rsa::generate(4096).unwrap();
let key = PKey::from_rsa(key).unwrap();

let mut ctx = PkeyCtx::new(&key).unwrap();
ctx.encrypt_init().unwrap();

let data = b"Some Crypto Text";
let mut ciphertext = vec![];
ctx.encrypt_to_vec(data, &mut ciphertext).unwrap();


Generate a CMAC key

use openssl::pkey_ctx::PkeyCtx;
use openssl::pkey::Id;
use openssl::cipher::Cipher;

let mut ctx = PkeyCtx::new_id(Id::CMAC).unwrap();
ctx.keygen_init().unwrap();
ctx.set_keygen_cipher(Cipher::aes_128_cbc()).unwrap();
ctx.set_keygen_mac_key(b"0123456789abcdef").unwrap();
let cmac_key = ctx.keygen().unwrap();

Sign and verify data with RSA

use openssl::pkey_ctx::PkeyCtx;
use openssl::pkey::PKey;
use openssl::rsa::Rsa;

// Generate a random RSA key.
let key = Rsa::generate(4096).unwrap();
let key = PKey::from_rsa(key).unwrap();

let text = b"Some Crypto Text";

// Create the signature.
let mut ctx = PkeyCtx::new(&key).unwrap();
ctx.sign_init().unwrap();
let mut signature = vec![];
ctx.sign_to_vec(text, &mut signature).unwrap();

// Verify the signature.
let mut ctx = PkeyCtx::new(&key).unwrap();
ctx.verify_init().unwrap();
let valid = ctx.verify(text, &signature).unwrap();
assert!(valid);

Structs

HKDF modes of operation.
A context object which can perform asymmetric cryptography operations.
A reference to a PkeyCtx.