dovecot-2.0: liblib: Added a common API for accessing all hash m...

dovecot at dovecot.org dovecot at dovecot.org
Tue Oct 19 20:54:57 EEST 2010


details:   http://hg.dovecot.org/dovecot-2.0/rev/368fd1cce4d6
changeset: 12307:368fd1cce4d6
user:      Timo Sirainen <tss at iki.fi>
date:      Tue Oct 19 18:09:16 2010 +0100
description:
liblib: Added a common API for accessing all hash methods.

diffstat:

 src/lib/Makefile.am   |   2 +
 src/lib/hash-method.c |  68 ++++++++++++++++++++++++++++++++++
 src/lib/hash-method.h |  21 ++++++++++
 src/lib/md4.c         |  24 ++++++++++++
 src/lib/md4.h         |   4 ++
 src/lib/md5.c         |  24 ++++++++++++
 src/lib/md5.h         |   4 ++
 src/lib/sha1.c        |  24 ++++++++++++
 src/lib/sha1.h        |   4 ++
 src/lib/sha2.c        |  48 ++++++++++++++++++++++++
 src/lib/sha2.h        |   5 ++
 11 files changed, 228 insertions(+), 0 deletions(-)

diffs (truncated from 341 to 300 lines):

diff -r d95510ee0c8f -r 368fd1cce4d6 src/lib/Makefile.am
--- a/src/lib/Makefile.am	Tue Oct 19 18:39:27 2010 +0100
+++ b/src/lib/Makefile.am	Tue Oct 19 18:09:16 2010 +0100
@@ -36,6 +36,7 @@
 	file-lock.c \
 	file-set-size.c \
 	hash.c \
+	hash-method.c \
 	hash2.c \
 	hex-binary.c \
 	hex-dec.c \
@@ -145,6 +146,7 @@
 	file-set-size.h \
 	fsync-mode.h \
 	hash.h \
+	hash-method.h \
 	hash2.h \
 	hex-binary.h \
 	hex-dec.h \
diff -r d95510ee0c8f -r 368fd1cce4d6 src/lib/hash-method.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/hash-method.c	Tue Oct 19 18:09:16 2010 +0100
@@ -0,0 +1,68 @@
+/* Copyright (c) 2010 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "md4.h"
+#include "md5.h"
+#include "sha1.h"
+#include "sha2.h"
+#include "hash-method.h"
+
+const struct hash_method *hash_method_lookup(const char *name)
+{
+	unsigned int i;
+
+	for (i = 0; hash_methods[i] != NULL; i++) {
+		if (strcmp(hash_methods[i]->name, name) == 0)
+			return hash_methods[i];
+	}
+	return NULL;
+}
+
+static void hash_method_init_size(void *context)
+{
+	uint64_t *ctx = context;
+
+	*ctx = 0;
+}
+
+static void
+hash_method_loop_size(void *context, const void *data ATTR_UNUSED, size_t size)
+{
+	uint64_t *ctx = context;
+
+	*ctx += size;
+}
+
+static void hash_method_result_size(void *context, unsigned char *result_r)
+{
+	uint64_t *ctx = context;
+
+	result_r[0] = (*ctx & 0xff00000000000000ULL) >> 56;
+	result_r[1] = (*ctx & 0x00ff000000000000ULL) >> 48;
+	result_r[2] = (*ctx & 0x0000ff0000000000ULL) >> 40;
+	result_r[3] = (*ctx & 0x000000ff00000000ULL) >> 32;
+	result_r[4] = (*ctx & 0x00000000ff000000ULL) >> 24;
+	result_r[5] = (*ctx & 0x0000000000ff0000ULL) >> 16;
+	result_r[6] = (*ctx & 0x000000000000ff00ULL) >> 8;
+	result_r[7] = (*ctx & 0x00000000000000ffULL);
+}
+
+const struct hash_method hash_method_size = {
+	"size",
+	sizeof(uint64_t),
+	sizeof(uint64_t),
+
+	hash_method_init_size,
+	hash_method_loop_size,
+	hash_method_result_size
+};
+
+const struct hash_method *hash_methods[] = {
+	&hash_method_md4,
+	&hash_method_md5,
+	&hash_method_sha1,
+	&hash_method_sha256,
+	&hash_method_sha512,
+	&hash_method_size,
+	NULL
+};
diff -r d95510ee0c8f -r 368fd1cce4d6 src/lib/hash-method.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/hash-method.h	Tue Oct 19 18:09:16 2010 +0100
@@ -0,0 +1,21 @@
+#ifndef HASH_METHOD_H
+#define HASH_METHOD_H
+
+struct hash_method {
+	const char *name;
+	/* Number of bytes that must be allocated for context */
+	unsigned int context_size;
+	/* Number of bytes that must be allocated for result()'s digest */
+	unsigned int digest_size;
+
+	void (*init)(void *context);
+	void (*loop)(void *context, const void *data, size_t size);
+	void (*result)(void *context, unsigned char *digest_r);
+};
+
+const struct hash_method *hash_method_lookup(const char *name);
+
+/* NULL-terminated list of all hash methods */
+extern const struct hash_method *hash_methods[];
+
+#endif
diff -r d95510ee0c8f -r 368fd1cce4d6 src/lib/md4.c
--- a/src/lib/md4.c	Tue Oct 19 18:39:27 2010 +0100
+++ b/src/lib/md4.c	Tue Oct 19 18:09:16 2010 +0100
@@ -265,3 +265,27 @@
 	md4_update(&ctx, data, size);
 	md4_final(&ctx, result);
 }
