Legends-of-Azeroth-Pandaria-5.4.8

Форк
0
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

22
Trinity::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

29
Trinity::Crypto::AES::~AES()
30
{
31
    EVP_CIPHER_CTX_free(_ctx);
32
}
33

34
void Trinity::Crypto::AES::Init(Key const& key)
35
{
36
    int status = EVP_CipherInit_ex(_ctx, nullptr, nullptr, key.data(), nullptr, -1);
37
    ASSERT(status);
38
}
39

40
bool 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

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.