|
| 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