core.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 ARGON2_CORE_H
  18. #define ARGON2_CORE_H
  19. #include "argon2.h"
  20. #define CONST_CAST(x) (x)(uintptr_t)
  21. /**********************Argon2 internal constants*******************************/
  22. enum argon2_core_constants {
  23. /* Memory block size in bytes */
  24. ARGON2_BLOCK_SIZE = 1024,
  25. ARGON2_QWORDS_IN_BLOCK = ARGON2_BLOCK_SIZE / 8,
  26. ARGON2_OWORDS_IN_BLOCK = ARGON2_BLOCK_SIZE / 16,
  27. ARGON2_HWORDS_IN_BLOCK = ARGON2_BLOCK_SIZE / 32,
  28. ARGON2_512BIT_WORDS_IN_BLOCK = ARGON2_BLOCK_SIZE / 64,
  29. /* Number of pseudo-random values generated by one call to Blake in Argon2i
  30. to
  31. generate reference block positions */
  32. ARGON2_ADDRESSES_IN_BLOCK = 128,
  33. /* Pre-hashing digest length and its extension*/
  34. ARGON2_PREHASH_DIGEST_LENGTH = 64,
  35. ARGON2_PREHASH_SEED_LENGTH = 72
  36. };
  37. /*************************Argon2 internal data types***********************/
  38. /*
  39. * Structure for the (1KB) memory block implemented as 128 64-bit words.
  40. * Memory blocks can be copied, XORed. Internal words can be accessed by [] (no
  41. * bounds checking).
  42. */
  43. typedef struct block_ { uint64_t v[ARGON2_QWORDS_IN_BLOCK]; } block;
  44. /*****************Functions that work with the block******************/
  45. /* Initialize each byte of the block with @in */
  46. void init_block_value(block *b, uint8_t in);
  47. /* Copy block @src to block @dst */
  48. void copy_block(block *dst, const block *src);
  49. /* XOR @src onto @dst bytewise */
  50. void xor_block(block *dst, const block *src);
  51. /*
  52. * Argon2 instance: memory pointer, number of passes, amount of memory, type,
  53. * and derived values.
  54. * Used to evaluate the number and location of blocks to construct in each
  55. * thread
  56. */
  57. typedef struct Argon2_instance_t {
  58. block *memory; /* Memory pointer */
  59. uint32_t version;
  60. uint32_t passes; /* Number of passes */
  61. uint32_t memory_blocks; /* Number of blocks in memory */
  62. uint32_t segment_length;
  63. uint32_t lane_length;
  64. uint32_t lanes;
  65. uint32_t threads;
  66. argon2_type_t type;
  67. int print_internals; /* whether to print the memory blocks */
  68. argon2_context_t *context_ptr; /* points back to original context */
  69. } argon2_instance_t;
  70. /*
  71. * Argon2 position: where we construct the block right now. Used to distribute
  72. * work between threads.
  73. */
  74. typedef struct Argon2_position_t {
  75. uint32_t pass;
  76. uint32_t lane;
  77. uint8_t slice;
  78. uint32_t index;
  79. } argon2_position_t;
  80. /*Struct that holds the inputs for thread handling FillSegment*/
  81. typedef struct Argon2_thread_data {
  82. argon2_instance_t *instance_ptr;
  83. argon2_position_t pos;
  84. } argon2_thread_data;
  85. /*************************Argon2 core functions********************************/
  86. /* Allocates memory to the given pointer, uses the appropriate allocator as
  87. * specified in the context. Total allocated memory is num*size.
  88. * @param context argon2_context which specifies the allocator
  89. * @param memory pointer to the pointer to the memory
  90. * @param size the size in bytes for each element to be allocated
  91. * @param num the number of elements to be allocated
  92. * @return ARGON2_OK if @memory is a valid pointer and memory is allocated
  93. */
  94. int allocate_memory(const argon2_context_t *context, uint8_t **memory,
  95. size_t num, size_t size);
  96. /*
  97. * Frees memory at the given pointer, uses the appropriate deallocator as
  98. * specified in the context. Also cleans the memory using clear_internal_memory.
  99. * @param context argon2_context which specifies the deallocator
  100. * @param memory pointer to buffer to be freed
  101. * @param size the size in bytes for each element to be deallocated
  102. * @param num the number of elements to be deallocated
  103. */
  104. void free_memory(const argon2_context_t *context, uint8_t *memory,
  105. size_t num, size_t size);
  106. /* Function that securely cleans the memory. This ignores any flags set
  107. * regarding clearing memory. Usually one just calls clear_internal_memory.
  108. * @param mem Pointer to the memory
  109. * @param s Memory size in bytes
  110. */
  111. void secure_wipe_memory(void *v, size_t n);
  112. /* Function that securely clears the memory if FLAG_clear_internal_memory is
  113. * set. If the flag isn't set, this function does nothing.
  114. * @param mem Pointer to the memory
  115. * @param s Memory size in bytes
  116. */
  117. void clear_internal_memory(void *v, size_t n);
  118. /*
  119. * Computes absolute position of reference block in the lane following a skewed
  120. * distribution and using a pseudo-random value as input
  121. * @param instance Pointer to the current instance
  122. * @param position Pointer to the current position
  123. * @param pseudo_rand 32-bit pseudo-random value used to determine the position
  124. * @param same_lane Indicates if the block will be taken from the current lane.
  125. * If so we can reference the current segment
  126. * @pre All pointers must be valid
  127. */
  128. uint32_t index_alpha(const argon2_instance_t *instance,
  129. const argon2_position_t *position, uint32_t pseudo_rand,
  130. int same_lane);
  131. /*
  132. * Function that validates all inputs against predefined restrictions and return
  133. * an error code
  134. * @param context Pointer to current Argon2 context
  135. * @return ARGON2_OK if everything is all right, otherwise one of error codes
  136. * (all defined in <argon2.h>
  137. */
  138. int validate_inputs(const argon2_context_t *context);
  139. /*
  140. * Hashes all the inputs into @a blockhash[PREHASH_DIGEST_LENGTH], clears
  141. * password and secret if needed
  142. * @param context Pointer to the Argon2 internal structure containing memory
  143. * pointer, and parameters for time and space requirements.
  144. * @param blockhash Buffer for pre-hashing digest
  145. * @param type Argon2 type
  146. * @pre @a blockhash must have at least @a PREHASH_DIGEST_LENGTH bytes
  147. * allocated
  148. */
  149. void initial_hash(uint8_t *blockhash, argon2_context_t *context,
  150. argon2_type_t type);
  151. /*
  152. * Function creates first 2 blocks per lane
  153. * @param instance Pointer to the current instance
  154. * @param blockhash Pointer to the pre-hashing digest
  155. * @pre blockhash must point to @a PREHASH_SEED_LENGTH allocated values
  156. */
  157. void fill_first_blocks(uint8_t *blockhash, const argon2_instance_t *instance);
  158. /*
  159. * Function allocates memory, hashes the inputs with Blake, and creates first
  160. * two blocks. Returns the pointer to the main memory with 2 blocks per lane
  161. * initialized
  162. * @param context Pointer to the Argon2 internal structure containing memory
  163. * pointer, and parameters for time and space requirements.
  164. * @param instance Current Argon2 instance
  165. * @return Zero if successful, -1 if memory failed to allocate. @context->state
  166. * will be modified if successful.
  167. */
  168. int initialize(argon2_instance_t *instance, argon2_context_t *context);
  169. /*
  170. * XORing the last block of each lane, hashing it, making the tag. Deallocates
  171. * the memory.
  172. * @param context Pointer to current Argon2 context (use only the out parameters
  173. * from it)
  174. * @param instance Pointer to current instance of Argon2
  175. * @pre instance->state must point to necessary amount of memory
  176. * @pre context->out must point to outlen bytes of memory
  177. * @pre if context->free_cbk is not NULL, it should point to a function that
  178. * deallocates memory
  179. */
  180. void finalize(const argon2_context_t *context, argon2_instance_t *instance);
  181. /*
  182. * Function that fills the segment using previous segments also from other
  183. * threads
  184. * @param context current context
  185. * @param instance Pointer to the current instance
  186. * @param position Current position
  187. * @pre all block pointers must be valid
  188. */
  189. void fill_segment(const argon2_instance_t *instance,
  190. argon2_position_t position);
  191. /*
  192. * Function that fills the entire memory t_cost times based on the first two
  193. * blocks in each lane
  194. * @param instance Pointer to the current instance
  195. * @return ARGON2_OK if successful, @context->state
  196. */
  197. int fill_memory_blocks(argon2_instance_t *instance);
  198. #endif