sha3.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_SHA3_H
  10. #define GMSSL_SHA3_H
  11. #include <string.h>
  12. #include <stdint.h>
  13. #include <sys/types.h>
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #define SHA3_KECCAK_P_SIZE (1600/8)
  18. #define SHA3_224_DIGEST_SIZE (224/8)
  19. #define SHA3_256_DIGEST_SIZE (256/8)
  20. #define SHA3_384_DIGEST_SIZE (384/8)
  21. #define SHA3_512_DIGEST_SIZE (512/8)
  22. #define SHA3_224_CAPACITY (SHA3_224_DIGEST_SIZE * 2)
  23. #define SHA3_256_CAPACITY (SHA3_256_DIGEST_SIZE * 2)
  24. #define SHA3_384_CAPACITY (SHA3_384_DIGEST_SIZE * 2)
  25. #define SHA3_512_CAPACITY (SHA3_512_DIGEST_SIZE * 2)
  26. #define SHA3_224_BLOCK_SIZE (SHA3_KECCAK_P_SIZE - SHA3_224_CAPACITY) // 144
  27. #define SHA3_256_BLOCK_SIZE (SHA3_KECCAK_P_SIZE - SHA3_224_CAPACITY) // 136
  28. #define SHA3_384_BLOCK_SIZE (SHA3_KECCAK_P_SIZE - SHA3_224_CAPACITY) // 104
  29. #define SHA3_512_BLOCK_SIZE (SHA3_KECCAK_P_SIZE - SHA3_224_CAPACITY) // 72
  30. typedef struct {
  31. uint64_t A[5][5];
  32. uint8_t buf[SHA3_224_BLOCK_SIZE];
  33. int num;
  34. } SHA3_224_CTX;
  35. void sha3_224_init(SHA3_224_CTX *ctx);
  36. void sha3_224_update(SHA3_224_CTX *ctx, const uint8_t *data, size_t datalen);
  37. void sha3_224_finish(SHA3_224_CTX *ctx, uint8_t dgst[SHA3_224_DIGEST_SIZE]);
  38. typedef struct {
  39. uint64_t A[5][5];
  40. uint8_t buf[SHA3_256_BLOCK_SIZE];
  41. int num;
  42. } SHA3_256_CTX;
  43. void sha3_256_init(SHA3_256_CTX *ctx);
  44. void sha3_256_update(SHA3_256_CTX *ctx, const uint8_t *data, size_t datalen);
  45. void sha3_256_finish(SHA3_256_CTX *ctx, uint8_t dgst[SHA3_256_DIGEST_SIZE]);
  46. typedef struct {
  47. uint64_t A[5][5];
  48. uint8_t buf[SHA3_384_BLOCK_SIZE];
  49. int num;
  50. } SHA3_384_CTX;
  51. void sha3_384_init(SHA3_384_CTX *ctx);
  52. void sha3_384_update(SHA3_384_CTX *ctx, const uint8_t *data, size_t datalen);
  53. void sha3_384_finish(SHA3_384_CTX *ctx, uint8_t dgst[SHA3_384_DIGEST_SIZE]);
  54. typedef struct {
  55. uint64_t A[5][5];
  56. uint8_t buf[SHA3_512_BLOCK_SIZE];
  57. int num;
  58. } SHA3_512_CTX;
  59. void sha3_512_init(SHA3_512_CTX *ctx);
  60. void sha3_512_update(SHA3_512_CTX *ctx, const uint8_t *data, size_t datalen);
  61. void sha3_512_finish(SHA3_512_CTX *ctx, uint8_t dgst[SHA3_512_DIGEST_SIZE]);
  62. void sha3_shake128(const uint8_t *in, size_t *inlen, size_t outlen, uint8_t *out);
  63. void sha3_shake256(const uint8_t *in, size_t *inlen, size_t outlen, uint8_t *out);
  64. void sha3_keccak_p(uint8_t state[SHA3_KECCAK_P_SIZE]);
  65. #ifdef __cplusplus
  66. }
  67. #endif
  68. #endif