From 4b8968ddae4a750b87d9f1a64a9833ee530bd398 Mon Sep 17 00:00:00 2001 From: mdw Date: Sat, 17 Jun 2000 11:55:22 +0000 Subject: [PATCH] New key size interface. Allow key material to be combined with an existing initialized context. Use secure arena for memory allocation. --- rc4.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 55 insertions(+), 23 deletions(-) diff --git a/rc4.c b/rc4.c index a4e595f..69adcc6 100644 --- a/rc4.c +++ b/rc4.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: rc4.c,v 1.3 1999/12/13 15:34:01 mdw Exp $ + * $Id: rc4.c,v 1.4 2000/06/17 11:55:22 mdw Exp $ * * The alleged RC4 stream cipher * @@ -30,6 +30,10 @@ /*----- Revision history --------------------------------------------------* * * $Log: rc4.c,v $ + * Revision 1.4 2000/06/17 11:55:22 mdw + * New key size interface. Allow key material to be combined with an + * existing initialized context. Use secure arena for memory allocation. + * * Revision 1.3 1999/12/13 15:34:01 mdw * Add support for seeding from a generic pseudorandom source. * @@ -50,35 +54,38 @@ #include #include +#include "arena.h" #include "gcipher.h" #include "grand.h" +#include "paranoia.h" #include "rc4.h" +/*----- Global variables --------------------------------------------------*/ + +const octet rc4_keysz[] = { KSZ_RANGE, RC4_KEYSZ, 1, 255, 1 }; + /*----- Main code ---------------------------------------------------------*/ -/* --- @rc4_init@ --- * +/* --- @rc4_addkey@ --- * * - * Arguments: @rc4_ctx *ctx@ = pointer to context to initialize + * Arguments: @rc4_ctx *ctx@ = pointer to context to key * @const void *k@ = pointer to key data to use * @size_t sz@ = size of the key data * * Returns: --- * - * Use: Initializes an RC4 context ready for use. + * Use: Mixes key data with an RC4 context. The RC4 context is not + * reset before mixing. This may be used to mix new key + * material with an existing RC4 context. */ -void rc4_init(rc4_ctx *ctx, const void *k, size_t sz) +void rc4_addkey(rc4_ctx *ctx, const void *k, size_t sz) { unsigned i, j; const octet *p = k, *q = p + sz; - assert(((void)"RC4 does not support zero length keys", sz != 0)); + KSZ_ASSERT(rc4, sz); - for (i = 0; i < 256; i++) - ctx->s[i] = i; - ctx->f = 0; - ctx->i = ctx->j = 0; - for (i = j = 0; i < 256; i++) { unsigned si = ctx->s[i]; j = (j + si + *p++) & 0xff; @@ -86,7 +93,30 @@ void rc4_init(rc4_ctx *ctx, const void *k, size_t sz) ctx->s[j] = si; if (p == q) p = k; - } + } + + ctx->i = ctx->j = 0; +} + +/* --- @rc4_init@ --- * + * + * Arguments: @rc4_ctx *ctx@ = pointer to context to initialize + * @const void *k@ = pointer to key data to use + * @size_t sz@ = size of the key data + * + * Returns: --- + * + * Use: Initializes an RC4 context ready for use. + */ + +void rc4_init(rc4_ctx *ctx, const void *k, size_t sz) +{ + unsigned i; + + for (i = 0; i < 256; i++) + ctx->s[i] = i; + ctx->f = 0; + rc4_addkey(ctx, k, sz); } /* --- @rc4_encrypt@ --- * @@ -131,7 +161,7 @@ static const gcipher_ops gops; static gcipher *ginit(const void *k, size_t sz) { - gctx *g = CREATE(gctx); + gctx *g = S_CREATE(gctx); g->c.ops = &gops; rc4_init(&g->rc4, k, sz); return (&g->c); @@ -146,16 +176,17 @@ static void gencrypt(gcipher *c, const void *s, void *t, size_t sz) static void gdestroy(gcipher *c) { gctx *g = (gctx *)c; - DESTROY(g); + BURN(*g); + S_DESTROY(g); } static const gcipher_ops gops = { - &rc4.b, + &rc4, gencrypt, gencrypt, gdestroy, 0, 0 }; const gccipher rc4 = { - { "rc4", 0, 0 }, + "rc4", rc4_keysz, 0, ginit }; @@ -169,7 +200,8 @@ typedef struct grctx { static void grdestroy(grand *r) { grctx *g = (grctx *)r; - DESTROY(g); + BURN(*g); + S_DESTROY(g); } static int grmisc(grand *r, unsigned op, ...) @@ -197,22 +229,22 @@ static int grmisc(grand *r, unsigned op, ...) break; case GRAND_SEEDINT: STORE32(buf, va_arg(ap, unsigned)); - rc4_init(&g->rc4, buf, sizeof(buf)); + rc4_addkey(&g->rc4, buf, sizeof(buf)); break; case GRAND_SEEDUINT32: STORE32(buf, va_arg(ap, uint32)); - rc4_init(&g->rc4, buf, sizeof(buf)); + rc4_addkey(&g->rc4, buf, sizeof(buf)); break; case GRAND_SEEDBLOCK: { const void *p = va_arg(ap, const void *); size_t sz = va_arg(ap, size_t); - rc4_init(&g->rc4, p, sz); + rc4_addkey(&g->rc4, p, sz); } break; case GRAND_SEEDRAND: { grand *rr = va_arg(ap, grand *); octet buf[16]; rr->ops->fill(rr, buf, sizeof(buf)); - rc4_init(&g->rc4, buf, sizeof(buf)); + rc4_addkey(&g->rc4, buf, sizeof(buf)); } break; default: GRAND_BADOP; @@ -250,7 +282,7 @@ static void grfill(grand *r, void *p, size_t sz) static const grand_ops grops = { "rc4", - 0, + GRAND_CRYPTO, 0, grmisc, grdestroy, grword, grbyte, grword, grand_range, grfill }; @@ -268,7 +300,7 @@ static const grand_ops grops = { grand *rc4_rand(const void *k, size_t sz) { - grctx *g = CREATE(grctx); + grctx *g = S_CREATE(grctx); g->r.ops = &grops; rc4_init(&g->rc4, k, sz); return (&g->r); -- 2.11.0