argon2_core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. /*For memory wiping*/
  21. #ifdef _MSC_VER
  22. #include <windows.h>
  23. #include <winbase.h> /* For SecureZeroMemory */
  24. #endif
  25. #if defined __STDC_LIB_EXT1__
  26. #define __STDC_WANT_LIB_EXT1__ 1
  27. #endif
  28. #define VC_GE_2005(version) (version >= 1400)
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "argon2_core.h"
  33. #include "blake2/blake2.h"
  34. #include "blake2/blake2-impl.h"
  35. #ifdef GENKAT
  36. #include "genkat.h"
  37. #endif
  38. #if defined(__clang__)
  39. #if __has_attribute(optnone)
  40. #define NOT_OPTIMIZED __attribute__((optnone))
  41. #endif
  42. #elif defined(__GNUC__)
  43. #define GCC_VERSION \
  44. (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
  45. #if GCC_VERSION >= 40400
  46. #define NOT_OPTIMIZED __attribute__((optimize("O0")))
  47. #endif
  48. #endif
  49. #ifndef NOT_OPTIMIZED
  50. #define NOT_OPTIMIZED
  51. #endif
  52. /***************Instance and Position constructors**********/
  53. void init_block_value(block *b, uint8_t in) { memset(b->v, in, sizeof(b->v)); }
  54. void copy_block(block *dst, const block *src) {
  55. memcpy(dst->v, src->v, sizeof(uint64_t) * ARGON2_QWORDS_IN_BLOCK);
  56. }
  57. void xor_block(block *dst, const block *src) {
  58. int i;
  59. for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
  60. dst->v[i] ^= src->v[i];
  61. }
  62. }
  63. static void load_block(block *dst, const void *input) {
  64. unsigned i;
  65. for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
  66. dst->v[i] = load64((const uint8_t *)input + i * sizeof(dst->v[i]));
  67. }
  68. }
  69. static void store_block(void *output, const block *src) {
  70. unsigned i;
  71. for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
  72. store64((uint8_t *)output + i * sizeof(src->v[i]), src->v[i]);
  73. }
  74. }
  75. /***************Memory functions*****************/
  76. int allocate_memory(const argon2_context *context, uint8_t **memory,
  77. size_t num, size_t size) {
  78. size_t memory_size = num * size;
  79. if (memory == NULL) {
  80. return ARGON2_MEMORY_ALLOCATION_ERROR;
  81. }
  82. /* 1. Check for multiplication overflow */
  83. if (size != 0 && memory_size / size != num) {
  84. return ARGON2_MEMORY_ALLOCATION_ERROR;
  85. }
  86. /* 2. Try to allocate with appropriate allocator */
  87. if (context->allocate_cbk) {
  88. (context->allocate_cbk)(memory, memory_size);
  89. }
  90. else {
  91. *memory = (uint8_t*)malloc(memory_size);
  92. }
  93. if (*memory == NULL) {
  94. return ARGON2_MEMORY_ALLOCATION_ERROR;
  95. }
  96. return ARGON2_OK;
  97. }
  98. void free_memory(const argon2_context *context, uint8_t *memory,
  99. size_t num, size_t size) {
  100. size_t memory_size = num * size;
  101. clear_internal_memory(memory, memory_size);
  102. if (context->free_cbk) {
  103. (context->free_cbk)(memory, memory_size);
  104. }
  105. else {
  106. free(memory);
  107. }
  108. }
  109. void NOT_OPTIMIZED secure_wipe_memory(void *v, size_t n) {
  110. #if defined(_MSC_VER) && VC_GE_2005(_MSC_VER)
  111. SecureZeroMemory(v, n);
  112. #elif defined memset_s
  113. memset_s(v, n, 0, n);
  114. #elif defined(__OpenBSD__)
  115. explicit_bzero(v, n);
  116. #else
  117. static void *(*const volatile memset_sec)(void *, int, size_t) = &memset;
  118. memset_sec(v, 0, n);
  119. #endif
  120. }
  121. /* Memory clear flag defaults to true. */
  122. #define FLAG_clear_internal_memory 0
  123. void clear_internal_memory(void *v, size_t n) {
  124. if (FLAG_clear_internal_memory && v) {
  125. secure_wipe_memory(v, n);
  126. }
  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. * Pass 0:
  133. * This lane : all already finished segments plus already constructed
  134. * blocks in this segment
  135. * Other lanes : all already finished segments
  136. * Pass 1+:
  137. * This lane : (SYNC_POINTS - 1) last segments plus already constructed
  138. * blocks in this segment
  139. * Other lanes : (SYNC_POINTS - 1) last segments
  140. */
  141. uint32_t reference_area_size;
  142. uint64_t relative_position;
  143. uint32_t start_position, absolute_position;
  144. if (0 == position->pass) {
  145. /* First pass */
  146. if (0 == position->slice) {
  147. /* First slice */
  148. reference_area_size =
  149. position->index - 1; /* all but the previous */
  150. }
  151. else {
  152. if (same_lane) {
  153. /* The same lane => add current segment */
  154. reference_area_size =
  155. position->slice * instance->segment_length +
  156. position->index - 1;
  157. }
  158. else {
  159. reference_area_size =
  160. position->slice * instance->segment_length +
  161. ((position->index == 0) ? (-1) : 0);
  162. }
  163. }
  164. }
  165. else {
  166. /* Second pass */
  167. if (same_lane) {
  168. reference_area_size = instance->lane_length -
  169. instance->segment_length + position->index -
  170. 1;
  171. }
  172. else {
  173. reference_area_size = instance->lane_length -
  174. instance->segment_length +
  175. ((position->index == 0) ? (-1) : 0);
  176. }
  177. }
  178. /* 1.2.4. Mapping pseudo_rand to 0..<reference_area_size-1> and produce
  179. * relative position */
  180. relative_position = pseudo_rand;
  181. relative_position = relative_position * relative_position >> 32;
  182. relative_position = reference_area_size - 1 -
  183. (reference_area_size * relative_position >> 32);
  184. /* 1.2.5 Computing starting position */
  185. start_position = 0;
  186. if (0 != position->pass) {
  187. start_position = (position->slice == ARGON2_SYNC_POINTS - 1)
  188. ? 0
  189. : (position->slice + 1) * instance->segment_length;
  190. }
  191. /* 1.2.6. Computing absolute position */
  192. absolute_position = (start_position + relative_position) %
  193. instance->lane_length; /* absolute position */
  194. return absolute_position;
  195. }
  196. /* Single-threaded version for p=1 case */
  197. static int fill_memory_blocks_st(argon2_instance_t *instance) {
  198. uint32_t r, s, l;
  199. for (r = 0; r < instance->passes; ++r) {
  200. for (s = 0; s < ARGON2_SYNC_POINTS; ++s) {
  201. for (l = 0; l < instance->lanes; ++l) {
  202. argon2_position_t position = { r, l, (uint8_t)s, 0 };
  203. fill_segment(instance, position);
  204. }
  205. }
  206. #ifdef GENKAT
  207. internal_kat(instance, r); /* Print all memory blocks */
  208. #endif
  209. }
  210. return ARGON2_OK;
  211. }
  212. int fill_memory_blocks(argon2_instance_t *instance) {
  213. if (instance == NULL || instance->lanes == 0) {
  214. return ARGON2_INCORRECT_PARAMETER;
  215. }
  216. return fill_memory_blocks_st(instance);
  217. }
  218. int validate_inputs(const argon2_context *context) {
  219. if (NULL == context) {
  220. return ARGON2_INCORRECT_PARAMETER;
  221. }
  222. if (NULL == context->out) {
  223. return ARGON2_OUTPUT_PTR_NULL;
  224. }
  225. /* Validate output length */
  226. if (ARGON2_MIN_OUTLEN > context->outlen) {
  227. return ARGON2_OUTPUT_TOO_SHORT;
  228. }
  229. if (ARGON2_MAX_OUTLEN < context->outlen) {
  230. return ARGON2_OUTPUT_TOO_LONG;
  231. }
  232. /* Validate password (required param) */
  233. if (NULL == context->pwd) {
  234. if (0 != context->pwdlen) {
  235. return ARGON2_PWD_PTR_MISMATCH;
  236. }
  237. }
  238. if (ARGON2_MIN_PWD_LENGTH > context->pwdlen) {
  239. return ARGON2_PWD_TOO_SHORT;
  240. }
  241. if (ARGON2_MAX_PWD_LENGTH < context->pwdlen) {
  242. return ARGON2_PWD_TOO_LONG;
  243. }
  244. /* Validate salt (required param) */
  245. if (NULL == context->salt) {
  246. if (0 != context->saltlen) {
  247. return ARGON2_SALT_PTR_MISMATCH;
  248. }
  249. }
  250. if (ARGON2_MIN_SALT_LENGTH > context->saltlen) {
  251. return ARGON2_SALT_TOO_SHORT;
  252. }
  253. if (ARGON2_MAX_SALT_LENGTH < context->saltlen) {
  254. return ARGON2_SALT_TOO_LONG;
  255. }
  256. /* Validate secret (optional param) */
  257. if (NULL == context->secret) {
  258. if (0 != context->secretlen) {
  259. return ARGON2_SECRET_PTR_MISMATCH;
  260. }
  261. }
  262. else {
  263. if (ARGON2_MIN_SECRET > context->secretlen) {
  264. return ARGON2_SECRET_TOO_SHORT;
  265. }
  266. if (ARGON2_MAX_SECRET < context->secretlen) {
  267. return ARGON2_SECRET_TOO_LONG;
  268. }
  269. }
  270. /* Validate associated data (optional param) */
  271. if (NULL == context->ad) {
  272. if (0 != context->adlen) {
  273. return ARGON2_AD_PTR_MISMATCH;
  274. }
  275. }
  276. else {
  277. if (ARGON2_MIN_AD_LENGTH > context->adlen) {
  278. return ARGON2_AD_TOO_SHORT;
  279. }
  280. if (ARGON2_MAX_AD_LENGTH < context->adlen) {
  281. return ARGON2_AD_TOO_LONG;
  282. }
  283. }
  284. /* Validate memory cost */
  285. if (ARGON2_MIN_MEMORY > context->m_cost) {
  286. return ARGON2_MEMORY_TOO_LITTLE;
  287. }
  288. if (ARGON2_MAX_MEMORY < context->m_cost) {
  289. return ARGON2_MEMORY_TOO_MUCH;
  290. }
  291. if (context->m_cost < 8 * context->lanes) {
  292. return ARGON2_MEMORY_TOO_LITTLE;
  293. }
  294. /* Validate time cost */
  295. if (ARGON2_MIN_TIME > context->t_cost) {
  296. return ARGON2_TIME_TOO_SMALL;
  297. }
  298. if (ARGON2_MAX_TIME < context->t_cost) {
  299. return ARGON2_TIME_TOO_LARGE;
  300. }
  301. /* Validate lanes */
  302. if (ARGON2_MIN_LANES > context->lanes) {
  303. return ARGON2_LANES_TOO_FEW;
  304. }
  305. if (ARGON2_MAX_LANES < context->lanes) {
  306. return ARGON2_LANES_TOO_MANY;
  307. }
  308. /* Validate threads */
  309. if (ARGON2_MIN_THREADS > context->threads) {
  310. return ARGON2_THREADS_TOO_FEW;
  311. }
  312. if (ARGON2_MAX_THREADS < context->threads) {
  313. return ARGON2_THREADS_TOO_MANY;
  314. }
  315. if (NULL != context->allocate_cbk && NULL == context->free_cbk) {
  316. return ARGON2_FREE_MEMORY_CBK_NULL;
  317. }
  318. if (NULL == context->allocate_cbk && NULL != context->free_cbk) {
  319. return ARGON2_ALLOCATE_MEMORY_CBK_NULL;
  320. }
  321. return ARGON2_OK;
  322. }
  323. void fill_first_blocks(uint8_t *blockhash, const argon2_instance_t *instance) {
  324. uint32_t l;
  325. /* Make the first and second block in each lane as G(H0||0||i) or
  326. G(H0||1||i) */
  327. uint8_t blockhash_bytes[ARGON2_BLOCK_SIZE];
  328. for (l = 0; l < instance->lanes; ++l) {
  329. store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH, 0);
  330. store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH + 4, l);
  331. blake2b_long(blockhash_bytes, ARGON2_BLOCK_SIZE, blockhash,
  332. ARGON2_PREHASH_SEED_LENGTH);
  333. load_block(&instance->memory[l * instance->lane_length + 0],
  334. blockhash_bytes);
  335. store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH, 1);
  336. blake2b_long(blockhash_bytes, ARGON2_BLOCK_SIZE, blockhash,
  337. ARGON2_PREHASH_SEED_LENGTH);
  338. load_block(&instance->memory[l * instance->lane_length + 1],
  339. blockhash_bytes);
  340. }
  341. clear_internal_memory(blockhash_bytes, ARGON2_BLOCK_SIZE);
  342. }
  343. void initial_hash(uint8_t *blockhash, argon2_context *context, argon2_type type) {
  344. blake2b_state BlakeHash;
  345. uint8_t value[sizeof(uint32_t)];
  346. if (NULL == context || NULL == blockhash) {
  347. return;
  348. }
  349. blake2b_init(&BlakeHash, ARGON2_PREHASH_DIGEST_LENGTH);
  350. store32(&value, context->lanes);
  351. blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
  352. store32(&value, context->outlen);
  353. blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
  354. store32(&value, context->m_cost);
  355. blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
  356. store32(&value, context->t_cost);
  357. blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
  358. store32(&value, context->version);
  359. blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
  360. store32(&value, (uint32_t)type);
  361. blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
  362. store32(&value, context->pwdlen);
  363. blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
  364. if (context->pwd != NULL) {
  365. blake2b_update(&BlakeHash, (const uint8_t *)context->pwd,
  366. context->pwdlen);
  367. if (context->flags & ARGON2_FLAG_CLEAR_PASSWORD) {
  368. secure_wipe_memory(context->pwd, context->pwdlen);
  369. context->pwdlen = 0;
  370. }
  371. }
  372. store32(&value, context->saltlen);
  373. blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
  374. if (context->salt != NULL) {
  375. blake2b_update(&BlakeHash, (const uint8_t *)context->salt, context->saltlen);
  376. }
  377. store32(&value, context->secretlen);
  378. blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
  379. if (context->secret != NULL) {
  380. blake2b_update(&BlakeHash, (const uint8_t *)context->secret,
  381. context->secretlen);
  382. if (context->flags & ARGON2_FLAG_CLEAR_SECRET) {
  383. secure_wipe_memory(context->secret, context->secretlen);
  384. context->secretlen = 0;
  385. }
  386. }
  387. store32(&value, context->adlen);
  388. blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
  389. if (context->ad != NULL) {
  390. blake2b_update(&BlakeHash, (const uint8_t *)context->ad,
  391. context->adlen);
  392. }
  393. blake2b_final(&BlakeHash, blockhash, ARGON2_PREHASH_DIGEST_LENGTH);
  394. }
  395. int argon_initialize(argon2_instance_t *instance, argon2_context *context) {
  396. uint8_t blockhash[ARGON2_PREHASH_SEED_LENGTH];
  397. int result = ARGON2_OK;
  398. if (instance == NULL || context == NULL)
  399. return ARGON2_INCORRECT_PARAMETER;
  400. instance->context_ptr = context;
  401. /* 1. Memory allocation */
  402. /*result = allocate_memory(context, (uint8_t **)&(instance->memory), instance->memory_blocks, sizeof(block));
  403. if (result != ARGON2_OK) {
  404. return result;
  405. }*/
  406. /* 2. Initial hashing */
  407. /* H_0 + 8 extra bytes to produce the first blocks */
  408. /* uint8_t blockhash[ARGON2_PREHASH_SEED_LENGTH]; */
  409. /* Hashing all inputs */
  410. initial_hash(blockhash, context, instance->type);
  411. /* Zeroing 8 extra bytes */
  412. clear_internal_memory(blockhash + ARGON2_PREHASH_DIGEST_LENGTH,
  413. ARGON2_PREHASH_SEED_LENGTH -
  414. ARGON2_PREHASH_DIGEST_LENGTH);
  415. /* 3. Creating first blocks, we always have at least two blocks in a slice
  416. */
  417. fill_first_blocks(blockhash, instance);
  418. /* Clearing the hash */
  419. clear_internal_memory(blockhash, ARGON2_PREHASH_SEED_LENGTH);
  420. return ARGON2_OK;
  421. }