[dovecot-cvs] dovecot/src/auth mech-cram-md5.c, 1.6, 1.7 password-scheme-cram-md5.c, 1.2, 1.3

cras at dovecot.org cras at dovecot.org
Fri Jul 30 04:57:06 EEST 2004


Update of /home/cvs/dovecot/src/auth
In directory talvi:/tmp/cvs-serv10939/auth

Modified Files:
	mech-cram-md5.c password-scheme-cram-md5.c 
Log Message:
Merged CRAM-MD5 and NTLM hmac-md5 code. Patch by Joshua Goodall



Index: mech-cram-md5.c
===================================================================
RCS file: /home/cvs/dovecot/src/auth/mech-cram-md5.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- mech-cram-md5.c	31 May 2004 20:10:02 -0000	1.6
+++ mech-cram-md5.c	30 Jul 2004 01:57:04 -0000	1.7
@@ -7,7 +7,7 @@
 #include "ioloop.h"
 #include "buffer.h"
 #include "hex-binary.h"
-#include "md5.h"
+#include "hmac-md5.h"
 #include "randgen.h"
 #include "mech.h"
 #include "passdb.h"
@@ -50,8 +50,8 @@
 			      const char *credentials)
 {
 	
-	unsigned char digest[16], context_digest[32], *cdp;
-	struct md5_context ctxo, ctxi;
+	unsigned char digest[16], context_digest[32];
+        struct hmac_md5_context ctx;
 	buffer_t *context_digest_buf;
 	const char *response_hex;
 
@@ -65,30 +65,10 @@
 	if (hex_to_binary(credentials, context_digest_buf) <= 0)
 		return FALSE;
 
-#define CDGET(p, c) STMT_START { \
-	(c)  = (*p++);           \
-	(c) += (*p++ << 8);      \
-	(c) += (*p++ << 16);     \
-	(c) += (*p++ << 24);     \
-} STMT_END
-
-	cdp = context_digest;
-	CDGET(cdp, ctxo.a);
-	CDGET(cdp, ctxo.b);
-	CDGET(cdp, ctxo.c);
-	CDGET(cdp, ctxo.d);
-	CDGET(cdp, ctxi.a);
-	CDGET(cdp, ctxi.b);
-	CDGET(cdp, ctxi.c);
-	CDGET(cdp, ctxi.d);
-
-	ctxo.lo = ctxi.lo = 64;
-	ctxo.hi = ctxi.hi = 0;
+	hmac_md5_set_cram_context(&ctx, context_digest);
+	md5_update(&ctx.ctx, auth->challenge, strlen(auth->challenge));
+	hmac_md5_final(&ctx, digest);
 
-	md5_update(&ctxi, auth->challenge, strlen(auth->challenge));
-	md5_final(&ctxi, digest);
-	md5_update(&ctxo, digest, 16);
-	md5_final(&ctxo, digest);
 	response_hex = binary_to_hex(digest, 16);
 
 	if (memcmp(response_hex, auth->response, 32) != 0) {

Index: password-scheme-cram-md5.c
===================================================================
RCS file: /home/cvs/dovecot/src/auth/password-scheme-cram-md5.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- password-scheme-cram-md5.c	11 Nov 2003 09:59:27 -0000	1.2
+++ password-scheme-cram-md5.c	30 Jul 2004 01:57:04 -0000	1.3
@@ -1,58 +1,16 @@
 /* Copyright (C) 2003 Timo Sirainen / Joshua Goodall */
 
 #include "lib.h"
-#include "md5.h"
+#include "hmac-md5.h"
 #include "hex-binary.h"
 #include "password-scheme.h"
 
 const char *password_generate_cram_md5(const char *plaintext)
 {
-	unsigned char digest[16], ipad[64], opad[64], context_digest[32], *cdp;
-	struct md5_context ctxo, ctxi;
-	size_t len;
-	int i;
-
-	memset(ipad, 0, sizeof(ipad));
-	memset(opad, 0, sizeof(opad));
-
-	/* Hash excessively long passwords */
-	len = strlen(plaintext);
-	if (len > 64) {
-		md5_get_digest(plaintext, len, digest);
-		memcpy(ipad, digest, 16);
-		memcpy(opad, digest, 16);
-	} else {
-		memcpy(ipad, plaintext, len);
-		memcpy(opad, plaintext, len);
-	}
-
-	/* ipad/opad operation */
-	for (i = 0; i < 64; i++) {
-		ipad[i] ^= 0x36;
-		opad[i] ^= 0x5c;
-	}
-
-	md5_init(&ctxi);
-	md5_init(&ctxo);
-	md5_update(&ctxi, ipad, 64);
-	md5_update(&ctxo, opad, 64);
-
-	/* Make HMAC-MD5 hex digest */
-#define CDPUT(p, c) STMT_START {   \
-	*(p)++ = (c) & 0xff;       \
-	*(p)++ = (c) >> 8 & 0xff;  \
-	*(p)++ = (c) >> 16 & 0xff; \
-	*(p)++ = (c) >> 24 & 0xff; \
-} STMT_END
-	cdp = context_digest;
-	CDPUT(cdp, ctxo.a);
-	CDPUT(cdp, ctxo.b);
-	CDPUT(cdp, ctxo.c);
-	CDPUT(cdp, ctxo.d);
-	CDPUT(cdp, ctxi.a);
-	CDPUT(cdp, ctxi.b);
-	CDPUT(cdp, ctxi.c);
-	CDPUT(cdp, ctxi.d);
+	struct hmac_md5_context ctx;
+	unsigned char context_digest[32];
 
+	hmac_md5_init(&ctx, plaintext, strlen(plaintext));
+	hmac_md5_get_cram_context(&ctx, context_digest);
 	return binary_to_hex(context_digest, sizeof(context_digest));
 }



More information about the dovecot-cvs mailing list