How to replicate bluetooth CCM scheme in .NET?

I am working on a firmware upgrade scheme that requires end-to-end encryption of the firmware image. The target device is a low-power Bluetooth chip with hardware cryptography support specified in Blueooth Spec, AES-CCM. We want to use this equipment to minimize the size and speed of the code, so we need to encrypt the firmware image in the format for which the equipment is installed.

So, I'm trying to use the AesManaged .NET class so that I can play back the data samples specified in the Bluetooth Specification (p. 1547), but I do not get the same results. Here is an example of data:

Payload byte length: 08
K: 89678967 89678967 45234523 45234523
Payload counter: 0000bc614e
Zero ACL-U length Continuation: 0
Direction: 0
Initialization vector: 66778899 aabbccdd
LT_ADDR: 1
Packet type: 3
LLID: 2
Payload: 68696a6b 6c6d6e6

B0: 494e61bc 0000ddcc bbaa9988 77660008
B1: 00190200 000000000000000000000000
B2: 68696a6b 6c6d6e6f 0000000000000000

Y0: 95ddc3d4 2c9a70f1 61a28ee2 c08271ab

Y1: 418635ff 54615443 8aceca41 fe274779
Y2: 08d78b32 9d78ed33 b285fc42 e178d781

T: 08d78b32

CTR0: 014e61bc 0000ddcc bbaa9988 77660000
CTR1: 014e61bc 0000ddcc bbaa9988 77660001

S0: b90f2b23 f63717d3 38e0559d 1e7e785e
S1: d8c7e3e1 02050abb 025d0895 17cbe5fb

MIC: b1d8a011
: b0ae898a 6e6864d4

. , MIC Encrypted Payload T XOR'd S0 S1 , - S0. , , ECB'ing CTR0 K:

//I've tried a few endian-ness permutations of K, none work
byte[] sampleKey = { 0x23, 0x45, 0x23, 0x45, 0x23, 0x45, 0x23, 0x45,
                    0x67, 0x89, 0x67, 0x89, 0x67, 0x89, 0x67, 0x89};
byte[] sampleCtr0 = { 01, 0x4e, 0x61, 0xbc, 00, 00, 0xdd, 0xcc,
                    0xbb, 0xaa, 0x99, 0x88, 0x77, 0x66, 00, 00 };
byte[] encrypted;

using (AesManaged aesAlg = new AesManaged())
{
    aesAlg.Mode = CipherMode.ECB; //CTR implemented as ECB w/ manually-incrementing counter

    // Create an encrytor to perform the stream transform.
    ICryptoTransform encryptor = aesAlg.CreateEncryptor(sampleKey, zeros); //zeros is a byte array of 16 0's

    // Create the streams used for encryption.
    using (MemoryStream msEncrypt = new MemoryStream())
    {
        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
        {
            using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
            {
                //Write all data to the stream.
                swEncrypt.Write(sampleCtr0);
            }
            encrypted = msEncrypt.ToArray();
        }
    }
}

S0 , . ?

+4
1

, StreamWriter . csEncrypt.Write(), .

- , , , , , , , , . , - , .

+1

Source: https://habr.com/ru/post/1691257/


All Articles