1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
4 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | */ |
---|
20 | |
---|
21 | #include "AuthCrypt.h" |
---|
22 | #include "Hmac.h" |
---|
23 | |
---|
24 | AuthCrypt::AuthCrypt() |
---|
25 | { |
---|
26 | _initialized = false; |
---|
27 | } |
---|
28 | |
---|
29 | void AuthCrypt::Init() |
---|
30 | { |
---|
31 | _send_i = _send_j = _recv_i = _recv_j = 0; |
---|
32 | _initialized = true; |
---|
33 | } |
---|
34 | |
---|
35 | void AuthCrypt::DecryptRecv(uint8 *data, size_t len) |
---|
36 | { |
---|
37 | if (!_initialized) return; |
---|
38 | if (len < CRYPTED_RECV_LEN) return; |
---|
39 | |
---|
40 | for (size_t t = 0; t < CRYPTED_RECV_LEN; t++) |
---|
41 | { |
---|
42 | _recv_i %= _key.size(); |
---|
43 | uint8 x = (data[t] - _recv_j) ^ _key[_recv_i]; |
---|
44 | ++_recv_i; |
---|
45 | _recv_j = data[t]; |
---|
46 | data[t] = x; |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | void AuthCrypt::EncryptSend(uint8 *data, size_t len) |
---|
51 | { |
---|
52 | if (!_initialized) return; |
---|
53 | if (len < CRYPTED_SEND_LEN) return; |
---|
54 | |
---|
55 | for (size_t t = 0; t < CRYPTED_SEND_LEN; t++) |
---|
56 | { |
---|
57 | _send_i %= _key.size(); |
---|
58 | uint8 x = (data[t] ^ _key[_send_i]) + _send_j; |
---|
59 | ++_send_i; |
---|
60 | data[t] = _send_j = x; |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | void AuthCrypt::SetKey(BigNumber *bn) |
---|
65 | { |
---|
66 | uint8 *key = new uint8[SHA_DIGEST_LENGTH]; |
---|
67 | GenerateKey(key, bn); |
---|
68 | _key.resize(SHA_DIGEST_LENGTH); |
---|
69 | std::copy(key, key + SHA_DIGEST_LENGTH, _key.begin()); |
---|
70 | delete key; |
---|
71 | } |
---|
72 | |
---|
73 | AuthCrypt::~AuthCrypt() |
---|
74 | { |
---|
75 | } |
---|
76 | |
---|
77 | void AuthCrypt::GenerateKey(uint8 *key, BigNumber *bn) |
---|
78 | { |
---|
79 | HmacHash hash; |
---|
80 | hash.UpdateBigNumber(bn); |
---|
81 | hash.Finalize(); |
---|
82 | memcpy(key, hash.GetDigest(), SHA_DIGEST_LENGTH); |
---|
83 | } |
---|