Skip to content
carl-tudPublic

About

Dangerous constant-time, typed cryptography baked at home, for demonstration purposes only.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

74 Commits

Folders and files

Repository files navigation

Curve25519/X25519, Ed25519, and SIGMA Implementations

Warning

This is home-baked cryptography. Very unsafe and not fast. Use at your own risk. This crate is merely supposed to illustrate how select cryptographic algorithms work. While implementing this for P79, performance was a subordinate goal, with clarity being the primary aim.

This repo implements the X25519 key agreement scheme in two ways:

  1. Double-and-add mechanism using group law (lacks support for points on the quadratic twist)
  2. Montgomery ladder

In addition to crypteng::ecc25519::dh, Ed25519 is implemented in crypteng::ecc25519::signing.

Both implementations aim to run in constant time and zero memory used during computations afterwards. The library introduces separate types for different keys and secrets to ensure safe use and to erase keys from memory once they've been dropped. Again, the logic in this repo has neither been formally verified, nor are the zeroing and constant-time claims.

For constant-time ops, this crate uses subtle (crates.io). Memory is erased using zeroize (crates.io).

This repo also implements a small-scale PKI system (without identity verification when issuing certification), and an implementation of the SIGMA protocol without identitify protection.

Building

cargo build

Running the demo

cargo run

Running tests

cargo test

Examples

X25519

cargo add sha2
use crypteng::{
    ecc25519::dh
};

// Alice computes key pair
let alice_private_key = dh::PrivateKey::new();
let alice_public_key = alice_private_key.public_key::<dh::MontgomeryLadder>();
let alice_public_key_bytes: [u8; _] = alice_public_key.bytes().into();

// Bob generates key pair too
let bob_private_key = dh::PrivateKey::new();
let bob_public_key = bob_private_key.public_key::<dh::MontgomeryLadder>();
let bob_public_key_bytes: [u8; _] = bob_public_key.bytes().into();

// ...
// Alice send public key bytes to bob
// Bob sends public key bytes to alice

// Alice computes shared secret
let alice_shared_secret = 
    dh::agree_on_shared_secret::<dh::MontgomeryLadder>(
        &dh::PublicKey::from_bytes(bob_public_key_bytes), 
        &alice_private_key
    ).expect("X25519 DH key exchange failed (Alice's side)");

// Bob computes shared secret
let bob_shared_secret = 
    dh::agree_on_shared_secret::<dh::MontgomeryLadder>(
        &dh::PublicKey::from_bytes(alice_public_key_bytes), 
        &bob_private_key
    ).expect("X25519 DH key exchange failed (Bob's side)");

assert_eq!(alice_shared_secret, bob_shared_secret);
// Wohoo!

let symmetric_shared_key = 
    alice_shared_secret.derive_key_hkdf::<sha2::Sha512>(None, b"I am a sketchy info string")
        .expect("HKDF-SHA512 on DH shared secret failed");

Ed25519

use crypteng::{
    ecc25519::signing
};

let private_key = signing::PrivateKey::new().unwrap();
let public_key = private_key.public_key();

let message = b"Houston, we've had a problem!";
let signature = private_key.signature(message);

let _isValid = public_key.verify(message, signature);

fn verify_from_bytes(public_key_slice: &[u8], message: &[u8], signature: &[u8]) -> bool {
  let public_key: signing::PublicKey = public_key_slice.try_into().expect("public key not 32 bytes long");
  public_key.verify(message, signature)
}

let _isValid2 = verify_from_bytes(&public_key, message, &signature);

assert!(_isValid);
assert!(_isValid2);

Secure Channel (SIGMA implementation)

// Two parties A and B have their own certificate
let (cert_a, sk_a) = issue_cert("084E8A190DBF4484983FEDE4B2CF69AC", "A");
let (cert_b, sk_b) = issue_cert("094E8A190DBF4484983FEDE4B2CF69AC", "B");

// Both parties retrieve trusted certificates from an online service and save them in a local
// trust store.
let mut trust_a = pki::TrustStore::new();
trust_a.add(&cert_b);

let mut trust_b = pki::TrustStore::new();
trust_b.add(&cert_a);

// Each party creates a Rendezvous with their role, presenting a chain of certificates
// that let's anyone with access to the PKI services verify their certificate, i.e.,
// the chain needs to end in a trusted certificate and each certificate needs to be able
// to verify the previous one.
let mut rendezvous_a = secure_channel::Rendezvous::<_, Initiator>::new(
    pki::CertificateChain { certificate: &cert_a, intermediates: &[] }, 
    &sk_a
).unwrap();

let mut rendezvous_b= secure_channel::Rendezvous::<_, Target>::new(
    pki::CertificateChain { certificate: &cert_b, intermediates: &[] }, 
    &sk_b
).unwrap();

// Let's simulate the network using a simple buffer.
let mut network = Vec::<u8>::new();

// The secure channel rendezvous involves three messages:
//  - the Hello message from the initiator
//  - an Introduction message from the target to the initiator
//  - an Introduction message from the initiator to the target

// A sends Hello message
rendezvous_a.build_hello(&mut network).unwrap();

// B processes Hello message and sends introduction
rendezvous_b.process_hello(network.drain(..)).unwrap();
rendezvous_b.build_target_introduction(&mut network).unwrap();

// A processes B's introduction and sends intoduction
rendezvous_a.process_target_introduction(network.drain(..), &trust_a).unwrap();
let session_a = rendezvous_a.build_initiator_introduction(&mut network).unwrap();

// B processes A's introduction.
let session_b =  rendezvous_b.process_initiator_introduction(network.drain(..), &trust_b).unwrap();

// Let A send an encrypted datagram to B. The secure channel impl supports 
// ChaCha20Poly1305 and AES-GCM, both using a 256-bit key.
use cose_minicbor::structs::CoseAlg;
session_a.encrypt(
    b"Secret message", 
    CoseAlg::ChaCha20Poly1305, 
    &mut network
).unwrap();

// B should see the same message that was sent
let decrypted = session_b.decrypt(network.drain(..)).unwrap();
println!("Decrypted channel datagram: '{}'", str::from_utf8(&decrypted).unwrap());

About

Dangerous constant-time, typed cryptography baked at home, for demonstration purposes only.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages