bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

hmac-sha1


From: Simon Josefsson
Subject: hmac-sha1
Date: Fri, 07 Oct 2005 14:44:13 +0200
User-agent: Gnus/5.110004 (No Gnus v0.4) Emacs/22.0.50 (gnu/linux)

What do you think?  This is very similar to the hmac-md5 module.

The reason the modules are separate is that I don't want to force
hmac-sha1 users to have a hmac-md5 and md5 implementation around.
Alas, the sha1 module depend on md5 for 'md5_uint32'.  Couldn't we fix
it to use 'stdint' instead?  Doing that would perhaps make it possible
to sync md5 with glibc, because I think we could change glibc's md5.h
to use uint32_t too.

Thanks.

Index: modules/hmac-sha1
===================================================================
RCS file: modules/hmac-sha1
diff -N modules/hmac-sha1
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ modules/hmac-sha1   7 Oct 2005 12:42:18 -0000
@@ -0,0 +1,25 @@
+Description:
+Compute hashed message authentication codes with SHA1.
+
+Files:
+lib/hmac.h
+lib/hmac-sha1.c
+m4/hmac-sha1.m4
+
+Depends-on:
+memxor
+sha1
+
+configure.ac:
+gl_HMAC_SHA1
+
+Makefile.am:
+
+Include:
+"hmac.h"
+
+License:
+LGPL
+
+Maintainer:
+Simon Josefsson
Index: modules/hmac-sha1-tests
===================================================================
RCS file: modules/hmac-sha1-tests
diff -N modules/hmac-sha1-tests
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ modules/hmac-sha1-tests     7 Oct 2005 12:42:18 -0000
@@ -0,0 +1,11 @@
+Files:
+tests/test-hmac-sha1.c
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-hmac-sha1
+noinst_PROGRAMS += test-hmac-sha1
+test_hmac_sha1_SOURCES = test-hmac-sha1.c
Index: m4/hmac-sha1.m4
===================================================================
RCS file: m4/hmac-sha1.m4
diff -N m4/hmac-sha1.m4
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ m4/hmac-sha1.m4     7 Oct 2005 12:42:18 -0000
@@ -0,0 +1,11 @@
+# hmac-sha1.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_HMAC_SHA1],
+[
+  AC_LIBSOURCES([hmac.h, hmac-sha1.c])
+  AC_LIBOBJ([hmac-sha1])
+])
Index: lib/hmac.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/hmac.h,v
retrieving revision 1.1
diff -u -p -r1.1 hmac.h
--- lib/hmac.h  6 Oct 2005 15:58:27 -0000       1.1
+++ lib/hmac.h  7 Oct 2005 12:42:18 -0000
@@ -30,4 +30,12 @@ int
 hmac_md5 (const void *key, size_t keylen,
          const void *buffer, size_t buflen, void *resbuf);
 
+/* Compute Hashed Message Authentication Code with SHA-1, over BUFFER
+   data of BUFLEN bytes using the KEY of KEYLEN bytes, writing the
+   output to pre-allocated 20 byte minimum RESBUF buffer.  Return 0 on
+   success.  */
+int
+hmac_sha1 (const void *key, size_t keylen,
+          const void *in, size_t inlen, void *resbuf);
+
 #endif /* HMAC_H */
