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/>.
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
)
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
++) {
37 j
+= (state
->sbox
[ind
] + key
->data
[ind
%key
->length
]);
39 tc
= state
->sbox
[ind
];
40 state
->sbox
[ind
] = state
->sbox
[j
];
47 /* crypt the data with arcfour */
48 _PUBLIC_
void arcfour_crypt_sbox(struct arcfour_state
*state
, uint8_t *data
,
53 for (ind
= 0; ind
< len
; ind
++) {
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
)
86 DATA_BLOB key
= { .data
= keycopy
, .length
= sizeof(keycopy
) };
88 memcpy(keycopy
, keystr
, sizeof(keycopy
));
90 arcfour_crypt_blob(data
, len
, &key
);