fix crypto.c:aes_cbc_reset, use it to avoid continuous malloc/free of aes_ctx_audio

This commit is contained in:
fduncanh
2022-01-12 17:17:22 -05:00
parent cb831783c1
commit 2fa1aa9e9e
2 changed files with 11 additions and 5 deletions

View File

@@ -165,7 +165,7 @@ void aes_cbc_decrypt(aes_ctx_t *ctx, const uint8_t *in, uint8_t *out, int len) {
}
void aes_cbc_reset(aes_ctx_t *ctx) {
aes_reset(ctx, EVP_aes_128_ctr(), ctx->direction);
aes_reset(ctx, EVP_aes_128_cbc(), ctx->direction);
}
void aes_cbc_destroy(aes_ctx_t *ctx) {

View File

@@ -49,7 +49,7 @@ struct raop_buffer_s {
/* Key and IV used for decryption */
unsigned char aeskey[RAOP_AESKEY_LEN];
unsigned char aesiv[RAOP_AESIV_LEN];
aes_ctx_t *aes_ctx_audio;
/* First and last seqnum */
int is_empty;
unsigned short first_seqnum;
@@ -74,6 +74,7 @@ raop_buffer_init(logger_t *logger,
raop_buffer->logger = logger;
memcpy(raop_buffer->aeskey, aeskey, RAOP_AESKEY_LEN);
memcpy(raop_buffer->aesiv, aesiv, RAOP_AESIV_LEN);
raop_buffer->aes_ctx_audio = NULL;
#ifdef DUMP_AUDIO
if (file_keyiv != NULL) {
@@ -104,6 +105,8 @@ raop_buffer_destroy(raop_buffer_t *raop_buffer)
}
}
aes_cbc_destroy(raop_buffer->aes_ctx_audio);
if (raop_buffer) {
free(raop_buffer);
}
@@ -158,9 +161,12 @@ raop_buffer_decrypt(raop_buffer_t *raop_buffer, unsigned char *data, unsigned ch
encryptedlen = payload_size / 16*16;
memset(output, 0, payload_size);
// Need to be initialized internally
aes_ctx_t *aes_ctx_audio = aes_cbc_init(raop_buffer->aeskey, raop_buffer->aesiv, AES_DECRYPT);
aes_cbc_decrypt(aes_ctx_audio, &data[12], output, encryptedlen);
aes_cbc_destroy(aes_ctx_audio);
if (!raop_buffer->aes_ctx_audio) {
raop_buffer->aes_ctx_audio = aes_cbc_init(raop_buffer->aeskey, raop_buffer->aesiv, AES_DECRYPT);
} else {
aes_cbc_reset(raop_buffer->aes_ctx_audio);
}
aes_cbc_decrypt(raop_buffer->aes_ctx_audio, &data[12], output, encryptedlen);
memcpy(output + encryptedlen, &data[12 + encryptedlen], payload_size - encryptedlen);
*outputlen = payload_size;