use std;
use std::{io, mem};
use std::ptr;
use buffer::{ReadBuffer, WriteBuffer, BufferResult};
use buffer::BufferResult::{BufferUnderflow, BufferOverflow};
use symmetriccipher::{SynchronousStreamCipher, SymmetricCipherError};
pub fn write_u64_be(dst: &mut[u8], mut input: u64) {
assert!(dst.len() == 8);
input = input.to_be();
unsafe {
let tmp = &input as *const _ as *const u8;
ptr::copy_nonoverlapping(tmp, dst.get_unchecked_mut(0), 8);
}
}
pub fn write_u64_le(dst: &mut[u8], mut input: u64) {
assert!(dst.len() == 8);
input = input.to_le();
unsafe {
let tmp = &input as *const _ as *const u8;
ptr::copy_nonoverlapping(tmp, dst.get_unchecked_mut(0), 8);
}
}
pub fn write_u64v_le(dst: &mut[u8], input: &[u64]) {
assert!(dst.len() == 8 * input.len());
unsafe {
let mut x: *mut u8 = dst.get_unchecked_mut(0);
let mut y: *const u64 = input.get_unchecked(0);
for _ in 0..input.len() {
let tmp = (*y).to_le();
ptr::copy_nonoverlapping(&tmp as *const _ as *const u8, x, 8);
x = x.offset(8);
y = y.offset(1);
}
}
}
pub fn write_u32_be(dst: &mut [u8], mut input: u32) {
assert!(dst.len() == 4);
input = input.to_be();
unsafe {
let tmp = &input as *const _ as *const u8;
ptr::copy_nonoverlapping(tmp, dst.get_unchecked_mut(0), 4);
}
}
pub fn write_u32_le(dst: &mut[u8], mut input: u32) {
assert!(dst.len() == 4);
input = input.to_le();
unsafe {
let tmp = &input as *const _ as *const u8;
ptr::copy_nonoverlapping(tmp, dst.get_unchecked_mut(0), 4);
}
}
pub fn write_u32v_le (dst: &mut[u8], input: &[u32]) {
assert!(dst.len() == 4 * input.len());
unsafe {
let mut x: *mut u8 = dst.get_unchecked_mut(0);
let mut y: *const u32 = input.get_unchecked(0);
for _ in 0..input.len() {
let tmp = (*y).to_le();
ptr::copy_nonoverlapping(&tmp as *const _ as *const u8, x, 4);
x = x.offset(4);
y = y.offset(1);
}
}
}
pub fn read_u64v_be(dst: &mut[u64], input: &[u8]) {
assert!(dst.len() * 8 == input.len());
unsafe {
let mut x: *mut u64 = dst.get_unchecked_mut(0);
let mut y: *const u8 = input.get_unchecked(0);
for _ in 0..dst.len() {
let mut tmp: u64 = mem::uninitialized();
ptr::copy_nonoverlapping(y, &mut tmp as *mut _ as *mut u8, 8);
*x = u64::from_be(tmp);
x = x.offset(1);
y = y.offset(8);
}
}
}
pub fn read_u64v_le(dst: &mut[u64], input: &[u8]) {
assert!(dst.len() * 8 == input.len());
unsafe {
let mut x: *mut u64 = dst.get_unchecked_mut(0);
let mut y: *const u8 = input.get_unchecked(0);
for _ in 0..dst.len() {
let mut tmp: u64 = mem::uninitialized();
ptr::copy_nonoverlapping(y, &mut tmp as *mut _ as *mut u8, 8);
*x = u64::from_le(tmp);
x = x.offset(1);
y = y.offset(8);
}
}
}
pub fn read_u32v_be(dst: &mut[u32], input: &[u8]) {
assert!(dst.len() * 4 == input.len());
unsafe {
let mut x: *mut u32 = dst.get_unchecked_mut(0);
let mut y: *const u8 = input.get_unchecked(0);
for _ in 0..dst.len() {
let mut tmp: u32 = mem::uninitialized();
ptr::copy_nonoverlapping(y, &mut tmp as *mut _ as *mut u8, 4);
*x = u32::from_be(tmp);
x = x.offset(1);
y = y.offset(4);
}
}
}
pub fn read_u32v_le(dst: &mut[u32], input: &[u8]) {
assert!(dst.len() * 4 == input.len());
unsafe {
let mut x: *mut u32 = dst.get_unchecked_mut(0);
let mut y: *const u8 = input.get_unchecked(0);
for _ in 0..dst.len() {
let mut tmp: u32 = mem::uninitialized();
ptr::copy_nonoverlapping(y, &mut tmp as *mut _ as *mut u8, 4);
*x = u32::from_le(tmp);
x = x.offset(1);
y = y.offset(4);
}
}
}
pub fn read_u32_le(input: &[u8]) -> u32 {
assert!(input.len() == 4);
unsafe {
let mut tmp: u32 = mem::uninitialized();
ptr::copy_nonoverlapping(input.get_unchecked(0), &mut tmp as *mut _ as *mut u8, 4);
u32::from_le(tmp)
}
}
pub fn read_u32_be(input: &[u8]) -> u32 {
assert!(input.len() == 4);
unsafe {
let mut tmp: u32 = mem::uninitialized();
ptr::copy_nonoverlapping(input.get_unchecked(0), &mut tmp as *mut _ as *mut u8, 4);
u32::from_be(tmp)
}
}
pub fn xor_keystream(dst: &mut[u8], plaintext: &[u8], keystream: &[u8]) {
assert!(dst.len() == plaintext.len());
assert!(plaintext.len() <= keystream.len());
let p = plaintext.as_ptr();
let k = keystream.as_ptr();
let d = dst.as_mut_ptr();
for i in 0isize..plaintext.len() as isize {
unsafe{ *d.offset(i) = *p.offset(i) ^ *k.offset(i) };
}
}
#[inline]
pub fn copy_memory(src: &[u8], dst: &mut [u8]) {
assert!(dst.len() >= src.len());
unsafe {
let srcp = src.as_ptr();
let dstp = dst.as_mut_ptr();
ptr::copy_nonoverlapping(srcp, dstp, src.len());
}
}
#[inline]
pub fn zero(dst: &mut [u8]) {
unsafe {
ptr::write_bytes(dst.as_mut_ptr(), 0, dst.len());
}
}
pub trait WriteExt {
fn write_u8(&mut self, val: u8) -> io::Result<()>;
fn write_u32_le(&mut self, val: u32) -> io::Result<()>;
fn write_u32_be(&mut self, val: u32) -> io::Result<()>;
fn write_u64_le(&mut self, val: u64) -> io::Result<()>;
fn write_u64_be(&mut self, val: u64) -> io::Result<()>;
}
impl <T> WriteExt for T where T: io::Write {
fn write_u8(&mut self, val: u8) -> io::Result<()> {
let buff = [val];
self.write_all(&buff)
}
fn write_u32_le(&mut self, val: u32) -> io::Result<()> {
let mut buff = [0u8; 4];
write_u32_le(&mut buff, val);
self.write_all(&buff)
}
fn write_u32_be(&mut self, val: u32) -> io::Result<()> {
let mut buff = [0u8; 4];
write_u32_be(&mut buff, val);
self.write_all(&buff)
}
fn write_u64_le(&mut self, val: u64) -> io::Result<()> {
let mut buff = [0u8; 8];
write_u64_le(&mut buff, val);
self.write_all(&buff)
}
fn write_u64_be(&mut self, val: u64) -> io::Result<()> {
let mut buff = [0u8; 8];
write_u64_be(&mut buff, val);
self.write_all(&buff)
}
}
pub fn symm_enc_or_dec<S: SynchronousStreamCipher, R: ReadBuffer, W: WriteBuffer>(
c: &mut S,
input: &mut R,
output: &mut W) ->
Result<BufferResult, SymmetricCipherError> {
let count = std::cmp::min(input.remaining(), output.remaining());
c.process(input.take_next(count), output.take_next(count));
if input.is_empty() {
Ok(BufferUnderflow)
} else {
Ok(BufferOverflow)
}
}
fn to_bits(x: u64) -> (u64, u64) {
(x >> 61, x << 3)
}
pub fn add_bytes_to_bits(bits: u64, bytes: u64) -> u64 {
let (new_high_bits, new_low_bits) = to_bits(bytes);
if new_high_bits > 0 {
panic!("Numeric overflow occured.")
}
bits.checked_add(new_low_bits).expect("Numeric overflow occured.")
}
pub fn add_bytes_to_bits_tuple
(bits: (u64, u64), bytes: u64) -> (u64, u64) {
let (new_high_bits, new_low_bits) = to_bits(bytes);
let (hi, low) = bits;
match low.checked_add(new_low_bits) {
Some(x) => {
if new_high_bits == 0 {
return (hi, x);
} else {
match hi.checked_add(new_high_bits) {
Some(y) => return (y, x),
None => panic!("Numeric overflow occured.")
}
}
},
None => {
let z = match new_high_bits.checked_add(1) {
Some(w) => w,
None => panic!("Numeric overflow occured.")
};
match hi.checked_add(z) {
Some(y) => return (y, low.wrapping_add(new_low_bits)),
None => panic!("Numeric overflow occured.")
}
}
}
}
pub trait FixedBuffer {
fn input<F: FnMut(&[u8])>(&mut self, input: &[u8], func: F);
fn reset(&mut self);
fn zero_until(&mut self, idx: usize);
fn next<'s>(&'s mut self, len: usize) -> &'s mut [u8];
fn full_buffer<'s>(&'s mut self) -> &'s [u8];
fn current_buffer<'s>(&'s mut self) -> &'s [u8];
fn position(&self) -> usize;
fn remaining(&self) -> usize;
fn size(&self) -> usize;
}
macro_rules! impl_fixed_buffer( ($name:ident, $size:expr) => (
impl FixedBuffer for $name {
fn input<F: FnMut(&[u8])>(&mut self, input: &[u8], mut func: F) {
let mut i = 0;
let size = $size;
if self.buffer_idx != 0 {
let buffer_remaining = size - self.buffer_idx;
if input.len() >= buffer_remaining {
copy_memory(
&input[..buffer_remaining],
&mut self.buffer[self.buffer_idx..size]);
self.buffer_idx = 0;
func(&self.buffer);
i += buffer_remaining;
} else {
copy_memory(
input,
&mut self.buffer[self.buffer_idx..self.buffer_idx + input.len()]);
self.buffer_idx += input.len();
return;
}
}
while input.len() - i >= size {
func(&input[i..i + size]);
i += size;
}
let input_remaining = input.len() - i;
copy_memory(
&input[i..],
&mut self.buffer[0..input_remaining]);
self.buffer_idx += input_remaining;
}
fn reset(&mut self) {
self.buffer_idx = 0;
}
fn zero_until(&mut self, idx: usize) {
assert!(idx >= self.buffer_idx);
zero(&mut self.buffer[self.buffer_idx..idx]);
self.buffer_idx = idx;
}
fn next<'s>(&'s mut self, len: usize) -> &'s mut [u8] {
self.buffer_idx += len;
&mut self.buffer[self.buffer_idx - len..self.buffer_idx]
}
fn full_buffer<'s>(&'s mut self) -> &'s [u8] {
assert!(self.buffer_idx == $size);
self.buffer_idx = 0;
&self.buffer[..$size]
}
fn current_buffer<'s>(&'s mut self) -> &'s [u8] {
let tmp = self.buffer_idx;
self.buffer_idx = 0;
&self.buffer[..tmp]
}
fn position(&self) -> usize { self.buffer_idx }
fn remaining(&self) -> usize { $size - self.buffer_idx }
fn size(&self) -> usize { $size }
}
));
#[derive(Copy)]
pub struct FixedBuffer64 {
buffer: [u8; 64],
buffer_idx: usize,
}
impl Clone for FixedBuffer64 { fn clone(&self) -> FixedBuffer64 { *self } }
impl FixedBuffer64 {
pub fn new() -> FixedBuffer64 {
FixedBuffer64 {
buffer: [0u8; 64],
buffer_idx: 0
}
}
}
impl_fixed_buffer!(FixedBuffer64, 64);
#[derive(Copy)]
pub struct FixedBuffer128 {
buffer: [u8; 128],
buffer_idx: usize,
}
impl Clone for FixedBuffer128 { fn clone(&self) -> FixedBuffer128 { *self } }
impl FixedBuffer128 {
pub fn new() -> FixedBuffer128 {
FixedBuffer128 {
buffer: [0u8; 128],
buffer_idx: 0
}
}
}
impl_fixed_buffer!(FixedBuffer128, 128);
pub trait StandardPadding {
fn standard_padding<F: FnMut(&[u8])>(&mut self, rem: usize, func: F);
}
impl <T: FixedBuffer> StandardPadding for T {
fn standard_padding<F: FnMut(&[u8])>(&mut self, rem: usize, mut func: F) {
let size = self.size();
self.next(1)[0] = 128;
if self.remaining() < rem {
self.zero_until(size);
func(self.full_buffer());
}
self.zero_until(size - rem);
}
}
#[cfg(test)]
pub mod test {
use std;
use std::iter::repeat;
use rand::IsaacRng;
use rand::distributions::{IndependentSample, Range};
use cryptoutil::{add_bytes_to_bits, add_bytes_to_bits_tuple};
use digest::Digest;
pub fn test_digest_1million_random<D: Digest>(digest: &mut D, blocksize: usize, expected: &str) {
let total_size = 1000000;
let buffer: Vec<u8> = repeat('a' as u8).take(blocksize * 2).collect();
let mut rng = IsaacRng::new_unseeded();
let range = Range::new(0, 2 * blocksize + 1);
let mut count = 0;
digest.reset();
while count < total_size {
let next = range.ind_sample(&mut rng);
let remaining = total_size - count;
let size = if next > remaining { remaining } else { next };
digest.input(&buffer[..size]);
count += size;
}
let result_str = digest.result_str();
assert!(expected == &result_str[..]);
}
#[test]
fn test_add_bytes_to_bits_ok() {
assert!(add_bytes_to_bits(100, 10) == 180);
}
#[test]
#[should_panic]
fn test_add_bytes_to_bits_overflow() {
add_bytes_to_bits(std::u64::MAX, 1);
}
#[test]
fn test_add_bytes_to_bits_tuple_ok() {
assert!(add_bytes_to_bits_tuple((5, 100), 10) == (5, 180));
}
#[test]
fn test_add_bytes_to_bits_tuple_ok2() {
assert!(add_bytes_to_bits_tuple((5, std::u64::MAX), 1) == (6, 7));
}
#[test]
fn test_add_bytes_to_bits_tuple_ok3() {
assert!(add_bytes_to_bits_tuple((5, 0), 0x4000000000000001) == (7, 8));
}
#[test]
#[should_panic]
fn test_add_bytes_to_bits_tuple_overflow() {
add_bytes_to_bits_tuple((std::u64::MAX, std::u64::MAX), 1);
}
#[test]
#[should_panic]
fn test_add_bytes_to_bits_tuple_overflow2() {
let value: u64 = std::u64::MAX;
add_bytes_to_bits_tuple((value - 1, 0), 0x8000000000000000);
}
}