+
+static void hash_method_init_md4(void *context)
+{
+	md4_init(context);
+}
+static void hash_method_loop_md4(void *context, const void *data, size_t size)
+{
+	md4_update(context, data, size);
+}
+
+static void hash_method_result_md4(void *context, unsigned char *result_r)
+{
+	md4_final(context, result_r);
+}
+
+const struct hash_method hash_method_md4 = {
+	"md4",
+	sizeof(struct md4_context),
+	MD4_RESULTLEN,
+
+	hash_method_init_md4,
+	hash_method_loop_md4,
+	hash_method_result_md4
+};
diff -r d95510ee0c8f -r 368fd1cce4d6 src/lib/md4.h
--- a/src/lib/md4.h	Tue Oct 19 18:39:27 2010 +0100
+++ b/src/lib/md4.h	Tue Oct 19 18:09:16 2010 +0100
@@ -9,6 +9,8 @@
 #ifndef MD4_H
 #define MD4_H
 
+#include "hash-method.h"
+
 #define	MD4_RESULTLEN (128/8)
 
 struct md4_context {
@@ -25,4 +27,6 @@
 void md4_get_digest(const void *data, size_t size,
 		    unsigned char result[MD4_RESULTLEN]);
 
+extern const struct hash_method hash_method_md4;
+
 #endif
diff -r d95510ee0c8f -r 368fd1cce4d6 src/lib/md5.c
--- a/src/lib/md5.c	Tue Oct 19 18:39:27 2010 +0100
+++ b/src/lib/md5.c	Tue Oct 19 18:09:16 2010 +0100
@@ -280,3 +280,27 @@
 	md5_update(&ctx, data, size);
 	md5_final(&ctx, result);
 }
