Search for location of waf script
[Samba.git] / lib / crypto / arcfour.c
blobaf9b20cc01e224fc79f7282c4658d2d7c39d25ed
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 "replace.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 size_t 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,
49 int len)
51 int ind;
53 for (ind = 0; ind < len; ind++) {
54 uint8_t tc;
55 uint8_t t;
57 state->index_i++;
58 state->index_j += state->sbox[state->index_i];
60 tc = state->sbox[state->index_i];
61 state->sbox[state->index_i] = state->sbox[state->index_j];
62 state->sbox[state->index_j] = tc;
64 t = state->sbox[state->index_i] + state->sbox[state->index_j];
65 data[ind] = data[ind] ^ state->sbox[t];
70 arcfour encryption with a blob key
72 _PUBLIC_ void arcfour_crypt_blob(uint8_t *data, int len, const DATA_BLOB *key)
74 struct arcfour_state state;
75 arcfour_init(&state, key);
76 arcfour_crypt_sbox(&state, data, len);
80 a variant that assumes a 16 byte key. This should be removed
81 when the last user is gone
83 _PUBLIC_ void arcfour_crypt(uint8_t *data, const uint8_t keystr[16], int len)
85 uint8_t keycopy[16];
86 DATA_BLOB key = { .data = keycopy, .length = sizeof(keycopy) };
88 memcpy(keycopy, keystr, sizeof(keycopy));
90 arcfour_crypt_blob(data, len, &key);