shishi-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

shishi/lib libgcrypt.c


From: shishi-commit
Subject: shishi/lib libgcrypt.c
Date: Sun, 19 Oct 2003 21:43:53 -0400

CVSROOT:        /cvsroot/shishi
Module name:    shishi
Branch:         
Changes by:     Simon Josefsson <address@hidden>        03/10/19 21:43:53

Modified files:
        lib            : libgcrypt.c 

Log message:
        (shishi_arcfour): Rewrite to use nettle arcfour, to make IVOUT work.

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/shishi/shishi/lib/libgcrypt.c.diff?tr1=1.19&tr2=1.20&r1=text&r2=text

Patches:
Index: shishi/lib/libgcrypt.c
diff -u shishi/lib/libgcrypt.c:1.19 shishi/lib/libgcrypt.c:1.20
--- shishi/lib/libgcrypt.c:1.19 Sun Sep 28 19:38:03 2003
+++ shishi/lib/libgcrypt.c      Sun Oct 19 21:43:53 2003
@@ -376,16 +376,83 @@
   return SHISHI_OK;
 }
 
+/* BEGIN: Taken from Nettle arcfour.h and arcfour.c */
+struct arcfour_ctx
+{
+  uint8_t S[256];
+  uint8_t i;
+  uint8_t j;
+};
+
+#define SWAP(a,b) do { int _t = a; a = b; b = _t; } while(0)
+
+static void
+arcfour_set_key(struct arcfour_ctx *ctx,
+               unsigned length, const uint8_t *key)
+{
+  unsigned i, j, k;
+
+  /* Initialize context */
+  for (i = 0; i<256; i++)
+    ctx->S[i] = i;
+
+  for (i = j = k = 0; i<256; i++)
+    {
+      j += ctx->S[i] + key[k]; j &= 0xff;
+      SWAP(ctx->S[i], ctx->S[j]);
+      /* Repeat key as needed */
+      k = (k + 1) % length;
+    }
+  ctx->i = ctx->j = 0;
+}
+
+static void
+arcfour_crypt(struct arcfour_ctx *ctx,
+             unsigned length, uint8_t *dst,
+             const uint8_t *src)
+{
+  register uint8_t i, j;
+
+  i = ctx->i; j = ctx->j;
+  while(length--)
+    {
+      i++; i &= 0xff;
+      j += ctx->S[i]; j &= 0xff;
+      SWAP(ctx->S[i], ctx->S[j]);
+      *dst++ = *src++ ^ ctx->S[ (ctx->S[i] + ctx->S[j]) & 0xff ];
+    }
+  ctx->i = i; ctx->j = j;
+}
+/* END: Taken from Nettle arcfour.h and arcfour.c */
+
 int
 shishi_arcfour (Shishi * handle, int decryptp,
                const char *key, size_t keylen,
                const char iv[258], char *ivout[258],
                const char *in, size_t inlen, char **out)
 {
-  /* XXX Support iv/ivout. */
-  return libgcrypt_dencrypt (handle, GCRY_CIPHER_ARCFOUR, 0,
-                            GCRY_CIPHER_MODE_STREAM, decryptp,
-                            key, keylen, NULL, NULL, in, inlen, out);
+  struct arcfour_ctx ctx;
+
+  /* Same as in nettle.c.  The reason for all this is that libgcrypt
+   * does not export any API to extract the ARCFOUR S-BOX, which we
+   * need. */
+
+  *out = xmalloc (inlen);
+
+  if (iv)
+    memcpy (&ctx, iv, sizeof (ctx));
+  else
+    arcfour_set_key (&ctx, keylen, key);
+
+  arcfour_crypt (&ctx, inlen, *out, in);
+
+  if (ivout)
+    {
+      *ivout = xmalloc (sizeof (ctx));
+      memcpy (*ivout, &ctx, sizeof (ctx));
+    }
+
+  return SHISHI_OK;
 }
 
 int




reply via email to

[Prev in Thread] Current Thread [Next in Thread]