~increscent/aes-ccm

a1a8058cbaa6c1d7066e6c00d2249efa3eda382c — Robert Williams 2 months ago 4915312
Refactoring... there's likely a zephyr bug
6 files changed, 45 insertions(+), 47 deletions(-)

M Makefile
M decrypt.c
M encrypt.c
M lib.c
M lib.h
M zencrypt.c
M Makefile => Makefile +3 -3
@@ 6,13 6,13 @@ LDFLAGS = -lssl -lcrypto -lpthread -lrob
.PHONY: all
all: encrypt decrypt zencrypt

decrypt: decrypt.c lib.c
decrypt: decrypt.c lib.c lib.h
	$(CC) -o decrypt decrypt.c lib.c $(CFLAGS) $(LDFLAGS)

encrypt: encrypt.c lib.c
encrypt: encrypt.c lib.c lib.h
	$(CC) -o encrypt encrypt.c lib.c $(CFLAGS) $(LDFLAGS)

zencrypt: zencrypt.c lib.c
zencrypt: zencrypt.c lib.c lib.h
	$(CC) -o zencrypt zencrypt.c lib.c tinycrypt/lib/source/*.c -Itinycrypt/lib/include $(CFLAGS) $(LDFLAGS)

.PHONY: clean

M decrypt.c => decrypt.c +7 -17
@@ 2,21 2,10 @@
#include <stdio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <string.h>

#include "lib.h"

unsigned char KEY[16] = {0xE3, 0xBB, 0x08, 0xF7, 0x9A, 0xCB, 0xF5, 0xEA, 0x14, 0x87, 0x98, 0x0D, 0xBE, 0xF7, 0x8D, 0x49};
//unsigned char IV[13] = {0x01, 0x80, 0x00, 0x01, 0xA5, 0x00, 0x33, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00};
unsigned char IV[13] = {0x01, 0x80, 0x00, 0x00, 0x53, 0x00, 0x35, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00};
char IV_STR[26] = "01800000530035000100000000";
//unsigned char TAG[8] = {0xA2, 0x69, 0xDC, 0x20, 0x58, 0x38, 0x59, 0xF1};
unsigned char TAG[8] = {0xfa, 0x28, 0x0d, 0x84, 0xeb, 0x5c, 0x25, 0xd0};
//char TAG_STR[16] = "3d34ae2bb1db3e22";
char TAG_ST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xF8, 0x32, 0x6D, 0x46, 0xB6, 0xAB, 0x51, 0x84, 0xC4, 0x21, 0x2B, 0x2F, 0x7D, 0x0D, 0x39, 0x12, 0x8C, 0x36, 0x89, 0xB7, 0x66, 0x14, 0x58, 0x69, 0x0B, 0xDC, 0xE8, 0x20, 0x24, 0xC7, 0x8A, 0xAC, 0x76, 0x68, 0xBC, 0x4E, 0xD4, 0x2F, 0x8F, 0x08, 0x6F, 0x81, 0xD7, 0x07, 0x3B, 0x59, 0x10, 0xA5, 0x07, 0xB8, 0xC2, 0x13, 0x76, 0xD0, 0x12, 0x93, 0xE9, 0xD4, 0x3A, 0xB7, 0xFB, 0xE5, 0x3F, 0x1C, 0xF8, 0x46, 0x7E, 0xF3, 0x11, 0x32, 0x63, 0x55, 0xF7, 0xAD, 0x00, 0x18, 0x64, 0x72, 0xB1, 0x87, 0xAD, 0x21, 0xF5, 0x23, 0x9C, 0x79, 0x62, 0xE6, 0xA7, 0xE0, 0x6C, 0x37, 0x17, 0x77, 0x93, 0xFA, 0x7A, 0x45, 0xF6, 0x3E, 0xE1, 0x97, 0xD1, 0x30, 0x59, 0x71, 0x87, 0x68, 0x59, 0x58, 0x3E, 0x93, 0xDC, 0xB8, 0xBB, 0x0F, 0xC2};
unsigned char CIPHERTEXT[293] = {0xc4, 0xef, 0xbe, 0x01, 0x00, 0x01, 0x1a, 0x00, 0x08, 0x00, 0x00, 0xbf, 0x63, 0x6f, 0x66, 0x66, 0x00, 0x64, 0x64, 0x61, 0x74, 0x61, 0x59, 0x01, 0x00, 0x49, 0x4e, 0x46, 0x3a, 0x20, 0x0a, 0x0a, 0x42, 0x75, 0x69, 0x6c, 0x64, 0x20, 0x54, 0x69, 0x6d, 0x65, 0x3a, 0x09, 0x4a, 0x75, 0x6c, 0x20, 0x32, 0x36, 0x20, 0x32, 0x30, 0x32, 0x32, 0x20, 0x31, 0x30, 0x3a, 0x30, 0x32, 0x3a, 0x34, 0x33, 0x0a, 0x41, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x3a, 0x09, 0x73, 0x74, 0x65, 0x72, 0x6c, 0x69, 0x6e, 0x67, 0x40, 0x70, 0x61, 0x73, 0x73, 0x69, 0x76, 0x65, 0x6c, 0x6f, 0x67, 0x69, 0x63, 0x2e, 0x63, 0x6f, 0x6d, 0x0a, 0x48, 0x61, 0x73, 0x68, 0x3a, 0x09, 0x76, 0x30, 0x2e, 0x31, 0x2d, 0x31, 0x32, 0x39, 0x30, 0x2d, 0x67, 0x61, 0x61, 0x63, 0x30, 0x64, 0x30, 0x32, 0x65, 0x2d, 0x64, 0x69, 0x72, 0x74, 0x79, 0x0a, 0x44, 0x61, 0x74, 0x65, 0x3a, 0x09, 0x54, 0x75, 0x65, 0x20, 0x4a, 0x75, 0x6c, 0x20, 0x31, 0x39, 0x20, 0x31, 0x36, 0x3a, 0x31, 0x37, 0x3a, 0x35, 0x39, 0x20, 0x32, 0x30, 0x32, 0x32, 0x0a, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x72, 0x63, 0x00, 0x63, 0x6c, 0x65, 0x6e, 0x19, 0x01, 0x00, 0xff};
char CIPHERTEXT_STR[586] = "7ac99054766c4c9887003ac6abeba2d8cab033dbe912d8ea8c5a47087e3926510d7e27b99a3a4c238d8448ecb13134c7213f8fff02de17272253842e566e7446a475835c543811ce23e8ec8e1ae543ec12ac7d8446d8b10e0883e9eef191a81db4c01c2c5fc1b003e437d4654f421b259b8219c2db3864693e708fae34fb7d4e30166f205daf4f144ca8ef993062be4c1887c8af115d23ca8dbcbfaa73a583ea37a304c54f4fe4a8bf37d66d5a68e7cf1b9f8eb523078f1682ec3ea3b459c71252b0f6665276fb1f68e216715dd44973a543ac262de8522e62d65b46fad2b172304acdecd9823eba6829f247bb636e1bb48ccf284056f42ffce7910056bfd144f511eb0ce541eba906e0450b573963ed73055635bdc6ff72fd0460853895eebddd06f9efc9";

int main()
{
    EVP_CIPHER_CTX *ctx;


@@ 25,9 14,11 @@ int main()
    int ret;
    unsigned char plaintext[1024];

    hexToBytes(CIPHERTEXT_STR, sizeof(CIPHERTEXT_STR), CIPHERTEXT);
    hexToBytes(TAG_STR, sizeof(TAG_STR), TAG);
    hexToBytes(IV_STR, sizeof(IV_STR), IV);
    hexToBytes(KEY_STR, strlen(KEY_STR), KEY);
    hexToBytes(IV_STR, strlen(IV_STR), IV);
    hexToBytes(PLAINTEXT_STR, strlen(PLAINTEXT_STR), PLAINTEXT);
    hexToBytes(CIPHERTEXT_STR, strlen(CIPHERTEXT_STR), CIPHERTEXT);
    hexToBytes(MIC_STR, strlen(MIC_STR), MIC);

    /* Create and initialise the context */
    if(!(ctx = EVP_CIPHER_CTX_new()))


@@ 43,14 34,13 @@ int main()
        handleErrors("3");

    /* Set expected tag value. */
    if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, sizeof(TAG), TAG))
    if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, sizeof(MIC), MIC))
        handleErrors("4");

    /* Initialise key and IV */
    if(1 != EVP_DecryptInit_ex(ctx, NULL, NULL, KEY, IV))
        handleErrors("5");


    /* Provide the total ciphertext length */
    if(1 != EVP_DecryptUpdate(ctx, NULL, &len, NULL, sizeof(CIPHERTEXT)))
        handleErrors("6");

M encrypt.c => encrypt.c +9 -13
@@ 2,17 2,10 @@
#include <stdio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <string.h>

#include "lib.h"

unsigned char KEY[16] = {0xE3, 0xBB, 0x08, 0xF7, 0x9A, 0xCB, 0xF5, 0xEA, 0x14, 0x87, 0x98, 0x0D, 0xBE, 0xF7, 0x8D, 0x49};
unsigned char IV[13] = {0};
char IV_STR[26] = "01800000110038000100000000";
//unsigned char TAG[8] = {0xA2, 0x69, 0xDC, 0x20, 0x58, 0x38, 0x59, 0xF1};
//unsigned char PLAINTEXT[293] = {0xA7, 0x91, 0x55, 0xF3, 0xF9, 0x9D, 0x22, 0x38, 0xB0, 0x7C, 0xB0, 0x49, 0xF9, 0x8E, 0xC3, 0x46, 0xE7, 0x3D, 0x5D, 0x78, 0x7B, 0xE3, 0xA4, 0x6D, 0xE1, 0x30, 0x6E, 0xD2, 0x44, 0xFC, 0xD5, 0xBD, 0x88, 0x06, 0x85, 0x2B, 0x5F, 0xB2, 0x2A, 0xC6, 0x34, 0xF5, 0x14, 0xFA, 0x06, 0x88, 0x1B, 0x64, 0x93, 0x89, 0x83, 0xBE, 0x36, 0x68, 0x58, 0x30, 0x89, 0xF3, 0x66, 0xDB, 0x55, 0x70, 0xA5, 0x7D, 0x89, 0x80, 0x6B, 0xD6, 0xA0, 0x6F, 0x4C, 0x48, 0x5F, 0x4A, 0x41, 0x83, 0xE9, 0xC6, 0x86, 0x31, 0xC3, 0x63, 0xED, 0x5C, 0xA5, 0x6F, 0x4D, 0xBB, 0xD1, 0xD3, 0x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, 0x3E, 0xE1, 0x97, 0xD1, 0x30, 0x59, 0x71, 0x87, 0x68, 0x59, 0x58, 0x3E, 0x93, 0xDC, 0xB8, 0xBB, 0x0F, 0xC2};
//unsigned char PLAINTEXT[293] = {0xc4, 0xef, 0xbe, 0x01, 0x00, 0x01, 0x1a, 0x00, 0x08, 0x00, 0x00, 0xbf, 0x63, 0x6f, 0x66, 0x66, 0x00, 0x64, 0x64, 0x61, 0x74, 0x61, 0x59, 0x01, 0x00, 0x49, 0x4e, 0x46, 0x3a, 0x20, 0x0a, 0x0a, 0x42, 0x75, 0x69, 0x6c, 0x64, 0x20, 0x54, 0x69, 0x6d, 0x65, 0x3a, 0x09, 0x4a, 0x75, 0x6c, 0x20, 0x32, 0x36, 0x20, 0x32, 0x30, 0x32, 0x32, 0x20, 0x31, 0x32, 0x3a, 0x30, 0x30, 0x3a, 0x30, 0x32, 0x0a, 0x41, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x3a, 0x09, 0x73, 0x74, 0x65, 0x72, 0x6c, 0x69, 0x6e, 0x67, 0x40, 0x70, 0x61, 0x73, 0x73, 0x69, 0x76, 0x65, 0x6c, 0x6f, 0x67, 0x69, 0x63, 0x2e, 0x63, 0x6f, 0x6d, 0x0a, 0x48, 0x61, 0x73, 0x68, 0x3a, 0x09, 0x76, 0x30, 0x2e, 0x31, 0x2d, 0x31, 0x32, 0x39, 0x30, 0x2d, 0x67, 0x61, 0x61, 0x63, 0x30, 0x64, 0x30, 0x32, 0x65, 0x2d, 0x64, 0x69, 0x72, 0x74, 0x79, 0x0a, 0x44, 0x61, 0x74, 0x65, 0x3a, 0x09, 0x54, 0x75, 0x65, 0x20, 0x4a, 0x75, 0x6c, 0x20, 0x31, 0x39, 0x20, 0x31, 0x36, 0x3a, 0x31, 0x37, 0x3a, 0x35, 0x39, 0x20, 0x32, 0x30, 0x32, 0x32, 0x0a, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x72, 0x63, 0x00, 0x63, 0x6c, 0x65, 0x6e, 0x19, 0x01, 0x00, 0xff};
extern unsigned char PLAINTEXT[293];

