blake2.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. Copyright (c) 2018 tevador
  3. This file is part of RandomX.
  4. RandomX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. RandomX is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with RandomX. If not, see<http://www.gnu.org/licenses/>.
  14. */
  15. /* Original code from Argon2 reference source code package used under CC0 Licence
  16. * https://github.com/P-H-C/phc-winner-argon2
  17. * Copyright 2015
  18. * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
  19. */
  20. #ifndef PORTABLE_BLAKE2_H
  21. #define PORTABLE_BLAKE2_H
  22. #include <stdint.h>
  23. #include <limits.h>
  24. #if defined(__cplusplus)
  25. extern "C" {
  26. #endif
  27. enum blake2b_constant {
  28. BLAKE2B_BLOCKBYTES = 128,
  29. BLAKE2B_OUTBYTES = 64,
  30. BLAKE2B_KEYBYTES = 64,
  31. BLAKE2B_SALTBYTES = 16,
  32. BLAKE2B_PERSONALBYTES = 16
  33. };
  34. #pragma pack(push, 1)
  35. typedef struct __blake2b_param {
  36. uint8_t digest_length; /* 1 */
  37. uint8_t key_length; /* 2 */
  38. uint8_t fanout; /* 3 */
  39. uint8_t depth; /* 4 */
  40. uint32_t leaf_length; /* 8 */
  41. uint64_t node_offset; /* 16 */
  42. uint8_t node_depth; /* 17 */
  43. uint8_t inner_length; /* 18 */
  44. uint8_t reserved[14]; /* 32 */
  45. uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */
  46. uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */
  47. } blake2b_param;
  48. #pragma pack(pop)
  49. typedef struct __blake2b_state {
  50. uint64_t h[8];
  51. uint64_t t[2];
  52. uint64_t f[2];
  53. uint8_t buf[BLAKE2B_BLOCKBYTES];
  54. unsigned buflen;
  55. unsigned outlen;
  56. uint8_t last_node;
  57. } blake2b_state;
  58. /* Ensure param structs have not been wrongly padded */
  59. /* Poor man's static_assert */
  60. enum {
  61. blake2_size_check_0 = 1 / !!(CHAR_BIT == 8),
  62. blake2_size_check_2 =
  63. 1 / !!(sizeof(blake2b_param) == sizeof(uint64_t) * CHAR_BIT)
  64. };
  65. /* Streaming API */
  66. int blake2b_init(blake2b_state *S, size_t outlen);
  67. int blake2b_init_key(blake2b_state *S, size_t outlen, const void *key,
  68. size_t keylen);
  69. int blake2b_init_param(blake2b_state *S, const blake2b_param *P);
  70. int blake2b_update(blake2b_state *S, const void *in, size_t inlen);
  71. int blake2b_final(blake2b_state *S, void *out, size_t outlen);
  72. /* Simple API */
  73. int blake2b(void *out, size_t outlen, const void *in, size_t inlen,
  74. const void *key, size_t keylen);
  75. /* Argon2 Team - Begin Code */
  76. int blake2b_long(void *out, size_t outlen, const void *in, size_t inlen);
  77. /* Argon2 Team - End Code */
  78. #if defined(__cplusplus)
  79. }
  80. #endif
  81. #endif