I am attempting to decrypt some test data that is encrypted by a blueooth module. The bluetooth's firmware is programmed in C, if that matters.
The data encrypted was:
// Test Bytes - 16 bytes
byte[] testInput = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
// Test key - 16 bytes, 128-bit
byte[] keyBytes = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
// Test nonce - 13 bytes, 104-bit
byte[] nonce = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
0x0a,0x0b,0x0c};
Here's the problem. Encrypting the data in C, using AES/CCM, produces an ouput of 16-bytes, with a 4-byte MIC. When I encrypt the data using AES/CCM/NoPadding in Java, the output is also 16 bytes, but has a 8-byte MAC. The terms MAC and MIC are ambiguous, it seems, where MIC is used for bluetooth terminology.
When I encrypt the above testInput in Java, I get the same 16 bytes of output as the C programming encryption. But, due to the MIC and MAC being different lengths, I cannot decrypt the data on either end.
Is there a solution to this?
I have added my Java code:
Cipher cipher = Cipher.getInstance("AES/CCM/NoPadding", "BC");
SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(nonce);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] encrypted = cipher.doFinal(testInput);
// The first 16 bytes print out equivalently with the C-language AES/CCM
Below is an image of my output:
Below is an image of the C output.
BLE Advertising Packet
EDIT: I am using the selected answer, but please look at Matthew Beckler's answer as well. It will provide a bit more depth to the answer, as well as prevent errors later.