1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,104 @@ |
0 |
+/* |
|
1 |
+ * Copyright (C) 2011 Sourcefire, Inc. |
|
2 |
+ * |
|
3 |
+ * Author: Török Edvin |
|
4 |
+ * |
|
5 |
+ * This program is free software; you can redistribute it and/or modify |
|
6 |
+ * it under the terms of the GNU General Public License version 2 as |
|
7 |
+ * published by the Free Software Foundation. |
|
8 |
+ * |
|
9 |
+ * This program 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 |
|
12 |
+ * GNU General Public License for more details. |
|
13 |
+ * |
|
14 |
+ * You should have received a copy of the GNU General Public License |
|
15 |
+ * along with this program; if not, write to the Free Software |
|
16 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
17 |
+ * MA 02110-1301, USA. |
|
18 |
+ */ |
|
19 |
+ |
|
20 |
+#if HAVE_CONFIG_H |
|
21 |
+#include "clamav-config.h" |
|
22 |
+#endif |
|
23 |
+ |
|
24 |
+#include "cltypes.h" |
|
25 |
+#include "arc4.h" |
|
26 |
+#include <string.h> |
|
27 |
+ |
|
28 |
+void arc4_init(struct arc4_state *a, const uint8_t *key, unsigned keylength) |
|
29 |
+{ |
|
30 |
+ unsigned i; |
|
31 |
+ uint8_t j; |
|
32 |
+ uint32_t *S = &a->S[0]; |
|
33 |
+ |
|
34 |
+ for (i=0; i < 256;i ++) |
|
35 |
+ S[i] = i; |
|
36 |
+ for (i=0,j=0; i < 256; i++) { |
|
37 |
+ uint8_t tmp = S[i]; |
|
38 |
+ j = j + S[i] + key[i % keylength]; |
|
39 |
+ S[i] = S[j]; |
|
40 |
+ S[j] = tmp; |
|
41 |
+ } |
|
42 |
+ a->i = a->j = 0; |
|
43 |
+} |
|
44 |
+ |
|
45 |
+void arc4_apply(struct arc4_state *s, uint8_t *data, unsigned len) |
|
46 |
+{ |
|
47 |
+ uint8_t i = s->i, j = s->j; |
|
48 |
+ uint32_t *S = &s->S[0]; |
|
49 |
+ |
|
50 |
+ while (len-- > 0) { |
|
51 |
+ uint32_t a, b; |
|
52 |
+ i++; |
|
53 |
+ a = S[i]; |
|
54 |
+ j += a; |
|
55 |
+ b = S[i] = S[j]; |
|
56 |
+ b += a; |
|
57 |
+ S[j] = a; |
|
58 |
+ *data++ ^= S[b & 0xff]; |
|
59 |
+ } |
|
60 |
+ |
|
61 |
+ s->i = i; |
|
62 |
+ s->j = j; |
|
63 |
+} |
|
64 |
+ |
|
65 |
+#if 0 |
|
66 |
+#include <sys/time.h> |
|
67 |
+static struct { |
|
68 |
+ const char *key; |
|
69 |
+ const char *plaintext; |
|
70 |
+ const char *result; |
|
71 |
+} testdata[] = { |
|
72 |
+ {"Key", "Plaintext", "\xBB\xF3\x16\xE8\xD9\x40\xAF\x0A\xD3"}, |
|
73 |
+ {"Wiki", "pedia", "\x10\x21\xBF\x04\x20"}, |
|
74 |
+ {"Secret", "Attack at dawn", "\x45\xA0\x1F\x64\x5F\xC3\x5B\x38\x35\x52\x54\x4B\x9B\xF5"} |
|
75 |
+}; |
|
76 |
+ |
|
77 |
+static int data[10*1024*1024]; |
|
78 |
+int main(void) |
|
79 |
+{ |
|
80 |
+ struct arc4_state a; |
|
81 |
+ uint8_t data[32]; |
|
82 |
+ unsigned i; |
|
83 |
+ struct timeval tv0, tv1; |
|
84 |
+ |
|
85 |
+ for (i=0;i<sizeof(testdata)/sizeof(testdata[0]);i++) { |
|
86 |
+ unsigned len; |
|
87 |
+ arc4_init(&a, (const uint8_t*)testdata[i].key, strlen(testdata[i].key)); |
|
88 |
+ len = strlen(testdata[i].plaintext); |
|
89 |
+ memcpy(data, testdata[i].plaintext, len); |
|
90 |
+ arc4_apply(&a, data, len); |
|
91 |
+ if (memcmp(data, testdata[i].result, len)) { |
|
92 |
+ printf("Bad result at %d\n", i); |
|
93 |
+ } |
|
94 |
+ } |
|
95 |
+ gettimeofday(&tv0, NULL); |
|
96 |
+ for (i=0;i<1000000;i++) |
|
97 |
+ arc4_apply(&a, data, sizeof(data)); |
|
98 |
+ gettimeofday(&tv1, NULL); |
|
99 |
+ tv1.tv_sec -= tv0.tv_sec; |
|
100 |
+ tv1.tv_usec -= tv0.tv_usec; |
|
101 |
+ printf("Time: %f us\n", tv1.tv_sec*1000000.0 + tv1.tv_usec); |
|
102 |
+} |
|
103 |
+#endif |
0 | 104 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,29 @@ |
0 |
+/* |
|
1 |
+ * Copyright (C) 2011 Sourcefire, Inc. |
|
2 |
+ * |
|
3 |
+ * Author: Török Edvin |
|
4 |
+ * |
|
5 |
+ * This program is free software; you can redistribute it and/or modify |
|
6 |
+ * it under the terms of the GNU General Public License version 2 as |
|
7 |
+ * published by the Free Software Foundation. |
|
8 |
+ * |
|
9 |
+ * This program 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 |
|
12 |
+ * GNU General Public License for more details. |
|
13 |
+ * |
|
14 |
+ * You should have received a copy of the GNU General Public License |
|
15 |
+ * along with this program; if not, write to the Free Software |
|
16 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
17 |
+ * MA 02110-1301, USA. |
|
18 |
+ */ |
|
19 |
+ |
|
20 |
+#include "cltypes.h" |
|
21 |
+struct arc4_state { |
|
22 |
+ /* really just 8 bit, but it is faster if reads are aligned */ |
|
23 |
+ uint32_t S[256]; |
|
24 |
+ uint8_t i, j; |
|
25 |
+}; |
|
26 |
+ |
|
27 |
+void arc4_init(struct arc4_state *a, const uint8_t *key, unsigned keylength); |
|
28 |
+void arc4_apply(struct arc4_state *s, uint8_t *data, unsigned len); |