Class: Crypt::Rijndael

Inherits:
Object
  • Object
show all
Includes:
CBC, RijndaelTables
Defined in:
lib/crypt/rijndael.rb

Constant Summary

Constants included from RijndaelTables

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(user_key, key_bits = 256, block_bits = 128) ⇒ Rijndael

Returns a new instance of Rijndael.



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
# File 'lib/crypt/rijndael.rb', line 16

def initialize(user_key, key_bits = 256, block_bits = 128)
  case key_bits
    when 128
      @key_words = 4
    when 192
      @key_words = 6
    when 256
      @key_words = 8
    else raise "The key must be 128, 192, or 256 bits long."
  end

  case (key_bits >= block_bits) ? key_bits : block_bits
    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 block_bits
    when 128
      @block_size = 16
      @block_words = 4
      @shift_index = 0
    when 192
      @block_size = 24
      @block_words = 6
      @shift_index = 1
    when 256
      @block_size = 32
      @block_words = 8
      @shift_index = 2
    else raise "The block size must be 128, 192, or 256 bits long."
  end

  uk = user_key.unpack('C'*user_key.length)
  max_useful_size_of_user_key = (key_bits/8)
  uk = uk[0..max_useful_size_of_user_key-1]    # truncate
  padding = 0
  if (user_key.length < key_bits/8)
    shortfall_in_user_key = (key_bits/8 - user_key.length)
    shortfall_in_user_key.times { uk << padding }
  end
  @key = [[], [], [], []]
  0.upto(uk.length-1) { |pos|
    @key[pos % 4][pos / 4] = uk[pos]
  }
  @round_keys = generate_key_schedule(@key, key_bits, block_bits)
end

Instance Method Details

#add_round_key(block_array, round_key) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/crypt/rijndael.rb', line 84

def add_round_key(block_array, round_key)
0.upto(3) { |i|
  0.upto(@block_words) { |j|
    block_array[i][j] ^= round_key[i][j]
  }
}
return(block_array)
end

#block_sizeObject



69
70
71
# File 'lib/crypt/rijndael.rb', line 69

def block_size
  return(@block_size) # needed for CBC
end

#decrypt_block(block) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/crypt/rijndael.rb', line 251

def decrypt_block(block)
  raise "block must be #{@block_size} bytes long" if (block.length() != @block_size)
  block_array = [[], [], [], []]
  block_bytes = block.bytes.to_a
  0.upto(@block_size - 1) { |pos|
    block_array[pos % 4][pos / 4] = block_bytes[pos]
  }
  decrypted_block = decrypt_byte_array(block_array)
  decrypted = ""
  0.upto(@block_size - 1) { |pos|
    decrypted << decrypted_block[pos % 4][pos / 4]
  }
  return(decrypted)
end

#decrypt_byte_array(block_array) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/crypt/rijndael.rb', line 234

def decrypt_byte_array(block_array)
  # first special round without inverse_mix_columns
  # add_round_key is an involution - applying it a second time returns the original result
  block_array = add_round_key(block_array, @round_keys[@rounds])
  block_array = substitution(block_array,Si)   # using inverse S-box
  block_array = shift_rows(block_array,1)
  (@rounds-1).downto(1) { |round|
    block_array = add_round_key(block_array, @round_keys[round])
    block_array = inverse_mix_columns(block_array)
    block_array = substitution(block_array, Si)
    block_array = shift_rows(block_array, 1)
  }
  block_array = add_round_key(block_array, @round_keys[0])
  return(block_array)
end

#encrypt_block(block) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/crypt/rijndael.rb', line 218

def encrypt_block(block)
  raise "block must be #{@block_size} bytes long" if (block.length() != @block_size)
  block_array = [[], [], [], []]
  block_bytes = block.bytes.to_a
  0.upto(@block_size - 1) { |pos|
    block_array[pos % 4][pos / 4] = block_bytes[pos]
  }
  encrypted_block = encrypt_byte_array(block_array)
  encrypted = ""
  0.upto(@block_size - 1) { |pos|
    encrypted << encrypted_block[pos % 4][pos / 4]
  }
  return(encrypted)
end

#encrypt_byte_array(block_array) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/crypt/rijndael.rb', line 202