int main()
{
    EVP_CIPHER_CTX *ctx;


@@ 21,9 14,12 @@ int main()

    int ciphertext_len;
    unsigned char ciphertext[1024];
    unsigned char tag[8];

    hexToBytes(IV_STR, sizeof(IV_STR), IV);
    hexToBytes(KEY_STR, strlen(KEY_STR), KEY);
    hexToBytes(IV_STR, strlen(IV_STR), IV);
    hexToBytes(PLAINTEXT_STR, strlen(PLAINTEXT_STR), PLAINTEXT);
    hexToBytes(CIPHERTEXT_STR, strlen(CIPHERTEXT_STR), CIPHERTEXT);
    hexToBytes(MIC_STR, strlen(MIC_STR), MIC);

    /* Create and initialise the context */
    if(!(ctx = EVP_CIPHER_CTX_new()))


@@ 40,7 36,7 @@ int main()
        handleErrors("3");

    /* Set tag length */
    EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, sizeof(tag), NULL);
    EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, sizeof(MIC), NULL);

    /* Initialise key and IV */
    if(1 != EVP_EncryptInit_ex(ctx, NULL, NULL, KEY, IV))


@@ 71,13 67,13 @@ int main()
    ciphertext_len += len;

    /* Get the tag */
    if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_GET_TAG, sizeof(tag), tag))
    if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_GET_TAG, sizeof(MIC), MIC))
        handleErrors("8");

    /* Clean up */
    EVP_CIPHER_CTX_free(ctx);

    printHex(tag, sizeof(tag));
    printHex(MIC, sizeof(MIC));
    printf("len: %d\n", ciphertext_len);
    printHex(ciphertext, ciphertext_len);


