]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/sodcrypto.hh
dnsdist: Add HTTPStatusAction to return a specific HTTP response
[thirdparty/pdns.git] / pdns / sodcrypto.hh
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 <string>
25 #include <stdint.h>
26
27 #include <arpa/inet.h>
28
29 #ifndef HAVE_LIBSODIUM
30 struct SodiumNonce
31 {
32 void init(){};
33 void merge(const SodiumNonce& lower, const SodiumNonce& higher) {};
34 void increment(){};
35 unsigned char value[1];
36 };
37 #else
38 #include <sodium.h>
39
40 struct SodiumNonce
41 {
42 void init()
43 {
44 randombytes_buf(value, sizeof value);
45 }
46
47 void merge(const SodiumNonce& lower, const SodiumNonce& higher)
48 {
49 static const size_t halfSize = (sizeof value) / 2;
50 memcpy(value, lower.value, halfSize);
51 memcpy(value + halfSize, higher.value + halfSize, halfSize);
52 }
53
54 void increment()
55 {
56 uint32_t* p = (uint32_t*)value;
57 uint32_t count=htonl(*p);
58 *p=ntohl(++count);
59 }
60
61 string toString() const
62 {
63 return string((const char*)value, crypto_secretbox_NONCEBYTES);
64 }
65
66 unsigned char value[crypto_secretbox_NONCEBYTES];
67 };
68 #endif
69 std::string newKeypair();
70 std::string sodEncryptSym(const std::string& msg, const std::string& key, SodiumNonce&);
71 std::string sodDecryptSym(const std::string& msg, const std::string& key, SodiumNonce&);
72 std::string newKey();
73 bool sodIsValidKey(const std::string& key);