Compare commits
No commits in common. "fbd946c7ecc0f6a7e6d7e9d2741abf359de8458a" and "de7b7536ce74b18bdde9b27fd35cf6a44dd4ad95" have entirely different histories.
fbd946c7ec
...
de7b7536ce
1 changed files with 38 additions and 38 deletions
76
src/main.rs
76
src/main.rs
|
|
@ -7,7 +7,7 @@ use std::{
|
||||||
|
|
||||||
use aes::{
|
use aes::{
|
||||||
Aes128,
|
Aes128,
|
||||||
cipher::{self, BlockEncrypt, KeyInit, generic_array::GenericArray},
|
cipher::{BlockEncrypt, KeyInit, generic_array::GenericArray},
|
||||||
};
|
};
|
||||||
use rayon::iter::{IntoParallelIterator, ParallelIterator};
|
use rayon::iter::{IntoParallelIterator, ParallelIterator};
|
||||||
|
|
||||||
|
|
@ -30,21 +30,21 @@ static RSBOX: [u8; 256] = [
|
||||||
0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d,
|
0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d,
|
||||||
];
|
];
|
||||||
|
|
||||||
fn calculate_models(ciphertexts: &[[u8; 16]]) -> Vec<Vec<[u8; 16]>> {
|
fn calculate_models(ciphertexts: &[[u8; 16]]) -> Vec<[[u8; 16]; 256]> {
|
||||||
let mut r = vec![Vec::with_capacity(ciphertexts.len()); 256];
|
ciphertexts
|
||||||
|
.iter()
|
||||||
|
.map(|c| {
|
||||||
|
let mut row = [[0; 16]; 256];
|
||||||
|
|
||||||
for (i, c) in ciphertexts.iter().enumerate() {
|
for i in 0..256 {
|
||||||
for j in 0..256 {
|
for j in 0..16 {
|
||||||
let mut row = [0; 16];
|
row[i][j] = RSBOX[(c[j] ^ (i as u8)) as usize];
|
||||||
|
}
|
||||||
for k in 0..16 {
|
|
||||||
row[k] = RSBOX[(c[k] ^ (j as u8)) as usize];
|
|
||||||
}
|
}
|
||||||
r[j].push(row);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
r
|
row
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_msgs(path: impl AsRef<Path>) -> Vec<[u8; 16]> {
|
fn read_msgs(path: impl AsRef<Path>) -> Vec<[u8; 16]> {
|
||||||
|
|
@ -78,35 +78,38 @@ fn read_msgs(path: impl AsRef<Path>) -> Vec<[u8; 16]> {
|
||||||
|
|
||||||
const TRACES: usize = 87;
|
const TRACES: usize = 87;
|
||||||
|
|
||||||
fn read_traces(path: impl AsRef<Path>, capacity: usize) -> Vec<Vec<u8>> {
|
fn read_traces(path: impl AsRef<Path>) -> Vec<[u8; TRACES]> {
|
||||||
let file = std::fs::File::open(path).unwrap();
|
let file = std::fs::File::open(path).unwrap();
|
||||||
|
|
||||||
let bufreader = std::io::BufReader::new(file);
|
let bufreader = std::io::BufReader::new(file);
|
||||||
|
|
||||||
let mut r: Vec<Vec<u8>> = (0..TRACES).map(|_| vec![0; capacity]).collect();
|
bufreader
|
||||||
|
.lines()
|
||||||
|
.map(|l| {
|
||||||
|
let l = l.unwrap();
|
||||||
|
|
||||||
for (i, l) in bufreader.lines().enumerate() {
|
let mut trace = [0; TRACES];
|
||||||
let l = l.unwrap();
|
|
||||||
|
|
||||||
for (j, t) in l
|
for (i, t) in l
|
||||||
.trim()
|
.trim()
|
||||||
.split(',')
|
.split(',')
|
||||||
.map(|t| t.parse::<u8>().unwrap())
|
.map(|t| t.parse::<u8>().unwrap())
|
||||||
.enumerate()
|
.enumerate()
|
||||||
{
|
{
|
||||||
r[j][i] = t;
|
trace[i] = t;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
r
|
trace
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn correlation(
|
fn correlation(
|
||||||
bit: usize,
|
bit: usize,
|
||||||
key_hypothesis: usize,
|
key_hypothesis: usize,
|
||||||
trace_index: usize,
|
trace_index: usize,
|
||||||
cyphtertext: &[Vec<[u8; 16]>],
|
cyphtertext: &[[[u8; 16]; 256]],
|
||||||
traces: &[Vec<u8>],
|
traces: &[[u8; TRACES]],
|
||||||
) -> f64 {
|
) -> f64 {
|
||||||
let mut x = 0i64;
|
let mut x = 0i64;
|
||||||
let mut y = 0i64;
|
let mut y = 0i64;
|
||||||
|
|
@ -114,9 +117,9 @@ fn correlation(
|
||||||
let mut xsqr = 0i64;
|
let mut xsqr = 0i64;
|
||||||
let mut ysqr = 0i64;
|
let mut ysqr = 0i64;
|
||||||
|
|
||||||
for i in 0..traces[0].len() {
|
for i in 0..traces.len() {
|
||||||
let xi = (cyphtertext[key_hypothesis][i][bit / 8] & (1 << (bit % 8))) as i64;
|
let xi = (cyphtertext[i][key_hypothesis][bit / 8] & (1 << (bit % 8))) as i64;
|
||||||
let yi = traces[trace_index][i] as i64;
|
let yi = traces[i][trace_index] as i64;
|
||||||
|
|
||||||
x += xi;
|
x += xi;
|
||||||
y += yi;
|
y += yi;
|
||||||
|
|
@ -125,7 +128,7 @@ fn correlation(
|
||||||
ysqr += yi * yi;
|
ysqr += yi * yi;
|
||||||
}
|
}
|
||||||
|
|
||||||
let n = traces[0].len() as i64;
|
let n = traces.len() as i64;
|
||||||
let num = (n * xy - x * y) as f64;
|
let num = (n * xy - x * y) as f64;
|
||||||
let denom = f64::sqrt((n * xsqr - x * x) as f64) * f64::sqrt((n * ysqr - y * y) as f64);
|
let denom = f64::sqrt((n * xsqr - x * x) as f64) * f64::sqrt((n * ysqr - y * y) as f64);
|
||||||
|
|
||||||
|
|
@ -142,10 +145,7 @@ fn main() {
|
||||||
println!("calculate models: {:?}", start.elapsed());
|
println!("calculate models: {:?}", start.elapsed());
|
||||||
|
|
||||||
let start = std::time::Instant::now();
|
let start = std::time::Instant::now();
|
||||||
let traces = read_traces(
|
let traces = read_traces("./alan/Task-3-example_traces/test_traces.csv");
|
||||||
"./alan/Task-3-example_traces/test_traces.csv",
|
|
||||||
cyphertext.len(),
|
|
||||||
);
|
|
||||||
println!("read traces: {:?}", start.elapsed());
|
println!("read traces: {:?}", start.elapsed());
|
||||||
|
|
||||||
let start = std::time::Instant::now();
|
let start = std::time::Instant::now();
|
||||||
|
|
@ -153,7 +153,7 @@ fn main() {
|
||||||
let (max_index, max) = (0..256)
|
let (max_index, max) = (0..256)
|
||||||
.into_par_iter()
|
.into_par_iter()
|
||||||
.map(|key_hypothesis| {
|
.map(|key_hypothesis| {
|
||||||
let m = (0..TRACES)
|
let m = (0..traces[0].len())
|
||||||
.map(|trace_index| {
|
.map(|trace_index| {
|
||||||
correlation(bit, key_hypothesis, trace_index, &models, &traces).abs()
|
correlation(bit, key_hypothesis, trace_index, &models, &traces).abs()
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue