/* * Copyright (C) 2013-2019 Cisco Systems, Inc. and/or its affiliates. All rights reserved. * Copyright (C) 2011-2013 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 "clamav-types.h" #include "arc4.h" #include 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 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]; size_t i; struct timeval tv0, tv1; for (i=0;i