+
+static void hash_method_init_md5(void *context)
+{
+	md5_init(context);
+}
+static void hash_method_loop_md5(void *context, const void *data, size_t size)
+{
+	md5_update(context, data, size);
+}
+
+static void hash_method_result_md5(void *context, unsigned char *result_r)
+{
+	md5_final(context, result_r);
+}
+
+const struct hash_method hash_method_md5 = {
+	"md5",
+	sizeof(struct md5_context),
+	MD5_RESULTLEN,
+
+	hash_method_init_md5,
+	hash_method_loop_md5,
+	hash_method_result_md5
+};
diff -r d95510ee0c8f -r 368fd1cce4d6 src/lib/md5.h
--- a/src/lib/md5.h	Tue Oct 19 18:39:27 2010 +0100
+++ b/src/lib/md5.h	Tue Oct 19 18:09:16 2010 +0100
@@ -9,6 +9,8 @@
 #ifndef MD5_H
 #define MD5_H
 
+#include "hash-method.h"
+
 #define	MD5_RESULTLEN (128/8)
 
 struct md5_context {
@@ -25,4 +27,6 @@
 void md5_get_digest(const void *data, size_t size,
 		    unsigned char result[MD5_RESULTLEN]);
 
+extern const struct hash_method hash_method_md5;
+
 #endif
diff -r d95510ee0c8f -r 368fd1cce4d6 src/lib/sha1.c
--- a/src/lib/sha1.c	Tue Oct 19 18:39:27 2010 +0100
+++ b/src/lib/sha1.c	Tue Oct 19 18:09:16 2010 +0100
@@ -261,3 +261,27 @@
 	sha1_loop(&ctx, data, size);
 	sha1_result(&ctx, result);
 }
+
+static void hash_method_init_sha1(void *context)
+{
+	sha1_init(context);
+}
+static void hash_method_loop_sha1(void *context, const void *data, size_t size)
+{
+	sha1_loop(context, data, size);
+}
+
+static void hash_method_result_sha1(void *context, unsigned char *result_r)
+{
+	sha1_result(context, result_r);
+}
+
+const struct hash_method hash_method_sha1 = {
+	"sha1",
+	sizeof(struct sha1_ctxt),
+	SHA1_RESULTLEN,
+
+	hash_method_init_sha1,
+	hash_method_loop_sha1,
+	hash_method_result_sha1
+};
diff -r d95510ee0c8f -r 368fd1cce4d6 src/lib/sha1.h
--- a/src/lib/sha1.h	Tue Oct 19 18:39:27 2010 +0100
+++ b/src/lib/sha1.h	Tue Oct 19 18:09:16 2010 +0100
@@ -38,6 +38,8 @@
 #ifndef SHA1_H
 #define SHA1_H
 
+#include "hash-method.h"
+
 /* libmysqlclient really should try to keep its internal stuff internal so
    they won't conflict with the actual programs that are trying to use it.
    This particular instance has been fixed in 4.1.18 and 5.0.19, but there
@@ -77,4 +79,6 @@
 extern void sha1_get_digest(const void *, size_t,
 	unsigned char [SHA1_RESULTLEN]);
 
+extern const struct hash_method hash_method_sha1;
+
 #endif
diff -r d95510ee0c8f -r 368fd1cce4d6 src/lib/sha2.c
--- a/src/lib/sha2.c	Tue Oct 19 18:39:27 2010 +0100
+++ b/src/lib/sha2.c	Tue Oct 19 18:09:16 2010 +0100
@@ -423,3 +423,51 @@
 	sha512_loop(&ctx, data, size);
 	sha512_result(&ctx, digest);
 }
+
+static void hash_method_init_sha256(void *context)
+{
+	sha256_init(context);
+}
+static void hash_method_loop_sha256(void *context, const void *data, size_t size)
+{
+	sha256_loop(context, data, size);
+}
+
+static void hash_method_result_sha256(void *context, unsigned char *result_r)
+{
+	sha256_result(context, result_r);
+}
+
+const struct hash_method hash_method_sha256 = {
+	"sha256",
+	sizeof(struct sha256_ctx),
+	SHA256_RESULTLEN,
+
+	hash_method_init_sha256,
+	hash_method_loop_sha256,
+	hash_method_result_sha256
+};
+
+static void hash_method_init_sha512(void *context)
+{


More information about the dovecot-cvs mailing list