File: arc4.c

package info (click to toggle)
clamav 0.98.7+dfsg-0+deb6u2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 60,204 kB
  • ctags: 49,129
  • sloc: cpp: 267,090; ansic: 152,211; sh: 35,196; python: 2,630; makefile: 2,220; perl: 1,690; pascal: 1,218; lisp: 184; csh: 117; xml: 38; asm: 32; exp: 4
file content (104 lines) | stat: -rw-r--r-- 2,578 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 *  Copyright (C) 2011 Sourcefire, Inc.
 *
 *  Author: Török Edvin
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *  MA 02110-1301, USA.
 */

#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif

#include "cltypes.h"
#include "arc4.h"
#include <string.h>

void arc4_init(struct arc4_state *a, const uint8_t *key, unsigned keylength)
{
    unsigned i;
    uint8_t j;
    uint32_t *S = &a->S[0];

    for (i=0; i < 256;i ++)
	S[i] = i;
    for (i=0,j=0; i < 256; i++) {
	uint8_t tmp = S[i];
	j = j + S[i] + key[i % keylength];
	S[i] = S[j];
	S[j] = tmp;
    }
    a->i = a->j = 0;
}

void arc4_apply(struct arc4_state *s, uint8_t *data, unsigned len)
{
    uint8_t i = s->i, j = s->j;
    uint32_t *S = &s->S[0];

    while (len-- > 0) {
	uint32_t a, b;
	i++;
	a = S[i];
	j += a;
	b = S[i] = S[j];
	b += a;
	S[j] = a;
	*data++ ^= S[b & 0xff];
    }

    s->i = i;
    s->j = j;
}

#if 0
#include <sys/time.h>
static struct {
    const char *key;
    const char *plaintext;
    const char *result;
} testdata[] = {
    {"Key", "Plaintext", "\xBB\xF3\x16\xE8\xD9\x40\xAF\x0A\xD3"},
    {"Wiki", "pedia", "\x10\x21\xBF\x04\x20"},
    {"Secret", "Attack at dawn", "\x45\xA0\x1F\x64\x5F\xC3\x5B\x38\x35\x52\x54\x4B\x9B\xF5"}
};

static int data[10*1024*1024];
int main(void)
{
    struct arc4_state a;
    uint8_t data[32];
    unsigned i;
    struct timeval tv0, tv1;

    for (i=0;i<sizeof(testdata)/sizeof(testdata[0]);i++) {
	unsigned len;
	arc4_init(&a, (const uint8_t*)testdata[i].key, strlen(testdata[i].key));
	len = strlen(testdata[i].plaintext);
	memcpy(data, testdata[i].plaintext, len);
	arc4_apply(&a, data, len);
	if (memcmp(data, testdata[i].result, len)) {
	    printf("Bad result at %d\n", i);
	}
    }
    gettimeofday(&tv0, NULL);
    for (i=0;i<1000000;i++)
	arc4_apply(&a, data, sizeof(data));
    gettimeofday(&tv1, NULL);
    tv1.tv_sec -= tv0.tv_sec;
    tv1.tv_usec -= tv0.tv_usec;
    printf("Time: %f us\n", tv1.tv_sec*1000000.0 + tv1.tv_usec);
}
#endif