def encrypt_byte_array(block_array)
  block_array = add_round_key(block_array, @round_keys[0])
  1.upto(@rounds - 1) { |round|
    block_array = substitution(block_array, S)
    block_array = shift_rows(block_array, 0)
    block_array = mix_columns(block_array)
    block_array = add_round_key(block_array, @round_keys[round])
  }
  # special round without mix_columns
  block_array = substitution(block_array,S)
  block_array = shift_rows(block_array,0)
  block_array = add_round_key(block_array, @round_keys[@rounds])
  return(block_array)
end

#generate_key_schedule(k, key_bits, block_bits) ⇒ Object



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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/crypt/rijndael.rb', line 147

def generate_key_schedule(k, key_bits, block_bits)
  tk = k[0..3][0..@key_words-1]  # using slice to get a copy instead of a reference
  key_sched = []
  (@rounds + 1).times { key_sched << [[], [], [], []] }
  t = 0
  j = 0
  while ((j < @key_words) && (t < (@rounds+1)*@block_words))
    0.upto(3) { |i|
      key_sched[t / @block_words][i][t % @block_words] = tk[i][j]
    }
    j += 1
    t += 1
  end
  # while not enough round key material collected, calculate new values
  rcon_index = 0
  while (t < (@rounds+1)*@block_words)
    0.upto(3) { |i|
      tk[i][0] ^= S[tk[(i + 1) % 4][@key_words - 1]]
    }
    tk[0][0] ^= Rcon[rcon_index]
    rcon_index = rcon_index.next
    if (@key_words != 8)
      1.upto(@key_words - 1) { |j|
        0.upto(3) { |i|
          tk[i][j] ^= tk[i][j-1];
        }
      }
    else
      1.upto(@key_words/2 - 1) { |j|
        0.upto(3) { |i|
          tk[i][j] ^= tk[i][j-1]
        }
      }
      0.upto(3) { |i|
        tk[i][@key_words/2] ^= S[tk[i][@key_words/2 - 1]]
      }
      (@key_words/2 + 1).upto(@key_words - 1) { |j|
        0.upto(3) { |i|
          tk[i][j] ^= tk[i][j-1]
        }
      }
    end
    j = 0
    while ((j < @key_words) && (t < (@rounds+1) * @block_words))
      0.upto(3) { |i|
        key_sched[t / @block_words][i][t % @block_words] = tk[i][j]
      }
      j += 1
      t += 1
    end
  end
  return(key_sched)
end

#inverse_mix_columns(block_array) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/crypt/rijndael.rb', line 133

def inverse_mix_columns(block_array)
  unmixed = [[], [], [], []]
  0.upto(@block_words-1) { |j|
    0.upto(3) { |i|
      unmixed[i][j] = mul(0xe, block_array[i][j]) ^
        mul(0xb, block_array[(i + 1) % 4][j]) ^
        mul(0xd, block_array[(i + 2) % 4][j]) ^
        mul(0x9, block_array[(i + 3) % 4][j])
    }
  }
   return(unmixed)
end

#mix_columns(block_array) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/crypt/rijndael.rb', line 119

def mix_columns(block_array)
  mixed = [[], [], [], []]
  0.upto(@block_words-1) { |j|
    0.upto(3) { |i|
      mixed[i][j] = mul(2,block_array[i][j]) ^
        mul(3,block_array[(i + 1) % 4][j]) ^
        block_array[(i + 2) % 4][j] ^
        block_array[(i + 3) % 4][j]
    }
  }
  return(mixed)
end

#mul(a, b) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/crypt/rijndael.rb', line 74

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(block_array, direction) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/crypt/rijndael.rb', line 94

def shift_rows(block_array, direction)
  tmp = []
  1.upto(3) { |i|  # row zero remains unchanged
    0.upto(@block_words-1) { |j|
      tmp[j] = block_array[i][(j + Shifts[@shift_index][i][direction]) % @block_words]
    }
    0.upto(@block_words-1) { |j|
      block_array[i][j] = tmp[j]
    }
  }
  return(block_array)
end

#substitution(block_array, sBox) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/crypt/rijndael.rb', line 108

def substitution(block_array, sBox)
  # replace every byte of the input with the byte at that position in the S-box
  0.upto(3) { |i|
    0.upto(@block_words-1) { |j|
      block_array[i][j] = sBox[block_array[i][j]]
    }
  }
  return(block_array)
end