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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// crypto_box.h

pub const crypto_box_SEEDBYTES: usize = crypto_box_curve25519xsalsa20poly1305_SEEDBYTES;
pub const crypto_box_PUBLICKEYBYTES: usize = crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES;
pub const crypto_box_SECRETKEYBYTES: usize = crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES;
pub const crypto_box_BEFORENMBYTES: usize = crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES;
pub const crypto_box_NONCEBYTES: usize = crypto_box_curve25519xsalsa20poly1305_NONCEBYTES;
pub const crypto_box_ZEROBYTES: usize = crypto_box_curve25519xsalsa20poly1305_ZEROBYTES;
pub const crypto_box_BOXZEROBYTES: usize = crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES;
pub const crypto_box_MACBYTES: usize = crypto_box_curve25519xsalsa20poly1305_MACBYTES;
pub const crypto_box_PRIMITIVE: &'static str = "curve25519xsalsa20poly1305";


extern {
    pub fn crypto_box_seedbytes() -> size_t;
    pub fn crypto_box_publickeybytes() -> size_t;
    pub fn crypto_box_secretkeybytes() -> size_t;
    pub fn crypto_box_beforenmbytes() -> size_t;
    pub fn crypto_box_noncebytes() -> size_t;
    pub fn crypto_box_zerobytes() -> size_t;
    pub fn crypto_box_boxzerobytes() -> size_t;
    pub fn crypto_box_macbytes() -> size_t;
    pub fn crypto_box_primitive() -> *const c_char;

    pub fn crypto_box_seed_keypair(
        pk: *mut [u8; crypto_box_PUBLICKEYBYTES],
        sk: *mut [u8; crypto_box_SECRETKEYBYTES],
        seed: *const [u8; crypto_box_SEEDBYTES])
        -> c_int;
    pub fn crypto_box_keypair(
        pk: *mut [u8; crypto_box_PUBLICKEYBYTES],
        sk: *mut [u8; crypto_box_SECRETKEYBYTES])
        -> c_int;
    pub fn crypto_box_beforenm(
        k: *mut [u8; crypto_box_BEFORENMBYTES],
        pk: *const [u8; crypto_box_PUBLICKEYBYTES],
        sk: *const [u8; crypto_box_SECRETKEYBYTES])
        -> c_int;
    pub fn crypto_box_afternm(
        c: *mut u8,
        m: *const u8,
        mlen: c_ulonglong,
        n: *const [u8; crypto_box_NONCEBYTES],
        k: *const [u8; crypto_box_BEFORENMBYTES])
        -> c_int;
    pub fn crypto_box_open_afternm(
        m: *mut u8,
        c: *const u8,
        clen: c_ulonglong,
        n: *const [u8; crypto_box_NONCEBYTES],
        k: *const [u8; crypto_box_BEFORENMBYTES])
        -> c_int;
    pub fn crypto_box(
        c: *mut u8,
        m: *const u8,
        mlen: c_ulonglong,
        n: *const [u8; crypto_box_NONCEBYTES],
        pk: *const [u8; crypto_box_PUBLICKEYBYTES],
        sk: *const [u8; crypto_box_SECRETKEYBYTES])
        -> c_int;
    pub fn crypto_box_open(
        m: *mut u8,
        c: *const u8,
        clen: c_ulonglong,
        n: *const [u8; crypto_box_NONCEBYTES],
        pk: *const [u8; crypto_box_PUBLICKEYBYTES],
        sk: *const [u8; crypto_box_SECRETKEYBYTES])
        -> c_int;
    pub fn crypto_box_easy(
        c: *mut u8,
        m: *const u8,
        mlen: c_ulonglong,
        n: *const [u8; crypto_box_NONCEBYTES],
        pk: *const [u8; crypto_box_PUBLICKEYBYTES],
        sk: *const [u8; crypto_box_SECRETKEYBYTES])
        -> c_int;
    pub fn crypto_box_open_easy(
        m: *mut u8,
        c: *const u8,
        clen: c_ulonglong,
        n: *const [u8; crypto_box_NONCEBYTES],
        pk: *const [u8; crypto_box_PUBLICKEYBYTES],
        sk: *const [u8; crypto_box_SECRETKEYBYTES])
        -> c_int;
    pub fn crypto_box_detached(
        c: *mut u8,
        mac: *mut [u8; crypto_box_MACBYTES],
        m: *const u8,
        mlen: c_ulonglong,
        n: *const [u8; crypto_box_NONCEBYTES],
        pk: *const [u8; crypto_box_PUBLICKEYBYTES],
        sk: *const [u8; crypto_box_SECRETKEYBYTES])
        -> c_int;
    pub fn crypto_box_open_detached(
        m: *mut u8,
        c: *const u8,
        mac: *const [u8; crypto_box_MACBYTES],
        clen: c_ulonglong,
        n: *const [u8; crypto_box_NONCEBYTES],
        pk: *const [u8; crypto_box_PUBLICKEYBYTES],
        sk: *const [u8; crypto_box_SECRETKEYBYTES])
        -> c_int;
}


#[test]
fn test_crypto_box_seedbytes() {
    assert!(unsafe {
        crypto_box_seedbytes() as usize
    } == crypto_box_SEEDBYTES)
}
#[test]
fn test_crypto_box_publickeybytes() {
    assert!(unsafe {
        crypto_box_publickeybytes() as usize
    } == crypto_box_PUBLICKEYBYTES)
}
#[test]
fn test_crypto_box_secretkeybytes() {
    assert!(unsafe {
        crypto_box_secretkeybytes() as usize
    } == crypto_box_SECRETKEYBYTES)
}
#[test]
fn test_crypto_box_beforenmbytes() {
    assert!(unsafe {
        crypto_box_beforenmbytes() as usize
    } == crypto_box_BEFORENMBYTES)
}
#[test]
fn test_crypto_box_noncebytes() {
    assert!(unsafe {
        crypto_box_noncebytes() as usize
    } == crypto_box_NONCEBYTES)
}
#[test]
fn test_crypto_box_zerobytes() {
    assert!(unsafe {
        crypto_box_zerobytes() as usize
    } == crypto_box_ZEROBYTES)
}
#[test]
fn test_crypto_box_boxzerobytes() {
    assert!(unsafe {
        crypto_box_boxzerobytes() as usize
    } == crypto_box_BOXZEROBYTES)
}
#[test]
fn test_crypto_box_macbytes() {
    assert!(unsafe {
        crypto_box_macbytes() as usize
    } == crypto_box_MACBYTES)
}
#[test]
fn test_crypto_box_primitive() {
    unsafe {
        let s = crypto_box_primitive();
        let s = std::ffi::CStr::from_ptr(s).to_bytes();
        assert!(s == crypto_box_PRIMITIVE.as_bytes());
    }
}