blob.c 3.2 KB

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