fsl_blob.c 5.3 KB

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