fsl_blob.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2014 Freescale Semiconductor, Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. *
  6. */
  7. #include <common.h>
  8. #include <malloc.h>
  9. #include <fsl_sec.h>
  10. #include <linux/errno.h>
  11. #include "jobdesc.h"
  12. #include "desc.h"
  13. #include "jr.h"
  14. int blob_decap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
  15. {
  16. int ret, i = 0;
  17. u32 *desc;
  18. printf("\nDecapsulating blob to get data\n");
  19. desc = malloc(sizeof(int) * MAX_CAAM_DESCSIZE);
  20. if (!desc) {
  21. debug("Not enough memory for descriptor allocation\n");
  22. return -1;
  23. }
  24. inline_cnstr_jobdesc_blob_decap(desc, key_mod, src, dst, len);
  25. debug("Descriptor dump:\n");
  26. for (i = 0; i < 14; i++)
  27. debug("Word[%d]: %08x\n", i, *(desc + i));
  28. ret = run_descriptor_jr(desc);
  29. if (ret)
  30. printf("Error in Decapsulation %d\n", ret);
  31. else
  32. printf("Decapsulation Success\n");
  33. free(desc);
  34. return ret;
  35. }
  36. int blob_encap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
  37. {
  38. int ret, i = 0;
  39. u32 *desc;
  40. printf("\nEncapsulating data to form blob\n");
  41. desc = malloc(sizeof(int) * MAX_CAAM_DESCSIZE);
  42. if (!desc) {
  43. debug("Not enough memory for descriptor allocation\n");
  44. return -1;
  45. }
  46. inline_cnstr_jobdesc_blob_encap(desc, key_mod, src, dst, len);
  47. debug("Descriptor dump:\n");
  48. for (i = 0; i < 14; i++)
  49. debug("Word[%d]: %08x\n", i, *(desc + i));
  50. ret = run_descriptor_jr(desc);
  51. if (ret)
  52. printf("Error in Encapsulation %d\n", ret);
  53. else
  54. printf("Encapsulation Success\n");
  55. free(desc);
  56. return ret;
  57. }
  58. #ifdef CONFIG_CMD_DEKBLOB
  59. int blob_dek(const u8 *src, u8 *dst, u8 len)
  60. {
  61. int ret, size, i = 0;
  62. u32 *desc;
  63. int out_sz = WRP_HDR_SIZE + len + KEY_BLOB_SIZE + MAC_SIZE;
  64. puts("\nEncapsulating provided DEK to form blob\n");
  65. desc = memalign(ARCH_DMA_MINALIGN,
  66. sizeof(uint32_t) * DEK_BLOB_DESCSIZE);
  67. if (!desc) {
  68. debug("Not enough memory for descriptor allocation\n");
  69. return -ENOMEM;
  70. }
  71. ret = inline_cnstr_jobdesc_blob_dek(desc, src, dst, len);
  72. if (ret) {
  73. debug("Error in Job Descriptor Construction: %d\n", ret);
  74. } else {
  75. size = roundup(sizeof(uint32_t) * DEK_BLOB_DESCSIZE,
  76. ARCH_DMA_MINALIGN);
  77. flush_dcache_range((unsigned long)desc,
  78. (unsigned long)desc + size);
  79. size = roundup(sizeof(uint8_t) * out_sz, ARCH_DMA_MINALIGN);
  80. flush_dcache_range((unsigned long)dst,
  81. (unsigned long)dst + size);
  82. ret = run_descriptor_jr(desc);
  83. }
  84. if (ret) {
  85. debug("Error in Encapsulation %d\n", ret);
  86. goto err;
  87. }
  88. size = roundup(out_sz, ARCH_DMA_MINALIGN);
  89. invalidate_dcache_range((unsigned long)dst, (unsigned long)dst+size);
  90. puts("DEK Blob\n");
  91. for (i = 0; i < out_sz; i++)
  92. printf("%02X", ((uint8_t *)dst)[i]);
  93. printf("\n");
  94. err:
  95. free(desc);
  96. return ret;
  97. }
  98. #endif