TrinityCore
Loading...
Searching...
No Matches
AES.cpp
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#include "AES.h"
19#include "Errors.h"
20#include <limits>
21
22Trinity::Crypto::AES::AES(bool encrypting, size_t keySizeBits /*= 128*/) : _ctx(EVP_CIPHER_CTX_new()), _encrypting(encrypting)
23{
24 EVP_CIPHER_CTX_init(_ctx);
25 EVP_CIPHER const* cipher = nullptr;
26 switch (keySizeBits)
27 {
28 case 128:
29 cipher = EVP_aes_128_gcm();
30 break;
31 case 192:
32 cipher = EVP_aes_192_gcm();
33 break;
34 case 256:
35 cipher = EVP_aes_256_gcm();
36 break;
37 default:
38 ASSERT(false, "Invalid AES key size " SZFMTD, keySizeBits);
39 }
40
41 int status = EVP_CipherInit_ex(_ctx, cipher, nullptr, nullptr, nullptr, _encrypting ? 1 : 0);
42 ASSERT(status);
43}
44
46{
47 EVP_CIPHER_CTX_free(_ctx);
48}
49
51{
52 int status = EVP_CipherInit_ex(_ctx, nullptr, nullptr, key.data(), nullptr, -1);
53 ASSERT(status);
54}
55
56void Trinity::Crypto::AES::Init(std::span<uint8 const> key)
57{
58 ASSERT(key.size() == size_t(EVP_CIPHER_CTX_get_key_length(_ctx)));
59 int status = EVP_CipherInit_ex(_ctx, nullptr, nullptr, key.data(), nullptr, -1);
60 ASSERT(status);
61}
62
63bool Trinity::Crypto::AES::Process(IV const& iv, uint8* data, size_t length, Tag& tag)
64{
65 ASSERT(length <= static_cast<size_t>(std::numeric_limits<int>::max()));
66 int len = static_cast<int>(length);
67 if (!EVP_CipherInit_ex(_ctx, nullptr, nullptr, nullptr, iv.data(), -1))
68 return false;
69
70 int outLen;
71 if (!EVP_CipherUpdate(_ctx, data, &outLen, data, len))
72 return false;
73
74 len -= outLen;
75
76 if (!_encrypting && !EVP_CIPHER_CTX_ctrl(_ctx, EVP_CTRL_GCM_SET_TAG, sizeof(tag), tag))
77 return false;
78
79 if (!EVP_CipherFinal_ex(_ctx, data + outLen, &outLen))
80 return false;
81
82 ASSERT(len == outLen);
83
84 if (_encrypting && !EVP_CIPHER_CTX_ctrl(_ctx, EVP_CTRL_GCM_GET_TAG, sizeof(tag), tag))
85 return false;
86
87 return true;
88}
89
90bool Trinity::Crypto::AES::ProcessNoIntegrityCheck(IV const& iv, uint8* data, size_t partialLength)
91{
92 ASSERT(!_encrypting, "Partial encryption is not allowed");
93 ASSERT(partialLength <= static_cast<size_t>(std::numeric_limits<int>::max()));
94 int len = static_cast<int>(partialLength);
95 if (!EVP_CipherInit_ex(_ctx, nullptr, nullptr, nullptr, iv.data(), -1))
96 return false;
97
98 int outLen;
99 if (!EVP_CipherUpdate(_ctx, data, &outLen, data, len))
100 return false;
101
102 len -= outLen;
103
104 if (!EVP_CipherFinal_ex(_ctx, data + outLen, &outLen))
105 return false;
106
107 ASSERT(len == outLen);
108
109 return true;
110}
uint8_t uint8
Definition Define.h:156
#define SZFMTD
Definition Define.h:144
#define ASSERT
Definition Errors.h:80
AES(bool encrypting, size_t keySizeBits=128)
Definition AES.cpp:22
uint8[TAG_SIZE_BYTES] Tag
Definition AES.h:37
std::array< uint8, IV_SIZE_BYTES > IV
Definition AES.h:35
bool ProcessNoIntegrityCheck(IV const &iv, uint8 *data, size_t partialLength)
Definition AES.cpp:90
std::array< uint8, KEY_SIZE_BYTES > Key
Definition AES.h:36
EVP_CIPHER_CTX * _ctx
Definition AES.h:53
bool Process(IV const &iv, uint8 *data, size_t length, Tag &tag)
Definition AES.cpp:63
void Init(Key const &key)
Definition AES.cpp:50