blob.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. *
  3. * Command for encapsulating/decapsulating blob of memory.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <environment.h>
  10. #include <malloc.h>
  11. #include <asm/byteorder.h>
  12. #include <linux/compiler.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. /**
  15. * blob_decap() - Decapsulate the data as a blob
  16. * @key_mod: - Pointer to key modifier/key
  17. * @src: - Address of data to be decapsulated
  18. * @dst: - Address of data to be decapsulated
  19. * @len: - Size of data to be decapsulated
  20. *
  21. * Returns zero on success,and negative on error.
  22. */
  23. __weak int blob_decap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
  24. {
  25. return 0;
  26. }
  27. /**
  28. * blob_encap() - Encapsulate the data as a blob
  29. * @key_mod: - Pointer to key modifier/key
  30. * @src: - Address of data to be encapsulated
  31. * @dst: - Address of data to be encapsulated
  32. * @len: - Size of data to be encapsulated
  33. *
  34. * Returns zero on success,and negative on error.
  35. */
  36. __weak int blob_encap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
  37. {
  38. return 0;
  39. }
  40. /**
  41. * do_blob() - Handle the "blob" command-line command
  42. * @cmdtp: Command data struct pointer
  43. * @flag: Command flag
  44. * @argc: Command-line argument count
  45. * @argv: Array of command-line arguments
  46. *
  47. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  48. * on error.
  49. */
  50. static int do_blob(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  51. {
  52. uint32_t key_addr, src_addr, dst_addr, len;
  53. uint8_t *km_ptr, *src_ptr, *dst_ptr;
  54. int enc, ret = 0;
  55. if (argc != 6)
  56. return CMD_RET_USAGE;
  57. if (!strncmp(argv[1], "enc", 3))
  58. enc = 1;
  59. else if (!strncmp(argv[1], "dec", 3))
  60. enc = 0;
  61. else
  62. return CMD_RET_USAGE;
  63. src_addr = simple_strtoul(argv[2], NULL, 16);
  64. dst_addr = simple_strtoul(argv[3], NULL, 16);
  65. len = simple_strtoul(argv[4], NULL, 16);
  66. key_addr = simple_strtoul(argv[5], NULL, 16);
  67. km_ptr = (uint8_t *)(uintptr_t)key_addr;
  68. src_ptr = (uint8_t *)(uintptr_t)src_addr;
  69. dst_ptr = (uint8_t *)(uintptr_t)dst_addr;
  70. if (enc)
  71. ret = blob_encap(km_ptr, src_ptr, dst_ptr, len);
  72. else
  73. ret = blob_decap(km_ptr, src_ptr, dst_ptr, len);
  74. return ret;
  75. }
  76. /***************************************************/
  77. static char blob_help_text[] =
  78. "enc src dst len km - Encapsulate and create blob of data\n"
  79. " $len bytes long at address $src and\n"
  80. " store the result at address $dst.\n"
  81. " $km is the address where the key\n"
  82. " modifier is stored.\n"
  83. " The modifier is required for generation\n"
  84. " /use as key for cryptographic operation.\n"
  85. " Key modifier should be 16 byte long.\n"
  86. "blob dec src dst len km - Decapsulate the blob of data at address\n"
  87. " $src and store result of $len byte at\n"
  88. " addr $dst.\n"
  89. " $km is the address where the key\n"
  90. " modifier is stored.\n"
  91. " The modifier is required for generation\n"
  92. " /use as key for cryptographic operation.\n"
  93. " Key modifier should be 16 byte long.\n";
  94. U_BOOT_CMD(
  95. blob, 6, 1, do_blob,
  96. "Blob encapsulation/decryption",
  97. blob_help_text
  98. );