/src/mdbtools/src/libmdb/rc4.c
Line | Count | Source |
1 | | /* MDB Tools - A library for reading MS Access database files |
2 | | * Copyright (C) 2000 Brian Bruns |
3 | | * |
4 | | * This library is free software; you can redistribute it and/or |
5 | | * modify it under the terms of the GNU Library General Public |
6 | | * License as published by the Free Software Foundation; either |
7 | | * version 2 of the License, or (at your option) any later version. |
8 | | * |
9 | | * This library is distributed in the hope that it will be useful, |
10 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | | * Library General Public License for more details. |
13 | | * |
14 | | * You should have received a copy of the GNU Library General Public |
15 | | * License along with this library; if not, write to the Free Software |
16 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
17 | | */ |
18 | | |
19 | | #include "mdbprivate.h" |
20 | | |
21 | | typedef struct |
22 | | { |
23 | | unsigned char state[256]; |
24 | | unsigned char x; |
25 | | unsigned char y; |
26 | | } RC4_KEY; |
27 | | |
28 | 1.01M | #define swap_byte(x,y) t = *(x); *(x) = *(y); *(y) = t |
29 | | |
30 | | |
31 | | static void RC4_set_key(RC4_KEY *key, int key_data_len, unsigned char *key_data_ptr) |
32 | 2.34k | { |
33 | 2.34k | unsigned char t; |
34 | 2.34k | unsigned char index1; |
35 | 2.34k | unsigned char index2; |
36 | 2.34k | unsigned char* state; |
37 | 2.34k | short counter; |
38 | | |
39 | 2.34k | state = &key->state[0]; |
40 | 602k | for(counter = 0; counter < 256; counter++) |
41 | 600k | state[counter] = counter; |
42 | 2.34k | key->x = 0; |
43 | 2.34k | key->y = 0; |
44 | 2.34k | index1 = 0; |
45 | 2.34k | index2 = 0; |
46 | 602k | for(counter = 0; counter < 256; counter++) { |
47 | 600k | index2 = (key_data_ptr[index1] + state[counter] + index2) % 256; |
48 | 600k | swap_byte(&state[counter], &state[index2]); |
49 | 600k | index1 = (index1 + 1) % key_data_len; |
50 | 600k | } |
51 | 2.34k | } |
52 | | |
53 | | /* |
54 | | * this algorithm does 'encrypt in place' instead of inbuff/outbuff |
55 | | * note also: encryption and decryption use same routine |
56 | | * implementation supplied by (Adam Back) at <adam at cypherspace dot org> |
57 | | */ |
58 | | static void RC4(RC4_KEY *key, int buffer_len, unsigned char * buff) |
59 | 2.34k | { |
60 | 2.34k | unsigned char t; |
61 | 2.34k | unsigned char x; |
62 | 2.34k | unsigned char y; |
63 | 2.34k | unsigned char* state; |
64 | 2.34k | unsigned char xorIndex; |
65 | 2.34k | short counter; |
66 | | |
67 | 2.34k | x = key->x; |
68 | 2.34k | y = key->y; |
69 | 2.34k | state = &key->state[0]; |
70 | 412k | for(counter = 0; counter < buffer_len; counter++) { |
71 | 410k | x = (x + 1) % 256; |
72 | 410k | y = (state[x] + y) % 256; |
73 | 410k | swap_byte(&state[x], &state[y]); |
74 | 410k | xorIndex = (state[x] + state[y]) % 256; |
75 | 410k | buff[counter] ^= state[xorIndex]; |
76 | 410k | } |
77 | 2.34k | key->x = x; |
78 | 2.34k | key->y = y; |
79 | 2.34k | } |
80 | | |
81 | 2.34k | void mdbi_rc4(unsigned char *key, guint32 key_len, unsigned char *buf, guint32 buf_len) { |
82 | 2.34k | RC4_KEY rc4_key; |
83 | 2.34k | RC4_set_key(&rc4_key, key_len, key); |
84 | 2.34k | RC4(&rc4_key, buf_len, buf); |
85 | 2.34k | } |