Skip to content

Commit da42313

Browse files
committed
gea12.py: separate tests, use Python's unittest framework
1 parent cced9ac commit da42313

File tree

2 files changed

+105
-81
lines changed

2 files changed

+105
-81
lines changed

gea12.py

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -519,84 +519,3 @@ def gen(self, bl):
519519
self.C.clock()
520520
self.D.clock()
521521
return list(reversed(self.K))
522-
523-
524-
#------------------------------------------------------------------------------#
525-
# test vectors
526-
#------------------------------------------------------------------------------#
527-
528-
TestVectorsGEA1 = []
529-
530-
for cipher, plain, (key, iv, dir) in zip(
531-
[
532-
0x1FA198AB2114C38A9EBCCB63AD4813A740C1,
533-
0x58dad06457b9fe1015da0776ed19907b7888,
534-
0xd72197f65d4d67b14d2cee812cb0b9bea0c9
535-
],
536-
[
537-
0x000000000000000000000000000000000000,
538-
0x6e00cfe7b7fb974892b8cde5e43363397d85,
539-
0x96e7b1d92b1ea8fcdda41233c63294055383
540-
],
541-
[
542-
(0x0000000000000000, 0x00000000, 0),
543-
(0x55e303eb7d55b685, 0xda637a83, 1),
544-
(0xa7265d1932a0d618, 0x0e9b8adf, 0)
545-
]
546-
):
547-
TestVectorsGEA1.append(
548-
(iv, dir, key, cipher, plain)
549-
)
550-
551-
552-
def test_gea1():
553-
for i, (iv, dir, key, cipher, plain) in enumerate(TestVectorsGEA1[:3]):
554-
ks = bitlist_to_uint(GEA1(iv, dir, key).gen(144))
555-
# keystream byte-order needs to be reverted
556-
if LFSR.dbg:
557-
print('Keystream: 0x%x' % ks)
558-
if byte_rev(plain, 18) ^ byte_rev(cipher, 18) == ks:
559-
print('GEA1 test vector %i: OK' % i)
560-
else:
561-
print('GEA1 test vector %i: NOK' % i)
562-
563-
TestVectorsGEA2 = []
564-
565-
for cipher, plain, (key, iv, dir) in zip(
566-
[
567-
0x045115d5e5a2d62541da078b18baa53ffe14,
568-
0x5156569d2ab98257be1a37d60ddf07ae9075,
569-
0x509c19b78d1d4ceb49c3b1f43df014f74cda
570-
],
571-
[
572-
0x000000000000000000000000000000000000,
573-
0xeabf6d3c6ba5dbf76ebb3c4c0ac0240cb0ab,
574-
0xf9373de52ea62c49069711e83389d037fc17
575-
],
576-
[
577-
(0x0000000000000000, 0x00000000, 0),
578-
(0xb10f389b78a61648, 0x24c05b01, 1),
579-
(0x0c34b2940a9707fd, 0xf59cc96a, 0)
580-
]
581-
):
582-
TestVectorsGEA2.append(
583-
(iv, dir, key, cipher, plain)
584-
)
585-
586-
587-
def test_gea2():
588-
for i, (iv, dir, key, cipher, plain) in enumerate(TestVectorsGEA2[:3]):
589-
ks = bitlist_to_uint(GEA2(iv, dir, key).gen(144))
590-
# keystream byte-order needs to be reverted
591-
if LFSR.dbg:
592-
print('Keystream: 0x%x' % ks)
593-
if byte_rev(plain, 18) ^ byte_rev(cipher, 18) == ks:
594-
print('GEA2 test vector %i: OK' % i)
595-
else:
596-
print('GEA2 test vector %i: NOK' % i)
597-
598-
599-
if __name__ == '__main__':
600-
test_gea1()
601-
test_gea2()
602-

