Commit Graph

39 Commits

Author SHA1 Message Date
Milan Broz
e6f6ee9291 Do not allow PBKDF benchmark over maximum allowed threads.
Unfortunatelly the benchmark function cannot return
corrected parallel cost, so it must fail.

Note that some backends (like OpenSSL) also limits maximal thread count,
so currently it was clapped at 4 for luksFormat and 8 for benchmark.

This patch set it all to PBKDF internal parallel limit.
2025-05-26 11:30:37 +02:00
Milan Broz
8364178b38 Update copyright year. 2025-01-15 23:09:36 +01:00
Milan Broz
609277d957 Add a comment about wiping derived key in benchmark.
Fixes: #916
2024-11-23 20:52:19 +01:00
Milan Broz
c0449fc24e Revert "Do not call safe erase on benchmark data."
This reverts commit 17f0261024.

The password can be set by user through expeorted API, we should
not keep traces of derived key from a real password.
2024-11-01 12:59:25 +01:00
Ondrej Kozina
17f0261024 Do not call safe erase on benchmark data.
In both pbkdf2 and argon2* benchmark key variable
is pointer to benchmark data and does not need to be erased
safely as regular key data would need to.
2024-11-01 10:27:24 +01:00
Milan Broz
661f57def4 Use SPDX license identifiers.
This patch switches code to SPDX one-line license identifiers according to
https://spdx.dev/learn/handling-license-info/
and replacing long license text headers.

I used C++ format on the first line in style
// SPDX-License-Identifier: <id>
except exported libcryptsetup.h, when only C comments are used.

The only additional changes are:
- switch backend utf8.c from LGPL2+ to LGPL2.1+ (as in systemd)
- add some additional formatting lines.
2024-06-03 16:38:15 +00:00
Milan Broz
bd0ef58b3a Update copyright notice to include 2024 year. 2024-01-23 16:10:44 +01:00
Milan Broz
72f799b393 Update Copyright year. 2023-02-09 17:11:18 +01:00
Milan Broz
ab975bc1c4 Update copyright year.
And unify format in several places.
2022-01-29 10:43:02 +01:00
Milan Broz
d9fc45de04 Increase minimal memory cost for Argon2 benchmark to 64MiB.
Argon2 draft defines suggested parameters for disk encryption use, but LUKS2
approach is slightly different. We need to provide platform independent
values. The values in draft expects 64bit systems (suggesting using 6 GiB
of RAM), while we need to provide compatibility with all 32bit systems,
so allocating more than 4GiB memory is not option for LUKS2.

The maximal limit in LUKS2 stays for 4 GiB, and by default LUKS2
PBKDF benchmarking sets maximum to 1 GIB, prefering increase of CPU cost.

But for the minimal memory cost we had a quite low limit 32 MiB.

This patch increases the bechmarking value to 64 MiB (as minimal
suggested values in Argon2 RFC). For compatibility reasons we still
allow older limit if set by a parameter.
2021-06-22 13:08:50 +00:00
Milan Broz
4471452105 Remove some stale FIXME markings. 2021-02-11 11:12:11 +00:00
Milan Broz
d1d9dd8e20 Update Copyright year. 2021-01-25 22:00:25 +01:00
lixiaokeng
eff4da95a1 lib: fix memory leak in crypt_pbkdf_check
There is a memory leak when PBKDF2_temp > UINT32_MAX. Here,
we change return to goto out to free key.

Signed-off-by: Lixiaokeng <lixiaokeng@huawei.com>
Signed-off-by: Linfeilong <linfeilong@huawei.com>
2020-11-09 09:57:25 +08:00
Milan Broz
a2c13fbc48 Used CLOCK_MONOTONIC in benchmark on ancient systems. 2020-02-21 10:42:47 +01:00
Milan Broz
080566a1fd Update copyright year. 2020-01-03 13:04:55 +01:00
Milan Broz
4d6269a42d Fix some gcc warnings on 32bit systems. 2019-08-30 09:41:04 +02:00
Milan Broz
a68f3939cf Use min memory limit from PBKDF struct in Argon benchmark. 2019-01-31 10:53:51 +01:00
Milan Broz
a6f5ce8c7b Update copyright year.
And unify name copyright format.
2019-01-25 09:45:57 +01:00
Milan Broz
2300c692b8 Check hash value in pbkdf setting early. 2018-11-22 15:51:10 +01:00
Milan Broz
16dc58312c Move PBKDF limits to crypto backend (to one place). 2018-02-08 15:34:45 +01:00
Milan Broz
1fe014dbae Update copyright year. 2018-01-20 17:55:21 +01:00
Milan Broz
497fb0b3c3 Fix Argon2 benchmark.
1) If the calculated costs were the same, it run forever.

2) If the calculation returned final values in the first step,
out costs were not updated and benchmark returned too low values.
2017-08-16 15:43:11 +02:00
Milan Broz
87dd427d79 Make benchmark progress parameter the same as the internal unsigned type. 2017-08-12 17:55:01 +02:00
Ondrej Mosnáček
15b4f64b91 Fix Argon2 benchmark for decreasing parameters
When we have measured time smaller than target time, we are decreasing
the parameters. Thus, we should first try to decrease t_cost and only
if that is not possible should we try to decrease m_cost instead. The
original logic was only valid for the case where parameters are being
increased. Most notably this caused unusual parameter combinations for
iteration time < 250 ms.

