1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2019, softathome
11 #include <uboot_aes.h>
13 int image_aes_decrypt(struct image_cipher_info *info,
14 const void *cipher, size_t cipher_len,
15 void **data, size_t *size)
18 unsigned char key_exp[AES256_EXPAND_KEY_LENGTH];
19 unsigned int aes_blocks, key_len = info->cipher->key_len;
21 *data = malloc(cipher_len);
23 printf("Can't allocate memory to decrypt\n");
26 *size = info->size_unciphered;
28 memcpy(&key_exp[0], info->key, key_len);
30 /* First we expand the key. */
31 aes_expand_key((u8 *)info->key, key_len, key_exp);
33 /* Calculate the number of AES blocks to encrypt. */
34 aes_blocks = DIV_ROUND_UP(cipher_len, AES_BLOCK_LENGTH);
36 aes_cbc_decrypt_blocks(key_len, key_exp, (u8 *)info->iv,
37 (u8 *)cipher, *data, aes_blocks);