# HG changeset patch # User Simon Josefsson # Date 1129678550 0 # Node ID 1ddc29532d1892b421afa4ec2e3cd4d7602677b1 # Parent 3131bab6f53d88c1fff3a1beb7a018012d1d1118 Add gc-md4 and gc-md4-tests modules. diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2005-10-19 Simon Josefsson + + * modules/gc-md4, modules/gc-md4-tests: New file. + + * tests/test-gc-md4.c: New file. + 2005-10-18 Simon Josefsson * tests/test-md4.c: New file. diff --git a/lib/ChangeLog b/lib/ChangeLog --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,7 @@ +2005-10-19 Simon Josefsson + + * gc.h, gc-gnulib.c, gc-libgcrypt.c: Support MD4. + 2005-10-18 Simon Josefsson * md4.h, md4.c: New files, based on md5.?. diff --git a/lib/gc-gnulib.c b/lib/gc-gnulib.c --- a/lib/gc-gnulib.c +++ b/lib/gc-gnulib.c @@ -37,6 +37,9 @@ #include #include +#ifdef GC_USE_MD4 +# include "md4.h" +#endif #ifdef GC_USE_MD5 # include "md5.h" #endif @@ -149,6 +152,12 @@ { switch (hash) { +#ifdef GC_USE_MD4 + case GC_MD4: + md4_buffer (in, inlen, resbuf); + break; +#endif + #ifdef GC_USE_MD5 case GC_MD5: md5_buffer (in, inlen, resbuf); @@ -168,6 +177,15 @@ return GC_OK; } +#ifdef GC_USE_MD4 +Gc_rc +gc_md4 (const void *in, size_t inlen, void *resbuf) +{ + md4_buffer (in, inlen, resbuf); + return GC_OK; +} +#endif + #ifdef GC_USE_MD5 Gc_rc gc_md5 (const void *in, size_t inlen, void *resbuf) diff --git a/lib/gc-libgcrypt.c b/lib/gc-libgcrypt.c --- a/lib/gc-libgcrypt.c +++ b/lib/gc-libgcrypt.c @@ -222,6 +222,10 @@ switch (hash) { + case GC_MD4: + gcryalg = GCRY_MD_MD4; + break; + case GC_MD5: gcryalg = GCRY_MD_MD5; break; @@ -278,6 +282,10 @@ switch (hash) { + case GC_MD4: + gcryalg = GCRY_MD_MD4; + break; + case GC_MD5: gcryalg = GCRY_MD_MD5; break; @@ -333,6 +341,12 @@ switch (hash) { +#ifdef GC_USE_MD4 + case GC_MD4: + gcryalg = GCRY_MD_MD4; + break; +#endif + #ifdef GC_USE_MD5 case GC_MD5: gcryalg = GCRY_MD_MD5; @@ -362,6 +376,38 @@ /* One-call interface. */ +#ifdef GC_USE_MD4 +Gc_rc +gc_md4 (const void *in, size_t inlen, void *resbuf) +{ + size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD4); + gcry_md_hd_t hd; + gpg_error_t err; + unsigned char *p; + + assert (outlen == GC_MD4_DIGEST_SIZE); + + err = gcry_md_open (&hd, GCRY_MD_MD4, 0); + if (err != GPG_ERR_NO_ERROR) + return GC_INVALID_HASH; + + gcry_md_write (hd, in, inlen); + + p = gcry_md_read (hd, GCRY_MD_MD4); + if (p == NULL) + { + gcry_md_close (hd); + return GC_INVALID_HASH; + } + + memcpy (resbuf, p, outlen); + + gcry_md_close (hd); + + return GC_OK; +} +#endif + #ifdef GC_USE_MD5 Gc_rc gc_md5 (const void *in, size_t inlen, void *resbuf) diff --git a/lib/gc.h b/lib/gc.h --- a/lib/gc.h +++ b/lib/gc.h @@ -41,6 +41,7 @@ /* Hash types. */ enum Gc_hash { + GC_MD4, GC_MD5, GC_SHA1, GC_MD2, @@ -56,6 +57,7 @@ typedef void *gc_hash_handle; +#define GC_MD4_DIGEST_SIZE 16 #define GC_MD5_DIGEST_SIZE 16 #define GC_SHA1_DIGEST_SIZE 20 diff --git a/m4/ChangeLog b/m4/ChangeLog --- a/m4/ChangeLog +++ b/m4/ChangeLog @@ -1,3 +1,7 @@ +2005-10-19 Simon Josefsson + + * gc-md4.m4: New file. + 2005-10-18 Simon Josefsson * md4.m4: New file. diff --git a/m4/gc-md4.m4 b/m4/gc-md4.m4 new file mode 100644 --- /dev/null +++ b/m4/gc-md4.m4 @@ -0,0 +1,14 @@ +# gc-md4.m4 serial 1 +dnl Copyright (C) 2005 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_GC_MD4], +[ + AC_REQUIRE([gl_GC]) + AC_DEFINE(GC_USE_MD4, 1, [Define to if you want to support MD4 through GC.]) + if test "$ac_cv_libgcrypt" != yes; then + gl_MD4 + fi +]) diff --git a/modules/gc-md4 b/modules/gc-md4 new file mode 100644 --- /dev/null +++ b/modules/gc-md4 @@ -0,0 +1,26 @@ +Description: +Generic crypto wrappers for MD4 functions. + +Files: +m4/gc-md4.m4 +lib/md4.h +lib/md4.c +m4/md4.m4 + +Depends-on: +stdint +gc + +configure.ac: +gl_GC_MD4 + +Makefile.am: + +Include: +"gc.h" + +License: +LGPL + +Maintainer: +Simon Josefsson diff --git a/modules/gc-md4-tests b/modules/gc-md4-tests new file mode 100644 --- /dev/null +++ b/modules/gc-md4-tests @@ -0,0 +1,11 @@ +Files: +tests/test-gc-md4.c + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-gc-md4 +noinst_PROGRAMS += test-gc-md4 +test_gc_md4_SOURCES = test-gc-md4.c diff --git a/tests/test-gc-md4.c b/tests/test-gc-md4.c new file mode 100644 --- /dev/null +++ b/tests/test-gc-md4.c @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2005 Free Software Foundation + * Written by Simon Josefsson + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include +#include +#include "gc.h" + +int +main (int argc, char *argv[]) +{ + Gc_rc rc; + + rc = gc_init (); + if (rc != GC_OK) + { + printf ("gc_init() failed\n"); + return 1; + } + + /* Test vectors from RFC 1320. */ + + { + const char *in = "abc"; + size_t inlen = strlen (in); + const char *expect = + "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72\x9d"; + char out[16]; + + /* MD4 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b */ + + if (gc_md4 (in, inlen, out) != 0) + { + printf ("gc_md4 call failed\n"); + return 1; + } + + if (memcmp (out, expect, 16) != 0) + { + size_t i; + printf ("md4 1 missmatch. expected:\n"); + for (i = 0; i < 16; i++) + printf ("%02x ", expect[i] & 0xFF); + printf ("\ncomputed:\n"); + for (i = 0; i < 16; i++) + printf ("%02x ", out[i] & 0xFF); + printf ("\n"); + return 1; + } + } + + gc_done (); + + return 0; +}