arc4.c

Go to the documentation of this file.
00001 /*
00002  * The ARC4 stream cipher.
00003  *
00004  * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>.
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License as
00008  * published by the Free Software Foundation; either version 2 of the
00009  * License, or any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 FILE_LICENCE ( GPL2_OR_LATER );
00022 
00023 #include <gpxe/crypto.h>
00024 #include <gpxe/arc4.h>
00025 
00026 #define SWAP( ary, i, j )       \
00027         ({ u8 temp = ary[i]; ary[i] = ary[j]; ary[j] = temp; })
00028 
00029 /**
00030  * Set ARC4 key
00031  *
00032  * @v ctxv      ARC4 encryption context
00033  * @v keyv      Key to set
00034  * @v keylen    Length of key
00035  *
00036  * If an initialisation vector is to be used, it should be prepended
00037  * to the key; ARC4 does not implement the @c setiv function because
00038  * there is no standard length for an initialisation vector in the
00039  * cipher.
00040  */
00041 static int arc4_setkey ( void *ctxv, const void *keyv, size_t keylen )
00042 {
00043         struct arc4_ctx *ctx = ctxv;
00044         const u8 *key = keyv;
00045         u8 *S = ctx->state;
00046         int i, j;
00047 
00048         for ( i = 0; i < 256; i++ ) {
00049                 S[i] = i;
00050         }
00051 
00052         for ( i = j = 0; i < 256; i++ ) {
00053                 j = ( j + S[i] + key[i % keylen] ) & 0xff;
00054                 SWAP ( S, i, j );
00055         }
00056 
00057         ctx->i = ctx->j = 0;
00058         return 0;
00059 }
00060 
00061 /**
00062  * Perform ARC4 encryption or decryption
00063  *
00064  * @v ctxv      ARC4 encryption context
00065  * @v srcv      Data to encrypt or decrypt
00066  * @v dstv      Location to store encrypted or decrypted data
00067  * @v len       Length of data to operate on
00068  *
00069  * ARC4 is a stream cipher that works by generating a stream of PRNG
00070  * data based on the key, and XOR'ing it with the data to be
00071  * encrypted. Since XOR is symmetric, encryption and decryption in
00072  * ARC4 are the same operation.
00073  *
00074  * If you pass a @c NULL source or destination pointer, @a len
00075  * keystream bytes will be consumed without encrypting any data.
00076  */
00077 static void arc4_xor ( void *ctxv, const void *srcv, void *dstv,
00078                        size_t len )
00079 {
00080         struct arc4_ctx *ctx = ctxv;
00081         const u8 *src = srcv;
00082         u8 *dst = dstv;
00083         u8 *S = ctx->state;
00084         int i = ctx->i, j = ctx->j;
00085 
00086         while ( len-- ) {
00087                 i = ( i + 1 ) & 0xff;
00088                 j = ( j + S[i] ) & 0xff;
00089                 SWAP ( S, i, j );
00090                 if ( srcv && dstv )
00091                         *dst++ = *src++ ^ S[(S[i] + S[j]) & 0xff];
00092         }
00093 
00094         ctx->i = i;
00095         ctx->j = j;
00096 }
00097 
00098 static void arc4_setiv ( void *ctx __unused, const void *iv __unused )
00099 {
00100         /* ARC4 does not use a fixed-length IV */
00101 }
00102 
00103 
00104 /**
00105  * Perform ARC4 encryption or decryption, skipping initial keystream bytes
00106  *
00107  * @v key       ARC4 encryption key
00108  * @v keylen    Key length
00109  * @v skip      Number of bytes of keystream to skip
00110  * @v src       Message to encrypt or decrypt
00111  * @v msglen    Length of message
00112  * @ret dst     Encrypted or decrypted message
00113  */
00114 void arc4_skip ( const void *key, size_t keylen, size_t skip,
00115                  const void *src, void *dst, size_t msglen )
00116 {
00117         struct arc4_ctx ctx;
00118         arc4_setkey ( &ctx, key, keylen );
00119         arc4_xor ( &ctx, NULL, NULL, skip );
00120         arc4_xor ( &ctx, src, dst, msglen );
00121 }
00122 
00123 struct cipher_algorithm arc4_algorithm = {
00124         .name = "ARC4",
00125         .ctxsize = ARC4_CTX_SIZE,
00126         .blocksize = 1,
00127         .setkey = arc4_setkey,
00128         .setiv = arc4_setiv,
00129         .encrypt = arc4_xor,
00130         .decrypt = arc4_xor,
00131 };

Generated on Tue Apr 6 20:00:52 2010 for gPXE by  doxygen 1.5.7.1