Created
April 4, 2012 16:45
-
-
Save siddMahen/2303727 to your computer and use it in GitHub Desktop.
Find Linear Patterns in 16x16 bit S-Boxes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Useful when performing linear cryptanalysis of block ciphers w/ S-Boxes | |
// Example S-Boxes used in the GHOST algorithm | |
var sb1 = [ 4, 10, 9, 2, 13, 8, 0, 14, 6, 11, 1, 12, 7, 15, 5, 3 ], | |
sb2 = [ 14, 11, 4, 12, 6, 13, 15, 10, 2, 3, 8, 1, 0, 7, 5, 9 ], | |
sb3 = [ 5, 8, 1, 13, 10, 3, 4, 2, 14, 15, 12, 7, 6, 0, 9, 11 ], | |
sb4 = [ 7, 13, 10, 1, 0, 8, 9, 15, 14, 4, 6, 12, 11, 2, 5, 3 ], | |
sb5 = [ 6, 12, 7, 1, 5, 15, 13, 8, 4, 10, 9, 14, 0, 2, 11, 2 ], | |
sb6 = [ 4, 11, 10, 0, 7, 2, 1, 13, 3, 6, 8, 5, 9, 12, 15, 14 ], | |
sb7 = [ 13, 11, 4, 1, 3, 15, 5, 9, 0, 10, 14, 7, 6, 8, 2, 12 ], | |
sb8 = [ 1, 15, 13, 0, 5, 7, 10, 4, 9, 2, 3, 14, 6, 11, 8, 12 ]; | |
function findRel(sbox, X, Y){ | |
var works = 0; | |
for(var i = 0, len = sbox.length; i < len; i++){ | |
var x = i, | |
x4 = (x & 1) ? 1 : 0, | |
x3 = (x & 2) ? 1 : 0, | |
x2 = (x & 4) ? 1 : 0, | |
x1 = (x & 8) ? 1 : 0; | |
var y = sbox[i], | |
y4 = (y & 1) ? 1 : 0, | |
y3 = (y & 2) ? 1 : 0, | |
y2 = (y & 4) ? 1 : 0, | |
y1 = (y & 8) ? 1 : 0; | |
var Xres = ((X[0]*x1) ^ (X[1]*x2) ^ (X[2]*x3) ^ (X[3]*x4)), | |
Yres = ((Y[0]*y1) ^ (Y[1]*y2) ^ (Y[2]*y3) ^ (Y[3]*y4)); | |
if(Xres == Yres) | |
works++; | |
} | |
return works; | |
} | |
function everyRel(sbox){ | |
var picks = []; | |
for(var i = 0, len = sbox.length; i < len; i++){ | |
var b = i, | |
b4 = (b & 1) ? 1 : 0, | |
b3 = (b & 2) ? 1 : 0, | |
b2 = (b & 4) ? 1 : 0, | |
b1 = (b & 8) ? 1 : 0; | |
for(var j = 0; j < len; j++){ | |
var B = j, | |
B4 = (B & 1) ? 1 : 0, | |
B3 = (B & 2) ? 1 : 0, | |
B2 = (B & 4) ? 1 : 0, | |
B1 = (B & 8) ? 1 : 0; | |
var res = findRel(sbox, [b1, b2, b3, b4], [B1, B2, B3, B4]); | |
// arbitrary limits | |
if((res > 10 || res < 4) && res !== 16){ | |
var str = "X:"+b1+""+b2+""+b3+""+b4+"\n"; | |
str += "Y:"+B1+""+B2+""+B3+""+B4+"\n"; | |
str += res+"\n"; | |
picks.push(str); | |
} | |
} | |
} | |
return picks; | |
} | |
function pprint(picks){ | |
for(var i = 0; i < 3; i++){ | |
console.log(picks[i]); | |
} | |
} | |
console.log("S-box 1"); | |
pprint(everyRel(sb1)); | |
console.log("S-box 2"); | |
pprint(everyRel(sb2)); | |
console.log("S-box 3"); | |
pprint(everyRel(sb3)); | |
console.log("S-box 4"); | |
pprint(everyRel(sb4)); | |
console.log("S-box 5"); | |
pprint(everyRel(sb5)); | |
console.log("S-box 6"); | |
pprint(everyRel(sb6)); | |
console.log("S-box 7"); | |
pprint(everyRel(sb7)); | |
console.log("S-box 8"); | |
pprint(everyRel(sb8)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment