forked from iagox86/poracle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoTests.rb
111 lines (94 loc) · 2.67 KB
/
DoTests.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
$LOAD_PATH << File.dirname(__FILE__) # A hack to make this work on 1.8/1.9
require 'benchmark'
require 'openssl'
require 'LocalTestModule'
require 'RemoteTestModule'
require 'Poracle'
if(ARGV[0] == 'remote')
# Attempt a remote check
puts("Starting remote test (this requires RemoteTestServer.rb to be running on localhost:20222)")
begin
mod = RemoteTestModule.new
time = Benchmark.measure do
puts Poracle.decrypt(mod, mod.data, mod.iv, true)
end
puts("Guesses: #{Poracle.guesses}")
puts("Time: #{time}")
rescue Errno::ECONNREFUSED => e
puts(e.class)
puts("Couldn't connect to remote server: #{e}")
end
end
# Perform local checks
ciphers = OpenSSL::Cipher::ciphers.grep(/cbc/)
srand(123456)
passes = 0
failures = 0
print("> AES-256-CBC with known data... ")
mod = LocalTestModule.new("AES-256-CBC", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
d = Poracle.decrypt(mod, mod.ciphertext, mod.iv, true)
if(d == "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
passes += 1
puts "Passed!"
else
failures += 1
puts "Failed!"
puts "Expected: ABCDEFGHIJKLMNOPQRSTUVWXYZ"
puts "Received: #{d}"
puts
puts "First test failed; bailing"
exit
end
# Test strings that require backtracking
0.upto(512) do
print("> AES-128-CBC that requires backtracking...")
data_length = rand(15).to_i + 1
data = (1..data_length).map{(rand(0x60) + 0x20).to_i.chr}.join
cipher = "AES-128-CBC"
#iv = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x09\x00"
iv = (1..16).map{rand(255).to_i.chr}.join
iv[14] = ((16 - data_length) ^ 2).chr
mod = LocalTestModule.new(cipher, data, nil, iv)
d = Poracle.decrypt(mod, mod.ciphertext, mod.iv, false)
if(d == data)
passes += 1
puts "Passed!"
else
failures += 1
puts "Failed!"
end
end
# Do a bunch of very short strings
(0..512).to_a.each do |i|
data = (0..rand(8)).map{rand(255).to_i.chr}.join
cipher = ciphers.shuffle[0]
print("> #{cipher} with random short data... ")
mod = LocalTestModule.new(cipher, data, nil, nil)
d = Poracle.decrypt(mod, mod.ciphertext, mod.iv)
if(d == data)
passes += 1
puts "Passed!"
else
failures += 1
puts "Failed!"
end
end
# Try the different ciphers
ciphers.each do |cipher|
(0..128).to_a.shuffle[0, 8].each do |i|
print("> #{cipher} with random data (#{i} bytes)... ")
data = (0..i).map{(rand(0x7E - 0x20) + 0x20).chr}.join
mod = LocalTestModule.new(cipher, data)
d = Poracle.decrypt(mod, mod.ciphertext, mod.iv, false)
if(d == data)
passes += 1
puts "Passed!"
else
failures += 1
puts "Failed!"
end
end
end
puts("Ciphers tested: #{ciphers.join(", ")}")
puts("Tests passed: #{passes}")
puts("Tests failed: #{failures}")