Class: Crypt::Rijndael
Constant Summary
Crypt::RijndaelTables::AlogTable, Crypt::RijndaelTables::IG, Crypt::RijndaelTables::LogTable, Crypt::RijndaelTables::Rcon, Crypt::RijndaelTables::S, Crypt::RijndaelTables::Shifts, Crypt::RijndaelTables::Si
Constants included
from CBC
CBC::ULONG
Instance Method Summary
collapse
Methods included from CBC
#carefully_open_file, #decrypt_file, #decrypt_stream, #decrypt_string, #encrypt_file, #encrypt_stream, #encrypt_string, #generate_initialization_vector
Constructor Details
#initialize(userKey, keyBits = 256, blockBits = 128) ⇒ Rijndael
Returns a new instance of Rijndael.
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
|
# File 'lib/flatulent/crypt/rijndael.rb', line 19
def initialize(userKey, keyBits = 256, blockBits = 128)
case keyBits
when 128
@keyWords = 4
when 192
@keyWords = 6
when 256
@keyWords = 8
else raise "The key must be 128, 192, or 256 bits long."
end
case (keyBits >= blockBits) ? keyBits : blockBits
when 128
@rounds = 10
when 192
@rounds = 12
when 256
@rounds = 14
else raise "The key and block sizes must be 128, 192, or 256 bits long."
end
case blockBits
when 128
@blockSize = 16
@blockWords = 4
@shiftIndex = 0
when 192
@blockSize = 24
@blockWords = 6
@shiftIndex = 1
when 256
@blockSize = 32
@blockWords = 8
@shiftIndex = 2
else raise "The block size must be 128, 192, or 256 bits long."
end
uk = userKey.unpack('C'*userKey.length)
maxUsefulSizeOfUserKey = (keyBits/8)
uk = uk[0..maxUsefulSizeOfUserKey-1]
padding = 0
if (userKey.length < keyBits/8)
shortfallInUserKey = (keyBits/8 - userKey.length)
shortfallInUserKey.times { uk << padding }
end
@key = [[], [], [], []]
0.upto(uk.length-1) { |pos|
@key[pos % 4][pos / 4] = uk[pos]
}
@roundKeys = generate_key_schedule(@key, keyBits, blockBits)
end
|
Instance Method Details
#add_round_key(blockArray, roundKey) ⇒ Object
87
88
89
90
91
92
93
94
|
# File 'lib/flatulent/crypt/rijndael.rb', line 87
def add_round_key(blockArray, roundKey)
0.upto(3) { |i|
0.upto(@blockWords) { |j|
blockArray[i][j] ^= roundKey[i][j]
}
}
return(blockArray)
end
|
#block_size ⇒ Object
72
73
74
|
# File 'lib/flatulent/crypt/rijndael.rb', line 72
def block_size
return(@blockSize)
end
|
#decrypt_block(block) ⇒ Object
253
254
255
256
257
258
259
260
261
262
263
264
265
|
# File 'lib/flatulent/crypt/rijndael.rb', line 253
def decrypt_block(block)
raise "block must be #{@blockSize} bytes long" if (block.length() != @blockSize)
blockArray = [[], [], [], []]
0.upto(@blockSize - 1) { |pos|
blockArray[pos % 4][pos / 4] = block[pos]
}
decryptedBlock = decrypt_byte_array(blockArray)
decrypted = ""
0.upto(@blockSize - 1) { |pos|
decrypted << decryptedBlock[pos % 4][pos / 4]
}
return(decrypted)
end
|
#decrypt_byte_array(blockArray) ⇒ Object
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
# File 'lib/flatulent/crypt/rijndael.rb', line 236
def decrypt_byte_array(blockArray)
blockArray = add_round_key(blockArray, @roundKeys[@rounds])
blockArray = substitution(blockArray,Si)
blockArray = shift_rows(blockArray,1)
(@rounds-1).downto(1) { |round|
blockArray = add_round_key(blockArray, @roundKeys[round])
blockArray = inverse_mix_columns(blockArray)
blockArray = substitution(blockArray, Si)
blockArray = shift_rows(blockArray, 1)
}
blockArray = add_round_key(blockArray, @roundKeys[0])
return(blockArray)
end
|
#encrypt_block(block) ⇒ Object
221
222
223
224
225
226
227
228
229
230
231
232
233
|
# File 'lib/flatulent/crypt/rijndael.rb', line 221
def encrypt_block(block)
raise "block must be #{@blockSize} bytes long" if (block.length() != @blockSize)
blockArray = [[], [], [], []]
0.upto(@blockSize - 1) { |pos|
blockArray[pos % 4][pos / 4] = block[pos]
}
encryptedBlock = encrypt_byte_array(blockArray)
encrypted = ""
0.upto(@blockSize - 1) { |pos|
encrypted << encryptedBlock[pos % 4][pos / 4]
}
return(encrypted)
end
|
#encrypt_byte_array(blockArray) ⇒ Object
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
# File 'lib/flatulent/crypt/rijndael.rb', line 205
def encrypt_byte_array(blockArray)
blockArray = add_round_key(blockArray, @roundKeys[0])
1.upto(@rounds - 1) { |round|
blockArray = substitution(blockArray, S)
blockArray = shift_rows(blockArray, 0)
blockArray = mix_columns(blockArray)
blockArray = add_round_key(blockArray, @roundKeys[round])
}
blockArray = substitution(blockArray,S)
blockArray = shift_rows(blockArray,0)
blockArray = add_round_key(blockArray, @roundKeys[@rounds])
return(blockArray)
end
|
#generate_key_schedule(k, keyBits, blockBits) ⇒ Object
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
# File 'lib/flatulent/crypt/rijndael.rb', line 150
def generate_key_schedule(k, keyBits, blockBits)
tk = k[0..3][0..@keyWords-1]
keySched = []
(@rounds + 1).times { keySched << [[], [], [], []] }
t = 0
j = 0
while ((j < @keyWords) && (t < (@rounds+1)*@blockWords))
0.upto(3) { |i|
keySched[t / @blockWords][i][t % @blockWords] = tk[i][j]
}
j += 1
t += 1
end
rconIndex = 0
while (t < (@rounds+1)*@blockWords)
0.upto(3) { |i|
tk[i][0] ^= S[tk[(i + 1) % 4][@keyWords - 1]]
}
tk[0][0] ^= Rcon[rconIndex]
rconIndex = rconIndex.next
if (@keyWords != 8)
1.upto(@keyWords - 1) { |j|
0.upto(3) { |i|
tk[i][j] ^= tk[i][j-1];
}
}
else
1.upto(@keyWords/2 - 1) { |j|
0.upto(3) { |i|
tk[i][j] ^= tk[i][j-1]
}
}
0.upto(3) { |i|
tk[i][@keyWords/2] ^= S[tk[i][@keyWords/2 - 1]]
}
(@keyWords/2 + 1).upto(@keyWords - 1) { |j|
0.upto(3) { |i|
tk[i][j] ^= tk[i][j-1]
}
}
end
j = 0
while ((j < @keyWords) && (t < (@rounds+1) * @blockWords))
0.upto(3) { |i|
keySched[t / @blockWords][i][t % @blockWords] = tk[i][j]
}
j += 1
t += 1
end
end
return(keySched)
end
|
#inverse_mix_columns(blockArray) ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/flatulent/crypt/rijndael.rb', line 136
def inverse_mix_columns(blockArray)
unmixed = [[], [], [], []]
0.upto(@blockWords-1) { |j|
0.upto(3) { |i|
unmixed[i][j] = mul(0xe, blockArray[i][j]) ^
mul(0xb, blockArray[(i + 1) % 4][j]) ^
mul(0xd, blockArray[(i + 2) % 4][j]) ^
mul(0x9, blockArray[(i + 3) % 4][j])
}
}
return(unmixed)
end
|
#mix_columns(blockArray) ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/flatulent/crypt/rijndael.rb', line 122
def mix_columns(blockArray)
mixed = [[], [], [], []]
0.upto(@blockWords-1) { |j|
0.upto(3) { |i|
mixed[i][j] = mul(2,blockArray[i][j]) ^
mul(3,blockArray[(i + 1) % 4][j]) ^
blockArray[(i + 2) % 4][j] ^
blockArray[(i + 3) % 4][j]
}
}
return(mixed)
end
|
#mul(a, b) ⇒ Object
77
78
79
80
81
82
83
84
|
# File 'lib/flatulent/crypt/rijndael.rb', line 77
def mul(a, b)
if ((a ==0) | (b == 0))
result = 0
else
result = AlogTable[(LogTable[a] + LogTable[b]) % 255]
end
return(result)
end
|
#shift_rows(blockArray, direction) ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/flatulent/crypt/rijndael.rb', line 97
def shift_rows(blockArray, direction)
tmp = []
1.upto(3) { |i|
0.upto(@blockWords-1) { |j|
tmp[j] = blockArray[i][(j + Shifts[@shiftIndex][i][direction]) % @blockWords]
}
0.upto(@blockWords-1) { |j|
blockArray[i][j] = tmp[j]
}
}
return(blockArray)
end
|
#substitution(blockArray, sBox) ⇒ Object
111
112
113
114
115
116
117
118
119
|
# File 'lib/flatulent/crypt/rijndael.rb', line 111
def substitution(blockArray, sBox)
0.upto(3) { |i|
0.upto(@blockWords-1) { |j|
blockArray[i][j] = sBox[blockArray[i][j]]
}
}
return(blockArray)
end
|