Harald Welte | 75a58d1 | 2022-07-31 15:51:19 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import unittest |
| 4 | from pySim.utils import h2b, b2h |
| 5 | from pySim.ota import * |
| 6 | |
Harald Welte | 75e31c5 | 2023-06-06 20:49:57 +0200 | [diff] [blame] | 7 | # pre-defined SPI values for use in test cases below |
| 8 | SPI_CC_POR_CIPHERED_CC = { |
| 9 | 'counter':'no_counter', |
| 10 | 'ciphering':True, |
| 11 | 'rc_cc_ds': 'cc', |
| 12 | 'por_in_submit':False, |
| 13 | 'por_shall_be_ciphered':True, |
| 14 | 'por_rc_cc_ds': 'cc', |
| 15 | 'por': 'por_required' |
| 16 | } |
| 17 | |
| 18 | SPI_CC_POR_UNCIPHERED_CC = { |
| 19 | 'counter':'no_counter', |
| 20 | 'ciphering':True, |
| 21 | 'rc_cc_ds': 'cc', |
| 22 | 'por_in_submit':False, |
| 23 | 'por_shall_be_ciphered':False, |
| 24 | 'por_rc_cc_ds': 'cc', |
| 25 | 'por': 'por_required' |
| 26 | } |
| 27 | |
| 28 | SPI_CC_POR_UNCIPHERED_NOCC = { |
| 29 | 'counter':'no_counter', |
| 30 | 'ciphering':True, |
| 31 | 'rc_cc_ds': 'cc', |
| 32 | 'por_in_submit':False, |
| 33 | 'por_shall_be_ciphered':False, |
| 34 | 'por_rc_cc_ds': 'no_rc_cc_ds', |
| 35 | 'por': 'por_required' |
| 36 | } |
| 37 | |
| 38 | |
| 39 | class Test_SMS_AES128(unittest.TestCase): |
| 40 | tar = h2b('B00011') |
| 41 | """Test the OtaDialectSms for AES128 algorithms.""" |
| 42 | def __init__(self, foo, **kwargs): |
| 43 | super().__init__(foo, **kwargs) |
| 44 | self.od = OtaKeyset(algo_crypt='aes_cbc', kic_idx=2, |
| 45 | algo_auth='aes_cmac', kid_idx=2, |
| 46 | kic=h2b('200102030405060708090a0b0c0d0e0f'), |
| 47 | kid=h2b('201102030405060708090a0b0c0d0e0f')) |
| 48 | self.dialect = OtaDialectSms() |
| 49 | self.spi_base = SPI_CC_POR_CIPHERED_CC |
| 50 | |
| 51 | def _check_response(self, r, d): |
| 52 | self.assertEqual(d['number_of_commands'], 1) |
| 53 | self.assertEqual(d['last_status_word'], '6132') |
| 54 | self.assertEqual(d['last_response_data'], u'') |
| 55 | self.assertEqual(r['response_status'], 'por_ok') |
| 56 | |
| 57 | def test_resp_aes128_ciphered(self): |
| 58 | spi = self.spi_base |
| 59 | r, d = self.dialect.decode_resp(self.od, spi, '027100002412b00011ebc6b497e2cad7aedf36ace0e3a29b38853f0fe9ccde81913be5702b73abce1f') |
| 60 | self._check_response(r, d) |
| 61 | |
| 62 | def test_cmd_aes128_ciphered(self): |
| 63 | spi = self.spi_base |
| 64 | r = self.dialect.encode_cmd(self.od, self.tar, spi, h2b('00a40004023f00')) |
| 65 | self.assertEqual(b2h(r), '00281506192222b00011e87cceebb2d93083011ce294f93fc4d8de80da1abae8c37ca3e72ec4432e5058') |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
Harald Welte | 75a58d1 | 2022-07-31 15:51:19 +0200 | [diff] [blame] | 70 | class Test_SMS_3DES(unittest.TestCase): |
| 71 | tar = h2b('b00000') |
| 72 | """Test the OtaDialectSms for 3DES algorithms.""" |
| 73 | def __init__(self, foo, **kwargs): |
| 74 | super().__init__(foo, **kwargs) |
| 75 | # KIC1 + KID1 of 8988211000000467285 |
| 76 | KIC1 = h2b('D0FDA31990D8D64178601317191669B4') |
| 77 | KID1 = h2b('D24EB461799C5E035C77451FD9404463') |
| 78 | KIC3 = h2b('C21DD66ACAC13CB3BC8B331B24AFB57B') |
| 79 | KID3 = h2b('12110C78E678C25408233076AA033615') |
| 80 | self.od = OtaKeyset(algo_crypt='triple_des_cbc2', kic_idx=3, kic=KIC3, |
| 81 | algo_auth='triple_des_cbc2', kid_idx=3, kid=KID3) |
| 82 | self.dialect = OtaDialectSms() |
| 83 | self.spi_base = { |
| 84 | 'counter':'no_counter', |
| 85 | 'ciphering': True, |
| 86 | 'rc_cc_ds': 'cc', |
| 87 | 'por_in_submit':False, |
| 88 | 'por': 'por_required', |
| 89 | 'por_shall_be_ciphered': True, |
| 90 | 'por_rc_cc_ds': 'cc', |
| 91 | } |
| 92 | |
| 93 | def _check_response(self, r, d): |
| 94 | self.assertEqual(d['number_of_commands'], 1) |
| 95 | self.assertEqual(d['last_status_word'], '612f') |
| 96 | self.assertEqual(d['last_response_data'], u'') |
| 97 | self.assertEqual(r['response_status'], 'por_ok') |
| 98 | |
| 99 | def test_resp_3des_ciphered(self): |
| 100 | spi = self.spi_base |
| 101 | spi['por_shall_be_ciphered'] = True |
| 102 | spi['por_rc_cc_ds'] = 'cc' |
| 103 | r, d = self.dialect.decode_resp(self.od, spi, '027100001c12b000119660ebdb81be189b5e4389e9e7ab2bc0954f963ad869ed7c') |
| 104 | self._check_response(r, d) |
| 105 | |
| 106 | def test_resp_3des_signed(self): |
| 107 | spi = self.spi_base |
| 108 | spi['por_shall_be_ciphered'] = False |
| 109 | spi['por_rc_cc_ds'] = 'cc' |
| 110 | r, d = self.dialect.decode_resp(self.od, spi, '027100001612b000110000000000000055f47118381175fb01612f') |
| 111 | self._check_response(r, d) |
| 112 | |
| 113 | def test_resp_3des_signed_err(self): |
| 114 | """Expect an OtaCheckError exception if the computed CC != received CC""" |
| 115 | spi = self.spi_base |
| 116 | spi['por_shall_be_ciphered'] = False |
| 117 | spi['por_rc_cc_ds'] = 'cc' |
| 118 | with self.assertRaises(OtaCheckError) as context: |
| 119 | r, d = self.dialect.decode_resp(self.od, spi, '027100001612b000110000000000000055f47118381175fb02612f') |
| 120 | self.assertTrue('!= Computed CC' in str(context.exception)) |
| 121 | |
| 122 | def test_resp_3des_none(self): |
| 123 | spi = self.spi_base |
| 124 | spi['por_shall_be_ciphered'] = False |
| 125 | spi['por_rc_cc_ds'] = 'no_rc_cc_ds' |
| 126 | r, d = self.dialect.decode_resp(self.od, spi, '027100000e0ab000110000000000000001612f') |
| 127 | self._check_response(r, d) |
| 128 | |
| 129 | def test_cmd_3des_ciphered(self): |
| 130 | spi = self.spi_base |
| 131 | spi['ciphering'] = True |
| 132 | spi['rc_cc_ds'] = 'no_rc_cc_ds' |
| 133 | r = self.dialect.encode_cmd(self.od, self.tar, spi, h2b('00a40000023f00')) |
| 134 | self.assertEqual(b2h(r), '00180d04193535b00000e3ec80a849b554421276af3883927c20') |
| 135 | |
| 136 | def test_cmd_3des_signed(self): |
| 137 | spi = self.spi_base |
| 138 | spi['ciphering'] = False |
| 139 | spi['rc_cc_ds'] = 'cc' |
| 140 | r = self.dialect.encode_cmd(self.od, self.tar, spi, h2b('00a40000023f00')) |
| 141 | self.assertEqual(b2h(r), '1502193535b00000000000000000072ea17bdb72060e00a40000023f00') |
| 142 | |
| 143 | def test_cmd_3des_none(self): |
| 144 | spi = self.spi_base |
| 145 | spi['ciphering'] = False |
| 146 | spi['rc_cc_ds'] = 'no_rc_cc_ds' |
| 147 | r = self.dialect.encode_cmd(self.od, self.tar, spi, h2b('00a40000023f00')) |
| 148 | self.assertEqual(b2h(r), '0d00193535b0000000000000000000a40000023f00') |