sha256.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * @file sha256.h
  3. * @brief SHA-256 (Secure Hash Algorithm 256)
  4. *
  5. * @section License
  6. *
  7. * SPDX-License-Identifier: GPL-2.0-or-later
  8. *
  9. * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
  10. *
  11. * This file is part of CycloneCRYPTO Open.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version 2
  16. * of the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software Foundation,
  25. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  26. *
  27. * @author Oryx Embedded SARL (www.oryx-embedded.com)
  28. * @version 2.4.2
  29. **/
  30. #ifndef _SHA256_H
  31. #define _SHA256_H
  32. //Application specific context
  33. #ifndef SHA256_PRIVATE_CONTEXT
  34. #define SHA256_PRIVATE_CONTEXT
  35. #endif
  36. //SHA-256 block size
  37. #define SHA256_BLOCK_SIZE 64
  38. //SHA-256 digest size
  39. #define SHA256_DIGEST_SIZE 32
  40. //Minimum length of the padding string
  41. #define SHA256_MIN_PAD_SIZE 9
  42. //C++ guard
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @brief SHA-256 algorithm context
  48. **/
  49. typedef struct
  50. {
  51. union
  52. {
  53. uint32_t h[8];
  54. uint8_t digest[32];
  55. };
  56. union
  57. {
  58. uint32_t w[16];
  59. uint8_t buffer[64];
  60. };
  61. size_t size;
  62. uint64_t totalSize;
  63. SHA256_PRIVATE_CONTEXT
  64. } Sha256Context;
  65. //SHA-256 related constants
  66. extern const uint8_t SHA256_OID[9];
  67. //SHA-256 related functions
  68. int sha256Compute(const void *data, size_t length, uint8_t *digest);
  69. void sha256Init(Sha256Context *context);
  70. void sha256Update(Sha256Context *context, const void *data, size_t length);
  71. void sha256Final(Sha256Context *context, uint8_t *digest);
  72. void sha256FinalRaw(Sha256Context *context, uint8_t *digest);
  73. void sha256ProcessBlock(Sha256Context *context);
  74. //C++ guard
  75. #ifdef __cplusplus
  76. }
  77. #endif
  78. #endif