Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr. Capybara committed Oct 17, 2023
1 parent 97dae51 commit 929b2bc
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 18 deletions.
64 changes: 63 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,73 @@ This library uses an FFI to GMP by means of the rug crate. To successfully build
apt-get install m4
```

## Quick Start

### Schnorr Signatures:
```rust
use capycrypt::{KeyPair, Message, Signable};
// Get random 5mb
let mut msg = Message::new(&mut get_random_bytes(5242880));
// Get a random password
let pw = get_random_bytes(64);
// Generate a public/private keypair
let mut key_pair = KeyPair::new(&pw, "test key".to_string(), E448, 512);

// Sign the message with the private key
msg.sign(&mut key_pair, 512);
// Verify the message with the public key
msg.verify(key_pair.pub_key, 512);
assert!(msg.op_result.unwrap());
```

### Compute Digest:
```rust
use capycrypt::{Hashable, Message};
// Hash the empty string
let mut data = Message::new(&mut 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);
```

### Symmetric Encrypt/Decrypt:
```rust
use capycrypt::{Message, PwEncryptable};

let pw = get_random_bytes(64); // Get a random password
let mut msg = Message::new(&mut get_random_bytes(5242880)); // Get 5mb random data

msg.pw_encrypt(&mut pw.clone(), 512); // Encrypt the data with 512 bits of security
msg.pw_decrypt(&mut pw.clone(), 512); // Decrypt the data

assert!(msg.op_result.unwrap()); // Verify operation success
```

### Asymmetric Encrypt/Decrypt:
```rust
use capycrypt::{Message, KeyEncryptable};
// 5mb random data
let mut msg = Message::new(&mut get_random_bytes(5242880));
// Generate a private/public keypair
let key_pair = KeyPair::new(&get_random_bytes(32), "test key".to_string(), E448, 256);

// Encrypt with public key with 256 bits of security
msg.key_encrypt(&key_pair.pub_key, 256);
// Decrypt with private key
msg.key_decrypt(&key_pair.priv_key, 256);
// Verify correct decryption
assert!(msg.op_result.unwrap());
```



## Benches
This library uses the criterion crate for benches. Running:
```bash
cargo bench
```
Conducts benchmarks in order from lowest security to highest. For example, the lowest security configuration available in this library is the pairing of E222 with cSHAKE256, while the highest security offered is E521 paired with cSHAKE512.
conducts benchmarks in order from lowest security to highest. For example, the lowest security configuration available in this library is the pairing of E222 with cSHAKE256, while the highest security offered is E521 paired with cSHAKE512.

I make no claims as to the security of this library. It probably shouldn't be used for anything serious. If you find cool ways to make it better, open a PR and I'll gladly engage.
8 changes: 4 additions & 4 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,20 +350,20 @@ impl KeyEncryptable for Message {
impl Signable for Message {
/// # Schnorr Signatures
/// Generates a signature for a byte array m under passphrase pw.
///
///
/// ## Algorithm:
/// * `s` ← kmac_xof(pw, “”, 512, “K”); s ← 4s
/// * `k` ← kmac_xof(s, m, 512, “N”); k ← 4k
/// * `𝑈` ← k*𝑮;
/// * `ℎ` ← kmac_xof(𝑈ₓ , m, 512, “T”); 𝑍 ← (𝑘 – ℎ𝑠) mod r
///
///
/// ## Arguments:
/// * `key: &mut 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
/// ```
/// ```
Expand Down
14 changes: 4 additions & 10 deletions tests/ops_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ pub mod ops_tests {
msg.pw_encrypt(&mut pw.clone(), 512);
msg.pw_decrypt(&mut pw.clone(), 512);

let res = msg.op_result.unwrap();
assert!(res);
assert!(msg.op_result.unwrap());
}
#[test]
pub fn test_sym_enc_256() {
Expand All @@ -24,33 +23,28 @@ pub mod ops_tests {
msg.pw_encrypt(&mut pw.clone(), 256);
msg.pw_decrypt(&mut pw.clone(), 256);

let res = msg.op_result.unwrap();
assert!(res);
assert!(msg.op_result.unwrap());
}
#[test]
fn test_key_gen_enc_dec_256() {
//check conversion to and from bytes.
let mut msg = Message::new(&mut get_random_bytes(5242880));
let key_pair = KeyPair::new(&get_random_bytes(32), "test key".to_string(), E448, 256);

msg.key_encrypt(&key_pair.pub_key, 256);
msg.key_decrypt(&key_pair.priv_key, 256);

let res = msg.op_result.unwrap();
assert!(res);
assert!(msg.op_result.unwrap());
}

#[test]
fn test_key_gen_enc_dec_512() {
//check conversion to and from bytes.
let mut msg = Message::new(&mut get_random_bytes(5242880));
let key_pair = KeyPair::new(&get_random_bytes(32), "test key".to_string(), E448, 512);

msg.key_encrypt(&key_pair.pub_key, 512);
msg.key_decrypt(&key_pair.priv_key, 512);

let res = msg.op_result.unwrap();
assert!(res);
assert!(msg.op_result.unwrap());
}
#[test]
pub fn test_signature_512() {
Expand Down
5 changes: 2 additions & 3 deletions tests/sponge_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use capycrypt::{Hashable, Message};

/// Test cases for cSHAKE and KMAC functionality. All values labeled
/// "exptected" in cshake and kmac tests are official test vectors supplied by NIST.
#[cfg(test)]
Expand Down Expand Up @@ -157,7 +156,7 @@ mod sponge_tests {
}
#[test]
fn test_shake_224() {
let mut data = Message::new(&mut Box::new("".as_bytes().to_owned()));
let mut data = Message::new(&mut vec![]);
let expected = "6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7";
data.compute_sha3_hash(224);
assert!(hex::encode(data.digest.unwrap().to_vec()) == expected);
Expand All @@ -170,7 +169,7 @@ fn test_shake_224() {

#[test]
fn test_shake_256() {
let mut data = Message::new(&mut Box::new("".as_bytes().to_owned()));
let mut data = Message::new(&mut vec![]);
let expected = "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a";
data.compute_sha3_hash(256);
assert!(hex::encode(data.digest.unwrap().to_vec()) == expected);
Expand Down

0 comments on commit 929b2bc

Please sign in to comment.