In this commit we also factor out the now heavily nested parameter
update formula.
2017-08-11 16:01:09 +02:00
Milan Broz
9a532080d0 Fix PBKDF2 check to not overflow and not divide by zero. 2017-08-10 18:14:36 +02:00
Milan Broz
4125beb0fb Add callback for PBKDF benchmark.
Also change API so the kdf structure is continuously updated
with the benchmarked data (callback can see progress or debug).
2017-08-10 12:44:24 +02:00
Ondrej Mosnáček
93ea4f4f6f Tweak Argon2 benchmark to finish faster.
The benchmark is taking too long...
2017-08-10 12:39:25 +02:00
Milan Broz
8a859391be Add Argon2 benchmark code.
Code based on patch by Ondrej Mosnacek

The new benchmark works as follows:

Phase 1:
It searches for smallest parameters, such that the duration is 250 ms
(this part is quite fast).
Then it uses that data point to estimate the paramters that will have
the desired duration (and fulfill the basic constraints).

Phase 2:
The candidate parameters are then measured and if their duration falls
within +-5% of the target duration, they are accepted.
Otherwise, new candidate parameters are estimated based on the last
measurement and phase 2 is repeated.

When measuring the duration for given parameters, the measurement
is repeated 3 or 4 times and a minimum of the measured durations
is used as the final duration (to reduce variance in measurements).
A minimum is taken instead of mean, because the measurements definitely
have a certain lower bound, but no upper bound (therefore mean value
would tend to be higher than the value with highest probability density).
The actual "most likely" duration is going to be somewhere just above
the minimum measurable value, so minimum over the observations is
a better estimate than mean.

Signed-off-by: Milan Broz <gmazyland@gmail.com>
2017-08-06 21:30:11 +02:00
Milan Broz
0abf57be5d Change PBKDF interface API.
Prepare API for PBKDF that can set three costs
  - time (similar to iterations in PBKDF2)
  - memory (required memory for memory-hard function)
  - threads (required number of threads/CPUs).

This patch also removes wrongly designed API call
crypt_benchmark_kdf and replaces it with the new call
crypt_benchmark_pbkdf.

Two functions for PBKDF per context setting
are introduced: crypt_set_pbkdf_type and crypt_get_pbkdf_type.

The patch should be backward compatible when using
crypt_set_iteration_time function (works only for PBKDF2).

Signed-off-by: Milan Broz <gmazyland@gmail.com>
2017-08-06 21:24:05 +02:00
Milan Broz
09d14a0b6c Add Argon2 bundled library to crypto backend.
The Argon2i/id is a password hashing function that
won Password Hashing Competiton.

It will be (optionally) used in LUKS2 for passworrd-based
key derivation.

We have to bundle code for now (similar PBKDF2 years ago)
because there is yet no usable implementation in common
crypto libraries.
(Once there is native implementation, cryptsetup
will switch to the crypto library version.)

For now, we use reference (not optimized but portable) implementation.

This patch contains bundled Argon2 algorithm library copied from
  https://github.com/P-H-C/phc-winner-argon2

For more info see Password Hashing Competition site:
  https://password-hashing.net/
and draft of RFC document
  https://datatracker.ietf.org/doc/draft-irtf-cfrg-argon2/

Signed-off-by: Milan Broz <gmazyland@gmail.com>
2017-08-06 21:13:12 +02:00
Milan Broz
98368c4770 Update copyright years. 2017-03-12 13:17:15 +01:00
Milan Broz
67d55d08f8 Fix PBKDF2 benchmark to not double iteration count for corner case.
If measurement function returns exactly 500 ms, the iteration
calculation loop doubles iteration count but instead of repeating
measurement it uses this value directly.

Thanks to Ondrej Mosnacek for bug report.
2016-06-23 09:47:22 +02:00
Milan Broz
4609fd87d7 Fix PBKDF2 iteration benchmark for longer key sizes.
The previous PBKDF2 benchmark code did not take into account
output key length.
For SHA1 (with 160-bits output) and 256-bit keys (and longer)
it means that the final value was higher than it should be.

For other hash algorithms (like SHA256 or SHA512) it caused
that iteration count was smaller (in comparison to SHA1) than
expected for the requested time period.

This patch fixes the code to use key size for the formatted device
(or default LUKS key size if running in informational benchmark mode).

Thanks to A.Visconti, S.Bossi, A.Calo and H.Ragab
(http://www.club.di.unimi.it/) for point this out.
(Based on "What users should know about Full Disk Encryption
based on LUKS" paper to be presented on CANS2015).
2015-10-29 11:52:18 +01:00
Milan Broz
9563aa33c8 Fix PBKDF2 for crypto backens which does not support long HMAC keys.
(Or it rehases key in every iteration.)

- Kernel backens seems not to support >20480 HMAC key
- NSS is slow (without proper key reset)

Add some test vectors (commented out by default).
2014-04-13 19:34:50 +02:00
Milan Broz
7415c5858d Count system time in PBKDF2 benchmark if kernel return no self usage info.
This is kind of workaround for Issue#192...
2013-12-22 10:12:36 +01:00
Milan Broz
83f02e6682 Add copyright line for files I have written or modified. 2012-12-21 16:40:33 +01:00
Milan Broz
80d21c039e Fix some problems found by Coverity scan. 2012-12-10 17:28:52 +01:00
Milan Broz
bd494d23c5 Add PBKDF2 benchmark. 2012-12-05 20:35:42 +01:00
Milan Broz
6123541d80 Add missing pbkdf check file. 2012-11-30 17:05:03 +01:00