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