aes.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2014 Marek Vasut <marex@denx.de>
  4. *
  5. * Command for en/de-crypting block of memory with AES-128-CBC cipher.
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <uboot_aes.h>
  10. #include <malloc.h>
  11. #include <asm/byteorder.h>
  12. #include <linux/compiler.h>
  13. /**
  14. * do_aes() - Handle the "aes" command-line command
  15. * @cmdtp: Command data struct pointer
  16. * @flag: Command flag
  17. * @argc: Command-line argument count
  18. * @argv: Array of command-line arguments
  19. *
  20. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  21. * on error.
  22. */
  23. static int do_aes(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  24. {
  25. uint32_t key_addr, iv_addr, src_addr, dst_addr, len;
  26. uint8_t *key_ptr, *iv_ptr, *src_ptr, *dst_ptr;
  27. uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
  28. uint32_t aes_blocks;
  29. int enc;
  30. if (argc != 7)
  31. return CMD_RET_USAGE;
  32. if (!strncmp(argv[1], "enc", 3))
  33. enc = 1;
  34. else if (!strncmp(argv[1], "dec", 3))
  35. enc = 0;
  36. else
  37. return CMD_RET_USAGE;
  38. key_addr = simple_strtoul(argv[2], NULL, 16);
  39. iv_addr = simple_strtoul(argv[3], NULL, 16);
  40. src_addr = simple_strtoul(argv[4], NULL, 16);
  41. dst_addr = simple_strtoul(argv[5], NULL, 16);
  42. len = simple_strtoul(argv[6], NULL, 16);
  43. key_ptr = (uint8_t *)key_addr;
  44. iv_ptr = (uint8_t *)iv_addr;
  45. src_ptr = (uint8_t *)src_addr;
  46. dst_ptr = (uint8_t *)dst_addr;
  47. /* First we expand the key. */
  48. aes_expand_key(key_ptr, key_exp);
  49. /* Calculate the number of AES blocks to encrypt. */
  50. aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH);
  51. if (enc)
  52. aes_cbc_encrypt_blocks(key_exp, iv_ptr, src_ptr, dst_ptr,
  53. aes_blocks);
  54. else
  55. aes_cbc_decrypt_blocks(key_exp, iv_ptr, src_ptr, dst_ptr,
  56. aes_blocks);
  57. return 0;
  58. }
  59. /***************************************************/
  60. #ifdef CONFIG_SYS_LONGHELP
  61. static char aes_help_text[] =
  62. "enc key iv src dst len - Encrypt block of data $len bytes long\n"
  63. " at address $src using a key at address\n"
  64. " $key with initialization vector at address\n"
  65. " $iv. Store the result at address $dst.\n"
  66. " The $len size must be multiple of 16 bytes.\n"
  67. " The $key and $iv must be 16 bytes long.\n"
  68. "aes dec key iv src dst len - Decrypt block of data $len bytes long\n"
  69. " at address $src using a key at address\n"
  70. " $key with initialization vector at address\n"
  71. " $iv. Store the result at address $dst.\n"
  72. " The $len size must be multiple of 16 bytes.\n"
  73. " The $key and $iv must be 16 bytes long.";
  74. #endif
  75. U_BOOT_CMD(
  76. aes, 7, 1, do_aes,
  77. "AES 128 CBC encryption",
  78. aes_help_text
  79. );