argon2_core.c 14 KB

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