int AJ_Main(void) { AJ_Status status = AJ_OK; size_t i; char out[128]; for (i = 0; i < ArraySize(testVector); i++) { uint8_t key[16]; uint8_t msg[64]; uint8_t nonce[16]; uint32_t nlen = (uint32_t)strlen(testVector[i].nonce) / 2; uint32_t mlen = (uint32_t)strlen(testVector[i].input) / 2; AJ_HexToRaw(testVector[i].key, 0, key, sizeof(key)); AJ_HexToRaw(testVector[i].nonce, 0, nonce, nlen); AJ_HexToRaw(testVector[i].input, 0, msg, mlen); status = AJ_Encrypt_CCM(key, msg, mlen, testVector[i].hdrLen, testVector[i].authLen, nonce, nlen); if (status != AJ_OK) { AJ_AlwaysPrintf(("Encryption failed (%d) for test #%zu\n", status, i)); goto ErrorExit; } AJ_RawToHex(msg, mlen + testVector[i].authLen, out, sizeof(out), FALSE); if (strcmp(out, testVector[i].output) != 0) { AJ_AlwaysPrintf(("Encrypt verification failure for test #%zu\n%s\n", i, out)); goto ErrorExit; } /* * Verify decryption. */ status = AJ_Decrypt_CCM(key, msg, mlen, testVector[i].hdrLen, testVector[i].authLen, nonce, nlen); if (status != AJ_OK) { AJ_AlwaysPrintf(("Authentication failure (%d) for test #%zu\n", status, i)); goto ErrorExit; } AJ_RawToHex(msg, mlen, out, sizeof(out), FALSE); if (strcmp(out, testVector[i].input) != 0) { AJ_AlwaysPrintf(("Decrypt verification failure for test #%zu\n%s\n", i, out)); goto ErrorExit; } AJ_AlwaysPrintf(("Passed and verified test #%zu\n", i)); } AJ_AlwaysPrintf(("AES CCM unit test PASSED\n")); return 0; ErrorExit: AJ_AlwaysPrintf(("AES CCM unit test FAILED\n")); return 1; }
int main(void) { AJ_Status status = AJ_OK; size_t i; uint8_t cmp[1024]; for (i = 0; i < sizeof(msg); ++i) { msg[i] = (uint8_t)(127 + i * 11 + i * 13 + i * 17); } for (i = 0; i < 10000; ++i) { uint8_t hdrLen; for (hdrLen = 10; hdrLen < 60; hdrLen += 3) { memcpy(cmp, msg, sizeof(msg)); status = AJ_Encrypt_CCM(key, msg, sizeof(msg), hdrLen, 12, (const uint8_t*) nonce, sizeof(nonce)); if (status != AJ_OK) { AJ_AlwaysPrintf(("Encryption failed (%d) for test #%zu\n", status, i)); goto ErrorExit; } status = AJ_Decrypt_CCM(key, msg, sizeof(msg), hdrLen, 12, (const uint8_t*) nonce, sizeof(nonce)); if (status != AJ_OK) { AJ_AlwaysPrintf(("Authentication failure (%d) for test #%zu\n", status, i)); goto ErrorExit; } if (memcmp(cmp, msg, sizeof(msg)) != 0) { AJ_AlwaysPrintf(("Decrypt verification failure \n")); goto ErrorExit; } nonce[0] += 1; } } return 0; ErrorExit: AJ_AlwaysPrintf(("AES CCM unit test FAILED\n")); return 1; }
static AJ_Status DecryptMessage(AJ_Message* msg) { AJ_IOBuffer* ioBuf = &msg->bus->sock.rx; AJ_Status status; uint8_t key[16]; uint8_t nonce[5]; uint8_t role = AJ_ROLE_KEY_UNDEFINED; uint32_t mlen = MessageLen(msg); uint32_t hLen = mlen - msg->hdr->bodyLen; /* * TODO - handle case where msg endianess is different than the host endianess */ if (msg->hdr->endianess != HOST_ENDIANESS) { return AJ_ERR_SECURITY; } /* * Use the group key for multicast and broadcast signals the session key otherwise. */ if ((msg->hdr->msgType == AJ_MSG_SIGNAL) && !msg->destination) { status = AJ_GetGroupKey(msg->sender, key); } else { status = AJ_GetSessionKey(msg->sender, key, &role); /* * We use the oppsite role when decrypting. */ role ^= 3; } if (status != AJ_OK) { status = AJ_ERR_SECURITY; } else { InitNonce(msg, role, nonce); status = AJ_Decrypt_CCM(key, ioBuf->bufStart, mlen - MAC_LENGTH, hLen, MAC_LENGTH, nonce, sizeof(nonce)); } return status; }
int AJ_Main(void) { AJ_Status status = AJ_OK; size_t i; char out[128]; for (i = 0; i < ArraySize(testVector); i++) { uint8_t key[16]; uint8_t msg[64]; uint8_t nonce[16]; uint32_t nlen = (uint32_t)strlen(testVector[i].nonce) / 2; uint32_t mlen = (uint32_t)strlen(testVector[i].input) / 2; AJ_HexToRaw(testVector[i].key, 0, key, sizeof(key)); AJ_HexToRaw(testVector[i].nonce, 0, nonce, nlen); AJ_HexToRaw(testVector[i].input, 0, msg, mlen); status = AJ_Encrypt_CCM(key, msg, mlen, testVector[i].hdrLen, testVector[i].authLen, nonce, nlen); if (status != AJ_OK) { AJ_Printf("Encryption failed (%d) for test #%zu\n", status, i); goto ErrorExit; } AJ_RawToHex(msg, mlen + testVector[i].authLen, out, sizeof(out), FALSE); if (strcmp(out, testVector[i].output) != 0) { AJ_Printf("Encrypt verification failure for test #%zu\n%s\n", i, out); goto ErrorExit; } /* * Verify decryption. */ status = AJ_Decrypt_CCM(key, msg, mlen, testVector[i].hdrLen, testVector[i].authLen, nonce, nlen); if (status != AJ_OK) { AJ_Printf("Authentication failure (%d) for test #%zu\n", status, i); goto ErrorExit; } AJ_RawToHex(msg, mlen, out, sizeof(out), FALSE); if (strcmp(out, testVector[i].input) != 0) { AJ_Printf("Decrypt verification failure for test #%zu\n%s\n", i, out); goto ErrorExit; } AJ_Printf("Passed and verified test #%zu\n", i); } AJ_Printf("AES CCM unit test PASSED\n"); { static const char expect[] = "F19787716404918CA20F174CFF2E165F21B17A70C472480AE91891B5BB8DD261CBD4273612D41BC6"; const char secret[] = "1234ABCDE"; const char seed[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234"; uint8_t key[40]; const char* inputs[3]; uint8_t length[3]; inputs[0] = secret; length[0] = (uint8_t)strlen(secret); inputs[1] = seed; length[1] = (uint8_t)strlen(seed); inputs[2] = "prf test"; length[2] = 8; status = AJ_Crypto_PRF((const uint8_t**)inputs, length, ArraySize(inputs), key, sizeof(key)); if (status != AJ_OK) { AJ_Printf("AJ_Crypto_PRF %d\n", status); goto ErrorExit; } AJ_RawToHex(key, sizeof(key), out, sizeof(out), FALSE); if (strcmp(out, expect) != 0) { AJ_Printf("AJ_Crypto_PRF failed: %d\n", status); goto ErrorExit; } AJ_Printf("AJ_Crypto_PRF test PASSED: %d\n", status); } return 0; ErrorExit: AJ_Printf("AES CCM unit test FAILED\n"); return 1; }
int AJ_Main(void) { AJ_Status status = AJ_OK; uint32_t i; AJ_AlwaysPrintf(("AES CCM unit test start\n")); for (i = 0; i < ArraySize(testVector); i++) { uint8_t key[16]; uint8_t input[64]; uint8_t* msg; uint8_t nonce[16]; uint32_t nlen = (uint32_t)strlen(testVector[i].nonce) / 2; uint32_t ilen = (uint32_t)strlen(testVector[i].input) / 2; uint32_t mlen = ilen * testVector[i].repeat; uint32_t j; char* out; size_t olen; AJ_HexToRaw(testVector[i].key, 0, key, sizeof(key)); AJ_HexToRaw(testVector[i].nonce, 0, nonce, nlen); AJ_HexToRaw(testVector[i].input, 0, input, mlen); msg = AJ_Malloc(mlen + testVector[i].authLen); if (!msg) { AJ_AlwaysPrintf(("Allocation failed for test #%zu\n", i)); goto ErrorExit; } for (j = 0; j < testVector[i].repeat; j++) { memcpy(&msg[ilen * j], &input[0], ilen); } olen = 2 * (mlen + testVector[i].authLen) + 1; out = AJ_Malloc(olen); if (!out) { AJ_AlwaysPrintf(("Allocation failed for test #%zu\n", i)); goto ErrorExit; } status = AJ_Encrypt_CCM(key, msg, mlen, testVector[i].hdrLen, testVector[i].authLen, nonce, nlen); if (status != AJ_OK) { AJ_AlwaysPrintf(("Encryption failed (%d) for test #%u\n", status, i)); goto ErrorExit; } AJ_RawToHex(msg, mlen + testVector[i].authLen, out, olen, FALSE); if (strcmp(out, testVector[i].output) != 0) { AJ_AlwaysPrintf(("Encrypt verification failure for test #%u\n%s\n", i, out)); goto ErrorExit; } /* * Verify decryption. */ status = AJ_Decrypt_CCM(key, msg, mlen, testVector[i].hdrLen, testVector[i].authLen, nonce, nlen); if (status != AJ_OK) { AJ_AlwaysPrintf(("Authentication failure (%d) for test #%u\n", status, i)); goto ErrorExit; } AJ_RawToHex(msg, mlen, out, olen, FALSE); for (j = 0; j < testVector[i].repeat; j++) { if (strncmp(&out[2 * ilen * j], testVector[i].input, ilen * 2) != 0) { AJ_AlwaysPrintf(("Decrypt verification failure for test #%u\n%s\n", i, out)); goto ErrorExit; } } AJ_AlwaysPrintf(("Passed and verified test #%zu\n", i)); AJ_Free(msg); AJ_Free(out); } AJ_AlwaysPrintf(("AES CCM unit test PASSED\n")); return 0; ErrorExit: AJ_AlwaysPrintf(("AES CCM unit test FAILED\n")); return 1; }