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# ----------------------------------------------------------------------------- 18from bumble.crypto import * 19 20 21# ----------------------------------------------------------------------------- 22def reversed_hex(hex): 23 return bytes(reversed(bytes.fromhex(hex))) 24 25 26# ----------------------------------------------------------------------------- 27def test_ecc(): 28 key = EccKey.generate() 29 x = key.x 30 y = key.y 31 32 assert(len(x) == 32) 33 assert(len(y) == 32) 34 35 # Test DH with test vectors from the spec 36 private_A = '3f49f6d4 a3c55f38 74c9b3e3 d2103f50 4aff607b eb40b799 5899b8a6 cd3c1abd' 37 private_B = '55188b3d 32f6bb9a 900afcfb eed4e72a 59cb9ac2 f19d7cfb 6b4fdd49 f47fc5fd' 38 public_A_x = '20b003d2 f297be2c 5e2c83a7 e9f9a5b9 eff49111 acf4fddb cc030148 0e359de6' 39 public_A_y = 'dc809c49 652aeb6d 63329abf 5a52155c 766345c2 8fed3024 741c8ed0 1589d28b' 40 public_B_x = '1ea1f0f0 1faf1d96 09592284 f19e4c00 47b58afd 8615a69f 559077b2 2faaa190' 41 public_B_y = '4c55f33e 429dad37 7356703a 9ab85160 472d1130 e28e3676 5f89aff9 15b1214a' 42 dhkey = 'ec0234a3 57c8ad05 341010a6 0a397d9b 99796b13 b4f866f1 868d34f3 73bfa698' 43 44 key_a = EccKey.from_private_key_bytes( 45 bytes.fromhex(private_A), 46 bytes.fromhex(public_A_x), 47 bytes.fromhex(public_A_y) 48 ) 49 shared_key = key_a.dh( 50 bytes.fromhex(public_B_x), 51 bytes.fromhex(public_B_y) 52 ) 53 assert(shared_key == bytes.fromhex(dhkey)) 54 55 key_b = EccKey.from_private_key_bytes( 56 bytes.fromhex(private_B), 57 bytes.fromhex(public_B_x), 58 bytes.fromhex(public_B_y) 59 ) 60 shared_key = key_b.dh( 61 bytes.fromhex(public_A_x), 62 bytes.fromhex(public_A_y) 63 ) 64 assert(shared_key == bytes.fromhex(dhkey)) 65 66 67# ----------------------------------------------------------------------------- 68def test_c1(): 69 k = bytes(16) 70 r = reversed_hex('5783D52156AD6F0E6388274EC6702EE0') 71 pres = reversed_hex('05000800000302') 72 preq = reversed_hex('07071000000101') 73 iat = 1 74 ia = reversed_hex('A1A2A3A4A5A6') 75 rat = 0 76 ra = reversed_hex('B1B2B3B4B5B6') 77 result = c1(k, r, preq, pres, iat, rat, ia, ra) 78 assert(result == reversed_hex('1e1e3fef878988ead2a74dc5bef13b86')) 79 80 81# ----------------------------------------------------------------------------- 82def test_s1(): 83 k = bytes(16) 84 r1 = reversed_hex('000F0E0D0C0B0A091122334455667788') 85 r2 = reversed_hex('010203040506070899AABBCCDDEEFF00') 86 result = s1(k, r1, r2) 87 assert(result == reversed_hex('9a1fe1f0e8b0f49b5b4216ae796da062')) 88 89 90# ----------------------------------------------------------------------------- 91def test_aes_cmac(): 92 m = b'' 93 k = bytes.fromhex('2b7e1516 28aed2a6 abf71588 09cf4f3c') 94 cmac = aes_cmac(m, k) 95 assert(cmac == bytes.fromhex('bb1d6929 e9593728 7fa37d12 9b756746')) 96 97 m = bytes.fromhex('6bc1bee2 2e409f96 e93d7e11 7393172a') 98 cmac = aes_cmac(m, k) 99 assert(cmac == bytes.fromhex('070a16b4 6b4d4144 f79bdd9d d04a287c')) 100 101 m = bytes.fromhex( 102 '6bc1bee2 2e409f96 e93d7e11 7393172a' + 103 'ae2d8a57 1e03ac9c 9eb76fac 45af8e51' + 104 '30c81c46 a35ce411' 105 ) 106 cmac = aes_cmac(m, k) 107 assert(cmac == bytes.fromhex('dfa66747 de9ae630 30ca3261 1497c827')) 108 109 m = bytes.fromhex( 110 '6bc1bee2 2e409f96 e93d7e11 7393172a' + 111 'ae2d8a57 1e03ac9c 9eb76fac 45af8e51' + 112 '30c81c46 a35ce411 e5fbc119 1a0a52ef' + 113 'f69f2445 df4f9b17 ad2b417b e66c3710' 114 ) 115 cmac = aes_cmac(m, k) 116 assert(cmac == bytes.fromhex('51f0bebf 7e3b9d92 fc497417 79363cfe')) 117 118 119# ----------------------------------------------------------------------------- 120def test_f4(): 121 u = bytes(reversed(bytes.fromhex( 122 '20b003d2 f297be2c 5e2c83a7 e9f9a5b9' + 123 'eff49111 acf4fddb cc030148 0e359de6' 124 ))) 125 v = bytes(reversed(bytes.fromhex( 126 '55188b3d 32f6bb9a 900afcfb eed4e72a' + 127 '59cb9ac2 f19d7cfb 6b4fdd49 f47fc5fd' 128 ))) 129 x = bytes(reversed(bytes.fromhex('d5cb8454 d177733e ffffb2ec 712baeab'))) 130 z = bytes([0]) 131 value = f4(u, v, x, z) 132 assert(bytes(reversed(value)) == bytes.fromhex('f2c916f1 07a9bd1c f1eda1be a974872d')) 133 134 135# ----------------------------------------------------------------------------- 136def test_f5(): 137 w = bytes(reversed(bytes.fromhex( 138 'ec0234a3 57c8ad05 341010a6 0a397d9b' + 139 '99796b13 b4f866f1 868d34f3 73bfa698' 140 ))) 141 n1 = bytes(reversed(bytes.fromhex('d5cb8454 d177733e ffffb2ec 712baeab'))) 142 n2 = bytes(reversed(bytes.fromhex('a6e8e7cc 25a75f6e 216583f7 ff3dc4cf'))) 143 a1 = bytes(reversed(bytes.fromhex('00561237 37bfce'))) 144 a2 = bytes(reversed(bytes.fromhex('00a71370 2dcfc1'))) 145 value = f5(w, n1, n2, a1, a2) 146 assert(bytes(reversed(value[0])) == bytes.fromhex('2965f176 a1084a02 fd3f6a20 ce636e20')) 147 assert(bytes(reversed(value[1])) == bytes.fromhex('69867911 69d7cd23 980522b5 94750a38')) 148 149 150# ----------------------------------------------------------------------------- 151def test_f6(): 152 n1 = bytes(reversed(bytes.fromhex('d5cb8454 d177733e ffffb2ec 712baeab'))) 153 n2 = bytes(reversed(bytes.fromhex('a6e8e7cc 25a75f6e 216583f7 ff3dc4cf'))) 154 mac_key = bytes(reversed(bytes.fromhex('2965f176 a1084a02 fd3f6a20 ce636e20'))) 155 r = bytes(reversed(bytes.fromhex('12a3343b b453bb54 08da42d2 0c2d0fc8'))) 156 io_cap = bytes(reversed(bytes.fromhex('010102'))) 157 a1 = bytes(reversed(bytes.fromhex('00561237 37bfce'))) 158 a2 = bytes(reversed(bytes.fromhex('00a71370 2dcfc1'))) 159 value = f6(mac_key, n1, n2, r, io_cap, a1, a2) 160 assert(bytes(reversed(value)) == bytes.fromhex('e3c47398 9cd0e8c5 d26c0b09 da958f61')) 161 162 163# ----------------------------------------------------------------------------- 164def test_g2(): 165 u = bytes(reversed(bytes.fromhex( 166 '20b003d2 f297be2c 5e2c83a7 e9f9a5b9' + 167 'eff49111 acf4fddb cc030148 0e359de6' 168 ))) 169 v = bytes(reversed(bytes.fromhex( 170 '55188b3d 32f6bb9a 900afcfb eed4e72a' + 171 '59cb9ac2 f19d7cfb 6b4fdd49 f47fc5fd' 172 ))) 173 x = bytes(reversed(bytes.fromhex('d5cb8454 d177733e ffffb2ec 712baeab'))) 174 y = bytes(reversed(bytes.fromhex('a6e8e7cc 25a75f6e 216583f7 ff3dc4cf'))) 175 value = g2(u, v, x, y) 176 assert(value == 0x2f9ed5ba) 177 178 179# ----------------------------------------------------------------------------- 180def test_h6(): 181 KEY = bytes.fromhex('ec0234a3 57c8ad05 341010a6 0a397d9b') 182 KEY_ID = bytes.fromhex('6c656272') 183 assert(h6(KEY, KEY_ID) == bytes.fromhex('2d9ae102 e76dc91c e8d3a9e2 80b16399')) 184 185 186# ----------------------------------------------------------------------------- 187def test_h7(): 188 KEY = bytes.fromhex('ec0234a3 57c8ad05 341010a6 0a397d9b') 189 SALT = bytes.fromhex('00000000 00000000 00000000 746D7031') 190 assert(h7(SALT, KEY) == bytes.fromhex('fb173597 c6a3c0ec d2998c2a 75a57011')) 191 192 193# ----------------------------------------------------------------------------- 194def test_ah(): 195 irk = bytes(reversed(bytes.fromhex('ec0234a3 57c8ad05 341010a6 0a397d9b'))) 196 prand = bytes(reversed(bytes.fromhex('708194'))) 197 value = ah(irk, prand) 198 expected = bytes(reversed(bytes.fromhex('0dfbaa'))) 199 assert(value == expected) 200 201 202# ----------------------------------------------------------------------------- 203if __name__ == '__main__': 204 test_ecc() 205 test_c1() 206 test_s1() 207 test_aes_cmac() 208 test_f4() 209 test_f5() 210 test_f6() 211 test_g2() 212 test_h6() 213 test_h7() 214 test_ah() 215