Skip to content

Commit

Permalink
refactor/sys: use FFI types consistently
Browse files Browse the repository at this point in the history
Also disable crypto_generichash due to inability to specify alignment (see
rust-lang/rust#33626)
  • Loading branch information
Fraser999 committed Feb 8, 2018
1 parent 1cebf65 commit 6c2972c
Show file tree
Hide file tree
Showing 37 changed files with 834 additions and 907 deletions.
8 changes: 4 additions & 4 deletions rust_sodium-sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ extern crate rand;
#[macro_use]
extern crate unwrap;


use libc::{c_char, c_int, c_ulonglong, c_void, size_t, uint32_t};
use libc::{c_char, c_int, c_uchar, c_ulonglong, c_void, size_t, uint32_t, uint64_t, uint8_t};
use rand::{Rng, SeedableRng, XorShiftRng};
use std::cell::RefCell;
use std::ffi::CString;
Expand All @@ -52,8 +51,9 @@ include!("src/crypto_core_salsa20.rs");
include!("src/crypto_core_salsa2012.rs");
include!("src/crypto_core_salsa208.rs");

include!("src/crypto_generichash.rs");
include!("src/crypto_generichash_blake2b.rs");
// TODO re-include once https://github.com/rust-lang/rust/issues/33626 is in stable.
// include!("src/crypto_generichash.rs");
// include!("src/crypto_generichash_blake2b.rs");

include!("src/crypto_hash.rs");
include!("src/crypto_hash_sha256.rs");
Expand Down
60 changes: 31 additions & 29 deletions rust_sodium-sys/src/crypto_aead_chacha20poly1305.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,67 @@
// crypto_aead_chacha20poly1305.h

pub const crypto_aead_chacha20poly1305_KEYBYTES: usize = 32;
pub const crypto_aead_chacha20poly1305_NSECBYTES: usize = 0;
pub const crypto_aead_chacha20poly1305_NPUBBYTES: usize = 8;
pub const crypto_aead_chacha20poly1305_ABYTES: usize = 16;

pub const crypto_aead_chacha20poly1305_KEYBYTES: size_t = 32;
pub const crypto_aead_chacha20poly1305_NSECBYTES: size_t = 0;
pub const crypto_aead_chacha20poly1305_NPUBBYTES: size_t = 8;
pub const crypto_aead_chacha20poly1305_ABYTES: size_t = 16;

extern "C" {
pub fn crypto_aead_chacha20poly1305_keybytes() -> size_t;
pub fn crypto_aead_chacha20poly1305_nsecbytes() -> size_t;
pub fn crypto_aead_chacha20poly1305_npubbytes() -> size_t;
pub fn crypto_aead_chacha20poly1305_abytes() -> size_t;
pub fn crypto_aead_chacha20poly1305_encrypt(
c: *mut u8,
c: *mut c_uchar,
clen: *mut c_ulonglong,
m: *const u8,
m: *const c_uchar,
mlen: c_ulonglong,
ad: *const u8,
ad: *const c_uchar,
adlen: c_ulonglong,
nsec: *const u8,
npub: *const u8,
k: *const u8,
nsec: *const c_uchar,
npub: *const c_uchar,
k: *const c_uchar,
) -> c_int;
pub fn crypto_aead_chacha20poly1305_decrypt(
m: *mut u8,
m: *mut c_uchar,
mlen: *mut c_ulonglong,
nsec: *mut u8,
c: *const u8,
nsec: *mut c_uchar,
c: *const c_uchar,
clen: c_ulonglong,
ad: *const u8,
ad: *const c_uchar,
adlen: c_ulonglong,
npub: *const u8,
k: *const u8,
npub: *const c_uchar,
k: *const c_uchar,
) -> c_int;
}


#[test]
fn test_crypto_aead_chacha20poly1305_keybytes() {
assert!(
unsafe { crypto_aead_chacha20poly1305_keybytes() as usize } ==
crypto_aead_chacha20poly1305_KEYBYTES
assert_eq!(
unsafe { crypto_aead_chacha20poly1305_keybytes() },
crypto_aead_chacha20poly1305_KEYBYTES
)
}

#[test]
fn test_crypto_aead_chacha20poly1305_nsecbytes() {
assert!(
unsafe { crypto_aead_chacha20poly1305_nsecbytes() as usize } ==
crypto_aead_chacha20poly1305_NSECBYTES
assert_eq!(
unsafe { crypto_aead_chacha20poly1305_nsecbytes() },
crypto_aead_chacha20poly1305_NSECBYTES
)
}

#[test]
fn test_crypto_aead_chacha20poly1305_npubbytes() {
assert!(
unsafe { crypto_aead_chacha20poly1305_npubbytes() as usize } ==
crypto_aead_chacha20poly1305_NPUBBYTES
assert_eq!(
unsafe { crypto_aead_chacha20poly1305_npubbytes() },
crypto_aead_chacha20poly1305_NPUBBYTES
)
}

#[test]
fn test_crypto_aead_chacha20poly1305_abytes() {
assert!(
unsafe { crypto_aead_chacha20poly1305_abytes() as usize } == crypto_aead_chacha20poly1305_ABYTES
assert_eq!(
unsafe { crypto_aead_chacha20poly1305_abytes() },
crypto_aead_chacha20poly1305_ABYTES
)
}
30 changes: 20 additions & 10 deletions rust_sodium-sys/src/crypto_auth.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
// crypto_auth.h

pub const crypto_auth_BYTES: usize = crypto_auth_hmacsha512256_BYTES;
pub const crypto_auth_KEYBYTES: usize = crypto_auth_hmacsha512256_KEYBYTES;
pub const crypto_auth_PRIMITIVE: *const c_char = (b"hmacsha512256\0" as *const u8) as *const c_char;

pub const crypto_auth_BYTES: size_t = crypto_auth_hmacsha512256_BYTES;
pub const crypto_auth_KEYBYTES: size_t = crypto_auth_hmacsha512256_KEYBYTES;
pub const crypto_auth_PRIMITIVE: *const c_char = (b"hmacsha512256\0" as *const c_uchar) as
*const c_char;

extern "C" {
pub fn crypto_auth_bytes() -> size_t;
pub fn crypto_auth_keybytes() -> size_t;
pub fn crypto_auth_primitive() -> *const c_char;
pub fn crypto_auth(a: *mut u8, m: *const u8, mlen: c_ulonglong, k: *const u8) -> c_int;
pub fn crypto_auth_verify(a: *const u8, m: *const u8, mlen: c_ulonglong, k: *const u8)
-> c_int;
pub fn crypto_auth(
a: *mut c_uchar,
m: *const c_uchar,
mlen: c_ulonglong,
k: *const c_uchar,
) -> c_int;
pub fn crypto_auth_verify(
a: *const c_uchar,
m: *const c_uchar,
mlen: c_ulonglong,
k: *const c_uchar,
) -> c_int;
}


#[test]
fn test_crypto_auth_bytes() {
assert!(unsafe { crypto_auth_bytes() as usize } == crypto_auth_BYTES)
assert_eq!(unsafe { crypto_auth_bytes() }, crypto_auth_BYTES)
}

#[test]
fn test_crypto_auth_keybytes() {
assert!(unsafe { crypto_auth_keybytes() as usize } == crypto_auth_KEYBYTES)
assert_eq!(unsafe { crypto_auth_keybytes() }, crypto_auth_KEYBYTES)
}

#[test]
fn test_crypto_auth_primitive() {
use std::ffi::CStr;
Expand Down
35 changes: 19 additions & 16 deletions rust_sodium-sys/src/crypto_auth_hmacsha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,52 @@ pub struct crypto_auth_hmacsha256_state {
octx: crypto_hash_sha256_state,
}

pub const crypto_auth_hmacsha256_BYTES: usize = 32;
pub const crypto_auth_hmacsha256_KEYBYTES: usize = 32;

pub const crypto_auth_hmacsha256_BYTES: size_t = 32;
pub const crypto_auth_hmacsha256_KEYBYTES: size_t = 32;

extern "C" {
pub fn crypto_auth_hmacsha256_bytes() -> size_t;
pub fn crypto_auth_hmacsha256_keybytes() -> size_t;
pub fn crypto_auth_hmacsha256(
a: *mut u8,
m: *const u8,
a: *mut c_uchar,
m: *const c_uchar,
mlen: c_ulonglong,
k: *const u8,
k: *const c_uchar,
) -> c_int;
pub fn crypto_auth_hmacsha256_verify(
a: *const u8,
m: *const u8,
a: *const c_uchar,
m: *const c_uchar,
mlen: c_ulonglong,
k: *const u8,
k: *const c_uchar,
) -> c_int;
pub fn crypto_auth_hmacsha256_init(
state: *mut crypto_auth_hmacsha256_state,
key: *const u8,
key: *const c_uchar,
keylen: size_t,
) -> c_int;
pub fn crypto_auth_hmacsha256_update(
state: *mut crypto_auth_hmacsha256_state,
m: *const u8,
m: *const c_uchar,
mlen: c_ulonglong,
) -> c_int;
pub fn crypto_auth_hmacsha256_final(
state: *mut crypto_auth_hmacsha256_state,
a: *mut u8,
a: *mut c_uchar,
) -> c_int;
}


#[test]
fn test_crypto_auth_hmacsha256_bytes() {
assert!(unsafe { crypto_auth_hmacsha256_bytes() as usize } == crypto_auth_hmacsha256_BYTES)
assert_eq!(
unsafe { crypto_auth_hmacsha256_bytes() },
crypto_auth_hmacsha256_BYTES
)
}

#[test]
fn test_crypto_auth_hmacsha256_keybytes() {
assert!(
unsafe { crypto_auth_hmacsha256_keybytes() as usize } == crypto_auth_hmacsha256_KEYBYTES
assert_eq!(
unsafe { crypto_auth_hmacsha256_keybytes() },
crypto_auth_hmacsha256_KEYBYTES
)
}
35 changes: 19 additions & 16 deletions rust_sodium-sys/src/crypto_auth_hmacsha512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,52 @@ pub struct crypto_auth_hmacsha512_state {
octx: crypto_hash_sha512_state,
}

pub const crypto_auth_hmacsha512_BYTES: usize = 64;
pub const crypto_auth_hmacsha512_KEYBYTES: usize = 32;

pub const crypto_auth_hmacsha512_BYTES: size_t = 64;
pub const crypto_auth_hmacsha512_KEYBYTES: size_t = 32;

extern "C" {
pub fn crypto_auth_hmacsha512_bytes() -> size_t;
pub fn crypto_auth_hmacsha512_keybytes() -> size_t;
pub fn crypto_auth_hmacsha512(
a: *mut u8,
m: *const u8,
a: *mut c_uchar,
m: *const c_uchar,
mlen: c_ulonglong,
k: *const u8,
k: *const c_uchar,
) -> c_int;
pub fn crypto_auth_hmacsha512_verify(
a: *const u8,
m: *const u8,
a: *const c_uchar,
m: *const c_uchar,
mlen: c_ulonglong,
k: *const u8,
k: *const c_uchar,
) -> c_int;
pub fn crypto_auth_hmacsha512_init(
state: *mut crypto_auth_hmacsha512_state,
key: *const u8,
key: *const c_uchar,
keylen: size_t,
) -> c_int;
pub fn crypto_auth_hmacsha512_update(
state: *mut crypto_auth_hmacsha512_state,
m: *const u8,
m: *const c_uchar,
mlen: c_ulonglong,
) -> c_int;
pub fn crypto_auth_hmacsha512_final(
state: *mut crypto_auth_hmacsha512_state,
a: *mut u8,
a: *mut c_uchar,
) -> c_int;
}


#[test]
fn test_crypto_auth_hmacsha512_bytes() {
assert!(unsafe { crypto_auth_hmacsha512_bytes() as usize } == crypto_auth_hmacsha512_BYTES)
assert_eq!(
unsafe { crypto_auth_hmacsha512_bytes() },
crypto_auth_hmacsha512_BYTES
)
}

#[test]
fn test_crypto_auth_hmacsha512_keybytes() {
assert!(
unsafe { crypto_auth_hmacsha512_keybytes() as usize } == crypto_auth_hmacsha512_KEYBYTES
assert_eq!(
unsafe { crypto_auth_hmacsha512_keybytes() },
crypto_auth_hmacsha512_KEYBYTES
)
}
31 changes: 16 additions & 15 deletions rust_sodium-sys/src/crypto_auth_hmacsha512256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,49 @@ pub type crypto_auth_hmacsha512256_state = crypto_auth_hmacsha512_state;
pub const crypto_auth_hmacsha512256_BYTES: size_t = 32;
pub const crypto_auth_hmacsha512256_KEYBYTES: size_t = 32;


extern "C" {
pub fn crypto_auth_hmacsha512256_bytes() -> size_t;
pub fn crypto_auth_hmacsha512256_keybytes() -> size_t;
pub fn crypto_auth_hmacsha512256(
a: *mut u8,
m: *const u8,
a: *mut c_uchar,
m: *const c_uchar,
mlen: c_ulonglong,
k: *const u8,
k: *const c_uchar,
) -> c_int;
pub fn crypto_auth_hmacsha512256_verify(
a: *const u8,
m: *const u8,
a: *const c_uchar,
m: *const c_uchar,
mlen: c_ulonglong,
k: *const u8,
k: *const c_uchar,
) -> c_int;
pub fn crypto_auth_hmacsha512256_init(
state: *mut crypto_auth_hmacsha512256_state,
key: *const u8,
key: *const c_uchar,
keylen: size_t,
) -> c_int;
pub fn crypto_auth_hmacsha512256_update(
state: *mut crypto_auth_hmacsha512256_state,
m: *const u8,
m: *const c_uchar,
mlen: c_ulonglong,
) -> c_int;
pub fn crypto_auth_hmacsha512256_final(
state: *mut crypto_auth_hmacsha512256_state,
a: *mut u8,
a: *mut c_uchar,
) -> c_int;
}


#[test]
fn test_crypto_auth_hmacsha512256_bytes() {
assert!(
unsafe { crypto_auth_hmacsha512256_bytes() as usize } == crypto_auth_hmacsha512256_BYTES
assert_eq!(
unsafe { crypto_auth_hmacsha512256_bytes() },
crypto_auth_hmacsha512256_BYTES
)
}

#[test]
fn test_crypto_auth_hmacsha512256_keybytes() {
assert!(
unsafe { crypto_auth_hmacsha512256_keybytes() as usize } == crypto_auth_hmacsha512256_KEYBYTES
assert_eq!(
unsafe { crypto_auth_hmacsha512256_keybytes() },
crypto_auth_hmacsha512256_KEYBYTES
)
}
Loading

0 comments on commit 6c2972c

Please sign in to comment.