Release 2.1.2.
[u/mdw/catacomb] / dsarand.c
index fd54b6d..5372b3f 100644 (file)
--- a/dsarand.c
+++ b/dsarand.c
@@ -1,13 +1,13 @@
 /* -*-c-*-
  *
- * $Id: dsarand.c,v 1.1 1999/12/22 15:53:12 mdw Exp $
+ * $Id: dsarand.c,v 1.4 2004/04/08 01:36:15 mdw Exp $
  *
  * Random number generator for DSA
  *
  * (c) 1999 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of Catacomb.
  *
  * 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: dsarand.c,v $
- * Revision 1.1  1999/12/22 15:53:12  mdw
- * Random number generator for finding DSA parameters.
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include <stdarg.h>
 #include <string.h>
 
+#include <mLib/alloc.h>
 #include <mLib/bits.h>
 #include <mLib/sub.h>
 
@@ -84,6 +77,7 @@ void dsarand_init(dsarand *d, const void *p, size_t sz)
 {
   d->p = xmalloc(sz);
   d->sz = sz;
+  d->passes = 1;
   if (p)
     memcpy(d->p, p, sz);
 }
@@ -101,7 +95,7 @@ void dsarand_init(dsarand *d, const void *p, size_t sz)
 
 void dsarand_reseed(dsarand *d, const void *p, size_t sz)
 {
-  free(d->p);
+  xfree(d->p);
   d->p = xmalloc(sz);
   d->sz = sz;
   d->passes = 1;
@@ -120,7 +114,7 @@ void dsarand_reseed(dsarand *d, const void *p, size_t sz)
 
 void dsarand_destroy(dsarand *d)
 {
-  free(d->p);
+  xfree(d->p);
 }
 
 /* --- @dsarand_fill@ --- *
@@ -135,7 +129,7 @@ void dsarand_destroy(dsarand *d)
  *
  *             Let %$p$% be the numerical value of the input buffer, and let
  *             %$b$% be the number of bytes required.  Let
- *             %$z = \lceil b / 20 \rceil%$ be the number of SHA outputs
+ *             %$z = \lceil b / 20 \rceil$% be the number of SHA outputs
  *             required.  Then the output of pass %$n$% is
  *
  *               %$P_n = \sum_{0 \le i < z} 2^{160i} SHA(p + nz + i)$%
@@ -272,6 +266,8 @@ static int gmisc(grand *r, unsigned op, ...)
        case GRAND_SEEDBLOCK:
        case GRAND_SEEDRAND:
        case DSARAND_PASSES:
+       case DSARAND_SEEDSZ:
+       case DSARAND_GETSEED:
          rc = 1;
          break;
        default:
@@ -291,6 +287,12 @@ static int gmisc(grand *r, unsigned op, ...)
     case DSARAND_PASSES:
       g->d.passes = va_arg(ap, unsigned);
       break;
+    case DSARAND_SEEDSZ:
+      rc = g->d.sz;
+      break;
+    case DSARAND_GETSEED:
+      memcpy(va_arg(ap, void *), g->d.p, g->d.sz);
+      break;
     default:
       GRAND_BADOP;
       break;
@@ -308,7 +310,7 @@ static void gfill(grand *r, void *p, size_t sz)
 
 static const grand_ops gops = {
   "dsarand",
-  0,
+  0, 0,
   gmisc, gdestroy,
   grand_word, grand_byte, grand_word, grand_range, gfill
 };