Commit 30b8219c authored by Luc Libralesso's avatar Luc Libralesso
Browse files

add aes256 writer

parent 1998890a
#!/usr/bin/ruby
require_relative './constants.rb'
require_relative '../../cryptodag.rb'
require_relative '../../simulate_cryptodag.rb'
require_relative '../../nodes/input.rb'
require_relative '../../nodes/xor.rb'
require_relative '../../nodes/subbytes.rb'
require_relative '../../nodes/shiftrows.rb'
require_relative '../../nodes/mixcolumn.rb'
require_relative '../../nodes/aeskeyschedule.rb'
##
# combine the 2 first columns of m1 starting at x coordinate i1 and the 2 last columns of m2 starting at x coordinate i2
def combine_columns_2h(m1, i1, m2, i2)
matrix = MultiMatrix.build(m1.type, [4,4])
for i in 0..1
4.times do |j|
matrix[j][i] = m1[j][i1+i]
end
end
for i in 0..1
4.times do |j|
matrix[j][i+2] = m2[j][i2+i]
end
end
return matrix
end
def extract_four_first(m1)
matrix = MultiMatrix.build(m1.type, [4,4])
for i in 0..3
4.times do |j|
# matrix[j][i] = m1[j][i]
matrix[i][j] = m1[j][i]
end
end
return matrix
end
# implements the AES 256 DAG
class AES256_Dag < CryptoDag
def initialize(nb_rounds=12)
# prepare constants
@rcon = rcon
@mc = mixcolumns_matrix()
@sbox = s_box()
# define input nodes
nodes = []
@nb_rounds = nb_rounds
@x = InputNode.new(name:"X", dimensions:[4,4])
nodes.push(@x)
@k = InputNode.new(name:"K", dimensions:[4,8])
nodes.push(@k)
key_nodes = []
key_nodes.push(@k)
for i in 0..(nb_rounds/2).ceil
if i > 0
k_current = AESKeyScheduleNode.new(
name:"K_#{i}",
input:key_nodes[key_nodes.length-1].outputs[0],
subtable:@sbox,rcon:@rcon[i]
)
key_nodes.push(k_current)
nodes.push(k_current)
end
end
# puts "keynodes size: #{key_nodes.length}"
#define internal nodes
# p "##### #{@x.outputs[0].dimensions}\t#{@k.outputs[0].dimensions}"
# addkey = XorNode.new(name:"ARK_0", inputs:[@x.outputs[0],extract_four_first(@k.outputs[0])])
addkey = XorNode.new(name:"ARK_0", inputs:[
@x.outputs[0],
combine_columns_2h(
key_nodes[0].outputs[0], 0,
key_nodes[0].outputs[0], 2,
)
])
nodes.push(addkey)
kprev = @k
for i in 1..nb_rounds do
c1 = (i)*4
c2 = (i)*4 + 2
# puts "round #{i}:\tm1:#{(c1/6).floor}\tm2:#{(c2/6).floor}"
ki = combine_columns_2h(
key_nodes[(c1/8).floor].outputs[0], c1%8,
key_nodes[(c2/8).floor].outputs[0], c2%8,
)
subbytes = SubBytesNode.new(name:"SB_#{i}", input:addkey.outputs[0], subtable:@sbox)
nodes.push(subbytes)
shiftrows = ShiftRowsNode.new(name:"SR_#{i}", input:subbytes.outputs[0])
nodes.push(shiftrows)
if i < nb_rounds
mixcolumns = MixColumnNode.new(name:"MC_#{i}", input:shiftrows.outputs[0], m:@mc)
nodes.push(mixcolumns)
else
mixcolumns = shiftrows
end
addkey = XorNode.new(name:"ARK_#{i}", inputs:[mixcolumns.outputs[0],ki])
nodes.push(addkey)
end
# define dag inputs/outputs
super([@x,@k], [addkey], nodes)
end
##
# @param x [2DVec<u8>] input message
# @param k [2DVec<u8>] input key
# @returns [2DVec<u8>] crypted message
def simulate_behavior(x,k)
computed_outputs = compute_set_of_operators(
x.flatten()+k.flatten(), # input values
@x.flatten_output(0)+@k.flatten_output(0), # input variables
@output_nodes[0].flatten_output(0), # output variables
@nodes.map{|n| n.operators}.reduce([], :+)
)
return computed_outputs
end
end
\ No newline at end of file
......@@ -14,7 +14,7 @@ require "pry"
class TestWriterGraphviz < Minitest::Unit::TestCase
def test_aes192()
# create dag
nb_rounds = 5
nb_rounds =8
dag = AES192_Dag.new(nb_rounds = nb_rounds)
# shave dag
# atoms,operators = dag.atoms(), dag.operators()
......
#!/usr/bin/ruby
require_relative "../../cryptodag.rb"
require_relative "../../cryptosystems/aes/aes256.rb"
require_relative "../../writers/abstract_constraints_atomic.rb"
require_relative "../../backends/minizinc"
require_relative "../../writers/graphviz_atomic.rb"
require "minitest/autorun"
# require "minitest/color"
require "pry"
class TestWriterGraphviz < Minitest::Unit::TestCase
def test_aes256()
# create dag
nb_rounds =4
dag = AES256_Dag.new(nb_rounds = nb_rounds)
# shave dag
# atoms,operators = dag.atoms(), dag.operators()
atoms,operators = *shave_dag(dag.atoms, dag.operators)
writefile_graphviz(atoms, operators, "aes256_#{nb_rounds}.dot")
# create initial model
obj_values = {}
obj_values[3] = 1
obj_values[4] = 3
obj_values[5] = 3
obj_values[6] = 5
obj_values[7] = 5
obj_values[8] = 10
obj_values[9] = 15
obj_values[10] = 16
obj_values[11] = 20
obj_values[12] = 20
obj_values[13] = 24
obj_values[14] = 24
model,variable_dict,xor_clauses = *create_abstract_model(
atoms,
operators,
"solve minimize obj",
# "constraint obj=#{obj_values[nb_rounds]}; solve satisfy",
# mds_set=Set[0,5,6,7,8]
)
# add diff variables
xor_clauses = xor_clauses + add_diff_variables_mixcolumn_lines(model, atoms, operators, variable_dict, mds_set=Set[0,5,6,7,8])
# generate new xor clauses and add them to the model
puts("ADDING XOR CLAUSES")
puts("\tstarting with #{xor_clauses.length} clauses")
xor_clauses = generate_xors(atoms, operators, xor_clauses, max_size=5)
puts("\tnow having: #{xor_clauses.length} clauses")
xor_clauses_sizes = [0,0,0,0,0,0,0]
xor_clauses.each do |xor|
xor_clauses_sizes[xor.length] += 1
model.add_constraints(Diff1.new(*(xor.map{|v| variable_dict.fetch(v,v)})))
end
puts("\tXOReq sizes:")
for i in 1..6
puts("\t\t#{i}\t#{xor_clauses_sizes[i]}")
end
file = File.open("aes256_#{nb_rounds}.mzn", "w")
file.puts(Minizinc.new.generate_code(model))
file.close
puts "FILE: aes256_#{nb_rounds}.mzn correctly written"
# finish successfully the test
assert_equal(true, true)
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment