|
1 | 1 | /* |
2 | 2 | * Copyright (c) 2023, stelar7 <[email protected]> |
3 | 3 | * Copyright (c) 2024, Ben Wiederhake <[email protected]> |
| 4 | + * Copyright (c) 2025, Altomani Gianluca <[email protected]> |
4 | 5 | * |
5 | 6 | * SPDX-License-Identifier: BSD-2-Clause |
6 | 7 | */ |
7 | 8 |
|
8 | 9 | #pragma once |
9 | 10 |
|
10 | | -#include <LibCrypto/Authentication/HMAC.h> |
| 11 | +#include <LibCrypto/Hash/HashManager.h> |
11 | 12 |
|
12 | 13 | namespace Crypto::Hash { |
13 | 14 |
|
14 | | -// https://www.rfc-editor.org/rfc/rfc5869#section-2 |
15 | | -template<typename HashT> |
16 | 15 | class HKDF { |
17 | 16 | public: |
18 | | - using HashType = HashT; |
19 | | - using DigestType = typename HashType::DigestType; |
20 | | - using HMACType = typename Crypto::Authentication::HMAC<HashType>; |
| 17 | + HKDF(HashKind hash_kind); |
21 | 18 |
|
22 | 19 | // Note: The output is different for a salt of length zero and an absent salt, |
23 | 20 | // so Optional<ReadonlyBytes> really is the correct type. |
24 | | - static ErrorOr<ByteBuffer> derive_key(Optional<ReadonlyBytes> maybe_salt, ReadonlyBytes input_keying_material, ReadonlyBytes info, u32 output_key_length) |
25 | | - { |
26 | | - if (output_key_length > 255 * DigestType::Size) { |
27 | | - return Error::from_string_view("requested output_key_length is too large"sv); |
28 | | - } |
29 | | - // Note that it feels like we should also refuse to run with output_key_length == 0, |
30 | | - // but the spec allows this. |
31 | | - |
32 | | - // https://www.rfc-editor.org/rfc/rfc5869#section-2.1 |
33 | | - // Note that in the extract step, 'IKM' is used as the HMAC input, not as the HMAC key. |
34 | | - |
35 | | - // salt: optional salt value (a non-secret random value); if not provided, it is set to a string of HashLen zeros. |
36 | | - ByteBuffer salt_buffer; |
37 | | - auto salt = maybe_salt.value_or_lazy_evaluated([&] { |
38 | | - salt_buffer.resize(DigestType::Size, ByteBuffer::ZeroFillNewElements::Yes); |
39 | | - return salt_buffer.bytes(); |
40 | | - }); |
41 | | - HMACType hmac_salt(salt); |
42 | | - |
43 | | - // https://www.rfc-editor.org/rfc/rfc5869#section-2.2 |
44 | | - // PRK = HMAC-Hash(salt, IKM) |
45 | | - auto prk_digest = hmac_salt.process(input_keying_material); |
46 | | - auto prk = prk_digest.bytes(); |
47 | | - ASSERT(prk.size() == DigestType::Size); |
48 | | - |
49 | | - // https://www.rfc-editor.org/rfc/rfc5869#section-2.3 |
50 | | - // N = ceil(L/HashLen) |
51 | | - auto num_iterations = ceil_div(static_cast<size_t>(output_key_length), DigestType::Size); |
52 | | - // T = T(1) | T(2) | T(3) | ... | T(N) |
53 | | - ByteBuffer output_buffer; |
54 | | - // where: |
55 | | - // T(0) = empty string (zero length) |
56 | | - // T(1) = HMAC-Hash(PRK, T(0) | info | 0x01) |
57 | | - // T(2) = HMAC-Hash(PRK, T(1) | info | 0x02) |
58 | | - // T(3) = HMAC-Hash(PRK, T(2) | info | 0x03) |
59 | | - HMACType hmac_prk(prk); |
60 | | - // In iteration i we compute T(i), and deduce T(i - 1) from 'output_buffer'. |
61 | | - // Hence, we do not need to run i == 0. |
62 | | - // INVARIANT: At the beginning of each iteration, hmac_prk is freshly reset. |
63 | | - // For the first iteration, this is given by the constructor of HMAC. |
64 | | - for (size_t i = 1; i < 1 + num_iterations; ++i) { |
65 | | - if (i > 1) { |
66 | | - auto t_i_minus_one = output_buffer.bytes().slice_from_end(DigestType::Size); |
67 | | - hmac_prk.update(t_i_minus_one); |
68 | | - } |
69 | | - hmac_prk.update(info); |
70 | | - u8 const pad_byte = static_cast<u8>(i & 0xff); |
71 | | - hmac_prk.update(ReadonlyBytes(&pad_byte, 1)); |
72 | | - auto t_i_digest = hmac_prk.digest(); |
73 | | - output_buffer.append(t_i_digest.bytes()); |
74 | | - } |
75 | | - |
76 | | - // OKM = first L octets of T |
77 | | - ASSERT(output_buffer.size() >= output_key_length); |
78 | | - output_buffer.trim(output_key_length, false); |
79 | | - |
80 | | - // 5. Output the derived key DK |
81 | | - return { output_buffer }; |
82 | | - } |
| 21 | + ErrorOr<ByteBuffer> derive_key(Optional<ReadonlyBytes> maybe_salt, ReadonlyBytes input_keying_material, ReadonlyBytes info, u32 key_length_bytes); |
83 | 22 |
|
84 | 23 | private: |
85 | | - HKDF() = delete; |
| 24 | + EVP_KDF* m_kdf; |
| 25 | + HashKind m_hash_kind; |
86 | 26 | }; |
87 | 27 |
|
88 | 28 | } |
0 commit comments