libgcrypt and patches [CLEANUP_PATCH]

Dirk Stoecker misc at dstoecker.de
Mon Sep 5 11:13:55 CEST 2005


Hello,

> > Is there any sense in resending the patches?
> 
> I recall a mail which tried to solve some one time allocations - we
> want apply such a fix.

Ok, I try it again. Attached is a minor patch against current svn, which 
uses GCRYCTL_FINALIZE to get a cleanup of the whole library. When this is
used, all the loaded memory is freed in a way which (hopefully) brings
the system to initial state. It should be possible to reuse the library 
afterwards as if it has never been used (except for possible bugs). I use 
this for 3-4 months now and never had any problems.

I did not modify the examples, althought this would be a good idea. All 
the examples should run in valgrind without reporting any unfreed memory!

Ciao
-- 
 ____  _ _  ____  _ _    _ _  ____
|    |  |  |    |  | \  / |  |    | the cool Gremlin from Bischofswerda
|  __   |   ____|  |  \/  |  |    | WWW: http://www.dstoecker.de/
|    |  |  |       |      |  |    | PGP key available on www page.
|____| _|_ |____| _|_    _|_ |____| I hope AMIGA never stops making fun!
-------------- next part --------------

Eigenschafts?nderungen: 
___________________________________________________________________
Name: svn:ignore
   + aclocal.m4
autom4te.cache
config.h
config.h.in
config.log
config.status
configure
libtool
Makefile
Makefile.in
stamp-h1



Eigenschafts?nderungen: w32-dll
___________________________________________________________________
Name: svn:ignore
   + Makefile
Makefile.in



Eigenschafts?nderungen: mpi
___________________________________________________________________
Name: svn:ignore
   + .deps
.libs
asm-syntax.h
Makefile
Makefile.in
mpi-asm-defs.h
mpih-add1.S
mpih-lshift.S
mpih-mul1.S
mpih-mul2.S
mpih-mul3.S
mpih-rshift.S
mpih-sub1.S
sysdep.h



Eigenschafts?nderungen: src
___________________________________________________________________
Name: svn:ignore
   + .deps
.libs
libgcrypt-config
Makefile
Makefile.in


Index: src/global.c
===================================================================
--- src/global.c	(Revision 1102)
+++ src/global.c	(Arbeitskopie)
@@ -81,7 +81,18 @@
     BUG ();
 }
 
+static void
+global_finalize (void)
+{
+  _gcry_pk_finalize();
+  _gcry_md_finalize();
+  _gcry_cipher_finalize();
+//  ath_finalize();
+  _gcry_random_finalize();
+  any_init_done = 0;
+}
 
+
 static const char*
 parse_version_number( const char *s, int *number )
 {
@@ -284,6 +295,10 @@
       _gcry_fast_random_poll (); 
       break;
 
+    case GCRYCTL_FINALIZE:
+      global_finalize();
+      break;
+
     default:
       err = GPG_ERR_INV_OP;
     }
Index: src/g10lib.h
===================================================================
--- src/g10lib.h	(Revision 1102)
+++ src/g10lib.h	(Arbeitskopie)
@@ -239,6 +239,10 @@
 gcry_err_code_t _gcry_pk_init (void);
 gcry_err_code_t _gcry_ac_init (void);
 
+void _gcry_cipher_finalize(void);
+void _gcry_md_finalize(void);
+void _gcry_pk_finalize(void);
+
 gcry_err_code_t _gcry_pk_module_lookup (int id, gcry_module_t *module);
 void _gcry_pk_module_release (gcry_module_t module);
 gcry_err_code_t _gcry_pk_get_elements (int algo, char **enc, char **sig);

Eigenschafts?nderungen: tests
___________________________________________________________________
Name: svn:ignore
   + .deps
.libs
ac
ac-data
ac-schemes
basic
benchmark
hmac
keygen
Makefile
Makefile.in
pkbench
prime
pubkey
register
tsexp



