hw_hash.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-04-23 tyx the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <hw_hash.h>
  13. /**
  14. * @brief Creating hash Context
  15. *
  16. * @param device Hardware crypto device
  17. * @param type Type of hash context
  18. *
  19. * @return Hash context
  20. */
  21. struct rt_hwcrypto_ctx *rt_hwcrypto_hash_create(struct rt_hwcrypto_device *device, hwcrypto_type type)
  22. {
  23. struct rt_hwcrypto_ctx *ctx;
  24. ctx = rt_hwcrypto_ctx_create(device, type, sizeof(struct hwcrypto_hash));
  25. return ctx;
  26. }
  27. /**
  28. * @brief Destroy hash Context
  29. *
  30. * @param ctx Hash context
  31. */
  32. void rt_hwcrypto_hash_destroy(struct rt_hwcrypto_ctx *ctx)
  33. {
  34. rt_hwcrypto_ctx_destroy(ctx);
  35. }
  36. /**
  37. * @brief Get the final hash value
  38. *
  39. * @param ctx Hash context
  40. * @param output Hash value buffer
  41. * @param length Hash value buffer length
  42. *
  43. * @return RT_EOK on success.
  44. */
  45. rt_err_t rt_hwcrypto_hash_finish(struct rt_hwcrypto_ctx *ctx, rt_uint8_t *output, rt_size_t length)
  46. {
  47. if (ctx && ((struct hwcrypto_hash *)ctx)->ops->finish)
  48. {
  49. return ((struct hwcrypto_hash *)ctx)->ops->finish((struct hwcrypto_hash *)ctx, output, length);
  50. }
  51. return -RT_ERROR;
  52. }
  53. /**
  54. * @brief Processing a packet of data
  55. *
  56. * @param ctx Hash context
  57. * @param input Data buffer to be Processed
  58. * @param length Data Buffer length
  59. *
  60. * @return RT_EOK on success.
  61. */
  62. rt_err_t rt_hwcrypto_hash_update(struct rt_hwcrypto_ctx *ctx, const rt_uint8_t *input, rt_size_t length)
  63. {
  64. if (ctx && ((struct hwcrypto_hash *)ctx)->ops->update)
  65. {
  66. return ((struct hwcrypto_hash *)ctx)->ops->update((struct hwcrypto_hash *)ctx, input, length);
  67. }
  68. return -RT_ERROR;
  69. }
  70. /**
  71. * @brief This function copy hash context
  72. *
  73. * @param des The destination hash context
  74. * @param src The hash context to be copy
  75. *
  76. * @return RT_EOK on success.
  77. */
  78. rt_err_t rt_hwcrypto_hash_cpy(struct rt_hwcrypto_ctx *des, const struct rt_hwcrypto_ctx *src)
  79. {
  80. return rt_hwcrypto_ctx_cpy(des, src);
  81. }
  82. /**
  83. * @brief Reset hash context
  84. *
  85. * @param ctx Hash context
  86. */
  87. void rt_hwcrypto_hash_reset(struct rt_hwcrypto_ctx *ctx)
  88. {
  89. rt_hwcrypto_ctx_reset(ctx);
  90. }
  91. /**
  92. * @brief Setting hash context type
  93. *
  94. * @param ctx Hash context
  95. * @param type Types of settings
  96. *
  97. * @return RT_EOK on success.
  98. */
  99. rt_err_t rt_hwcrypto_hash_set_type(struct rt_hwcrypto_ctx *ctx, hwcrypto_type type)
  100. {
  101. return rt_hwcrypto_set_type(ctx, type);
  102. }