1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// crypto_sign_ed25519.h
pub const crypto_sign_ed25519_BYTES: usize = 64;
pub const crypto_sign_ed25519_SEEDBYTES: usize = 32;
pub const crypto_sign_ed25519_PUBLICKEYBYTES: usize = 32;
pub const crypto_sign_ed25519_SECRETKEYBYTES: usize = 64;

#[repr(C)]
#[derive(Copy)]
pub struct crypto_sign_ed25519ph_state {
    hs: crypto_hash_sha512_state,
}
impl Clone for crypto_sign_ed25519ph_state { fn clone(&self) -> crypto_sign_ed25519ph_state { *self } }

extern {
    pub fn crypto_sign_ed25519_keypair(
        pk: *mut [u8; crypto_sign_ed25519_PUBLICKEYBYTES],
        sk: *mut [u8; crypto_sign_ed25519_SECRETKEYBYTES]) -> c_int;
    pub fn crypto_sign_ed25519_seed_keypair(
        pk: *mut [u8; crypto_sign_ed25519_PUBLICKEYBYTES],
        sk: *mut [u8; crypto_sign_ed25519_SECRETKEYBYTES],
        seed: *const [u8; crypto_sign_ed25519_SEEDBYTES]) -> c_int;
    pub fn crypto_sign_ed25519(
        sm: *mut u8,
        smlen: *mut c_ulonglong,
        m: *const u8,
        mlen: c_ulonglong,
        sk: *const [u8; crypto_sign_ed25519_SECRETKEYBYTES]) -> c_int;
    pub fn crypto_sign_ed25519_open(
        m: *mut u8,
        mlen: *mut c_ulonglong,
        sm: *const u8,
        smlen: c_ulonglong,
        pk: *const [u8; crypto_sign_ed25519_PUBLICKEYBYTES]) -> c_int;
    pub fn crypto_sign_ed25519_detached(
        sig: *mut [u8; crypto_sign_ed25519_BYTES],
        siglen: *mut c_ulonglong,
        m: *const u8,
        mlen: c_ulonglong,
        sk: *const [u8; crypto_sign_ed25519_SECRETKEYBYTES]) -> c_int;
    pub fn crypto_sign_ed25519_verify_detached(
        sig: *const [u8; crypto_sign_ed25519_BYTES],
        m: *const u8,
        mlen: c_ulonglong,
        pk: *const [u8; crypto_sign_ed25519_PUBLICKEYBYTES]) -> c_int;
    pub fn crypto_sign_ed25519_bytes() -> size_t;
    pub fn crypto_sign_ed25519_seedbytes() -> size_t;
    pub fn crypto_sign_ed25519_publickeybytes() -> size_t;
    pub fn crypto_sign_ed25519_secretkeybytes() -> size_t;

    pub fn crypto_sign_ed25519ph_init(state: *mut crypto_sign_ed25519ph_state) -> c_int;
    pub fn crypto_sign_ed25519ph_update(
        state: *mut crypto_sign_ed25519ph_state,
        m: *const u8,
        mlen: c_ulonglong) -> c_int;
    pub fn crypto_sign_ed25519ph_final_create(state: *mut crypto_sign_ed25519ph_state,
        sig: *const u8,
        siglen: *mut c_ulonglong,
        sk: *const [u8; crypto_sign_ed25519_SECRETKEYBYTES]) -> c_int;
    pub fn crypto_sign_ed25519ph_final_verify(state: *mut crypto_sign_ed25519ph_state,
        sig: *const u8,
        pk: *const [u8; crypto_sign_ed25519_PUBLICKEYBYTES]) -> c_int;

    pub fn crypto_sign_ed25519_pk_to_curve25519(curve25519_sk: *mut [u8; crypto_scalarmult_curve25519_BYTES],
        ed25519_sk: *const [u8; crypto_scalarmult_curve25519_BYTES]);

    pub fn crypto_sign_ed25519_sk_to_curve25519(curve25519_sk: *mut [u8; crypto_scalarmult_curve25519_BYTES],
        ed25519_sk: *const [u8; crypto_scalarmult_curve25519_BYTES]);

    pub fn crypto_sign_ed25519_sk_to_seed(seed: *mut [u8; crypto_sign_ed25519_SEEDBYTES],
                                          sk: *const [u8; crypto_sign_ed25519_SECRETKEYBYTES]) -> c_int;

    pub fn crypto_sign_ed25519_sk_to_pk(pk: *mut [u8; crypto_sign_ed25519_PUBLICKEYBYTES],
                                        sk: *const [u8; crypto_sign_ed25519_SECRETKEYBYTES]) -> c_int;
}


#[test]
fn test_crypto_sign_ed25519_bytes() {
    assert_eq!(unsafe {
        crypto_sign_ed25519_bytes() as usize
    }, crypto_sign_ed25519_BYTES)
}
#[test]
fn test_crypto_sign_ed25519_seedbytes() {
    assert_eq!(unsafe {
        crypto_sign_ed25519_seedbytes() as usize
    }, crypto_sign_ed25519_SEEDBYTES)
}
#[test]
fn test_crypto_sign_ed25519_publickeybytes() {
    assert_eq!(unsafe {
        crypto_sign_ed25519_publickeybytes() as usize
    }, crypto_sign_ed25519_PUBLICKEYBYTES)
}
#[test]
fn test_crypto_sign_ed25519_secretkeybytes() {
    assert_eq!(unsafe {
        crypto_sign_ed25519_secretkeybytes() as usize
    }, crypto_sign_ed25519_SECRETKEYBYTES)
}

#[test]
fn test_crypto_sign_ed25519_sk_to_seed() {
    let mut pk = [0; crypto_sign_ed25519_PUBLICKEYBYTES];
    let mut sk = [0; crypto_sign_ed25519_SECRETKEYBYTES];
    let mut seed = [0; crypto_sign_ed25519_SEEDBYTES];
    unsafe {
        assert_eq!(crypto_sign_ed25519_keypair(&mut pk, &mut sk), 0);
        assert_eq!(crypto_sign_ed25519_sk_to_seed(&mut seed, &sk), 0);
    }
    assert_eq!(seed, sk[..crypto_sign_ed25519_SEEDBYTES]);
}

#[test]
fn test_crypto_sign_ed25519_sk_to_pk() {
    let mut pk = [0; crypto_sign_ed25519_PUBLICKEYBYTES];
    let mut sk = [0; crypto_sign_ed25519_SECRETKEYBYTES];
    let mut pk_from_sk = [0; crypto_sign_ed25519_PUBLICKEYBYTES];
    unsafe {
        assert_eq!(crypto_sign_ed25519_keypair(&mut pk, &mut sk), 0);
        assert_eq!(crypto_sign_ed25519_sk_to_pk(&mut pk_from_sk, &sk), 0);
    }
    assert_eq!(pk, pk_from_sk);
}