fsl_hash.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 "jobdesc.h"
  10. #include "desc.h"
  11. #include "jr.h"
  12. #define CRYPTO_MAX_ALG_NAME 80
  13. #define SHA1_DIGEST_SIZE 20
  14. #define SHA256_DIGEST_SIZE 32
  15. struct caam_hash_template {
  16. char name[CRYPTO_MAX_ALG_NAME];
  17. unsigned int digestsize;
  18. u32 alg_type;
  19. };
  20. enum caam_hash_algos {
  21. SHA1 = 0,
  22. SHA256
  23. };
  24. static struct caam_hash_template driver_hash[] = {
  25. {
  26. .name = "sha1",
  27. .digestsize = SHA1_DIGEST_SIZE,
  28. .alg_type = OP_ALG_ALGSEL_SHA1,
  29. },
  30. {
  31. .name = "sha256",
  32. .digestsize = SHA256_DIGEST_SIZE,
  33. .alg_type = OP_ALG_ALGSEL_SHA256,
  34. },
  35. };
  36. int caam_hash(const unsigned char *pbuf, unsigned int buf_len,
  37. unsigned char *pout, enum caam_hash_algos algo)
  38. {
  39. int ret = 0;
  40. uint32_t *desc;
  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_hash(desc, pbuf, buf_len, pout,
  47. driver_hash[algo].alg_type,
  48. driver_hash[algo].digestsize,
  49. 0);
  50. ret = run_descriptor_jr(desc);
  51. free(desc);
  52. return ret;
  53. }
  54. void hw_sha256(const unsigned char *pbuf, unsigned int buf_len,
  55. unsigned char *pout, unsigned int chunk_size)
  56. {
  57. if (caam_hash(pbuf, buf_len, pout, SHA256))
  58. printf("CAAM was not setup properly or it is faulty\n");
  59. }
  60. void hw_sha1(const unsigned char *pbuf, unsigned int buf_len,
  61. unsigned char *pout, unsigned int chunk_size)
  62. {
  63. if (caam_hash(pbuf, buf_len, pout, SHA1))
  64. printf("CAAM was not setup properly or it is faulty\n");
  65. }