M lib.c => lib.c +0 -2
@@ 2,8 2,6 @@
#include <stdio.h>
#include <stdlib.h>

unsigned char PLAINTEXT[293] = {0xc4, 0xef, 0xbe, 0x01, 0x00, 0x01, 0x1a, 0x00, 0x08, 0x00, 0x00, 0xbf, 0x63, 0x6f, 0x66, 0x66, 0x00, 0x64, 0x64, 0x61, 0x74, 0x61, 0x59, 0x01, 0x00, 0x49, 0x4e, 0x46, 0x3a, 0x20, 0x0a, 0x0a, 0x42, 0x75, 0x69, 0x6c, 0x64, 0x20, 0x54, 0x69, 0x6d, 0x65, 0x3a, 0x09, 0x4a, 0x75, 0x6c, 0x20, 0x32, 0x36, 0x20, 0x32, 0x30, 0x32, 0x32, 0x20, 0x31, 0x32, 0x3a, 0x30, 0x30, 0x3a, 0x30, 0x32, 0x0a, 0x41, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x3a, 0x09, 0x73, 0x74, 0x65, 0x72, 0x6c, 0x69, 0x6e, 0x67, 0x40, 0x70, 0x61, 0x73, 0x73, 0x69, 0x76, 0x65, 0x6c, 0x6f, 0x67, 0x69, 0x63, 0x2e, 0x63, 0x6f, 0x6d, 0x0a, 0x48, 0x61, 0x73, 0x68, 0x3a, 0x09, 0x76, 0x30, 0x2e, 0x31, 0x2d, 0x31, 0x32, 0x39, 0x30, 0x2d, 0x67, 0x61, 0x61, 0x63, 0x30, 0x64, 0x30, 0x32, 0x65, 0x2d, 0x64, 0x69, 0x72, 0x74, 0x79, 0x0a, 0x44, 0x61, 0x74, 0x65, 0x3a, 0x09, 0x54, 0x75, 0x65, 0x20, 0x4a, 0x75, 0x6c, 0x20, 0x31, 0x39, 0x20, 0x31, 0x36, 0x3a, 0x31, 0x37, 0x3a, 0x35, 0x39, 0x20, 0x32, 0x30, 0x32, 0x32, 0x0a, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x72, 0x63, 0x00, 0x63, 0x6c, 0x65, 0x6e, 0x19, 0x01, 0x00, 0xff};

