Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is remote address in SMP wrong? #20

Open
ghost opened this issue Sep 16, 2018 · 0 comments
Open

Is remote address in SMP wrong? #20

ghost opened this issue Sep 16, 2018 · 0 comments

Comments

@ghost
Copy link

ghost commented Sep 16, 2018

Hi Adam, I wrote the c1 function to calculate the confirm value, as in Bluetooth 4.1 specificiation page 2247 's example, if:
ia = array.array('B', [0XA1, 0XA2, 0XA3, 0XA4, 0XA5, 0XA6])
ra = array.array('B', [0XB1, 0XB2, 0XB3, 0XB4, 0XB5, 0XB6])
iat = array.array('B', [0x01])
rat = array.array('B', [0x00])
preq_list = array.array('B', [0x07, 0x07, 0x10, 0x00, 0x00, 0x01, 0x01])
pres = array.array('B', [0x05, 0x00, 0x08, 0x00, 0x00, 0x03, 0x02])
_tk = array.array('B', [0]*16)

I get 0x1e1e3fef878988ead2a74dc5bef13b86 to prove c1 function as correct.
But Still the Mrand calculation result is wrong with normal ia, ra, and other paras.
And I notice your comment to say remove address is wrong, and local address has been reversed.
As I installed bleno to check it ,and find remove address the same.

The process output:
crypto start
INFO:purifier:Purifier initialize
start
INFO:main:Hit to disconnect
address result from HCI@@@@@@@@@@@: bytearray(b"\xf1\xb7z\xeb'\xb8")
DEBUG:main:on -> stateChange: poweredOn
DEBUG:main:on -> advertisingStart:success
INFO:main:advertising...
data: bytearray(b'\x01\x04\x00\r\x10\x07\x07')
code: 1
pairing response: b'\x02\x03\x00\x05\x10\x03\x03'
data: bytearray(b'\x03\xaeZ7\xf7\x1c\xe0\xc0\x86@\xb0\x0e\x01|\x97\n\xa8')
code: 3
ia: array('B', [68, 78, 13, 46, 202, 119])
ra: array('B', [241, 183, 122, 235, 39, 184])
iat: array('B', [1])
rat: array('B', [0])
preq: bytearray(b'\x01\x04\x00\r\x10\x07\x07')
pres: array('B', [2, 3, 0, 5, 16, 3, 3])
preq_list: array('B', [1, 4, 0, 13, 16, 7, 7])
res: bytearray(b'\xcb:\xee\xfd\xd0e\xe0\xc8\x8fA\xfb\xcc\x14L\x82\xa9')
res_list: array('B', [203, 58, 238, 253, 208, 101, 224, 200, 143, 65, 251, 204, 20, 76, 130, 169])
set temp_array: array('B', [203, 58, 238, 253, 208, 101, 224, 200, 143, 65, 251 , 204, 20, 76, 130, 169])
set temp_array: array('B', [3, 203, 58, 238, 253, 208, 101, 224, 200, 143, 65, 251, 204, 20, 76, 130, 169])
data: bytearray(b'\x04I\xf1\x9a\xf2\xb3\xa0\x92[\xfa\xdc\xa8>\x88\x0b@\x99')
code: 4
data: bytearray(b'\x04I\xf1\x9a\xf2\xb3\xa0\x92[\xfa\xdc\xa8>\x88\x0b@\x99')
r in random: array('B', [73, 241, 154, 242, 179, 160, 146, 91, 250, 220, 168, 6 2, 136, 11, 64, 153])
ia: array('B', [68, 78, 13, 46, 202, 119])
ra: array('B', [241, 183, 122, 235, 39, 184])
iat: array('B', [1])
rat: array('B', [0])
preq: bytearray(b'\x01\x04\x00\r\x10\x07\x07')
pres: array('B', [2, 3, 0, 5, 16, 3, 3])
preq_list: array('B', [1, 4, 0, 13, 16, 7, 7])
res: bytearray(b'\x06\x0f\xed\xe3qm\xb2\x1e\xd0\x18\xde\xf3\xff\xc9\x06\x89')
res_list: array('B', [6, 15, 237, 227, 113, 109, 178, 30, 208, 24, 222, 243, 25 5, 201, 6, 137])
type: <class 'array.array'>
pcnf: array('B', [3, 6, 15, 237, 227, 113, 109, 178, 30, 208, 24, 222, 243, 255 , 201, 6, 137])
self._pcnf array('B', [3, 174, 90, 55, 247, 28, 224, 192, 134, 64, 176, 14, 1, 1 24, 151, 10, 168])
pairing fail!!!!

