blob: 345029fa784787bbc6d7473b56f8d1678615c51e [file] [log] [blame]
Philippe Reynes4df35782019-12-18 18:25:42 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2019, softathome
4 */
5
6#ifndef USE_HOSTCC
7#include <common.h>
8#include <malloc.h>
9#endif
10#include <image.h>
11#include <uboot_aes.h>
12
13int image_aes_decrypt(struct image_cipher_info *info,
14 const void *cipher, size_t cipher_len,
15 void **data, size_t *size)
16{
17#ifndef USE_HOSTCC
18 unsigned char key_exp[AES256_EXPAND_KEY_LENGTH];
19 unsigned int aes_blocks, key_len = info->cipher->key_len;
20
21 *data = malloc(cipher_len);
22 if (!*data) {
23 printf("Can't allocate memory to decrypt\n");
24 return -ENOMEM;
25 }
26 *size = info->size_unciphered;
27
28 memcpy(&key_exp[0], info->key, key_len);
29
30 /* First we expand the key. */
31 aes_expand_key((u8 *)info->key, key_len, key_exp);
32
33 /* Calculate the number of AES blocks to encrypt. */
34 aes_blocks = DIV_ROUND_UP(cipher_len, AES_BLOCK_LENGTH);
35
36 aes_cbc_decrypt_blocks(key_len, key_exp, (u8 *)info->iv,
37 (u8 *)cipher, *data, aes_blocks);
38#endif
39
40 return 0;
41}