[PATCH] Add ability to save/load an md5 checkpoint file

Ben Guthro ben.guthro at virtualcomputer.com
Wed Nov 30 22:59:36 CET 2011


For large encryption streams - it can be convenient to write a checkpoint file, 
to be able to be able to resume later.

This implements save/load of md5 checkpoints

Signed-off-by: Ben Guthro <ben.guthro at virtualcomputer.com>

diff --git a/cipher/md.c b/cipher/md.c
index 5ae9aee..7f3115f 100644
--- a/cipher/md.c
+++ b/cipher/md.c
@@ -1315,6 +1315,46 @@ gcry_md_is_enabled (gcry_md_hd_t a, int algo)
   return value;
 }
 
+int
+gcry_save_md5_checkpoint(FILE *f, gcry_md_hd_t *hd)
+{
+	GcryDigestEntry *entry;
+	int n;
+
+	n = (*hd)->ctx->actual_handle_size - sizeof (struct gcry_md_context);
+	if (fwrite(*hd, n, 1, f) != 1)
+		return (-1);
+	entry = (*hd)->ctx->list;
+	if (fwrite(entry->context.c, entry->digest->contextsize, 1, f) != 1)
+		return (-1);
+
+	return (0);
+}
+
+int
+gcry_load_md5_checkpoint(FILE *f, gcry_md_hd_t *hd)
+{
+	GcryDigestEntry *entry;
+	void *ctx;
+	int n;
+
+	gcry_md_open(hd, GCRY_MD_MD5, 0);
+	/* see comment above in md_open() for description of layout */
+	ctx = (*hd)->ctx;
+	n = (*hd)->ctx->actual_handle_size - sizeof (struct gcry_md_context);
+	if (fread(*hd, n, 1, f) != 1)
+		goto fail;
+	(*hd)->ctx = ctx;
+	entry = (*hd)->ctx->list;
+	if (fread(entry->context.c, entry->digest->contextsize, 1, f) != 1)
+		goto fail;
+
+	return (0);
+fail:
+	gcry_md_close(*hd);
+	return (-1);
+}
+
 
 /* Run the selftests for digest algorithm ALGO with optional reporting
    function REPORT.  */
diff --git a/src/gcrypt.h.in b/src/gcrypt.h.in
index b34ff08..565b5d3 100644
--- a/src/gcrypt.h.in
+++ b/src/gcrypt.h.in
@@ -22,6 +22,7 @@
 #ifndef _GCRYPT_H
 #define _GCRYPT_H
 
+#include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
 #include <string.h>
@@ -1444,6 +1445,8 @@ int gcry_is_secure (const void *a) _GCRY_GCC_ATTR_PURE;
 /* Return true if Libgcrypt is in FIPS mode.  */
 #define gcry_fips_mode_active()  !!gcry_control (GCRYCTL_FIPS_MODE_P, 0)
 
+int gcry_save_md5_checkpoint(FILE *f, gcry_md_hd_t *hd);
+int gcry_load_md5_checkpoint(FILE *f, gcry_md_hd_t *hd);
 
 #if 0 /* (Keep Emacsens' auto-indent happy.) */
 {
diff --git a/src/libgcrypt.def b/src/libgcrypt.def
index 9bf0167..93a3ba1 100644
--- a/src/libgcrypt.def
+++ b/src/libgcrypt.def
@@ -211,3 +211,6 @@ EXPORTS
       gcry_pk_get_param     @193
 
       gcry_kdf_derive       @194
+
+      gcry_save_md5_checkpoint   @195
+      gcry_load_md5_checkpoint   @196
diff --git a/src/libgcrypt.vers b/src/libgcrypt.vers
index dcb3749..7f32a7d 100644
--- a/src/libgcrypt.vers
+++ b/src/libgcrypt.vers
@@ -88,6 +88,8 @@ GCRYPT_1.6 {
     gcry_mpi_subm; gcry_mpi_swap; gcry_mpi_test_bit;
     gcry_mpi_lshift;
 
+    gcry_save_md5_checkpoint; gcry_load_md5_checkpoint;
+
   local:
     *;
 
diff --git a/src/visibility.c b/src/visibility.c
index 2d3edbc..3ec73c3 100644
--- a/src/visibility.c
+++ b/src/visibility.c
@@ -1144,3 +1144,15 @@ gcry_is_secure (const void *a)
 {
   return _gcry_is_secure (a);
 }
+
+int
+gcry_save_md5_checkpoint(FILE *f, gcry_md_hd_t *hd)
+{
+	_gcry_save_md5_checkpoint(f, hd);
+}
+
+int
+gcry_load_md5_checkpoint(FILE *f, gcry_md_hd_t *hd)
+{
+	_gcry_load_md5_checkpoint(f, hd);
+}
diff --git a/src/visibility.h b/src/visibility.h
index 4606a20..dc7ec77 100644
--- a/src/visibility.h
+++ b/src/visibility.h
@@ -185,6 +185,8 @@
 #define gcry_mpi_swap               _gcry_mpi_swap
 #define gcry_mpi_test_bit           _gcry_mpi_test_bit
 
+#define gcry_save_md5_checkpoint    _gcry_save_md5_checkpoint
+#define gcry_load_md5_checkpoint    _gcry_load_md5_checkpoint
 
 /* Include the main header here so that public symbols are mapped to
    the internal underscored ones.  */
@@ -390,7 +392,8 @@ gcry_err_code_t gcry_md_get (gcry_md_hd_t hd, int algo,
 #undef gcry_mpi_subm
 #undef gcry_mpi_swap
 #undef gcry_mpi_test_bit
-
+#undef gcry_save_md5_checkpoint
+#undef gcry_load_md5_checkpoint
 
 /* Now mark all symbols.  */
 
@@ -557,7 +560,8 @@ MARK_VISIBLE (gcry_mpi_subm)
 MARK_VISIBLE (gcry_mpi_swap)
 MARK_VISIBLE (gcry_mpi_test_bit)
 
-
+MARK_VISIBLE (gcry_save_md5_checkpoint)
+MARK_VISIBLE (gcry_load_md5_checkpoint)
 
 #undef MARK_VISIBLE
 #endif /*_GCRY_INCLUDED_BY_VISIBILITY_C*/



More information about the Gcrypt-devel mailing list