blake2.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. BLAKE2 reference source code package - optimized C implementations
  3. Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
  4. To the extent possible under law, the author(s) have dedicated all copyright
  5. and related and neighboring rights to this software to the public domain
  6. worldwide. This software is distributed without any warranty.
  7. You should have received a copy of the CC0 Public Domain Dedication along with
  8. this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
  9. */
  10. #pragma once
  11. #ifndef __BLAKE2_H__
  12. #define __BLAKE2_H__
  13. #include <stddef.h>
  14. #include <stdint.h>
  15. #if defined(_WIN32) || defined(__CYGWIN__)
  16. #define BLAKE2_DLL_IMPORT __declspec(dllimport)
  17. #define BLAKE2_DLL_EXPORT __declspec(dllexport)
  18. #define BLAKE2_DLL_PRIVATE
  19. #elif __GNUC__ >= 4
  20. #define BLAKE2_DLL_IMPORT __attribute__ ((visibility ("default")))
  21. #define BLAKE2_DLL_EXPORT __attribute__ ((visibility ("default")))
  22. #define BLAKE2_DLL_PRIVATE __attribute__ ((visibility ("hidden")))
  23. #else
  24. #define BLAKE2_DLL_IMPORT
  25. #define BLAKE2_DLL_EXPORT
  26. #define BLAKE2_DLL_PRIVATE
  27. #endif
  28. #if defined(BLAKE2_DLL)
  29. #if defined(BLAKE2_DLL_EXPORTS) // defined if we are building the DLL
  30. #define BLAKE2_API BLAKE2_DLL_EXPORT
  31. #else
  32. #define BLAKE2_API BLAKE2_DLL_IMPORT
  33. #endif
  34. #define BLAKE2_PRIVATE BLAKE2_DLL_PRIVATE // must only be used by hidden logic
  35. #else
  36. #define BLAKE2_API
  37. #define BLAKE2_PRIVATE
  38. #endif
  39. #if defined(__cplusplus)
  40. extern "C" {
  41. #elif defined(_MSC_VER) && !defined(inline)
  42. #define inline __inline
  43. #endif
  44. enum blake2s_constant
  45. {
  46. BLAKE2S_BLOCKBYTES = 64,
  47. BLAKE2S_OUTBYTES = 32,
  48. BLAKE2S_KEYBYTES = 32,
  49. BLAKE2S_SALTBYTES = 8,
  50. BLAKE2S_PERSONALBYTES = 8
  51. };
  52. enum blake2b_constant
  53. {
  54. BLAKE2B_BLOCKBYTES = 128,
  55. BLAKE2B_OUTBYTES = 64,
  56. BLAKE2B_KEYBYTES = 64,
  57. BLAKE2B_SALTBYTES = 16,
  58. BLAKE2B_PERSONALBYTES = 16
  59. };
  60. #pragma pack(push, 1)
  61. typedef struct __blake2s_param
  62. {
  63. uint8_t digest_length; // 1
  64. uint8_t key_length; // 2
  65. uint8_t fanout; // 3
  66. uint8_t depth; // 4
  67. uint32_t leaf_length; // 8
  68. uint8_t node_offset[6];// 14
  69. uint8_t node_depth; // 15
  70. uint8_t inner_length; // 16
  71. // uint8_t reserved[0];
  72. uint8_t salt[BLAKE2S_SALTBYTES]; // 24
  73. uint8_t personal[BLAKE2S_PERSONALBYTES]; // 32
  74. } blake2s_param;
  75. typedef struct __blake2s_state
  76. {
  77. uint32_t h[8];
  78. uint32_t t[2];
  79. uint32_t f[2];
  80. uint8_t buf[2 * BLAKE2S_BLOCKBYTES];
  81. uint32_t buflen;
  82. uint8_t outlen;
  83. uint8_t last_node;
  84. } blake2s_state;
  85. typedef struct __blake2b_param
  86. {
  87. uint8_t digest_length; // 1
  88. uint8_t key_length; // 2
  89. uint8_t fanout; // 3
  90. uint8_t depth; // 4
  91. uint32_t leaf_length; // 8
  92. uint64_t node_offset; // 16
  93. uint8_t node_depth; // 17
  94. uint8_t inner_length; // 18
  95. uint8_t reserved[14]; // 32
  96. uint8_t salt[BLAKE2B_SALTBYTES]; // 48
  97. uint8_t personal[BLAKE2B_PERSONALBYTES]; // 64
  98. } blake2b_param;
  99. typedef struct __blake2b_state
  100. {
  101. uint64_t h[8];
  102. uint64_t t[2];
  103. uint64_t f[2];
  104. uint8_t buf[2 * BLAKE2B_BLOCKBYTES];
  105. uint32_t buflen;
  106. uint8_t outlen;
  107. uint8_t last_node;
  108. } blake2b_state;
  109. typedef struct __blake2sp_state
  110. {
  111. blake2s_state S[8][1];
  112. blake2s_state R[1];
  113. uint8_t buf[8 * BLAKE2S_BLOCKBYTES];
  114. uint32_t buflen;
  115. uint8_t outlen;
  116. } blake2sp_state;
  117. typedef struct __blake2bp_state
  118. {
  119. blake2b_state S[4][1];
  120. blake2b_state R[1];
  121. uint8_t buf[4 * BLAKE2B_BLOCKBYTES];
  122. uint32_t buflen;
  123. uint8_t outlen;
  124. } blake2bp_state;
  125. #pragma pack(pop)
  126. // Streaming API
  127. BLAKE2_API int blake2s_init( blake2s_state *S, size_t outlen );
  128. BLAKE2_API int blake2s_init_key( blake2s_state *S, size_t outlen, const void *key, size_t keylen );
  129. BLAKE2_API int blake2s_init_param( blake2s_state *S, const blake2s_param *P );
  130. BLAKE2_API int blake2s_update( blake2s_state *S, const uint8_t *in, size_t inlen );
  131. BLAKE2_API int blake2s_final( blake2s_state *S, uint8_t *out, size_t outlen );
  132. BLAKE2_API int blake2b_init( blake2b_state *S, size_t outlen );
  133. BLAKE2_API int blake2b_init_key( blake2b_state *S, size_t outlen, const void *key, size_t keylen );
  134. BLAKE2_API int blake2b_init_param( blake2b_state *S, const blake2b_param *P );
  135. BLAKE2_API int blake2b_update( blake2b_state *S, const uint8_t *in, size_t inlen );
  136. BLAKE2_API int blake2b_final( blake2b_state *S, uint8_t *out, size_t outlen );
  137. BLAKE2_API int blake2sp_init( blake2sp_state *S, size_t outlen );
  138. BLAKE2_API int blake2sp_init_key( blake2sp_state *S, size_t outlen, const void *key, size_t keylen );
  139. BLAKE2_API int blake2sp_update( blake2sp_state *S, const uint8_t *in, size_t inlen );
  140. BLAKE2_API int blake2sp_final( blake2sp_state *S, uint8_t *out, size_t outlen );
  141. BLAKE2_API int blake2bp_init( blake2bp_state *S, size_t outlen );
  142. BLAKE2_API int blake2bp_init_key( blake2bp_state *S, size_t outlen, const void *key, size_t keylen );
  143. BLAKE2_API int blake2bp_update( blake2bp_state *S, const uint8_t *in, size_t inlen );
  144. BLAKE2_API int blake2bp_final( blake2bp_state *S, uint8_t *out, size_t outlen );
  145. // Simple API
  146. BLAKE2_API int blake2s( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
  147. BLAKE2_API int blake2b( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
  148. BLAKE2_API int blake2sp( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
  149. BLAKE2_API int blake2bp( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
  150. #if defined(__cplusplus)
  151. }
  152. #endif
  153. #endif