summaryrefslogtreecommitdiffstats
path: root/src/lib/crypto/enc_provider/arcfour.c
blob: 43c71f17957edad3ac28efb9373e97abdcb7cc09 (plain)
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
193
194
195
196
197
198
199
200
201
202
203
204
/* arcfour.c 
 *
 * Copyright (c) 2000 by Computer Science Laboratory,
 *                       Rensselaer Polytechnic Institute
 *
 * #include STD_DISCLAIMER
 */

#include "k5-int.h"
#include "arcfour-int.h"
#include "enc_provider.h"
/* 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);

/* Interface layer to kerb5 crypto layer */
static krb5_error_code
k5_arcfour_docrypt(krb5_const krb5_keyblock *, krb5_const krb5_data *,
		   krb5_const krb5_data *, krb5_data *);


/* The blocksize for the enctype */
static void k5_arcfour_blocksize(size_t *);

/* keysize for the enctype (number of bytes, and length of key (parity/etc) */
static void k5_arcfour_keysize(size_t *, size_t *);

/* from a random bitstrem, construct a key */
static krb5_error_code
k5_arcfour_make_key(krb5_const krb5_data *, krb5_keyblock *);

static unsigned char arcfour_weakkey1[] = {0x00, 0x00, 0xfd};
static unsigned char arcfour_weakkey2[] = {0x03, 0xfd, 0xfc};
static krb5_data arcfour_weakkeys[] = { {KV5M_DATA, sizeof (arcfour_weakkey1),
					 (char * ) arcfour_weakkey1},
					{KV5M_DATA, sizeof (arcfour_weakkey2),
					 (char * ) arcfour_weakkey2},
					{KV5M_DATA, 0, 0}
};

/*xxx we really should check for c9x here and use inline on 
 * more than just gcc. */
#if ((__GNUC__ >= 2) )
static __inline__ unsigned int k5_arcfour_byte(ArcfourContext * ctx)
#else
static unsigned int k5_arcfour_byte(ArcfourContext * ctx)
#endif /* gcc inlines*/
{
  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 */
  for(counter=0;arcfour_weakkeys[counter].length >0; counter++)
    if (memcmp(key, arcfour_weakkeys[counter].data,
	       arcfour_weakkeys[counter].length) == 0)
      return KRB5DES_WEAK_KEY; /* most certainly not the correct error */

  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;
}

/* This seems to work... although I am not sure what the implications are
   in other places in the kerberos library */
static void
k5_arcfour_blocksize(size_t *blocksize)
{
  *blocksize = 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.
*/
static void
k5_arcfour_keysize(size_t *keybytes, size_t *keylength)
{
    *keybytes = 16;
    *keylength = 16;
}

/* The workhorse of the arcfour system, this impliments the cipher */
static krb5_error_code
k5_arcfour_docrypt(krb5_const krb5_keyblock *key, krb5_const krb5_data *state,
	       krb5_const krb5_data *input, krb5_data *output)
{
  ArcfourContext *arcfour_ctx;
  int ret;

  if (key->length != 16)
    return(KRB5_BAD_KEYSIZE);
  if (state && (state->length != sizeof (ArcfourContext)))
    return(KRB5_BAD_MSIZE);
  if (input->length != output->length)
    return(KRB5_BAD_MSIZE);

  if (state) {
    arcfour_ctx=(ArcfourContext *)state->data;
    k5_arcfour_crypt(arcfour_ctx, (unsigned char *) output->data, (const unsigned char *) input->data, input->length);
  }
  else {
    arcfour_ctx=malloc(sizeof (ArcfourContext));
    if (arcfour_ctx == NULL)
      return ENOMEM;
    if ((ret=k5_arcfour_init(arcfour_ctx, key->contents, key->length))) {
      free(arcfour_ctx);
      return (ret);
    }
    k5_arcfour_crypt(arcfour_ctx, (unsigned char * ) output->data,
		     (const unsigned char * ) input->data, input->length);
    memset(arcfour_ctx, 0, sizeof (ArcfourContext));
    free(arcfour_ctx);
  }
  
  return 0;
}

static krb5_error_code
k5_arcfour_make_key(krb5_const krb5_data *randombits, krb5_keyblock *key)
{
    if (key->length != 16)
	return(KRB5_BAD_KEYSIZE);
    if (randombits->length != 16)
	return(KRB5_CRYPTO_INTERNAL);

    key->magic = KV5M_KEYBLOCK;
    key->length = 16;

    memcpy(key->contents, randombits->data, randombits->length);

    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 = {
    k5_arcfour_blocksize,
    k5_arcfour_keysize,
    k5_arcfour_docrypt,
    k5_arcfour_docrypt,
    k5_arcfour_make_key
};