ECLIPSE + PyDev Project
[Samba/kamenim.git] / lib / crypto / arcfour.c
blobc57e05d0e9175c0d4615fea2952b50d25f89e23e
1 /*
2 Unix SMB/CIFS implementation.
4 An implementation of the arcfour algorithm
6 Copyright (C) Andrew Tridgell 1998
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "../lib/crypto/arcfour.h"
25 /* initialise the arcfour sbox with key */
26 _PUBLIC_ void arcfour_init(struct arcfour_state *state, const DATA_BLOB *key)
28 int ind;
29 uint8_t j = 0;
30 for (ind = 0; ind < sizeof(state->sbox); ind++) {
31 state->sbox[ind] = (uint8_t)ind;
34 for (ind = 0; ind < sizeof(state->sbox); ind++) {
35 uint8_t tc;
37 j += (state->sbox[ind] + key->data[ind%key->length]);
39 tc = state->sbox[ind];
40 state->sbox[ind] = state->sbox[j];
41 state->sbox[j] = tc;
43 state->index_i = 0;
44 state->index_j = 0;
47 /* crypt the data with arcfour */
48 _PUBLIC_ void arcfour_crypt_sbox(struct arcfour_state *state, uint8_t *data, int len)
50 int ind;
52 for (ind = 0; ind < len; ind++) {
53 uint8_t tc;
54 uint8_t t;
56 state->index_i++;
57 state->index_j += state->sbox[state->index_i];
59 tc = state->sbox[state->index_i];
60 state->sbox[state->index_i] = state->sbox[state->index_j];
61 state->sbox[state->index_j] = tc;
63 t = state->sbox[state->index_i] + state->sbox[state->index_j];
64 data[ind] = data[ind] ^ state->sbox[t];
69 arcfour encryption with a blob key
71 _PUBLIC_ void arcfour_crypt_blob(uint8_t *data, int len, const DATA_BLOB *key)
73 struct arcfour_state state;
74 arcfour_init(&state, key);
75 arcfour_crypt_sbox(&state, data, len);
79 a variant that assumes a 16 byte key. This should be removed
80 when the last user is gone
82 _PUBLIC_ void arcfour_crypt(uint8_t *data, const uint8_t keystr[16], int len)
84 DATA_BLOB key = data_blob(keystr, 16);
86 arcfour_crypt_blob(data, len, &key);
88 data_blob_free(&key);