test_gea12.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: UTF-8 -*-
3+
#/**
4+
# * Software Name : gea12.py
5+
# * Version : 0.1
6+
# *
7+
# * Copyright 2021. Benoit Michau. P1Sec.
8+
# *
9+
# * This program is free software: you can redistribute it and/or modify
10+
# * it under the terms of the GNU Affero General Public License as published by
11+
# * the Free Software Foundation, either version 3 of the License, or
12+
# * (at your option) any later version.
13+
# *
14+
# * This program is distributed in the hope that it will be useful,
15+
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
# * GNU Affero General Public License for more details.
18+
# *
19+
# * You should have received a copy of the GNU Affero General Public License
20+
# * along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
# *
22+
# *--------------------------------------------------------
23+
# * File Name : test_gea12.py
24+
# * Created : 2022-03-25
25+
# * Authors : Benoit Michau, Vadim Yanitskiy
26+
# *--------------------------------------------------------
27+
#*/
28+
29+
import unittest
30+
import logging
31+
32+
from gea12 import bitlist_to_uint, byte_rev
33+
from gea12 import LFSR, GEA1, GEA2
34+
35+
class TestGEA1(unittest.TestCase):
36+
TestVectors = []
37+
38+
for cipher, plain, (key, iv, dir) in zip(
39+
[
40+
0x1FA198AB2114C38A9EBCCB63AD4813A740C1,
41+
0x58dad06457b9fe1015da0776ed19907b7888,
42+
0xd72197f65d4d67b14d2cee812cb0b9bea0c9
43+
],
44+
[
45+
0x000000000000000000000000000000000000,
46+
0x6e00cfe7b7fb974892b8cde5e43363397d85,
47+
0x96e7b1d92b1ea8fcdda41233c63294055383
48+
],
49+
[
50+
(0x0000000000000000, 0x00000000, 0),
51+
(0x55e303eb7d55b685, 0xda637a83, 1),
52+
(0xa7265d1932a0d618, 0x0e9b8adf, 0)
53+
]
54+
):
55+
TestVectors.append(
56+
(iv, dir, key, cipher, plain)
57+
)
58+
59+
def test_gea1(self):
60+
for i, (iv, dir, key, cipher, plain) in enumerate(self.TestVectors[:3]):
61+
with self.subTest(i):
62+
ks = bitlist_to_uint(GEA1(iv, dir, key).gen(144))
63+
# keystream byte-order needs to be reverted
64+
if LFSR.dbg:
65+
logging.debug('Keystream: 0x%x', ks)
66+
self.assertEqual(byte_rev(plain, 18) ^ byte_rev(cipher, 18), ks)
67+
68+
69+
class TestGEA2(unittest.TestCase):
70+
TestVectors = []
71+
72+
for cipher, plain, (key, iv, dir) in zip(
73+
[
74+
0x045115d5e5a2d62541da078b18baa53ffe14,
75+
0x5156569d2ab98257be1a37d60ddf07ae9075,
76+
0x509c19b78d1d4ceb49c3b1f43df014f74cda
77+
],
78+
[
79+
0x000000000000000000000000000000000000,
80+
0xeabf6d3c6ba5dbf76ebb3c4c0ac0240cb0ab,
81+
0xf9373de52ea62c49069711e83389d037fc17
82+
],
83+
[
84+
(0x0000000000000000, 0x00000000, 0),
85+
(0xb10f389b78a61648, 0x24c05b01, 1),
86+
(0x0c34b2940a9707fd, 0xf59cc96a, 0)
87+
]
88+
):
89+
TestVectors.append(
90+
(iv, dir, key, cipher, plain)
91+
)
92+
93+
94+
def test_gea2(self):
95+
for i, (iv, dir, key, cipher, plain) in enumerate(self.TestVectors[:3]):
96+
with self.subTest(i):
97+
ks = bitlist_to_uint(GEA2(iv, dir, key).gen(144))
98+
# keystream byte-order needs to be reverted
99+
if LFSR.dbg:
100+
logging.debug('Keystream: 0x%x', ks)
101+
self.assertEqual(byte_rev(plain, 18) ^ byte_rev(cipher, 18), ks)
102+
103+
104+
if __name__ == '__main__':
105+
unittest.main()

0 commit comments

Comments
 (0)