aes.c 3.0 KB

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