fsl_blob.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2014 Freescale Semiconductor, Inc.
  4. *
  5. */
  6. #include <common.h>
  7. #include <malloc.h>
  8. #include <memalign.h>
  9. #include <fsl_sec.h>
  10. #include <linux/errno.h>
  11. #include "jobdesc.h"
  12. #include "desc.h"
  13. #include "jr.h"
  14. /**
  15. * blob_decap() - Decapsulate the data from a blob
  16. * @key_mod: - Key modifier address
  17. * @src: - Source address (blob)
  18. * @dst: - Destination address (data)
  19. * @len: - Size of decapsulated data
  20. *
  21. * Note: Start and end of the key_mod, src and dst buffers have to be aligned to
  22. * the cache line size (ARCH_DMA_MINALIGN) for the CAAM operation to succeed.
  23. *
  24. * Returns zero on success, negative on error.
  25. */
  26. int blob_decap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
  27. {
  28. int ret, size, i = 0;
  29. u32 *desc;
  30. if (!IS_ALIGNED((uintptr_t)key_mod, ARCH_DMA_MINALIGN) ||
  31. !IS_ALIGNED((uintptr_t)src, ARCH_DMA_MINALIGN) ||
  32. !IS_ALIGNED((uintptr_t)dst, ARCH_DMA_MINALIGN)) {
  33. puts("Error: blob_decap: Address arguments are not aligned!\n");
  34. return -EINVAL;
  35. }
  36. printf("\nDecapsulating blob to get data\n");
  37. desc = malloc_cache_aligned(sizeof(int) * MAX_CAAM_DESCSIZE);
  38. if (!desc) {
  39. debug("Not enough memory for descriptor allocation\n");
  40. return -ENOMEM;
  41. }
  42. size = ALIGN(16, ARCH_DMA_MINALIGN);
  43. flush_dcache_range((unsigned long)key_mod,
  44. (unsigned long)key_mod + size);
  45. size = ALIGN(BLOB_SIZE(len), ARCH_DMA_MINALIGN);
  46. flush_dcache_range((unsigned long)src,
  47. (unsigned long)src + size);
  48. inline_cnstr_jobdesc_blob_decap(desc, key_mod, src, dst, len);
  49. debug("Descriptor dump:\n");
  50. for (i = 0; i < 14; i++)
  51. debug("Word[%d]: %08x\n", i, *(desc + i));
  52. size = ALIGN(sizeof(int) * MAX_CAAM_DESCSIZE, ARCH_DMA_MINALIGN);
  53. flush_dcache_range((unsigned long)desc,
  54. (unsigned long)desc + size);
  55. ret = run_descriptor_jr(desc);
  56. if (ret) {
  57. printf("Error in blob decapsulation: %d\n", ret);
  58. } else {
  59. size = ALIGN(len, ARCH_DMA_MINALIGN);
  60. invalidate_dcache_range((unsigned long)dst,
  61. (unsigned long)dst + size);
  62. puts("Blob decapsulation successful.\n");
  63. }
  64. free(desc);
  65. return ret;
  66. }
  67. /**
  68. * blob_encap() - Encapsulate the data as a blob
  69. * @key_mod: - Key modifier address
  70. * @src: - Source address (data)
  71. * @dst: - Destination address (blob)
  72. * @len: - Size of data to be encapsulated
  73. *
  74. * Note: Start and end of the key_mod, src and dst buffers have to be aligned to
  75. * the cache line size (ARCH_DMA_MINALIGN) for the CAAM operation to succeed.
  76. *
  77. * Returns zero on success, negative on error.
  78. */
  79. int blob_encap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
  80. {
  81. int ret, size, i = 0;
  82. u32 *desc;
  83. if (!IS_ALIGNED((uintptr_t)key_mod, ARCH_DMA_MINALIGN) ||
  84. !IS_ALIGNED((uintptr_t)src, ARCH_DMA_MINALIGN) ||
  85. !IS_ALIGNED((uintptr_t)dst, ARCH_DMA_MINALIGN)) {
  86. puts("Error: blob_encap: Address arguments are not aligned!\n");
  87. return -EINVAL;
  88. }
  89. printf("\nEncapsulating data to form blob\n");
  90. desc = malloc_cache_aligned(sizeof(int) * MAX_CAAM_DESCSIZE);
  91. if (!desc) {
  92. debug("Not enough memory for descriptor allocation\n");
  93. return -ENOMEM;
  94. }
  95. size = ALIGN(16, ARCH_DMA_MINALIGN);
  96. flush_dcache_range((unsigned long)key_mod,
  97. (unsigned long)key_mod + size);
  98. size = ALIGN(len, ARCH_DMA_MINALIGN);
  99. flush_dcache_range((unsigned long)src,
  100. (unsigned long)src + size);
  101. inline_cnstr_jobdesc_blob_encap(desc, key_mod, src, dst, len);
  102. debug("Descriptor dump:\n");
  103. for (i = 0; i < 14; i++)
  104. debug("Word[%d]: %08x\n", i, *(desc + i));
  105. size = ALIGN(sizeof(int) * MAX_CAAM_DESCSIZE, ARCH_DMA_MINALIGN);
  106. flush_dcache_range((unsigned long)desc,
  107. (unsigned long)desc + size);
  108. ret = run_descriptor_jr(desc);
  109. if (ret) {
  110. printf("Error in blob encapsulation: %d\n", ret);
  111. } else {
  112. size = ALIGN(BLOB_SIZE(len), ARCH_DMA_MINALIGN);
  113. invalidate_dcache_range((unsigned long)dst,
  114. (unsigned long)dst + size);
  115. puts("Blob encapsulation successful.\n");
  116. }
  117. free(desc);
  118. return ret;
  119. }
  120. #ifdef CONFIG_CMD_DEKBLOB
  121. int blob_dek(const u8 *src, u8 *dst, u8 len)
  122. {
  123. int ret, size, i = 0;
  124. u32 *desc;
  125. int out_sz = WRP_HDR_SIZE + len + KEY_BLOB_SIZE + MAC_SIZE;
  126. puts("\nEncapsulating provided DEK to form blob\n");
  127. desc = memalign(ARCH_DMA_MINALIGN,
  128. sizeof(uint32_t) * DEK_BLOB_DESCSIZE);
  129. if (!desc) {
  130. debug("Not enough memory for descriptor allocation\n");
  131. return -ENOMEM;
  132. }
  133. ret = inline_cnstr_jobdesc_blob_dek(desc, src, dst, len);
  134. if (ret) {
  135. debug("Error in Job Descriptor Construction: %d\n", ret);
  136. } else {
  137. size = roundup(sizeof(uint32_t) * DEK_BLOB_DESCSIZE,
  138. ARCH_DMA_MINALIGN);
  139. flush_dcache_range((unsigned long)desc,
  140. (unsigned long)desc + size);
  141. size = roundup(sizeof(uint8_t) * out_sz, ARCH_DMA_MINALIGN);
  142. flush_dcache_range((unsigned long)dst,
  143. (unsigned long)dst + size);
  144. ret = run_descriptor_jr(desc);
  145. }
  146. if (ret) {
  147. debug("Error in Encapsulation %d\n", ret);
  148. goto err;
  149. }
  150. size = roundup(out_sz, ARCH_DMA_MINALIGN);
  151. invalidate_dcache_range((unsigned long)dst, (unsigned long)dst+size);
  152. puts("DEK Blob\n");
  153. for (i = 0; i < out_sz; i++)
  154. printf("%02X", ((uint8_t *)dst)[i]);
  155. printf("\n");
  156. err:
  157. free(desc);
  158. return ret;
  159. }
  160. #endif