Index: lib/hmac-sha1.c
===================================================================
RCS file: lib/hmac-sha1.c
diff -N lib/hmac-sha1.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ lib/hmac-sha1.c     7 Oct 2005 12:42:18 -0000
@@ -0,0 +1,76 @@
+/* hmac-sha1.c -- hashed message authentication codes
+   Copyright (C) 2005 Free Software Foundation, Inc.
+
+   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.  */
+
+/* Written by Simon Josefsson.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "hmac.h"
+
+#include "sha1.h"
+
+#include <string.h>
+
+#define IPAD 0x36
+#define OPAD 0x5c
+
+int
+hmac_sha1 (const void *key, size_t keylen,
+          const void *in, size_t inlen, void *resbuf)
+{
+  struct md5_ctx inner;
+  struct md5_ctx outer;
+  char optkeybuf[20];
+  char block[64];
+  char innerhash[20];
+
+  if (keylen > 64)
+    {
+      struct sha1_ctx keyhash;
+
+      sha1_init_ctx (&keyhash);
+      sha1_process_bytes (key, keylen, &keyhash);
+      sha1_finish_ctx (&keyhash, optkeybuf);
+
+      key = optkeybuf;
+      keylen = 20;
+    }
+
+  sha1_init_ctx (&inner);
+
+  memset (block, IPAD, sizeof (block));
+  memxor (block, key, keylen);
+
+  sha1_process_block (block, 64, &inner);
+  sha1_process_bytes (in, inlen, &inner);
+
+  sha1_finish_ctx (&inner, innerhash);
+
+  sha1_init_ctx (&outer);
+
+  memset (block, OPAD, sizeof (block));
+  memxor (block, key, keylen);
+
+  sha1_process_block (block, 64, &outer);
+  sha1_process_bytes (innerhash, 20, &outer);
+
+  sha1_finish_ctx (&outer, resbuf);
+
+  return 0;
+}
Index: tests/test-hmac-sha1.c
===================================================================
RCS file: tests/test-hmac-sha1.c
diff -N tests/test-hmac-sha1.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ tests/test-hmac-sha1.c      7 Oct 2005 12:42:18 -0000
@@ -0,0 +1,125 @@
+/*
+ * Copyright (C) 2005 Free Software Foundation
+ *
+ * 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.  */
+
+/* Written by Simon Josefsson.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdio.h>
+#include <string.h>
+#include "hmac.h"
+
+int
+main (int argc, char *argv[])
+{
+  {
+    char *key =
+      "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
+    size_t key_len = 16;
+    char *data = "Hi There";
+    size_t data_len = 8;
+    char *digest =
+      
"\x67\x5b\x0b\x3a\x1b\x4d\xdf\x4e\x12\x48\x72\xda\x6c\x2f\x63\x2b\xfe\xd9\x57\xe9";
+    char out[20];
+
+    if (hmac_sha1 (key, key_len, data, data_len, out) != 0)
+      {
+       printf ("call failure\n");
+       return 1;
+      }
+
+    if (memcmp (digest, out, 20) != 0)
+      {
+       size_t i;
+       printf ("hash 1 missmatch. expected:\n");
+       for (i = 0; i < 20; i++)
+         printf ("%02x ", digest[i] & 0xFF);
+       printf ("\ncomputed:\n");
+       for (i = 0; i < 20; i++)
+         printf ("%02x ", out[i] & 0xFF);
+       printf ("\n");
+       return 1;
+      }
+  }
+
+  {
+    char *key = "Jefe";
+    size_t key_len = 4;
+    char *data = "what do ya want for nothing?";
+    size_t data_len = 28;
+    char *digest =
+      
"\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79";
+    char out[20];
+
+    if (hmac_sha1 (key, key_len, data, data_len, out) != 0)
+      {
+       printf ("call failure\n");
+       return 1;
+      }
+
+    if (memcmp (digest, out, 20) != 0)
+      {
+       size_t i;
+       printf ("hash 2 missmatch. expected:\n");
+       for (i = 0; i < 20; i++)
+         printf ("%02x ", digest[i] & 0xFF);
+       printf ("\ncomputed:\n");
+       for (i = 0; i < 20; i++)
+         printf ("%02x ", out[i] & 0xFF);
+       printf ("\n");
+       return 1;
+      }
+  }
+
+  {
+    char *key =
+      "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA";
+    size_t key_len = 16;
+    char *data = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
+      "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
+      "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
+      "\xDD\xDD";
+    size_t data_len = 50;
+    char *digest =
+      
"\xd7\x30\x59\x4d\x16\x7e\x35\xd5\x95\x6f\xd8\x00\x3d\x0d\xb3\xd3\xf4\x6d\xc7\xbb";
+    char out[20];
+
+    if (hmac_sha1 (key, key_len, data, data_len, out) != 0)
+      {
+       printf ("call failure\n");
+       return 1;
+      }
+
+    if (memcmp (digest, out, 20) != 0)
+      {
+       size_t i;
+       printf ("hash 3 missmatch. expected:\n");
+       for (i = 0; i < 20; i++)
+         printf ("%02x ", digest[i] & 0xFF);
+       printf ("\ncomputed:\n");
+       for (i = 0; i < 20; i++)
+         printf ("%02x ", out[i] & 0xFF);
+       printf ("\n");
+       return 1;
+      }
+  }
+
+  return 0;
+}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]