Major memory management overhaul. Added arena support. Use the secure
[u/mdw/catacomb] / mprand.c
index bf8af69..d4acbb3 100644 (file)
--- a/mprand.c
+++ b/mprand.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: mprand.c,v 1.2 1999/12/22 15:55:33 mdw Exp $
+ * $Id: mprand.c,v 1.3 2000/06/17 11:45:09 mdw Exp $
  *
  * Generate a random multiprecision integer
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: mprand.c,v $
+ * Revision 1.3  2000/06/17 11:45:09  mdw
+ * Major memory management overhaul.  Added arena support.  Use the secure
+ * arena for secret integers.  Replace and improve the MP management macros
+ * (e.g., replace MP_MODIFY by MP_DEST).
+ *
  * Revision 1.2  1999/12/22 15:55:33  mdw
  * Modify `mprand' slightly.  Add `mprand_range'.
  *
@@ -67,7 +72,8 @@
 mp *mprand(mp *d, unsigned b, grand *r, mpw or)
 {
   size_t sz = (b + 7) >> 3;
-  octet *v = xmalloc(sz);
+  arena *a = (d && (d->f & MP_BURN)) ? arena_secure : arena_global;
+  octet *v = x_alloc(a, sz);
   unsigned m;
 
   /* --- Fill buffer with random data --- */
@@ -87,7 +93,8 @@ mp *mprand(mp *d, unsigned b, grand *r, mpw or)
 
   d = mp_loadb(d, v, sz);
   d->v[0] |= or;
-  free(v);
+  memset(v, 0, sz);
+  x_free(a, v);
   return (d);
 }
 
@@ -109,7 +116,8 @@ mp *mprand_range(mp *d, mp *l, grand *r, mpw or)
 {
   size_t b = mp_bits(l);
   size_t sz = (b + 7) >> 3;
-  octet *v = xmalloc(sz);
+  arena *a = (d && (d->f & MP_BURN)) ? arena_secure : arena_global;
+  octet *v = x_alloc(a, sz);
   unsigned m;
 
   /* --- The algorithm --- *
@@ -140,7 +148,8 @@ mp *mprand_range(mp *d, mp *l, grand *r, mpw or)
 
   /* --- Done --- */
 
-  free(v);
+  memset(v, 0, sz);
+  x_free(a, v);
   return (d);
 }