sha2.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #ifndef GMSSL_SHA2_H
  10. #define GMSSL_SHA2_H
  11. #include <string.h>
  12. #include <stdint.h>
  13. #include <sys/types.h>
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #define SHA2_IS_BIG_ENDIAN 1
  18. #define SHA224_DIGEST_SIZE 28
  19. #define SHA224_BLOCK_SIZE 64
  20. #define SHA224_STATE_WORDS 8
  21. typedef struct {
  22. uint32_t state[SHA224_STATE_WORDS];
  23. uint64_t nblocks;
  24. uint8_t block[SHA224_BLOCK_SIZE];
  25. size_t num;
  26. } SHA224_CTX;
  27. void sha224_init(SHA224_CTX *ctx);
  28. void sha224_update(SHA224_CTX *ctx, const uint8_t* data, size_t datalen);
  29. void sha224_finish(SHA224_CTX *ctx, uint8_t dgst[SHA224_DIGEST_SIZE]);
  30. void sha224_digest(const uint8_t *data, size_t datalen,
  31. uint8_t dgst[SHA224_DIGEST_SIZE]);
  32. #define SHA256_DIGEST_SIZE 32
  33. #define SHA256_BLOCK_SIZE 64
  34. #define SHA256_STATE_WORDS 8
  35. typedef struct {
  36. uint32_t state[SHA256_STATE_WORDS];
  37. uint64_t nblocks;
  38. uint8_t block[SHA256_BLOCK_SIZE];
  39. size_t num;
  40. } SHA256_CTX;
  41. void sha256_init(SHA256_CTX *ctx);
  42. void sha256_update(SHA256_CTX *ctx, const uint8_t* data, size_t datalen);
  43. void sha256_finish(SHA256_CTX *ctx, uint8_t dgst[SHA256_DIGEST_SIZE]);
  44. void sha256_digest(const uint8_t *data, size_t datalen,
  45. uint8_t dgst[SHA256_DIGEST_SIZE]);
  46. #define SHA384_DIGEST_SIZE 48
  47. #define SHA384_BLOCK_SIZE 128
  48. #define SHA384_STATE_WORDS 8
  49. typedef struct {
  50. uint64_t state[SHA384_STATE_WORDS];
  51. uint64_t nblocks;
  52. uint8_t block[SHA384_BLOCK_SIZE];
  53. size_t num;
  54. } SHA384_CTX;
  55. void sha384_init(SHA384_CTX *ctx);
  56. void sha384_update(SHA384_CTX *ctx, const uint8_t* data, size_t datalen);
  57. void sha384_finish(SHA384_CTX *ctx, uint8_t dgst[SHA384_DIGEST_SIZE]);
  58. void sha384_digest(const uint8_t *data, size_t datalen,
  59. uint8_t dgst[SHA384_DIGEST_SIZE]);
  60. #define SHA512_DIGEST_SIZE 64
  61. #define SHA512_BLOCK_SIZE 128
  62. #define SHA512_STATE_WORDS 8
  63. typedef struct {
  64. uint64_t state[SHA512_STATE_WORDS];
  65. uint64_t nblocks;
  66. uint8_t block[SHA512_BLOCK_SIZE];
  67. size_t num;
  68. } SHA512_CTX;
  69. void sha512_init(SHA512_CTX *ctx);
  70. void sha512_update(SHA512_CTX *ctx, const uint8_t* data, size_t datalen);
  71. void sha512_finish(SHA512_CTX *ctx, uint8_t dgst[SHA512_DIGEST_SIZE]);
  72. void sha512_digest(const uint8_t *data, size_t datalen,
  73. uint8_t dgst[SHA512_DIGEST_SIZE]);
  74. #ifdef __cplusplus
  75. }
  76. #endif
  77. #endif