It's starting to become a proper challange

This commit is contained in:
2019-06-26 14:44:06 +02:00
parent da749e84f4
commit 0c9768e065
8 changed files with 500265 additions and 46 deletions

View File

@@ -9,8 +9,8 @@ using Formatting
function parse_csv(filename)
data = CSV.read(filename, header=0)
plaintexts::Matrix{UInt8} = convert(Matrix{UInt8}, data[:, 1:16])#[1:50, :]
timings::Matrix{UInt32} = convert(Matrix{UInt32}, data[:, 17:17])#[1:50]
plaintexts::Matrix{UInt8} = convert(Matrix{UInt8}, data[:, 1:16])
timings::Matrix{UInt32} = convert(Matrix{UInt32}, data[:, 17:17])
return plaintexts, timings
end
@@ -51,30 +51,44 @@ function build_key_schedule(initial_key)
return round_keys
end
function gf_mult2(value::UInt8)::UInt8
result = value << 1
if value & 0x80 != 0
result ⊻= value
end
return result
end
function gf_mult3(value::UInt8)::UInt8
return gf_mult2(value) value
end
function break_decryption()
keys, timings = parse_csv("timing.csv")
round_keys = Array{UInt8, 3}(undef,11, size(keys, 1), 16)
for keyno=1:size(keys, 1)
@Threads.threads for keyno=1:size(keys, 1)
round_keys[:, keyno, :] = build_key_schedule(keys[keyno, :])
end
t_values = Vector{Float64}(undef, 0x100)
r_shiftrows=[0, 13, 10, 7, 4, 1, 14, 11, 8, 5, 2, 15, 12, 9, 6, 3] .+ 1
for secretbyte=1:16
for secret=0:0xFF
# TODO Shift rows must be respected here
msbs_set = ((round_keys[10, :, r_shiftrows[secretbyte]] .⊻ r_sbox[(secret .⊻ round_keys[11, :, secretbyte]) .+ 1]) .& 0xE0) .!= 0
#msbs_set = (sbox[(keys[:, secretbyte] .⊻ secret) .+ 1] .& 0x08) .!= 0
group_slow = timings[msbs_set]
group_fast = timings[msbs_set.==false]
t_values[secret + 1] = t_val(group_fast, group_slow)
end
print(format("{:02x} ", argmax(t_values) - 1))
if secretbyte==1
plt = plot_discrete_tval(0:0xFF, t_values, "secret data")
display(plt)
end
end
println()
shiftrows = [0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, 1, 6, 11] .+ 1
r_shiftrows = [0, 13, 10, 7, 4, 1, 14, 11, 8, 5, 2, 15, 12, 9, 6, 3] .+ 1
byte1 = shiftrows[1]
byte2 = shiftrows[2]
byte3 = shiftrows[3]
byte4 = shiftrows[4]
high_tvalues = [Vector{Any}(undef, 0) for i=1:Threads.nthreads()]
@Threads.threads for p1=0xA0:0xA0
for p2=0x00:0x00, p3=0x70:0x80, p4=0xD0:0xE0
msbs_set = (gf_mult2.(sbox[p1 .⊻ round_keys[1, :, byte1] .+ 1]) .⊻ gf_mult3.(sbox[p2 .⊻ round_keys[1, :, byte2] .+ 1]) .⊻ sbox[p3 .⊻ round_keys[1, :, byte3] .+ 1] .⊻ sbox[p4 .⊻ round_keys[1, :, byte4] .+ 1]) .& 0xC0 .!= 0
group_slow = timings[msbs_set]
group_fast = timings[msbs_set.==false]
t_value = t_val(group_fast, group_slow)
if abs(t_value) > 4
push!(high_tvalues[Threads.threadid()], (p1, p2, p3, p4, t_value))
end
end
end
println(high_tvalues)
end
break_decryption()
@time break_decryption()