blake2.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. Copyright (c) 2018-2019, tevador <tevador@gmail.com>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of the copyright holder nor the
  12. names of its contributors may be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  18. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  20. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  22. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /* Original code from Argon2 reference source code package used under CC0 Licence
  26. * https://github.com/P-H-C/phc-winner-argon2
  27. * Copyright 2015
  28. * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
  29. */
  30. #ifndef PORTABLE_BLAKE2_H
  31. #define PORTABLE_BLAKE2_H
  32. #include <stdint.h>
  33. #include <limits.h>
  34. #if defined(__cplusplus)
  35. extern "C" {
  36. #endif
  37. enum blake2b_constant {
  38. BLAKE2B_BLOCKBYTES = 128,
  39. BLAKE2B_OUTBYTES = 64,
  40. BLAKE2B_KEYBYTES = 64,
  41. BLAKE2B_SALTBYTES = 16,
  42. BLAKE2B_PERSONALBYTES = 16
  43. };
  44. #pragma pack(push, 1)
  45. typedef struct __blake2b_param {
  46. uint8_t digest_length; /* 1 */
  47. uint8_t key_length; /* 2 */
  48. uint8_t fanout; /* 3 */
  49. uint8_t depth; /* 4 */
  50. uint32_t leaf_length; /* 8 */
  51. uint64_t node_offset; /* 16 */
  52. uint8_t node_depth; /* 17 */
  53. uint8_t inner_length; /* 18 */
  54. uint8_t reserved[14]; /* 32 */
  55. uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */
  56. uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */
  57. } blake2b_param;
  58. #pragma pack(pop)
  59. typedef struct __blake2b_state {
  60. uint64_t h[8];
  61. uint64_t t[2];
  62. uint64_t f[2];
  63. uint8_t buf[BLAKE2B_BLOCKBYTES];
  64. unsigned buflen;
  65. unsigned outlen;
  66. uint8_t last_node;
  67. } blake2b_state;
  68. /* Ensure param structs have not been wrongly padded */
  69. /* Poor man's static_assert */
  70. enum {
  71. blake2_size_check_0 = 1 / !!(CHAR_BIT == 8),
  72. blake2_size_check_2 =
  73. 1 / !!(sizeof(blake2b_param) == sizeof(uint64_t) * CHAR_BIT)
  74. };
  75. /* Streaming API */
  76. int blake2b_init(blake2b_state *S, size_t outlen);
  77. int blake2b_init_key(blake2b_state *S, size_t outlen, const void *key,
  78. size_t keylen);
  79. int blake2b_init_param(blake2b_state *S, const blake2b_param *P);
  80. int blake2b_update(blake2b_state *S, const void *in, size_t inlen);
  81. int blake2b_final(blake2b_state *S, void *out, size_t outlen);
  82. /* Simple API */
  83. int blake2b(void *out, size_t outlen, const void *in, size_t inlen,
  84. const void *key, size_t keylen);
  85. /* Argon2 Team - Begin Code */
  86. int rxa2_blake2b_long(void *out, size_t outlen, const void *in, size_t inlen);
  87. /* Argon2 Team - End Code */
  88. #if defined(__cplusplus)
  89. }
  90. #endif
  91. #endif