pyldb: avoid segfault when adding an element with no name
[kai/samba-autobuild/.git] / lib / crypto / arcfour.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    An implementation of the arcfour algorithm
5
6    Copyright (C) Andrew Tridgell 1998
7
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.
12
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.
17
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/>.
20 */
21
22 #include "replace.h"
23 #include "../lib/crypto/arcfour.h"
24
25 /* initialise the arcfour sbox with key */
26 _PUBLIC_ void arcfour_init(struct arcfour_state *state, const DATA_BLOB *key)
27 {
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;
32         }
33
34         for (ind = 0; ind < sizeof(state->sbox); ind++) {
35                 uint8_t tc;
36
37                 j += (state->sbox[ind] + key->data[ind%key->length]);
38
39                 tc = state->sbox[ind];
40                 state->sbox[ind] = state->sbox[j];
41                 state->sbox[j] = tc;
42         }
43         state->index_i = 0;
44         state->index_j = 0;
45 }
46
47 /* crypt the data with arcfour */
48 _PUBLIC_ void arcfour_crypt_sbox(struct arcfour_state *state, uint8_t *data,
49                                  int len)
50 {
51         int ind;
52
53         for (ind = 0; ind < len; ind++) {
54                 uint8_t tc;
55                 uint8_t t;
56
57                 state->index_i++;
58                 state->index_j += state->sbox[state->index_i];
59
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;
63
64                 t = state->sbox[state->index_i] + state->sbox[state->index_j];
65                 data[ind] = data[ind] ^ state->sbox[t];
66         }
67 }
68
69 /*
70   arcfour encryption with a blob key
71 */
72 _PUBLIC_ void arcfour_crypt_blob(uint8_t *data, int len, const DATA_BLOB *key)
73 {
74         struct arcfour_state state;
75         arcfour_init(&state, key);
76         arcfour_crypt_sbox(&state, data, len);
77 }
78
79 /*
80   a variant that assumes a 16 byte key. This should be removed
81   when the last user is gone
82 */
83 _PUBLIC_ void arcfour_crypt(uint8_t *data, const uint8_t keystr[16], int len)
84 {
85         uint8_t keycopy[16];
86         DATA_BLOB key = { .data = keycopy, .length = sizeof(keycopy) };
87
88         memcpy(keycopy, keystr, sizeof(keycopy));
89
90         arcfour_crypt_blob(data, len, &key);
91 }
92
93