| 1 |
/* kdf.c - Key Derivation Functions |
| 2 |
* Copyright (C) 1998, 2008, 2011 Free Software Foundation, Inc. |
| 3 |
* Copyright (C) 2013 g10 Code GmbH |
| 4 |
* |
| 5 |
* This file is part of Libgcrypt. |
| 6 |
* |
| 7 |
* Libgcrypt is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU Lesser General Public License as |
| 9 |
* published by the Free Software Foundation; either version 2.1 of |
| 10 |
* the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* Libgcrypt is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public |
| 18 |
* License along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <config.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
#include <errno.h> |
| 26 |
|
| 27 |
#include "g10lib.h" |
| 28 |
#include "cipher.h" |
| 29 |
#include "kdf-internal.h" |
| 30 |
|
| 31 |
|
| 32 |
/* Transform a passphrase into a suitable key of length KEYSIZE and |
| 33 |
store this key in the caller provided buffer KEYBUFFER. The caller |
| 34 |
must provide an HASHALGO, a valid ALGO and depending on that algo a |
| 35 |
SALT of 8 bytes and the number of ITERATIONS. Code taken from |
| 36 |
gnupg/agent/protect.c:hash_passphrase. */ |
| 37 |
static gpg_err_code_t |
| 38 |
openpgp_s2k (const void *passphrase, size_t passphraselen, |
| 39 |
int algo, int hashalgo, |
| 40 |
const void *salt, size_t saltlen, |
| 41 |
unsigned long iterations, |
| 42 |
size_t keysize, void *keybuffer) |
| 43 |
{ |
| 44 |
gpg_err_code_t ec; |
| 45 |
gcry_md_hd_t md; |
| 46 |
char *key = keybuffer; |
| 47 |
int pass, i; |
| 48 |
int used = 0; |
| 49 |
int secmode; |
| 50 |
|
| 51 |
if ((algo == GCRY_KDF_SALTED_S2K || algo == GCRY_KDF_ITERSALTED_S2K) |
| 52 |
&& (!salt || saltlen != 8)) |
| 53 |
return GPG_ERR_INV_VALUE; |
| 54 |
|
| 55 |
secmode = _gcry_is_secure (passphrase) || _gcry_is_secure (keybuffer); |
| 56 |
|
| 57 |
ec = _gcry_md_open (&md, hashalgo, secmode? GCRY_MD_FLAG_SECURE : 0); |
| 58 |
if (ec) |
| 59 |
return ec; |
| 60 |
|
| 61 |
for (pass=0; used < keysize; pass++) |
| 62 |
{ |
| 63 |
if (pass) |
| 64 |
{ |
| 65 |
_gcry_md_reset (md); |
| 66 |
for (i=0; i < pass; i++) /* Preset the hash context. */ |
| 67 |
_gcry_md_putc (md, 0); |
| 68 |
} |
| 69 |
|
| 70 |
if (algo == GCRY_KDF_SALTED_S2K || algo == GCRY_KDF_ITERSALTED_S2K) |
| 71 |
{ |
| 72 |
int len2 = passphraselen + 8; |
| 73 |
unsigned long count = len2; |
| 74 |
|
| 75 |
if (algo == GCRY_KDF_ITERSALTED_S2K) |
| 76 |
{ |
| 77 |
count = iterations; |
| 78 |
if (count < len2) |
| 79 |
count = len2; |
| 80 |
} |
| 81 |
|
| 82 |
while (count > len2) |
| 83 |
{ |
| 84 |
_gcry_md_write (md, salt, saltlen); |
| 85 |
_gcry_md_write (md, passphrase, passphraselen); |
| 86 |
count -= len2; |
| 87 |
} |
| 88 |
if (count < saltlen) |
| 89 |
_gcry_md_write (md, salt, count); |
| 90 |
else |
| 91 |
{ |
| 92 |
_gcry_md_write (md, salt, saltlen); |
| 93 |
count -= saltlen; |
| 94 |
_gcry_md_write (md, passphrase, count); |
| 95 |
} |
| 96 |
} |
| 97 |
else |
| 98 |
_gcry_md_write (md, passphrase, passphraselen); |
| 99 |
|
| 100 |
_gcry_md_final (md); |
| 101 |
i = _gcry_md_get_algo_dlen (hashalgo); |
| 102 |
if (i > keysize - used) |
| 103 |
i = keysize - used; |
| 104 |
memcpy (key+used, _gcry_md_read (md, hashalgo), i); |
| 105 |
used += i; |
| 106 |
} |
| 107 |
_gcry_md_close (md); |
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
/* Transform a passphrase into a suitable key of length KEYSIZE and |
| 113 |
store this key in the caller provided buffer KEYBUFFER. The caller |
| 114 |
must provide PRFALGO which indicates the pseudorandom function to |
| 115 |
use: This shall be the algorithms id of a hash algorithm; it is |
| 116 |
used in HMAC mode. SALT is a salt of length SALTLEN and ITERATIONS |
| 117 |
gives the number of iterations. */ |
| 118 |
gpg_err_code_t |
| 119 |
_gcry_kdf_pkdf2 (const void *passphrase, size_t passphraselen, |
| 120 |
int hashalgo, |
| 121 |
const void *salt, size_t saltlen, |
| 122 |
unsigned long iterations, |
| 123 |
size_t keysize, void *keybuffer) |
| 124 |
{ |
| 125 |
gpg_err_code_t ec; |
| 126 |
gcry_md_hd_t md; |
| 127 |
int secmode; |
| 128 |
unsigned long dklen = keysize; |
| 129 |
char *dk = keybuffer; |
| 130 |
unsigned int hlen; /* Output length of the digest function. */ |
| 131 |
unsigned int l; /* Rounded up number of blocks. */ |
| 132 |
unsigned int r; /* Number of octets in the last block. */ |
| 133 |
char *sbuf; /* Malloced buffer to concatenate salt and iter |
| 134 |
as well as space to hold TBUF and UBUF. */ |
| 135 |
char *tbuf; /* Buffer for T; ptr into SBUF, size is HLEN. */ |
| 136 |
char *ubuf; /* Buffer for U; ptr into SBUF, size is HLEN. */ |
| 137 |
unsigned int lidx; /* Current block number. */ |
| 138 |
unsigned long iter; /* Current iteration number. */ |
| 139 |
unsigned int i; |
| 140 |
|
| 141 |
/* We allow for a saltlen of 0 here to support scrypt. It is not |
| 142 |
clear whether rfc2898 allows for this this, thus we do a test on |
| 143 |
saltlen > 0 only in gcry_kdf_derive. */ |
| 144 |
if (!salt || !iterations || !dklen) |
| 145 |
return GPG_ERR_INV_VALUE; |
| 146 |
|
| 147 |
hlen = _gcry_md_get_algo_dlen (hashalgo); |
| 148 |
if (!hlen) |
| 149 |
return GPG_ERR_DIGEST_ALGO; |
| 150 |
|
| 151 |
secmode = _gcry_is_secure (passphrase) || _gcry_is_secure (keybuffer); |
| 152 |
|
| 153 |
/* Step 1 */ |
| 154 |
/* If dkLen > (2^32 - 1) * hLen, output "derived key too long" and |
| 155 |
* stop. We use a stronger inequality but only if our type can hold |
| 156 |
* a larger value. */ |
| 157 |
|
| 158 |
#if SIZEOF_UNSIGNED_LONG > 4 |
| 159 |
if (dklen > 0xffffffffU) |
| 160 |
return GPG_ERR_INV_VALUE; |
| 161 |
#endif |
| 162 |
|
| 163 |
/* Step 2 */ |
| 164 |
l = ((dklen - 1)/ hlen) + 1; |
| 165 |
r = dklen - (l - 1) * hlen; |
| 166 |
|
| 167 |
/* Setup buffers and prepare a hash context. */ |
| 168 |
sbuf = (secmode |
| 169 |
? xtrymalloc_secure (saltlen + 4 + hlen + hlen) |
| 170 |
: xtrymalloc (saltlen + 4 + hlen + hlen)); |
| 171 |
if (!sbuf) |
| 172 |
return gpg_err_code_from_syserror (); |
| 173 |
tbuf = sbuf + saltlen + 4; |
| 174 |
ubuf = tbuf + hlen; |
| 175 |
|
| 176 |
ec = _gcry_md_open (&md, hashalgo, (GCRY_MD_FLAG_HMAC |
| 177 |
| (secmode?GCRY_MD_FLAG_SECURE:0))); |
| 178 |
if (ec) |
| 179 |
{ |
| 180 |
xfree (sbuf); |
| 181 |
return ec; |
| 182 |
} |
| 183 |
|
| 184 |
ec = _gcry_md_setkey (md, passphrase, passphraselen); |
| 185 |
if (ec) |
| 186 |
{ |
| 187 |
_gcry_md_close (md); |
| 188 |
xfree (sbuf); |
| 189 |
return ec; |
| 190 |
} |
| 191 |
|
| 192 |
/* Step 3 and 4. */ |
| 193 |
memcpy (sbuf, salt, saltlen); |
| 194 |
for (lidx = 1; lidx <= l; lidx++) |
| 195 |
{ |
| 196 |
for (iter = 0; iter < iterations; iter++) |
| 197 |
{ |
| 198 |
_gcry_md_reset (md); |
| 199 |
if (!iter) /* Compute U_1: */ |
| 200 |
{ |
| 201 |
sbuf[saltlen] = (lidx >> 24); |
| 202 |
sbuf[saltlen + 1] = (lidx >> 16); |
| 203 |
sbuf[saltlen + 2] = (lidx >> 8); |
| 204 |
sbuf[saltlen + 3] = lidx; |
| 205 |
_gcry_md_write (md, sbuf, saltlen + 4); |
| 206 |
memcpy (ubuf, _gcry_md_read (md, 0), hlen); |
| 207 |
memcpy (tbuf, ubuf, hlen); |
| 208 |
} |
| 209 |
else /* Compute U_(2..c): */ |
| 210 |
{ |
| 211 |
_gcry_md_write (md, ubuf, hlen); |
| 212 |
memcpy (ubuf, _gcry_md_read (md, 0), hlen); |
| 213 |
for (i=0; i < hlen; i++) |
| 214 |
tbuf[i] ^= ubuf[i]; |
| 215 |
} |
| 216 |
} |
| 217 |
if (lidx == l) /* Last block. */ |
| 218 |
memcpy (dk, tbuf, r); |
| 219 |
else |
| 220 |
{ |
| 221 |
memcpy (dk, tbuf, hlen); |
| 222 |
dk += hlen; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
_gcry_md_close (md); |
| 227 |
xfree (sbuf); |
| 228 |
return 0; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
/* Derive a key from a passphrase. KEYSIZE gives the requested size |
| 233 |
of the keys in octets. KEYBUFFER is a caller provided buffer |
| 234 |
filled on success with the derived key. The input passphrase is |
| 235 |
taken from (PASSPHRASE,PASSPHRASELEN) which is an arbitrary memory |
| 236 |
buffer. ALGO specifies the KDF algorithm to use; these are the |
| 237 |
constants GCRY_KDF_*. SUBALGO specifies an algorithm used |
| 238 |
internally by the KDF algorithms; this is usually a hash algorithm |
| 239 |
but certain KDF algorithm may use it differently. {SALT,SALTLEN} |
| 240 |
is a salt as needed by most KDF algorithms. ITERATIONS is a |
| 241 |
positive integer parameter to most KDFs. 0 is returned on success, |
| 242 |
or an error code on failure. */ |
| 243 |
gpg_err_code_t |
| 244 |
_gcry_kdf_derive (const void *passphrase, size_t passphraselen, |
| 245 |
int algo, int subalgo, |
| 246 |
const void *salt, size_t saltlen, |
| 247 |
unsigned long iterations, |
| 248 |
size_t keysize, void *keybuffer) |
| 249 |
{ |
| 250 |
gpg_err_code_t ec; |
| 251 |
int is_compliant_algo = 0; |
| 252 |
|
| 253 |
if (!passphrase) |
| 254 |
{ |
| 255 |
ec = GPG_ERR_INV_DATA; |
| 256 |
goto leave; |
| 257 |
} |
| 258 |
|
| 259 |
if (!keybuffer || !keysize) |
| 260 |
{ |
| 261 |
ec = GPG_ERR_INV_VALUE; |
| 262 |
goto leave; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
switch (algo) |
| 267 |
{ |
| 268 |
case GCRY_KDF_SIMPLE_S2K: |
| 269 |
case GCRY_KDF_SALTED_S2K: |
| 270 |
case GCRY_KDF_ITERSALTED_S2K: |
| 271 |
if (!passphraselen) |
| 272 |
ec = GPG_ERR_INV_DATA; |
| 273 |
else |
| 274 |
ec = openpgp_s2k (passphrase, passphraselen, algo, subalgo, |
| 275 |
salt, saltlen, iterations, keysize, keybuffer); |
| 276 |
break; |
| 277 |
|
| 278 |
case GCRY_KDF_PBKDF1: |
| 279 |
ec = GPG_ERR_UNSUPPORTED_ALGORITHM; |
| 280 |
break; |
| 281 |
|
| 282 |
case GCRY_KDF_PBKDF2: |
| 283 |
is_compliant_algo = 1; |
| 284 |
if (!saltlen || !iterations) |
| 285 |
ec = GPG_ERR_INV_VALUE; |
| 286 |
else |
| 287 |
{ |
| 288 |
if (fips_mode ()) |
| 289 |
{ |
| 290 |
/* FIPS requires minimum passphrase length, see FIPS 140-3 IG D.N */ |
| 291 |
if (passphraselen < 8) |
| 292 |
fips_service_indicator_mark_non_compliant (); |
| 293 |
|
| 294 |
/* FIPS requires minimum salt length of 128 b (SP 800-132 sec. 5.1, p.6) */ |
| 295 |
if (saltlen < 16) |
| 296 |
fips_service_indicator_mark_non_compliant (); |
| 297 |
|
| 298 |
/* FIPS requires minimum iterations bound (SP 800-132 sec 5.2, p.6) */ |
| 299 |
if (iterations < 1000) |
| 300 |
fips_service_indicator_mark_non_compliant (); |
| 301 |
|
| 302 |
/* Check minimum key size */ |
| 303 |
if (keysize < 14) |
| 304 |
fips_service_indicator_mark_non_compliant (); |
| 305 |
} |
| 306 |
|
| 307 |
ec = _gcry_kdf_pkdf2 (passphrase, passphraselen, subalgo, |
| 308 |
salt, saltlen, iterations, keysize, keybuffer); |
| 309 |
} |
| 310 |
break; |
| 311 |
|
| 312 |
case 41: |
| 313 |
case GCRY_KDF_SCRYPT: |
| 314 |
#if USE_SCRYPT |
| 315 |
ec = _gcry_kdf_scrypt (passphrase, passphraselen, algo, subalgo, |
| 316 |
salt, saltlen, iterations, keysize, keybuffer); |
| 317 |
#else |
| 318 |
ec = GPG_ERR_UNSUPPORTED_ALGORITHM; |
| 319 |
#endif /*USE_SCRYPT*/ |
| 320 |
break; |
| 321 |
|
| 322 |
default: |
| 323 |
ec = GPG_ERR_UNKNOWN_ALGORITHM; |
| 324 |
break; |
| 325 |
} |
| 326 |
|
| 327 |
if (!ec && !is_compliant_algo && fips_mode ()) |
| 328 |
fips_service_indicator_mark_non_compliant (); |
| 329 |
|
| 330 |
leave: |
| 331 |
return ec; |
| 332 |
} |
| 333 |
|
| 334 |
#include "bufhelp.h" |
| 335 |
|
| 336 |
typedef struct argon2_context *argon2_ctx_t; |
| 337 |
|
| 338 |
/* Per thread data for Argon2. */ |
| 339 |
struct argon2_thread_data { |
| 340 |
argon2_ctx_t a; |
| 341 |
unsigned int pass; |
| 342 |
unsigned int slice; |
| 343 |
unsigned int lane; |
| 344 |
}; |
| 345 |
|
| 346 |
/* Argon2 context */ |
| 347 |
struct argon2_context { |
| 348 |
int algo; |
| 349 |
int hash_type; |
| 350 |
|
| 351 |
unsigned int outlen; |
| 352 |
|
| 353 |
const unsigned char *password; |
| 354 |
size_t passwordlen; |
| 355 |
|
| 356 |
const unsigned char *salt; |
| 357 |
size_t saltlen; |
| 358 |
|
| 359 |
const unsigned char *key; |
| 360 |
size_t keylen; |
| 361 |
|
| 362 |
const unsigned char *ad; |
| 363 |
size_t adlen; |
| 364 |
|
| 365 |
unsigned int m_cost; |
| 366 |
|
| 367 |
unsigned int passes; |
| 368 |
unsigned int memory_blocks; |
| 369 |
unsigned int segment_length; |
| 370 |
unsigned int lane_length; |
| 371 |
unsigned int lanes; |
| 372 |
|
| 373 |
u64 *block; |
| 374 |
struct argon2_thread_data *thread_data; |
| 375 |
|
| 376 |
unsigned char out[1]; /* In future, we may use flexible array member. */ |
| 377 |
}; |
| 378 |
|
| 379 |
#define ARGON2_VERSION 0x13 |
| 380 |
|
| 381 |
#define ARGON2_WORDS_IN_BLOCK (1024/8) |
| 382 |
|
| 383 |
static void |
| 384 |
xor_block (u64 *dst, const u64 *src) |
| 385 |
{ |
| 386 |
int i; |
| 387 |
|
| 388 |
for (i = 0; i < ARGON2_WORDS_IN_BLOCK; i++) |
| 389 |
dst[i] ^= src[i]; |
| 390 |
} |
| 391 |
|
| 392 |
static void |
| 393 |
beswap64_block (u64 *dst) |
| 394 |
{ |
| 395 |
#ifdef WORDS_BIGENDIAN |
| 396 |
int i; |
| 397 |
|
| 398 |
/* Swap a block in big-endian 64-bit word into one in |
| 399 |
little-endian. */ |
| 400 |
for (i = 0; i < ARGON2_WORDS_IN_BLOCK; i++) |
| 401 |
dst[i] = _gcry_bswap64 (dst[i]); |
| 402 |
#else |
| 403 |
/* Nothing to do. */ |
| 404 |
(void)dst; |
| 405 |
#endif |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
static gpg_err_code_t |
| 410 |
argon2_fill_first_blocks (argon2_ctx_t a) |
| 411 |
{ |
| 412 |
unsigned char h0_01_i[72]; |
| 413 |
unsigned char buf[10][4]; |
| 414 |
gcry_buffer_t iov[8]; |
| 415 |
unsigned int iov_count = 0; |
| 416 |
int i; |
| 417 |
|
| 418 |
/* Generate H0. */ |
| 419 |
buf_put_le32 (buf[0], a->lanes); |
| 420 |
buf_put_le32 (buf[1], a->outlen); |
| 421 |
buf_put_le32 (buf[2], a->m_cost); |
| 422 |
buf_put_le32 (buf[3], a->passes); |
| 423 |
buf_put_le32 (buf[4], ARGON2_VERSION); |
| 424 |
buf_put_le32 (buf[5], a->hash_type); |
| 425 |
buf_put_le32 (buf[6], a->passwordlen); |
| 426 |
iov[iov_count].data = buf[0]; |
| 427 |
iov[iov_count].len = 4 * 7; |
| 428 |
iov[iov_count].off = 0; |
| 429 |
iov_count++; |
| 430 |
if (a->passwordlen) |
| 431 |
{ |
| 432 |
iov[iov_count].data = (void *)a->password; |
| 433 |
iov[iov_count].len = a->passwordlen; |
| 434 |
iov[iov_count].off = 0; |
| 435 |
iov_count++; |
| 436 |
} |
| 437 |
|
| 438 |
buf_put_le32 (buf[7], a->saltlen); |
| 439 |
iov[iov_count].data = buf[7]; |
| 440 |
iov[iov_count].len = 4; |
| 441 |
iov[iov_count].off = 0; |
| 442 |
iov_count++; |
| 443 |
iov[iov_count].data = (void *)a->salt; |
| 444 |
iov[iov_count].len = a->saltlen; |
| 445 |
iov[iov_count].off = 0; |
| 446 |
iov_count++; |
| 447 |
|
| 448 |
buf_put_le32 (buf[8], a->keylen); |
| 449 |
iov[iov_count].data = buf[8]; |
| 450 |
iov[iov_count].len = 4; |
| 451 |
iov[iov_count].off = 0; |
| 452 |
iov_count++; |
| 453 |
if (a->key) |
| 454 |
{ |
| 455 |
iov[iov_count].data = (void *)a->key; |
| 456 |
iov[iov_count].len = a->keylen; |
| 457 |
iov[iov_count].off = 0; |
| 458 |
iov_count++; |
| 459 |
} |
| 460 |
|
| 461 |
buf_put_le32 (buf[9], a->adlen); |
| 462 |
iov[iov_count].data = buf[9]; |
| 463 |
iov[iov_count].len = 4; |
| 464 |
iov[iov_count].off = 0; |
| 465 |
iov_count++; |
| 466 |
if (a->ad) |
| 467 |
{ |
| 468 |
iov[iov_count].data = (void *)a->ad; |
| 469 |
iov[iov_count].len = a->adlen; |
| 470 |
iov[iov_count].off = 0; |
| 471 |
iov_count++; |
| 472 |
} |
| 473 |
|
| 474 |
_gcry_digest_spec_blake2b_512.hash_buffers (h0_01_i, 64, iov, iov_count); |
| 475 |
|
| 476 |
for (i = 0; i < a->lanes; i++) |
| 477 |
{ |
| 478 |
memset (h0_01_i+64, 0, 4); |
| 479 |
buf_put_le32 (h0_01_i+64+4, i); |
| 480 |
blake2b_vl_hash (h0_01_i, 72, 1024, |
| 481 |
&a->block[i*a->lane_length*ARGON2_WORDS_IN_BLOCK]); |
| 482 |
beswap64_block (&a->block[i*a->lane_length*ARGON2_WORDS_IN_BLOCK]); |
| 483 |
|
| 484 |
buf_put_le32 (h0_01_i+64, 1); |
| 485 |
blake2b_vl_hash (h0_01_i, 72, 1024, |
| 486 |
&a->block[(i*a->lane_length+1)*ARGON2_WORDS_IN_BLOCK]); |
| 487 |
beswap64_block (&a->block[(i*a->lane_length+1)*ARGON2_WORDS_IN_BLOCK]); |
| 488 |
} |
| 489 |
return 0; |
| 490 |
} |
| 491 |
|
| 492 |
static gpg_err_code_t |
| 493 |
argon2_init (argon2_ctx_t a, unsigned int parallelism, |
| 494 |
unsigned int m_cost, unsigned int t_cost) |
| 495 |
{ |
| 496 |
gpg_err_code_t ec = 0; |
| 497 |
unsigned int memory_blocks; |
| 498 |
size_t memory_bytes; |
| 499 |
unsigned int segment_length; |
| 500 |
void *block; |
| 501 |
struct argon2_thread_data *thread_data; |
| 502 |
|
| 503 |
memory_blocks = m_cost; |
| 504 |
if (memory_blocks < 8 * parallelism) |
| 505 |
memory_blocks = 8 * parallelism; |
| 506 |
|
| 507 |
segment_length = memory_blocks / (parallelism * 4); |
| 508 |
memory_blocks = segment_length * parallelism * 4; |
| 509 |
|
| 510 |
a->passes = t_cost; |
| 511 |
a->memory_blocks = memory_blocks; |
| 512 |
a->segment_length = segment_length; |
| 513 |
a->lane_length = segment_length * 4; |
| 514 |
a->lanes = parallelism; |
| 515 |
|
| 516 |
a->block = NULL; |
| 517 |
a->thread_data = NULL; |
| 518 |
|
| 519 |
if (U64_C(1024) * memory_blocks > SIZE_MAX) |
| 520 |
return GPG_ERR_INV_VALUE; |
| 521 |
|
| 522 |
memory_bytes = 1024 * (size_t)memory_blocks; |
| 523 |
block = xtrymalloc (memory_bytes); |
| 524 |
if (!block) |
| 525 |
{ |
| 526 |
ec = gpg_err_code_from_errno (errno); |
| 527 |
return ec; |
| 528 |
} |
| 529 |
memset (block, 0, memory_bytes); |
| 530 |
|
| 531 |
thread_data = xtrymalloc (a->lanes * sizeof (struct argon2_thread_data)); |
| 532 |
if (!thread_data) |
| 533 |
{ |
| 534 |
ec = gpg_err_code_from_errno (errno); |
| 535 |
xfree (block); |
| 536 |
return ec; |
| 537 |
} |
| 538 |
|
| 539 |
memset (thread_data, 0, a->lanes * sizeof (struct argon2_thread_data)); |
| 540 |
|
| 541 |
a->block = block; |
| 542 |
a->thread_data = thread_data; |
| 543 |
return 0; |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
static u64 fBlaMka (u64 x, u64 y) |
| 548 |
{ |
| 549 |
const u64 m = U64_C(0xFFFFFFFF); |
| 550 |
return x + y + 2 * (x & m) * (y & m); |
| 551 |
} |
| 552 |
|
| 553 |
static u64 rotr64 (u64 w, unsigned int c) |
| 554 |
{ |
| 555 |
return (w >> c) | (w << (64 - c)); |
| 556 |
} |
| 557 |
|
| 558 |
#define G(a, b, c, d) \ |
| 559 |
do { \ |
| 560 |
a = fBlaMka(a, b); \ |
| 561 |
d = rotr64(d ^ a, 32); \ |
| 562 |
c = fBlaMka(c, d); \ |
| 563 |
b = rotr64(b ^ c, 24); \ |
| 564 |
a = fBlaMka(a, b); \ |
| 565 |
d = rotr64(d ^ a, 16); \ |
| 566 |
c = fBlaMka(c, d); \ |
| 567 |
b = rotr64(b ^ c, 63); \ |
| 568 |
} while ((void)0, 0) |
| 569 |
|
| 570 |
#define BLAKE2_ROUND_NOMSG(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, \ |
| 571 |
v12, v13, v14, v15) \ |
| 572 |
do { \ |
| 573 |
G(v0, v4, v8, v12); \ |
| 574 |
G(v1, v5, v9, v13); \ |
| 575 |
G(v2, v6, v10, v14); \ |
| 576 |
G(v3, v7, v11, v15); \ |
| 577 |
G(v0, v5, v10, v15); \ |
| 578 |
G(v1, v6, v11, v12); \ |
| 579 |
G(v2, v7, v8, v13); \ |
| 580 |
G(v3, v4, v9, v14); \ |
| 581 |
} while ((void)0, 0) |
| 582 |
|
| 583 |
static void |
| 584 |
fill_block (const u64 *prev_block, const u64 *ref_block, u64 *curr_block, |
| 585 |
int with_xor) |
| 586 |
{ |
| 587 |
u64 block_r[ARGON2_WORDS_IN_BLOCK]; |
| 588 |
u64 block_tmp[ARGON2_WORDS_IN_BLOCK]; |
| 589 |
int i; |
| 590 |
|
| 591 |
if (prev_block) |
| 592 |
{ |
| 593 |
for (i = 0; i < ARGON2_WORDS_IN_BLOCK; i++) |
| 594 |
block_r[i] = ref_block[i] ^ prev_block[i]; |
| 595 |
} |
| 596 |
else |
| 597 |
memcpy (block_r, ref_block, 1024); |
| 598 |
|
| 599 |
memcpy (block_tmp, block_r, 1024); |
| 600 |
|
| 601 |
if (with_xor) |
| 602 |
xor_block (block_tmp, curr_block); |
| 603 |
|
| 604 |
for (i = 0; i < 8; ++i) |
| 605 |
BLAKE2_ROUND_NOMSG |
| 606 |
(block_r[16 * i], block_r[16 * i + 1], block_r[16 * i + 2], |
| 607 |
block_r[16 * i + 3], block_r[16 * i + 4], block_r[16 * i + 5], |
| 608 |
block_r[16 * i + 6], block_r[16 * i + 7], block_r[16 * i + 8], |
| 609 |
block_r[16 * i + 9], block_r[16 * i + 10], block_r[16 * i + 11], |
| 610 |
block_r[16 * i + 12], block_r[16 * i + 13], block_r[16 * i + 14], |
| 611 |
block_r[16 * i + 15]); |
| 612 |
|
| 613 |
for (i = 0; i < 8; i++) |
| 614 |
BLAKE2_ROUND_NOMSG |
| 615 |
(block_r[2 * i], block_r[2 * i + 1], block_r[2 * i + 16], |
| 616 |
block_r[2 * i + 17], block_r[2 * i + 32], block_r[2 * i + 33], |
| 617 |
block_r[2 * i + 48], block_r[2 * i + 49], block_r[2 * i + 64], |
| 618 |
block_r[2 * i + 65], block_r[2 * i + 80], block_r[2 * i + 81], |
| 619 |
block_r[2 * i + 96], block_r[2 * i + 97], block_r[2 * i + 112], |
| 620 |
block_r[2 * i + 113]); |
| 621 |
|
| 622 |
memcpy (curr_block, block_tmp, 1024); |
| 623 |
xor_block (curr_block, block_r); |
| 624 |
} |
| 625 |
|
| 626 |
static void |
| 627 |
pseudo_random_generate (u64 *random_block, u64 *input_block) |
| 628 |
{ |
| 629 |
input_block[6]++; |
| 630 |
fill_block (NULL, input_block, random_block, 0); |
| 631 |
fill_block (NULL, random_block, random_block, 0); |
| 632 |
} |
| 633 |
|
| 634 |
static u32 |
| 635 |
index_alpha (argon2_ctx_t a, const struct argon2_thread_data *t, |
| 636 |
int segment_index, u32 random, int same_lane) |
| 637 |
{ |
| 638 |
u32 reference_area_size; |
| 639 |
u64 relative_position; |
| 640 |
u32 start_position; |
| 641 |
|
| 642 |
if (t->pass == 0) |
| 643 |
{ |
| 644 |
if (t->slice == 0) |
| 645 |
reference_area_size = segment_index - 1; |
| 646 |
else |
| 647 |
{ |
| 648 |
if (same_lane) |
| 649 |
reference_area_size = t->slice * a->segment_length |
| 650 |
+ segment_index - 1; |
| 651 |
else |
| 652 |
reference_area_size = t->slice * a->segment_length + |
| 653 |
((segment_index == 0) ? -1 : 0); |
| 654 |
} |
| 655 |
} |
| 656 |
else |
| 657 |
{ |
| 658 |
if (same_lane) |
| 659 |
reference_area_size = a->lane_length |
| 660 |
- a->segment_length + segment_index - 1; |
| 661 |
else |
| 662 |
reference_area_size = a->lane_length |
| 663 |
- a->segment_length + ((segment_index == 0) ? -1 : 0); |
| 664 |
} |
| 665 |
|
| 666 |
relative_position = (random * (u64)random) >> 32; |
| 667 |
relative_position = reference_area_size - 1 - |
| 668 |
((reference_area_size * relative_position) >> 32); |
| 669 |
|
| 670 |
if (t->pass == 0) |
| 671 |
start_position = 0; |
| 672 |
else |
| 673 |
start_position = (t->slice == 4 - 1) |
| 674 |
? 0 |
| 675 |
: (t->slice + 1) * a->segment_length; |
| 676 |
|
| 677 |
return (start_position + relative_position) % a->lane_length; |
| 678 |
} |
| 679 |
|
| 680 |
static void |
| 681 |
argon2_compute_segment (void *priv) |
| 682 |
{ |
| 683 |
const struct argon2_thread_data *t = (const struct argon2_thread_data *)priv; |
| 684 |
argon2_ctx_t a = t->a; |
| 685 |
int i; |
| 686 |
int prev_offset, curr_offset; |
| 687 |
u32 ref_index, ref_lane; |
| 688 |
u64 input_block[1024/sizeof (u64)]; |
| 689 |
u64 address_block[1024/sizeof (u64)]; |
| 690 |
u64 *random_block = NULL; |
| 691 |
|
| 692 |
if (a->hash_type == GCRY_KDF_ARGON2I |
| 693 |
|| (a->hash_type == GCRY_KDF_ARGON2ID && t->pass == 0 && t->slice < 2)) |
| 694 |
{ |
| 695 |
memset (input_block, 0, 1024); |
| 696 |
input_block[0] = t->pass; |
| 697 |
input_block[1] = t->lane; |
| 698 |
input_block[2] = t->slice; |
| 699 |
input_block[3] = a->memory_blocks; |
| 700 |
input_block[4] = a->passes; |
| 701 |
input_block[5] = a->hash_type; |
| 702 |
random_block = address_block; |
| 703 |
} |
| 704 |
|
| 705 |
if (t->pass == 0 && t->slice == 0) |
| 706 |
{ |
| 707 |
if (random_block) |
| 708 |
pseudo_random_generate (random_block, input_block); |
| 709 |
i = 2; |
| 710 |
} |
| 711 |
else |
| 712 |
i = 0; |
| 713 |
|
| 714 |
curr_offset = t->lane * a->lane_length + t->slice * a->segment_length + i; |
| 715 |
if ((curr_offset % a->lane_length)) |
| 716 |
prev_offset = curr_offset - 1; |
| 717 |
else |
| 718 |
prev_offset = curr_offset + a->lane_length - 1; |
| 719 |
|
| 720 |
for (; i < a->segment_length; i++, curr_offset++, prev_offset++) |
| 721 |
{ |
| 722 |
u64 *ref_block, *curr_block; |
| 723 |
u64 rand64; |
| 724 |
|
| 725 |
if ((curr_offset % a->lane_length) == 1) |
| 726 |
prev_offset = curr_offset - 1; |
| 727 |
|
| 728 |
if (random_block) |
| 729 |
{ |
| 730 |
if ((i % (1024/sizeof (u64))) == 0) |
| 731 |
pseudo_random_generate (random_block, input_block); |
| 732 |
|
| 733 |
rand64 = random_block[(i% (1024/sizeof (u64)))]; |
| 734 |
} |
| 735 |
else |
| 736 |
rand64 = a->block[prev_offset*ARGON2_WORDS_IN_BLOCK]; |
| 737 |
|
| 738 |
if (t->pass == 0 && t->slice == 0) |
| 739 |
ref_lane = t->lane; |
| 740 |
else |
| 741 |
ref_lane = (rand64 >> 32) % a->lanes; |
| 742 |
|
| 743 |
ref_index = index_alpha (a, t, i, (rand64 & 0xffffffff), |
| 744 |
ref_lane == t->lane); |
| 745 |
ref_block = |
| 746 |
&a->block[(a->lane_length * ref_lane + ref_index)* ARGON2_WORDS_IN_BLOCK]; |
| 747 |
|
| 748 |
curr_block = &a->block[curr_offset * ARGON2_WORDS_IN_BLOCK]; |
| 749 |
fill_block (&a->block[prev_offset * ARGON2_WORDS_IN_BLOCK], ref_block, |
| 750 |
curr_block, t->pass != 0); |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
static gpg_err_code_t |
| 756 |
argon2_compute (argon2_ctx_t a, const struct gcry_kdf_thread_ops *ops) |
| 757 |
{ |
| 758 |
gpg_err_code_t ec; |
| 759 |
unsigned int r; |
| 760 |
unsigned int s; |
| 761 |
unsigned int l; |
| 762 |
int ret; |
| 763 |
|
| 764 |
ec = argon2_fill_first_blocks (a); |
| 765 |
if (ec) |
| 766 |
return ec; |
| 767 |
|
| 768 |
for (r = 0; r < a->passes; r++) |
| 769 |
for (s = 0; s < 4; s++) |
| 770 |
{ |
| 771 |
for (l = 0; l < a->lanes; l++) |
| 772 |
{ |
| 773 |
struct argon2_thread_data *thread_data; |
| 774 |
|
| 775 |
/* launch a thread. */ |
| 776 |
thread_data = &a->thread_data[l]; |
| 777 |
thread_data->a = a; |
| 778 |
thread_data->pass = r; |
| 779 |
thread_data->slice = s; |
| 780 |
thread_data->lane = l; |
| 781 |
|
| 782 |
if (ops) |
| 783 |
{ |
| 784 |
ret = ops->dispatch_job (ops->jobs_context, |
| 785 |
argon2_compute_segment, thread_data); |
| 786 |
if (ret < 0) |
| 787 |
return GPG_ERR_CANCELED; |
| 788 |
} |
| 789 |
else |
| 790 |
argon2_compute_segment (thread_data); |
| 791 |
} |
| 792 |
|
| 793 |
if (ops) |
| 794 |
{ |
| 795 |
ret = ops->wait_all_jobs (ops->jobs_context); |
| 796 |
if (ret < 0) |
| 797 |
return GPG_ERR_CANCELED; |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
return 0; |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
static gpg_err_code_t |
| 806 |
argon2_final (argon2_ctx_t a, size_t resultlen, void *result) |
| 807 |
{ |
| 808 |
int i; |
| 809 |
|
| 810 |
if (resultlen != a->outlen) |
| 811 |
return GPG_ERR_INV_VALUE; |
| 812 |
|
| 813 |
memset (a->block, 0, 1024); |
| 814 |
for (i = 0; i < a->lanes; i++) |
| 815 |
{ |
| 816 |
u64 *last_block; |
| 817 |
|
| 818 |
last_block = &a->block[(a->lane_length * i + (a->lane_length - 1)) |
| 819 |
* ARGON2_WORDS_IN_BLOCK]; |
| 820 |
xor_block (a->block, last_block); |
| 821 |
} |
| 822 |
|
| 823 |
beswap64_block (a->block); |
| 824 |
blake2b_vl_hash (a->block, 1024, a->outlen, result); |
| 825 |
return 0; |
| 826 |
} |
| 827 |
|
| 828 |
static void |
| 829 |
argon2_close (argon2_ctx_t a) |
| 830 |
{ |
| 831 |
size_t n; |
| 832 |
|
| 833 |
n = offsetof (struct argon2_context, out) + a->outlen; |
| 834 |
|
| 835 |
if (a->block) |
| 836 |
{ |
| 837 |
wipememory (a->block, 1024 * a->memory_blocks); |
| 838 |
xfree (a->block); |
| 839 |
} |
| 840 |
|
| 841 |
if (a->thread_data) |
| 842 |
xfree (a->thread_data); |
| 843 |
|
| 844 |
wipememory (a, n); |
| 845 |
xfree (a); |
| 846 |
} |
| 847 |
|
| 848 |
static gpg_err_code_t |
| 849 |
argon2_open (gcry_kdf_hd_t *hd, int subalgo, |
| 850 |
const unsigned long *param, unsigned int paramlen, |
| 851 |
const void *password, size_t passwordlen, |
| 852 |
const void *salt, size_t saltlen, |
| 853 |
const void *key, size_t keylen, |
| 854 |
const void *ad, size_t adlen) |
| 855 |
{ |
| 856 |
int hash_type; |
| 857 |
unsigned int taglen; |
| 858 |
unsigned int t_cost; |
| 859 |
unsigned int m_cost; |
| 860 |
unsigned int parallelism = 1; |
| 861 |
argon2_ctx_t a; |
| 862 |
gpg_err_code_t ec; |
| 863 |
size_t n; |
| 864 |
|
| 865 |
if (subalgo != GCRY_KDF_ARGON2D |
| 866 |
&& subalgo != GCRY_KDF_ARGON2I |
| 867 |
&& subalgo != GCRY_KDF_ARGON2ID) |
| 868 |
return GPG_ERR_INV_VALUE; |
| 869 |
else |
| 870 |
hash_type = subalgo; |
| 871 |
|
| 872 |
/* param : [ tag_length, t_cost, m_cost, parallelism ] */ |
| 873 |
if (paramlen < 3 || paramlen > 4) |
| 874 |
return GPG_ERR_INV_VALUE; |
| 875 |
else |
| 876 |
{ |
| 877 |
taglen = (unsigned int)param[0]; |
| 878 |
t_cost = (unsigned int)param[1]; |
| 879 |
m_cost = (unsigned int)param[2]; |
| 880 |
if (paramlen >= 4) |
| 881 |
parallelism = (unsigned int)param[3]; |
| 882 |
} |
| 883 |
|
| 884 |
if (parallelism == 0) |
| 885 |
return GPG_ERR_INV_VALUE; |
| 886 |
|
| 887 |
if (U64_C(1024) * m_cost > SIZE_MAX) |
| 888 |
return GPG_ERR_INV_VALUE; |
| 889 |
|
| 890 |
n = offsetof (struct argon2_context, out) + taglen; |
| 891 |
a = xtrymalloc (n); |
| 892 |
if (!a) |
| 893 |
return gpg_err_code_from_errno (errno); |
| 894 |
|
| 895 |
a->algo = GCRY_KDF_ARGON2; |
| 896 |
a->hash_type = hash_type; |
| 897 |
|
| 898 |
a->outlen = taglen; |
| 899 |
|
| 900 |
a->password = password; |
| 901 |
a->passwordlen = passwordlen; |
| 902 |
a->salt = salt; |
| 903 |
a->saltlen = saltlen; |
| 904 |
a->key = key; |
| 905 |
a->keylen = keylen; |
| 906 |
a->ad = ad; |
| 907 |
a->adlen = adlen; |
| 908 |
|
| 909 |
a->m_cost = m_cost; |
| 910 |
|
| 911 |
a->block = NULL; |
| 912 |
a->thread_data = NULL; |
| 913 |
|
| 914 |
ec = argon2_init (a, parallelism, m_cost, t_cost); |
| 915 |
if (ec) |
| 916 |
{ |
| 917 |
xfree (a); |
| 918 |
return ec; |
| 919 |
} |
| 920 |
|
| 921 |
*hd = (void *)a; |
| 922 |
return 0; |
| 923 |
} |
| 924 |
|
| 925 |
typedef struct balloon_context *balloon_ctx_t; |
| 926 |
|
| 927 |
/* Per thread data for Balloon. */ |
| 928 |
struct balloon_thread_data { |
| 929 |
balloon_ctx_t b; |
| 930 |
gpg_err_code_t ec; |
| 931 |
unsigned int idx; |
| 932 |
unsigned char *block; |
| 933 |
}; |
| 934 |
|
| 935 |
/* Balloon context */ |
| 936 |
struct balloon_context { |
| 937 |
int algo; |
| 938 |
int prng_type; |
| 939 |
|
| 940 |
unsigned int blklen; |
| 941 |
const gcry_md_spec_t *md_spec; |
| 942 |
|
| 943 |
const unsigned char *password; |
| 944 |
size_t passwordlen; |
| 945 |
|
| 946 |
const unsigned char *salt; |
| 947 |
/* Length of salt is fixed. */ |
| 948 |
|
| 949 |
unsigned int s_cost; |
| 950 |
unsigned int t_cost; |
| 951 |
unsigned int parallelism; |
| 952 |
|
| 953 |
u64 n_blocks; |
| 954 |
|
| 955 |
unsigned char *block; |
| 956 |
|
| 957 |
/* In future, we may use flexible array member. */ |
| 958 |
struct balloon_thread_data thread_data[1]; |
| 959 |
}; |
| 960 |
|
| 961 |
/* Maximum size of underlining digest size. */ |
| 962 |
#define BALLOON_BLOCK_LEN_MAX 64 |
| 963 |
|
| 964 |
static gpg_err_code_t |
| 965 |
prng_aes_ctr_init (gcry_cipher_hd_t *hd_p, balloon_ctx_t b, |
| 966 |
gcry_buffer_t *iov, unsigned int iov_count) |
| 967 |
{ |
| 968 |
gpg_err_code_t ec; |
| 969 |
gcry_cipher_hd_t hd; |
| 970 |
unsigned char key[BALLOON_BLOCK_LEN_MAX]; |
| 971 |
int cipher_algo; |
| 972 |
unsigned int keylen, blklen; |
| 973 |
|
| 974 |
switch (b->blklen) |
| 975 |
{ |
| 976 |
case 64: |
| 977 |
cipher_algo = GCRY_CIPHER_AES256; |
| 978 |
break; |
| 979 |
|
| 980 |
case 48: |
| 981 |
cipher_algo = GCRY_CIPHER_AES192; |
| 982 |
break; |
| 983 |
|
| 984 |
default: |
| 985 |
case 32: |
| 986 |
cipher_algo = GCRY_CIPHER_AES; |
| 987 |
break; |
| 988 |
} |
| 989 |
|
| 990 |
keylen = _gcry_cipher_get_algo_keylen (cipher_algo); |
| 991 |
blklen = _gcry_cipher_get_algo_blklen (cipher_algo); |
| 992 |
|
| 993 |
b->md_spec->hash_buffers (key, b->blklen, iov, iov_count); |
| 994 |
ec = _gcry_cipher_open (&hd, cipher_algo, GCRY_CIPHER_MODE_CTR, 0); |
| 995 |
if (ec) |
| 996 |
return ec; |
| 997 |
|
| 998 |
ec = _gcry_cipher_setkey (hd, key, keylen); |
| 999 |
if (ec) |
| 1000 |
{ |
| 1001 |
_gcry_cipher_close (hd); |
| 1002 |
return ec; |
| 1003 |
} |
| 1004 |
|
| 1005 |
if (cipher_algo == GCRY_CIPHER_AES |
| 1006 |
&& b->md_spec == &_gcry_digest_spec_sha256) |
| 1007 |
/* Original Balloon uses zero IV. */ |
| 1008 |
; |
| 1009 |
else |
| 1010 |
{ |
| 1011 |
ec = _gcry_cipher_setiv (hd, key+keylen, blklen); |
| 1012 |
if (ec) |
| 1013 |
{ |
| 1014 |
_gcry_cipher_close (hd); |
| 1015 |
return ec; |
| 1016 |
} |
| 1017 |
} |
| 1018 |
|
| 1019 |
wipememory (key, BALLOON_BLOCK_LEN_MAX); |
| 1020 |
*hd_p = hd; |
| 1021 |
return ec; |
| 1022 |
} |
| 1023 |
|
| 1024 |
static u64 |
| 1025 |
prng_aes_ctr_get_rand64 (gcry_cipher_hd_t hd) |
| 1026 |
{ |
| 1027 |
static const unsigned char zero64[8]; |
| 1028 |
unsigned char rand64[8]; |
| 1029 |
|
| 1030 |
_gcry_cipher_encrypt (hd, rand64, sizeof (rand64), zero64, sizeof (zero64)); |
| 1031 |
return buf_get_le64 (rand64); |
| 1032 |
} |
| 1033 |
|
| 1034 |
static void |
| 1035 |
prng_aes_ctr_fini (gcry_cipher_hd_t hd) |
| 1036 |
{ |
| 1037 |
_gcry_cipher_close (hd); |
| 1038 |
} |
| 1039 |
|
| 1040 |
static size_t |
| 1041 |
ballon_context_size (unsigned int parallelism) |
| 1042 |
{ |
| 1043 |
size_t n; |
| 1044 |
|
| 1045 |
n = offsetof (struct balloon_context, thread_data) |
| 1046 |
+ parallelism * sizeof (struct balloon_thread_data); |
| 1047 |
return n; |
| 1048 |
} |
| 1049 |
|
| 1050 |
static gpg_err_code_t |
| 1051 |
balloon_open (gcry_kdf_hd_t *hd, int subalgo, |
| 1052 |
const unsigned long *param, unsigned int paramlen, |
| 1053 |
const void *password, size_t passwordlen, |
| 1054 |
const void *salt, size_t saltlen) |
| 1055 |
{ |
| 1056 |
unsigned int blklen; |
| 1057 |
int hash_type; |
| 1058 |
unsigned int s_cost; |
| 1059 |
unsigned int t_cost; |
| 1060 |
unsigned int parallelism = 1; |
| 1061 |
balloon_ctx_t b; |
| 1062 |
gpg_err_code_t ec; |
| 1063 |
size_t n; |
| 1064 |
unsigned char *block; |
| 1065 |
unsigned int i; |
| 1066 |
const gcry_md_spec_t *md_spec; |
| 1067 |
|
| 1068 |
hash_type = subalgo; |
| 1069 |
switch (hash_type) |
| 1070 |
{ |
| 1071 |
case GCRY_MD_SHA256: |
| 1072 |
md_spec = &_gcry_digest_spec_sha256; |
| 1073 |
break; |
| 1074 |
|
| 1075 |
case GCRY_MD_SHA384: |
| 1076 |
md_spec = &_gcry_digest_spec_sha384; |
| 1077 |
break; |
| 1078 |
|
| 1079 |
case GCRY_MD_SHA512: |
| 1080 |
md_spec = &_gcry_digest_spec_sha512; |
| 1081 |
break; |
| 1082 |
|
| 1083 |
case GCRY_MD_SHA3_256: |
| 1084 |
md_spec = &_gcry_digest_spec_sha3_256; |
| 1085 |
break; |
| 1086 |
|
| 1087 |
case GCRY_MD_SHA3_384: |
| 1088 |
md_spec = &_gcry_digest_spec_sha3_384; |
| 1089 |
break; |
| 1090 |
|
| 1091 |
case GCRY_MD_SHA3_512: |
| 1092 |
md_spec = &_gcry_digest_spec_sha3_512; |
| 1093 |
break; |
| 1094 |
|
| 1095 |
default: |
| 1096 |
return GPG_ERR_NOT_SUPPORTED; |
| 1097 |
} |
| 1098 |
|
| 1099 |
blklen = _gcry_md_get_algo_dlen (hash_type); |
| 1100 |
if (!blklen || blklen > BALLOON_BLOCK_LEN_MAX) |
| 1101 |
return GPG_ERR_NOT_SUPPORTED; |
| 1102 |
|
| 1103 |
if (saltlen != blklen) |
| 1104 |
return GPG_ERR_NOT_SUPPORTED; |
| 1105 |
|
| 1106 |
/* |
| 1107 |
* It should have space_cost and time_cost. |
| 1108 |
* Optionally, for parallelised version, it has parallelism. |
| 1109 |
* Possibly (in future), it may have option to specify PRNG type. |
| 1110 |
*/ |
| 1111 |
if (paramlen != 2 && paramlen != 3) |
| 1112 |
return GPG_ERR_INV_VALUE; |
| 1113 |
else |
| 1114 |
{ |
| 1115 |
s_cost = (unsigned int)param[0]; |
| 1116 |
t_cost = (unsigned int)param[1]; |
| 1117 |
if (paramlen >= 3) |
| 1118 |
parallelism = (unsigned int)param[2]; |
| 1119 |
} |
| 1120 |
|
| 1121 |
if (s_cost < 1) |
| 1122 |
return GPG_ERR_INV_VALUE; |
| 1123 |
|
| 1124 |
n = ballon_context_size (parallelism); |
| 1125 |
b = xtrymalloc (n); |
| 1126 |
if (!b) |
| 1127 |
return gpg_err_code_from_errno (errno); |
| 1128 |
|
| 1129 |
b->algo = GCRY_KDF_BALLOON; |
| 1130 |
b->md_spec = md_spec; |
| 1131 |
b->blklen = blklen; |
| 1132 |
|
| 1133 |
b->password = password; |
| 1134 |
b->passwordlen = passwordlen; |
| 1135 |
b->salt = salt; |
| 1136 |
|
| 1137 |
b->s_cost = s_cost; |
| 1138 |
b->t_cost = t_cost; |
| 1139 |
b->parallelism = parallelism; |
| 1140 |
|
| 1141 |
b->n_blocks = (s_cost * 1024) / b->blklen; |
| 1142 |
|
| 1143 |
block = xtrycalloc (parallelism * b->n_blocks, b->blklen); |
| 1144 |
if (!block) |
| 1145 |
{ |
| 1146 |
ec = gpg_err_code_from_errno (errno); |
| 1147 |
xfree (b); |
| 1148 |
return ec; |
| 1149 |
} |
| 1150 |
b->block = block; |
| 1151 |
|
| 1152 |
for (i = 0; i < parallelism; i++) |
| 1153 |
{ |
| 1154 |
struct balloon_thread_data *t = &b->thread_data[i]; |
| 1155 |
|
| 1156 |
t->b = b; |
| 1157 |
t->ec = 0; |
| 1158 |
t->idx = i; |
| 1159 |
t->block = block; |
| 1160 |
block += b->blklen * b->n_blocks; |
| 1161 |
} |
| 1162 |
|
| 1163 |
*hd = (void *)b; |
| 1164 |
return 0; |
| 1165 |
} |
| 1166 |
|
| 1167 |
|
| 1168 |
static void |
| 1169 |
balloon_xor_block (balloon_ctx_t b, u64 *dst, const u64 *src) |
| 1170 |
{ |
| 1171 |
int i; |
| 1172 |
|
| 1173 |
for (i = 0; i < b->blklen/8; i++) |
| 1174 |
dst[i] ^= src[i]; |
| 1175 |
} |
| 1176 |
|
| 1177 |
#define BALLOON_COMPRESS_BLOCKS 5 |
| 1178 |
|
| 1179 |
static void |
| 1180 |
balloon_compress (balloon_ctx_t b, u64 *counter_p, unsigned char *out, |
| 1181 |
const unsigned char *blocks[BALLOON_COMPRESS_BLOCKS]) |
| 1182 |
{ |
| 1183 |
gcry_buffer_t iov[1+BALLOON_COMPRESS_BLOCKS]; |
| 1184 |
unsigned char octet_counter[sizeof (u64)]; |
| 1185 |
unsigned int i; |
| 1186 |
|
| 1187 |
buf_put_le64 (octet_counter, *counter_p); |
| 1188 |
iov[0].data = octet_counter; |
| 1189 |
iov[0].len = sizeof (octet_counter); |
| 1190 |
iov[0].off = 0; |
| 1191 |
|
| 1192 |
for (i = 1; i < 1+BALLOON_COMPRESS_BLOCKS; i++) |
| 1193 |
{ |
| 1194 |
iov[i].data = (void *)blocks[i-1]; |
| 1195 |
iov[i].len = b->blklen; |
| 1196 |
iov[i].off = 0; |
| 1197 |
} |
| 1198 |
|
| 1199 |
b->md_spec->hash_buffers (out, b->blklen, iov, 1+BALLOON_COMPRESS_BLOCKS); |
| 1200 |
*counter_p += 1; |
| 1201 |
} |
| 1202 |
|
| 1203 |
static void |
| 1204 |
balloon_expand (balloon_ctx_t b, u64 *counter_p, unsigned char *block, |
| 1205 |
u64 n_blocks) |
| 1206 |
{ |
| 1207 |
gcry_buffer_t iov[2]; |
| 1208 |
unsigned char octet_counter[sizeof (u64)]; |
| 1209 |
u64 i; |
| 1210 |
|
| 1211 |
iov[0].data = octet_counter; |
| 1212 |
iov[0].len = sizeof (octet_counter); |
| 1213 |
iov[0].off = 0; |
| 1214 |
iov[1].len = b->blklen; |
| 1215 |
iov[1].off = 0; |
| 1216 |
|
| 1217 |
for (i = 1; i < n_blocks; i++) |
| 1218 |
{ |
| 1219 |
buf_put_le64 (octet_counter, *counter_p); |
| 1220 |
iov[1].data = block; |
| 1221 |
block += b->blklen; |
| 1222 |
b->md_spec->hash_buffers (block, b->blklen, iov, 2); |
| 1223 |
*counter_p += 1; |
| 1224 |
} |
| 1225 |
} |
| 1226 |
|
| 1227 |
static void |
| 1228 |
balloon_compute_fill (balloon_ctx_t b, |
| 1229 |
struct balloon_thread_data *t, |
| 1230 |
const unsigned char *salt, |
| 1231 |
u64 *counter_p) |
| 1232 |
{ |
| 1233 |
gcry_buffer_t iov[6]; |
| 1234 |
unsigned char octet_counter[sizeof (u64)]; |
| 1235 |
unsigned char octet_s_cost[4]; |
| 1236 |
unsigned char octet_t_cost[4]; |
| 1237 |
unsigned char octet_parallelism[4]; |
| 1238 |
|
| 1239 |
buf_put_le64 (octet_counter, *counter_p); |
| 1240 |
buf_put_le32 (octet_s_cost, b->s_cost); |
| 1241 |
buf_put_le32 (octet_t_cost, b->t_cost); |
| 1242 |
buf_put_le32 (octet_parallelism, b->parallelism); |
| 1243 |
|
| 1244 |
iov[0].data = octet_counter; |
| 1245 |
iov[0].len = sizeof (octet_counter); |
| 1246 |
iov[0].off = 0; |
| 1247 |
iov[1].data = (void *)salt; |
| 1248 |
iov[1].len = b->blklen; |
| 1249 |
iov[1].off = 0; |
| 1250 |
iov[2].data = (void *)b->password; |
| 1251 |
iov[2].len = b->passwordlen; |
| 1252 |
iov[2].off = 0; |
| 1253 |
iov[3].data = octet_s_cost; |
| 1254 |
iov[3].len = 4; |
| 1255 |
iov[3].off = 0; |
| 1256 |
iov[4].data = octet_t_cost; |
| 1257 |
iov[4].len = 4; |
| 1258 |
iov[4].off = 0; |
| 1259 |
iov[5].data = octet_parallelism; |
| 1260 |
iov[5].len = 4; |
| 1261 |
iov[5].off = 0; |
| 1262 |
b->md_spec->hash_buffers (t->block, b->blklen, iov, 6); |
| 1263 |
*counter_p += 1; |
| 1264 |
balloon_expand (b, counter_p, t->block, b->n_blocks); |
| 1265 |
} |
| 1266 |
|
| 1267 |
static void |
| 1268 |
balloon_compute_mix (gcry_cipher_hd_t prng, |
| 1269 |
balloon_ctx_t b, struct balloon_thread_data *t, |
| 1270 |
u64 *counter_p) |
| 1271 |
{ |
| 1272 |
u64 i; |
| 1273 |
|
| 1274 |
for (i = 0; i < b->n_blocks; i++) |
| 1275 |
{ |
| 1276 |
unsigned char *cur_block = t->block + (b->blklen * i); |
| 1277 |
const unsigned char *blocks[BALLOON_COMPRESS_BLOCKS]; |
| 1278 |
const unsigned char *prev_block; |
| 1279 |
unsigned int n; |
| 1280 |
|
| 1281 |
prev_block = i |
| 1282 |
? cur_block - b->blklen |
| 1283 |
: t->block + (b->blklen * (t->b->n_blocks - 1)); |
| 1284 |
|
| 1285 |
n = 0; |
| 1286 |
blocks[n++] = prev_block; |
| 1287 |
blocks[n++] = cur_block; |
| 1288 |
|
| 1289 |
for (; n < BALLOON_COMPRESS_BLOCKS; n++) |
| 1290 |
{ |
| 1291 |
u64 rand64 = prng_aes_ctr_get_rand64 (prng); |
| 1292 |
blocks[n] = t->block + (b->blklen * (rand64 % b->n_blocks)); |
| 1293 |
} |
| 1294 |
|
| 1295 |
balloon_compress (b, counter_p, cur_block, blocks); |
| 1296 |
} |
| 1297 |
} |
| 1298 |
|
| 1299 |
|
| 1300 |
static void |
| 1301 |
balloon_compute (void *priv) |
| 1302 |
{ |
| 1303 |
struct balloon_thread_data *t = (struct balloon_thread_data *)priv; |
| 1304 |
balloon_ctx_t b = t->b; |
| 1305 |
gcry_cipher_hd_t prng; |
| 1306 |
gcry_buffer_t iov[4]; |
| 1307 |
unsigned char salt[BALLOON_BLOCK_LEN_MAX]; |
| 1308 |
unsigned char octet_s_cost[4]; |
| 1309 |
unsigned char octet_t_cost[4]; |
| 1310 |
unsigned char octet_parallelism[4]; |
| 1311 |
u32 u; |
| 1312 |
u64 counter; |
| 1313 |
unsigned int i; |
| 1314 |
|
| 1315 |
counter = 0; |
| 1316 |
|
| 1317 |
memcpy (salt, b->salt, b->blklen); |
| 1318 |
u = buf_get_le32 (b->salt) + t->idx; |
| 1319 |
buf_put_le32 (salt, u); |
| 1320 |
|
| 1321 |
buf_put_le32 (octet_s_cost, b->s_cost); |
| 1322 |
buf_put_le32 (octet_t_cost, b->t_cost); |
| 1323 |
buf_put_le32 (octet_parallelism, b->parallelism); |
| 1324 |
|
| 1325 |
iov[0].data = salt; |
| 1326 |
iov[0].len = b->blklen; |
| 1327 |
iov[0].off = 0; |
| 1328 |
iov[1].data = octet_s_cost; |
| 1329 |
iov[1].len = 4; |
| 1330 |
iov[1].off = 0; |
| 1331 |
iov[2].data = octet_t_cost; |
| 1332 |
iov[2].len = 4; |
| 1333 |
iov[2].off = 0; |
| 1334 |
iov[3].data = octet_parallelism; |
| 1335 |
iov[3].len = 4; |
| 1336 |
iov[3].off = 0; |
| 1337 |
|
| 1338 |
t->ec = prng_aes_ctr_init (&prng, b, iov, 4); |
| 1339 |
if (t->ec) |
| 1340 |
return; |
| 1341 |
|
| 1342 |
balloon_compute_fill (b, t, salt, &counter); |
| 1343 |
|
| 1344 |
for (i = 0; i < b->t_cost; i++) |
| 1345 |
balloon_compute_mix (prng, b, t, &counter); |
| 1346 |
|
| 1347 |
/* The result is now at the last block. */ |
| 1348 |
|
| 1349 |
prng_aes_ctr_fini (prng); |
| 1350 |
} |
| 1351 |
|
| 1352 |
static gpg_err_code_t |
| 1353 |
balloon_compute_all (balloon_ctx_t b, const struct gcry_kdf_thread_ops *ops) |
| 1354 |
{ |
| 1355 |
unsigned int parallelism = b->parallelism; |
| 1356 |
unsigned int i; |
| 1357 |
int ret; |
| 1358 |
|
| 1359 |
for (i = 0; i < parallelism; i++) |
| 1360 |
{ |
| 1361 |
struct balloon_thread_data *t = &b->thread_data[i]; |
| 1362 |
|
| 1363 |
if (ops) |
| 1364 |
{ |
| 1365 |
ret = ops->dispatch_job (ops->jobs_context, balloon_compute, t); |
| 1366 |
if (ret < 0) |
| 1367 |
return GPG_ERR_CANCELED; |
| 1368 |
} |
| 1369 |
else |
| 1370 |
balloon_compute (t); |
| 1371 |
} |
| 1372 |
|
| 1373 |
if (ops) |
| 1374 |
{ |
| 1375 |
ret = ops->wait_all_jobs (ops->jobs_context); |
| 1376 |
if (ret < 0) |
| 1377 |
return GPG_ERR_CANCELED; |
| 1378 |
} |
| 1379 |
|
| 1380 |
return 0; |
| 1381 |
} |
| 1382 |
|
| 1383 |
static gpg_err_code_t |
| 1384 |
balloon_final (balloon_ctx_t b, size_t resultlen, void *result) |
| 1385 |
{ |
| 1386 |
unsigned int parallelism = b->parallelism; |
| 1387 |
unsigned int i; |
| 1388 |
u64 out[BALLOON_BLOCK_LEN_MAX/8]; |
| 1389 |
|
| 1390 |
if (resultlen != b->blklen) |
| 1391 |
return GPG_ERR_INV_VALUE; |
| 1392 |
|
| 1393 |
memset (out, 0, b->blklen); |
| 1394 |
for (i = 0; i < parallelism; i++) |
| 1395 |
{ |
| 1396 |
struct balloon_thread_data *t = &b->thread_data[i]; |
| 1397 |
const unsigned char *last_block; |
| 1398 |
|
| 1399 |
if (t->ec) |
| 1400 |
return t->ec; |
| 1401 |
|
| 1402 |
last_block = t->block + (b->blklen * (t->b->n_blocks - 1)); |
| 1403 |
balloon_xor_block (b, out, (const u64 *)(void *)last_block); |
| 1404 |
} |
| 1405 |
|
| 1406 |
memcpy (result, out, b->blklen); |
| 1407 |
|
| 1408 |
return 0; |
| 1409 |
} |
| 1410 |
|
| 1411 |
static void |
| 1412 |
balloon_close (balloon_ctx_t b) |
| 1413 |
{ |
| 1414 |
unsigned int parallelism = b->parallelism; |
| 1415 |
size_t n = ballon_context_size (parallelism); |
| 1416 |
|
| 1417 |
if (b->block) |
| 1418 |
{ |
| 1419 |
wipememory (b->block, parallelism * b->n_blocks); |
| 1420 |
xfree (b->block); |
| 1421 |
} |
| 1422 |
|
| 1423 |
wipememory (b, n); |
| 1424 |
xfree (b); |
| 1425 |
} |
| 1426 |
|
| 1427 |
typedef struct onestep_kdf_context *onestep_kdf_ctx_t; |
| 1428 |
|
| 1429 |
/* OneStepKDF context */ |
| 1430 |
struct onestep_kdf_context { |
| 1431 |
int algo; |
| 1432 |
gcry_md_hd_t md; |
| 1433 |
unsigned int blklen; |
| 1434 |
unsigned int outlen; |
| 1435 |
const void *input; |
| 1436 |
size_t inputlen; |
| 1437 |
const void *fixedinfo; |
| 1438 |
size_t fixedinfolen; |
| 1439 |
}; |
| 1440 |
|
| 1441 |
static gpg_err_code_t |
| 1442 |
onestep_kdf_open (gcry_kdf_hd_t *hd, int hashalgo, |
| 1443 |
const unsigned long *param, unsigned int paramlen, |
| 1444 |
const void *input, size_t inputlen, |
| 1445 |
const void *fixedinfo, size_t fixedinfolen) |
| 1446 |
{ |
| 1447 |
gpg_err_code_t ec; |
| 1448 |
unsigned int outlen; |
| 1449 |
onestep_kdf_ctx_t o; |
| 1450 |
size_t n; |
| 1451 |
|
| 1452 |
if (paramlen != 1) |
| 1453 |
return GPG_ERR_INV_VALUE; |
| 1454 |
else |
| 1455 |
outlen = (unsigned int)param[0]; |
| 1456 |
|
| 1457 |
n = sizeof (struct onestep_kdf_context); |
| 1458 |
o = xtrymalloc (n); |
| 1459 |
if (!o) |
| 1460 |
return gpg_err_code_from_errno (errno); |
| 1461 |
|
| 1462 |
o->blklen = _gcry_md_get_algo_dlen (hashalgo); |
| 1463 |
if (!o->blklen) |
| 1464 |
{ |
| 1465 |
xfree (o); |
| 1466 |
return GPG_ERR_DIGEST_ALGO; |
| 1467 |
} |
| 1468 |
ec = _gcry_md_open (&o->md, hashalgo, 0); |
| 1469 |
if (ec) |
| 1470 |
{ |
| 1471 |
xfree (o); |
| 1472 |
return ec; |
| 1473 |
} |
| 1474 |
o->algo = GCRY_KDF_ONESTEP_KDF; |
| 1475 |
o->outlen = outlen; |
| 1476 |
o->input = input; |
| 1477 |
o->inputlen = inputlen; |
| 1478 |
o->fixedinfo = fixedinfo; |
| 1479 |
o->fixedinfolen = fixedinfolen; |
| 1480 |
|
| 1481 |
*hd = (void *)o; |
| 1482 |
return 0; |
| 1483 |
} |
| 1484 |
|
| 1485 |
|
| 1486 |
static gpg_err_code_t |
| 1487 |
onestep_kdf_compute (onestep_kdf_ctx_t o, const struct gcry_kdf_thread_ops *ops) |
| 1488 |
{ |
| 1489 |
(void)o; |
| 1490 |
|
| 1491 |
if (ops != NULL) |
| 1492 |
return GPG_ERR_INV_VALUE; |
| 1493 |
|
| 1494 |
return 0; |
| 1495 |
} |
| 1496 |
|
| 1497 |
static gpg_err_code_t |
| 1498 |
onestep_kdf_final (onestep_kdf_ctx_t o, size_t resultlen, void *result) |
| 1499 |
{ |
| 1500 |
u32 counter = 0; |
| 1501 |
unsigned char cnt[4]; |
| 1502 |
int i; |
| 1503 |
|
| 1504 |
if (resultlen != o->outlen) |
| 1505 |
return GPG_ERR_INV_VALUE; |
| 1506 |
|
| 1507 |
for (i = 0; i < o->outlen / o->blklen; i++) |
| 1508 |
{ |
| 1509 |
counter++; |
| 1510 |
buf_put_be32 (cnt, counter); |
| 1511 |
_gcry_md_write (o->md, cnt, sizeof (cnt)); |
| 1512 |
_gcry_md_write (o->md, o->input, o->inputlen); |
| 1513 |
_gcry_md_write (o->md, o->fixedinfo, o->fixedinfolen); |
| 1514 |
_gcry_md_final (o->md); |
| 1515 |
memcpy ((char *)result + o->blklen * i, |
| 1516 |
_gcry_md_read (o->md, 0), o->blklen); |
| 1517 |
resultlen -= o->blklen; |
| 1518 |
_gcry_md_reset (o->md); |
| 1519 |
} |
| 1520 |
|
| 1521 |
if (resultlen) |
| 1522 |
{ |
| 1523 |
counter++; |
| 1524 |
buf_put_be32 (cnt, counter); |
| 1525 |
_gcry_md_write (o->md, cnt, sizeof (cnt)); |
| 1526 |
_gcry_md_write (o->md, o->input, o->inputlen); |
| 1527 |
_gcry_md_write (o->md, o->fixedinfo, o->fixedinfolen); |
| 1528 |
_gcry_md_final (o->md); |
| 1529 |
memcpy ((char *)result + o->blklen * i, |
| 1530 |
_gcry_md_read (o->md, 0), resultlen); |
| 1531 |
} |
| 1532 |
|
| 1533 |
return 0; |
| 1534 |
} |
| 1535 |
|
| 1536 |
static void |
| 1537 |
onestep_kdf_close (onestep_kdf_ctx_t o) |
| 1538 |
{ |
| 1539 |
_gcry_md_close (o->md); |
| 1540 |
xfree (o); |
| 1541 |
} |
| 1542 |
|
| 1543 |
typedef struct onestep_kdf_mac_context *onestep_kdf_mac_ctx_t; |
| 1544 |
|
| 1545 |
/* OneStep_KDF_MAC context */ |
| 1546 |
struct onestep_kdf_mac_context { |
| 1547 |
int algo; |
| 1548 |
gcry_mac_hd_t md; |
| 1549 |
unsigned int blklen; |
| 1550 |
unsigned int outlen; |
| 1551 |
const void *input; |
| 1552 |
size_t inputlen; |
| 1553 |
const void *salt; |
| 1554 |
size_t saltlen; |
| 1555 |
const void *fixedinfo; |
| 1556 |
size_t fixedinfolen; |
| 1557 |
}; |
| 1558 |
|
| 1559 |
static gpg_err_code_t |
| 1560 |
onestep_kdf_mac_open (gcry_kdf_hd_t *hd, int macalgo, |
| 1561 |
const unsigned long *param, unsigned int paramlen, |
| 1562 |
const void *input, size_t inputlen, |
| 1563 |
const void *key, size_t keylen, |
| 1564 |
const void *fixedinfo, size_t fixedinfolen) |
| 1565 |
{ |
| 1566 |
gpg_err_code_t ec; |
| 1567 |
unsigned int outlen; |
| 1568 |
onestep_kdf_mac_ctx_t o; |
| 1569 |
size_t n; |
| 1570 |
|
| 1571 |
if (paramlen != 1) |
| 1572 |
return GPG_ERR_INV_VALUE; |
| 1573 |
else |
| 1574 |
outlen = (unsigned int)param[0]; |
| 1575 |
|
| 1576 |
n = sizeof (struct onestep_kdf_mac_context); |
| 1577 |
o = xtrymalloc (n); |
| 1578 |
if (!o) |
| 1579 |
return gpg_err_code_from_errno (errno); |
| 1580 |
|
| 1581 |
o->blklen = _gcry_mac_get_algo_maclen (macalgo); |
| 1582 |
if (!o->blklen) |
| 1583 |
{ |
| 1584 |
xfree (o); |
| 1585 |
return GPG_ERR_MAC_ALGO; |
| 1586 |
} |
| 1587 |
ec = _gcry_mac_open (&o->md, macalgo, 0, NULL); |
| 1588 |
if (ec) |
| 1589 |
{ |
| 1590 |
xfree (o); |
| 1591 |
return ec; |
| 1592 |
} |
| 1593 |
o->algo = GCRY_KDF_ONESTEP_KDF_MAC; |
| 1594 |
o->outlen = outlen; |
| 1595 |
o->input = input; |
| 1596 |
o->inputlen = inputlen; |
| 1597 |
o->salt = key; |
| 1598 |
o->saltlen = keylen; |
| 1599 |
o->fixedinfo = fixedinfo; |
| 1600 |
o->fixedinfolen = fixedinfolen; |
| 1601 |
|
| 1602 |
*hd = (void *)o; |
| 1603 |
return 0; |
| 1604 |
} |
| 1605 |
|
| 1606 |
|
| 1607 |
static gpg_err_code_t |
| 1608 |
onestep_kdf_mac_compute (onestep_kdf_mac_ctx_t o, |
| 1609 |
const struct gcry_kdf_thread_ops *ops) |
| 1610 |
{ |
| 1611 |
(void)o; |
| 1612 |
|
| 1613 |
if (ops != NULL) |
| 1614 |
return GPG_ERR_INV_VALUE; |
| 1615 |
|
| 1616 |
return 0; |
| 1617 |
} |
| 1618 |
|
| 1619 |
static gpg_err_code_t |
| 1620 |
onestep_kdf_mac_final (onestep_kdf_mac_ctx_t o, size_t resultlen, void *result) |
| 1621 |
{ |
| 1622 |
u32 counter = 0; |
| 1623 |
unsigned char cnt[4]; |
| 1624 |
int i; |
| 1625 |
gcry_err_code_t ec; |
| 1626 |
size_t len = o->blklen; |
| 1627 |
|
| 1628 |
if (resultlen != o->outlen) |
| 1629 |
return GPG_ERR_INV_VALUE; |
| 1630 |
|
| 1631 |
ec = _gcry_mac_setkey (o->md, o->salt, o->saltlen); |
| 1632 |
if (ec) |
| 1633 |
return ec; |
| 1634 |
|
| 1635 |
for (i = 0; i < o->outlen / o->blklen; i++) |
| 1636 |
{ |
| 1637 |
counter++; |
| 1638 |
buf_put_be32 (cnt, counter); |
| 1639 |
ec = _gcry_mac_write (o->md, cnt, sizeof (cnt)); |
| 1640 |
if (ec) |
| 1641 |
return ec; |
| 1642 |
ec = _gcry_mac_write (o->md, o->input, o->inputlen); |
| 1643 |
if (ec) |
| 1644 |
return ec; |
| 1645 |
ec = _gcry_mac_write (o->md, o->fixedinfo, o->fixedinfolen); |
| 1646 |
if (ec) |
| 1647 |
return ec; |
| 1648 |
ec = _gcry_mac_read (o->md, (char *)result + o->blklen * i, &len); |
| 1649 |
if (ec) |
| 1650 |
return ec; |
| 1651 |
resultlen -= o->blklen; |
| 1652 |
ec = _gcry_mac_ctl (o->md, GCRYCTL_RESET, NULL, 0); |
| 1653 |
if (ec) |
| 1654 |
return ec; |
| 1655 |
} |
| 1656 |
|
| 1657 |
if (resultlen) |
| 1658 |
{ |
| 1659 |
counter++; |
| 1660 |
len = resultlen; |
| 1661 |
buf_put_be32 (cnt, counter); |
| 1662 |
ec = _gcry_mac_write (o->md, cnt, sizeof (cnt)); |
| 1663 |
if (ec) |
| 1664 |
return ec; |
| 1665 |
ec = _gcry_mac_write (o->md, o->input, o->inputlen); |
| 1666 |
if (ec) |
| 1667 |
return ec; |
| 1668 |
ec =_gcry_mac_write (o->md, o->fixedinfo, o->fixedinfolen); |
| 1669 |
if (ec) |
| 1670 |
return ec; |
| 1671 |
ec = _gcry_mac_read (o->md, (char *)result + o->blklen * i, &len); |
| 1672 |
if (ec) |
| 1673 |
return ec; |
| 1674 |
} |
| 1675 |
|
| 1676 |
return 0; |
| 1677 |
} |
| 1678 |
|
| 1679 |
static void |
| 1680 |
onestep_kdf_mac_close (onestep_kdf_mac_ctx_t o) |
| 1681 |
{ |
| 1682 |
_gcry_mac_close (o->md); |
| 1683 |
xfree (o); |
| 1684 |
} |
| 1685 |
|
| 1686 |
typedef struct hkdf_context *hkdf_ctx_t; |
| 1687 |
|
| 1688 |
/* Hkdf context */ |
| 1689 |
struct hkdf_context { |
| 1690 |
int algo; |
| 1691 |
gcry_mac_hd_t md; |
| 1692 |
int mode; |
| 1693 |
unsigned int blklen; |
| 1694 |
unsigned int outlen; |
| 1695 |
const void *input; |
| 1696 |
size_t inputlen; |
| 1697 |
const void *salt; |
| 1698 |
size_t saltlen; |
| 1699 |
const void *fixedinfo; |
| 1700 |
size_t fixedinfolen; |
| 1701 |
unsigned char *prk; |
| 1702 |
}; |
| 1703 |
|
| 1704 |
static gpg_err_code_t |
| 1705 |
hkdf_open (gcry_kdf_hd_t *hd, int macalgo, |
| 1706 |
const unsigned long *param, unsigned int paramlen, |
| 1707 |
const void *input, size_t inputlen, |
| 1708 |
const void *salt, size_t saltlen, |
| 1709 |
const void *fixedinfo, size_t fixedinfolen) |
| 1710 |
{ |
| 1711 |
gpg_err_code_t ec; |
| 1712 |
unsigned int outlen; |
| 1713 |
int mode; |
| 1714 |
hkdf_ctx_t h; |
| 1715 |
size_t n; |
| 1716 |
unsigned char *prk; |
| 1717 |
|
| 1718 |
if (paramlen != 1 && paramlen != 2) |
| 1719 |
return GPG_ERR_INV_VALUE; |
| 1720 |
else |
| 1721 |
{ |
| 1722 |
outlen = (unsigned int)param[0]; |
| 1723 |
/* MODE: support extract only, expand only: FIXME*/ |
| 1724 |
if (paramlen == 2) |
| 1725 |
mode = (unsigned int)param[1]; |
| 1726 |
else |
| 1727 |
mode = 0; |
| 1728 |
} |
| 1729 |
|
| 1730 |
n = sizeof (struct hkdf_context); |
| 1731 |
h = xtrymalloc (n); |
| 1732 |
if (!h) |
| 1733 |
return gpg_err_code_from_errno (errno); |
| 1734 |
|
| 1735 |
h->blklen = _gcry_mac_get_algo_maclen (macalgo); |
| 1736 |
if (!h->blklen) |
| 1737 |
{ |
| 1738 |
xfree (h); |
| 1739 |
return GPG_ERR_MAC_ALGO; |
| 1740 |
} |
| 1741 |
|
| 1742 |
if (outlen > 255 * h->blklen) |
| 1743 |
{ |
| 1744 |
xfree (h); |
| 1745 |
return GPG_ERR_INV_VALUE; |
| 1746 |
} |
| 1747 |
|
| 1748 |
ec = _gcry_mac_open (&h->md, macalgo, 0, NULL); |
| 1749 |
if (ec) |
| 1750 |
{ |
| 1751 |
xfree (h); |
| 1752 |
return ec; |
| 1753 |
} |
| 1754 |
prk = xtrymalloc (h->blklen); |
| 1755 |
if (!prk) |
| 1756 |
{ |
| 1757 |
_gcry_mac_close (h->md); |
| 1758 |
xfree (h); |
| 1759 |
return gpg_err_code_from_errno (errno); |
| 1760 |
} |
| 1761 |
h->prk = prk; |
| 1762 |
h->algo = GCRY_KDF_HKDF; |
| 1763 |
h->outlen = outlen; |
| 1764 |
h->mode = mode; |
| 1765 |
h->input = input; |
| 1766 |
h->inputlen = inputlen; |
| 1767 |
h->salt = salt; |
| 1768 |
h->saltlen = saltlen; |
| 1769 |
h->fixedinfo = fixedinfo; |
| 1770 |
h->fixedinfolen = fixedinfolen; |
| 1771 |
|
| 1772 |
*hd = (void *)h; |
| 1773 |
return 0; |
| 1774 |
} |
| 1775 |
|
| 1776 |
|
| 1777 |
static gpg_err_code_t |
| 1778 |
hkdf_compute (hkdf_ctx_t h, const struct gcry_kdf_thread_ops *ops) |
| 1779 |
{ |
| 1780 |
gcry_err_code_t ec; |
| 1781 |
size_t len = h->blklen; |
| 1782 |
|
| 1783 |
if (ops != NULL) |
| 1784 |
return GPG_ERR_INV_VALUE; |
| 1785 |
|
| 1786 |
/* Extract */ |
| 1787 |
ec = _gcry_mac_setkey (h->md, h->salt, h->saltlen); |
| 1788 |
if (ec) |
| 1789 |
return ec; |
| 1790 |
|
| 1791 |
ec = _gcry_mac_write (h->md, h->input, h->inputlen); |
| 1792 |
if (ec) |
| 1793 |
return ec; |
| 1794 |
|
| 1795 |
ec = _gcry_mac_read (h->md, h->prk, &len); |
| 1796 |
if (ec) |
| 1797 |
return ec; |
| 1798 |
|
| 1799 |
ec = _gcry_mac_ctl (h->md, GCRYCTL_RESET, NULL, 0); |
| 1800 |
if (ec) |
| 1801 |
return ec; |
| 1802 |
|
| 1803 |
return 0; |
| 1804 |
} |
| 1805 |
|
| 1806 |
static gpg_err_code_t |
| 1807 |
hkdf_final (hkdf_ctx_t h, size_t resultlen, void *result) |
| 1808 |
{ |
| 1809 |
unsigned char counter = 0; |
| 1810 |
int i; |
| 1811 |
gcry_err_code_t ec; |
| 1812 |
size_t len = h->blklen; |
| 1813 |
|
| 1814 |
if (resultlen != h->outlen) |
| 1815 |
return GPG_ERR_INV_VALUE; |
| 1816 |
|
| 1817 |
/* Expand */ |
| 1818 |
ec = _gcry_mac_setkey (h->md, h->prk, h->blklen); |
| 1819 |
if (ec) |
| 1820 |
return ec; |
| 1821 |
|
| 1822 |
/* We re-use the memory of ->prk. */ |
| 1823 |
|
| 1824 |
for (i = 0; i < h->outlen / h->blklen; i++) |
| 1825 |
{ |
| 1826 |
counter++; |
| 1827 |
if (i) |
| 1828 |
{ |
| 1829 |
ec = _gcry_mac_write (h->md, h->prk, h->blklen); |
| 1830 |
if (ec) |
| 1831 |
return ec; |
| 1832 |
} |
| 1833 |
if (h->fixedinfo) |
| 1834 |
{ |
| 1835 |
ec = _gcry_mac_write (h->md, h->fixedinfo, h->fixedinfolen); |
| 1836 |
if (ec) |
| 1837 |
return ec; |
| 1838 |
} |
| 1839 |
ec = _gcry_mac_write (h->md, &counter, 1); |
| 1840 |
if (ec) |
| 1841 |
return ec; |
| 1842 |
ec = _gcry_mac_read (h->md, h->prk, &len); |
| 1843 |
if (ec) |
| 1844 |
return ec; |
| 1845 |
memcpy ((char *)result + h->blklen * i, h->prk, len); |
| 1846 |
resultlen -= h->blklen; |
| 1847 |
ec = _gcry_mac_ctl (h->md, GCRYCTL_RESET, NULL, 0); |
| 1848 |
if (ec) |
| 1849 |
return ec; |
| 1850 |
} |
| 1851 |
|
| 1852 |
if (resultlen) |
| 1853 |
{ |
| 1854 |
counter++; |
| 1855 |
len = resultlen; |
| 1856 |
if (i) |
| 1857 |
{ |
| 1858 |
ec = _gcry_mac_write (h->md, h->prk, h->blklen); |
| 1859 |
if (ec) |
| 1860 |
return ec; |
| 1861 |
} |
| 1862 |
if (h->fixedinfo) |
| 1863 |
{ |
| 1864 |
ec = _gcry_mac_write (h->md, h->fixedinfo, h->fixedinfolen); |
| 1865 |
if (ec) |
| 1866 |
return ec; |
| 1867 |
} |
| 1868 |
ec = _gcry_mac_write (h->md, &counter, 1); |
| 1869 |
if (ec) |
| 1870 |
return ec; |
| 1871 |
ec = _gcry_mac_read (h->md, (char *)result + h->blklen * i, &len); |
| 1872 |
if (ec) |
| 1873 |
return ec; |
| 1874 |
} |
| 1875 |
|
| 1876 |
return 0; |
| 1877 |
} |
| 1878 |
|
| 1879 |
static void |
| 1880 |
hkdf_close (hkdf_ctx_t h) |
| 1881 |
{ |
| 1882 |
_gcry_mac_close (h->md); |
| 1883 |
xfree (h->prk); |
| 1884 |
xfree (h); |
| 1885 |
} |
| 1886 |
|
| 1887 |
typedef struct x963_kdf_context *x963_kdf_ctx_t; |
| 1888 |
|
| 1889 |
/* X963KDF context */ |
| 1890 |
struct x963_kdf_context { |
| 1891 |
int algo; |
| 1892 |
gcry_md_hd_t md; |
| 1893 |
unsigned int blklen; |
| 1894 |
unsigned int outlen; |
| 1895 |
const void *input; |
| 1896 |
size_t inputlen; |
| 1897 |
const void *sharedinfo; |
| 1898 |
size_t sharedinfolen; |
| 1899 |
}; |
| 1900 |
|
| 1901 |
static gpg_err_code_t |
| 1902 |
x963_kdf_open (gcry_kdf_hd_t *hd, int hashalgo, |
| 1903 |
const unsigned long *param, unsigned int paramlen, |
| 1904 |
const void *input, size_t inputlen, |
| 1905 |
const void *sharedinfo, size_t sharedinfolen) |
| 1906 |
{ |
| 1907 |
gpg_err_code_t ec; |
| 1908 |
unsigned int outlen; |
| 1909 |
x963_kdf_ctx_t o; |
| 1910 |
size_t n; |
| 1911 |
|
| 1912 |
if (paramlen != 1) |
| 1913 |
return GPG_ERR_INV_VALUE; |
| 1914 |
else |
| 1915 |
outlen = (unsigned int)param[0]; |
| 1916 |
|
| 1917 |
n = sizeof (struct x963_kdf_context); |
| 1918 |
o = xtrymalloc (n); |
| 1919 |
if (!o) |
| 1920 |
return gpg_err_code_from_errno (errno); |
| 1921 |
|
| 1922 |
o->blklen = _gcry_md_get_algo_dlen (hashalgo); |
| 1923 |
if (!o->blklen) |
| 1924 |
{ |
| 1925 |
xfree (o); |
| 1926 |
return GPG_ERR_DIGEST_ALGO; |
| 1927 |
} |
| 1928 |
ec = _gcry_md_open (&o->md, hashalgo, 0); |
| 1929 |
if (ec) |
| 1930 |
{ |
| 1931 |
xfree (o); |
| 1932 |
return ec; |
| 1933 |
} |
| 1934 |
o->algo = GCRY_KDF_X963_KDF; |
| 1935 |
o->outlen = outlen; |
| 1936 |
o->input = input; |
| 1937 |
o->inputlen = inputlen; |
| 1938 |
o->sharedinfo = sharedinfo; |
| 1939 |
o->sharedinfolen = sharedinfolen; |
| 1940 |
|
| 1941 |
*hd = (void *)o; |
| 1942 |
return 0; |
| 1943 |
} |
| 1944 |
|
| 1945 |
|
| 1946 |
static gpg_err_code_t |
| 1947 |
x963_kdf_compute (x963_kdf_ctx_t o, const struct gcry_kdf_thread_ops *ops) |
| 1948 |
{ |
| 1949 |
(void)o; |
| 1950 |
|
| 1951 |
if (ops != NULL) |
| 1952 |
return GPG_ERR_INV_VALUE; |
| 1953 |
|
| 1954 |
return 0; |
| 1955 |
} |
| 1956 |
|
| 1957 |
static gpg_err_code_t |
| 1958 |
x963_kdf_final (x963_kdf_ctx_t o, size_t resultlen, void *result) |
| 1959 |
{ |
| 1960 |
u32 counter = 0; |
| 1961 |
unsigned char cnt[4]; |
| 1962 |
int i; |
| 1963 |
|
| 1964 |
if (resultlen != o->outlen) |
| 1965 |
return GPG_ERR_INV_VALUE; |
| 1966 |
|
| 1967 |
for (i = 0; i < o->outlen / o->blklen; i++) |
| 1968 |
{ |
| 1969 |
counter++; |
| 1970 |
_gcry_md_write (o->md, o->input, o->inputlen); |
| 1971 |
buf_put_be32 (cnt, counter); |
| 1972 |
_gcry_md_write (o->md, cnt, sizeof (cnt)); |
| 1973 |
if (o->sharedinfolen) |
| 1974 |
_gcry_md_write (o->md, o->sharedinfo, o->sharedinfolen); |
| 1975 |
_gcry_md_final (o->md); |
| 1976 |
memcpy ((char *)result + o->blklen * i, |
| 1977 |
_gcry_md_read (o->md, 0), o->blklen); |
| 1978 |
resultlen -= o->blklen; |
| 1979 |
_gcry_md_reset (o->md); |
| 1980 |
} |
| 1981 |
|
| 1982 |
if (resultlen) |
| 1983 |
{ |
| 1984 |
counter++; |
| 1985 |
_gcry_md_write (o->md, o->input, o->inputlen); |
| 1986 |
buf_put_be32 (cnt, counter); |
| 1987 |
_gcry_md_write (o->md, cnt, sizeof (cnt)); |
| 1988 |
if (o->sharedinfolen) |
| 1989 |
_gcry_md_write (o->md, o->sharedinfo, o->sharedinfolen); |
| 1990 |
_gcry_md_final (o->md); |
| 1991 |
memcpy ((char *)result + o->blklen * i, |
| 1992 |
_gcry_md_read (o->md, 0), resultlen); |
| 1993 |
} |
| 1994 |
|
| 1995 |
return 0; |
| 1996 |
} |
| 1997 |
|
| 1998 |
static void |
| 1999 |
x963_kdf_close (x963_kdf_ctx_t o) |
| 2000 |
{ |
| 2001 |
_gcry_md_close (o->md); |
| 2002 |
xfree (o); |
| 2003 |
} |
| 2004 |
|
| 2005 |
struct gcry_kdf_handle { |
| 2006 |
int algo; |
| 2007 |
/* And algo specific parts come. */ |
| 2008 |
}; |
| 2009 |
|
| 2010 |
gpg_err_code_t |
| 2011 |
_gcry_kdf_open (gcry_kdf_hd_t *hd, int algo, int subalgo, |
| 2012 |
const unsigned long *param, unsigned int paramlen, |
| 2013 |
const void *input, size_t inputlen, |
| 2014 |
const void *salt, size_t saltlen, |
| 2015 |
const void *key, size_t keylen, |
| 2016 |
const void *ad, size_t adlen) |
| 2017 |
{ |
| 2018 |
gpg_err_code_t ec; |
| 2019 |
|
| 2020 |
switch (algo) |
| 2021 |
{ |
| 2022 |
case GCRY_KDF_ARGON2: |
| 2023 |
if (!saltlen) |
| 2024 |
ec = GPG_ERR_INV_VALUE; |
| 2025 |
else |
| 2026 |
ec = argon2_open (hd, subalgo, param, paramlen, |
| 2027 |
input, inputlen, salt, saltlen, |
| 2028 |
key, keylen, ad, adlen); |
| 2029 |
break; |
| 2030 |
|
| 2031 |
case GCRY_KDF_BALLOON: |
| 2032 |
if (!inputlen || !saltlen || keylen || adlen) |
| 2033 |
ec = GPG_ERR_INV_VALUE; |
| 2034 |
else |
| 2035 |
{ |
| 2036 |
(void)key; |
| 2037 |
(void)ad; |
| 2038 |
ec = balloon_open (hd, subalgo, param, paramlen, |
| 2039 |
input, inputlen, salt, saltlen); |
| 2040 |
} |
| 2041 |
break; |
| 2042 |
|
| 2043 |
case GCRY_KDF_ONESTEP_KDF: |
| 2044 |
if (!inputlen || !paramlen || !adlen) |
| 2045 |
ec = GPG_ERR_INV_VALUE; |
| 2046 |
else |
| 2047 |
{ |
| 2048 |
(void)salt; |
| 2049 |
(void)key; |
| 2050 |
ec = onestep_kdf_open (hd, subalgo, param, paramlen, |
| 2051 |
input, inputlen, ad, adlen); |
| 2052 |
} |
| 2053 |
break; |
| 2054 |
|
| 2055 |
case GCRY_KDF_ONESTEP_KDF_MAC: |
| 2056 |
if (!inputlen || !paramlen || !keylen || !adlen) |
| 2057 |
ec = GPG_ERR_INV_VALUE; |
| 2058 |
else |
| 2059 |
{ |
| 2060 |
(void)salt; |
| 2061 |
ec = onestep_kdf_mac_open (hd, subalgo, param, paramlen, |
| 2062 |
input, inputlen, key, keylen, ad, adlen); |
| 2063 |
} |
| 2064 |
break; |
| 2065 |
|
| 2066 |
case GCRY_KDF_HKDF: |
| 2067 |
if (!inputlen || !paramlen) |
| 2068 |
ec = GPG_ERR_INV_VALUE; |
| 2069 |
else |
| 2070 |
{ |
| 2071 |
(void)salt; |
| 2072 |
ec = hkdf_open (hd, subalgo, param, paramlen, |
| 2073 |
input, inputlen, key, keylen, ad, adlen); |
| 2074 |
} |
| 2075 |
break; |
| 2076 |
|
| 2077 |
case GCRY_KDF_X963_KDF: |
| 2078 |
if (!inputlen || !paramlen) |
| 2079 |
ec = GPG_ERR_INV_VALUE; |
| 2080 |
else |
| 2081 |
{ |
| 2082 |
(void)salt; |
| 2083 |
(void)key; |
| 2084 |
ec = x963_kdf_open (hd, subalgo, param, paramlen, |
| 2085 |
input, inputlen, ad, adlen); |
| 2086 |
} |
| 2087 |
break; |
| 2088 |
|
| 2089 |
default: |
| 2090 |
ec = GPG_ERR_UNKNOWN_ALGORITHM; |
| 2091 |
break; |
| 2092 |
} |
| 2093 |
|
| 2094 |
return ec; |
| 2095 |
} |
| 2096 |
|
| 2097 |
gpg_err_code_t |
| 2098 |
_gcry_kdf_compute (gcry_kdf_hd_t h, const struct gcry_kdf_thread_ops *ops) |
| 2099 |
{ |
| 2100 |
gpg_err_code_t ec; |
| 2101 |
|
| 2102 |
switch (h->algo) |
| 2103 |
{ |
| 2104 |
case GCRY_KDF_ARGON2: |
| 2105 |
ec = argon2_compute ((argon2_ctx_t)(void *)h, ops); |
| 2106 |
break; |
| 2107 |
|
| 2108 |
case GCRY_KDF_BALLOON: |
| 2109 |
ec = balloon_compute_all ((balloon_ctx_t)(void *)h, ops); |
| 2110 |
break; |
| 2111 |
|
| 2112 |
case GCRY_KDF_ONESTEP_KDF: |
| 2113 |
ec = onestep_kdf_compute ((onestep_kdf_ctx_t)(void *)h, ops); |
| 2114 |
break; |
| 2115 |
|
| 2116 |
case GCRY_KDF_ONESTEP_KDF_MAC: |
| 2117 |
ec = onestep_kdf_mac_compute ((onestep_kdf_mac_ctx_t)(void *)h, ops); |
| 2118 |
break; |
| 2119 |
|
| 2120 |
case GCRY_KDF_HKDF: |
| 2121 |
ec = hkdf_compute ((hkdf_ctx_t)(void *)h, ops); |
| 2122 |
break; |
| 2123 |
|
| 2124 |
case GCRY_KDF_X963_KDF: |
| 2125 |
ec = x963_kdf_compute ((x963_kdf_ctx_t)(void *)h, ops); |
| 2126 |
break; |
| 2127 |
|
| 2128 |
default: |
| 2129 |
ec = GPG_ERR_UNKNOWN_ALGORITHM; |
| 2130 |
break; |
| 2131 |
} |
| 2132 |
|
| 2133 |
return ec; |
| 2134 |
} |
| 2135 |
|
| 2136 |
|
| 2137 |
gpg_err_code_t |
| 2138 |
_gcry_kdf_final (gcry_kdf_hd_t h, size_t resultlen, void *result) |
| 2139 |
{ |
| 2140 |
gpg_err_code_t ec; |
| 2141 |
|
| 2142 |
switch (h->algo) |
| 2143 |
{ |
| 2144 |
case GCRY_KDF_ARGON2: |
| 2145 |
ec = argon2_final ((argon2_ctx_t)(void *)h, resultlen, result); |
| 2146 |
break; |
| 2147 |
|
| 2148 |
case GCRY_KDF_BALLOON: |
| 2149 |
ec = balloon_final ((balloon_ctx_t)(void *)h, resultlen, result); |
| 2150 |
break; |
| 2151 |
|
| 2152 |
case GCRY_KDF_ONESTEP_KDF: |
| 2153 |
ec = onestep_kdf_final ((onestep_kdf_ctx_t)(void *)h, resultlen, result); |
| 2154 |
break; |
| 2155 |
|
| 2156 |
case GCRY_KDF_ONESTEP_KDF_MAC: |
| 2157 |
ec = onestep_kdf_mac_final ((onestep_kdf_mac_ctx_t)(void *)h, |
| 2158 |
resultlen, result); |
| 2159 |
break; |
| 2160 |
|
| 2161 |
case GCRY_KDF_HKDF: |
| 2162 |
ec = hkdf_final ((hkdf_ctx_t)(void *)h, resultlen, result); |
| 2163 |
break; |
| 2164 |
|
| 2165 |
case GCRY_KDF_X963_KDF: |
| 2166 |
ec = x963_kdf_final ((x963_kdf_ctx_t)(void *)h, resultlen, result); |
| 2167 |
break; |
| 2168 |
|
| 2169 |
default: |
| 2170 |
ec = GPG_ERR_UNKNOWN_ALGORITHM; |
| 2171 |
break; |
| 2172 |
} |
| 2173 |
|
| 2174 |
return ec; |
| 2175 |
} |
| 2176 |
|
| 2177 |
void |
| 2178 |
_gcry_kdf_close (gcry_kdf_hd_t h) |
| 2179 |
{ |
| 2180 |
switch (h->algo) |
| 2181 |
{ |
| 2182 |
case GCRY_KDF_ARGON2: |
| 2183 |
argon2_close ((argon2_ctx_t)(void *)h); |
| 2184 |
break; |
| 2185 |
|
| 2186 |
case GCRY_KDF_BALLOON: |
| 2187 |
balloon_close ((balloon_ctx_t)(void *)h); |
| 2188 |
break; |
| 2189 |
|
| 2190 |
case GCRY_KDF_ONESTEP_KDF: |
| 2191 |
onestep_kdf_close ((onestep_kdf_ctx_t)(void *)h); |
| 2192 |
break; |
| 2193 |
|
| 2194 |
case GCRY_KDF_ONESTEP_KDF_MAC: |
| 2195 |
onestep_kdf_mac_close ((onestep_kdf_mac_ctx_t)(void *)h); |
| 2196 |
break; |
| 2197 |
|
| 2198 |
case GCRY_KDF_HKDF: |
| 2199 |
hkdf_close ((hkdf_ctx_t)(void *)h); |
| 2200 |
break; |
| 2201 |
|
| 2202 |
case GCRY_KDF_X963_KDF: |
| 2203 |
x963_kdf_close ((x963_kdf_ctx_t)(void *)h); |
| 2204 |
break; |
| 2205 |
|
| 2206 |
default: |
| 2207 |
break; |
| 2208 |
} |
| 2209 |
} |
| 2210 |
|
| 2211 |
/* Check one KDF call with ALGO and HASH_ALGO using the regular KDF |
| 2212 |
* API. (passphrase,passphraselen) is the password to be derived, |
| 2213 |
* (salt,saltlen) the salt for the key derivation, |
| 2214 |
* iterations is the number of the kdf iterations, |
| 2215 |
* and (expect,expectlen) the expected result. Returns NULL on |
| 2216 |
* success or a string describing the failure. */ |
| 2217 |
|
| 2218 |
static const char * |
| 2219 |
check_one (int algo, int hash_algo, |
| 2220 |
const void *passphrase, size_t passphraselen, |
| 2221 |
const void *salt, size_t saltlen, |
| 2222 |
unsigned long iterations, |
| 2223 |
const void *expect, size_t expectlen) |
| 2224 |
{ |
| 2225 |
unsigned char key[512]; /* hardcoded to avoid allocation */ |
| 2226 |
size_t keysize = expectlen; |
| 2227 |
int rv; |
| 2228 |
|
| 2229 |
if (keysize > sizeof(key)) |
| 2230 |
return "invalid tests data"; |
| 2231 |
|
| 2232 |
rv = _gcry_kdf_derive (passphrase, passphraselen, algo, |
| 2233 |
hash_algo, salt, saltlen, iterations, |
| 2234 |
keysize, key); |
| 2235 |
/* In fips mode we have special requirements for the input and |
| 2236 |
* output parameters */ |
| 2237 |
if (fips_mode ()) |
| 2238 |
{ |
| 2239 |
if (rv && (passphraselen < 8 || saltlen < 16 || |
| 2240 |
iterations < 1000 || expectlen < 14)) |
| 2241 |
return NULL; |
| 2242 |
else if (rv) |
| 2243 |
return "gcry_kdf_derive unexpectedly failed in FIPS Mode"; |
| 2244 |
} |
| 2245 |
else if (rv) |
| 2246 |
return "gcry_kdf_derive failed"; |
| 2247 |
|
| 2248 |
if (memcmp (key, expect, expectlen)) |
| 2249 |
return "does not match"; |
| 2250 |
|
| 2251 |
return NULL; |
| 2252 |
} |
| 2253 |
|
| 2254 |
|
| 2255 |
static gpg_err_code_t |
| 2256 |
selftest_pbkdf2 (int extended, selftest_report_func_t report) |
| 2257 |
{ |
| 2258 |
static const struct { |
| 2259 |
const char *desc; |
| 2260 |
const char *p; /* Passphrase. */ |
| 2261 |
size_t plen; /* Length of P. */ |
| 2262 |
const char *salt; |
| 2263 |
size_t saltlen; |
| 2264 |
int hashalgo; |
| 2265 |
unsigned long c; /* Iterations. */ |
| 2266 |
int dklen; /* Requested key length. */ |
| 2267 |
const char *dk; /* Derived key. */ |
| 2268 |
int disabled; |
| 2269 |
} tv[] = { |
| 2270 |
#if USE_SHA1 |
| 2271 |
#define NUM_TEST_VECTORS 9 |
| 2272 |
/* SHA1 test vectors are from RFC-6070. */ |
| 2273 |
{ |
| 2274 |
"Basic PBKDF2 SHA1 #1", |
| 2275 |
"password", 8, |
| 2276 |
"salt", 4, |
| 2277 |
GCRY_MD_SHA1, |
| 2278 |
1, |
| 2279 |
20, |
| 2280 |
"\x0c\x60\xc8\x0f\x96\x1f\x0e\x71\xf3\xa9" |
| 2281 |
"\xb5\x24\xaf\x60\x12\x06\x2f\xe0\x37\xa6" |
| 2282 |
}, |
| 2283 |
{ |
| 2284 |
"Basic PBKDF2 SHA1 #2", |
| 2285 |
"password", 8, |
| 2286 |
"salt", 4, |
| 2287 |
GCRY_MD_SHA1, |
| 2288 |
2, |
| 2289 |
20, |
| 2290 |
"\xea\x6c\x01\x4d\xc7\x2d\x6f\x8c\xcd\x1e" |
| 2291 |
"\xd9\x2a\xce\x1d\x41\xf0\xd8\xde\x89\x57" |
| 2292 |
}, |
| 2293 |
{ |
| 2294 |
"Basic PBKDF2 SHA1 #3", |
| 2295 |
"password", 8, |
| 2296 |
"salt", 4, |
| 2297 |
GCRY_MD_SHA1, |
| 2298 |
4096, |
| 2299 |
20, |
| 2300 |
"\x4b\x00\x79\x01\xb7\x65\x48\x9a\xbe\xad" |
| 2301 |
"\x49\xd9\x26\xf7\x21\xd0\x65\xa4\x29\xc1" |
| 2302 |
}, |
| 2303 |
{ |
| 2304 |
"Basic PBKDF2 SHA1 #4", |
| 2305 |
"password", 8, |
| 2306 |
"salt", 4, |
| 2307 |
GCRY_MD_SHA1, |
| 2308 |
16777216, |
| 2309 |
20, |
| 2310 |
"\xee\xfe\x3d\x61\xcd\x4d\xa4\xe4\xe9\x94" |
| 2311 |
"\x5b\x3d\x6b\xa2\x15\x8c\x26\x34\xe9\x84", |
| 2312 |
1 /* This test takes too long. */ |
| 2313 |
}, |
| 2314 |
{ |
| 2315 |
"Basic PBKDF2 SHA1 #5", |
| 2316 |
"passwordPASSWORDpassword", 24, |
| 2317 |
"saltSALTsaltSALTsaltSALTsaltSALTsalt", 36, |
| 2318 |
GCRY_MD_SHA1, |
| 2319 |
4096, |
| 2320 |
25, |
| 2321 |
"\x3d\x2e\xec\x4f\xe4\x1c\x84\x9b\x80\xc8" |
| 2322 |
"\xd8\x36\x62\xc0\xe4\x4a\x8b\x29\x1a\x96" |
| 2323 |
"\x4c\xf2\xf0\x70\x38" |
| 2324 |
}, |
| 2325 |
{ |
| 2326 |
"Basic PBKDF2 SHA1 #6", |
| 2327 |
"pass\0word", 9, |
| 2328 |
"sa\0lt", 5, |
| 2329 |
GCRY_MD_SHA1, |
| 2330 |
4096, |
| 2331 |
16, |
| 2332 |
"\x56\xfa\x6a\xa7\x55\x48\x09\x9d\xcc\x37" |
| 2333 |
"\xd7\xf0\x34\x25\xe0\xc3" |
| 2334 |
}, |
| 2335 |
{ /* empty password test, not in RFC-6070 */ |
| 2336 |
"Basic PBKDF2 SHA1 #7", |
| 2337 |
"", 0, |
| 2338 |
"salt", 4, |
| 2339 |
GCRY_MD_SHA1, |
| 2340 |
2, |
| 2341 |
20, |
| 2342 |
"\x13\x3a\x4c\xe8\x37\xb4\xd2\x52\x1e\xe2" |
| 2343 |
"\xbf\x03\xe1\x1c\x71\xca\x79\x4e\x07\x97" |
| 2344 |
}, |
| 2345 |
#else |
| 2346 |
#define NUM_TEST_VECTORS 2 |
| 2347 |
#endif |
| 2348 |
{ |
| 2349 |
"Basic PBKDF2 SHA256", |
| 2350 |
"password", 8, |
| 2351 |
"salt", 4, |
| 2352 |
GCRY_MD_SHA256, |
| 2353 |
2, |
| 2354 |
32, |
| 2355 |
"\xae\x4d\x0c\x95\xaf\x6b\x46\xd3\x2d\x0a\xdf\xf9\x28\xf0\x6d\xd0" |
| 2356 |
"\x2a\x30\x3f\x8e\xf3\xc2\x51\xdf\xd6\xe2\xd8\x5a\x95\x47\x4c\x43" |
| 2357 |
}, |
| 2358 |
{ |
| 2359 |
"Extended PBKDF2 SHA256", |
| 2360 |
"passwordPASSWORDpassword", 24, |
| 2361 |
"saltSALTsaltSALTsaltSALTsaltSALTsalt", 36, |
| 2362 |
GCRY_MD_SHA256, |
| 2363 |
4096, |
| 2364 |
40, |
| 2365 |
"\x34\x8c\x89\xdb\xcb\xd3\x2b\x2f\x32\xd8\x14\xb8\x11\x6e\x84\xcf" |
| 2366 |
"\x2b\x17\x34\x7e\xbc\x18\x00\x18\x1c\x4e\x2a\x1f\xb8\xdd\x53\xe1" |
| 2367 |
"\xc6\x35\x51\x8c\x7d\xac\x47\xe9" |
| 2368 |
}, |
| 2369 |
{ NULL } |
| 2370 |
}; |
| 2371 |
const char *what; |
| 2372 |
const char *errtxt; |
| 2373 |
int tvidx; |
| 2374 |
|
| 2375 |
for (tvidx=0; tv[tvidx].desc; tvidx++) |
| 2376 |
{ |
| 2377 |
what = tv[tvidx].desc; |
| 2378 |
if (tv[tvidx].disabled) |
| 2379 |
continue; |
| 2380 |
errtxt = check_one (GCRY_KDF_PBKDF2, tv[tvidx].hashalgo, |
| 2381 |
tv[tvidx].p, tv[tvidx].plen, |
| 2382 |
tv[tvidx].salt, tv[tvidx].saltlen, |
| 2383 |
tv[tvidx].c, |
| 2384 |
tv[tvidx].dk, tv[tvidx].dklen); |
| 2385 |
if (errtxt) |
| 2386 |
goto failed; |
| 2387 |
if (tvidx >= NUM_TEST_VECTORS - 1 && !extended) |
| 2388 |
break; |
| 2389 |
} |
| 2390 |
|
| 2391 |
return 0; /* Succeeded. */ |
| 2392 |
|
| 2393 |
failed: |
| 2394 |
if (report) |
| 2395 |
report ("kdf", GCRY_KDF_PBKDF2, what, errtxt); |
| 2396 |
return GPG_ERR_SELFTEST_FAILED; |
| 2397 |
} |
| 2398 |
|
| 2399 |
|
| 2400 |
/* Run the selftests for KDF with KDF algorithm ALGO with optional |
| 2401 |
reporting function REPORT. */ |
| 2402 |
gpg_error_t |
| 2403 |
_gcry_kdf_selftest (int algo, int extended, selftest_report_func_t report) |
| 2404 |
{ |
| 2405 |
gcry_err_code_t ec = 0; |
| 2406 |
|
| 2407 |
if (algo == GCRY_KDF_PBKDF2) |
| 2408 |
ec = selftest_pbkdf2 (extended, report); |
| 2409 |
else |
| 2410 |
{ |
| 2411 |
ec = GPG_ERR_UNSUPPORTED_ALGORITHM; |
| 2412 |
if (report) |
| 2413 |
report ("kdf", algo, "module", "algorithm not available"); |
| 2414 |
} |
| 2415 |
return gpg_error (ec); |
| 2416 |
} |