sha256.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * \file sha256.h
  3. *
  4. * \brief This file contains SHA-224 and SHA-256 definitions and functions.
  5. *
  6. * The Secure Hash Algorithms 224 and 256 (SHA-224 and SHA-256) cryptographic
  7. * hash functions are defined in <em>FIPS 180-4: Secure Hash Standard (SHS)</em>.
  8. */
  9. /*
  10. * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
  11. * SPDX-License-Identifier: Apache-2.0
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * This file is part of Mbed TLS (https://tls.mbed.org)
  26. */
  27. #ifndef MBEDTLS_SHA256_H
  28. #define MBEDTLS_SHA256_H
  29. #include <stddef.h>
  30. #include <stdint.h>
  31. /* MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED is deprecated and should not be used. */
  32. #define MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED -0x0037 /**< SHA-256 hardware accelerator failed */
  33. #define MBEDTLS_ERR_SHA256_BAD_INPUT_DATA -0x0074 /**< SHA-256 input data was malformed. */
  34. #define mbedtls_printf(...) printf(__VA_ARGS__)
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /**
  39. * \brief The SHA-256 context structure.
  40. *
  41. * The structure is used both for SHA-256 and for SHA-224
  42. * checksum calculations. The choice between these two is
  43. * made in the call to mbedtls_sha256_starts_ret().
  44. */
  45. typedef struct mbedtls_sha256_context
  46. {
  47. uint32_t total[2]; /*!< The number of Bytes processed. */
  48. uint32_t state[8]; /*!< The intermediate digest state. */
  49. unsigned char buffer[64]; /*!< The data block being processed. */
  50. int is224; /*!< Determines which function to use:
  51. 0: Use SHA-256, or 1: Use SHA-224. */
  52. }
  53. mbedtls_sha256_context;
  54. /**
  55. * \brief This function initializes a SHA-256 context.
  56. *
  57. * \param ctx The SHA-256 context to initialize. This must not be \c NULL.
  58. */
  59. void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
  60. /**
  61. * \brief This function clears a SHA-256 context.
  62. *
  63. * \param ctx The SHA-256 context to clear. This may be \c NULL, in which
  64. * case this function returns immediately. If it is not \c NULL,
  65. * it must point to an initialized SHA-256 context.
  66. */
  67. void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
  68. /**
  69. * \brief This function clones the state of a SHA-256 context.
  70. *
  71. * \param dst The destination context. This must be initialized.
  72. * \param src The context to clone. This must be initialized.
  73. */
  74. void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
  75. const mbedtls_sha256_context *src );
  76. /**
  77. * \brief This function starts a SHA-224 or SHA-256 checksum
  78. * calculation.
  79. *
  80. * \param ctx The context to use. This must be initialized.
  81. * \param is224 This determines which function to use. This must be
  82. * either \c 0 for SHA-256, or \c 1 for SHA-224.
  83. *
  84. * \return \c 0 on success.
  85. * \return A negative error code on failure.
  86. */
  87. int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 );
  88. /**
  89. * \brief This function feeds an input buffer into an ongoing
  90. * SHA-256 checksum calculation.
  91. *
  92. * \param ctx The SHA-256 context. This must be initialized
  93. * and have a hash operation started.
  94. * \param input The buffer holding the data. This must be a readable
  95. * buffer of length \p ilen Bytes.
  96. * \param ilen The length of the input data in Bytes.
  97. *
  98. * \return \c 0 on success.
  99. * \return A negative error code on failure.
  100. */
  101. int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
  102. const unsigned char *input,
  103. size_t ilen );
  104. /**
  105. * \brief This function finishes the SHA-256 operation, and writes
  106. * the result to the output buffer.
  107. *
  108. * \param ctx The SHA-256 context. This must be initialized
  109. * and have a hash operation started.
  110. * \param output The SHA-224 or SHA-256 checksum result.
  111. * This must be a writable buffer of length \c 32 Bytes.
  112. *
  113. * \return \c 0 on success.
  114. * \return A negative error code on failure.
  115. */
  116. int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
  117. unsigned char output[32] );
  118. /**
  119. * \brief This function processes a single data block within
  120. * the ongoing SHA-256 computation. This function is for
  121. * internal use only.
  122. *
  123. * \param ctx The SHA-256 context. This must be initialized.
  124. * \param data The buffer holding one block of data. This must
  125. * be a readable buffer of length \c 64 Bytes.
  126. *
  127. * \return \c 0 on success.
  128. * \return A negative error code on failure.
  129. */
  130. int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
  131. const unsigned char data[64] );
  132. #ifdef __cplusplus
  133. }
  134. #endif
  135. #endif /* mbedtls_sha256.h */