occ_sha512.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * Copyright (c) 2016 - 2019, 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. * SHA-512 is part of the SHA-2 family that is a set of cryptographic hash
  42. * functions designed by the NSA. It is the successor of the SHA-1 algorithm.
  43. *
  44. * A fixed-sized message digest is computed from variable length input data.
  45. * The function is practically impossible to revert, and small changes in the
  46. * input message lead to major changes in the message digest.
  47. */
  48. #ifndef OCC_SHA512_H
  49. #define OCC_SHA512_H
  50. #include <stdint.h>
  51. #include <stddef.h>
  52. /**
  53. * Length of SHA-512 hash.
  54. */
  55. #define occ_sha512_BYTES (64)
  56. /**@cond */
  57. typedef struct {
  58. uint64_t h[8];
  59. uint8_t padded[128];
  60. uint32_t length;
  61. size_t bytes;
  62. } occ_sha512_ctx;
  63. /**@endcond */
  64. /**@name Incremental SHA-512 generator.
  65. *
  66. * This group of functions can be used to incrementally compute the SHA-512
  67. * hash for a given message.
  68. *
  69. * **Example**
  70. * @include occ_sha512_incremental.c
  71. */
  72. /**@{*/
  73. /**
  74. * SHA-512 initialization.
  75. *
  76. * The generator state @p ctx is initialized by this function.
  77. *
  78. * @param[out] ctx Generator state.
  79. */
  80. void occ_sha512_init(occ_sha512_ctx *ctx);
  81. /**
  82. * SHA-512 incremental data input.
  83. *
  84. * The generator state @p ctx is updated to hash a message chunk @p in.
  85. *
  86. * This function can be called repeatedly until the whole message is processed.
  87. *
  88. * @param[in,out] ctx Generator state.
  89. * @param in Input data.
  90. * @param in_len Length of @p in.
  91. *
  92. * @remark Initialization of the generator state @p ctx through
  93. * @c occ_sha512_init is required before this function can be called.
  94. */
  95. void occ_sha512_update(occ_sha512_ctx *ctx,
  96. const uint8_t *in, size_t in_len);
  97. /**
  98. * SHA-512 output.
  99. *
  100. * The generator state @p ctx is updated to finalize the hash for the previously
  101. * processed message chunks. The hash is put into @p r.
  102. *
  103. * @param[out] r Generated hash value.
  104. * @param[in,out] ctx Generator state.
  105. *
  106. * @remark Initialization of the generator state @p ctx through
  107. * @c occ_sha512_init is required before this function can be called.
  108. *
  109. * @remark After return, the generator state @p ctx must no longer be used with
  110. * @c occ_sha512_update and @c occ_sha512_final unless it is
  111. * reinitialized using @c occ_sha512_init.
  112. */
  113. void occ_sha512_final(uint8_t r[occ_sha512_BYTES], occ_sha512_ctx *ctx);
  114. /**@}*/
  115. /**
  116. * SHA-512 hash.
  117. *
  118. * The SHA-512 hash of a given input message @p in is computed and put into @p r.
  119. *
  120. * **Example**
  121. * @include occ_sha512.c
  122. *
  123. * @param[out] r Generated hash.
  124. * @param in Input data.
  125. * @param in_len Length of @p in.
  126. */
  127. void occ_sha512(uint8_t r[occ_sha512_BYTES],
  128. const uint8_t *in, size_t in_len);
  129. #endif