pub struct Message {
pub msg: Box<Vec<u8>>,
pub d: Option<u64>,
pub sym_nonce: Option<Vec<u8>>,
pub asym_nonce: Option<EdCurvePoint>,
pub digest: Option<Vec<u8>>,
pub op_result: Option<bool>,
pub sig: Option<Signature>,
}Expand description
Message type for which cryptographic traits are defined.
Fields§
§msg: Box<Vec<u8>>§d: Option<u64>§sym_nonce: Option<Vec<u8>>§asym_nonce: Option<EdCurvePoint>§digest: Option<Vec<u8>>§op_result: Option<bool>§sig: Option<Signature>Implementations§
Trait Implementations§
source§impl Hashable for Message
impl Hashable for Message
source§fn compute_sha3_hash(&mut self, d: u64)
fn compute_sha3_hash(&mut self, d: u64)
Message Digest
Computes SHA3-d hash of input. Does not consume input.
Replaces Message.digest with result of operation.
Arguments:
d: u64: requested security strength in bits. Supported bitstrengths are 224, 256, 384, or 512.
Usage:
use capycrypt::{Hashable, Message};
// Hash the empty string
let mut data = Message::new(vec![]);
// Obtained from OpenSSL
let expected = "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a";
// Compute a SHA3 digest with 256 bits of security
data.compute_sha3_hash(256);
assert!(hex::encode(data.digest.unwrap().to_vec()) == expected);source§fn compute_tagged_hash(&mut self, pw: &mut Vec<u8>, s: &str, d: u64)
fn compute_tagged_hash(&mut self, pw: &mut Vec<u8>, s: &str, d: u64)
Tagged Hash
Computes an authentication tag t of a byte array m under passphrase pw.
Replaces:
Message.twith keyed hash of plaintext.
Arguments:
pw: &mut Vec<u8>: symmetric encryption key, can be blank but shouldnt bemessage: &mut Vec<u8>: message to encrypts: &mut str: domain seperation stringd: u64: requested security strength in bits. Supported bitstrengths are 224, 256, 384, or 512.
Usage:
use capycrypt::{Hashable, Message};
let mut pw = "test".as_bytes().to_vec();
let mut data = Message::new(vec![]);
let expected = "0f9b5dcd47dc08e08a173bbe9a57b1a65784e318cf93cccb7f1f79f186ee1caeff11b12f8ca3a39db82a63f4ca0b65836f5261ee64644ce5a88456d3d30efbed";
data.compute_tagged_hash(&mut pw, &"", 512);
assert!(hex::encode(data.digest.unwrap().to_vec()) == expected);source§impl KeyEncryptable for Message
impl KeyEncryptable for Message
source§fn key_encrypt(&mut self, pub_key: &EdCurvePoint, d: u64)
fn key_encrypt(&mut self, pub_key: &EdCurvePoint, d: u64)
Asymmetric Encryption
Encrypts a Message in place under the (Schnorr/ECDHIES) public key 𝑉.
Operates under Schnorr/ECDHIES principle in that shared symmetric key is
exchanged with recipient. SECURITY NOTE: ciphertext length == plaintext length
Replaces:
Message.datawith result of encryption.Message.twith keyed hash of plaintext.Message.asym_noncewith z, as defined below.
Algorithm:
- k ← Random(512); k ← 4k
- W ← kV; 𝑍 ← k*𝑮
- (ke || ka) ← kmac_xof(W x , “”, 1024, “P”)
- c ← kmac_xof(ke, “”, |m|, “PKE”) ⊕ m
- t ← kmac_xof(ka, m, 512, “PKA”)
Arguments:
- pub_key:
EdCurvePoint: X coordinate of public key 𝑉 - d: u64: Requested security strength in bits. Can only be 224, 256, 384, or 512.
Usage:
use capycrypt::{
KeyEncryptable,
KeyPair,
Message,
sha3::aux_functions::byte_utils::get_random_bytes,
curves::EdCurves::E448};
// Get 5mb random data
let mut msg = Message::new(get_random_bytes(5242880));
// Generate the keypair
let key_pair = KeyPair::new(&get_random_bytes(32), "test key".to_string(), E448, 512);
// Encrypt with the public key
msg.key_encrypt(&key_pair.pub_key, 512);source§fn key_decrypt(&mut self, pw: &[u8])
fn key_decrypt(&mut self, pw: &[u8])
Asymmetric Decryption
Decrypts a Message in place under private key.
Operates under Schnorr/ECDHIES principle in that shared symmetric key is
derived from 𝑍.
Assumes:
- well-formed encryption
- Some(Message.t)
- Some(Message.z)
Replaces:
Message.datawith result of decryption.Message.op_resultwith result of comparision ofMessage.t== keyed hash of decryption.
Algorithm:
- s ← KMACXOF256(pw, “”, 512, “K”); s ← 4s
- W ← sZ
- (ke || ka) ← KMACXOF256(W x , “”, 1024, “P”)
- m ← KMACXOF256(ke, “”, |c|, “PKE”) ⊕ c
- t’ ← KMACXOF256(ka, m, 512, “PKA”)
Arguments:
- pw: &u8: password used to generate
CurvePointencryption key. - d: u64: encryption security strength in bits. Can only be 224, 256, 384, or 512.
Usage:
use capycrypt::{
KeyEncryptable,
KeyPair,
Message,
sha3::aux_functions::byte_utils::get_random_bytes,
curves::EdCurves::E448};
// Get 5mb random data
let mut msg = Message::new(get_random_bytes(5242880));
// Create a new private/public keypair
let key_pair = KeyPair::new(&get_random_bytes(32), "test key".to_string(), E448, 512);
// Encrypt the message
msg.key_encrypt(&key_pair.pub_key, 512);
//Decrypt the message
msg.key_decrypt(&key_pair.priv_key);
// Verify
assert!(msg.op_result.unwrap());source§impl PwEncryptable for Message
impl PwEncryptable for Message
source§fn pw_encrypt(&mut self, pw: &[u8], d: u64)
fn pw_encrypt(&mut self, pw: &[u8], d: u64)
Symmetric Encryption
Encrypts a Message m symmetrically under passphrase pw.
Replaces:
Message.datawith result of encryption.Message.twith keyed hash of plaintext.Message.sym_noncewith z, as defined below. SECURITY NOTE: ciphertext length == plaintext length
Algorithm:
- z ← Random(512)
- (ke || ka) ← kmac_xof(z || pw, “”, 1024, “S”)
- c ← kmac_xof(ke, “”, |m|, “SKE”) ⊕ m
- t ← kmac_xof(ka, m, 512, “SKA”)
Arguments:
pw: &[u8]: symmetric encryption key, can be blank but shouldnt bed: u64: requested security strength in bits. Supported bitstrengths are 224, 256, 384, or 512.
Usage:
use capycrypt::{
Message,
PwEncryptable,
sha3::{aux_functions::{byte_utils::{get_random_bytes}}}
};
// Get a random password
let pw = get_random_bytes(64);
// Get 5mb random data
let mut msg = Message::new(get_random_bytes(5242880));
// Encrypt the data with 512 bits of security
msg.pw_encrypt(&pw, 512);
// Decrypt the data
msg.pw_decrypt(&pw);
// Verify operation success
assert!(msg.op_result.unwrap());source§fn pw_decrypt(&mut self, pw: &[u8])
fn pw_decrypt(&mut self, pw: &[u8])
Symmetric Decryption
Decrypts a Message (z, c, t) under passphrase pw.
Assumes:
- well-formed encryption
- Some(Message.t)
- Some(Message.z)
Replaces:
Message.datawith result of decryption.Message.op_resultwith result of comparision ofMessage.t== keyed hash of decryption.
Algorithm:
- (ke || ka) ← kmac_xof(z || pw, “”, 1024, “S”)
- m ← kmac_xof(ke, “”, |c|, “SKE”) ⊕ c
- t’ ← kmac_xof(ka, m, 512, “SKA”)
Arguments:
pw: &[u8]: decryption password, can be blank
Usage:
use capycrypt::{
Message,
PwEncryptable,
sha3::{aux_functions::{byte_utils::{get_random_bytes}}}
};
// Get a random password
let pw = get_random_bytes(64);
// Get 5mb random data
let mut msg = Message::new(get_random_bytes(5242880));
// Encrypt the data with 512 bits of security
msg.pw_encrypt(&pw, 512);
// Decrypt the data
msg.pw_decrypt(&pw);
// Verify operation success
assert!(msg.op_result.unwrap());source§impl Signable for Message
impl Signable for Message
source§fn sign(&mut self, key: &KeyPair, d: u64)
fn sign(&mut self, key: &KeyPair, d: u64)
Schnorr Signatures
Signs a Message under passphrase pw.
Algorithm:
s← kmac_xof(pw, “”, 512, “K”); s ← 4sk← kmac_xof(s, m, 512, “N”); k ← 4k𝑈← k*𝑮;ℎ← kmac_xof(𝑈ₓ , m, 512, “T”); 𝑍 ← (𝑘 – ℎ𝑠) mod r
Arguments:
- key: &
KeyPair, : reference to KeyPair. - d: u64: encryption security strength in bits. Can only be 224, 256, 384, or 512.
Assumes:
- Some(key.priv_key)
Usage
use capycrypt::{
Signable,
KeyPair,
Message,
sha3::aux_functions::byte_utils::get_random_bytes,
curves::EdCurves::E448};
// Get random 5mb
let mut msg = Message::new(get_random_bytes(5242880));
// Get a random password
let pw = get_random_bytes(64);
// Generate a signing keypair
let key_pair = KeyPair::new(&pw, "test key".to_string(), E448, 512);
// Sign with 512 bits of security
msg.sign(&key_pair, 512);source§fn verify(&mut self, pub_key: &EdCurvePoint)
fn verify(&mut self, pub_key: &EdCurvePoint)
Signature Verification
Verifies a Signature (h, 𝑍) for a byte array m under the (Schnorr/
ECDHIES) public key 𝑉.
Algorithm:
- 𝑈 ← 𝑍*𝑮 + h𝑉
Arguments:
- sig: &
Signature: Pointer to a signature object (h, 𝑍) - pubKey: CurvePoint key 𝑉 used to sign message m
- message: Vec
of message to verify
Assumes:
- Some(key.pub_key)
- Some(
Message.sig)
Usage
use capycrypt::{
Signable,
KeyPair,
Message,
sha3::aux_functions::byte_utils::get_random_bytes,
curves::EdCurves::E448};
// Get random 5mb
let mut msg = Message::new(get_random_bytes(5242880));
// Get a random password
let pw = get_random_bytes(64);
// Generate a signing keypair
let key_pair = KeyPair::new(&pw, "test key".to_string(), E448, 512);
// Sign with 512 bits of security
msg.sign(&key_pair, 512);
// Verify
msg.verify(&key_pair.pub_key);
assert!(msg.op_result.unwrap());