fsl_hash.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 <memalign.h>
  10. #include "jobdesc.h"
  11. #include "desc.h"
  12. #include "jr.h"
  13. #include "fsl_hash.h"
  14. #include <hw_sha.h>
  15. #include <linux/errno.h>
  16. #define CRYPTO_MAX_ALG_NAME 80
  17. #define SHA1_DIGEST_SIZE 20
  18. #define SHA256_DIGEST_SIZE 32
  19. struct caam_hash_template {
  20. char name[CRYPTO_MAX_ALG_NAME];
  21. unsigned int digestsize;
  22. u32 alg_type;
  23. };
  24. enum caam_hash_algos {
  25. SHA1 = 0,
  26. SHA256
  27. };
  28. static struct caam_hash_template driver_hash[] = {
  29. {
  30. .name = "sha1",
  31. .digestsize = SHA1_DIGEST_SIZE,
  32. .alg_type = OP_ALG_ALGSEL_SHA1,
  33. },
  34. {
  35. .name = "sha256",
  36. .digestsize = SHA256_DIGEST_SIZE,
  37. .alg_type = OP_ALG_ALGSEL_SHA256,
  38. },
  39. };
  40. static enum caam_hash_algos get_hash_type(struct hash_algo *algo)
  41. {
  42. if (!strcmp(algo->name, driver_hash[SHA1].name))
  43. return SHA1;
  44. else
  45. return SHA256;
  46. }
  47. /* Create the context for progressive hashing using h/w acceleration.
  48. *
  49. * @ctxp: Pointer to the pointer of the context for hashing
  50. * @caam_algo: Enum for SHA1 or SHA256
  51. * @return 0 if ok, -ENOMEM on error
  52. */
  53. static int caam_hash_init(void **ctxp, enum caam_hash_algos caam_algo)
  54. {
  55. *ctxp = calloc(1, sizeof(struct sha_ctx));
  56. if (*ctxp == NULL) {
  57. debug("Cannot allocate memory for context\n");
  58. return -ENOMEM;
  59. }
  60. return 0;
  61. }
  62. /*
  63. * Update sg table for progressive hashing using h/w acceleration
  64. *
  65. * The context is freed by this function if an error occurs.
  66. * We support at most 32 Scatter/Gather Entries.
  67. *
  68. * @hash_ctx: Pointer to the context for hashing
  69. * @buf: Pointer to the buffer being hashed
  70. * @size: Size of the buffer being hashed
  71. * @is_last: 1 if this is the last update; 0 otherwise
  72. * @caam_algo: Enum for SHA1 or SHA256
  73. * @return 0 if ok, -EINVAL on error
  74. */
  75. static int caam_hash_update(void *hash_ctx, const void *buf,
  76. unsigned int size, int is_last,
  77. enum caam_hash_algos caam_algo)
  78. {
  79. uint32_t final = 0;
  80. phys_addr_t addr = virt_to_phys((void *)buf);
  81. struct sha_ctx *ctx = hash_ctx;
  82. if (ctx->sg_num >= MAX_SG_32) {
  83. free(ctx);
  84. return -EINVAL;
  85. }
  86. #ifdef CONFIG_PHYS_64BIT
  87. sec_out32(&ctx->sg_tbl[ctx->sg_num].addr_hi, (uint32_t)(addr >> 32));
  88. #else
  89. sec_out32(&ctx->sg_tbl[ctx->sg_num].addr_hi, 0x0);
  90. #endif
  91. sec_out32(&ctx->sg_tbl[ctx->sg_num].addr_lo, (uint32_t)addr);
  92. sec_out32(&ctx->sg_tbl[ctx->sg_num].len_flag,
  93. (size & SG_ENTRY_LENGTH_MASK));
  94. ctx->sg_num++;
  95. if (is_last) {
  96. final = sec_in32(&ctx->sg_tbl[ctx->sg_num - 1].len_flag) |
  97. SG_ENTRY_FINAL_BIT;
  98. sec_out32(&ctx->sg_tbl[ctx->sg_num - 1].len_flag, final);
  99. }
  100. return 0;
  101. }
  102. /*
  103. * Perform progressive hashing on the given buffer and copy hash at
  104. * destination buffer
  105. *
  106. * The context is freed after completion of hash operation.
  107. *
  108. * @hash_ctx: Pointer to the context for hashing
  109. * @dest_buf: Pointer to the destination buffer where hash is to be copied
  110. * @size: Size of the buffer being hashed
  111. * @caam_algo: Enum for SHA1 or SHA256
  112. * @return 0 if ok, -EINVAL on error
  113. */
  114. static int caam_hash_finish(void *hash_ctx, void *dest_buf,
  115. int size, enum caam_hash_algos caam_algo)
  116. {
  117. uint32_t len = 0;
  118. struct sha_ctx *ctx = hash_ctx;
  119. int i = 0, ret = 0;
  120. if (size < driver_hash[caam_algo].digestsize) {
  121. free(ctx);
  122. return -EINVAL;
  123. }
  124. for (i = 0; i < ctx->sg_num; i++)
  125. len += (sec_in32(&ctx->sg_tbl[i].len_flag) &
  126. SG_ENTRY_LENGTH_MASK);
  127. inline_cnstr_jobdesc_hash(ctx->sha_desc, (uint8_t *)ctx->sg_tbl, len,
  128. ctx->hash,
  129. driver_hash[caam_algo].alg_type,
  130. driver_hash[caam_algo].digestsize,
  131. 1);
  132. ret = run_descriptor_jr(ctx->sha_desc);
  133. if (ret)
  134. debug("Error %x\n", ret);
  135. else
  136. memcpy(dest_buf, ctx->hash, sizeof(ctx->hash));
  137. free(ctx);
  138. return ret;
  139. }
  140. int caam_hash(const unsigned char *pbuf, unsigned int buf_len,
  141. unsigned char *pout, enum caam_hash_algos algo)
  142. {
  143. int ret = 0;
  144. uint32_t *desc;
  145. unsigned int size;
  146. desc = malloc_cache_aligned(sizeof(int) * MAX_CAAM_DESCSIZE);
  147. if (!desc) {
  148. debug("Not enough memory for descriptor allocation\n");
  149. return -ENOMEM;
  150. }
  151. if (!IS_ALIGNED((uintptr_t)pbuf, ARCH_DMA_MINALIGN) ||
  152. !IS_ALIGNED((uintptr_t)pout, ARCH_DMA_MINALIGN)) {
  153. puts("Error: Address arguments are not aligned\n");
  154. return -EINVAL;
  155. }
  156. size = ALIGN(buf_len, ARCH_DMA_MINALIGN);
  157. flush_dcache_range((unsigned long)pbuf, (unsigned long)pbuf + size);
  158. inline_cnstr_jobdesc_hash(desc, pbuf, buf_len, pout,
  159. driver_hash[algo].alg_type,
  160. driver_hash[algo].digestsize,
  161. 0);
  162. size = ALIGN(sizeof(int) * MAX_CAAM_DESCSIZE, ARCH_DMA_MINALIGN);
  163. flush_dcache_range((unsigned long)desc, (unsigned long)desc + size);
  164. ret = run_descriptor_jr(desc);
  165. size = ALIGN(driver_hash[algo].digestsize, ARCH_DMA_MINALIGN);
  166. invalidate_dcache_range((unsigned long)pout,
  167. (unsigned long)pout + size);
  168. free(desc);
  169. return ret;
  170. }
  171. void hw_sha256(const unsigned char *pbuf, unsigned int buf_len,
  172. unsigned char *pout, unsigned int chunk_size)
  173. {
  174. if (caam_hash(pbuf, buf_len, pout, SHA256))
  175. printf("CAAM was not setup properly or it is faulty\n");
  176. }
  177. void hw_sha1(const unsigned char *pbuf, unsigned int buf_len,
  178. unsigned char *pout, unsigned int chunk_size)
  179. {
  180. if (caam_hash(pbuf, buf_len, pout, SHA1))
  181. printf("CAAM was not setup properly or it is faulty\n");
  182. }
  183. int hw_sha_init(struct hash_algo *algo, void **ctxp)
  184. {
  185. return caam_hash_init(ctxp, get_hash_type(algo));
  186. }
  187. int hw_sha_update(struct hash_algo *algo, void *ctx, const void *buf,
  188. unsigned int size, int is_last)
  189. {
  190. return caam_hash_update(ctx, buf, size, is_last, get_hash_type(algo));
  191. }
  192. int hw_sha_finish(struct hash_algo *algo, void *ctx, void *dest_buf,
  193. int size)
  194. {
  195. return caam_hash_finish(ctx, dest_buf, size, get_hash_type(algo));
  196. }