The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/crypto/arc4/arc4.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: arc4.c,v 1.5 2003/08/26 20:12:22 thorpej Exp $ */
    2 
    3 /*
    4  * ARC4 implementation
    5  *      A Stream Cipher Encryption Algorithm "Arcfour"
    6  *      <draft-kaukonen-cipher-arcfour-03.txt>
    7  */
    8 
    9 /*        This code illustrates a sample implementation
   10  *                 of the Arcfour algorithm
   11  *         Copyright (c) April 29, 1997 Kalle Kaukonen.
   12  *                    All Rights Reserved.
   13  *
   14  * Redistribution and use in source and binary forms, with or
   15  * without modification, are permitted provided that this copyright
   16  * notice and disclaimer are retained.
   17  *
   18  * THIS SOFTWARE IS PROVIDED BY KALLE KAUKONEN AND CONTRIBUTORS ``AS
   19  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL KALLE
   22  * KAUKONEN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __KERNEL_RCSID(0, "$NetBSD: arc4.c,v 1.5 2003/08/26 20:12:22 thorpej Exp $");
   34 
   35 #include <sys/types.h>
   36 
   37 #include <crypto/arc4/arc4.h>
   38 
   39 struct arc4_ctx {
   40         unsigned int    x;
   41         unsigned int    y;
   42         unsigned int    state[256];
   43         /* was unsigned char, changed to int for performance -- onoe */
   44 };
   45 
   46 int
   47 arc4_ctxlen(void)
   48 {
   49         return sizeof(struct arc4_ctx);
   50 }
   51 
   52 void
   53 arc4_setkey(void *ctxp, const u_char *key, u_int keylen)
   54 {
   55         struct arc4_ctx *ctx = ctxp;
   56         unsigned int i, t, u, ki, si;
   57         unsigned int *state;
   58 
   59         state = ctx->state;
   60         ctx->x = 0;
   61         ctx->y = 0;
   62         for (i = 0; i < 256; i++)
   63                state[i] = i;
   64         ki = si = 0;
   65         for (i = 0; i < 256; i++) {
   66                 t = state[i];
   67                 si = (si + key[ki] + t) & 0xff;
   68                 u = state[si];
   69                 state[si] = t;
   70                 state[i] = u;
   71                 if (++ki >= keylen)
   72                         ki = 0;
   73         }
   74 }
   75 
   76 void
   77 arc4_encrypt(void *ctxp, u_char *dst, const u_char *src, int len)
   78 {
   79         struct arc4_ctx *ctx = ctxp;
   80         unsigned int x, y, sx, sy;
   81         unsigned int *state;
   82         const unsigned char *endsrc;
   83 
   84         state = ctx->state;
   85         x = ctx->x;
   86         y = ctx->y;
   87         for (endsrc = src + len; src != endsrc; src++, dst++) {
   88                 x = (x + 1) & 0xff;
   89                 sx = state[x];
   90                 y = (sx + y) & 0xff;
   91                 state[x] = sy = state[y];
   92                 state[y] = sx;
   93                 *dst = *src ^ state[(sx + sy) & 0xff];
   94         }
   95         ctx->x = x;
   96         ctx->y = y;
   97 }
   98 
   99 void
  100 arc4_decrypt(void *ctxp, u_char *dst, const u_char *src, int len)
  101 {
  102 
  103         arc4_encrypt(ctxp, dst, src, len);
  104 }

Cache object: 5c18067afa356a5a0ae8dbff5147d151


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.