dovecot-2.1: lib: Add hmac-sha1 adapted from hmac-md5

dovecot at dovecot.org dovecot at dovecot.org
Wed Nov 23 22:56:40 EET 2011


details:   http://hg.dovecot.org/dovecot-2.1/rev/4d56549a5505
changeset: 13762:4d56549a5505
user:      Florian Zeitz <florob at babelmonkeys.de>
date:      Fri Sep 16 02:22:49 2011 +0200
description:
lib: Add hmac-sha1 adapted from hmac-md5

diffstat:

 src/lib/Makefile.am |   1 +
 src/lib/hmac-sha1.c |  52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/lib/hmac-sha1.h |  22 ++++++++++++++++++++++
 3 files changed, 75 insertions(+), 0 deletions(-)

diffs (93 lines):

diff -r 59e25ebc976f -r 4d56549a5505 src/lib/Makefile.am
--- a/src/lib/Makefile.am	Wed Nov 23 22:08:09 2011 +0200
+++ b/src/lib/Makefile.am	Fri Sep 16 02:22:49 2011 +0200
@@ -43,6 +43,7 @@
 	hex-binary.c \
 	hex-dec.c \
 	hmac-md5.c \
+	hmac-sha1.c \
 	home-expand.c \
 	hostpid.c \
 	imem.c \
diff -r 59e25ebc976f -r 4d56549a5505 src/lib/hmac-sha1.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/hmac-sha1.c	Fri Sep 16 02:22:49 2011 +0200
@@ -0,0 +1,52 @@
+/*
+ * HMAC-SHA1 (RFC-2104) implementation.
+ *
+ * Copyright (c) 2004 Andrey Panin <pazke at donpac.ru>
+ * Copyright (c) 2011 Florian Zeitz <florob at babelmonkeys.de>
+ *
+ * This software is released under the MIT license.
+ */
+
+#include "lib.h"
+#include "hmac-sha1.h"
+#include "safe-memset.h"
+
+void hmac_sha1_init(struct hmac_sha1_context *ctx,
+		   const unsigned char *key, size_t key_len)
+{
+	int i;
+	unsigned char sha1key[20];
+	unsigned char k_ipad[64];
+	unsigned char k_opad[64];
+
+	if (key_len > 64) {
+		sha1_get_digest(key, key_len, sha1key);
+		key = sha1key;
+		key_len = 20;
+	}
+
+	memcpy(k_ipad, key, key_len);
+	memset(k_ipad + key_len, 0, 64 - key_len);
+	memcpy(k_opad, k_ipad, 64);
+
+	for (i = 0; i < 64; i++) {
+		k_ipad[i] ^= 0x36;
+		k_opad[i] ^= 0x5c;
+	}
+
+	sha1_init(&ctx->ctx);
+	sha1_loop(&ctx->ctx, k_ipad, 64);
+	sha1_init(&ctx->ctxo);
+	sha1_loop(&ctx->ctxo, k_opad, 64);
+
+	safe_memset(k_ipad, 0, 64);
+	safe_memset(k_opad, 0, 64);
+}
+
+void hmac_sha1_final(struct hmac_sha1_context *ctx, unsigned char *digest)
+{
+	sha1_result(&ctx->ctx, digest);
+
+	sha1_loop(&ctx->ctxo, digest, 20);
+	sha1_result(&ctx->ctxo, digest);
+}
diff -r 59e25ebc976f -r 4d56549a5505 src/lib/hmac-sha1.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/hmac-sha1.h	Fri Sep 16 02:22:49 2011 +0200
@@ -0,0 +1,22 @@
+#ifndef HMAC_SHA1_H
+#define HMAC_SHA1_H
+
+#include "sha1.h"
+
+struct hmac_sha1_context {
+	struct sha1_ctxt ctx, ctxo;
+};
+
+void hmac_sha1_init(struct hmac_sha1_context *ctx,
+		   const unsigned char *key, size_t key_len);
+void hmac_sha1_final(struct hmac_sha1_context *ctx,
+		    unsigned char digest[SHA1_RESULTLEN]);
+
+
+static inline void
+hmac_sha1_update(struct hmac_sha1_context *ctx, const void *data, size_t size)
+{
+	sha1_loop(&ctx->ctx, data, size);
+}
+
+#endif


More information about the dovecot-cvs mailing list