]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/sodcrypto.hh
b2b-migrate did not open a transaction, breaking it for lmdb
[thirdparty/pdns.git] / pdns / sodcrypto.hh
CommitLineData
12471842
PL
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 */
6d01c80c 22#pragma once
0b62ec78 23#include "config.h"
6d01c80c 24#include <string>
8c732ebf 25#include <stdint.h>
6d01c80c 26
0b62ec78 27#include <arpa/inet.h>
6d01c80c 28
0b62ec78 29#ifndef HAVE_LIBSODIUM
30struct SodiumNonce
31{
333ea16e
RG
32 void init(){};
33 void merge(const SodiumNonce& lower, const SodiumNonce& higher) {};
34 void increment(){};
35 unsigned char value[1];
0b62ec78 36};
37#else
38#include <sodium.h>
8c732ebf 39
40struct SodiumNonce
41{
42 void init()
43 {
44 randombytes_buf(value, sizeof value);
45 }
333ea16e
RG
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
8c732ebf 54 void increment()
55 {
313ed7c5 56 uint32_t* p = (uint32_t*)value;
57 uint32_t count=htonl(*p);
58 *p=ntohl(++count);
8c732ebf 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};
0b62ec78 68#endif
69std::string newKeypair();
8c732ebf 70std::string sodEncryptSym(const std::string& msg, const std::string& key, SodiumNonce&);
71std::string sodDecryptSym(const std::string& msg, const std::string& key, SodiumNonce&);
72std::string newKey();
9c9b4998 73bool sodIsValidKey(const std::string& key);