TrinityCore
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) : _ctx(EVP_CIPHER_CTX_new()), _encrypting(encrypting)
23{
24 EVP_CIPHER_CTX_init(_ctx);
25 int status = EVP_CipherInit_ex(_ctx, EVP_aes_128_gcm(), nullptr, nullptr, nullptr, _encrypting ? 1 : 0);
26 ASSERT(status);
27}
28
30{
31 EVP_CIPHER_CTX_free(_ctx);
32}
33
35{
36 int status = EVP_CipherInit_ex(_ctx, nullptr, nullptr, key.data(), nullptr, -1);
37 ASSERT(status);
38}
39
40bool Trinity::Crypto::AES::Process(IV const& iv, uint8* data, size_t length, Tag& tag)
41{
42 ASSERT(length <= static_cast<size_t>(std::numeric_limits<int>::max()));
43 int len = static_cast<int>(length);
44 if (!EVP_CipherInit_ex(_ctx, nullptr, nullptr, nullptr, iv.data(), -1))
45 return false;
46
47 int outLen;
48 if (!EVP_CipherUpdate(_ctx, data, &outLen, data, len))
49 return false;
50
51 len -= outLen;
52
53 if (!_encrypting && !EVP_CIPHER_CTX_ctrl(_ctx, EVP_CTRL_GCM_SET_TAG, sizeof(tag), tag))
54 return false;
55
56 if (!EVP_CipherFinal_ex(_ctx, data + outLen, &outLen))
57 return false;
58
59 ASSERT(len == outLen);
60
61 if (_encrypting && !EVP_CIPHER_CTX_ctrl(_ctx, EVP_CTRL_GCM_GET_TAG, sizeof(tag), tag))
62 return false;
63
64 return true;
65}
66
67bool Trinity::Crypto::AES::ProcessNoIntegrityCheck(IV const& iv, uint8* data, size_t partialLength)
68{
69 ASSERT(!_encrypting, "Partial encryption is not allowed");
70 ASSERT(partialLength <= static_cast<size_t>(std::numeric_limits<int>::max()));
71 int len = static_cast<int>(partialLength);
72 if (!EVP_CipherInit_ex(_ctx, nullptr, nullptr, nullptr, iv.data(), -1))
73 return false;
74
75 int outLen;
76 if (!EVP_CipherUpdate(_ctx, data, &outLen, data, len))
77 return false;
78
79 len -= outLen;
80
81 if (!EVP_CipherFinal_ex(_ctx, data + outLen, &outLen))
82 return false;
83
84 ASSERT(len == outLen);
85
86 return true;
87}
uint8_t uint8
Definition: Define.h:144
#define ASSERT
Definition: Errors.h:68
uint8[TAG_SIZE_BYTES] Tag
Definition: AES.h:36
bool _encrypting
Definition: AES.h:48
std::array< uint8, IV_SIZE_BYTES > IV
Definition: AES.h:34
bool ProcessNoIntegrityCheck(IV const &iv, uint8 *data, size_t partialLength)
Definition: AES.cpp:67
AES(bool encrypting)
Definition: AES.cpp:22
std::array< uint8, KEY_SIZE_BYTES > Key
Definition: AES.h:35
EVP_CIPHER_CTX * _ctx
Definition: AES.h:47
bool Process(IV const &iv, uint8 *data, size_t length, Tag &tag)
Definition: AES.cpp:40
void Init(Key const &key)
Definition: AES.cpp:34