aes-decrypt.c 965 B

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