ocrypto_sha1.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * Copyright (c) 2019 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /**@file
  41. * @defgroup nrf_oberon_sha_1 SHA-1 APIs
  42. * @ingroup nrf_oberon
  43. * @{
  44. * @brief Type declarations and APIs for the SHA-1 algorithm.
  45. *
  46. * A fixed-sized message digest is computed from variable length input data.
  47. * The function is practically impossible to revert, and small changes in the
  48. * input message lead to major changes in the message digest.
  49. *
  50. * SHA-1 is no longer considered secure against well-funded opponents;
  51. * replacement by SHA-2 or SHA-3 is recommended.
  52. */
  53. #ifndef OCRYPTO_SHA1_H
  54. #define OCRYPTO_SHA1_H
  55. #include <stddef.h>
  56. #include <stdint.h>
  57. #ifdef __cplusplus
  58. extern "C" {
  59. #endif
  60. /**
  61. * Length of SHA-1 hash.
  62. */
  63. #define ocrypto_sha1_BYTES (20)
  64. /**@cond */
  65. typedef struct {
  66. uint32_t h[5];
  67. uint8_t padded[64];
  68. uint32_t length;
  69. size_t bytes;
  70. } ocrypto_sha1_ctx;
  71. /**@endcond */
  72. /**@name Incremental SHA-1 generator.
  73. *
  74. * This group of functions can be used to incrementally compute the SHA-1
  75. * hash for a given message.
  76. */
  77. /**@{*/
  78. /**
  79. * SHA-1 initialization.
  80. *
  81. * The generator state @p ctx is initialized by this function.
  82. *
  83. * @param[out] ctx Generator state.
  84. */
  85. void ocrypto_sha1_init(
  86. ocrypto_sha1_ctx *ctx);
  87. /**
  88. * SHA-1 incremental data input.
  89. *
  90. * The generator state @p ctx is updated to hash a message chunk @p in.
  91. *
  92. * This function can be called repeatedly until the whole message is processed.
  93. *
  94. * @param ctx Generator state.
  95. * @param in Input data.
  96. * @param in_len Length of @p in.
  97. *
  98. * @remark Initialization of the generator state @p ctx through
  99. * @c ocrypto_sha1_init is required before this function can be called.
  100. */
  101. void ocrypto_sha1_update(
  102. ocrypto_sha1_ctx *ctx,
  103. const uint8_t *in, size_t in_len);
  104. /**
  105. * SHA-1 output.
  106. *
  107. * The generator state @p ctx is updated to finalize the hash for the previously
  108. * processed message chunks. The hash is put into @p r.
  109. *
  110. * @param ctx Generator state.
  111. * @param[out] r Generated hash value.
  112. *
  113. * @remark Initialization of the generator state @p ctx through
  114. * @c ocrypto_sha1_init is required before this function can be called.
  115. *
  116. * @remark After return, the generator state @p ctx must no longer be used with
  117. * @c ocrypto_sha1_update and @c ocrypto_sha1_final unless it is
  118. * reinitialized using @c ocrypto_sha1_init.
  119. */
  120. void ocrypto_sha1_final(
  121. ocrypto_sha1_ctx *ctx,
  122. uint8_t r[ocrypto_sha1_BYTES]);
  123. /**@}*/
  124. /**
  125. * SHA-1 hash.
  126. *
  127. * The SHA-1 hash of a given input message @p in is computed and put into @p r.
  128. *
  129. * @param[out] r Generated hash.
  130. * @param in Input data.
  131. * @param in_len Length of @p in.
  132. */
  133. void ocrypto_sha1(
  134. uint8_t r[ocrypto_sha1_BYTES],
  135. const uint8_t *in, size_t in_len);
  136. #ifdef __cplusplus
  137. }
  138. #endif
  139. #endif /* #ifndef OCRYPTO_SHA1_H */
  140. /** @} */