argon2.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #pragma once
  31. #include <stdint.h>
  32. #include <stddef.h>
  33. #include <limits.h>
  34. /*
  35. * Argon2 input parameter restrictions
  36. */
  37. /* Minimum and maximum number of lanes (degree of parallelism) */
  38. #define ARGON2_MIN_LANES UINT32_C(1)
  39. #define ARGON2_MAX_LANES UINT32_C(0xFFFFFF)
  40. /* Minimum and maximum number of threads */
  41. #define ARGON2_MIN_THREADS UINT32_C(1)
  42. #define ARGON2_MAX_THREADS UINT32_C(0xFFFFFF)
  43. /* Number of synchronization points between lanes per pass */
  44. #define ARGON2_SYNC_POINTS UINT32_C(4)
  45. /* Minimum and maximum digest size in bytes */
  46. #define ARGON2_MIN_OUTLEN UINT32_C(4)
  47. #define ARGON2_MAX_OUTLEN UINT32_C(0xFFFFFFFF)
  48. /* Minimum and maximum number of memory blocks (each of BLOCK_SIZE bytes) */
  49. #define ARGON2_MIN_MEMORY (2 * ARGON2_SYNC_POINTS) /* 2 blocks per slice */
  50. #define ARGON2_MIN(a, b) ((a) < (b) ? (a) : (b))
  51. /* Max memory size is addressing-space/2, topping at 2^32 blocks (4 TB) */
  52. #define ARGON2_MAX_MEMORY_BITS \
  53. ARGON2_MIN(UINT32_C(32), (sizeof(void *) * CHAR_BIT - 10 - 1))
  54. #define ARGON2_MAX_MEMORY \
  55. ARGON2_MIN(UINT32_C(0xFFFFFFFF), UINT64_C(1) << ARGON2_MAX_MEMORY_BITS)
  56. /* Minimum and maximum number of passes */
  57. #define ARGON2_MIN_TIME UINT32_C(1)
  58. #define ARGON2_MAX_TIME UINT32_C(0xFFFFFFFF)
  59. /* Minimum and maximum password length in bytes */
  60. #define ARGON2_MIN_PWD_LENGTH UINT32_C(0)
  61. #define ARGON2_MAX_PWD_LENGTH UINT32_C(0xFFFFFFFF)
  62. /* Minimum and maximum associated data length in bytes */
  63. #define ARGON2_MIN_AD_LENGTH UINT32_C(0)
  64. #define ARGON2_MAX_AD_LENGTH UINT32_C(0xFFFFFFFF)
  65. /* Minimum and maximum salt length in bytes */
  66. #define ARGON2_MIN_SALT_LENGTH UINT32_C(8)
  67. #define ARGON2_MAX_SALT_LENGTH UINT32_C(0xFFFFFFFF)
  68. /* Minimum and maximum key length in bytes */
  69. #define ARGON2_MIN_SECRET UINT32_C(0)
  70. #define ARGON2_MAX_SECRET UINT32_C(0xFFFFFFFF)
  71. /* Flags to determine which fields are securely wiped (default = no wipe). */
  72. #define ARGON2_DEFAULT_FLAGS UINT32_C(0)
  73. #define ARGON2_FLAG_CLEAR_PASSWORD (UINT32_C(1) << 0)
  74. #define ARGON2_FLAG_CLEAR_SECRET (UINT32_C(1) << 1)
  75. /* Error codes */
  76. typedef enum Argon2_ErrorCodes {
  77. ARGON2_OK = 0,
  78. ARGON2_OUTPUT_PTR_NULL = -1,
  79. ARGON2_OUTPUT_TOO_SHORT = -2,
  80. ARGON2_OUTPUT_TOO_LONG = -3,
  81. ARGON2_PWD_TOO_SHORT = -4,
  82. ARGON2_PWD_TOO_LONG = -5,
  83. ARGON2_SALT_TOO_SHORT = -6,
  84. ARGON2_SALT_TOO_LONG = -7,
  85. ARGON2_AD_TOO_SHORT = -8,
  86. ARGON2_AD_TOO_LONG = -9,
  87. ARGON2_SECRET_TOO_SHORT = -10,
  88. ARGON2_SECRET_TOO_LONG = -11,
  89. ARGON2_TIME_TOO_SMALL = -12,
  90. ARGON2_TIME_TOO_LARGE = -13,
  91. ARGON2_MEMORY_TOO_LITTLE = -14,
  92. ARGON2_MEMORY_TOO_MUCH = -15,
  93. ARGON2_LANES_TOO_FEW = -16,
  94. ARGON2_LANES_TOO_MANY = -17,
  95. ARGON2_PWD_PTR_MISMATCH = -18, /* NULL ptr with non-zero length */
  96. ARGON2_SALT_PTR_MISMATCH = -19, /* NULL ptr with non-zero length */
  97. ARGON2_SECRET_PTR_MISMATCH = -20, /* NULL ptr with non-zero length */
  98. ARGON2_AD_PTR_MISMATCH = -21, /* NULL ptr with non-zero length */
  99. ARGON2_MEMORY_ALLOCATION_ERROR = -22,
  100. ARGON2_FREE_MEMORY_CBK_NULL = -23,
  101. ARGON2_ALLOCATE_MEMORY_CBK_NULL = -24,
  102. ARGON2_INCORRECT_PARAMETER = -25,
  103. ARGON2_INCORRECT_TYPE = -26,
  104. ARGON2_OUT_PTR_MISMATCH = -27,
  105. ARGON2_THREADS_TOO_FEW = -28,
  106. ARGON2_THREADS_TOO_MANY = -29,
  107. ARGON2_MISSING_ARGS = -30,
  108. ARGON2_ENCODING_FAIL = -31,
  109. ARGON2_DECODING_FAIL = -32,
  110. ARGON2_THREAD_FAIL = -33,
  111. ARGON2_DECODING_LENGTH_FAIL = -34,
  112. ARGON2_VERIFY_MISMATCH = -35
  113. } argon2_error_codes;
  114. /* Memory allocator types --- for external allocation */
  115. typedef int(*allocate_fptr)(uint8_t **memory, size_t bytes_to_allocate);
  116. typedef void(*deallocate_fptr)(uint8_t *memory, size_t bytes_to_allocate);
  117. /* Argon2 external data structures */
  118. /*
  119. *****
  120. * Context: structure to hold Argon2 inputs:
  121. * output array and its length,
  122. * password and its length,
  123. * salt and its length,
  124. * secret and its length,
  125. * associated data and its length,
  126. * number of passes, amount of used memory (in KBytes, can be rounded up a bit)
  127. * number of parallel threads that will be run.
  128. * All the parameters above affect the output hash value.
  129. * Additionally, two function pointers can be provided to allocate and
  130. * deallocate the memory (if NULL, memory will be allocated internally).
  131. * Also, three flags indicate whether to erase password, secret as soon as they
  132. * are pre-hashed (and thus not needed anymore), and the entire memory
  133. *****
  134. * Simplest situation: you have output array out[8], password is stored in
  135. * pwd[32], salt is stored in salt[16], you do not have keys nor associated
  136. * data. You need to spend 1 GB of RAM and you run 5 passes of Argon2d with
  137. * 4 parallel lanes.
  138. * You want to erase the password, but you're OK with last pass not being
  139. * erased. You want to use the default memory allocator.
  140. * Then you initialize:
  141. Argon2_Context(out,8,pwd,32,salt,16,NULL,0,NULL,0,5,1<<20,4,4,NULL,NULL,true,false,false,false)
  142. */
  143. typedef struct Argon2_Context {
  144. uint8_t *out; /* output array */
  145. uint32_t outlen; /* digest length */
  146. uint8_t *pwd; /* password array */
  147. uint32_t pwdlen; /* password length */
  148. uint8_t *salt; /* salt array */
  149. uint32_t saltlen; /* salt length */
  150. uint8_t *secret; /* key array */
  151. uint32_t secretlen; /* key length */
  152. uint8_t *ad; /* associated data array */
  153. uint32_t adlen; /* associated data length */
  154. uint32_t t_cost; /* number of passes */
  155. uint32_t m_cost; /* amount of memory requested (KB) */
  156. uint32_t lanes; /* number of lanes */
  157. uint32_t threads; /* maximum number of threads */
  158. uint32_t version; /* version number */
  159. allocate_fptr allocate_cbk; /* pointer to memory allocator */
  160. deallocate_fptr free_cbk; /* pointer to memory deallocator */
  161. uint32_t flags; /* array of bool options */
  162. } argon2_context;
  163. /* Argon2 primitive type */
  164. typedef enum Argon2_type {
  165. Argon2_d = 0,
  166. Argon2_i = 1,
  167. Argon2_id = 2
  168. } argon2_type;
  169. /* Version of the algorithm */
  170. typedef enum Argon2_version {
  171. ARGON2_VERSION_10 = 0x10,
  172. ARGON2_VERSION_13 = 0x13,
  173. ARGON2_VERSION_NUMBER = ARGON2_VERSION_13
  174. } argon2_version;
  175. //Argon2 instance - forward declaration
  176. typedef struct Argon2_instance_t argon2_instance_t;
  177. //Argon2 position = forward declaration
  178. typedef struct Argon2_position_t argon2_position_t;
  179. //Argon2 implementation function
  180. typedef void randomx_argon2_impl(const argon2_instance_t* instance,
  181. argon2_position_t position);
  182. #if defined(__cplusplus)
  183. extern "C" {
  184. #endif
  185. /*
  186. * Function that fills the segment using previous segments also from other
  187. * threads
  188. * @param context current context
  189. * @param instance Pointer to the current instance
  190. * @param position Current position
  191. * @pre all block pointers must be valid
  192. */
  193. void randomx_argon2_fill_segment_ref(const argon2_instance_t* instance,
  194. argon2_position_t position);
  195. randomx_argon2_impl *randomx_argon2_impl_ssse3();
  196. randomx_argon2_impl *randomx_argon2_impl_avx2();
  197. #if defined(__cplusplus)
  198. }
  199. #endif