1# Copyright 2021-2022 Google LLC 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15# ----------------------------------------------------------------------------- 16# Imports 17# ----------------------------------------------------------------------------- 18 19import pytest 20 21from bumble import smp 22from bumble.crypto import EccKey, aes_cmac, ah, c1, f4, f5, f6, g2, h6, h7, s1 23from bumble.pairing import OobData, OobSharedData, LeRole 24from bumble.hci import Address 25from bumble.core import AdvertisingData 26 27 28# ----------------------------------------------------------------------------- 29# pylint: disable=invalid-name 30# ----------------------------------------------------------------------------- 31 32 33# ----------------------------------------------------------------------------- 34def reversed_hex(hex_str: str) -> bytes: 35 return bytes.fromhex(hex_str)[::-1] 36 37 38# ----------------------------------------------------------------------------- 39def test_ecc(): 40 key = EccKey.generate() 41 x = key.x 42 y = key.y 43 44 assert len(x) == 32 45 assert len(y) == 32 46 47 # Test DH with test vectors from the spec 48 private_A = ( 49 '3f49f6d4 a3c55f38 74c9b3e3 d2103f50 4aff607b eb40b799 5899b8a6 cd3c1abd' 50 ) 51 private_B = ( 52 '55188b3d 32f6bb9a 900afcfb eed4e72a 59cb9ac2 f19d7cfb 6b4fdd49 f47fc5fd' 53 ) 54 public_A_x = ( 55 '20b003d2 f297be2c 5e2c83a7 e9f9a5b9 eff49111 acf4fddb cc030148 0e359de6' 56 ) 57 public_A_y = ( 58 'dc809c49 652aeb6d 63329abf 5a52155c 766345c2 8fed3024 741c8ed0 1589d28b' 59 ) 60 public_B_x = ( 61 '1ea1f0f0 1faf1d96 09592284 f19e4c00 47b58afd 8615a69f 559077b2 2faaa190' 62 ) 63 public_B_y = ( 64 '4c55f33e 429dad37 7356703a 9ab85160 472d1130 e28e3676 5f89aff9 15b1214a' 65 ) 66 dhkey = 'ec0234a3 57c8ad05 341010a6 0a397d9b 99796b13 b4f866f1 868d34f3 73bfa698' 67 68 key_a = EccKey.from_private_key_bytes( 69 bytes.fromhex(private_A), bytes.fromhex(public_A_x), bytes.fromhex(public_A_y) 70 ) 71 shared_key = key_a.dh(bytes.fromhex(public_B_x), bytes.fromhex(public_B_y)) 72 assert shared_key == bytes.fromhex(dhkey) 73 74 key_b = EccKey.from_private_key_bytes( 75 bytes.fromhex(private_B), bytes.fromhex(public_B_x), bytes.fromhex(public_B_y) 76 ) 77 shared_key = key_b.dh(bytes.fromhex(public_A_x), bytes.fromhex(public_A_y)) 78 assert shared_key == bytes.fromhex(dhkey) 79 80 81# ----------------------------------------------------------------------------- 82def test_c1(): 83 k = bytes(16) 84 r = reversed_hex('5783D52156AD6F0E6388274EC6702EE0') 85 pres = reversed_hex('05000800000302') 86 preq = reversed_hex('07071000000101') 87 iat = 1 88 ia = reversed_hex('A1A2A3A4A5A6') 89 rat = 0 90 ra = reversed_hex('B1B2B3B4B5B6') 91 result = c1(k, r, preq, pres, iat, rat, ia, ra) 92 assert result == reversed_hex('1e1e3fef878988ead2a74dc5bef13b86') 93 94 95# ----------------------------------------------------------------------------- 96def test_s1(): 97 k = bytes(16) 98 r1 = reversed_hex('000F0E0D0C0B0A091122334455667788') 99 r2 = reversed_hex('010203040506070899AABBCCDDEEFF00') 100 result = s1(k, r1, r2) 101 assert result == reversed_hex('9a1fe1f0e8b0f49b5b4216ae796da062') 102 103 104# ----------------------------------------------------------------------------- 105def test_aes_cmac(): 106 m = b'' 107 k = bytes.fromhex('2b7e1516 28aed2a6 abf71588 09cf4f3c') 108 cmac = aes_cmac(m, k) 109 assert cmac == bytes.fromhex('bb1d6929 e9593728 7fa37d12 9b756746') 110 111 m = bytes.fromhex('6bc1bee2 2e409f96 e93d7e11 7393172a') 112 cmac = aes_cmac(m, k) 113 assert cmac == bytes.fromhex('070a16b4 6b4d4144 f79bdd9d d04a287c') 114 115 m = bytes.fromhex( 116 '6bc1bee2 2e409f96 e93d7e11 7393172a' 117 + 'ae2d8a57 1e03ac9c 9eb76fac 45af8e51' 118 + '30c81c46 a35ce411' 119 ) 120 cmac = aes_cmac(m, k) 121 assert cmac == bytes.fromhex('dfa66747 de9ae630 30ca3261 1497c827') 122 123 m = bytes.fromhex( 124 '6bc1bee2 2e409f96 e93d7e11 7393172a' 125 + 'ae2d8a57 1e03ac9c 9eb76fac 45af8e51' 126 + '30c81c46 a35ce411 e5fbc119 1a0a52ef' 127 + 'f69f2445 df4f9b17 ad2b417b e66c3710' 128 ) 129 cmac = aes_cmac(m, k) 130 assert cmac == bytes.fromhex('51f0bebf 7e3b9d92 fc497417 79363cfe') 131 132 133# ----------------------------------------------------------------------------- 134def test_f4(): 135 u = reversed_hex( 136 '20b003d2 f297be2c 5e2c83a7 e9f9a5b9 eff49111 acf4fddb cc030148 0e359de6' 137 ) 138 v = reversed_hex( 139 '55188b3d 32f6bb9a 900afcfb eed4e72a 59cb9ac2 f19d7cfb 6b4fdd49 f47fc5fd' 140 ) 141 x = reversed_hex('d5cb8454 d177733e ffffb2ec 712baeab') 142 z = b'\0' 143 value = f4(u, v, x, z) 144 assert value == reversed_hex('f2c916f1 07a9bd1c f1eda1be a974872d') 145 146 147# ----------------------------------------------------------------------------- 148def test_f5(): 149 w = reversed_hex( 150 'ec0234a3 57c8ad05 341010a6 0a397d9b 99796b13 b4f866f1 868d34f3 73bfa698' 151 ) 152 n1 = reversed_hex('d5cb8454 d177733e ffffb2ec 712baeab') 153 n2 = reversed_hex('a6e8e7cc 25a75f6e 216583f7 ff3dc4cf') 154 a1 = reversed_hex('00561237 37bfce') 155 a2 = reversed_hex('00a71370 2dcfc1') 156 value = f5(w, n1, n2, a1, a2) 157 assert value[0] == reversed_hex('2965f176 a1084a02 fd3f6a20 ce636e20') 158 assert value[1] == reversed_hex('69867911 69d7cd23 980522b5 94750a38') 159 160 161# ----------------------------------------------------------------------------- 162def test_f6(): 163 n1 = reversed_hex('d5cb8454 d177733e ffffb2ec 712baeab') 164 n2 = reversed_hex('a6e8e7cc 25a75f6e 216583f7 ff3dc4cf') 165 mac_key = reversed_hex('2965f176 a1084a02 fd3f6a20 ce636e20') 166 r = reversed_hex('12a3343b b453bb54 08da42d2 0c2d0fc8') 167 io_cap = reversed_hex('010102') 168 a1 = reversed_hex('00561237 37bfce') 169 a2 = reversed_hex('00a71370 2dcfc1') 170 value = f6(mac_key, n1, n2, r, io_cap, a1, a2) 171 assert value == reversed_hex('e3c47398 9cd0e8c5 d26c0b09 da958f61') 172 173 174# ----------------------------------------------------------------------------- 175def test_g2(): 176 u = reversed_hex( 177 '20b003d2 f297be2c 5e2c83a7 e9f9a5b9 eff49111 acf4fddb cc030148 0e359de6' 178 ) 179 v = reversed_hex( 180 '55188b3d 32f6bb9a 900afcfb eed4e72a 59cb9ac2 f19d7cfb 6b4fdd49 f47fc5fd' 181 ) 182 x = reversed_hex('d5cb8454 d177733e ffffb2ec 712baeab') 183 y = reversed_hex('a6e8e7cc 25a75f6e 216583f7 ff3dc4cf') 184 value = g2(u, v, x, y) 185 assert value == 0x2F9ED5BA 186 187 188# ----------------------------------------------------------------------------- 189def test_h6(): 190 KEY = reversed_hex('ec0234a3 57c8ad05 341010a6 0a397d9b') 191 KEY_ID = bytes.fromhex('6c656272') 192 assert h6(KEY, KEY_ID) == reversed_hex('2d9ae102 e76dc91c e8d3a9e2 80b16399') 193 194 195# ----------------------------------------------------------------------------- 196def test_h7(): 197 KEY = reversed_hex('ec0234a3 57c8ad05 341010a6 0a397d9b') 198 SALT = bytes.fromhex('00000000 00000000 00000000 746D7031') 199 assert h7(SALT, KEY) == reversed_hex('fb173597 c6a3c0ec d2998c2a 75a57011') 200 201 202# ----------------------------------------------------------------------------- 203def test_ah(): 204 irk = reversed_hex('ec0234a3 57c8ad05 341010a6 0a397d9b') 205 prand = reversed_hex('708194') 206 value = ah(irk, prand) 207 expected = reversed_hex('0dfbaa') 208 assert value == expected 209 210 211# ----------------------------------------------------------------------------- 212def test_oob_data(): 213 oob_data = OobData( 214 address=Address("F0:F1:F2:F3:F4:F5"), 215 role=LeRole.BOTH_PERIPHERAL_PREFERRED, 216 shared_data=OobSharedData(c=b'12', r=b'34'), 217 ) 218 oob_data_ad = oob_data.to_ad() 219 oob_data_bytes = bytes(oob_data_ad) 220 oob_data_ad_parsed = AdvertisingData.from_bytes(oob_data_bytes) 221 oob_data_parsed = OobData.from_ad(oob_data_ad_parsed) 222 assert oob_data_parsed.address == oob_data.address 223 assert oob_data_parsed.role == oob_data.role 224 assert oob_data_parsed.shared_data.c == oob_data.shared_data.c 225 assert oob_data_parsed.shared_data.r == oob_data.shared_data.r 226 227 228# ----------------------------------------------------------------------------- 229@pytest.mark.parametrize( 230 'ct2, expected', 231 [ 232 (False, 'bc1ca4ef 633fc1bd 0d8230af ee388fb0'), 233 (True, '287ad379 dca40253 0a39f1f4 3047b835'), 234 ], 235) 236def test_ltk_to_link_key(ct2: bool, expected: str): 237 LTK = reversed_hex('368df9bc e3264b58 bd066c33 334fbf64') 238 assert smp.Session.derive_link_key(LTK, ct2) == reversed_hex(expected) 239 240 241# ----------------------------------------------------------------------------- 242@pytest.mark.parametrize( 243 'ct2, expected', 244 [ 245 (False, 'a813fb72 f1a3dfa1 8a2c9a43 f10d0a30'), 246 (True, 'e85e09eb 5eccb3e2 69418a13 3211bc79'), 247 ], 248) 249def test_link_key_to_ltk(ct2: bool, expected: str): 250 LINK_KEY = reversed_hex('05040302 01000908 07060504 03020100') 251 assert smp.Session.derive_ltk(LINK_KEY, ct2) == reversed_hex(expected) 252 253 254# ----------------------------------------------------------------------------- 255if __name__ == '__main__': 256 test_ecc() 257 test_c1() 258 test_s1() 259 test_aes_cmac() 260 test_f4() 261 test_f5() 262 test_f6() 263 test_g2() 264 test_h6() 265 test_h7() 266 test_ah() 267 test_oob_data() 268