aes.c 3.2 KB

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