Generic interface.
authormdw <mdw>
Fri, 10 Dec 1999 23:16:01 +0000 (23:16 +0000)
committermdw <mdw>
Fri, 10 Dec 1999 23:16:01 +0000 (23:16 +0000)
gcipher.h [new file with mode: 0644]
ghash.h [new file with mode: 0644]
gmac.h [new file with mode: 0644]
grand.c [new file with mode: 0644]
grand.h [new file with mode: 0644]

diff --git a/gcipher.h b/gcipher.h
new file mode 100644 (file)
index 0000000..4831913
--- /dev/null
+++ b/gcipher.h
@@ -0,0 +1,83 @@
+/* -*-c-*-
+ *
+ * $Id: gcipher.h,v 1.1 1999/12/10 23:16:01 mdw Exp $
+ *
+ * Generic symmetric cipher interface
+ *
+ * (c) 1999 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ * 
+ * Catacomb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Library General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------* 
+ *
+ * $Log: gcipher.h,v $
+ * Revision 1.1  1999/12/10 23:16:01  mdw
+ * Generic interface.
+ *
+ */
+
+#ifndef CATACOMB_GCIPHER_H
+#define CATACOMB_GCIPHER_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <stddef.h>
+
+/*----- Generic symmetric cipher interface --------------------------------*/
+
+typedef struct gccipher_base {
+  const char *name;                    /* Cipher name */
+  size_t keysz;                                /* Preferred key size in bytes */
+  size_t blksz;                                /* Block size or zero if none */
+} gccipher_base;
+
+typedef struct gcipher {
+  const struct gcipher_ops *ops;       /* Pointer to cipher operations */
+} gcipher;
+
+typedef struct gcipher_ops {
+  const struct gccipher_base *b;       /* Pointer to basic information */
+  void (*encrypt)(gcipher */*c*/, const void */*s*/,
+                 void */*t*/, size_t /*sz*/);
+  void (*decrypt)(gcipher */*c*/, const void */*s*/,
+                 void */*t*/, size_t /*sz*/);
+  void (*destroy)(gcipher */*c*/);
+  void (*setiv)(gcipher */*c*/, const void */*iv*/);
+  void (*bdry)(gcipher */*c*/);
+} gcipher_ops;
+
+typedef struct gccipher {
+  gccipher_base b;
+  gcipher *(*init)(const void */*k*/, size_t /*sz*/);
+} gccipher;
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif
diff --git a/ghash.h b/ghash.h
new file mode 100644 (file)
index 0000000..9cee338
--- /dev/null
+++ b/ghash.h
@@ -0,0 +1,78 @@
+/* -*-c-*-
+ *
+ * $Id: ghash.h,v 1.1 1999/12/10 23:16:01 mdw Exp $
+ *
+ * Generic hash function interface
+ *
+ * (c) 1999 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ * 
+ * Catacomb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Library General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------* 
+ *
+ * $Log: ghash.h,v $
+ * Revision 1.1  1999/12/10 23:16:01  mdw
+ * Generic interface.
+ *
+ */
+
+#ifndef CATACOMB_GHASH_H
+#define CATACOMB_GHASH_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <stddef.h>
+
+/*----- Generic hash function interface -----------------------------------*/
+
+typedef struct gchash_base {
+  const char *name;                    /* Name of the hash function */
+  size_t hashsz;                       /* Size of output hash */
+} gchash_base;  
+
+typedef struct ghash {
+  const struct ghash_ops *ops;         /* Pointer to hash operations */
+} ghash;
+
+typedef struct ghash_ops {
+  const gchash_base *b;                        /* Pointer to basic information */
+  void (*hash)(ghash */*h*/, const void */*p*/, size_t /*sz*/); /* Hash */
+  void (*done)(ghash */*h*/, void */*buf*/); /* Write result */
+  void (*destroy)(ghash */*h*/);       /* Destroy hash block */
+} ghash_ops;
+
+typedef struct gchash {
+  gchash_base b;                       /* Basic bits of information */
+  ghash *(*init)(void);                        /* Create a new hash instance */
+} gchash;
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif
diff --git a/gmac.h b/gmac.h
new file mode 100644 (file)
index 0000000..9471308
--- /dev/null
+++ b/gmac.h
@@ -0,0 +1,76 @@
+/* -*-c-*-
+ *
+ * $Id: gmac.h,v 1.1 1999/12/10 23:16:01 mdw Exp $
+ *
+ * Generic MAC function interface
+ *
+ * (c) 1999 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ * 
+ * Catacomb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Library General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------* 
+ *
+ * $Log: gmac.h,v $
+ * Revision 1.1  1999/12/10 23:16:01  mdw
+ * Generic interface.
+ *
+ */
+
+#ifndef CATACOMB_GMAC_H
+#define CATACOMB_GMAC_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <stddef.h>
+
+#ifndef CATACOMB_GHASH_H
+#  include "ghash.h"
+#endif
+
+/*----- Generic MAC function interface ------------------------------------*/
+
+typedef struct gmac {
+  const struct gmac_ops *ops;          /* Pointer to MAC operations */
+} gmac;
+
+typedef struct gmac_ops {
+  const struct gchash_base *b;         /* Pointer to basic information */
+  ghash *(*init)(gmac */*m*/);         /* Create keyed hash instance */
+  void (*destroy)(gmac */*m*/);                /* Destroy MAC key block */
+} gmac_ops;
+
+typedef struct gcmac {
+  gchash_base b;                       /* Basic information */
+  gmac *(*key)(const void */*k*/, size_t /*sz*/); /* Create key */
+} gcmac;
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif
diff --git a/grand.c b/grand.c
new file mode 100644 (file)
index 0000000..0f9eee5
--- /dev/null
+++ b/grand.c
@@ -0,0 +1,166 @@
+/* -*-c-*-
+ *
+ * $Id: grand.c,v 1.1 1999/12/10 23:16:01 mdw Exp $
+ *
+ * Generic interface to random number generators
+ *
+ * (c) 1999 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ * 
+ * Catacomb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Library General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------* 
+ *
+ * $Log: grand.c,v $
+ * Revision 1.1  1999/12/10 23:16:01  mdw
+ * Generic interface.
+ *
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <stddef.h>
+
+#include <mLib/bits.h>
+
+#include "grand.h"
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @grand_byte@ --- *
+ *
+ * Arguments:  @grand *r@ = pointet to generic generator
+ *
+ * Returns:    A uniformly-distributed pseudorandom integer in the interval
+ *             %$[0, 256)$%.
+ */
+
+octet grand_byte(grand *r)
+{
+  if (r->ops->byte != grand_byte)
+    return (r->ops->byte(r));
+  else if (r->ops->word != grand_word)
+    return (r->ops->word(r) & 0xff);
+  else if (r->ops->fill != grand_fill) {
+    octet o;
+    r->ops->fill(r, &o, 1);
+    return (o);
+  } else
+    return (grand_range(r, 256));
+}
+
+/* --- @grand_word@ --- *
+ *
+ * Arguments:  @grand *r@ = pointet to generic generator
+ *
+ * Returns:    A uniformly-distributed pseudorandom integer in the interval
+ *             %$[0, 2^{32})$%.
+ */
+
+uint32 grand_word(grand *r)
+{
+  if (r->ops->word != grand_word)
+    return (r->ops->word(r));
+  else {
+    octet b[4];
+    grand_fill(r, b, sizeof(b));
+    return (LOAD32(b));
+  }
+}
+
+/* --- @grand_range@ --- *
+ *
+ * Arguments:  @grand *r@ = pointet to generic generator
+ *             @uint32 l@ = limit for acceptable results
+ *
+ * Returns:    A uniformly-distributed pseudorandom integer in the interval
+ *             %$[0, l)$%.
+ */
+
+uint32 grand_range(grand *r, uint32 l)
+{
+  if (r->ops->range != grand_range)
+    return (r->ops->range(r, l));
+  else {
+    uint32 m, z;
+    uint32 (*w)(grand */*r*/);
+    uint32 x;
+
+    /* --- Decide where to get data from --- *
+     *
+     * The choice of %$2^{32} - 1$% as a limit when using @grand_word@ isn't
+     * wonderful, but working with %$2^{32}$% is awkward and the loss of a
+     * few return values isn't significant.  The algorithm below still
+     * successfully returns uniformly distributed results.
+     */
+
+    if (r->ops->max) {
+      w = r->ops->raw;
+      m = r->ops->max;
+    } else {
+      w = grand_word;
+      m = 0xffffffff;
+    }
+
+    /* --- Work out maximum acceptable return value --- *
+     *
+     * This will be the highest multiple of @l@ less than @m@.
+     */
+
+    z = m - (m % l);
+    m = z / l;
+
+    /* --- Generate numbers until something acceptable is found --- *
+     *
+     * This will require an expected number of attempts less than 2.
+     */
+
+    do x = w(r); while (x >= z);
+    return (x / m);
+  }
+}
+
+/* --- @grand_fill@ --- *
+ *
+ * Arguments:  @grand *r@ = pointet to generic generator
+ *             @void *p@ = pointer to a buffer
+ *             @size_t sz@ = size of the buffer
+ *
+ * Returns:    ---
+ *
+ * Use:                Fills a buffer with uniformly distributed pseudorandom bytes
+ *             (see @grand_byte@).
+ */
+
+void grand_fill(grand *r, void *p, size_t sz)
+{
+  if (r->ops->fill != grand_fill)
+    r->ops->fill(r, p, sz);
+  else {
+    octet *q = p;
+    while (sz) {
+      *q++ = r->ops->byte(r);
+      sz--;
+    }
+  }
+}
+
+/*----- That's all, folks -------------------------------------------------*/
diff --git a/grand.h b/grand.h
new file mode 100644 (file)
index 0000000..04925bb
--- /dev/null
+++ b/grand.h
@@ -0,0 +1,160 @@
+/* -*-c-*-
+ *
+ * $Id: grand.h,v 1.1 1999/12/10 23:16:01 mdw Exp $
+ *
+ * Generic interface to random number generators
+ *
+ * (c) 1999 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ * 
+ * Catacomb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Library General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------* 
+ *
+ * $Log: grand.h,v $
+ * Revision 1.1  1999/12/10 23:16:01  mdw
+ * Generic interface.
+ *
+ */
+
+#ifndef CATACOMB_GRAND_H
+#define CATACOMB_GRAND_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <assert.h>
+#include <stddef.h>
+
+#include <mLib/bits.h>
+
+/*----- Generic random number generator interface -------------------------*/
+
+typedef struct grand {
+  const struct grand_ops *ops;
+} grand;
+
+typedef struct grand_ops {
+
+  /* --- Various important properties --- */
+
+  const char *name;                    /* Generator's name */
+  uint32 max;                          /* Maximum raw output */
+
+  /* --- Maintenance methods --- */
+
+  int (*misc)(grand */*r*/, unsigned /*op*/, ...); /* Miscellaneous ops */
+  void (*destroy)(grand */*r*/);       /* Destroy generator context */
+
+  /* --- Output methods --- *
+   *
+   * Only one of these operations need actually be implemented.  All the
+   * other operations may be synthesized.  Of course, performance is improved
+   * if more are provided.
+   */
+
+  uint32 (*raw)(grand */*r*/);         /* Uniform over %$[0, max)$% */
+  octet (*byte)(grand */*r*/);         /* Uniform over %$[0, 256)%$ */
+  uint32 (*word)(grand */*r*/);                /* Uniform over %$[0, 2^{32})$% */
+  uint32 (*range)(grand */*r*/, uint32 /*l*/); /* Uniform over %$[0, l)$% */
+  void (*fill)(grand */*r*/, void */*p*/, size_t /*sz*/); /* Fill buffer */
+} grand_ops;
+
+/* --- Operation types --- */
+
+enum {
+
+  /* --- Required operations --- */
+
+  GRAND_CHECK,                         /* @unsigned op2@ */
+
+  /* --- Standard seeding operations --- */
+
+  GRAND_SEEDINT,                       /* @int i@ */
+  GRAND_SEEDUINT32,                    /* @uint32 i@ */
+  GRAND_SEEDBLOCK,                     /* @const void *p, size_t sz@ */
+  GRAND_SEEDMP,                                /* @mp *m@ */
+  GRAND_SEEDRAND,                      /* @grand *g@ */
+
+  /* --- Generator-specific operations --- */
+
+  GRAND_SPECIFIC = 256u
+};
+
+#define GRAND_BADOP assert(((void)"bad grand_misc op", 0))
+
+/*----- Functions provided ------------------------------------------------*/
+
+/* --- @grand_byte@ --- *
+ *
+ * Arguments:  @grand *r@ = pointet to generic generator
+ *
+ * Returns:    A uniformly-distributed pseudorandom integer in the interval
+ *             %$[0, 256)$%.
+ */
+
+extern octet grand_byte(grand */*r*/);
+
+/* --- @grand_word@ --- *
+ *
+ * Arguments:  @grand *r@ = pointet to generic generator
+ *
+ * Returns:    A uniformly-distributed pseudorandom integer in the interval
+ *             %$[0, 2^{32})$%.
+ */
+
+extern uint32 grand_word(grand */*r*/);
+
+/* --- @grand_range@ --- *
+ *
+ * Arguments:  @grand *r@ = pointet to generic generator
+ *             @uint32 l@ = limit for acceptable results
+ *
+ * Returns:    A uniformly-distributed pseudorandom integer in the interval
+ *             %$[0, l)$%.
+ */
+
+extern uint32 grand_range(grand */*r*/, uint32 /*l*/);
+
+/* --- @grand_fill@ --- *
+ *
+ * Arguments:  @grand *r@ = pointet to generic generator
+ *             @void *p@ = pointer to a buffer
+ *             @size_t sz@ = size of the buffer
+ *
+ * Returns:    ---
+ *
+ * Use:                Fills a buffer with uniformly distributed pseudorandom bytes
+ *             (see @grand_byte@).
+ */
+
+extern void grand_fill(grand */*r*/, void */*p*/, size_t /*sz*/);
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif