mirror of
https://github.com/henrydcase/pqc.git
synced 2024-11-22 15:39:07 +00:00
use aes_ctr_drbg crate
This commit is contained in:
parent
ebaeebc5da
commit
188e672764
11
test/katrunner/Cargo.lock
generated
11
test/katrunner/Cargo.lock
generated
@ -1,5 +1,15 @@
|
|||||||
# This file is automatically @generated by Cargo.
|
# This file is automatically @generated by Cargo.
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
|
[[package]]
|
||||||
|
name = "aes_ctr_drbg"
|
||||||
|
version = "0.0.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d0a42e20bb5f9502c40bee62428fae5d67b5c111b4a48502bbea4b284da790d0"
|
||||||
|
dependencies = [
|
||||||
|
"hex",
|
||||||
|
"rust-crypto",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "aho-corasick"
|
name = "aho-corasick"
|
||||||
version = "0.7.15"
|
version = "0.7.15"
|
||||||
@ -170,6 +180,7 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
|
|||||||
name = "katrunner"
|
name = "katrunner"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"aes_ctr_drbg",
|
||||||
"hex",
|
"hex",
|
||||||
"katwalk",
|
"katwalk",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
|
@ -10,4 +10,5 @@ pqc-sys = { path = "../../src/rustapi/pqc-sys" }
|
|||||||
hex = "0.4.2"
|
hex = "0.4.2"
|
||||||
threadpool = "1.8.1"
|
threadpool = "1.8.1"
|
||||||
rust-crypto = "^0.2"
|
rust-crypto = "^0.2"
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
|
aes_ctr_drbg = "0.0.2"
|
@ -1,113 +0,0 @@
|
|||||||
//
|
|
||||||
// Modified version of AES-CTR-DRBG by Bassham & Lawrence.
|
|
||||||
// Copyright © 2017 Bassham, Lawrence E (Fed). All rights reserved.
|
|
||||||
// Rust implementation by K. Kwiatkowski. All rights reserved.
|
|
||||||
//
|
|
||||||
pub mod ctr {
|
|
||||||
use crypto::aes;
|
|
||||||
use crypto::buffer::{ RefReadBuffer, RefWriteBuffer, BufferResult };
|
|
||||||
|
|
||||||
pub struct DrbgCtx{
|
|
||||||
pub reseed_counter: usize,
|
|
||||||
pub key: [u8;32],
|
|
||||||
pub ctr: [u8;16]
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DrbgCtx {
|
|
||||||
const CTR_LEN: usize = 16;
|
|
||||||
const KEY_LEN: usize = 32;
|
|
||||||
pub const fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
reseed_counter: 0,
|
|
||||||
key: [0; DrbgCtx::KEY_LEN],
|
|
||||||
ctr: [0; DrbgCtx::CTR_LEN]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn inc(&mut self) {
|
|
||||||
for i in 0..16 {
|
|
||||||
let j = 15-i;
|
|
||||||
if self.ctr[j] == 0xFF {
|
|
||||||
self.ctr[j] = 0
|
|
||||||
} else {
|
|
||||||
self.ctr[j] = self.ctr[j] + 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn process_aes_block(&self, block: &mut [u8]) {
|
|
||||||
let mut e = aes::ecb_encryptor(
|
|
||||||
aes::KeySize::KeySize256,
|
|
||||||
&self.key,
|
|
||||||
crypto::blockmodes::NoPadding);
|
|
||||||
let mut r = RefReadBuffer::new(&self.ctr);
|
|
||||||
let mut w = RefWriteBuffer::new(block);
|
|
||||||
match e.encrypt(&mut r, &mut w, true).unwrap() {
|
|
||||||
BufferResult::BufferOverflow => panic!("Wrong implementation"),
|
|
||||||
BufferResult::BufferUnderflow => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update(&mut self, seed: &[u8]) {
|
|
||||||
let mut t = vec![0;48];
|
|
||||||
|
|
||||||
for i in 0..3 {
|
|
||||||
self.inc();
|
|
||||||
self.process_aes_block(&mut t[i*16..]);
|
|
||||||
}
|
|
||||||
for i in 0..seed.len() {
|
|
||||||
t[i] ^= seed[i];
|
|
||||||
}
|
|
||||||
for i in 0..32 {
|
|
||||||
self.key[i] = t[i];
|
|
||||||
}
|
|
||||||
for i in 32..48 {
|
|
||||||
self.ctr[i-32] = t[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init(&mut self, entropy: &[u8], diversifier: Vec<u8>) {
|
|
||||||
let mut m = vec![0;48];
|
|
||||||
for i in 0..48 {
|
|
||||||
m[i] = entropy[i];
|
|
||||||
}
|
|
||||||
if diversifier.len() >= 48 {
|
|
||||||
for i in 0..48 {
|
|
||||||
m[i] ^= diversifier[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.key = [0; DrbgCtx::KEY_LEN];
|
|
||||||
self.ctr = [0; DrbgCtx::CTR_LEN];
|
|
||||||
self.update(m.as_slice());
|
|
||||||
self.reseed_counter = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_random(&mut self, data: &mut [u8]) {
|
|
||||||
let mut i = 0;
|
|
||||||
let mut b = vec![0; 16];
|
|
||||||
let mut l = data.len();
|
|
||||||
|
|
||||||
while l > 0 {
|
|
||||||
self.inc();
|
|
||||||
self.process_aes_block(&mut b);
|
|
||||||
|
|
||||||
if l > 15 {
|
|
||||||
for k in 0..16 {
|
|
||||||
data[i+k] = b[k];
|
|
||||||
}
|
|
||||||
i += 16;
|
|
||||||
l -= 16;
|
|
||||||
} else {
|
|
||||||
for k in 0..l {
|
|
||||||
data[i+k] = b[k];
|
|
||||||
}
|
|
||||||
l = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
self.update(Vec::new().as_slice());
|
|
||||||
self.reseed_counter = self.reseed_counter+1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -5,14 +5,12 @@ use std::env;
|
|||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use threadpool::ThreadPool;
|
use threadpool::ThreadPool;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
use drbg::ctr::DrbgCtx;
|
use aes_ctr_drbg::DrbgCtx;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
mod drbg;
|
|
||||||
|
|
||||||
// Used for signature algorithm registration
|
// Used for signature algorithm registration
|
||||||
macro_rules! REG_SIGN {
|
macro_rules! REG_SIGN {
|
||||||
($ID:expr,$F:expr) => {
|
($ID:expr,$F:expr) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user