sm3.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * \file sm3.h
  3. * thanks to Xyssl
  4. * SM3 standards:http://www.oscca.gov.cn/News/201012/News_1199.htm
  5. * author:goldboar
  6. * email:goldboar@163.com
  7. * 2011-10-26
  8. */
  9. #ifndef XYSSL_SM3_H
  10. #define XYSSL_SM3_H
  11. //#define _DEBUG
  12. /**
  13. * \brief SM3 context structure
  14. */
  15. typedef struct
  16. {
  17. unsigned long total[2]; /*!< number of bytes processed */
  18. unsigned long state[8]; /*!< intermediate digest state */
  19. unsigned char buffer[64]; /*!< data block being processed */
  20. unsigned char ipad[64]; /*!< HMAC: inner padding */
  21. unsigned char opad[64]; /*!< HMAC: outer padding */
  22. }
  23. sm3_context;
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /**
  28. * \brief SM3 context setup
  29. *
  30. * \param ctx context to be initialized
  31. */
  32. void sm3_starts( sm3_context *ctx );
  33. /**
  34. * \brief SM3 process buffer
  35. *
  36. * \param ctx SM3 context
  37. * \param input buffer holding the data
  38. * \param ilen length of the input data
  39. */
  40. void sm3_update( sm3_context *ctx, unsigned char *input, int ilen );
  41. /**
  42. * \brief SM3 final digest
  43. *
  44. * \param ctx SM3 context
  45. */
  46. void sm3_finish( sm3_context *ctx, unsigned char output[32] );
  47. /**
  48. * \brief Output = SM3( input buffer )
  49. *
  50. * \param input buffer holding the data
  51. * \param ilen length of the input data
  52. * \param output SM3 checksum result
  53. */
  54. void sm3( unsigned char *input, int ilen,
  55. unsigned char output[32]);
  56. /**
  57. * \brief Output = SM3( file contents )
  58. *
  59. * \param path input file name
  60. * \param output SM3 checksum result
  61. *
  62. * \return 0 if successful, 1 if fopen failed,
  63. * or 2 if fread failed
  64. */
  65. int sm3_file( char *path, unsigned char output[32] );
  66. /**
  67. * \brief SM3 HMAC context setup
  68. *
  69. * \param ctx HMAC context to be initialized
  70. * \param key HMAC secret key
  71. * \param keylen length of the HMAC key
  72. */
  73. void sm3_hmac_starts( sm3_context *ctx, unsigned char *key, int keylen);
  74. /**
  75. * \brief SM3 HMAC process buffer
  76. *
  77. * \param ctx HMAC context
  78. * \param input buffer holding the data
  79. * \param ilen length of the input data
  80. */
  81. void sm3_hmac_update( sm3_context *ctx, unsigned char *input, int ilen );
  82. /**
  83. * \brief SM3 HMAC final digest
  84. *
  85. * \param ctx HMAC context
  86. * \param output SM3 HMAC checksum result
  87. */
  88. void sm3_hmac_finish( sm3_context *ctx, unsigned char output[32] );
  89. /**
  90. * \brief Output = HMAC-SM3( hmac key, input buffer )
  91. *
  92. * \param key HMAC secret key
  93. * \param keylen length of the HMAC key
  94. * \param input buffer holding the data
  95. * \param ilen length of the input data
  96. * \param output HMAC-SM3 result
  97. */
  98. void sm3_hmac( unsigned char *key, int keylen,
  99. unsigned char *input, int ilen,
  100. unsigned char output[32] );
  101. #ifdef __cplusplus
  102. }
  103. #endif
  104. #endif /* sm3.h */