This repository was archived by the owner on May 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths-aes.rb
183 lines (144 loc) · 4.31 KB
/
s-aes.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#! /usr/bin/env ruby
############################
# The Simple AES Algorithm #
# #
# by Benjamin Mestdagh #
# #
############################
# Some matrices we need #
$sbox = [ [0x9, 0x4, 0xA, 0xB],
[0xD, 0x1, 0x8, 0x5],
[0x6, 0x2, 0x0, 0x3],
[0xC, 0xE, 0xF, 0x7] ]
$multiplyByFour = [0x0, 0x4, 0x8, 0xC,
0x3, 0x7, 0xB, 0xF,
0x6, 0x2, 0xE, 0xA,
0x5, 0x1, 0xD, 0x9 ]
# The functions we need #
def putInMatrix(number, matrix)
nibbles = ((Math.log(number)/Math.log(2)).ceil() / 4.0).ceil()
shift = (nibbles * 4) - 4
for i in 1..nibbles
if i.modulo(2) == 0 then row = 1 else row = 0 end
if i < 3 then col = 0 else col = 1 end
matrix[row][col] = (number >> shift) & 0xF
shift -= 4
end
end
def printMatrix(matrix)
for col in 0...matrix.length
for row in 0...matrix[col].length
result = matrix[row][col]
puts "S%d,%d = %x - %04b" % [row, col, result, result]
end
end
end
def numberFromMatrix(matrix)
result = 0
for col in 0...matrix[0].length
for row in 0...matrix.length
result = result << 4
result += matrix[row][col]
end
end
return result
end
def rotateNibbles(number)
newLeftPart = number & 0xF
number = number >> 4
newLeftPart = newLeftPart << 4
number += newLeftPart
return number
end
def addKey(matrix, key)
for i in 0...matrix.length
for j in 0...matrix[i].length
matrix[i][j] = matrix[i][j] ^ key[i][j]
end
end
end
def shiftRow(matrix)
matrix[1][0] = matrix[1][0] ^ matrix[1][1]
matrix[1][1] = matrix[1][0] ^ matrix[1][1]
matrix[1][0] = matrix[1][0] ^ matrix[1][1]
end
def substituteNibbles(matrix)
for i in 0...matrix.length
for j in 0...matrix[i].length
sboxRow = (matrix[i][j] >> 2) & 0b11
sboxCol = matrix[i][j] & 0b11
matrix[i][j] = $sbox[sboxRow][sboxCol]
end
end
end
def mixColumns(matrix)
tempState = Array.new(2) { Array.new(2) }
for col in 0...matrix.length
for row in 0...matrix[col].length
otherRow = (row + 1) % matrix.length
tempState[row][col] = matrix[row][col] ^ ($multiplyByFour[matrix[otherRow][col]])
end
end
cloneMatrix(matrix, tempState)
end
def cloneMatrix(matrix1, matrix2)
for col in 0...matrix1.length
for row in 0...matrix1.length
matrix1[row][col] = matrix2[row][col]
end
end
end
def getNextKeys(key)
rcon1 = 0b10000000
rcon2 = 0b00110000
w0 = (key & 0xFF00) >> 8
w1 = key & 0xFF
w1rot = rotateNibbles(w1)
wArr = [[(w1rot & 0xF0) >> 4],[w1rot & 0xF]]
substituteNibbles(wArr)
w2 = w0 ^ rcon1 ^ numberFromMatrix(wArr)
w3 = w2 ^ w1
w3rot = rotateNibbles(w3)
wArr = [[(w3rot & 0xF0) >> 4],[w3rot & 0xF]]
substituteNibbles(wArr)
w4 = w2 ^ rcon2 ^ numberFromMatrix(wArr)
w5 = w4 ^ w3
key1 = (w2 << 8) + w3
key2 = (w4 << 8) + w5
return [key1, key2]
end
def printMessage(message, result)
puts "%s:\n\t%016b\n\t%04x" % [message, result, result]
end
### Main starting point ###
message = 0b0110111101101011
key = 0b1010011100111011
puts "S-AES Algorithm"
puts "---------------\n"
printMessage("Message", message)
printMessage("Key", key)
state = Array.new(2) { Array.new(2) }
putInMatrix(message, state)
keyMatrix = Array.new(2) { Array.new(2) }
putInMatrix(key, keyMatrix)
addKey(state, keyMatrix)
printMessage("Add key", numberFromMatrix(state))
substituteNibbles(state)
printMessage("Substitute nibbles", numberFromMatrix(state))
shiftRow(state)
printMessage("Shift row", numberFromMatrix(state))
mixColumns(state)
printMessage("Mix columns", numberFromMatrix(state))
nextKeys = getNextKeys(key)
printMessage("Key 2", nextKeys[0])
printMessage("Key 3", nextKeys[1])
putInMatrix(nextKeys[0], keyMatrix)
addKey(state, keyMatrix)
printMessage("Add key", numberFromMatrix(state))
substituteNibbles(state)
printMessage("Substitute nibbles", numberFromMatrix(state))
shiftRow(state)
printMessage("Shift row", numberFromMatrix(state))
putInMatrix(nextKeys[1], keyMatrix)
addKey(state, keyMatrix)
printMessage("Result", numberFromMatrix(state))