iPXE
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., 51 Franklin Street, Fifth Floor, Boston, MA
00019  * 02110-1301, USA.
00020  */
00021 
00022 FILE_LICENCE ( GPL2_OR_LATER );
00023 
00024 #include <ipxe/crypto.h>
00025 #include <ipxe/arc4.h>
00026 
00027 #define SWAP( ary, i, j )       \
00028         ({ u8 temp = ary[i]; ary[i] = ary[j]; ary[j] = temp; })
00029 
00030 /**
00031  * Set ARC4 key
00032  *
00033  * @v ctxv      ARC4 encryption context
00034  * @v keyv      Key to set
00035  * @v keylen    Length of key
00036  *
00037  * If an initialisation vector is to be used, it should be prepended
00038  * to the key; ARC4 does not implement the @c setiv function because
00039  * there is no standard length for an initialisation vector in the
00040  * cipher.
00041  */
00042 static int arc4_setkey ( void *ctxv, const void *keyv, size_t keylen )
00043 {
00044         struct arc4_ctx *ctx = ctxv;
00045         const u8 *key = keyv;
00046         u8 *S = ctx->state;
00047         int i, j;
00048 
00049         for ( i = 0; i < 256; i++ ) {
00050                 S[i] = i;
00051         }
00052 
00053         for ( i = j = 0; i < 256; i++ ) {
00054                 j = ( j + S[i] + key[i % keylen] ) & 0xff;
00055                 SWAP ( S, i, j );
00056         }
00057 
00058         ctx->i = ctx->j = 0;
00059         return 0;
00060 }
00061 
00062 /**
00063  * Perform ARC4 encryption or decryption
00064  *
00065  * @v ctxv      ARC4 encryption context
00066  * @v srcv      Data to encrypt or decrypt
00067  * @v dstv      Location to store encrypted or decrypted data
00068  * @v len       Length of data to operate on
00069  *
00070  * ARC4 is a stream cipher that works by generating a stream of PRNG
00071  * data based on the key, and XOR'ing it with the data to be
00072  * encrypted. Since XOR is symmetric, encryption and decryption in
00073  * ARC4 are the same operation.
00074  *
00075  * If you pass a @c NULL source or destination pointer, @a len
00076  * keystream bytes will be consumed without encrypting any data.
00077  */
00078 static void arc4_xor ( void *ctxv, const void *srcv, void *dstv,
00079                        size_t len )
00080 {
00081         struct arc4_ctx *ctx = ctxv;
00082         const u8 *src = srcv;
00083         u8 *dst = dstv;
00084         u8 *S = ctx->state;
00085         int i = ctx->i, j = ctx->j;
00086 
00087         while ( len-- ) {
00088                 i = ( i + 1 ) & 0xff;
00089                 j = ( j + S[i] ) & 0xff;
00090                 SWAP ( S, i, j );
00091                 if ( srcv && dstv )
00092                         *dst++ = *src++ ^ S[(S[i] + S[j]) & 0xff];
00093         }
00094 
00095         ctx->i = i;
00096         ctx->j = j;
00097 }
00098 
00099 static void arc4_setiv ( void *ctx __unused, const void *iv __unused )
00100 {
00101         /* ARC4 does not use a fixed-length IV */
00102 }
00103 
00104 
00105 /**
00106  * Perform ARC4 encryption or decryption, skipping initial keystream bytes
00107  *
00108  * @v key       ARC4 encryption key
00109  * @v keylen    Key length
00110  * @v skip      Number of bytes of keystream to skip
00111  * @v src       Message to encrypt or decrypt
00112  * @v msglen    Length of message
00113  * @ret dst     Encrypted or decrypted message
00114  */
00115 void arc4_skip ( const void *key, size_t keylen, size_t skip,
00116                  const void *src, void *dst, size_t msglen )
00117 {
00118         struct arc4_ctx ctx;
00119         arc4_setkey ( &ctx, key, keylen );
00120         arc4_xor ( &ctx, NULL, NULL, skip );
00121         arc4_xor ( &ctx, src, dst, msglen );
00122 }
00123 
00124 struct cipher_algorithm arc4_algorithm = {
00125         .name = "ARC4",
00126         .ctxsize = ARC4_CTX_SIZE,
00127         .blocksize = 1,
00128         .setkey = arc4_setkey,
00129         .setiv = arc4_setiv,
00130         .encrypt = arc4_xor,
00131         .decrypt = arc4_xor,
00132 };