Skip to content

Instantly share code, notes, and snippets.

@dqi
Created August 28, 2017 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dqi/88c86e484fc9302694837810680d2023 to your computer and use it in GitHub Desktop.
Save dqi/88c86e484fc9302694837810680d2023 to your computer and use it in GitHub Desktop.
using Jlsca.Sca
using Jlsca.Trs
using Jlsca.Align
using Jlsca.Aes
using PyCall
using PyPlot.plot,PyPlot.figure
@pyimport numpy
trs = InspectorTrace("traces.trs");
# selecting the reference pattern in the first traces
referencestart = 800
referenceend = referencestart + 3000
reference = trs[1][2][referencestart:referenceend]
# in the search of alignemt, traces will be shifted by max this amount of samples
maxShift = 400
# the rejection threshold
corvalMin = 0.5
# create the alignment engine
alignstate = CorrelationAlignFFT(reference, referencestart, maxShift)
# add the alignment as a sample processing pass
# the end of the next line is somewhat complex I must say :) But it's an experimental toolbox, yay!
addSamplePass(trs, x -> ((shift,corval) = correlationAlign(x, alignstate); corval > corvalMin ? circshift(x, shift) : Vector{eltype(x)}(0)))
# execute the alignment pass by reading the traces
((data,samples),eof) = readTraces(trs, 1:length(trs))
# see what we've got
println(length(samples))
# plot(samples[:,:]', linewidth=.3)
# exit()
params = AesSboxAttack()
params.dataOffset = 1
params.mode = CIPHER
params.direction = FORWARD
numberOfTraces = length(trs);
params.analysis = CPA()
params.analysis.leakages = [HW()]
# params.analysis.leakages = [Bit(i) for i in [0,7]]
# params.xor = true
addSamplePass(trs, x -> x[1:end - 1])
# setPostProcessor(trs, IncrementalCorrelation())
key = sca(trs, params, 1, numberOfTraces)
# key = sca(DistributedTrace(),params,1, numberOfTraces, false)
w = KeyExpansionBackwards(key, 10, 4)
println(InvCipher(trs[1][1][1:16], w) == trs[1][1][17:32])
w = KeyExpansion(key, 10, 4)
println(InvCipher(trs[1][1][1:16], w) == trs[1][1][17:32])
w = KeyExpansionBackwards(key, 10, 4)
println(InvCipher(trs[1][1][17:32], w) == trs[1][1][1:16])
w = KeyExpansion(key, 10, 4)
println(InvCipher(trs[1][1][17:32], w) == trs[1][1][1:16])
w = KeyExpansionBackwards(key, 10, 4)
println(Cipher(trs[1][1][1:16], w) == trs[1][1][17:32])
w = KeyExpansion(key, 10, 4)
println(Cipher(trs[1][1][1:16], w) == trs[1][1][17:32])
w = KeyExpansionBackwards(key, 10, 4)
println(Cipher(trs[1][1][17:32], w) == trs[1][1][1:16])
w = KeyExpansion(key, 10, 4)
println(Cipher(trs[1][1][17:32], w) == trs[1][1][1:16])
w = EqInvKeyExpansion(key, 10, 4)
println(InvCipher(trs[1][1][1:16], w) == trs[1][1][17:32])
w = EqInvKeyExpansion(key, 10, 4)
println(InvCipher(trs[1][1][17:32], w) == trs[1][1][1:16])
w = EqInvKeyExpansion(key, 10, 4)
println(Cipher(trs[1][1][1:16], w) == trs[1][1][17:32])
w = EqInvKeyExpansion(key, 10, 4)
println(Cipher(trs[1][1][17:32], w) == trs[1][1][1:16])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment