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; }
/** * Prepend a string followed by a space to the buffer * * If hexify == TRUE do an inplace ascii-hex encoding before prepending the string. The assumption * is that the input string is NUL terminated ascii string. This is true for all of the current * AllJoyn authentication mechanisms. */ static AJ_Status PrependStr(const char* str, char* buf, uint32_t bufLen, uint8_t hexify) { size_t used = strlen(buf); size_t sz = strlen(str); if (hexify && (used > 0)) { AJ_Status status = AJ_RawToHex((uint8_t*)buf, used, buf, bufLen); if (status != AJ_OK) { return status; } /* Conversion to hex doubles the size of the string */ used *= 2; } if ((used + sz + 1) >= bufLen) { return AJ_ERR_RESOURCES; } if (used) { memmove(buf + sz + 1, buf, used + 1); memcpy(buf, str, sz); buf[sz] = ' '; } else { memcpy(buf, str, sz); buf[sz] = '\0'; } return AJ_OK; }
static AJ_Status SetPasscode(const char* daemonRealm, const uint8_t* newPasscode, uint8_t newPasscodeLen) { AJ_Status status = AJ_OK; char newStringPasscode[PASSWORD_VALUE_LENGTH + 1]; status = AJ_RawToHex(newPasscode, newPasscodeLen, newStringPasscode, sizeof(newStringPasscode), FALSE); if (status != AJ_OK) { return status; } if (AJSVC_PropertyStore_SetValue(AJSVC_PROPERTY_STORE_REALM_NAME, daemonRealm) && AJSVC_PropertyStore_SetValue(AJSVC_PROPERTY_STORE_PASSCODE, newStringPasscode)) { status = AJSVC_PropertyStore_SaveAll(); if (status != AJ_OK) { return status; } AJ_ClearCredentials(); status = AJ_ERR_READ; //Force disconnect of AJ and services to refresh current sessions } else { status = AJSVC_PropertyStore_LoadAll(); if (status != AJ_OK) { return status; } } return status; }
static AJ_Status KeyGen(const char* peerName, uint8_t role, const char* nonce1, const char* nonce2, uint8_t* outBuf, uint32_t len) { AJ_Status status; const uint8_t* data[4]; uint8_t lens[4]; const AJ_GUID* peerGuid = AJ_GUID_Find(peerName); AJ_PeerCred cred; status = AJ_GetRemoteCredential(peerGuid, &cred); if (AJ_OK != status) { return AJ_ERR_NO_MATCH; } data[0] = cred.secret; lens[0] = (uint32_t)sizeof(cred.secret); data[1] = (uint8_t*)nonce1; lens[1] = (uint32_t)strlen(nonce1); data[2] = (uint8_t*)nonce2; lens[2] = (uint32_t)strlen(nonce2); data[3] = (uint8_t*)"session key"; lens[3] = 11; /* * We use the outBuf to store both the key and verifier string. * Check that there is enough space to do so. */ if (len < (AES_KEY_LEN + VERIFIER_LEN)) { return AJ_ERR_RESOURCES; } status = AJ_Crypto_PRF(data, lens, ArraySize(data), outBuf, AES_KEY_LEN + VERIFIER_LEN); /* * Store the session key and compose the verifier string. */ if (status == AJ_OK) { status = AJ_SetSessionKey(peerName, outBuf, role); } if (status == AJ_OK) { memmove(outBuf, outBuf + AES_KEY_LEN, VERIFIER_LEN); status = AJ_RawToHex(outBuf, VERIFIER_LEN, (char*)outBuf, len); } 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; }
AJ_Status AJ_GUID_ToString(const AJ_GUID* guid, char* buffer, uint32_t bufLen) { return AJ_RawToHex(guid->val, 16, buffer, bufLen, TRUE); }
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; }