Eigenschafts?nderungen: doc
___________________________________________________________________
Name: svn:ignore
   + gcrypt.info
Makefile
Makefile.in



Eigenschafts?nderungen: cipher
___________________________________________________________________
Name: svn:ignore
   + .deps
.libs
Makefile.in
Makefile


Index: cipher/pubkey.c
===================================================================
--- cipher/pubkey.c	(Revision 1102)
+++ cipher/pubkey.c	(Arbeitskopie)
@@ -2305,6 +2305,20 @@
   return err;
 }
 
+void
+_gcry_pk_finalize(void)
+{
+  if(default_pubkeys_registered)
+  {
+    ath_mutex_lock(&pubkeys_registered_lock);
+    while(pubkeys_registered)
+    {
+      _gcry_module_release(pubkeys_registered);
+    }
+    default_pubkeys_registered = 0;
+    ath_mutex_unlock(&pubkeys_registered_lock);
+  }
+}
 
 gcry_err_code_t
 _gcry_pk_module_lookup (int algorithm, gcry_module_t *module)
Index: cipher/md.c
===================================================================
--- cipher/md.c	(Revision 1102)
+++ cipher/md.c	(Arbeitskopie)
@@ -1212,6 +1212,20 @@
   return err;
 }
 
+void
+_gcry_md_finalize(void)
+{
+  if(default_digests_registered)
+  {
+    ath_mutex_lock(&digests_registered_lock);
+    while(digests_registered)
+    {
+      _gcry_module_release(digests_registered);
+    }
+    default_digests_registered = 0;
+    ath_mutex_unlock(&digests_registered_lock);
+  }
+}
 
 int
 gcry_md_is_secure (gcry_md_hd_t a) 
Index: cipher/cipher.c
===================================================================
--- cipher/cipher.c	(Revision 1102)
+++ cipher/cipher.c	(Arbeitskopie)
@@ -1377,6 +1377,21 @@
   return err;
 }
 
+void
+_gcry_cipher_finalize(void)
+{
+  if(default_ciphers_registered)
+  {
+    ath_mutex_lock(&ciphers_registered_lock);
+    while(ciphers_registered)
+    {
+      _gcry_module_release(ciphers_registered);
+    }
+    default_ciphers_registered = 0;
+    ath_mutex_unlock(&ciphers_registered_lock);
+  }
+}
+
 /* Get a list consisting of the IDs of the loaded cipher modules.  If
    LIST is zero, write the number of loaded cipher modules to
    LIST_LENGTH and return.  If LIST is non-zero, the first
Index: cipher/random.c
===================================================================
--- cipher/random.c	(Revision 1102)
+++ cipher/random.c	(Arbeitskopie)
@@ -168,10 +168,8 @@
   keypool = secure_alloc ? gcry_xcalloc_secure(1,POOLSIZE+BLOCKLEN)
                          : gcry_xcalloc(1,POOLSIZE+BLOCKLEN);
   is_initialized = 1;
-
 }
 
-
 /* Used to register a progress callback. */
 void
 _gcry_register_random_progress (void (*cb)(void *,const char*,int,int,int),
@@ -206,6 +204,17 @@
 }
 
 void
+_gcry_random_finalize(void)
+{
+  if(is_initialized)
+  {
+    gcry_free(rndpool);
+    gcry_free(keypool);
+    is_initialized = 0;
+  }
+}
+
+void
 _gcry_random_dump_stats()
 {
     log_info (
Index: cipher/random.h
===================================================================
--- cipher/random.h	(Revision 1102)
+++ cipher/random.h	(Arbeitskopie)
@@ -23,6 +23,7 @@
 #include "types.h"
 
 void _gcry_random_initialize (int full);
+void _gcry_random_finalize(void);
 void _gcry_register_random_progress (void (*cb)(void *,const char*,int,int,int),
                                      void *cb_data );
 void _gcry_random_dump_stats(void);


More information about the Gcrypt-devel mailing list