void AesCounter::encrypt(const unsigned char *datain, unsigned char *dataout, int len, std::uint64_t sequenceNumber) { memset(ctr_buf, 0, DREL_AES_BLOCKLEN_BYTES); if (len <= 0 || len > DREL_AES_MAXPACKETOCT) throw AesException("Invalid input plaintext message length"); if (sequenceNumber == 0xFFFFFFFFFFFFFFFF) throw AesException("Packet counter exceeded - IV"); // Copy secretIV to counter buffer memcpy(ctr_buf, mSecretIV, DREL_AESCOUNTER_SECRETIVLEN); // Copy packet sequence number to counter buffer std::uint64_t beSequencenumber = San2::Utils::Endian::san_s_be64toh(sequenceNumber); memcpy(ctr_buf + DREL_AESCOUNTER_SECRETIVLEN, &beSequencenumber, sizeof(std::uint64_t)); // Set blockCounter to one std::uint16_t blockCounter = 1; blockCounter = San2::Utils::Endian::san_u_htobe16(blockCounter); memcpy(ctr_buf + DREL_AESCOUNTER_SECRETIVLEN + sizeof(std::uint64_t), &blockCounter, sizeof(std::uint16_t)); //aes_mode_reset(aes_ctx); // important!!! do not delete!!! if (aes_encrypt_key256(mKey, aes_ctx) != EXIT_SUCCESS) throw AesException("Key setup failed"); int ret = aes_ctr_crypt(datain, dataout, len, ctr_buf, aes_custom_inc, aes_ctx); if (ret != EXIT_SUCCESS) throw AesException("Aes encryption failed"); }
int main(int argc, char *argv[]) { unsigned char encrypted[sizeof(message)]; unsigned char decrypted[sizeof(message)]; unsigned char nonce[] = { 0xde, 0xea, 0xbe, 0xef, 'd', 'e', 'a', 'd' }; memcpy(decrypted, message, sizeof(message)); aes_ctr_crypt(decrypted, encrypted, sizeof(message), key_size * 8, key, nonce, true); memset(decrypted, 0, sizeof(message)); aes_ctr_crypt(encrypted, decrypted, sizeof(message), key_size * 8, key, nonce, true); printf("%s", decrypted); return 0; }
int BCMROMFN(aes_ccm_decrypt)(unsigned int *rk, const size_t key_len, const uint8 *nonce, const size_t aad_len, const uint8 *aad, const size_t data_len, const uint8 *ctxt, uint8 *ptxt) { uint8 A[AES_BLOCK_SZ], X[AES_BLOCK_SZ]; /* initialize counter */ A[0] = AES_CCM_CRYPT_FLAGS; memcpy(&A[1], nonce, AES_CCM_NONCE_LEN); A[AES_BLOCK_SZ - 2] = 0; A[AES_BLOCK_SZ - 1] = 1; pres("aes_ccm_decrypt: initial counter:", AES_BLOCK_SZ, A); /* decrypt data */ if (aes_ctr_crypt(rk, key_len, A, data_len-AES_CCM_AUTH_LEN, ctxt, ptxt)) return (-1); pres("aes_ccm_decrypt: decrypted data:", data_len-AES_CCM_AUTH_LEN, ptxt); /* decrypt MAC */ A[AES_BLOCK_SZ - 2] = 0; A[AES_BLOCK_SZ - 1] = 0; if (aes_ctr_crypt(rk, key_len, A, AES_CCM_AUTH_LEN, ctxt+data_len-AES_CCM_AUTH_LEN, ptxt + data_len - AES_CCM_AUTH_LEN)) return (-1); pres("aes_ccm_decrypt: decrypted MAC:", AES_CCM_AUTH_LEN, ptxt + data_len - AES_CCM_AUTH_LEN); /* calculate MAC */ if (aes_ccm_mac(rk, key_len, nonce, aad_len, aad, data_len - AES_CCM_AUTH_LEN, ptxt, X)) return (-1); pres("aes_ccm_decrypt: MAC:", AES_CCM_AUTH_LEN, X); if (memcmp(X, ptxt + data_len - AES_CCM_AUTH_LEN, AES_CCM_AUTH_LEN) != 0) return (-1); return (0); }
AES_RETURN rfc3686_crypt(const unsigned char *ibuf, unsigned char *obuf, int len, unsigned char *cbuf, aes_encrypt_ctx cx[1]) { return aes_ctr_crypt(ibuf, obuf, len, cbuf, rfc3686_inc, cx); }