Skip to content

Instantly share code, notes, and snippets.

@10maurycy10
Last active September 21, 2021 18:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 10maurycy10/551aca1145778086903542385cb65c0b to your computer and use it in GitHub Desktop.
Save 10maurycy10/551aca1145778086903542385cb65c0b to your computer and use it in GitHub Desktop.
a simple rust snippet to compute an AES/Rijndael Sbox.
// Substitution BOX, a lookup table to optimyze the substitution step
pub type Sbox = [u8; 256];
// shift 8 bits left
fn rot_l8(x: u8,shift: isize) -> u8 {
// check that input is in bounds
assert!(shift < 8);
assert!(shift > -8);
// actualy do it
((x) << (shift)) | ((x) >> (8 - (shift)))
}
// code addapeded from https://en.wikipedia.org/wiki/Rijndael_S-box
pub fn make_sub_box() -> Sbox {
let mut sbox: Sbox = [0_u8; 256];
let mut p: u8 = 1;
let mut q: u8 = 1;
loop {
let x = match (p & 0x80) {
0 => 0,
_ => 0x1B
};
p = p ^ (p << 1) ^ x;
q ^= q << 1;
q ^= q << 2;
q ^= q << 4;
q ^= match (q & 0x80) {
0 => 0,
_ => 0x09
};
let transformed = q ^ rot_l8(q, 1) ^ rot_l8(q, 2) ^ rot_l8(q, 3) ^ rot_l8(q, 4);
sbox[p as usize] = transformed ^ 0x63;
// stop if p is one
if p == 1 {
break;
}
}
//zero is specal
sbox[0] = 0x63;
return sbox;
}
// we need a way to undo the sbox.
pub fn invert_sub_box(sbox :&Sbox) -> Sbox {
// buffer for inverted sbox
let mut isbox = [0_u8; 256];
// i is the location in the sbox, v is the value at that location
for (i, v) in sbox.iter().enumerate() {
isbox[*v as usize] = i as u8;
}
return isbox;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment