argon2.h 6.8 KB

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