Compare commits
2 commits
de7b7536ce
...
fbd946c7ec
| Author | SHA1 | Date | |
|---|---|---|---|
| fbd946c7ec | |||
| 422e3018e2 |
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::{BlockEncrypt, KeyInit, generic_array::GenericArray},
|
cipher::{self, 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<[[u8; 16]; 256]> {
|
fn calculate_models(ciphertexts: &[[u8; 16]]) -> Vec<Vec<[u8; 16]>> {
|
||||||
ciphertexts
|
let mut r = vec![Vec::with_capacity(ciphertexts.len()); 256];
|
||||||
.iter()
|
|
||||||
.map(|c| {
|
|
||||||
let mut row = [[0; 16]; 256];
|
|
||||||
|
|
||||||
for i in 0..256 {
|
for (i, c) in ciphertexts.iter().enumerate() {
|
||||||
for j in 0..16 {
|
for j in 0..256 {
|
||||||
row[i][j] = RSBOX[(c[j] ^ (i as u8)) as usize];
|
let mut row = [0; 16];
|
||||||
}
|
|
||||||
|
for k in 0..16 {
|
||||||
|
row[k] = RSBOX[(c[k] ^ (j as u8)) as usize];
|
||||||
}
|
}
|
||||||
|
r[j].push(row);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
row
|
r
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_msgs(path: impl AsRef<Path>) -> Vec<[u8; 16]> {
|
fn read_msgs(path: impl AsRef<Path>) -> Vec<[u8; 16]> {
|
||||||
|
|
@ -78,38 +78,35 @@ 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>) -> Vec<[u8; TRACES]> {
|
fn read_traces(path: impl AsRef<Path>, capacity: usize) -> Vec<Vec<u8>> {
|
||||||
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);
|
||||||
|
|
||||||
bufreader
|
let mut r: Vec<Vec<u8>> = (0..TRACES).map(|_| vec![0; capacity]).collect();
|
||||||
.lines()
|
|
||||||
.map(|l| {
|
|
||||||
let l = l.unwrap();
|
|
||||||
|
|
||||||
let mut trace = [0; TRACES];
|
for (i, l) in bufreader.lines().enumerate() {
|
||||||
|
let l = l.unwrap();
|
||||||
|
|
||||||
for (i, t) in l
|
for (j, t) in l
|
||||||
.trim()
|
.trim()
|
||||||
.split(',')
|
.split(',')
|
||||||
.map(|t| t.parse::<u8>().unwrap())
|
.map(|t| t.parse::<u8>().unwrap())
|
||||||
.enumerate()
|
.enumerate()
|
||||||
{
|
{
|
||||||
trace[i] = t;
|
r[j][i] = t;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
trace
|
r
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn correlation(
|
fn correlation(
|
||||||
bit: usize,
|
bit: usize,
|
||||||
key_hypothesis: usize,
|
key_hypothesis: usize,
|
||||||
trace_index: usize,
|
trace_index: usize,
|
||||||
cyphtertext: &[[[u8; 16]; 256]],
|
cyphtertext: &[Vec<[u8; 16]>],
|
||||||
traces: &[[u8; TRACES]],
|
traces: &[Vec<u8>],
|
||||||
) -> f64 {
|
) -> f64 {
|
||||||
let mut x = 0i64;
|
let mut x = 0i64;
|
||||||
let mut y = 0i64;
|
let mut y = 0i64;
|
||||||
|
|
@ -117,9 +114,9 @@ fn correlation(
|
||||||
let mut xsqr = 0i64;
|
let mut xsqr = 0i64;
|
||||||
let mut ysqr = 0i64;
|
let mut ysqr = 0i64;
|
||||||
|
|
||||||
for i in 0..traces.len() {
|
for i in 0..traces[0].len() {
|
||||||
let xi = (cyphtertext[i][key_hypothesis][bit / 8] & (1 << (bit % 8))) as i64;
|
let xi = (cyphtertext[key_hypothesis][i][bit / 8] & (1 << (bit % 8))) as i64;
|
||||||
let yi = traces[i][trace_index] as i64;
|
let yi = traces[trace_index][i] as i64;
|
||||||
|
|
||||||
x += xi;
|
x += xi;
|
||||||
y += yi;
|
y += yi;
|
||||||
|
|
@ -128,7 +125,7 @@ fn correlation(
|
||||||
ysqr += yi * yi;
|
ysqr += yi * yi;
|
||||||
}
|
}
|
||||||
|
|
||||||
let n = traces.len() as i64;
|
let n = traces[0].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);
|
||||||
|
|
||||||
|
|
@ -145,7 +142,10 @@ 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("./alan/Task-3-example_traces/test_traces.csv");
|
let traces = read_traces(
|
||||||
|
"./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[0].len())
|
let m = (0..TRACES)
|
||||||
.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