/src/pdns/pdns/dnsdistdist/dnsdist-crypto.hh
Line | Count | Source |
1 | | /* |
2 | | * This file is part of PowerDNS or dnsdist. |
3 | | * Copyright -- PowerDNS.COM B.V. and its contributors |
4 | | * |
5 | | * This program is free software; you can redistribute it and/or modify |
6 | | * it under the terms of version 2 of the GNU General Public License as |
7 | | * published by the Free Software Foundation. |
8 | | * |
9 | | * In addition, for the avoidance of any doubt, permission is granted to |
10 | | * link this program with OpenSSL and to (re)distribute the binaries |
11 | | * produced as the result of such linking. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU General Public License |
19 | | * along with this program; if not, write to the Free Software |
20 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
21 | | */ |
22 | | #pragma once |
23 | | #include "config.h" |
24 | | #include <array> |
25 | | #include <string> |
26 | | #include <cstring> |
27 | | |
28 | | #if defined(HAVE_LIBSODIUM) |
29 | | #include <sodium.h> |
30 | | #endif |
31 | | |
32 | | namespace dnsdist::crypto::authenticated |
33 | | { |
34 | | struct Nonce |
35 | | { |
36 | 0 | Nonce() = default; |
37 | | Nonce(const Nonce&) = default; |
38 | | Nonce(Nonce&&) = default; |
39 | | Nonce& operator=(const Nonce&) = default; |
40 | | Nonce& operator=(Nonce&&) = default; |
41 | | ~Nonce() = default; |
42 | | |
43 | | void init(); |
44 | | void merge(const Nonce& lower, const Nonce& higher); |
45 | | void increment(); |
46 | | |
47 | | #if defined(HAVE_LIBSODIUM) |
48 | | std::array<unsigned char, crypto_secretbox_NONCEBYTES> value{}; |
49 | | #elif defined(HAVE_LIBCRYPTO) |
50 | | // IV is 96 bits |
51 | | std::array<unsigned char, 12> value{}; |
52 | | #else |
53 | | std::array<unsigned char, 1> value{}; |
54 | | #endif |
55 | | }; |
56 | | |
57 | | std::string encryptSym(const std::string_view& msg, const std::string& key, Nonce& nonce, bool incrementNonce = true); |
58 | | std::string decryptSym(const std::string_view& msg, const std::string& key, Nonce& nonce, bool incrementNonce = true); |
59 | | std::string newKey(bool base64Encoded = true); |
60 | | bool isValidKey(const std::string& key); |
61 | | |
62 | | constexpr size_t getEncryptedSize(size_t plainTextSize) |
63 | 0 | { |
64 | 0 | #if defined(HAVE_LIBSODIUM) |
65 | 0 | return plainTextSize + crypto_secretbox_MACBYTES; |
66 | 0 | #elif defined(HAVE_LIBCRYPTO) |
67 | 0 | return plainTextSize + 16U; |
68 | 0 | #else |
69 | 0 | return plainTextSize; |
70 | 0 | #endif |
71 | 0 | } |
72 | | } |