Skip to content

Commit

Permalink
Bring back the ring::c internal C types module.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Jun 14, 2019
1 parent 8237fac commit a804615
Show file tree
Hide file tree
Showing 16 changed files with 120 additions and 95 deletions.
23 changes: 11 additions & 12 deletions src/aead/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ use super::{
nonce::{self, Iv},
shift, Block, Direction, BLOCK_LEN,
};
use crate::{bits::BitLength, cpu, endian::*, error, polyfill};
use libc::size_t;
use crate::{bits::BitLength, c, cpu, endian::*, error, polyfill};

pub(crate) struct Key {
inner: AES_KEY,
Expand Down Expand Up @@ -49,14 +48,14 @@ impl Key {
extern "C" {
fn GFp_aes_hw_set_encrypt_key(
user_key: *const u8,
bits: libc::c_uint,
bits: c::uint,
key: &mut AES_KEY,
) -> ZeroMeansSuccess;
}
Result::from(unsafe {
GFp_aes_hw_set_encrypt_key(
bytes.as_ptr(),
key_bits.as_usize_bits() as libc::c_uint,
key_bits.as_usize_bits() as c::uint,
&mut key,
)
})?;
Expand All @@ -67,14 +66,14 @@ impl Key {
extern "C" {
fn GFp_vpaes_set_encrypt_key(
user_key: *const u8,
bits: libc::c_uint,
bits: c::uint,
key: &mut AES_KEY,
) -> ZeroMeansSuccess;
}
Result::from(unsafe {
GFp_vpaes_set_encrypt_key(
bytes.as_ptr(),
key_bits.as_usize_bits() as libc::c_uint,
key_bits.as_usize_bits() as c::uint,
&mut key,
)
})?;
Expand All @@ -84,14 +83,14 @@ impl Key {
extern "C" {
fn GFp_aes_nohw_set_encrypt_key(
user_key: *const u8,
bits: libc::c_uint,
bits: c::uint,
key: &mut AES_KEY,
) -> ZeroMeansSuccess;
}
Result::from(unsafe {
GFp_aes_nohw_set_encrypt_key(
bytes.as_ptr(),
key_bits.as_usize_bits() as libc::c_uint,
key_bits.as_usize_bits() as c::uint,
&mut key,
)
})?;
Expand Down Expand Up @@ -176,7 +175,7 @@ impl Key {
fn GFp_aes_hw_ctr32_encrypt_blocks(
input: *const u8,
output: *mut u8,
blocks: size_t,
blocks: c::size_t,
key: &AES_KEY,
ivec: &Counter,
);
Expand All @@ -193,7 +192,7 @@ impl Key {
fn GFp_bsaes_ctr32_encrypt_blocks(
input: *const u8,
output: *mut u8,
blocks: size_t,
blocks: c::size_t,
key: &AES_KEY,
ivec: &Counter,
);
Expand Down Expand Up @@ -241,7 +240,7 @@ impl Key {
#[repr(C)]
pub(super) struct AES_KEY {
pub rd_key: [u32; 4 * (MAX_ROUNDS + 1)],
pub rounds: libc::c_uint,
pub rounds: c::uint,
}

// Keep this in sync with `AES_MAXNR` in aes.h.
Expand Down Expand Up @@ -292,7 +291,7 @@ fn detect_implementation(cpu_features: cpu::Features) -> Implementation {

#[must_use]
#[repr(transparent)]
pub struct ZeroMeansSuccess(libc::c_int);
pub struct ZeroMeansSuccess(c::int);

impl From<ZeroMeansSuccess> for Result<(), error::Unspecified> {
fn from(ZeroMeansSuccess(value): ZeroMeansSuccess) -> Self {
Expand Down
10 changes: 5 additions & 5 deletions src/aead/aes_gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ fn integrated_aes_gcm<'a>(
direction: Direction,
cpu_features: cpu::Features,
) -> &'a mut [u8] {
use libc::size_t;
use crate::c;

if !aes_key.is_aes_hw() || !gcm_ctx.is_avx2(cpu_features) {
return in_out;
Expand All @@ -217,11 +217,11 @@ fn integrated_aes_gcm<'a>(
fn GFp_aesni_gcm_decrypt(
input: *const u8,
output: *mut u8,
len: size_t,
len: c::size_t,
key: &aes::AES_KEY,
ivec: &mut Counter,
gcm: &mut gcm::Context,
) -> size_t;
) -> c::size_t;
}
unsafe {
GFp_aesni_gcm_decrypt(
Expand All @@ -239,11 +239,11 @@ fn integrated_aes_gcm<'a>(
fn GFp_aesni_gcm_encrypt(
input: *const u8,
output: *mut u8,
len: size_t,
len: c::size_t,
key: &aes::AES_KEY,
ivec: &mut Counter,
gcm: &mut gcm::Context,
) -> size_t;
) -> c::size_t;
}
unsafe {
GFp_aesni_gcm_encrypt(
Expand Down
5 changes: 2 additions & 3 deletions src/aead/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ use super::{
nonce::{self, Iv},
Block, BLOCK_LEN,
};
use crate::{endian::*, polyfill::convert::*};
use libc::size_t;
use crate::{c, endian::*, polyfill::convert::*};

#[repr(C)]
pub struct Key([Block; KEY_BLOCKS]);
Expand Down Expand Up @@ -120,7 +119,7 @@ impl Key {
fn GFp_ChaCha20_ctr32(
out: *mut u8,
in_: *const u8,
in_len: size_t,
in_len: c::size_t,
key: &Key,
first_iv: &Iv,
);
Expand Down
11 changes: 5 additions & 6 deletions src/aead/gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use super::{Aad, Block, BLOCK_LEN};
use crate::cpu;
use libc::size_t;
use crate::{c, cpu};

#[repr(transparent)]
pub struct Key(GCM128_KEY);
Expand Down Expand Up @@ -111,7 +110,7 @@ impl Context {
ctx: &mut Context,
h_table: *const GCM128_KEY,
inp: *const u8,
len: size_t,
len: c::size_t,
);
}
unsafe {
Expand All @@ -125,7 +124,7 @@ impl Context {
ctx: &mut Context,
h_table: *const GCM128_KEY,
inp: *const u8,
len: size_t,
len: c::size_t,
);
}
unsafe {
Expand All @@ -140,7 +139,7 @@ impl Context {
ctx: &mut Context,
h_table: *const GCM128_KEY,
inp: *const u8,
len: size_t,
len: c::size_t,
);
}
unsafe {
Expand All @@ -154,7 +153,7 @@ impl Context {
ctx: &mut Context,
h_table: *const GCM128_KEY,
inp: *const u8,
len: size_t,
len: c::size_t,
);
}
unsafe {
Expand Down
7 changes: 3 additions & 4 deletions src/aead/poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ use super::{
block::{Block, BLOCK_LEN},
Tag,
};
use crate::{bssl, error};
use libc::size_t;
use crate::{bssl, c, error};

/// A Poly1305 key.
pub struct Key([Block; KEY_BLOCKS]);
Expand Down Expand Up @@ -51,7 +50,7 @@ impl Context {
fn GFp_poly1305_blocks(
state: &mut Opaque,
input: *const u8,
len: size_t,
len: c::size_t,
should_pad: Pad,
);
fn GFp_poly1305_emit(state: &mut Opaque, tag: &mut Tag, nonce: &Nonce);
Expand Down Expand Up @@ -123,7 +122,7 @@ struct Nonce(Block);
#[repr(C)]
struct Funcs {
blocks_fn:
unsafe extern "C" fn(&mut Opaque, input: *const u8, input_len: size_t, should_pad: Pad),
unsafe extern "C" fn(&mut Opaque, input: *const u8, input_len: c::size_t, should_pad: Pad),
emit_fn: unsafe extern "C" fn(&mut Opaque, &mut Tag, nonce: &Nonce),
}

Expand Down
8 changes: 4 additions & 4 deletions src/bssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use crate::error;
use crate::{c, error};

/// An `int` returned from a foreign function containing **1** if the function
/// was successful or **0** if an error occurred. This is the convention used by
/// C code in `ring`.
#[derive(Clone, Copy, Debug)]
#[must_use]
#[repr(transparent)]
pub struct Result(libc::c_int);
pub struct Result(c::int);

impl From<Result> for core::result::Result<(), error::Unspecified> {
fn from(ret: Result) -> Self {
Expand All @@ -37,12 +37,12 @@ impl From<Result> for core::result::Result<(), error::Unspecified> {
#[cfg(test)]
mod tests {
mod result {
use crate::bssl;
use crate::{bssl, c};
use core::mem;

#[test]
fn size_and_alignment() {
type Underlying = libc::c_int;
type Underlying = c::int;
assert_eq!(mem::size_of::<bssl::Result>(), mem::size_of::<Underlying>());
assert_eq!(
mem::align_of::<bssl::Result>(),
Expand Down
33 changes: 33 additions & 0 deletions src/c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2016-2019 Brian Smith.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

//! C types.
//!
//! The libc crate provide the C types for most, but not all, targets that
//! *ring* supports.

use libc;

pub(crate) type size_t = libc::size_t;
pub(crate) type int = libc::c_int;
pub(crate) type uint = libc::c_uint;

#[cfg(all(
any(target_os = "android", target_os = "linux"),
any(target_arch = "aarch64", target_arch = "arm")
))]
pub(crate) type ulong = libc::c_ulong;

#[cfg(any(target_os = "android", target_os = "linux"))]
pub(crate) type long = libc::c_long;
5 changes: 2 additions & 3 deletions src/constant_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

//! Constant-time operations.

use crate::error;
use libc::size_t;
use crate::{c, error};

/// Returns `Ok(())` if `a == b` and `Err(error::Unspecified)` otherwise.
/// The comparison of `a` and `b` is done in constant time with respect to the
Expand All @@ -33,7 +32,7 @@ pub fn verify_slices_are_equal(a: &[u8], b: &[u8]) -> Result<(), error::Unspecif
}

extern "C" {
fn GFp_memcmp(a: *const u8, b: *const u8, len: size_t) -> libc::c_int;
fn GFp_memcmp(a: *const u8, b: *const u8, len: c::size_t) -> c::int;
}

#[cfg(test)]
Expand Down
22 changes: 12 additions & 10 deletions src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,21 @@ pub(crate) mod arm {
any(target_arch = "aarch64", target_arch = "arm")
))]
pub fn linux_setup() {
use crate::c;

// XXX: The `libc` crate doesn't provide `libc::getauxval` consistently
// across all Android/Linux targets, e.g. musl.
extern "C" {
fn getauxval(type_: libc::c_ulong) -> libc::c_ulong;
fn getauxval(type_: c::ulong) -> c::ulong;
}

const AT_HWCAP: libc::c_ulong = 16;
const AT_HWCAP: c::ulong = 16;

#[cfg(target_arch = "aarch64")]
const HWCAP_NEON: libc::c_ulong = 1 << 1;
const HWCAP_NEON: c::ulong = 1 << 1;

#[cfg(target_arch = "arm")]
const HWCAP_NEON: libc::c_ulong = 1 << 12;
const HWCAP_NEON: c::ulong = 1 << 12;

let caps = unsafe { getauxval(AT_HWCAP) };

Expand All @@ -86,20 +88,20 @@ pub(crate) mod arm {
let mut features = NEON.mask;

#[cfg(target_arch = "aarch64")]
const OFFSET: libc::c_ulong = 3;
const OFFSET: c::ulong = 3;

#[cfg(target_arch = "arm")]
const OFFSET: libc::c_ulong = 0;
const OFFSET: c::ulong = 0;

#[cfg(target_arch = "arm")]
let caps = {
const AT_HWCAP2: libc::c_ulong = 26;
const AT_HWCAP2: c::ulong = 26;
unsafe { getauxval(AT_HWCAP2) }
};

const HWCAP_AES: libc::c_ulong = 1 << 0 + OFFSET;
const HWCAP_PMULL: libc::c_ulong = 1 << 1 + OFFSET;
const HWCAP_SHA2: libc::c_ulong = 1 << 3 + OFFSET;
const HWCAP_AES: c::ulong = 1 << 0 + OFFSET;
const HWCAP_PMULL: c::ulong = 1 << 1 + OFFSET;
const HWCAP_SHA2: c::ulong = 1 << 3 + OFFSET;

if caps & HWCAP_AES == HWCAP_AES {
features |= AES.mask;
Expand Down
9 changes: 4 additions & 5 deletions src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
// The goal for this implementation is to drive the overhead as close to zero
// as possible.

use crate::{cpu, debug, endian::*, polyfill};
use crate::{c, cpu, debug, endian::*, polyfill};
use core::num::Wrapping;
use libc::size_t;

mod sha1;

Expand Down Expand Up @@ -248,7 +247,7 @@ pub struct Algorithm {
/// The length of the length in the padding.
len_len: usize,

block_data_order: unsafe extern "C" fn(state: &mut State, data: *const u8, num: size_t),
block_data_order: unsafe extern "C" fn(state: &mut State, data: *const u8, num: c::size_t),
format_output: fn(input: State) -> Output,

initial_state: State,
Expand Down Expand Up @@ -484,8 +483,8 @@ const SHA512_BLOCK_LEN: usize = 1024 / 8;
const SHA512_LEN_LEN: usize = 128 / 8;

extern "C" {
fn GFp_sha256_block_data_order(state: &mut State, data: *const u8, num: size_t);
fn GFp_sha512_block_data_order(state: &mut State, data: *const u8, num: size_t);
fn GFp_sha256_block_data_order(state: &mut State, data: *const u8, num: c::size_t);
fn GFp_sha512_block_data_order(state: &mut State, data: *const u8, num: c::size_t);
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit a804615

Please sign in to comment.