| 1 | /* |
| 2 | |
| 3 | GS peerchat encryption/decryption algorithm 0.2 |
| 4 | by Luigi Auriemma |
| 5 | e-mail: [email protected] |
| 6 | web: aluigi.org |
| 7 | |
| 8 | |
| 9 | LICENSE |
| 10 | ======= |
| 11 | Copyright 2004,2005,2006 Luigi Auriemma |
| 12 | |
| 13 | This program is free software; you can redistribute it and/or modify |
| 14 | it under the terms of the GNU General Public License as published by |
| 15 | the Free Software Foundation; either version 2 of the License, or |
| 16 | (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, |
| 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | GNU General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 26 | |
| 27 | http://www.gnu.org/licenses/gpl.txt |
| 28 | |
| 29 | */ |
| 30 | |
| 31 | #include "common/setup_before.h" |
| 32 | #include "common/peerchat.h" |
| 33 | |
| 34 | #include "common/eventlog.h" |
| 35 | #include "common/xalloc.h" |
| 36 | |
| 37 | #include "common/setup_after.h" |
| 38 | |
| 39 | namespace pvpgn |
| 40 | { |
| 41 | |
| 42 | extern gs_peerchat_ctx * gs_peerchat_create() |
| 43 | { |
| 44 | gs_peerchat_ctx * temp; |
| 45 | |
| 46 | temp = (gs_peerchat_ctx*)xmalloc(sizeof(gs_peerchat_ctx)); |
| 47 | |
| 48 | return temp; |
| 49 | } |
| 50 | |
| 51 | extern void gs_peerchat_destroy(gs_peerchat_ctx const * ctx) |
| 52 | { |
| 53 | if (!ctx) { |
| 54 | ERROR0("got NULL ctx"); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | xfree((void *)ctx); /* avoid warning */ |
| 59 | } |
| 60 | |
| 61 | void gs_peerchat_init(gs_peerchat_ctx *ctx, unsigned char *chall, unsigned char *gamekey) { |
| 62 | unsigned char challenge[16], |
| 63 | *l, |
| 64 | *l1, |
| 65 | *p, |
| 66 | *p1, |
| 67 | *crypt, |
| 68 | t, |
| 69 | t1; |
| 70 | |
| 71 | ctx->gs_peerchat_1 = 0; |
| 72 | ctx->gs_peerchat_2 = 0; |
| 73 | crypt = ctx->gs_peerchat_crypt; |
| 74 | |
| 75 | p = challenge; |
| 76 | l = challenge + 16; |
| 77 | p1 = gamekey; |
| 78 | l1 = gamekey + 6; |
| 79 | do { |
| 80 | *p++ = *chall++ ^ *p1++; // avoid a memcpy(challenge, chall, 16); |
| 81 | if (p1 == l1) p1 = gamekey; |
| 82 | } while (p != l); |
| 83 | |
| 84 | t1 = 255; |
| 85 | p1 = crypt; |
| 86 | l1 = crypt + 256; |
| 87 | do { *p1++ = t1--; } while (p1 != l1); |
| 88 | |
| 89 | t1++; // means t1 = 0; |
| 90 | p = crypt; |
| 91 | p1 = challenge; |
| 92 | do { |
| 93 | t1 += *p1 + *p; |
| 94 | t = crypt[t1]; |
| 95 | crypt[t1] = *p; |
| 96 | *p = t; |
| 97 | p++; |
| 98 | p1++; |
| 99 | if (p1 == l) p1 = challenge; |
| 100 | } while (p != l1); |
| 101 | |
| 102 | // DEBUG3("initialised: %u/%u/%s",ctx->gs_peerchat_1,ctx->gs_peerchat_2,ctx->gs_peerchat_crypt); |
| 103 | } |
| 104 | |
| 105 | void gs_peerchat(gs_peerchat_ctx *ctx, unsigned char *data, int size) { |
| 106 | unsigned char num1, |
| 107 | num2, |
| 108 | t, |
| 109 | *crypt; |
| 110 | |
| 111 | num1 = ctx->gs_peerchat_1; |
| 112 | num2 = ctx->gs_peerchat_2; |
| 113 | crypt = ctx->gs_peerchat_crypt; |
| 114 | |
| 115 | while (size--) { |
| 116 | t = crypt[++num1]; |
| 117 | num2 += t; |
| 118 | crypt[num1] = crypt[num2]; |
| 119 | crypt[num2] = t; |
| 120 | t += crypt[num1]; |
| 121 | *data++ ^= crypt[t]; |
| 122 | } |
| 123 | |
| 124 | ctx->gs_peerchat_1 = num1; |
| 125 | ctx->gs_peerchat_2 = num2; |
| 126 | } |
| 127 | |
| 128 | } |