arcfour.c 2.18 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
/* arcfour.c
Niels Möller's avatar
Niels Möller committed
2
 *
Niels Möller's avatar
Niels Möller committed
3
 * The arcfour/rc4 stream cipher.
Niels Möller's avatar
Niels Möller committed
4 5
 */

Niels Möller's avatar
Niels Möller committed
6
/* nettle, low-level cryptographics library
Niels Möller's avatar
Niels Möller committed
7
 *
Niels Möller's avatar
Niels Möller committed
8 9 10 11 12 13 14
 * Copyright (C) 2001 Niels Mller
 *  
 * The nettle library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 * 
Niels Möller's avatar
Niels Möller committed
15
 * The nettle library is distributed in the hope that it will be useful, but
Niels Möller's avatar
Niels Möller committed
16 17 18 19 20
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
Niels Möller's avatar
Niels Möller committed
21
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
Niels Möller's avatar
Niels Möller committed
22 23
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
Niels Möller's avatar
Niels Möller committed
24 25
 */

Niels Möller's avatar
src/  
Niels Möller committed
26
#include "arcfour.h"
Niels Möller's avatar
Niels Möller committed
27

28 29
#include <assert.h>

Niels Möller's avatar
Niels Möller committed
30 31
#define SWAP(a,b) do { int _t = a; a = b; b = _t; } while(0)

Niels Möller's avatar
Niels Möller committed
32 33 34
void
arcfour_set_key(struct arcfour_ctx *ctx,
		unsigned length, const uint8_t *key)
35
{
Niels Möller's avatar
Niels Möller committed
36 37 38 39
  unsigned i, j, k;
  
  assert(length >= ARCFOUR_MIN_KEY_SIZE);
  assert(length <= ARCFOUR_MAX_KEY_SIZE);
40 41 42 43 44

  /* Initialize context */
  for (i = 0; i<256; i++)
    ctx->S[i] = i;

Niels Möller's avatar
Niels Möller committed
45
  for (i = j = k = 0; i<256; i++)
46 47 48
    {
      j += ctx->S[i] + key[k]; j &= 0xff;
      SWAP(ctx->S[i], ctx->S[j]);
Niels Möller's avatar
Niels Möller committed
49 50
      /* Repeat key as needed */
      k = (k + 1) % length;
51
    }
Niels Möller's avatar
Niels Möller committed
52
  ctx->i = ctx->j = 0;
53 54
}

Niels Möller's avatar
Niels Möller committed
55 56 57 58 59

void
arcfour_crypt(struct arcfour_ctx *ctx,
	      unsigned length, uint8_t *dst,
	      const uint8_t *src)
60
{
Niels Möller's avatar
Niels Möller committed
61
  register uint8_t i, j;
62

Niels Möller's avatar
Niels Möller committed
63 64
  i = ctx->i; j = ctx->j;
  while(length--)
65 66 67 68
    {
      i++; i &= 0xff;
      j += ctx->S[i]; j &= 0xff;
      SWAP(ctx->S[i], ctx->S[j]);
Niels Möller's avatar
Niels Möller committed
69
      *dst++ = *src++ ^ ctx->S[ (ctx->S[i] + ctx->S[j]) & 0xff ];
70 71 72 73
    }
  ctx->i = i; ctx->j = j;
}

Niels Möller's avatar
Niels Möller committed
74 75 76
void
arcfour_stream(struct arcfour_ctx *ctx,
	       unsigned length, uint8_t *dst)
Niels Möller's avatar
Niels Möller committed
77
{
Niels Möller's avatar
Niels Möller committed
78
  register uint8_t i, j;
Niels Möller's avatar
Niels Möller committed
79 80

  i = ctx->i; j = ctx->j;
81
  while(length--)
Niels Möller's avatar
Niels Möller committed
82
    {
83 84
      i++; i &= 0xff;
      j += ctx->S[i]; j &= 0xff;
Niels Möller's avatar
Niels Möller committed
85
      SWAP(ctx->S[i], ctx->S[j]);
Niels Möller's avatar
Niels Möller committed
86
      *dst++ = ctx->S[ (ctx->S[i] + ctx->S[j]) & 0xff ];
Niels Möller's avatar
Niels Möller committed
87 88 89
    }
  ctx->i = i; ctx->j = j;
}
Niels Möller's avatar
Niels Möller committed
90