sm3.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_SM3_H
  10. #define GMSSL_SM3_H
  11. #include <string.h>
  12. #include <stdint.h>
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. /*
  17. SM3 Public API
  18. SM3_DIGEST_SIZE
  19. SM3_HMAC_SIZE
  20. SM3_CTX
  21. sm3_init
  22. sm3_update
  23. sm3_finish
  24. SM3_HMAC_CTX
  25. sm3_hmac_init
  26. sm3_hmac_update
  27. sm3_hmac_finish
  28. sm3_digest
  29. sm3_hmac
  30. */
  31. #define SM3_IS_BIG_ENDIAN 1
  32. #define SM3_DIGEST_SIZE 32
  33. #define SM3_BLOCK_SIZE 64
  34. #define SM3_STATE_WORDS 8
  35. #define SM3_HMAC_SIZE (SM3_DIGEST_SIZE)
  36. typedef struct {
  37. uint32_t digest[SM3_STATE_WORDS];
  38. uint64_t nblocks;
  39. uint8_t block[SM3_BLOCK_SIZE];
  40. size_t num;
  41. } SM3_CTX;
  42. void sm3_init(SM3_CTX *ctx);
  43. void sm3_update(SM3_CTX *ctx, const uint8_t *data, size_t datalen);
  44. void sm3_finish(SM3_CTX *ctx, uint8_t dgst[SM3_DIGEST_SIZE]);
  45. void sm3_digest(const uint8_t *data, size_t datalen, uint8_t dgst[SM3_DIGEST_SIZE]);
  46. void sm3_compress_blocks(uint32_t digest[8], const uint8_t *data, size_t blocks);
  47. typedef struct {
  48. SM3_CTX sm3_ctx;
  49. unsigned char key[SM3_BLOCK_SIZE];
  50. } SM3_HMAC_CTX;
  51. void sm3_hmac_init(SM3_HMAC_CTX *ctx, const uint8_t *key, size_t keylen);
  52. void sm3_hmac_update(SM3_HMAC_CTX *ctx, const uint8_t *data, size_t datalen);
  53. void sm3_hmac_finish(SM3_HMAC_CTX *ctx, uint8_t mac[SM3_HMAC_SIZE]);
  54. void sm3_hmac(const uint8_t *key, size_t keylen,
  55. const uint8_t *data, size_t datalen,
  56. uint8_t mac[SM3_HMAC_SIZE]);
  57. typedef struct {
  58. SM3_CTX sm3_ctx;
  59. size_t outlen;
  60. } SM3_KDF_CTX;
  61. void sm3_kdf_init(SM3_KDF_CTX *ctx, size_t outlen);
  62. void sm3_kdf_update(SM3_KDF_CTX *ctx, const uint8_t *data, size_t datalen);
  63. void sm3_kdf_finish(SM3_KDF_CTX *ctx, uint8_t *out);
  64. #ifdef __cplusplus
  65. }
  66. #endif
  67. #endif