Search code examples
bluetoothcryptographybluetooth-lowenergypkipairing

How to do bluetooth pairing at factory time


I have some Bluetooth LE v4.2 beacons that I will connect ONLY with known devices that we may call "readers". The beacons are program and installed by me. I consume the data and I sell the service.

I want to use a hard-coded shared secret to realize the pairing or communication. My primary concern is that only a known and authenticated device SHALL be able to send data (with integrity protection).

What would be my best option ?

A few previsions :

  • We are talking about 1000s of devices, and more will join the network every day.
  • I am already doing advertisement filter, etc. I only connect to devices with my vendor id.
  • Replacement if preferable to any kind of lack of security in the authentication, my added value is the trust in data.
  • I have an OTA update system for all the devices.

Interesting documentation I found about Bluetooth Low Energy (BLE) security :


Solution

  • An answer to my question on the Nordicsemi devzone gave me some hints. Find below the answers I was looking for. I hope that will help.

    Mode 1 Level 4 (encryption) vs Mode 2 Level 2 (signing)

    Resources :

    Forget about CSRK. It's a bad idea that almost no BLE stacks support. One reason is that it only supports Write Without Response in one direction. Another is that you need to keep a write counter stored in flash. A third is that a MITM could potentially delay a message for an arbitrary time and doesn't need an active connection during this time. It has no benefits at all compared to the normal AES-CCM except that CCM takes 2.5 round trips to set up for BLE.

    How to ensure secure encryption with a pre-shared secret

    Resources :

    Do we need pairing ?

    No pairing :

    If you remove the pairing step from BLE security you basically just have AES-CCM with pre-shared keys, where each connection has an own key derived from the shared key and a nonce from each side. LESC is about the pairing step which you want to remove, so that doesn't apply in that case.

    Vs Out Of Band (OOB) :

    A pre-shared key is an example of OOB (Out of band) pairing. That might sound a bit strange, but essentially you are using the production setup in your factory as the medium to share keys. You do not want to have the LTK or any BLE bonding data pre-shared, but rather just a key at some location in flash which can be used in a regular OOB pairing.

    Preferred solution is Out of band pairing.

    LESC with pre-shared passkey vs OOB with pre-shared key ?

    Resources :

    The first time you connect you should authenticate the other device, and you can do this by using your pre-shared key when you bond. You can bond by using Passkey Entry or OOB. The key used with Passkey Entry is short, so I would recommend using a 128-bit key with OOB, this is much more secure.

    Out of band LESC Vs Out Of Band Legacy

    Both LESC and Legacy end up with 128-bit encryption keys, and these are equally secure. The power consumption will be the same after pairing is done. LESC uses a more complex algorithm so it will use more power during the pairing process. The difference is in the key generation algorithm. It depends on what kind of attacks you want to protect against. If you do OOB with legacy and you are sure that the attacker can't get the OOB data, you are secure. If the attacker can get this data, you should go for LESC. What kind of central device are you connecting to? Does it support OOB and/or LESC?

    In fact LESC out of band with pre-shared key is quite complicated to archive because of the calculation of the oob payload is supposed to be a random number signed with private key, and this mechanism is implemented in the softdevice but not accessible. Thus we could either re-invent the wheel, or just decide that this computation is useless as evedrop of out-of-band is just impossible with pre-shared key. Also, LESC oob pairing is more calculation intensive for no benefits.

    Out of band Legacy

    For more detailed explanations of Out of band Legacy pairing, see bluetooth.com.

    Temporary key calculation

    A master key will be included in the new FW release code (that's probably my major weakness, but I cannot do much about it). I will use legacy Out Of Band pairing. The Temporary Key (TK), used for paring communication encryption, will be derivated from the master key using generation function fc (inspired by the f5 function described in the Bluetooth specification).

    The definition of this key generation function fc makes use of the MAC function AES-CMACT with a 128-bit key T.

    The input of the function are:

    • M is 128 bits
    • A1 is 56 bits
    • A2 is 56 bits

    The string “******” is mapped into keyID using extended ASCII as follows:

    • keyID = 0xXXXXXXXXXXXX

    The output of the key generation function fc is as follows:

    • fc(M, A1, A2) = AES-CMACM(keyID || 0x00 || A1 || A2 || Length = 128)

    The TK is calculated as:

    • TK = fc(Master key, DB_ADDR_master, DB_ADDR_slave)