argon2.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560
  1. /*
  2. * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. *
  9. * RFC 9106 Argon2 (see https://www.rfc-editor.org/rfc/rfc9106.txt)
  10. *
  11. */
  12. #include <stdlib.h>
  13. #include <stddef.h>
  14. #include <stdarg.h>
  15. #include <string.h>
  16. #include <openssl/e_os2.h>
  17. #include <openssl/evp.h>
  18. #include <openssl/objects.h>
  19. #include <openssl/crypto.h>
  20. #include <openssl/kdf.h>
  21. #include <openssl/err.h>
  22. #include <openssl/core_names.h>
  23. #include <openssl/params.h>
  24. #include <openssl/thread.h>
  25. #include <openssl/proverr.h>
  26. #include "internal/thread.h"
  27. #include "internal/numbers.h"
  28. #include "internal/endian.h"
  29. #include "crypto/evp.h"
  30. #include "prov/implementations.h"
  31. #include "prov/provider_ctx.h"
  32. #include "prov/providercommon.h"
  33. #include "prov/blake2.h"
  34. #if defined(OPENSSL_NO_DEFAULT_THREAD_POOL) && defined(OPENSSL_NO_THREAD_POOL)
  35. # define ARGON2_NO_THREADS
  36. #endif
  37. #if !defined(OPENSSL_THREADS)
  38. # define ARGON2_NO_THREADS
  39. #endif
  40. #ifndef OPENSSL_NO_ARGON2
  41. # define ARGON2_MIN_LANES 1u
  42. # define ARGON2_MAX_LANES 0xFFFFFFu
  43. # define ARGON2_MIN_THREADS 1u
  44. # define ARGON2_MAX_THREADS 0xFFFFFFu
  45. # define ARGON2_SYNC_POINTS 4u
  46. # define ARGON2_MIN_OUT_LENGTH 4u
  47. # define ARGON2_MAX_OUT_LENGTH 0xFFFFFFFFu
  48. # define ARGON2_MIN_MEMORY (2 * ARGON2_SYNC_POINTS)
  49. # define ARGON2_MIN(a, b) ((a) < (b) ? (a) : (b))
  50. # define ARGON2_MAX_MEMORY 0xFFFFFFFFu
  51. # define ARGON2_MIN_TIME 1u
  52. # define ARGON2_MAX_TIME 0xFFFFFFFFu
  53. # define ARGON2_MIN_PWD_LENGTH 0u
  54. # define ARGON2_MAX_PWD_LENGTH 0xFFFFFFFFu
  55. # define ARGON2_MIN_AD_LENGTH 0u
  56. # define ARGON2_MAX_AD_LENGTH 0xFFFFFFFFu
  57. # define ARGON2_MIN_SALT_LENGTH 8u
  58. # define ARGON2_MAX_SALT_LENGTH 0xFFFFFFFFu
  59. # define ARGON2_MIN_SECRET 0u
  60. # define ARGON2_MAX_SECRET 0xFFFFFFFFu
  61. # define ARGON2_BLOCK_SIZE 1024
  62. # define ARGON2_QWORDS_IN_BLOCK ((ARGON2_BLOCK_SIZE) / 8)
  63. # define ARGON2_OWORDS_IN_BLOCK ((ARGON2_BLOCK_SIZE) / 16)
  64. # define ARGON2_HWORDS_IN_BLOCK ((ARGON2_BLOCK_SIZE) / 32)
  65. # define ARGON2_512BIT_WORDS_IN_BLOCK ((ARGON2_BLOCK_SIZE) / 64)
  66. # define ARGON2_ADDRESSES_IN_BLOCK 128
  67. # define ARGON2_PREHASH_DIGEST_LENGTH 64
  68. # define ARGON2_PREHASH_SEED_LENGTH \
  69. (ARGON2_PREHASH_DIGEST_LENGTH + (2 * sizeof(uint32_t)))
  70. # define ARGON2_DEFAULT_OUTLEN 64u
  71. # define ARGON2_DEFAULT_T_COST 3u
  72. # define ARGON2_DEFAULT_M_COST ARGON2_MIN_MEMORY
  73. # define ARGON2_DEFAULT_LANES 1u
  74. # define ARGON2_DEFAULT_THREADS 1u
  75. # define ARGON2_DEFAULT_VERSION ARGON2_VERSION_NUMBER
  76. # undef G
  77. # define G(a, b, c, d) \
  78. do { \
  79. a = a + b + 2 * mul_lower(a, b); \
  80. d = rotr64(d ^ a, 32); \
  81. c = c + d + 2 * mul_lower(c, d); \
  82. b = rotr64(b ^ c, 24); \
  83. a = a + b + 2 * mul_lower(a, b); \
  84. d = rotr64(d ^ a, 16); \
  85. c = c + d + 2 * mul_lower(c, d); \
  86. b = rotr64(b ^ c, 63); \
  87. } while ((void)0, 0)
  88. # undef PERMUTATION_P
  89. # define PERMUTATION_P(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, \
  90. v12, v13, v14, v15) \
  91. do { \
  92. G(v0, v4, v8, v12); \
  93. G(v1, v5, v9, v13); \
  94. G(v2, v6, v10, v14); \
  95. G(v3, v7, v11, v15); \
  96. G(v0, v5, v10, v15); \
  97. G(v1, v6, v11, v12); \
  98. G(v2, v7, v8, v13); \
  99. G(v3, v4, v9, v14); \
  100. } while ((void)0, 0)
  101. # undef PERMUTATION_P_COLUMN
  102. # define PERMUTATION_P_COLUMN(x, i) \
  103. do { \
  104. uint64_t *base = &x[16 * i]; \
  105. PERMUTATION_P( \
  106. *base, *(base + 1), *(base + 2), *(base + 3), \
  107. *(base + 4), *(base + 5), *(base + 6), *(base + 7), \
  108. *(base + 8), *(base + 9), *(base + 10), *(base + 11), \
  109. *(base + 12), *(base + 13), *(base + 14), *(base + 15) \
  110. ); \
  111. } while ((void)0, 0)
  112. # undef PERMUTATION_P_ROW
  113. # define PERMUTATION_P_ROW(x, i) \
  114. do { \
  115. uint64_t *base = &x[2 * i]; \
  116. PERMUTATION_P( \
  117. *base, *(base + 1), *(base + 16), *(base + 17), \
  118. *(base + 32), *(base + 33), *(base + 48), *(base + 49), \
  119. *(base + 64), *(base + 65), *(base + 80), *(base + 81), \
  120. *(base + 96), *(base + 97), *(base + 112), *(base + 113) \
  121. ); \
  122. } while ((void)0, 0)
  123. typedef struct {
  124. uint64_t v[ARGON2_QWORDS_IN_BLOCK];
  125. } BLOCK;
  126. typedef enum {
  127. ARGON2_VERSION_10 = 0x10,
  128. ARGON2_VERSION_13 = 0x13,
  129. ARGON2_VERSION_NUMBER = ARGON2_VERSION_13
  130. } ARGON2_VERSION;
  131. typedef enum {
  132. ARGON2_D = 0,
  133. ARGON2_I = 1,
  134. ARGON2_ID = 2
  135. } ARGON2_TYPE;
  136. typedef struct {
  137. uint32_t pass;
  138. uint32_t lane;
  139. uint8_t slice;
  140. uint32_t index;
  141. } ARGON2_POS;
  142. typedef struct {
  143. void *provctx;
  144. uint32_t outlen;
  145. uint8_t *pwd;
  146. uint32_t pwdlen;
  147. uint8_t *salt;
  148. uint32_t saltlen;
  149. uint8_t *secret;
  150. uint32_t secretlen;
  151. uint8_t *ad;
  152. uint32_t adlen;
  153. uint32_t t_cost;
  154. uint32_t m_cost;
  155. uint32_t lanes;
  156. uint32_t threads;
  157. uint32_t version;
  158. uint32_t early_clean;
  159. ARGON2_TYPE type;
  160. BLOCK *memory;
  161. uint32_t passes;
  162. uint32_t memory_blocks;
  163. uint32_t segment_length;
  164. uint32_t lane_length;
  165. OSSL_LIB_CTX *libctx;
  166. EVP_MD *md;
  167. EVP_MAC *mac;
  168. char *propq;
  169. } KDF_ARGON2;
  170. typedef struct {
  171. ARGON2_POS pos;
  172. KDF_ARGON2 *ctx;
  173. } ARGON2_THREAD_DATA;
  174. static OSSL_FUNC_kdf_newctx_fn kdf_argon2i_new;
  175. static OSSL_FUNC_kdf_newctx_fn kdf_argon2d_new;
  176. static OSSL_FUNC_kdf_newctx_fn kdf_argon2id_new;
  177. static OSSL_FUNC_kdf_freectx_fn kdf_argon2_free;
  178. static OSSL_FUNC_kdf_reset_fn kdf_argon2_reset;
  179. static OSSL_FUNC_kdf_derive_fn kdf_argon2_derive;
  180. static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_argon2_settable_ctx_params;
  181. static OSSL_FUNC_kdf_set_ctx_params_fn kdf_argon2_set_ctx_params;
  182. static void kdf_argon2_init(KDF_ARGON2 *ctx, ARGON2_TYPE t);
  183. static void *kdf_argon2d_new(void *provctx);
  184. static void *kdf_argon2i_new(void *provctx);
  185. static void *kdf_argon2id_new(void *provctx);
  186. static void kdf_argon2_free(void *vctx);
  187. static int kdf_argon2_derive(void *vctx, unsigned char *out, size_t outlen,
  188. const OSSL_PARAM params[]);
  189. static void kdf_argon2_reset(void *vctx);
  190. static int kdf_argon2_ctx_set_threads(KDF_ARGON2 *ctx, uint32_t threads);
  191. static int kdf_argon2_ctx_set_lanes(KDF_ARGON2 *ctx, uint32_t lanes);
  192. static int kdf_argon2_ctx_set_t_cost(KDF_ARGON2 *ctx, uint32_t t_cost);
  193. static int kdf_argon2_ctx_set_m_cost(KDF_ARGON2 *ctx, uint32_t m_cost);
  194. static int kdf_argon2_ctx_set_out_length(KDF_ARGON2 *ctx, uint32_t outlen);
  195. static int kdf_argon2_ctx_set_secret(KDF_ARGON2 *ctx, const OSSL_PARAM *p);
  196. static int kdf_argon2_ctx_set_pwd(KDF_ARGON2 *ctx, const OSSL_PARAM *p);
  197. static int kdf_argon2_ctx_set_salt(KDF_ARGON2 *ctx, const OSSL_PARAM *p);
  198. static int kdf_argon2_ctx_set_ad(KDF_ARGON2 *ctx, const OSSL_PARAM *p);
  199. static int kdf_argon2_set_ctx_params(void *vctx, const OSSL_PARAM params[]);
  200. static int kdf_argon2_get_ctx_params(void *vctx, OSSL_PARAM params[]);
  201. static int kdf_argon2_ctx_set_version(KDF_ARGON2 *ctx, uint32_t version);
  202. static const OSSL_PARAM *kdf_argon2_settable_ctx_params(ossl_unused void *ctx,
  203. ossl_unused void *p_ctx);
  204. static const OSSL_PARAM *kdf_argon2_gettable_ctx_params(ossl_unused void *ctx,
  205. ossl_unused void *p_ctx);
  206. static ossl_inline uint64_t load64(const uint8_t *src);
  207. static ossl_inline void store32(uint8_t *dst, uint32_t w);
  208. static ossl_inline void store64(uint8_t *dst, uint64_t w);
  209. static ossl_inline uint64_t rotr64(const uint64_t w, const unsigned int c);
  210. static ossl_inline uint64_t mul_lower(uint64_t x, uint64_t y);
  211. static void init_block_value(BLOCK *b, uint8_t in);
  212. static void copy_block(BLOCK *dst, const BLOCK *src);
  213. static void xor_block(BLOCK *dst, const BLOCK *src);
  214. static void load_block(BLOCK *dst, const void *input);
  215. static void store_block(void *output, const BLOCK *src);
  216. static void fill_first_blocks(uint8_t *blockhash, const KDF_ARGON2 *ctx);
  217. static void fill_block(const BLOCK *prev, const BLOCK *ref, BLOCK *next,
  218. int with_xor);
  219. static void next_addresses(BLOCK *address_block, BLOCK *input_block,
  220. const BLOCK *zero_block);
  221. static int data_indep_addressing(const KDF_ARGON2 *ctx, uint32_t pass,
  222. uint8_t slice);
  223. static uint32_t index_alpha(const KDF_ARGON2 *ctx, uint32_t pass,
  224. uint8_t slice, uint32_t index,
  225. uint32_t pseudo_rand, int same_lane);
  226. static void fill_segment(const KDF_ARGON2 *ctx, uint32_t pass, uint32_t lane,
  227. uint8_t slice);
  228. # if !defined(ARGON2_NO_THREADS)
  229. static uint32_t fill_segment_thr(void *thread_data);
  230. static int fill_mem_blocks_mt(KDF_ARGON2 *ctx);
  231. # endif
  232. static int fill_mem_blocks_st(KDF_ARGON2 *ctx);
  233. static ossl_inline int fill_memory_blocks(KDF_ARGON2 *ctx);
  234. static void initial_hash(uint8_t *blockhash, KDF_ARGON2 *ctx);
  235. static int initialize(KDF_ARGON2 *ctx);
  236. static void finalize(const KDF_ARGON2 *ctx, void *out);
  237. static int blake2b(EVP_MD *md, EVP_MAC *mac, void *out, size_t outlen,
  238. const void *in, size_t inlen, const void *key,
  239. size_t keylen);
  240. static int blake2b_long(EVP_MD *md, EVP_MAC *mac, unsigned char *out,
  241. size_t outlen, const void *in, size_t inlen);
  242. static ossl_inline uint64_t load64(const uint8_t *src)
  243. {
  244. return
  245. (((uint64_t)src[0]) << 0)
  246. | (((uint64_t)src[1]) << 8)
  247. | (((uint64_t)src[2]) << 16)
  248. | (((uint64_t)src[3]) << 24)
  249. | (((uint64_t)src[4]) << 32)
  250. | (((uint64_t)src[5]) << 40)
  251. | (((uint64_t)src[6]) << 48)
  252. | (((uint64_t)src[7]) << 56);
  253. }
  254. static ossl_inline void store32(uint8_t *dst, uint32_t w)
  255. {
  256. dst[0] = (uint8_t)(w >> 0);
  257. dst[1] = (uint8_t)(w >> 8);
  258. dst[2] = (uint8_t)(w >> 16);
  259. dst[3] = (uint8_t)(w >> 24);
  260. }
  261. static ossl_inline void store64(uint8_t *dst, uint64_t w)
  262. {
  263. dst[0] = (uint8_t)(w >> 0);
  264. dst[1] = (uint8_t)(w >> 8);
  265. dst[2] = (uint8_t)(w >> 16);
  266. dst[3] = (uint8_t)(w >> 24);
  267. dst[4] = (uint8_t)(w >> 32);
  268. dst[5] = (uint8_t)(w >> 40);
  269. dst[6] = (uint8_t)(w >> 48);
  270. dst[7] = (uint8_t)(w >> 56);
  271. }
  272. static ossl_inline uint64_t rotr64(const uint64_t w, const unsigned int c)
  273. {
  274. return (w >> c) | (w << (64 - c));
  275. }
  276. static ossl_inline uint64_t mul_lower(uint64_t x, uint64_t y)
  277. {
  278. const uint64_t m = 0xFFFFFFFFUL;
  279. return (x & m) * (y & m);
  280. }
  281. static void init_block_value(BLOCK *b, uint8_t in)
  282. {
  283. memset(b->v, in, sizeof(b->v));
  284. }
  285. static void copy_block(BLOCK *dst, const BLOCK *src)
  286. {
  287. memcpy(dst->v, src->v, sizeof(uint64_t) * ARGON2_QWORDS_IN_BLOCK);
  288. }
  289. static void xor_block(BLOCK *dst, const BLOCK *src)
  290. {
  291. int i;
  292. for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i)
  293. dst->v[i] ^= src->v[i];
  294. }
  295. static void load_block(BLOCK *dst, const void *input)
  296. {
  297. unsigned i;
  298. for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i)
  299. dst->v[i] = load64((const uint8_t *)input + i * sizeof(dst->v[i]));
  300. }
  301. static void store_block(void *output, const BLOCK *src)
  302. {
  303. unsigned i;
  304. for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i)
  305. store64((uint8_t *)output + i * sizeof(src->v[i]), src->v[i]);
  306. }
  307. static void fill_first_blocks(uint8_t *blockhash, const KDF_ARGON2 *ctx)
  308. {
  309. uint32_t l;
  310. uint8_t blockhash_bytes[ARGON2_BLOCK_SIZE];
  311. /*
  312. * Make the first and second block in each lane as G(H0||0||i)
  313. * or G(H0||1||i).
  314. */
  315. for (l = 0; l < ctx->lanes; ++l) {
  316. store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH, 0);
  317. store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH + 4, l);
  318. blake2b_long(ctx->md, ctx->mac, blockhash_bytes, ARGON2_BLOCK_SIZE,
  319. blockhash, ARGON2_PREHASH_SEED_LENGTH);
  320. load_block(&ctx->memory[l * ctx->lane_length + 0],
  321. blockhash_bytes);
  322. store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH, 1);
  323. blake2b_long(ctx->md, ctx->mac, blockhash_bytes, ARGON2_BLOCK_SIZE,
  324. blockhash, ARGON2_PREHASH_SEED_LENGTH);
  325. load_block(&ctx->memory[l * ctx->lane_length + 1],
  326. blockhash_bytes);
  327. }
  328. OPENSSL_cleanse(blockhash_bytes, ARGON2_BLOCK_SIZE);
  329. }
  330. static void fill_block(const BLOCK *prev, const BLOCK *ref,
  331. BLOCK *next, int with_xor)
  332. {
  333. BLOCK blockR, tmp;
  334. unsigned i;
  335. copy_block(&blockR, ref);
  336. xor_block(&blockR, prev);
  337. copy_block(&tmp, &blockR);
  338. if (with_xor)
  339. xor_block(&tmp, next);
  340. for (i = 0; i < 8; ++i)
  341. PERMUTATION_P_COLUMN(blockR.v, i);
  342. for (i = 0; i < 8; ++i)
  343. PERMUTATION_P_ROW(blockR.v, i);
  344. copy_block(next, &tmp);
  345. xor_block(next, &blockR);
  346. }
  347. static void next_addresses(BLOCK *address_block, BLOCK *input_block,
  348. const BLOCK *zero_block)
  349. {
  350. input_block->v[6]++;
  351. fill_block(zero_block, input_block, address_block, 0);
  352. fill_block(zero_block, address_block, address_block, 0);
  353. }
  354. static int data_indep_addressing(const KDF_ARGON2 *ctx, uint32_t pass,
  355. uint8_t slice)
  356. {
  357. switch (ctx->type) {
  358. case ARGON2_I:
  359. return 1;
  360. case ARGON2_ID:
  361. return (pass == 0) && (slice < ARGON2_SYNC_POINTS / 2);
  362. case ARGON2_D:
  363. default:
  364. return 0;
  365. }
  366. }
  367. /*
  368. * Pass 0 (pass = 0):
  369. * This lane: all already finished segments plus already constructed blocks
  370. * in this segment
  371. * Other lanes: all already finished segments
  372. *
  373. * Pass 1+:
  374. * This lane: (SYNC_POINTS - 1) last segments plus already constructed
  375. * blocks in this segment
  376. * Other lanes: (SYNC_POINTS - 1) last segments
  377. */
  378. static uint32_t index_alpha(const KDF_ARGON2 *ctx, uint32_t pass,
  379. uint8_t slice, uint32_t index,
  380. uint32_t pseudo_rand, int same_lane)
  381. {
  382. uint32_t ref_area_sz;
  383. uint64_t rel_pos;
  384. uint32_t start_pos, abs_pos;
  385. start_pos = 0;
  386. switch (pass) {
  387. case 0:
  388. if (slice == 0)
  389. ref_area_sz = index - 1;
  390. else if (same_lane)
  391. ref_area_sz = slice * ctx->segment_length + index - 1;
  392. else
  393. ref_area_sz = slice * ctx->segment_length +
  394. ((index == 0) ? (-1) : 0);
  395. break;
  396. default:
  397. if (same_lane)
  398. ref_area_sz = ctx->lane_length - ctx->segment_length + index - 1;
  399. else
  400. ref_area_sz = ctx->lane_length - ctx->segment_length +
  401. ((index == 0) ? (-1) : 0);
  402. if (slice != ARGON2_SYNC_POINTS - 1)
  403. start_pos = (slice + 1) * ctx->segment_length;
  404. break;
  405. }
  406. rel_pos = pseudo_rand;
  407. rel_pos = rel_pos * rel_pos >> 32;
  408. rel_pos = ref_area_sz - 1 - (ref_area_sz * rel_pos >> 32);
  409. abs_pos = (start_pos + rel_pos) % ctx->lane_length;
  410. return abs_pos;
  411. }
  412. static void fill_segment(const KDF_ARGON2 *ctx, uint32_t pass, uint32_t lane,
  413. uint8_t slice)
  414. {
  415. BLOCK *ref_block = NULL, *curr_block = NULL;
  416. BLOCK address_block, input_block, zero_block;
  417. uint64_t rnd, ref_index, ref_lane;
  418. uint32_t prev_offset;
  419. uint32_t start_idx;
  420. uint32_t j;
  421. uint32_t curr_offset; /* Offset of the current block */
  422. memset(&input_block, 0, sizeof(BLOCK));
  423. if (ctx == NULL)
  424. return;
  425. if (data_indep_addressing(ctx, pass, slice)) {
  426. init_block_value(&zero_block, 0);
  427. init_block_value(&input_block, 0);
  428. input_block.v[0] = pass;
  429. input_block.v[1] = lane;
  430. input_block.v[2] = slice;
  431. input_block.v[3] = ctx->memory_blocks;
  432. input_block.v[4] = ctx->passes;
  433. input_block.v[5] = ctx->type;
  434. }
  435. start_idx = 0;
  436. /* We've generated the first two blocks. Generate the 1st block of addrs. */
  437. if ((pass == 0) && (slice == 0)) {
  438. start_idx = 2;
  439. if (data_indep_addressing(ctx, pass, slice))
  440. next_addresses(&address_block, &input_block, &zero_block);
  441. }
  442. curr_offset = lane * ctx->lane_length + slice * ctx->segment_length
  443. + start_idx;
  444. if ((curr_offset % ctx->lane_length) == 0)
  445. prev_offset = curr_offset + ctx->lane_length - 1;
  446. else
  447. prev_offset = curr_offset - 1;
  448. for (j = start_idx; j < ctx->segment_length; ++j, ++curr_offset, ++prev_offset) {
  449. if (curr_offset % ctx->lane_length == 1)
  450. prev_offset = curr_offset - 1;
  451. /* Taking pseudo-random value from the previous block. */
  452. if (data_indep_addressing(ctx, pass, slice)) {
  453. if (j % ARGON2_ADDRESSES_IN_BLOCK == 0)
  454. next_addresses(&address_block, &input_block, &zero_block);
  455. rnd = address_block.v[j % ARGON2_ADDRESSES_IN_BLOCK];
  456. } else {
  457. rnd = ctx->memory[prev_offset].v[0];
  458. }
  459. /* Computing the lane of the reference block */
  460. ref_lane = ((rnd >> 32)) % ctx->lanes;
  461. /* Can not reference other lanes yet */
  462. if ((pass == 0) && (slice == 0))
  463. ref_lane = lane;
  464. /* Computing the number of possible reference block within the lane. */
  465. ref_index = index_alpha(ctx, pass, slice, j, rnd & 0xFFFFFFFF,
  466. ref_lane == lane);
  467. /* Creating a new block */
  468. ref_block = ctx->memory + ctx->lane_length * ref_lane + ref_index;
  469. curr_block = ctx->memory + curr_offset;
  470. if (ARGON2_VERSION_10 == ctx->version) {
  471. /* Version 1.2.1 and earlier: overwrite, not XOR */
  472. fill_block(ctx->memory + prev_offset, ref_block, curr_block, 0);
  473. continue;
  474. }
  475. fill_block(ctx->memory + prev_offset, ref_block, curr_block,
  476. pass == 0 ? 0 : 1);
  477. }
  478. }
  479. # if !defined(ARGON2_NO_THREADS)
  480. static uint32_t fill_segment_thr(void *thread_data)
  481. {
  482. ARGON2_THREAD_DATA *my_data;
  483. my_data = (ARGON2_THREAD_DATA *) thread_data;
  484. fill_segment(my_data->ctx, my_data->pos.pass, my_data->pos.lane,
  485. my_data->pos.slice);
  486. return 0;
  487. }
  488. static int fill_mem_blocks_mt(KDF_ARGON2 *ctx)
  489. {
  490. uint32_t r, s, l, ll;
  491. void **t;
  492. ARGON2_THREAD_DATA *t_data;
  493. t = OPENSSL_zalloc(sizeof(void *)*ctx->lanes);
  494. t_data = OPENSSL_zalloc(ctx->lanes * sizeof(ARGON2_THREAD_DATA));
  495. if (t == NULL || t_data == NULL)
  496. goto fail;
  497. for (r = 0; r < ctx->passes; ++r) {
  498. for (s = 0; s < ARGON2_SYNC_POINTS; ++s) {
  499. for (l = 0; l < ctx->lanes; ++l) {
  500. ARGON2_POS p;
  501. if (l >= ctx->threads) {
  502. if (ossl_crypto_thread_join(t[l - ctx->threads], NULL) == 0)
  503. goto fail;
  504. if (ossl_crypto_thread_clean(t[l - ctx->threads]) == 0)
  505. goto fail;
  506. t[l] = NULL;
  507. }
  508. p.pass = r;
  509. p.lane = l;
  510. p.slice = (uint8_t)s;
  511. p.index = 0;
  512. t_data[l].ctx = ctx;
  513. memcpy(&(t_data[l].pos), &p, sizeof(ARGON2_POS));
  514. t[l] = ossl_crypto_thread_start(ctx->libctx, &fill_segment_thr,
  515. (void *) &t_data[l]);
  516. if (t[l] == NULL) {
  517. for (ll = 0; ll < l; ++ll) {
  518. if (ossl_crypto_thread_join(t[ll], NULL) == 0)
  519. goto fail;
  520. if (ossl_crypto_thread_clean(t[ll]) == 0)
  521. goto fail;
  522. t[ll] = NULL;
  523. }
  524. goto fail;
  525. }
  526. }
  527. for (l = ctx->lanes - ctx->threads; l < ctx->lanes; ++l) {
  528. if (ossl_crypto_thread_join(t[l], NULL) == 0)
  529. goto fail;
  530. if (ossl_crypto_thread_clean(t[l]) == 0)
  531. goto fail;
  532. t[l] = NULL;
  533. }
  534. }
  535. }
  536. OPENSSL_free(t_data);
  537. OPENSSL_free(t);
  538. return 1;
  539. fail:
  540. if (t_data != NULL)
  541. OPENSSL_free(t_data);
  542. if (t != NULL)
  543. OPENSSL_free(t);
  544. return 0;
  545. }
  546. # endif /* !defined(ARGON2_NO_THREADS) */
  547. static int fill_mem_blocks_st(KDF_ARGON2 *ctx)
  548. {
  549. uint32_t r, s, l;
  550. for (r = 0; r < ctx->passes; ++r)
  551. for (s = 0; s < ARGON2_SYNC_POINTS; ++s)
  552. for (l = 0; l < ctx->lanes; ++l)
  553. fill_segment(ctx, r, l, s);
  554. return 1;
  555. }
  556. static ossl_inline int fill_memory_blocks(KDF_ARGON2 *ctx)
  557. {
  558. # if !defined(ARGON2_NO_THREADS)
  559. return ctx->threads == 1 ? fill_mem_blocks_st(ctx) : fill_mem_blocks_mt(ctx);
  560. # else
  561. return ctx->threads == 1 ? fill_mem_blocks_st(ctx) : 0;
  562. # endif
  563. }
  564. static void initial_hash(uint8_t *blockhash, KDF_ARGON2 *ctx)
  565. {
  566. EVP_MD_CTX *mdctx;
  567. uint8_t value[sizeof(uint32_t)];
  568. unsigned int tmp;
  569. uint32_t args[7];
  570. if (ctx == NULL || blockhash == NULL)
  571. return;
  572. args[0] = ctx->lanes;
  573. args[1] = ctx->outlen;
  574. args[2] = ctx->m_cost;
  575. args[3] = ctx->t_cost;
  576. args[4] = ctx->version;
  577. args[5] = (uint32_t) ctx->type;
  578. args[6] = ctx->pwdlen;
  579. mdctx = EVP_MD_CTX_create();
  580. if (mdctx == NULL || EVP_DigestInit_ex(mdctx, ctx->md, NULL) != 1)
  581. goto fail;
  582. for (tmp = 0; tmp < sizeof(args) / sizeof(uint32_t); ++tmp) {
  583. store32((uint8_t *) &value, args[tmp]);
  584. if (EVP_DigestUpdate(mdctx, &value, sizeof(value)) != 1)
  585. goto fail;
  586. }
  587. if (ctx->pwd != NULL) {
  588. if (EVP_DigestUpdate(mdctx, ctx->pwd, ctx->pwdlen) != 1)
  589. goto fail;
  590. if (ctx->early_clean) {
  591. OPENSSL_cleanse(ctx->pwd, ctx->pwdlen);
  592. ctx->pwdlen = 0;
  593. }
  594. }
  595. store32((uint8_t *) &value, ctx->saltlen);
  596. if (EVP_DigestUpdate(mdctx, &value, sizeof(value)) != 1)
  597. goto fail;
  598. if (ctx->salt != NULL)
  599. if (EVP_DigestUpdate(mdctx, ctx->salt, ctx->saltlen) != 1)
  600. goto fail;
  601. store32((uint8_t *) &value, ctx->secretlen);
  602. if (EVP_DigestUpdate(mdctx, &value, sizeof(value)) != 1)
  603. goto fail;
  604. if (ctx->secret != NULL) {
  605. if (EVP_DigestUpdate(mdctx, ctx->secret, ctx->secretlen) != 1)
  606. goto fail;
  607. if (ctx->early_clean) {
  608. OPENSSL_cleanse(ctx->secret, ctx->secretlen);
  609. ctx->secretlen = 0;
  610. }
  611. }
  612. store32((uint8_t *) &value, ctx->adlen);
  613. if (EVP_DigestUpdate(mdctx, &value, sizeof(value)) != 1)
  614. goto fail;
  615. if (ctx->ad != NULL)
  616. if (EVP_DigestUpdate(mdctx, ctx->ad, ctx->adlen) != 1)
  617. goto fail;
  618. tmp = ARGON2_PREHASH_DIGEST_LENGTH;
  619. if (EVP_DigestFinal_ex(mdctx, blockhash, &tmp) != 1)
  620. goto fail;
  621. fail:
  622. EVP_MD_CTX_destroy(mdctx);
  623. }
  624. static int initialize(KDF_ARGON2 *ctx)
  625. {
  626. uint8_t blockhash[ARGON2_PREHASH_SEED_LENGTH];
  627. if (ctx == NULL)
  628. return 0;
  629. if (ctx->memory_blocks * sizeof(BLOCK) / sizeof(BLOCK) != ctx->memory_blocks)
  630. return 0;
  631. if (ctx->type != ARGON2_D)
  632. ctx->memory = OPENSSL_secure_zalloc(ctx->memory_blocks *
  633. sizeof(BLOCK));
  634. else
  635. ctx->memory = OPENSSL_zalloc(ctx->memory_blocks *
  636. sizeof(BLOCK));
  637. if (ctx->memory == NULL) {
  638. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_MEMORY_SIZE,
  639. "cannot allocate required memory");
  640. return 0;
  641. }
  642. initial_hash(blockhash, ctx);
  643. OPENSSL_cleanse(blockhash + ARGON2_PREHASH_DIGEST_LENGTH,
  644. ARGON2_PREHASH_SEED_LENGTH - ARGON2_PREHASH_DIGEST_LENGTH);
  645. fill_first_blocks(blockhash, ctx);
  646. OPENSSL_cleanse(blockhash, ARGON2_PREHASH_SEED_LENGTH);
  647. return 1;
  648. }
  649. static void finalize(const KDF_ARGON2 *ctx, void *out)
  650. {
  651. BLOCK blockhash;
  652. uint8_t blockhash_bytes[ARGON2_BLOCK_SIZE];
  653. uint32_t last_block_in_lane;
  654. uint32_t l;
  655. if (ctx == NULL)
  656. return;
  657. copy_block(&blockhash, ctx->memory + ctx->lane_length - 1);
  658. /* XOR the last blocks */
  659. for (l = 1; l < ctx->lanes; ++l) {
  660. last_block_in_lane = l * ctx->lane_length + (ctx->lane_length - 1);
  661. xor_block(&blockhash, ctx->memory + last_block_in_lane);
  662. }
  663. /* Hash the result */
  664. store_block(blockhash_bytes, &blockhash);
  665. blake2b_long(ctx->md, ctx->mac, out, ctx->outlen, blockhash_bytes,
  666. ARGON2_BLOCK_SIZE);
  667. OPENSSL_cleanse(blockhash.v, ARGON2_BLOCK_SIZE);
  668. OPENSSL_cleanse(blockhash_bytes, ARGON2_BLOCK_SIZE);
  669. if (ctx->type != ARGON2_D)
  670. OPENSSL_secure_clear_free(ctx->memory,
  671. ctx->memory_blocks * sizeof(BLOCK));
  672. else
  673. OPENSSL_clear_free(ctx->memory,
  674. ctx->memory_blocks * sizeof(BLOCK));
  675. }
  676. static int blake2b_mac(EVP_MAC *mac, void *out, size_t outlen, const void *in,
  677. size_t inlen, const void *key, size_t keylen)
  678. {
  679. int ret = 0;
  680. size_t par_n = 0, out_written;
  681. EVP_MAC_CTX *ctx = NULL;
  682. OSSL_PARAM par[3];
  683. if ((ctx = EVP_MAC_CTX_new(mac)) == NULL)
  684. goto fail;
  685. par[par_n++] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
  686. (void *) key, keylen);
  687. par[par_n++] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &outlen);
  688. par[par_n++] = OSSL_PARAM_construct_end();
  689. ret = EVP_MAC_CTX_set_params(ctx, par) == 1
  690. && EVP_MAC_init(ctx, NULL, 0, NULL) == 1
  691. && EVP_MAC_update(ctx, in, inlen) == 1
  692. && EVP_MAC_final(ctx, out, (size_t *) &out_written, outlen) == 1;
  693. fail:
  694. EVP_MAC_CTX_free(ctx);
  695. return ret;
  696. }
  697. static int blake2b_md(EVP_MD *md, void *out, size_t outlen, const void *in,
  698. size_t inlen)
  699. {
  700. int ret = 0;
  701. EVP_MD_CTX *ctx = NULL;
  702. OSSL_PARAM par[2];
  703. if ((ctx = EVP_MD_CTX_create()) == NULL)
  704. return 0;
  705. par[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &outlen);
  706. par[1] = OSSL_PARAM_construct_end();
  707. ret = EVP_DigestInit_ex2(ctx, md, par) == 1
  708. && EVP_DigestUpdate(ctx, in, inlen) == 1
  709. && EVP_DigestFinal_ex(ctx, out, NULL) == 1;
  710. EVP_MD_CTX_free(ctx);
  711. return ret;
  712. }
  713. static int blake2b(EVP_MD *md, EVP_MAC *mac, void *out, size_t outlen,
  714. const void *in, size_t inlen, const void *key, size_t keylen)
  715. {
  716. if (out == NULL || outlen == 0)
  717. return 0;
  718. if (key == NULL || keylen == 0)
  719. return blake2b_md(md, out, outlen, in, inlen);
  720. return blake2b_mac(mac, out, outlen, in, inlen, key, keylen);
  721. }
  722. static int blake2b_long(EVP_MD *md, EVP_MAC *mac, unsigned char *out,
  723. size_t outlen, const void *in, size_t inlen)
  724. {
  725. int ret = 0;
  726. EVP_MD_CTX *ctx = NULL;
  727. uint32_t outlen_curr;
  728. uint8_t outbuf[BLAKE2B_OUTBYTES];
  729. uint8_t inbuf[BLAKE2B_OUTBYTES];
  730. uint8_t outlen_bytes[sizeof(uint32_t)] = {0};
  731. OSSL_PARAM par[2];
  732. size_t outlen_md;
  733. if (out == NULL || outlen == 0)
  734. return 0;
  735. /* Ensure little-endian byte order */
  736. store32(outlen_bytes, (uint32_t)outlen);
  737. if ((ctx = EVP_MD_CTX_create()) == NULL)
  738. return 0;
  739. outlen_md = (outlen <= BLAKE2B_OUTBYTES) ? outlen : BLAKE2B_OUTBYTES;
  740. par[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &outlen_md);
  741. par[1] = OSSL_PARAM_construct_end();
  742. ret = EVP_DigestInit_ex2(ctx, md, par) == 1
  743. && EVP_DigestUpdate(ctx, outlen_bytes, sizeof(outlen_bytes)) == 1
  744. && EVP_DigestUpdate(ctx, in, inlen) == 1
  745. && EVP_DigestFinal_ex(ctx, (outlen > BLAKE2B_OUTBYTES) ? outbuf : out,
  746. NULL) == 1;
  747. if (ret == 0)
  748. goto fail;
  749. if (outlen > BLAKE2B_OUTBYTES) {
  750. memcpy(out, outbuf, BLAKE2B_OUTBYTES / 2);
  751. out += BLAKE2B_OUTBYTES / 2;
  752. outlen_curr = (uint32_t) outlen - BLAKE2B_OUTBYTES / 2;
  753. while (outlen_curr > BLAKE2B_OUTBYTES) {
  754. memcpy(inbuf, outbuf, BLAKE2B_OUTBYTES);
  755. if (blake2b(md, mac, outbuf, BLAKE2B_OUTBYTES, inbuf,
  756. BLAKE2B_OUTBYTES, NULL, 0) != 1)
  757. goto fail;
  758. memcpy(out, outbuf, BLAKE2B_OUTBYTES / 2);
  759. out += BLAKE2B_OUTBYTES / 2;
  760. outlen_curr -= BLAKE2B_OUTBYTES / 2;
  761. }
  762. memcpy(inbuf, outbuf, BLAKE2B_OUTBYTES);
  763. if (blake2b(md, mac, outbuf, outlen_curr, inbuf, BLAKE2B_OUTBYTES,
  764. NULL, 0) != 1)
  765. goto fail;
  766. memcpy(out, outbuf, outlen_curr);
  767. }
  768. ret = 1;
  769. fail:
  770. EVP_MD_CTX_free(ctx);
  771. return ret;
  772. }
  773. static void kdf_argon2_init(KDF_ARGON2 *c, ARGON2_TYPE type)
  774. {
  775. OSSL_LIB_CTX *libctx;
  776. libctx = c->libctx;
  777. memset(c, 0, sizeof(*c));
  778. c->libctx = libctx;
  779. c->outlen = ARGON2_DEFAULT_OUTLEN;
  780. c->t_cost = ARGON2_DEFAULT_T_COST;
  781. c->m_cost = ARGON2_DEFAULT_M_COST;
  782. c->lanes = ARGON2_DEFAULT_LANES;
  783. c->threads = ARGON2_DEFAULT_THREADS;
  784. c->version = ARGON2_DEFAULT_VERSION;
  785. c->type = type;
  786. }
  787. static void *kdf_argon2d_new(void *provctx)
  788. {
  789. KDF_ARGON2 *ctx;
  790. if (!ossl_prov_is_running())
  791. return NULL;
  792. ctx = OPENSSL_zalloc(sizeof(*ctx));
  793. if (ctx == NULL) {
  794. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  795. return NULL;
  796. }
  797. ctx->libctx = PROV_LIBCTX_OF(provctx);
  798. kdf_argon2_init(ctx, ARGON2_D);
  799. return ctx;
  800. }
  801. static void *kdf_argon2i_new(void *provctx)
  802. {
  803. KDF_ARGON2 *ctx;
  804. if (!ossl_prov_is_running())
  805. return NULL;
  806. ctx = OPENSSL_zalloc(sizeof(*ctx));
  807. if (ctx == NULL) {
  808. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  809. return NULL;
  810. }
  811. ctx->libctx = PROV_LIBCTX_OF(provctx);
  812. kdf_argon2_init(ctx, ARGON2_I);
  813. return ctx;
  814. }
  815. static void *kdf_argon2id_new(void *provctx)
  816. {
  817. KDF_ARGON2 *ctx;
  818. if (!ossl_prov_is_running())
  819. return NULL;
  820. ctx = OPENSSL_zalloc(sizeof(*ctx));
  821. if (ctx == NULL) {
  822. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  823. return NULL;
  824. }
  825. ctx->libctx = PROV_LIBCTX_OF(provctx);
  826. kdf_argon2_init(ctx, ARGON2_ID);
  827. return ctx;
  828. }
  829. static void kdf_argon2_free(void *vctx)
  830. {
  831. KDF_ARGON2 *ctx = (KDF_ARGON2 *)vctx;
  832. if (ctx == NULL)
  833. return;
  834. if (ctx->pwd != NULL)
  835. OPENSSL_clear_free(ctx->pwd, ctx->pwdlen);
  836. if (ctx->salt != NULL)
  837. OPENSSL_clear_free(ctx->salt, ctx->saltlen);
  838. if (ctx->secret != NULL)
  839. OPENSSL_clear_free(ctx->secret, ctx->secretlen);
  840. if (ctx->ad != NULL)
  841. OPENSSL_clear_free(ctx->ad, ctx->adlen);
  842. EVP_MD_free(ctx->md);
  843. EVP_MAC_free(ctx->mac);
  844. OPENSSL_free(ctx->propq);
  845. memset(ctx, 0, sizeof(*ctx));
  846. OPENSSL_free(ctx);
  847. }
  848. static int kdf_argon2_derive(void *vctx, unsigned char *out, size_t outlen,
  849. const OSSL_PARAM params[])
  850. {
  851. KDF_ARGON2 *ctx;
  852. uint32_t memory_blocks, segment_length;
  853. ctx = (KDF_ARGON2 *)vctx;
  854. if (!ossl_prov_is_running() || !kdf_argon2_set_ctx_params(vctx, params))
  855. return 0;
  856. if (ctx->mac == NULL)
  857. ctx->mac = EVP_MAC_fetch(ctx->libctx, "blake2bmac", ctx->propq);
  858. if (ctx->mac == NULL) {
  859. ERR_raise_data(ERR_LIB_PROV, PROV_R_MISSING_MAC,
  860. "cannot fetch blake2bmac");
  861. return 0;
  862. }
  863. if (ctx->md == NULL)
  864. ctx->md = EVP_MD_fetch(ctx->libctx, "blake2b512", ctx->propq);
  865. if (ctx->md == NULL) {
  866. ERR_raise_data(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST,
  867. "cannot fetch blake2b512");
  868. return 0;
  869. }
  870. if (ctx->salt == NULL || ctx->saltlen == 0) {
  871. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
  872. return 0;
  873. }
  874. if (outlen != ctx->outlen) {
  875. if (OSSL_PARAM_locate((OSSL_PARAM *)params, "size") != NULL) {
  876. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  877. return 0;
  878. }
  879. if (!kdf_argon2_ctx_set_out_length(ctx, (uint32_t) outlen))
  880. return 0;
  881. }
  882. switch (ctx->type) {
  883. case ARGON2_D:
  884. case ARGON2_I:
  885. case ARGON2_ID:
  886. break;
  887. default:
  888. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_MODE, "invalid Argon2 type");
  889. return 0;
  890. }
  891. if (ctx->threads > 1) {
  892. # ifdef ARGON2_NO_THREADS
  893. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_THREAD_POOL_SIZE,
  894. "requested %u threads, single-threaded mode supported only",
  895. ctx->threads);
  896. return 0;
  897. # else
  898. if (ctx->threads > ossl_get_avail_threads(ctx->libctx)) {
  899. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_THREAD_POOL_SIZE,
  900. "requested %u threads, available: 1",
  901. ossl_get_avail_threads(ctx->libctx));
  902. return 0;
  903. }
  904. # endif
  905. if (ctx->threads > ctx->lanes) {
  906. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_THREAD_POOL_SIZE,
  907. "requested more threads (%u) than lanes (%u)",
  908. ctx->threads, ctx->lanes);
  909. return 0;
  910. }
  911. }
  912. if (ctx->m_cost < 8 * ctx->lanes) {
  913. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_MEMORY_SIZE,
  914. "m_cost must be greater or equal than 8 times the number of lanes");
  915. return 0;
  916. }
  917. memory_blocks = ctx->m_cost;
  918. if (memory_blocks < 2 * ARGON2_SYNC_POINTS * ctx->lanes)
  919. memory_blocks = 2 * ARGON2_SYNC_POINTS * ctx->lanes;
  920. /* Ensure that all segments have equal length */
  921. segment_length = memory_blocks / (ctx->lanes * ARGON2_SYNC_POINTS);
  922. memory_blocks = segment_length * (ctx->lanes * ARGON2_SYNC_POINTS);
  923. ctx->memory = NULL;
  924. ctx->memory_blocks = memory_blocks;
  925. ctx->segment_length = segment_length;
  926. ctx->passes = ctx->t_cost;
  927. ctx->lane_length = segment_length * ARGON2_SYNC_POINTS;
  928. if (initialize(ctx) != 1)
  929. return 0;
  930. if (fill_memory_blocks(ctx) != 1)
  931. return 0;
  932. finalize(ctx, out);
  933. return 1;
  934. }
  935. static void kdf_argon2_reset(void *vctx)
  936. {
  937. OSSL_LIB_CTX *libctx;
  938. KDF_ARGON2 *ctx;
  939. ARGON2_TYPE type;
  940. ctx = (KDF_ARGON2 *) vctx;
  941. type = ctx->type;
  942. libctx = ctx->libctx;
  943. EVP_MD_free(ctx->md);
  944. EVP_MAC_free(ctx->mac);
  945. OPENSSL_free(ctx->propq);
  946. if (ctx->pwd != NULL)
  947. OPENSSL_clear_free(ctx->pwd, ctx->pwdlen);
  948. if (ctx->salt != NULL)
  949. OPENSSL_clear_free(ctx->salt, ctx->saltlen);
  950. if (ctx->secret != NULL)
  951. OPENSSL_clear_free(ctx->secret, ctx->secretlen);
  952. if (ctx->ad != NULL)
  953. OPENSSL_clear_free(ctx->ad, ctx->adlen);
  954. memset(ctx, 0, sizeof(*ctx));
  955. ctx->libctx = libctx;
  956. kdf_argon2_init(ctx, type);
  957. }
  958. static int kdf_argon2_ctx_set_threads(KDF_ARGON2 *ctx, uint32_t threads)
  959. {
  960. if (threads < ARGON2_MIN_THREADS) {
  961. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_THREAD_POOL_SIZE,
  962. "min threads: %u", ARGON2_MIN_THREADS);
  963. return 0;
  964. }
  965. if (threads > ARGON2_MAX_THREADS) {
  966. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_THREAD_POOL_SIZE,
  967. "max threads: %u", ARGON2_MAX_THREADS);
  968. return 0;
  969. }
  970. ctx->threads = threads;
  971. return 1;
  972. }
  973. static int kdf_argon2_ctx_set_lanes(KDF_ARGON2 *ctx, uint32_t lanes)
  974. {
  975. if (lanes > ARGON2_MAX_LANES) {
  976. ERR_raise_data(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER,
  977. "max lanes: %u", ARGON2_MAX_LANES);
  978. return 0;
  979. }
  980. if (lanes < ARGON2_MIN_LANES) {
  981. ERR_raise_data(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER,
  982. "min lanes: %u", ARGON2_MIN_LANES);
  983. return 0;
  984. }
  985. ctx->lanes = lanes;
  986. return 1;
  987. }
  988. static int kdf_argon2_ctx_set_t_cost(KDF_ARGON2 *ctx, uint32_t t_cost)
  989. {
  990. /* ARGON2_MAX_MEMORY == max m_cost value, so skip check */
  991. if (t_cost < ARGON2_MIN_TIME) {
  992. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT,
  993. "min: %u", ARGON2_MIN_TIME);
  994. return 0;
  995. }
  996. ctx->t_cost = t_cost;
  997. return 1;
  998. }
  999. static int kdf_argon2_ctx_set_m_cost(KDF_ARGON2 *ctx, uint32_t m_cost)
  1000. {
  1001. /* ARGON2_MAX_MEMORY == max m_cost value, so skip check */
  1002. if (m_cost < ARGON2_MIN_MEMORY) {
  1003. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_MEMORY_SIZE, "min: %u",
  1004. ARGON2_MIN_MEMORY);
  1005. return 0;
  1006. }
  1007. ctx->m_cost = m_cost;
  1008. return 1;
  1009. }
  1010. static int kdf_argon2_ctx_set_out_length(KDF_ARGON2 *ctx, uint32_t outlen)
  1011. {
  1012. /*
  1013. * ARGON2_MAX_OUT_LENGTH == max outlen value, so upper bounds checks
  1014. * are always satisfied; to suppress compiler if statement tautology
  1015. * warnings, these checks are skipped.
  1016. */
  1017. if (outlen < ARGON2_MIN_OUT_LENGTH) {
  1018. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH, "min: %u",
  1019. ARGON2_MIN_OUT_LENGTH);
  1020. return 0;
  1021. }
  1022. ctx->outlen = outlen;
  1023. return 1;
  1024. }
  1025. static int kdf_argon2_ctx_set_secret(KDF_ARGON2 *ctx, const OSSL_PARAM *p)
  1026. {
  1027. size_t buflen;
  1028. if (p->data == NULL)
  1029. return 0;
  1030. if (ctx->secret != NULL) {
  1031. OPENSSL_clear_free(ctx->secret, ctx->secretlen);
  1032. ctx->secret = NULL;
  1033. ctx->secretlen = 0U;
  1034. }
  1035. if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->secret, 0, &buflen))
  1036. return 0;
  1037. if (buflen > ARGON2_MAX_SECRET) {
  1038. OPENSSL_free(ctx->secret);
  1039. ctx->secret = NULL;
  1040. ctx->secretlen = 0U;
  1041. return 0;
  1042. }
  1043. ctx->secretlen = (uint32_t) buflen;
  1044. return 1;
  1045. }
  1046. static int kdf_argon2_ctx_set_pwd(KDF_ARGON2 *ctx, const OSSL_PARAM *p)
  1047. {
  1048. size_t buflen;
  1049. if (p->data == NULL)
  1050. return 0;
  1051. if (ctx->pwd != NULL) {
  1052. OPENSSL_clear_free(ctx->pwd, ctx->pwdlen);
  1053. ctx->pwd = NULL;
  1054. ctx->pwdlen = 0U;
  1055. }
  1056. if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->pwd, 0, &buflen))
  1057. return 0;
  1058. if (buflen > ARGON2_MAX_PWD_LENGTH) {
  1059. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH, "max: %u",
  1060. ARGON2_MAX_PWD_LENGTH);
  1061. goto fail;
  1062. }
  1063. ctx->pwdlen = (uint32_t) buflen;
  1064. return 1;
  1065. fail:
  1066. OPENSSL_free(ctx->pwd);
  1067. ctx->pwd = NULL;
  1068. ctx->pwdlen = 0U;
  1069. return 0;
  1070. }
  1071. static int kdf_argon2_ctx_set_salt(KDF_ARGON2 *ctx, const OSSL_PARAM *p)
  1072. {
  1073. size_t buflen;
  1074. if (p->data == NULL)
  1075. return 0;
  1076. if (ctx->salt != NULL) {
  1077. OPENSSL_clear_free(ctx->salt, ctx->saltlen);
  1078. ctx->salt = NULL;
  1079. ctx->saltlen = 0U;
  1080. }
  1081. if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->salt, 0, &buflen))
  1082. return 0;
  1083. if (buflen < ARGON2_MIN_SALT_LENGTH) {
  1084. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH, "min: %u",
  1085. ARGON2_MIN_SALT_LENGTH);
  1086. goto fail;
  1087. }
  1088. if (buflen > ARGON2_MAX_SALT_LENGTH) {
  1089. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH, "max: %u",
  1090. ARGON2_MAX_SALT_LENGTH);
  1091. goto fail;
  1092. }
  1093. ctx->saltlen = (uint32_t) buflen;
  1094. return 1;
  1095. fail:
  1096. OPENSSL_free(ctx->salt);
  1097. ctx->salt = NULL;
  1098. ctx->saltlen = 0U;
  1099. return 0;
  1100. }
  1101. static int kdf_argon2_ctx_set_ad(KDF_ARGON2 *ctx, const OSSL_PARAM *p)
  1102. {
  1103. size_t buflen;
  1104. if (p->data == NULL)
  1105. return 0;
  1106. if (ctx->ad != NULL) {
  1107. OPENSSL_clear_free(ctx->ad, ctx->adlen);
  1108. ctx->ad = NULL;
  1109. ctx->adlen = 0U;
  1110. }
  1111. if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->ad, 0, &buflen))
  1112. return 0;
  1113. if (buflen > ARGON2_MAX_AD_LENGTH) {
  1114. OPENSSL_free(ctx->ad);
  1115. ctx->ad = NULL;
  1116. ctx->adlen = 0U;
  1117. return 0;
  1118. }
  1119. ctx->adlen = (uint32_t) buflen;
  1120. return 1;
  1121. }
  1122. static void kdf_argon2_ctx_set_flag_early_clean(KDF_ARGON2 *ctx, uint32_t f)
  1123. {
  1124. ctx->early_clean = !!(f);
  1125. }
  1126. static int kdf_argon2_ctx_set_version(KDF_ARGON2 *ctx, uint32_t version)
  1127. {
  1128. switch (version) {
  1129. case ARGON2_VERSION_10:
  1130. case ARGON2_VERSION_13:
  1131. ctx->version = version;
  1132. return 1;
  1133. default:
  1134. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_MODE,
  1135. "invalid Argon2 version");
  1136. return 0;
  1137. }
  1138. }
  1139. static int set_property_query(KDF_ARGON2 *ctx, const char *propq)
  1140. {
  1141. OPENSSL_free(ctx->propq);
  1142. ctx->propq = NULL;
  1143. if (propq != NULL) {
  1144. ctx->propq = OPENSSL_strdup(propq);
  1145. if (ctx->propq == NULL)
  1146. return 0;
  1147. }
  1148. EVP_MD_free(ctx->md);
  1149. ctx->md = NULL;
  1150. EVP_MAC_free(ctx->mac);
  1151. ctx->mac = NULL;
  1152. return 1;
  1153. }
  1154. static int kdf_argon2_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  1155. {
  1156. const OSSL_PARAM *p;
  1157. KDF_ARGON2 *ctx;
  1158. uint32_t u32_value;
  1159. if (params == NULL)
  1160. return 1;
  1161. ctx = (KDF_ARGON2 *) vctx;
  1162. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)
  1163. if (!kdf_argon2_ctx_set_pwd(ctx, p))
  1164. return 0;
  1165. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL)
  1166. if (!kdf_argon2_ctx_set_salt(ctx, p))
  1167. return 0;
  1168. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL)
  1169. if (!kdf_argon2_ctx_set_secret(ctx, p))
  1170. return 0;
  1171. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ARGON2_AD)) != NULL)
  1172. if (!kdf_argon2_ctx_set_ad(ctx, p))
  1173. return 0;
  1174. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SIZE)) != NULL) {
  1175. if (!OSSL_PARAM_get_uint32(p, &u32_value))
  1176. return 0;
  1177. if (!kdf_argon2_ctx_set_out_length(ctx, u32_value))
  1178. return 0;
  1179. }
  1180. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ITER)) != NULL) {
  1181. if (!OSSL_PARAM_get_uint32(p, &u32_value))
  1182. return 0;
  1183. if (!kdf_argon2_ctx_set_t_cost(ctx, u32_value))
  1184. return 0;
  1185. }
  1186. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_THREADS)) != NULL) {
  1187. if (!OSSL_PARAM_get_uint32(p, &u32_value))
  1188. return 0;
  1189. if (!kdf_argon2_ctx_set_threads(ctx, u32_value))
  1190. return 0;
  1191. }
  1192. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ARGON2_LANES)) != NULL) {
  1193. if (!OSSL_PARAM_get_uint32(p, &u32_value))
  1194. return 0;
  1195. if (!kdf_argon2_ctx_set_lanes(ctx, u32_value))
  1196. return 0;
  1197. }
  1198. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ARGON2_MEMCOST)) != NULL) {
  1199. if (!OSSL_PARAM_get_uint32(p, &u32_value))
  1200. return 0;
  1201. if (!kdf_argon2_ctx_set_m_cost(ctx, u32_value))
  1202. return 0;
  1203. }
  1204. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_EARLY_CLEAN)) != NULL) {
  1205. if (!OSSL_PARAM_get_uint32(p, &u32_value))
  1206. return 0;
  1207. kdf_argon2_ctx_set_flag_early_clean(ctx, u32_value);
  1208. }
  1209. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ARGON2_VERSION)) != NULL) {
  1210. if (!OSSL_PARAM_get_uint32(p, &u32_value))
  1211. return 0;
  1212. if (!kdf_argon2_ctx_set_version(ctx, u32_value))
  1213. return 0;
  1214. }
  1215. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PROPERTIES)) != NULL) {
  1216. if (p->data_type != OSSL_PARAM_UTF8_STRING
  1217. || !set_property_query(ctx, p->data))
  1218. return 0;
  1219. }
  1220. return 1;
  1221. }
  1222. static const OSSL_PARAM *kdf_argon2_settable_ctx_params(ossl_unused void *ctx,
  1223. ossl_unused void *p_ctx)
  1224. {
  1225. static const OSSL_PARAM known_settable_ctx_params[] = {
  1226. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0),
  1227. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
  1228. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
  1229. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_ARGON2_AD, NULL, 0),
  1230. OSSL_PARAM_uint32(OSSL_KDF_PARAM_SIZE, NULL),
  1231. OSSL_PARAM_uint32(OSSL_KDF_PARAM_ITER, NULL),
  1232. OSSL_PARAM_uint32(OSSL_KDF_PARAM_THREADS, NULL),
  1233. OSSL_PARAM_uint32(OSSL_KDF_PARAM_ARGON2_LANES, NULL),
  1234. OSSL_PARAM_uint32(OSSL_KDF_PARAM_ARGON2_MEMCOST, NULL),
  1235. OSSL_PARAM_uint32(OSSL_KDF_PARAM_EARLY_CLEAN, NULL),
  1236. OSSL_PARAM_uint32(OSSL_KDF_PARAM_ARGON2_VERSION, NULL),
  1237. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  1238. OSSL_PARAM_END
  1239. };
  1240. return known_settable_ctx_params;
  1241. }
  1242. static int kdf_argon2_get_ctx_params(void *vctx, OSSL_PARAM params[])
  1243. {
  1244. OSSL_PARAM *p;
  1245. (void) vctx;
  1246. if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
  1247. return OSSL_PARAM_set_size_t(p, SIZE_MAX);
  1248. return -2;
  1249. }
  1250. static const OSSL_PARAM *kdf_argon2_gettable_ctx_params(ossl_unused void *ctx,
  1251. ossl_unused void *p_ctx)
  1252. {
  1253. static const OSSL_PARAM known_gettable_ctx_params[] = {
  1254. OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
  1255. OSSL_PARAM_END
  1256. };
  1257. return known_gettable_ctx_params;
  1258. }
  1259. const OSSL_DISPATCH ossl_kdf_argon2i_functions[] = {
  1260. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_argon2i_new },
  1261. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_argon2_free },
  1262. { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_argon2_reset },
  1263. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_argon2_derive },
  1264. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  1265. (void(*)(void))kdf_argon2_settable_ctx_params },
  1266. { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_argon2_set_ctx_params },
  1267. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  1268. (void(*)(void))kdf_argon2_gettable_ctx_params },
  1269. { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_argon2_get_ctx_params },
  1270. OSSL_DISPATCH_END
  1271. };
  1272. const OSSL_DISPATCH ossl_kdf_argon2d_functions[] = {
  1273. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_argon2d_new },
  1274. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_argon2_free },
  1275. { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_argon2_reset },
  1276. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_argon2_derive },
  1277. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  1278. (void(*)(void))kdf_argon2_settable_ctx_params },
  1279. { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_argon2_set_ctx_params },
  1280. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  1281. (void(*)(void))kdf_argon2_gettable_ctx_params },
  1282. { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_argon2_get_ctx_params },
  1283. OSSL_DISPATCH_END
  1284. };
  1285. const OSSL_DISPATCH ossl_kdf_argon2id_functions[] = {
  1286. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_argon2id_new },
  1287. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_argon2_free },
  1288. { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_argon2_reset },
  1289. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_argon2_derive },
  1290. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  1291. (void(*)(void))kdf_argon2_settable_ctx_params },
  1292. { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_argon2_set_ctx_params },
  1293. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  1294. (void(*)(void))kdf_argon2_gettable_ctx_params },
  1295. { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_argon2_get_ctx_params },
  1296. OSSL_DISPATCH_END
  1297. };
  1298. #endif