self_made crypto code

`"""
crypto for python
"""

import base64
import os
import array
import struct
import hashlib
from Crypto.Cipher import AES
from Crypto import Random
from Crypto.Protocol.KDF import PBKDF2
import random
import binascii
import base64

class CRYPTO:
def init(self):
print('crypto start')

def r(self):

    # key = array.array('B', random.sample(range(256), 16))
    r = [0X57,0X83,0XD5,0X21,0X56,0XAD,0X6F,0X0E,0X63,0X88,0X27,0X4E,0XC6,0X70,0X2E, 0XE0]
    # print('r in crypto', r)
    # print(type(key))
    r_list = []
    for i in r:
        r_list.extend([i])
    r = array.array('B', r_list)
    return r

def c1(self, k, r, pres, preq, iat, ia, rat, ra):
    #k TK
    #r random request
    #pres response data
    #preq  request datat
    #iat initiator type
    #ia initiator address
    #rat responsor type
    #ra responsor address
    # ia = array.array('B', [0XA1, 0XA2, 0XA3, 0XA4, 0XA5, 0XA6])
    # ra = array.array('B', [0XB1, 0XB2, 0XB3, 0XB4, 0XB5, 0XB6])
    # iat =  array.array('B', [0x01])
    # rat =  array.array('B', [0x00])
    # preq_list =  array.array('B', [0x07, 0x07, 0x10, 0x00, 0x00, 0x01, 0x01])
    # pres =  array.array('B', [0x05, 0x00, 0x08, 0x00, 0x00, 0x03, 0x02])
    print('ia: ', ia)
    print('ra: ', ra)
    print('iat: ', iat)
    print('rat: ', rat)
    print('preq: ', preq)
    print('pres: ', pres)
    preq_list = []
    preq = [bytes([c]) for c in preq]
    for i in preq:
        preq_list.extend([int.from_bytes(i, byteorder='little')])

    preq_list = array.array('B', preq_list)
    # preq_list[3] = 0x01
    print('preq_list: ', preq_list)
    p1 = pres + preq_list + rat + iat
    p1 = bytearray(p1)
    # print('p1: ', p1)

    p2 =  array.array('B', [0] * 4) + ia + ra
    #r is key
    # print('p2: ', p2)
    res = self.xor(r, p1)
    res = self.e(k, res)
    res = self.xor(res, p2)
    res = bytearray(self.e(k, res))
    print('res: ', res)
    res_list =[]
    for i in res:
        res_list.extend([i])
    res = array.array('B', res_list)
    print('res_list: ', res)
    return res

def s1(self, k, r1, r2):
    print('r1', r1)
    print('r2', r2)
    return self.e(k, r2[0:8] +
                  r1[0:8]
                 )

# def e(self, key, data):
#     data = bytes(data)
#     print('data from encrypt_token: ', data)
#     key = bytes(key)
#     print('key from encrypt_token: ', key)
#     print('block size: ', AES.block_size)
#     IV = Random.new().read(AES.block_size)
#     # binascii.hexlify(IV)
#     aes = AES.new(key, AES.MODE_ECB, IV)
#     # aes = AES.new(key, AES.MODE_ECB)
#     return aes.encrypt(data)

def e(self, key, data):
    data = bytes(data)
    # print('data from encrypt_token: ', data)
    key = bytes(key)
    # print('key from encrypt_token: ', key)
    aes = AES.new(key)
    result = aes.encrypt(data)
    return result

def _pad(self, s):
    # print(s + (AES.block_size - len(s) % AES.block_size) * chr(AES.block_size - len(s) % AES.block_size))
    return s + (AES.block_size - len(s) % AES.block_size) * chr(AES.block_size - len(s) % AES.block_size).encode('utf-8')


def xor(self, b1, b2):
    # print('b1: ', b1)
    # print('b2: ', b2)
    # result = list(range(len(b1))
    # result = array.array('B', [0]*len(b1))
    result = list(range(len(b1)))
    i = 0
    for i in range(0, len(b1)):
        result[i] = b1[i] ^ b2[i]
    # print('result: ', result)
    return result

def bytes_to_int(self, bytes):
    result = 0

    for b in bytes:
        result = result * 256 + int(b)

    return result

def swap(self, INPUT):
    # output = list(range(len(INPUT)))
    output = array.array('B', [0] * len(INPUT))
    i = 0
    for i in range(0, len(output)):
        output[i] = INPUT[len(INPUT)-i-1]

    return output

crypto = CRYPTO()
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants