use util::errors;
use crypto;
-use crypto::symmetriccipher::SynchronousStreamCipher;
use std::{cmp, ptr, mem};
use std::collections::{HashMap, hash_map, HashSet};
use secp256k1::ecdh::SharedSecret;
use secp256k1;
-use crypto::aead::{AeadEncryptor, AeadDecryptor};
-
use util::chacha20poly1305rfc::ChaCha20Poly1305RFC;
use util::{byte_utils,rng};
mod real_chacha {
use std::cmp;
use util::byte_utils::{slice_to_le32, le32_to_array};
- use crypto::symmetriccipher::SynchronousStreamCipher;
#[derive(Clone, Copy, PartialEq, Eq)]
#[allow(non_camel_case_types)]
self.offset = 0;
}
- }
- impl SynchronousStreamCipher for ChaCha20 {
- fn process(&mut self, input: &[u8], output: &mut [u8]) {
+ pub fn process(&mut self, input: &[u8], output: &mut [u8]) {
assert!(input.len() == output.len());
let len = input.len();
let mut i = 0;
#[cfg(feature = "fuzztarget")]
mod fuzzy_chacha {
- use crypto::symmetriccipher::SynchronousStreamCipher;
-
pub struct ChaCha20 {}
impl ChaCha20 {
assert!(nonce.len() == 8 || nonce.len() == 12);
Self {}
}
- }
- impl SynchronousStreamCipher for ChaCha20 {
- fn process(&mut self, input: &[u8], output: &mut [u8]) {
+ pub fn process(&mut self, input: &[u8], output: &mut [u8]) {
output.copy_from_slice(input);
}
}
use std::iter::repeat;
use super::ChaCha20;
- use crypto::symmetriccipher::SynchronousStreamCipher;
#[test]
fn test_chacha20_256_tls_vectors() {
use util::chacha20::ChaCha20;
use util::poly1305::Poly1305;
- use crypto::aead::{AeadEncryptor,AeadDecryptor};
- use crypto::symmetriccipher::SynchronousStreamCipher;
- use crypto::mac::Mac;
use crypto::util::fixed_time_eq;
use util::byte_utils;
aad_len: aad.len() as u64,
}
}
- }
- impl AeadEncryptor for ChaCha20Poly1305RFC {
- fn encrypt(&mut self, input: &[u8], output: &mut [u8], out_tag: &mut [u8]) {
+ pub fn encrypt(&mut self, input: &[u8], output: &mut [u8], out_tag: &mut [u8]) {
assert!(input.len() == output.len());
assert!(self.finished == false);
self.cipher.process(input, output);
self.mac.input(&byte_utils::le64_to_array(self.data_len as u64));
self.mac.raw_result(out_tag);
}
- }
- impl AeadDecryptor for ChaCha20Poly1305RFC {
- fn decrypt(&mut self, input: &[u8], output: &mut [u8], tag: &[u8]) -> bool {
+ pub fn decrypt(&mut self, input: &[u8], output: &mut [u8], tag: &[u8]) -> bool {
assert!(input.len() == output.len());
assert!(self.finished == false);
#[cfg(feature = "fuzztarget")]
mod fuzzy_chachapoly {
- use crypto::aead::{AeadEncryptor,AeadDecryptor};
-
#[derive(Clone, Copy)]
pub struct ChaCha20Poly1305RFC {
tag: [u8; 16],
finished: false,
}
}
- }
- impl AeadEncryptor for ChaCha20Poly1305RFC {
- fn encrypt(&mut self, input: &[u8], output: &mut [u8], out_tag: &mut [u8]) {
+ pub fn encrypt(&mut self, input: &[u8], output: &mut [u8], out_tag: &mut [u8]) {
assert!(input.len() == output.len());
assert!(self.finished == false);
out_tag.copy_from_slice(&self.tag);
self.finished = true;
}
- }
- impl AeadDecryptor for ChaCha20Poly1305RFC {
- fn decrypt(&mut self, input: &[u8], output: &mut [u8], tag: &[u8]) -> bool {
+ pub fn decrypt(&mut self, input: &[u8], output: &mut [u8], tag: &[u8]) -> bool {
assert!(input.len() == output.len());
assert!(self.finished == false);
use std::cmp::min;
use util::byte_utils::{slice_to_le32, le32_to_array};
-use crypto::mac::{Mac, MacResult};
-
#[derive(Clone, Copy)]
pub struct Poly1305 {
r : [u32; 5],
self.h[4] = h4;
}
- fn finish(&mut self) {
+ pub fn finish(&mut self) {
if self.leftover > 0 {
self.buffer[self.leftover] = 1;
for i in self.leftover+1..16 {
self.h[2] = h2;
self.h[3] = h3;
}
-}
-impl Mac for Poly1305 {
- fn input(&mut self, data: &[u8]) {
+ pub fn input(&mut self, data: &[u8]) {
assert!(!self.finalized);
let mut m = data;
self.leftover = m.len();
}
- fn reset(&mut self) {
- self.h = [0u32; 5];
- self.leftover = 0;
- self.finalized = false;
- }
-
- fn result(&mut self) -> MacResult {
- let mut mac = [0u8; 16];
- self.raw_result(&mut mac);
- MacResult::new(&mac[..])
- }
-
- fn raw_result(&mut self, output: &mut [u8]) {
+ pub fn raw_result(&mut self, output: &mut [u8]) {
assert!(output.len() >= 16);
if !self.finalized{
self.finish();
output[8..12].copy_from_slice(&le32_to_array(self.h[2]));
output[12..16].copy_from_slice(&le32_to_array(self.h[3]));
}
-
- fn output_bytes(&self) -> usize { 16 }
}
#[cfg(test)]
use std::iter::repeat;
use util::poly1305::Poly1305;
- use crypto::mac::Mac;
fn poly1305(key: &[u8], msg: &[u8], mac: &mut [u8]) {
let mut poly = Poly1305::new(key);