digest.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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_DIGEST_H
  10. #define GMSSL_DIGEST_H
  11. #include <stdint.h>
  12. #include <stdlib.h>
  13. #include <gmssl/sm3.h>
  14. #ifdef ENABLE_BROKEN_CRYPTO
  15. #include <gmssl/md5.h>
  16. #include <gmssl/sha1.h>
  17. #endif
  18. #include <gmssl/sha2.h>
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. typedef struct DIGEST DIGEST;
  23. typedef struct DIGEST_CTX DIGEST_CTX;
  24. #define DIGEST_MAX_SIZE 64
  25. #define DIGEST_MAX_BLOCK_SIZE (1024/8)
  26. struct DIGEST_CTX {
  27. union {
  28. SM3_CTX sm3_ctx;
  29. #ifdef ENABLE_BROKEN_CRYPTO
  30. MD5_CTX md5_ctx;
  31. SHA1_CTX sha1_ctx;
  32. #endif
  33. SHA224_CTX sha224_ctx;
  34. SHA256_CTX sha256_ctx;
  35. SHA384_CTX sha384_ctx;
  36. SHA512_CTX sha512_ctx;
  37. } u;
  38. const DIGEST *digest;
  39. };
  40. struct DIGEST {
  41. int oid;
  42. size_t digest_size;
  43. size_t block_size;
  44. size_t ctx_size;
  45. int (*init)(DIGEST_CTX *ctx);
  46. int (*update)(DIGEST_CTX *ctx, const uint8_t *data, size_t datalen);
  47. int (*finish)(DIGEST_CTX *ctx, uint8_t *dgst);
  48. };
  49. const DIGEST *DIGEST_sm3(void);
  50. #ifdef ENABLE_BROKEN_CRYPTO
  51. const DIGEST *DIGEST_md5(void);
  52. const DIGEST *DIGEST_sha1(void);
  53. #endif
  54. const DIGEST *DIGEST_sha224(void);
  55. const DIGEST *DIGEST_sha256(void);
  56. const DIGEST *DIGEST_sha384(void);
  57. const DIGEST *DIGEST_sha512(void);
  58. const DIGEST *DIGEST_sha512_224(void);
  59. const DIGEST *DIGEST_sha512_256(void);
  60. const DIGEST *digest_from_name(const char *name);
  61. const char *digest_name(const DIGEST *digest);
  62. int digest_init(DIGEST_CTX *ctx, const DIGEST *algor);
  63. int digest_update(DIGEST_CTX *ctx, const uint8_t *data, size_t datalen);
  64. int digest_finish(DIGEST_CTX *ctx, uint8_t *dgst, size_t *dgstlen);
  65. int digest(const DIGEST *digest, const uint8_t *data, size_t datalen, uint8_t *dgst, size_t *dgstlen);
  66. #ifdef __cplusplus
  67. }
  68. #endif
  69. #endif