void handleErrors(const char *str) {
    fprintf(stderr, "Error: %s\n", str);
    exit(1);

M lib.h => lib.h +17 -0
@@ 1,6 1,23 @@
#ifndef _LIB_H_
#define _LIB_H_

//unsigned char PLAINTEXT[293] = {0xc4, 0xef, 0xbe, 0x01, 0x00, 0x01, 0x1a, 0x00, 0x08, 0x00, 0x00, 0xbf, 0x63, 0x6f, 0x66, 0x66, 0x00, 0x64, 0x64, 0x61, 0x74, 0x61, 0x59, 0x01, 0x00, 0x49, 0x4e, 0x46, 0x3a, 0x20, 0x0a, 0x0a, 0x42, 0x75, 0x69, 0x6c, 0x64, 0x20, 0x54, 0x69, 0x6d, 0x65, 0x3a, 0x09, 0x4a, 0x75, 0x6c, 0x20, 0x32, 0x36, 0x20, 0x32, 0x30, 0x32, 0x32, 0x20, 0x31, 0x32, 0x3a, 0x30, 0x30, 0x3a, 0x30, 0x32, 0x0a, 0x41, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x3a, 0x09, 0x73, 0x74, 0x65, 0x72, 0x6c, 0x69, 0x6e, 0x67, 0x40, 0x70, 0x61, 0x73, 0x73, 0x69, 0x76, 0x65, 0x6c, 0x6f, 0x67, 0x69, 0x63, 0x2e, 0x63, 0x6f, 0x6d, 0x0a, 0x48, 0x61, 0x73, 0x68, 0x3a, 0x09, 0x76, 0x30, 0x2e, 0x31, 0x2d, 0x31, 0x32, 0x39, 0x30, 0x2d, 0x67, 0x61, 0x61, 0x63, 0x30, 0x64, 0x30, 0x32, 0x65, 0x2d, 0x64, 0x69, 0x72, 0x74, 0x79, 0x0a, 0x44, 0x61, 0x74, 0x65, 0x3a, 0x09, 0x54, 0x75, 0x65, 0x20, 0x4a, 0x75, 0x6c, 0x20, 0x31, 0x39, 0x20, 0x31, 0x36, 0x3a, 0x31, 0x37, 0x3a, 0x35, 0x39, 0x20, 0x32, 0x30, 0x32, 0x32, 0x0a, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x72, 0x63, 0x00, 0x63, 0x6c, 0x65, 0x6e, 0x19, 0x01, 0x00, 0xff};
//unsigned char KEY[16] = {0xE3, 0xBB, 0x08, 0xF7, 0x9A, 0xCB, 0xF5, 0xEA, 0x14, 0x87, 0x98, 0x0D, 0xBE, 0xF7, 0x8D, 0x49};

char KEY_STR[] = "98B217B18E4AC60E57D52852C46280B9";
//char PLAINTEXT_STR[] = "0200EFBEFECABEBA0A00020000000301000002000010EFBEFECA";
char PLAINTEXT_STR[] = "c4efbe0100011a00080000bf636f6666006464617461590100494e463a200a0a4275696c642054696d653a094a756c20323620323032322031323a30303a30320a417574686f723a09737465726c696e6740706173736976656c6f6769632e636f6d0a486173683a0976302e312d313239302d6761616330643032652d64697274790a446174653a09547565204a756c2031392031363a31373a353920323032320a0a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000062726300636c656e190100ff";
char IV_STR[] = "0280000002003C000100000000";
char MIC_STR[] = "D188B56A94D354EC";
char CIPHERTEXT_STR[] = "50F569E06F2C5154ED0AEEEFEC2CEC84FB34566CA77E499512D0";

unsigned char KEY[sizeof(KEY_STR) / 2] = {0};
unsigned char PLAINTEXT[sizeof(PLAINTEXT_STR) / 2] = {0};
unsigned char IV[sizeof(IV_STR) / 2] = {0};
unsigned char MIC[sizeof(MIC_STR) / 2] = {0};
unsigned char CIPHERTEXT[sizeof(CIPHERTEXT_STR) / 2] = {0};

void init();
void handleErrors(const char *str);
void printHex(unsigned char *buf, int len);
int hexValue(char c);

M zencrypt.c => zencrypt.c +9 -12
@@ 11,12 11,6 @@

#include "lib.h"

unsigned char KEY[16] = {0xE3, 0xBB, 0x08, 0xF7, 0x9A, 0xCB, 0xF5, 0xEA, 0x14, 0x87, 0x98, 0x0D, 0xBE, 0xF7, 0x8D, 0x49};
unsigned char IV[13] = {0};
char IV_STR[26] = "01800000110038000100000000";
//unsigned char PLAINTEXT[293] = {0xc4, 0xef, 0xbe, 0x01, 0x00, 0x01, 0x1a, 0x00, 0x08, 0x00, 0x00, 0xbf, 0x63, 0x6f, 0x66, 0x66, 0x00, 0x64, 0x64, 0x61, 0x74, 0x61, 0x59, 0x01, 0x00, 0x49, 0x4e, 0x46, 0x3a, 0x20, 0x0a, 0x0a, 0x42, 0x75, 0x69, 0x6c, 0x64, 0x20, 0x54, 0x69, 0x6d, 0x65, 0x3a, 0x09, 0x4a, 0x75, 0x6c, 0x20, 0x32, 0x36, 0x20, 0x32, 0x30, 0x32, 0x32, 0x20, 0x31, 0x32, 0x3a, 0x30, 0x30, 0x3a, 0x30, 0x32, 0x0a, 0x41, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x3a, 0x09, 0x73, 0x74, 0x65, 0x72, 0x6c, 0x69, 0x6e, 0x67, 0x40, 0x70, 0x61, 0x73, 0x73, 0x69, 0x76, 0x65, 0x6c, 0x6f, 0x67, 0x69, 0x63, 0x2e, 0x63, 0x6f, 0x6d, 0x0a, 0x48, 0x61, 0x73, 0x68, 0x3a, 0x09, 0x76, 0x30, 0x2e, 0x31, 0x2d, 0x31, 0x32, 0x39, 0x30, 0x2d, 0x67, 0x61, 0x61, 0x63, 0x30, 0x64, 0x30, 0x32, 0x65, 0x2d, 0x64, 0x69, 0x72, 0x74, 0x79, 0x0a, 0x44, 0x61, 0x74, 0x65, 0x3a, 0x09, 0x54, 0x75, 0x65, 0x20, 0x4a, 0x75, 0x6c, 0x20, 0x31, 0x39, 0x20, 0x31, 0x36, 0x3a, 0x31, 0x37, 0x3a, 0x35, 0x39, 0x20, 0x32, 0x30, 0x32, 0x32, 0x0a, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x72, 0x63, 0x00, 0x63, 0x6c, 0x65, 0x6e, 0x19, 0x01, 0x00, 0xff};
extern unsigned char PLAINTEXT[293];

void sys_put_be16(int val, unsigned char *buf) {
    buf[0] = (val >> 8) & 0xFF;
    buf[1] = val & 0xFF;


@@ 240,16 234,19 @@ int bt_ccm_encrypt(const uint8_t key[16], uint8_t nonce[13],
}

int main() {
    uint8_t mic[8];
    uint8_t ciphertext[293 + sizeof(mic)] = {0};
    uint8_t ciphertext[293 + sizeof(MIC)] = {0};

    hexToBytes(IV_STR, sizeof(IV_STR), IV);
    hexToBytes(KEY_STR, strlen(KEY_STR), KEY);
    hexToBytes(IV_STR, strlen(IV_STR), IV);
    hexToBytes(PLAINTEXT_STR, strlen(PLAINTEXT_STR), PLAINTEXT);
    hexToBytes(CIPHERTEXT_STR, strlen(CIPHERTEXT_STR), CIPHERTEXT);
    hexToBytes(MIC_STR, strlen(MIC_STR), MIC);

    bt_ccm_encrypt(KEY, IV, PLAINTEXT, sizeof(PLAINTEXT), NULL, 0, ciphertext, sizeof(mic));
    bt_ccm_encrypt(KEY, IV, PLAINTEXT, sizeof(PLAINTEXT), NULL, 0, ciphertext, sizeof(MIC));

    memcpy(mic, ciphertext + sizeof(PLAINTEXT), sizeof(mic));
    memcpy(MIC, ciphertext + sizeof(PLAINTEXT), sizeof(MIC));

    printHex(mic, sizeof(mic));
    printHex(MIC, sizeof(MIC));
    printHex(ciphertext, sizeof(PLAINTEXT));

    return 0;