2N/A/* arcfour.c - The arcfour stream cipher
2N/A * Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
2N/A *
2N/A * This file is part of Libgcrypt.
2N/A *
2N/A * Libgcrypt is free software; you can redistribute it and/or modify
2N/A * it under the terms of the GNU Lesser general Public License as
2N/A * published by the Free Software Foundation; either version 2.1 of
2N/A * the License, or (at your option) any later version.
2N/A *
2N/A * Libgcrypt is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU Lesser General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU Lesser General Public
2N/A * License along with this program; if not, write to the Free Software
2N/A * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
2N/A *
2N/A * For a description of the algorithm, see:
2N/A * Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1996.
2N/A * ISBN 0-471-11709-9. Pages 397 ff.
2N/A */
2N/A
2N/A
2N/A#include <config.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include "types.h"
2N/A#include "g10lib.h"
2N/A#include "cipher.h"
2N/A
2N/Astatic const char *selftest(void);
2N/A
2N/Atypedef struct {
2N/A int idx_i, idx_j;
2N/A byte sbox[256];
2N/A} ARCFOUR_context;
2N/A
2N/Astatic void
2N/Ado_encrypt_stream( ARCFOUR_context *ctx,
2N/A byte *outbuf, const byte *inbuf, unsigned int length )
2N/A{
2N/A register int i = ctx->idx_i;
2N/A register int j = ctx->idx_j;
2N/A register byte *sbox = ctx->sbox;
2N/A register int t;
2N/A
2N/A while ( length-- )
2N/A {
2N/A i++;
2N/A i = i & 255; /* The and-op seems to be faster than the mod-op. */
2N/A j += sbox[i];
2N/A j &= 255;
2N/A t = sbox[i]; sbox[i] = sbox[j]; sbox[j] = t;
2N/A *outbuf++ = *inbuf++ ^ sbox[(sbox[i] + sbox[j]) & 255];
2N/A }
2N/A
2N/A ctx->idx_i = i;
2N/A ctx->idx_j = j;
2N/A}
2N/A
2N/Astatic void
2N/Aencrypt_stream (void *context,
2N/A byte *outbuf, const byte *inbuf, unsigned int length)
2N/A{
2N/A ARCFOUR_context *ctx = (ARCFOUR_context *) context;
2N/A do_encrypt_stream (ctx, outbuf, inbuf, length );
2N/A _gcry_burn_stack (64);
2N/A}
2N/A
2N/A
2N/Astatic gcry_err_code_t
2N/Ado_arcfour_setkey (void *context, const byte *key, unsigned int keylen)
2N/A{
2N/A static int initialized;
2N/A static const char* selftest_failed;
2N/A int i, j;
2N/A byte karr[256];
2N/A ARCFOUR_context *ctx = (ARCFOUR_context *) context;
2N/A
2N/A if (!initialized )
2N/A {
2N/A initialized = 1;
2N/A selftest_failed = selftest();
2N/A if( selftest_failed )
2N/A log_error ("ARCFOUR selftest failed (%s)\n", selftest_failed );
2N/A }
2N/A if( selftest_failed )
2N/A return GPG_ERR_SELFTEST_FAILED;
2N/A
2N/A if( keylen < 40/8 ) /* we want at least 40 bits */
2N/A return GPG_ERR_INV_KEYLEN;
2N/A
2N/A ctx->idx_i = ctx->idx_j = 0;
2N/A for (i=0; i < 256; i++ )
2N/A ctx->sbox[i] = i;
2N/A for (i=0; i < 256; i++ )
2N/A karr[i] = key[i%keylen];
2N/A for (i=j=0; i < 256; i++ )
2N/A {
2N/A int t;
2N/A j = (j + ctx->sbox[i] + karr[i]) % 256;
2N/A t = ctx->sbox[i];
2N/A ctx->sbox[i] = ctx->sbox[j];
2N/A ctx->sbox[j] = t;
2N/A }
2N/A memset( karr, 0, 256 );
2N/A
2N/A return GPG_ERR_NO_ERROR;
2N/A}
2N/A
2N/Astatic gcry_err_code_t
2N/Aarcfour_setkey ( void *context, const byte *key, unsigned int keylen )
2N/A{
2N/A ARCFOUR_context *ctx = (ARCFOUR_context *) context;
2N/A gcry_err_code_t rc = do_arcfour_setkey (ctx, key, keylen );
2N/A _gcry_burn_stack (300);
2N/A return rc;
2N/A}
2N/A
2N/A
2N/Astatic const char*
2N/Aselftest(void)
2N/A{
2N/A ARCFOUR_context ctx;
2N/A byte scratch[16];
2N/A
2N/A /* Test vector from Cryptlib labeled there: "from the
2N/A State/Commerce Department". */
2N/A static byte key_1[] =
2N/A { 0x61, 0x8A, 0x63, 0xD2, 0xFB };
2N/A static byte plaintext_1[] =
2N/A { 0xDC, 0xEE, 0x4C, 0xF9, 0x2C };
2N/A static const byte ciphertext_1[] =
2N/A { 0xF1, 0x38, 0x29, 0xC9, 0xDE };
2N/A
2N/A arcfour_setkey( &ctx, key_1, sizeof(key_1));
2N/A encrypt_stream( &ctx, scratch, plaintext_1, sizeof(plaintext_1));
2N/A if ( memcmp (scratch, ciphertext_1, sizeof (ciphertext_1)))
2N/A return "Arcfour encryption test 1 failed.";
2N/A arcfour_setkey( &ctx, key_1, sizeof(key_1));
2N/A encrypt_stream(&ctx, scratch, scratch, sizeof(plaintext_1)); /* decrypt */
2N/A if ( memcmp (scratch, plaintext_1, sizeof (plaintext_1)))
2N/A return "Arcfour decryption test 1 failed.";
2N/A return NULL;
2N/A}
2N/A
2N/A
2N/Agcry_cipher_spec_t _gcry_cipher_spec_arcfour =
2N/A {
2N/A "ARCFOUR", NULL, NULL, 1, 128, sizeof (ARCFOUR_context),
2N/A arcfour_setkey, NULL, NULL, encrypt_stream, encrypt_stream,
2N/A };
2N/A