Legends-of-Azeroth-Pandaria-5.4.8
65 строк · 2.0 Кб
1/*
2* This file is part of the Pandaria 5.4.8 Project. See THANKS 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{
24EVP_CIPHER_CTX_init(_ctx);25int status = EVP_CipherInit_ex(_ctx, EVP_aes_128_gcm(), nullptr, nullptr, nullptr, _encrypting ? 1 : 0);26ASSERT(status);27}
28
29Trinity::Crypto::AES::~AES()30{
31EVP_CIPHER_CTX_free(_ctx);32}
33
34void Trinity::Crypto::AES::Init(Key const& key)35{
36int status = EVP_CipherInit_ex(_ctx, nullptr, nullptr, key.data(), nullptr, -1);37ASSERT(status);38}
39
40bool Trinity::Crypto::AES::Process(IV const& iv, uint8* data, size_t length, Tag& tag)41{
42ASSERT(length <= static_cast<size_t>(std::numeric_limits<int>::max()));43int len = static_cast<int>(length);44if (!EVP_CipherInit_ex(_ctx, nullptr, nullptr, nullptr, iv.data(), -1))45return false;46
47int outLen;48if (!EVP_CipherUpdate(_ctx, data, &outLen, data, len))49return false;50
51len -= outLen;52
53if (!_encrypting && !EVP_CIPHER_CTX_ctrl(_ctx, EVP_CTRL_GCM_SET_TAG, sizeof(tag), tag))54return false;55
56if (!EVP_CipherFinal_ex(_ctx, data + outLen, &outLen))57return false;58
59ASSERT(len == outLen);60
61if (_encrypting && !EVP_CIPHER_CTX_ctrl(_ctx, EVP_CTRL_GCM_GET_TAG, sizeof(tag), tag))62return false;63
64return true;65}
66