Add hmac-sha1 module.
authorSimon Josefsson <simon@josefsson.org>
Wed, 12 Oct 2005 01:33:44 +0000 (01:33 +0000)
committerSimon Josefsson <simon@josefsson.org>
Wed, 12 Oct 2005 01:33:44 +0000 (01:33 +0000)
ChangeLog
lib/hmac-sha1.c [new file with mode: 0644]
lib/hmac.h
m4/ChangeLog
m4/hmac-sha1.m4 [new file with mode: 0644]
modules/hmac-sha1 [new file with mode: 0644]
modules/hmac-sha1-tests [new file with mode: 0644]
tests/test-hmac-sha1.c [new file with mode: 0644]

index 64e8d39..d85cafa 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2005-10-12  Simon Josefsson  <jas@extundo.com>
 
+       * tests/test-hmac-sha1.c: New file.
+
+       * modules/hmac-sha1-tests: New file.
+
+       * modules/hmac-sha1: New file.
+
+2005-10-12  Simon Josefsson  <jas@extundo.com>
+
        * modules/gc-md5, modules/gc-hmac-md5: New files.
 
        * modules/gc (Files): Remove md5, memxor and hmac files.
diff --git a/lib/hmac-sha1.c b/lib/hmac-sha1.c
new file mode 100644 (file)
index 0000000..bee7231
--- /dev/null
@@ -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 sha1_ctx inner;
+  struct sha1_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 444213e..b1359aa 100644 (file)
@@ -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 5678658..2b5f250 100644 (file)
@@ -1,5 +1,9 @@
 2005-10-12  Simon Josefsson  <jas@extundo.com>
 
+       * hmac-sha1.m4: New file.
+
+2005-10-12  Simon Josefsson  <jas@extundo.com>
+
        * gc-md5.m4, gc-hmac-md5: New files.
 
        * gc.m4: Don't call gl_MD5, gl_MEMXOR or gl_HMAC_MD5.
diff --git a/m4/hmac-sha1.m4 b/m4/hmac-sha1.m4
new file mode 100644 (file)
index 0000000..1e95822
--- /dev/null
@@ -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])
+])
diff --git a/modules/hmac-sha1 b/modules/hmac-sha1
new file mode 100644 (file)
index 0000000..9b630c5
--- /dev/null
@@ -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
diff --git a/modules/hmac-sha1-tests b/modules/hmac-sha1-tests
new file mode 100644 (file)
index 0000000..0519c34
--- /dev/null
@@ -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
diff --git a/tests/test-hmac-sha1.c b/tests/test-hmac-sha1.c
new file mode 100644 (file)
index 0000000..663814d
--- /dev/null
@@ -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;
+}