# HG changeset patch # User Simon Josefsson # Date 1129080824 0 # Node ID fb852f022f3f1d757d62addd2700db5496d69132 # Parent bae11f33bf3c16b95e7d18d1052a8bbe6fe3f695 Add hmac-sha1 module. diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2005-10-12 Simon Josefsson + + * tests/test-hmac-sha1.c: New file. + + * modules/hmac-sha1-tests: New file. + + * modules/hmac-sha1: New file. + 2005-10-12 Simon Josefsson * modules/gc-md5, modules/gc-hmac-md5: New files. diff --git a/lib/hmac-sha1.c b/lib/hmac-sha1.c new file mode 100644 --- /dev/null +++ b/lib/hmac-sha1.c @@ -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 +#endif + +#include "hmac.h" + +#include "sha1.h" + +#include + +#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; +} diff --git a/lib/hmac.h b/lib/hmac.h --- a/lib/hmac.h +++ b/lib/hmac.h @@ -30,4 +30,12 @@ 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 */ diff --git a/m4/ChangeLog b/m4/ChangeLog --- a/m4/ChangeLog +++ b/m4/ChangeLog @@ -1,3 +1,7 @@ +2005-10-12 Simon Josefsson + + * hmac-sha1.m4: New file. + 2005-10-12 Simon Josefsson * gc-md5.m4, gc-hmac-md5: New files. diff --git a/m4/hmac-sha1.m4 b/m4/hmac-sha1.m4 new file mode 100644 --- /dev/null +++ b/m4/hmac-sha1.m4 @@ -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 --- /dev/null +++ b/modules/hmac-sha1 @@ -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 --- /dev/null +++ b/modules/hmac-sha1-tests @@ -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 --- /dev/null +++ b/tests/test-hmac-sha1.c @@ -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 +#endif + +#include +#include +#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; +}