]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
liblib: Added a common API for accessing all hash methods.
authorTimo Sirainen <tss@iki.fi>
Tue, 19 Oct 2010 17:09:16 +0000 (18:09 +0100)
committerTimo Sirainen <tss@iki.fi>
Tue, 19 Oct 2010 17:09:16 +0000 (18:09 +0100)
src/lib/Makefile.am
src/lib/hash-method.c [new file with mode: 0644]
src/lib/hash-method.h [new file with mode: 0644]
src/lib/md4.c
src/lib/md4.h
src/lib/md5.c
src/lib/md5.h
src/lib/sha1.c
src/lib/sha1.h
src/lib/sha2.c
src/lib/sha2.h

index 0ee3f61fa07e967a9a113339e7ac43251a68e01f..69700e51223ade95d7c51c3587beee056bc3f667 100644 (file)
@@ -36,6 +36,7 @@ liblib_la_SOURCES = \
        file-lock.c \
        file-set-size.c \
        hash.c \
+       hash-method.c \
        hash2.c \
        hex-binary.c \
        hex-dec.c \
@@ -145,6 +146,7 @@ headers = \
        file-set-size.h \
        fsync-mode.h \
        hash.h \
+       hash-method.h \
        hash2.h \
        hex-binary.h \
        hex-dec.h \
diff --git a/src/lib/hash-method.c b/src/lib/hash-method.c
new file mode 100644 (file)
index 0000000..2e16b86
--- /dev/null
@@ -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 --git a/src/lib/hash-method.h b/src/lib/hash-method.h
new file mode 100644 (file)
index 0000000..a330e60
--- /dev/null
@@ -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
index 034f24fe54ea657fdd158e756b77aeff4638272c..114d8f7c618f16710aa85451a966d5e564e8c928 100644 (file)
@@ -265,3 +265,27 @@ void md4_get_digest(const void *data, size_t size,
        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
+};
index fce40bd289b8f670686088777b2fe276d3ace58a..771e08c850eb124356abe051c25527d1344abc81 100644 (file)
@@ -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_final(struct md4_context *ctx, unsigned char result[MD4_RESULTLEN]);
 void md4_get_digest(const void *data, size_t size,
                    unsigned char result[MD4_RESULTLEN]);
 
+extern const struct hash_method hash_method_md4;
+
 #endif
index c7c645e5e02cec68ed3f577524fdc38c5b593ada..9f1dfea7fcfdd04ab4ab9c98c89e104def332160 100644 (file)
@@ -280,3 +280,27 @@ void md5_get_digest(const void *data, size_t size,
        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
+};
index aa0e426147415959365d4b080fbbb116db3f13a7..3da87fba375e14fb760d5b08cebe38b8061b69c6 100644 (file)
@@ -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_final(struct md5_context *ctx, unsigned char result[MD5_RESULTLEN]);
 void md5_get_digest(const void *data, size_t size,
                    unsigned char result[MD5_RESULTLEN]);
 
+extern const struct hash_method hash_method_md5;
+
 #endif
index 48ef5111dd4d70f82e14c0d9e196f328b462ea53..947f3f7d8fb114f934fe1ee50101d7925f3998e5 100644 (file)
@@ -261,3 +261,27 @@ void sha1_get_digest(const void *data, size_t size,
        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
+};
index a7d6cf8480d13e07d2f412c436511305ee538e79..539f3c50c535c5c00a396730be6ae340995eca27 100644 (file)
@@ -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 @@ typedef struct sha1_ctxt SHA1_CTX;
 extern void sha1_get_digest(const void *, size_t,
        unsigned char [SHA1_RESULTLEN]);
 
+extern const struct hash_method hash_method_sha1;
+
 #endif
index a1815b781b6e93576c98b2b317347acacd9c584d..9ea500d6f095dab9f5a1fae2ade88ca20f66f14e 100644 (file)
@@ -423,3 +423,51 @@ void sha512_get_digest(const void *data, size_t size,
        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)
+{
+       sha512_init(context);
+}
+static void hash_method_loop_sha512(void *context, const void *data, size_t size)
+{
+       sha512_loop(context, data, size);
+}
+
+static void hash_method_result_sha512(void *context, unsigned char *result_r)
+{
+       sha512_result(context, result_r);
+}
+
+const struct hash_method hash_method_sha512 = {
+       "sha512",
+       sizeof(struct sha512_ctx),
+       SHA512_RESULTLEN,
+
+       hash_method_init_sha512,
+       hash_method_loop_sha512,
+       hash_method_result_sha512
+};
index 7318fae0df1dd8e380f80cdc1db0dc4f8ce3c6cd..3ee937441d6d82b550f3b2e588ec0dc1fc2c3c1f 100644 (file)
@@ -34,6 +34,8 @@
 #ifndef SHA2_H
 #define SHA2_H
 
+#include "hash-method.h"
+
 #define SHA256_RESULTLEN (256 / 8)
 #define SHA256_BLOCK_SIZE (512 / 8)
 
@@ -70,4 +72,7 @@ void sha512_result(struct sha512_ctx *ctx,
 void sha512_get_digest(const void *data, size_t size,
                       unsigned char digest[SHA512_RESULTLEN]);
 
+extern const struct hash_method hash_method_sha256;
+extern const struct hash_method hash_method_sha512;
+
 #endif /* !SHA2_H */