1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* lib/crypto/builtin/enc_provider/rc4.c */
/*
* Copyright (c) 2000 by Computer Science Laboratory,
* Rensselaer Polytechnic Institute
*
* #include STD_DISCLAIMER
*/
#include "crypto_int.h"
typedef struct
{
unsigned int x;
unsigned int y;
unsigned char state[256];
} ArcfourContext;
typedef struct {
int initialized;
ArcfourContext ctx;
} ArcFourCipherState;
/* gets the next byte from the PRNG */
#if ((__GNUC__ >= 2) )
static __inline__ unsigned int k5_arcfour_byte(ArcfourContext *);
#else
static unsigned int k5_arcfour_byte(ArcfourContext *);
#endif /* gcc inlines*/
/* Initializes the context and sets the key. */
static krb5_error_code k5_arcfour_init(ArcfourContext *ctx, const unsigned char *key,
unsigned int keylen);
/* Encrypts/decrypts data. */
static void k5_arcfour_crypt(ArcfourContext *ctx, unsigned char *dest,
const unsigned char *src, unsigned int len);
static inline unsigned int k5_arcfour_byte(ArcfourContext * ctx)
{
unsigned int x;
unsigned int y;
unsigned int sx, sy;
unsigned char *state;
state = ctx->state;
x = (ctx->x + 1) & 0xff;
sx = state[x];
y = (sx + ctx->y) & 0xff;
sy = state[y];
ctx->x = x;
ctx->y = y;
state[y] = sx;
state[x] = sy;
return state[(sx + sy) & 0xff];
}
static void k5_arcfour_crypt(ArcfourContext *ctx, unsigned char *dest,
const unsigned char *src, unsigned int len)
{
unsigned int i;
for (i = 0; i < len; i++)
dest[i] = src[i] ^ k5_arcfour_byte(ctx);
}
static krb5_error_code
k5_arcfour_init(ArcfourContext *ctx, const unsigned char *key,
unsigned int key_len)
{
unsigned int t, u;
unsigned int keyindex;
unsigned int stateindex;
unsigned char* state;
unsigned int counter;
if (key_len != 16)
return KRB5_BAD_MSIZE; /*this is probably not the correct error code
to return */
state = &ctx->state[0];
ctx->x = 0;
ctx->y = 0;
for (counter = 0; counter < 256; counter++)
state[counter] = counter;
keyindex = 0;
stateindex = 0;
for (counter = 0; counter < 256; counter++)
{
t = state[counter];
stateindex = (stateindex + key[keyindex] + t) & 0xff;
u = state[stateindex];
state[stateindex] = t;
state[counter] = u;
if (++keyindex >= key_len)
keyindex = 0;
}
return 0;
}
static krb5_error_code
k5_arcfour_docrypt(krb5_key key, const krb5_data *state, krb5_crypto_iov *data,
size_t num_data)
{
ArcfourContext *arcfour_ctx = NULL;
ArcFourCipherState *cipher_state = NULL;
krb5_error_code ret;
size_t i;
if (key->keyblock.length != 16)
return KRB5_BAD_KEYSIZE;
if (state != NULL && (state->length != sizeof(ArcFourCipherState)))
return KRB5_BAD_MSIZE;
if (state != NULL) {
cipher_state = (ArcFourCipherState *)state->data;
arcfour_ctx = &cipher_state->ctx;
if (cipher_state->initialized == 0) {
ret = k5_arcfour_init(arcfour_ctx, key->keyblock.contents,
key->keyblock.length);
if (ret != 0)
return ret;
cipher_state->initialized = 1;
}
} else {
arcfour_ctx = (ArcfourContext *)malloc(sizeof(ArcfourContext));
if (arcfour_ctx == NULL)
return ENOMEM;
ret = k5_arcfour_init(arcfour_ctx, key->keyblock.contents,
key->keyblock.length);
if (ret != 0) {
free(arcfour_ctx);
return ret;
}
}
for (i = 0; i < num_data; i++) {
krb5_crypto_iov *iov = &data[i];
if (ENCRYPT_IOV(iov))
k5_arcfour_crypt(arcfour_ctx, (unsigned char *)iov->data.data,
(const unsigned char *)iov->data.data, iov->data.length);
}
if (state == NULL) {
memset(arcfour_ctx, 0, sizeof(ArcfourContext));
free(arcfour_ctx);
}
return 0;
}
static krb5_error_code
k5_arcfour_init_state (const krb5_keyblock *key,
krb5_keyusage keyusage, krb5_data *new_state)
{
/* Note that we can't actually set up the state here because the key
* will change between now and when encrypt is called
* because it is data dependent. Yeah, this has strange
* properties. --SDH
*/
new_state->length = sizeof (ArcFourCipherState);
new_state->data = malloc (new_state->length);
if (new_state->data) {
memset (new_state->data, 0 , new_state->length);
/* That will set initialized to zero*/
}else {
return (ENOMEM);
}
return 0;
}
/* Since the arcfour cipher is identical going forwards and backwards,
we just call "docrypt" directly
*/
const struct krb5_enc_provider krb5int_enc_arcfour = {
/* This seems to work... although I am not sure what the
implications are in other places in the kerberos library */
1,
/* Keysize is arbitrary in arcfour, but the constraints of the
system, and to attempt to work with the MSFT system forces us
to 16byte/128bit. Since there is no parity in the key, the
byte and length are the same. */
16, 16,
k5_arcfour_docrypt,
k5_arcfour_docrypt,
NULL,
k5_arcfour_init_state, /*xxx not implemented yet*/
krb5int_default_free_state
};
|