blake2.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Argon2 reference source code package - reference C implementations
  3. *
  4. * Copyright 2015
  5. * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
  6. *
  7. * You may use this work under the terms of a Creative Commons CC0 1.0
  8. * License/Waiver or the Apache Public License 2.0, at your option. The terms of
  9. * these licenses can be found at:
  10. *
  11. * - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
  12. * - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * You should have received a copy of both of these licenses along with this
  15. * software. If not, they may be obtained at the above URLs.
  16. */
  17. #ifndef PORTABLE_BLAKE2_H
  18. #define PORTABLE_BLAKE2_H
  19. #include <argon2.h>
  20. #if defined(__cplusplus)
  21. extern "C" {
  22. #endif
  23. enum blake2b_constant {
  24. BLAKE2B_BLOCKBYTES = 128,
  25. BLAKE2B_OUTBYTES = 64,
  26. BLAKE2B_KEYBYTES = 64,
  27. BLAKE2B_SALTBYTES = 16,
  28. BLAKE2B_PERSONALBYTES = 16
  29. };
  30. #pragma pack(push, 1)
  31. typedef struct __blake2b_param {
  32. uint8_t digest_length; /* 1 */
  33. uint8_t key_length; /* 2 */
  34. uint8_t fanout; /* 3 */
  35. uint8_t depth; /* 4 */
  36. uint32_t leaf_length; /* 8 */
  37. uint64_t node_offset; /* 16 */
  38. uint8_t node_depth; /* 17 */
  39. uint8_t inner_length; /* 18 */
  40. uint8_t reserved[14]; /* 32 */
  41. uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */
  42. uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */
  43. } blake2b_param;
  44. #pragma pack(pop)
  45. typedef struct __blake2b_state {
  46. uint64_t h[8];
  47. uint64_t t[2];
  48. uint64_t f[2];
  49. uint8_t buf[BLAKE2B_BLOCKBYTES];
  50. unsigned buflen;
  51. unsigned outlen;
  52. uint8_t last_node;
  53. } blake2b_state;
  54. /* Ensure param structs have not been wrongly padded */
  55. /* Poor man's static_assert */
  56. enum {
  57. blake2_size_check_0 = 1 / !!(CHAR_BIT == 8),
  58. blake2_size_check_2 =
  59. 1 / !!(sizeof(blake2b_param) == sizeof(uint64_t) * CHAR_BIT)
  60. };
  61. /* Streaming API */
  62. ARGON2_LOCAL int kp_blake2b_init(blake2b_state *S, size_t outlen);
  63. ARGON2_LOCAL int kp_blake2b_init_key(blake2b_state *S, size_t outlen, const void *key,
  64. size_t keylen);
  65. ARGON2_LOCAL int kp_blake2b_init_param(blake2b_state *S, const blake2b_param *P);
  66. ARGON2_LOCAL int kp_blake2b_update(blake2b_state *S, const void *in, size_t inlen);
  67. ARGON2_LOCAL int kp_blake2b_final(blake2b_state *S, void *out, size_t outlen);
  68. /* Simple API */
  69. ARGON2_LOCAL int vblake2b(void *out, size_t outlen, const void *in, size_t inlen,
  70. const void *key, size_t keylen);
  71. /* Argon2 Team - Begin Code */
  72. ARGON2_LOCAL int kp_blake2b_long(void *out, size_t outlen, const void *in, size_t inlen);
  73. /* Argon2 Team - End Code */
  74. #if defined(__cplusplus)
  75. }
  76. #endif
  77. #endif