a little hack to smbclient to support extracting NT error codes
[Samba.git] / source / arcfour.c
bloba28d702a8612dbf61a9a867b72874681473eacf7
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
5 a implementation of arcfour designed for use in the
6 SMB password change protocol based on the description
7 in 'Applied Cryptography', 2nd Edition.
9 Copyright (C) Jeremy Allison 1997
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "arcfour.h"
28 void set_arc4_key(unsigned char *data, int key_length, arc4_key *arckey)
30 unsigned int i;
31 unsigned char j;
32 unsigned char tc;
33 unsigned char *s_box = &arckey->s_box[0];
35 arckey->index_i = 0;
36 arckey->index_j = 0;
37 for(i = 0; i < 256; i++)
38 s_box[i] = (unsigned char)i;
40 j = 0;
41 for( i = 0; i < 256; i++)
43 j += (s_box[i] + data[i%key_length]);
44 tc = s_box[i];
45 s_box[i] = s_box[j];
46 s_box[j] = tc;
50 void arc4(arc4_key *arckey, unsigned char *data_in, unsigned char *data_out,
51 int length)
53 unsigned char tc;
54 int ind;
55 unsigned char i, j;
56 unsigned char t;
57 unsigned char *s_box = &arckey->s_box[0];
59 for( ind = 0; ind < length; ind++)
61 i = ++arckey->index_i;
62 j = arckey->index_j += s_box[i];
63 tc = s_box[i];
64 s_box[i] = s_box[j];
65 s_box[j] = tc;
66 t = s_box[i] + s_box[j];
67 *data_out++ = *data_in++ ^ s_box[t];
71 #if 0
72 /* Test vector */
73 unsigned char key_data[] = { 0x61, 0x8a, 0x63, 0xd2, 0xfb };
74 unsigned char plaintext[] = { 0xdc, 0xee, 0x4c, 0xf9, 0x2c };
75 unsigned char ciphertext[] = { 0xf1, 0x38, 0x29, 0xc9, 0xde };
77 int main(int argc, char *argv[])
79 unsigned char out[5];
80 arc4_key key;
82 set_arc4_key(key_data, 5, &key);
83 arc4(&key, plaintext, out, 5);
85 if(memcmp(out, ciphertext, 5) ==0)
86 printf("Test ok !\n");
87 else
88 printf("Test fail !\n");
89 return 0;
91 #endif