sha256.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Copyright 2018 Canaan Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #ifndef _SHA256_H
  16. #define _SHA256_H
  17. #include <stddef.h>
  18. #include <stdint.h>
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. #define ENABLE_SHA (0x1)
  23. #define SHA256_BIG_ENDIAN (0x1)
  24. #define SHA256_HASH_LEN 32
  25. #define SHA256_HASH_WORDS 8
  26. #define SHA256_BLOCK_LEN 64L
  27. typedef struct _sha_num_reg
  28. {
  29. /* The total amount of data calculated by SHA256 is set by this register, and the smallest unit is 512bit. */
  30. uint32_t sha_data_cnt : 16;
  31. /* currently calculated block number. 512bit=1block*/
  32. uint32_t sha_data_num : 16;
  33. } __attribute__((packed, aligned(4))) sha_num_reg_t;
  34. typedef struct _sha_function_reg_0
  35. {
  36. /* write:SHA256 enable register. read:Calculation completed flag */
  37. uint32_t sha_en : 1;
  38. uint32_t reserved00 : 7;
  39. /* SHA256 calculation overflow flag */
  40. uint32_t sha_overflow : 1;
  41. uint32_t reserved01 : 7;
  42. /* Endian setting; b'0:little endian b'1:big endian */
  43. uint32_t sha_endian : 1;
  44. uint32_t reserved02 : 15;
  45. } __attribute__((packed, aligned(4))) sha_function_reg_0_t;
  46. typedef struct _sha_function_reg_1
  47. {
  48. /* Sha and DMA handshake signals enable.b'1:enable;b'0:disable */
  49. uint32_t dma_en : 1;
  50. uint32_t reserved10 : 7;
  51. /* b'1:sha256 fifo is full; b'0:not full */
  52. uint32_t fifo_in_full : 1;
  53. uint32_t reserved11 : 23;
  54. } __attribute__((packed, aligned(4))) sha_function_reg_1_t;
  55. typedef struct _sha256
  56. {
  57. /* Calculated sha256 return value. */
  58. uint32_t sha_result[8];
  59. /* SHA256 input data from this register. */
  60. uint32_t sha_data_in1;
  61. uint32_t reselved0;
  62. sha_num_reg_t sha_num_reg;
  63. sha_function_reg_0_t sha_function_reg_0;
  64. uint32_t reserved1;
  65. sha_function_reg_1_t sha_function_reg_1;
  66. } __attribute__((packed, aligned(4))) sha256_t;
  67. typedef struct _sha256_context
  68. {
  69. size_t total_len;
  70. size_t buffer_len;
  71. union
  72. {
  73. uint32_t words[16];
  74. uint8_t bytes[64];
  75. } buffer;
  76. } sha256_context_t;
  77. /**
  78. * @brief Init SHA256 calculation context
  79. *
  80. * @param[in] context SHA256 context object
  81. *
  82. */
  83. void sha256_init(sha256_context_t *context, size_t input_len);
  84. /**
  85. * @brief Called repeatedly with chunks of the message to be hashed
  86. *
  87. * @param[in] context SHA256 context object
  88. * @param[in] data_buf data chunk to be hashed
  89. * @param[in] buf_len length of data chunk
  90. *
  91. */
  92. void sha256_update(sha256_context_t *context, const void *input, size_t input_len);
  93. /**
  94. * @brief Finish SHA256 hash process, output the result.
  95. *
  96. * @param[in] context SHA256 context object
  97. * @param[out] output The buffer where SHA256 hash will be output
  98. *
  99. */
  100. void sha256_final(sha256_context_t *context, uint8_t *output);
  101. /**
  102. * @brief Simple SHA256 hash once.
  103. *
  104. * @param[in] data Data will be hashed
  105. * @param[in] data_len Data length
  106. * @param[out] output Output buffer
  107. *
  108. */
  109. void sha256_hard_calculate(const uint8_t *input, size_t input_len, uint8_t *output);
  110. #ifdef __cplusplus
  111. }
  112. #endif
  113. #endif