/src/rnp/src/lib/crypto/hkdf_botan.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2023, [MTG AG](https://www.mtg.de). |
3 | | * All rights reserved. |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without modification, |
6 | | * are permitted provided that the following conditions are met: |
7 | | * |
8 | | * 1. Redistributions of source code must retain the above copyright notice, |
9 | | * this list of conditions and the following disclaimer. |
10 | | * |
11 | | * 2. Redistributions in binary form must reproduce the above copyright notice, |
12 | | * this list of conditions and the following disclaimer in the documentation |
13 | | * and/or other materials provided with the distribution. |
14 | | * |
15 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
16 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
17 | | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
18 | | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
19 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
21 | | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
22 | | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
23 | | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
24 | | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | | */ |
26 | | |
27 | | #include "config.h" |
28 | | |
29 | | #if defined(ENABLE_CRYPTO_REFRESH) |
30 | | |
31 | | #include "hkdf_botan.hpp" |
32 | | #include "hash_botan.hpp" |
33 | | |
34 | | namespace rnp { |
35 | | |
36 | 0 | Hkdf_Botan::Hkdf_Botan(pgp_hash_alg_t hash_alg) : Hkdf(hash_alg) |
37 | 0 | { |
38 | 0 | } |
39 | | |
40 | | std::unique_ptr<Hkdf_Botan> |
41 | | Hkdf_Botan::create(pgp_hash_alg_t alg) |
42 | 0 | { |
43 | 0 | return std::unique_ptr<Hkdf_Botan>(new Hkdf_Botan(alg)); |
44 | 0 | } |
45 | | |
46 | | std::string |
47 | | Hkdf_Botan::alg() const |
48 | 0 | { |
49 | 0 | return std::string("HKDF(") + Hash_Botan::name_backend(Hkdf::alg()) + ")"; |
50 | 0 | } |
51 | | |
52 | | void |
53 | | Hkdf_Botan::extract_expand(const uint8_t *salt, |
54 | | size_t salt_len, |
55 | | const uint8_t *ikm, |
56 | | size_t ikm_len, |
57 | | const uint8_t *info, |
58 | | size_t info_len, |
59 | | uint8_t * output_buf, |
60 | | size_t output_length) |
61 | 0 | { |
62 | 0 | std::unique_ptr<Botan::KDF> kdf = Botan::KDF::create_or_throw(Hkdf_Botan::alg(), ""); |
63 | |
|
64 | 0 | Botan::secure_vector<uint8_t> OKM; |
65 | 0 | OKM = kdf->derive_key(output_length, ikm, ikm_len, salt, salt_len, info, info_len); |
66 | |
|
67 | 0 | memcpy(output_buf, Botan::unlock(OKM).data(), output_length); |
68 | 0 | } |
69 | | |
70 | | Hkdf_Botan::~Hkdf_Botan() |
71 | 0 | { |
72 | 0 | } |
73 | | |
74 | | } // namespace rnp |
75 | | |
76 | | #endif |