signcryption 0.1.3

Library implementing the Toorani-Beheshti signcryption scheme instantiated over Ristretto255 or Ed25519
Documentation
use signcryption::*;
use rand::{rngs::OsRng, RngCore};
use libsodium_sys::crypto_generichash_state as HashState;

const SENDER_ID: [u8; 6] = *b"sender";
const RECIPIENT_ID: [u8; 9] = *b"recipient";
const INFO: [u8; 24] = [88u8; 24];

#[test]
fn before_valid() {
  for curve in [Curve::Ed25519, Curve::Ristretto255] {
    let mut state = SignState::default();
    let mut crypt_key = [0u8; SHAREDBYTES];
    let mut info = [0u8; 24];
    
    let alice = Keypair::new(curve);
    let bob = Keypair::new(curve);
    let msg = b"test";
  
    OsRng.fill_bytes(&mut info);
    sign_before(
      &mut state, &mut crypt_key, &SENDER_ID, &RECIPIENT_ID, &INFO, 
      &alice.expose_secret(), &bob.public, msg, curve
    ).unwrap();
  }
}

#[test]
fn before_invalid_incompatible_curve() {
  let mut state = SignState::default();
  let mut crypt_key = [0u8; SHAREDBYTES];
  let mut info = [0u8; 24];
  
  let alice = Keypair::new(Curve::Ed25519);
  let bob = Keypair::new(Curve::Ed25519);
  let msg = b"test";

  OsRng.fill_bytes(&mut info);
  assert!(sign_before(
    &mut state, &mut crypt_key, &SENDER_ID, &RECIPIENT_ID, &INFO, 
    &alice.expose_secret(), &bob.public, msg, Curve::Ristretto255
  ).is_err());
}

#[test]
fn before_invalid_oversized_aad() {
  let mut state = SignState::default();
  let mut crypt_key = [0u8; SHAREDBYTES];
  let mut info = [0u8; 256]; //oversize
  
  let alice = Keypair::new(Curve::Ed25519);
  let bob = Keypair::new(Curve::Ed25519);
  let msg = b"test";

  OsRng.fill_bytes(&mut info);
  let res = sign_before(
    &mut state, &mut crypt_key, &SENDER_ID, &RECIPIENT_ID, &info,
    &alice.expose_secret(), &bob.public, msg, Curve::Ed25519
  );
  assert_eq!(res, Err(SignCryptError::InvalidLength));
}

#[test]
fn after_valid_ristretto255() {
  let mut state = SignState{
    h: HashState {opaque: [0x48, 0xc9, 0xbd, 0xf2, 0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xfe, 0xac, 0x85, 0xda, 0x84, 0x8e, 0x8, 0xcf, 0xee, 0x4e, 0x2a, 0xde, 0x5, 0x3f, 0x92, 0xe, 0x99, 0x6,],
    r: [0xf6, 0x2b, 0x20, 0x3a, 0x4c, 0x8, 0xcc, 0x21, 0xc, 0x94, 0xea, 0xbb, 0xe5, 0x4e, 0x75, 0xc2, 0xce, 0x75, 0xfd, 0xe1, 0x74, 0x4, 0xc2, 0xd9, 0x1b, 0x8, 0xa6, 0xb0, 0x2a, 0x1, 0xdd, 0x3a,],
    challenge: [0u8; 32],
  };
  let sk: [u8; 32] = [79, 226, 166, 132, 224, 230, 197, 227, 112, 202, 13, 137, 245, 226, 203, 13, 161, 226, 236, 212, 2, 143, 162, 211, 149, 251, 202, 78, 51, 242, 88, 5];
  let expected_sig: [u8; SIGNBYTES] = [246, 43, 32, 58, 76, 8, 204, 33, 12, 148, 234, 187, 229, 78, 117, 194, 206, 117, 253, 225, 116, 4, 194, 217, 27, 8, 166, 176, 42, 1, 221, 58, 191, 198, 30, 209, 52, 151, 103, 22, 65, 187, 184, 11, 55, 118, 250, 171, 158, 163, 162, 49, 215, 185, 221, 126, 203, 190, 249, 18, 21, 127, 32, 5];
  let mut sig = [0u8; SIGNBYTES];
  let ciphertext = [0xff, 0xe9, 0x60, 0x5e, 0x12, 0xae, 0xbc, 0xb1, 0x7d, 0xab, 0x9d, 0x11, 0xbc, 0x9, 0x97, 0xba, 0xc5, 0x44, 0x4d, 0xc5];
  
  sign_after(&mut state, &mut sig, &sk, &ciphertext, Curve::Ristretto255);
  assert_eq!(sig, expected_sig);
}

#[test]
fn after_invalid_corrupted_hashstate_ristretto255() {
  let mut state = SignState{
    // Corrupted first byte of hash state 
    h: HashState {opaque: [0x00, 0xc9, 0xbd, 0xf2, 0x67, 0xe6, 0x9, 0x6a, 0x3b, 0xa7, 0xca, 0x84, 0x85, 0xae, 0x67, 0xbb, 0x2b, 0xf8, 0x94, 0xfe, 0x72, 0xf3, 0x6e, 0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 0x2b, 0x20, 0x3a, 0x4c, 0x8, 0xcc, 0x21, 0xc, 0x94, 0xea, 0xbb, 0xe5, 0x4e, 0x75, 0xc2, 0xce, 0x75, 0xfd, 0xe1, 0x74, 0x4, 0xc2, 0xd9, 0x1b, 0x8, 0xa6, 0xb0, 0x2a, 0x1, 0xdd, 0x3a,],
    challenge: [0u8; 32],
  };
  let sk: [u8; 32] = [79, 226, 166, 132, 224, 230, 197, 227, 112, 202, 13, 137, 245, 226, 203, 13, 161, 226, 236, 212, 2, 143, 162, 211, 149, 251, 202, 78, 51, 242, 88, 5];
  let expected_sig: [u8; SIGNBYTES] = [246, 43, 32, 58, 76, 8, 204, 33, 12, 148, 234, 187, 229, 78, 117, 194, 206, 117, 253, 225, 116, 4, 194, 217, 27, 8, 166, 176, 42, 1, 221, 58, 191, 198, 30, 209, 52, 151, 103, 22, 65, 187, 184, 11, 55, 118, 250, 171, 158, 163, 162, 49, 215, 185, 221, 126, 203, 190, 249, 18, 21, 127, 32, 5];
  let mut sig = [0u8; SIGNBYTES];
  let ciphertext = [0xff, 0xe9, 0x60, 0x5e, 0x12, 0xae, 0xbc, 0xb1, 0x7d, 0xab, 0x9d, 0x11, 0xbc, 0x9, 0x97, 0xba, 0xc5, 0x44, 0x4d, 0xc5];
  
  sign_after(&mut state, &mut sig, &sk, &ciphertext, Curve::Ristretto255);
  assert_ne!(sig, expected_sig);
}