summaryrefslogtreecommitdiff
path: root/cipher/arcfour.c
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2003-12-09 14:10:32 +0000
committerWerner Koch <wk@gnupg.org>2003-12-09 14:10:32 +0000
commit840e10ca8330f55b53e61bab914ee4157c477021 (patch)
tree847e78f7e3340a288fe15d10598a3f9394f4658c /cipher/arcfour.c
parentec2f98eaa54748cbdbe4afd8971019cdf368e91e (diff)
downloadlibgcrypt-840e10ca8330f55b53e61bab914ee4157c477021.tar.gz
* dsa.c: Unified indentation style.
* elgamal.c: Ditto. * des.c (des_key_schedule): Code beautifications. * blowfish.c: Changed indentation style. * cast5.c (do_cast_setkey): Ditto.
Diffstat (limited to 'cipher/arcfour.c')
-rw-r--r--cipher/arcfour.c106
1 files changed, 54 insertions, 52 deletions
diff --git a/cipher/arcfour.c b/cipher/arcfour.c
index c41ff0d5..6bb0555c 100644
--- a/cipher/arcfour.c
+++ b/cipher/arcfour.c
@@ -50,7 +50,7 @@ do_encrypt_stream( ARCFOUR_context *ctx,
while ( length-- )
{
i++;
- i = i & 255; /* and seems to be faster than mod */
+ i = i & 255; /* The and-op seems to be faster than the mod-op. */
j += sbox[i];
j &= 255;
t = sbox[i]; sbox[i] = sbox[j]; sbox[j] = t;
@@ -74,39 +74,41 @@ encrypt_stream (void *context,
static gcry_err_code_t
do_arcfour_setkey (void *context, const byte *key, unsigned int keylen)
{
- static int initialized;
- static const char* selftest_failed;
- int i, j;
- byte karr[256];
- ARCFOUR_context *ctx = (ARCFOUR_context *) context;
-
- if( !initialized ) {
- initialized = 1;
- selftest_failed = selftest();
- if( selftest_failed )
- log_error ("ARCFOUR selftest failed (%s)\n", selftest_failed );
+ static int initialized;
+ static const char* selftest_failed;
+ int i, j;
+ byte karr[256];
+ ARCFOUR_context *ctx = (ARCFOUR_context *) context;
+
+ if (!initialized )
+ {
+ initialized = 1;
+ selftest_failed = selftest();
+ if( selftest_failed )
+ log_error ("ARCFOUR selftest failed (%s)\n", selftest_failed );
}
- if( selftest_failed )
- return GPG_ERR_SELFTEST_FAILED;
-
- if( keylen < 40/8 ) /* we want at least 40 bits */
- return GPG_ERR_INV_KEYLEN;
-
- ctx->idx_i = ctx->idx_j = 0;
- for (i=0; i < 256; i++ )
- ctx->sbox[i] = i;
- for (i=0; i < 256; i++ )
- karr[i] = key[i%keylen];
- for (i=j=0; i < 256; i++ ) {
- int t;
- j = (j + ctx->sbox[i] + karr[i]) % 256;
- t = ctx->sbox[i];
- ctx->sbox[i] = ctx->sbox[j];
- ctx->sbox[j] = t;
+ if( selftest_failed )
+ return GPG_ERR_SELFTEST_FAILED;
+
+ if( keylen < 40/8 ) /* we want at least 40 bits */
+ return GPG_ERR_INV_KEYLEN;
+
+ ctx->idx_i = ctx->idx_j = 0;
+ for (i=0; i < 256; i++ )
+ ctx->sbox[i] = i;
+ for (i=0; i < 256; i++ )
+ karr[i] = key[i%keylen];
+ for (i=j=0; i < 256; i++ )
+ {
+ int t;
+ j = (j + ctx->sbox[i] + karr[i]) % 256;
+ t = ctx->sbox[i];
+ ctx->sbox[i] = ctx->sbox[j];
+ ctx->sbox[j] = t;
}
- memset( karr, 0, 256 );
+ memset( karr, 0, 256 );
- return GPG_ERR_NO_ERROR;
+ return GPG_ERR_NO_ERROR;
}
static gcry_err_code_t
@@ -122,33 +124,33 @@ arcfour_setkey ( void *context, const byte *key, unsigned int keylen )
static const char*
selftest(void)
{
- ARCFOUR_context ctx;
- byte scratch[16];
+ ARCFOUR_context ctx;
+ byte scratch[16];
- /* Test vector from Cryptlib labeled there:
- * "from the State/Commerce Department" */
- static byte key_1[] =
- { 0x61, 0x8A, 0x63, 0xD2, 0xFB };
- static byte plaintext_1[] =
- { 0xDC, 0xEE, 0x4C, 0xF9, 0x2C };
- static const byte ciphertext_1[] =
- { 0xF1, 0x38, 0x29, 0xC9, 0xDE };
-
- arcfour_setkey( &ctx, key_1, sizeof(key_1));
- encrypt_stream( &ctx, scratch, plaintext_1, sizeof(plaintext_1));
- if (memcmp (scratch, ciphertext_1, sizeof (ciphertext_1)))
- return "Arcfour encryption test 1 failed.";
- arcfour_setkey( &ctx, key_1, sizeof(key_1));
- encrypt_stream(&ctx, scratch, scratch, sizeof(plaintext_1)); /* decrypt */
- if ( memcmp (scratch, plaintext_1, sizeof (plaintext_1)))
- return "Arcfour decryption test 1 failed.";
- return NULL;
+ /* Test vector from Cryptlib labeled there: "from the
+ State/Commerce Department". */
+ static byte key_1[] =
+ { 0x61, 0x8A, 0x63, 0xD2, 0xFB };
+ static byte plaintext_1[] =
+ { 0xDC, 0xEE, 0x4C, 0xF9, 0x2C };
+ static const byte ciphertext_1[] =
+ { 0xF1, 0x38, 0x29, 0xC9, 0xDE };
+
+ arcfour_setkey( &ctx, key_1, sizeof(key_1));
+ encrypt_stream( &ctx, scratch, plaintext_1, sizeof(plaintext_1));
+ if ( memcmp (scratch, ciphertext_1, sizeof (ciphertext_1)))
+ return "Arcfour encryption test 1 failed.";
+ arcfour_setkey( &ctx, key_1, sizeof(key_1));
+ encrypt_stream(&ctx, scratch, scratch, sizeof(plaintext_1)); /* decrypt */
+ if ( memcmp (scratch, plaintext_1, sizeof (plaintext_1)))
+ return "Arcfour decryption test 1 failed.";
+ return NULL;
}
-
gcry_cipher_spec_t _gcry_cipher_spec_arcfour =
{
"ARCFOUR", NULL, NULL, 1, 128, sizeof (ARCFOUR_context),
arcfour_setkey, NULL, NULL, encrypt_stream, encrypt_stream,
};
+