blake2.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. BLAKE2 reference source code package - reference 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. All code is triple-licensed under the
  8. [CC0](http://creativecommons.org/publicdomain/zero/1.0), the
  9. [OpenSSL Licence](https://www.openssl.org/source/license.html), or
  10. the [Apache Public License 2.0](http://www.apache.org/licenses/LICENSE-2.0),
  11. at your choosing.
  12. */
  13. #ifndef blake2_H
  14. #define blake2_H
  15. #include <stddef.h>
  16. #include <stdint.h>
  17. #include "crypto_generichash_blake2b.h"
  18. #include "export.h"
  19. #define blake2b_init_param crypto_generichash_blake2b__init_param
  20. #define blake2b_init crypto_generichash_blake2b__init
  21. #define blake2b_init_salt_personal \
  22. crypto_generichash_blake2b__init_salt_personal
  23. #define blake2b_init_key crypto_generichash_blake2b__init_key
  24. #define blake2b_init_key_salt_personal \
  25. crypto_generichash_blake2b__init_key_salt_personal
  26. #define blake2b_update crypto_generichash_blake2b__update
  27. #define blake2b_final crypto_generichash_blake2b__final
  28. #define blake2b crypto_generichash_blake2b__blake2b
  29. #define blake2b_salt_personal crypto_generichash_blake2b__blake2b_salt_personal
  30. #define blake2b_pick_best_implementation \
  31. crypto_generichash_blake2b__pick_best_implementation
  32. enum blake2b_constant {
  33. BLAKE2B_BLOCKBYTES = 128,
  34. BLAKE2B_OUTBYTES = 64,
  35. BLAKE2B_KEYBYTES = 64,
  36. BLAKE2B_SALTBYTES = 16,
  37. BLAKE2B_PERSONALBYTES = 16
  38. };
  39. #if defined(__IBMC__) || defined(__SUNPRO_C) || defined(__SUNPRO_CC)
  40. #pragma pack(1)
  41. #else
  42. #pragma pack(push, 1)
  43. #endif
  44. typedef struct blake2b_param_ {
  45. uint8_t digest_length; /* 1 */
  46. uint8_t key_length; /* 2 */
  47. uint8_t fanout; /* 3 */
  48. uint8_t depth; /* 4 */
  49. uint8_t leaf_length[4]; /* 8 */
  50. uint8_t node_offset[8]; /* 16 */
  51. uint8_t node_depth; /* 17 */
  52. uint8_t inner_length; /* 18 */
  53. uint8_t reserved[14]; /* 32 */
  54. uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */
  55. uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */
  56. } blake2b_param;
  57. typedef struct blake2b_state {
  58. uint64_t h[8];
  59. uint64_t t[2];
  60. uint64_t f[2];
  61. uint8_t buf[2 * 128];
  62. size_t buflen;
  63. uint8_t last_node;
  64. } blake2b_state;
  65. #if defined(__IBMC__) || defined(__SUNPRO_C) || defined(__SUNPRO_CC)
  66. #pragma pack()
  67. #else
  68. #pragma pack(pop)
  69. #endif
  70. /* Streaming API */
  71. int blake2b_init(blake2b_state *S, const uint8_t outlen);
  72. int blake2b_init_salt_personal(blake2b_state *S, const uint8_t outlen,
  73. const void *salt, const void *personal);
  74. int blake2b_init_key(blake2b_state *S, const uint8_t outlen, const void *key,
  75. const uint8_t keylen);
  76. int blake2b_init_key_salt_personal(blake2b_state *S, const uint8_t outlen,
  77. const void *key, const uint8_t keylen,
  78. const void *salt, const void *personal);
  79. int blake2b_init_param(blake2b_state *S, const blake2b_param *P);
  80. int blake2b_update(blake2b_state *S, const uint8_t *in, uint64_t inlen);
  81. int blake2b_final(blake2b_state *S, uint8_t *out, uint8_t outlen);
  82. /* Simple API */
  83. int blake2b(uint8_t *out, const void *in, const void *key, const uint8_t outlen,
  84. const uint64_t inlen, uint8_t keylen);
  85. int blake2b_salt_personal(uint8_t *out, const void *in, const void *key,
  86. const uint8_t outlen, const uint64_t inlen,
  87. uint8_t keylen, const void *salt,
  88. const void *personal);
  89. typedef int (*blake2b_compress_fn)(blake2b_state *S,
  90. const uint8_t block[BLAKE2B_BLOCKBYTES]);
  91. int blake2b_pick_best_implementation(void);
  92. int blake2b_compress_ref(blake2b_state *S,
  93. const uint8_t block[BLAKE2B_BLOCKBYTES]);
  94. int blake2b_compress_ssse3(blake2b_state *S,
  95. const uint8_t block[BLAKE2B_BLOCKBYTES]);
  96. int blake2b_compress_sse41(blake2b_state *S,
  97. const uint8_t block[BLAKE2B_BLOCKBYTES]);
  98. int blake2b_compress_avx2(blake2b_state *S,
  99. const uint8_t block[BLAKE2B_BLOCKBYTES]);
  100. #endif