Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Luc Libralesso
cryptodag
Commits
f8b57f71
Commit
f8b57f71
authored
Jan 12, 2021
by
Luc Libralesso
Browse files
add aes192 (to be debugged)
parent
81f0c5f9
Changes
2
Hide whitespace changes
Inline
Side-by-side
cryptosystems/aes/aes192.rb
0 → 100644
View file @
f8b57f71
#!/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'
def
extract_columns
(
e
,
i
,
j
)
res
=
[]
(
i
..
j
).
each
do
|
k
|
tmp
=
[]
4
.
times
do
|
l
|
tmp
.
append
(
e
[
l
][
k
])
end
res
.
append
(
tmp
)
end
return
res
end
def
combine_columns
(
a
,
b
)
res
=
[]
for
e
in
a
tmp
=
[]
4
.
times
do
|
i
|
tmp
.
append
(
e
[
i
])
end
res
.
append
(
tmp
)
end
for
e
in
b
tmp
=
[]
4
.
times
do
|
i
|
tmp
.
append
(
e
[
i
])
end
res
.
append
(
tmp
)
end
return
res
end
# implements the AES 192 DAG
class
AES192_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
,
6
])
nodes
.
push
(
@k
)
key_nodes
=
[]
key_nodes
.
push
(
@k
)
for
i
in
1
..
(
nb_rounds
*
4
/
6
).
ceil
-
1
kprev
=
key_nodes
[
key_nodes
.
length
-
1
]
k_current
=
AESKeyScheduleNode
.
new
(
name
:"K_
#{
i
}
"
,
input
:kprev
.
outputs
[
0
],
subtable
:@sbox
,
rcon
:@rcon
[
i
])
key_nodes
.
push
(
k_current
)
nodes
.
push
(
k_current
)
end
#define internal nodes
addkey
=
XorNode
.
new
(
name
:"ARK_0"
,
inputs
:[
@x
.
outputs
[
0
],
@k
.
outputs
[
0
]])
nodes
.
push
(
addkey
)
kprev
=
@k
for
i
in
1
..
nb_rounds
do
ki
=
[]
for
c
in
(
i
*
4
)
..
(
i
*
4
+
3
)
current_key_block
=
(
c
/
6
).
floor
()
current_block_col
=
c
-
current_key_block
*
6
tmp
=
[]
4
.
times
do
|
j
|
tmp
.
append
(
key_nodes
[
current_key_block
].
outputs
[
0
][
j
][
current_block_col
])
end
ki
.
append
(
tmp
)
end
# dirty hack ahead
ki
.
define_singleton_method
(
:dimensions
)
do
return
[
4
,
4
]
end
# end
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
tests/writers/abstract_aes192.rb
0 → 100755
View file @
f8b57f71
#!/usr/bin/ruby
require_relative
"../../cryptodag.rb"
require_relative
"../../cryptosystems/aes/aes192.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_aes192
()
# create dag
nb_rounds
=
3
dag
=
AES192_Dag
.
new
(
nb_rounds
=
nb_rounds
)
# shave dag
atoms
,
operators
=
*
shave_dag
(
dag
.
atoms
,
dag
.
operators
)
writefile_graphviz
(
atoms
,
operators
,
"aes192_
#{
nb_rounds
}
.dot"
)
# create initial model
assert_equal
(
true
,
true
)
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment