TrinityCore
CryptoGenerics.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITY_CRYPTO_GENERICS_HPP
19#define TRINITY_CRYPTO_GENERICS_HPP
20
21#include "BigNumber.h"
22#include "CryptoRandom.h"
23#include "Define.h"
24#include "Errors.h"
25#include <iterator>
26#include <vector>
27
29{
31 {
32 template <typename Cipher>
33 static typename Cipher::IV GenerateRandomIV()
34 {
35 typename Cipher::IV iv;
37 return iv;
38 }
39
40 template <typename Container>
41 static void AppendToBack(std::vector<uint8>& data, Container const& tail)
42 {
43 data.insert(data.end(), std::begin(tail), std::end(tail));
44 }
45
46 template <typename Container>
47 static void SplitFromBack(std::vector<uint8>& data, Container& tail)
48 {
49 ASSERT(data.size() >= std::size(tail));
50 for (size_t i = 1, N = std::size(tail); i <= N; ++i)
51 {
52 tail[N - i] = data.back();
53 data.pop_back();
54 }
55 }
56 };
57}
58
59namespace Trinity::Crypto
60{
61 template <typename Cipher>
62 void AEEncryptWithRandomIV(std::vector<uint8>& data, typename Cipher::Key const& key)
63 {
64 using IV = typename Cipher::IV;
65 using Tag = typename Cipher::Tag;
66 // select random IV
67 IV iv = Trinity::Impl::CryptoGenericsImpl::GenerateRandomIV<Cipher>();
68 Tag tag;
69
70 // encrypt data
71 Cipher cipher(true);
72 cipher.Init(key);
73 bool success = cipher.Process(iv, data.data(), data.size(), tag);
74 ASSERT(success);
75
76 // append trailing IV and tag
79 }
80
81 template <typename Cipher>
82 void AEEncryptWithRandomIV(std::vector<uint8>& data, BigNumber const& key)
83 {
84 AEEncryptWithRandomIV<Cipher>(data, key.ToByteArray<Cipher::KEY_SIZE_BYTES>());
85 }
86
87 template <typename Cipher>
88 bool AEDecrypt(std::vector<uint8>& data, typename Cipher::Key const& key)
89 {
90 using IV = typename Cipher::IV;
91 using Tag = typename Cipher::Tag;
92 // extract trailing IV and tag
93 IV iv;
94 Tag tag;
97
98 // decrypt data
99 Cipher cipher(false);
100 cipher.Init(key);
101 return cipher.Process(iv, data.data(), data.size(), tag);
102 }
103
104 template <typename Cipher>
105 bool AEDecrypt(std::vector<uint8>& data, BigNumber const& key)
106 {
107 return AEDecrypt<Cipher>(data, key.ToByteArray<Cipher::KEY_SIZE_BYTES>());
108 }
109}
110
111#endif
#define ASSERT
Definition: Errors.h:68
std::array< uint8, Size > ToByteArray(bool littleEndian=true) const
Definition: BigNumber.h:128
void AEEncryptWithRandomIV(std::vector< uint8 > &data, typename Cipher::Key const &key)
void TC_COMMON_API GetRandomBytes(uint8 *buf, size_t len)
bool AEDecrypt(std::vector< uint8 > &data, typename Cipher::Key const &key)
constexpr std::size_t size()
Definition: UpdateField.h:796
static Cipher::IV GenerateRandomIV()
static void SplitFromBack(std::vector< uint8 > &data, Container &tail)
static void AppendToBack(std::vector< uint8 > &data, Container const &tail)