From: Simon Josefsson Date: Mon, 26 Aug 2013 19:31:15 +0000 (+0200) Subject: gc: support HMAC-SHA256 and HMAC-SHA512. X-Git-Tag: v0.1~58 X-Git-Url: http://erislabs.net/gitweb/?p=gnulib.git;a=commitdiff_plain;h=4e1dc0e3d73be7c53e10b8541a0313df3d1ae35b gc: support HMAC-SHA256 and HMAC-SHA512. * lib/gc.h: Add gc_hmac_sha256 and gc_hmac_sha512. * lib/gc-libgcrypt.c (gc_hmac_sha256, gc_hmac_sha512): New functions. (gc_hmac_md5): Use symbolic constant. * lib/gc-gnulib.c: Include hmac.h for HMAC-SHA256/512 too. (gc_hmac_sha256, gc_hmac_sha512): New functions. * lib/hmac.h: Add hmac_sha256 and hmac_sha512 prototypes. * m4/sha256.m4: Protect against empty expansion. * m4/sha512.m4: Likewise. * lib/hmac-sha256.c: New file. * lib/hmac-sha512.c: Likewise. * m4/gc-hmac-sha256.m4: Likewise. * m4/gc-hmac-sha512.m4: Likewise. * m4/gc-sha256.m4: Likewise. * m4/gc-sha512.m4: Likewise. * modules/crypto/gc-hmac-sha256: Likewise. * modules/crypto/gc-hmac-sha256-tests: Likewise. * modules/crypto/gc-hmac-sha512: Likewise. * modules/crypto/gc-hmac-sha512-tests: Likewise. * modules/crypto/hmac-sha256: Likewise. * modules/crypto/hmac-sha256-tests: Likewise. * modules/crypto/hmac-sha512: Likewise. * modules/crypto/hmac-sha512-tests: Likewise. * tests/test-gc-hmac-sha256.c: Likewise. * tests/test-gc-hmac-sha512.c: Likewise * tests/test-hmac-sha256.c: Likewise. * tests/test-hmac-sha512.c: Likewise --- diff --git a/ChangeLog b/ChangeLog index 994c6d733..31dc201c0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,34 @@ +2013-08-26 Simon Josefsson + + gc: support HMAC-SHA256 and HMAC-SHA512. + * lib/gc.h: Add gc_hmac_sha256 and gc_hmac_sha512. + * lib/gc-libgcrypt.c (gc_hmac_sha256, gc_hmac_sha512): New + functions. + (gc_hmac_md5): Use symbolic constant. + * lib/gc-gnulib.c: Include hmac.h for HMAC-SHA256/512 too. + (gc_hmac_sha256, gc_hmac_sha512): New functions. + * lib/hmac.h: Add hmac_sha256 and hmac_sha512 prototypes. + * m4/sha256.m4: Protect against empty expansion. + * m4/sha512.m4: Likewise. + * lib/hmac-sha256.c: New file. + * lib/hmac-sha512.c: Likewise. + * m4/gc-hmac-sha256.m4: Likewise. + * m4/gc-hmac-sha512.m4: Likewise. + * m4/gc-sha256.m4: Likewise. + * m4/gc-sha512.m4: Likewise. + * modules/crypto/gc-hmac-sha256: Likewise. + * modules/crypto/gc-hmac-sha256-tests: Likewise. + * modules/crypto/gc-hmac-sha512: Likewise. + * modules/crypto/gc-hmac-sha512-tests: Likewise. + * modules/crypto/hmac-sha256: Likewise. + * modules/crypto/hmac-sha256-tests: Likewise. + * modules/crypto/hmac-sha512: Likewise. + * modules/crypto/hmac-sha512-tests: Likewise. + * tests/test-gc-hmac-sha256.c: Likewise. + * tests/test-gc-hmac-sha512.c: Likewise + * tests/test-hmac-sha256.c: Likewise. + * tests/test-hmac-sha512.c: Likewise + 2013-08-24 Daiki Ueno * m4/intl.m4: Update from gettext-0.18.3.1, which fixes a misuse diff --git a/lib/gc-gnulib.c b/lib/gc-gnulib.c index 4ec85cb50..7bd5bbca1 100644 --- a/lib/gc-gnulib.c +++ b/lib/gc-gnulib.c @@ -48,7 +48,7 @@ #ifdef GNULIB_GC_SHA1 # include "sha1.h" #endif -#if defined(GNULIB_GC_HMAC_MD5) || defined(GNULIB_GC_HMAC_SHA1) +#if defined(GNULIB_GC_HMAC_MD5) || defined(GNULIB_GC_HMAC_SHA1) || defined(GNULIB_GC_HMAC_SHA256) || defined(GNULIB_GC_HMAC_SHA512) # include "hmac.h" #endif @@ -914,3 +914,23 @@ gc_hmac_sha1 (const void *key, size_t keylen, return GC_OK; } #endif + +#ifdef GNULIB_GC_HMAC_SHA256 +Gc_rc +gc_hmac_sha256 (const void *key, size_t keylen, + const void *in, size_t inlen, char *resbuf) +{ + hmac_sha256 (key, keylen, in, inlen, resbuf); + return GC_OK; +} +#endif + +#ifdef GNULIB_GC_HMAC_SHA512 +Gc_rc +gc_hmac_sha512 (const void *key, size_t keylen, + const void *in, size_t inlen, char *resbuf) +{ + hmac_sha512 (key, keylen, in, inlen, resbuf); + return GC_OK; +} +#endif diff --git a/lib/gc-libgcrypt.c b/lib/gc-libgcrypt.c index 050b89f08..3be427295 100644 --- a/lib/gc-libgcrypt.c +++ b/lib/gc-libgcrypt.c @@ -656,7 +656,7 @@ gc_hmac_md5 (const void *key, size_t keylen, unsigned char *hash; gpg_error_t err; - assert (hlen == 16); + assert (hlen == GC_MD5_DIGEST_SIZE); err = gcry_md_open (&mdh, GCRY_MD_MD5, GCRY_MD_FLAG_HMAC); if (err != GPG_ERR_NO_ERROR) @@ -725,3 +725,83 @@ gc_hmac_sha1 (const void *key, size_t keylen, return GC_OK; } #endif + +#ifdef GNULIB_GC_HMAC_SHA256 +Gc_rc +gc_hmac_sha256 (const void *key, size_t keylen, + const void *in, size_t inlen, char *resbuf) +{ + size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_SHA256); + gcry_md_hd_t mdh; + unsigned char *hash; + gpg_error_t err; + + assert (hlen == GC_SHA256_DIGEST_SIZE); + + err = gcry_md_open (&mdh, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC); + if (err != GPG_ERR_NO_ERROR) + return GC_INVALID_HASH; + + err = gcry_md_setkey (mdh, key, keylen); + if (err != GPG_ERR_NO_ERROR) + { + gcry_md_close (mdh); + return GC_INVALID_HASH; + } + + gcry_md_write (mdh, in, inlen); + + hash = gcry_md_read (mdh, GCRY_MD_SHA256); + if (hash == NULL) + { + gcry_md_close (mdh); + return GC_INVALID_HASH; + } + + memcpy (resbuf, hash, hlen); + + gcry_md_close (mdh); + + return GC_OK; +} +#endif + +#ifdef GNULIB_GC_HMAC_SHA512 +Gc_rc +gc_hmac_sha512 (const void *key, size_t keylen, + const void *in, size_t inlen, char *resbuf) +{ + size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_SHA512); + gcry_md_hd_t mdh; + unsigned char *hash; + gpg_error_t err; + + assert (hlen == GC_SHA512_DIGEST_SIZE); + + err = gcry_md_open (&mdh, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC); + if (err != GPG_ERR_NO_ERROR) + return GC_INVALID_HASH; + + err = gcry_md_setkey (mdh, key, keylen); + if (err != GPG_ERR_NO_ERROR) + { + gcry_md_close (mdh); + return GC_INVALID_HASH; + } + + gcry_md_write (mdh, in, inlen); + + hash = gcry_md_read (mdh, GCRY_MD_SHA512); + if (hash == NULL) + { + gcry_md_close (mdh); + return GC_INVALID_HASH; + } + + memcpy (resbuf, hash, hlen); + + gcry_md_close (mdh); + + return GC_OK; +} +#endif diff --git a/lib/gc.h b/lib/gc.h index 1482ba5ce..d2cd7d8ac 100644 --- a/lib/gc.h +++ b/lib/gc.h @@ -159,6 +159,10 @@ extern Gc_rc gc_hmac_md5 (const void *key, size_t keylen, const void *in, size_t inlen, char *resbuf); extern Gc_rc gc_hmac_sha1 (const void *key, size_t keylen, const void *in, size_t inlen, char *resbuf); +extern Gc_rc gc_hmac_sha256 (const void *key, size_t keylen, + const void *in, size_t inlen, char *resbuf); +extern Gc_rc gc_hmac_sha512 (const void *key, size_t keylen, + const void *in, size_t inlen, char *resbuf); /* Derive cryptographic keys from a password P of length PLEN, with salt S of length SLEN, placing the result in pre-allocated buffer diff --git a/lib/hmac-sha256.c b/lib/hmac-sha256.c new file mode 100644 index 000000000..a8e42c961 --- /dev/null +++ b/lib/hmac-sha256.c @@ -0,0 +1,80 @@ +/* hmac-sha256.c -- hashed message authentication codes + Copyright (C) 2005-2006, 2009-2013 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, see . */ + +/* Written by Simon Josefsson. */ + +#include + +#include "hmac.h" + +#include "memxor.h" +#include "sha256.h" + +#include + +#define IPAD 0x36 +#define OPAD 0x5c + +int +hmac_sha256 (const void *key, size_t keylen, + const void *in, size_t inlen, void *resbuf) +{ + struct sha256_ctx inner; + struct sha256_ctx outer; + char optkeybuf[32]; + char block[64]; + char innerhash[32]; + + /* Reduce the key's size, so that it becomes <= 64 bytes large. */ + + if (keylen > 64) + { + struct sha256_ctx keyhash; + + sha256_init_ctx (&keyhash); + sha256_process_bytes (key, keylen, &keyhash); + sha256_finish_ctx (&keyhash, optkeybuf); + + key = optkeybuf; + keylen = 32; + } + + /* Compute INNERHASH from KEY and IN. */ + + sha256_init_ctx (&inner); + + memset (block, IPAD, sizeof (block)); + memxor (block, key, keylen); + + sha256_process_block (block, 64, &inner); + sha256_process_bytes (in, inlen, &inner); + + sha256_finish_ctx (&inner, innerhash); + + /* Compute result from KEY and INNERHASH. */ + + sha256_init_ctx (&outer); + + memset (block, OPAD, sizeof (block)); + memxor (block, key, keylen); + + sha256_process_block (block, 64, &outer); + sha256_process_bytes (innerhash, 32, &outer); + + sha256_finish_ctx (&outer, resbuf); + + return 0; +} diff --git a/lib/hmac-sha512.c b/lib/hmac-sha512.c new file mode 100644 index 000000000..05bf6ce41 --- /dev/null +++ b/lib/hmac-sha512.c @@ -0,0 +1,80 @@ +/* hmac-sha512.c -- hashed message authentication codes + Copyright (C) 2005-2006, 2009-2013 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, see . */ + +/* Written by Simon Josefsson. */ + +#include + +#include "hmac.h" + +#include "memxor.h" +#include "sha512.h" + +#include + +#define IPAD 0x36 +#define OPAD 0x5c + +int +hmac_sha512 (const void *key, size_t keylen, + const void *in, size_t inlen, void *resbuf) +{ + struct sha512_ctx inner; + struct sha512_ctx outer; + char optkeybuf[64]; + char block[128]; + char innerhash[64]; + + /* Reduce the key's size, so that it becomes <= 128 bytes large. */ + + if (keylen > 128) + { + struct sha512_ctx keyhash; + + sha512_init_ctx (&keyhash); + sha512_process_bytes (key, keylen, &keyhash); + sha512_finish_ctx (&keyhash, optkeybuf); + + key = optkeybuf; + keylen = 128; + } + + /* Compute INNERHASH from KEY and IN. */ + + sha512_init_ctx (&inner); + + memset (block, IPAD, sizeof (block)); + memxor (block, key, keylen); + + sha512_process_block (block, 128, &inner); + sha512_process_bytes (in, inlen, &inner); + + sha512_finish_ctx (&inner, innerhash); + + /* Compute result from KEY and INNERHASH. */ + + sha512_init_ctx (&outer); + + memset (block, OPAD, sizeof (block)); + memxor (block, key, keylen); + + sha512_process_block (block, 128, &outer); + sha512_process_bytes (innerhash, 64, &outer); + + sha512_finish_ctx (&outer, resbuf); + + return 0; +} diff --git a/lib/hmac.h b/lib/hmac.h index 93970bc0e..a36a5af26 100644 --- a/lib/hmac.h +++ b/lib/hmac.h @@ -37,4 +37,20 @@ int hmac_sha1 (const void *key, size_t keylen, const void *in, size_t inlen, void *resbuf); +/* Compute Hashed Message Authentication Code with SHA-256, over BUFFER + data of BUFLEN bytes using the KEY of KEYLEN bytes, writing the + output to pre-allocated 32 byte minimum RESBUF buffer. Return 0 on + success. */ +int +hmac_sha256 (const void *key, size_t keylen, + const void *in, size_t inlen, void *resbuf); + +/* Compute Hashed Message Authentication Code with SHA-512, over BUFFER + data of BUFLEN bytes using the KEY of KEYLEN bytes, writing the + output to pre-allocated 64 byte minimum RESBUF buffer. Return 0 on + success. */ +int +hmac_sha512 (const void *key, size_t keylen, + const void *in, size_t inlen, void *resbuf); + #endif /* HMAC_H */ diff --git a/m4/gc-hmac-sha256.m4 b/m4/gc-hmac-sha256.m4 new file mode 100644 index 000000000..574af4218 --- /dev/null +++ b/m4/gc-hmac-sha256.m4 @@ -0,0 +1,14 @@ +# gc-hmac-sha256.m4 serial 3 +dnl Copyright (C) 2005, 2007, 2009-2013 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_HMAC_SHA256], +[ + AC_REQUIRE([gl_GC]) + if test "$ac_cv_libgcrypt" != yes; then + gl_SHA256 + gl_MEMXOR + fi +]) diff --git a/m4/gc-hmac-sha512.m4 b/m4/gc-hmac-sha512.m4 new file mode 100644 index 000000000..e2133588b --- /dev/null +++ b/m4/gc-hmac-sha512.m4 @@ -0,0 +1,14 @@ +# gc-hmac-sha512.m4 serial 3 +dnl Copyright (C) 2005, 2007, 2009-2013 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_HMAC_SHA512], +[ + AC_REQUIRE([gl_GC]) + if test "$ac_cv_libgcrypt" != yes; then + gl_SHA512 + gl_MEMXOR + fi +]) diff --git a/m4/gc-sha256.m4 b/m4/gc-sha256.m4 new file mode 100644 index 000000000..ab98bfb03 --- /dev/null +++ b/m4/gc-sha256.m4 @@ -0,0 +1,10 @@ +# gc-sha256.m4 serial 1 +dnl Copyright (C) 2005, 2007, 2009-2013 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_SHA256], +[ + AC_REQUIRE([gl_GC]) +]) diff --git a/m4/gc-sha512.m4 b/m4/gc-sha512.m4 new file mode 100644 index 000000000..33f76f41c --- /dev/null +++ b/m4/gc-sha512.m4 @@ -0,0 +1,10 @@ +# gc-sha512.m4 serial 1 +dnl Copyright (C) 2005, 2007, 2009-2013 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_SHA512], +[ + AC_REQUIRE([gl_GC]) +]) diff --git a/m4/sha256.m4 b/m4/sha256.m4 index 7eede32fa..cbbd17a64 100644 --- a/m4/sha256.m4 +++ b/m4/sha256.m4 @@ -1,4 +1,4 @@ -# sha256.m4 serial 6 +# sha256.m4 serial 7 dnl Copyright (C) 2005, 2008-2013 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -8,4 +8,5 @@ AC_DEFUN([gl_SHA256], [ dnl Prerequisites of lib/sha256.c. AC_REQUIRE([gl_BIGENDIAN]) + : ]) diff --git a/m4/sha512.m4 b/m4/sha512.m4 index 46c089589..f4a6bf13b 100644 --- a/m4/sha512.m4 +++ b/m4/sha512.m4 @@ -1,4 +1,4 @@ -# sha512.m4 serial 7 +# sha512.m4 serial 8 dnl Copyright (C) 2005-2006, 2008-2013 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -8,4 +8,5 @@ AC_DEFUN([gl_SHA512], [ dnl Prerequisites of lib/sha512.c. AC_REQUIRE([gl_BIGENDIAN]) + : ]) diff --git a/modules/crypto/gc-hmac-sha256 b/modules/crypto/gc-hmac-sha256 new file mode 100644 index 000000000..bbf559172 --- /dev/null +++ b/modules/crypto/gc-hmac-sha256 @@ -0,0 +1,24 @@ +Description: +Generic crypto wrappers for HMAC-SHA256 functions. + +Files: +m4/gc-hmac-sha256.m4 + +Depends-on: +crypto/gc +crypto/hmac-sha256 [test "$ac_cv_libgcrypt" != yes] + +configure.ac: +gl_GC_HMAC_SHA256 +gl_MODULE_INDICATOR([gc-hmac-sha256]) + +Makefile.am: + +Include: +"gc.h" + +License: +LGPLv2+ + +Maintainer: +Simon Josefsson diff --git a/modules/crypto/gc-hmac-sha256-tests b/modules/crypto/gc-hmac-sha256-tests new file mode 100644 index 000000000..55934e0c7 --- /dev/null +++ b/modules/crypto/gc-hmac-sha256-tests @@ -0,0 +1,10 @@ +Files: +tests/test-gc-hmac-sha256.c + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-gc-hmac-sha256 +check_PROGRAMS += test-gc-hmac-sha256 diff --git a/modules/crypto/gc-hmac-sha512 b/modules/crypto/gc-hmac-sha512 new file mode 100644 index 000000000..3d9e61813 --- /dev/null +++ b/modules/crypto/gc-hmac-sha512 @@ -0,0 +1,24 @@ +Description: +Generic crypto wrappers for HMAC-SHA512 functions. + +Files: +m4/gc-hmac-sha512.m4 + +Depends-on: +crypto/gc +crypto/hmac-sha512 [test "$ac_cv_libgcrypt" != yes] + +configure.ac: +gl_GC_HMAC_SHA512 +gl_MODULE_INDICATOR([gc-hmac-sha512]) + +Makefile.am: + +Include: +"gc.h" + +License: +LGPLv2+ + +Maintainer: +Simon Josefsson diff --git a/modules/crypto/gc-hmac-sha512-tests b/modules/crypto/gc-hmac-sha512-tests new file mode 100644 index 000000000..cc434c56f --- /dev/null +++ b/modules/crypto/gc-hmac-sha512-tests @@ -0,0 +1,10 @@ +Files: +tests/test-gc-hmac-sha512.c + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-gc-hmac-sha512 +check_PROGRAMS += test-gc-hmac-sha512 diff --git a/modules/crypto/hmac-sha256 b/modules/crypto/hmac-sha256 new file mode 100644 index 000000000..ee347bd3c --- /dev/null +++ b/modules/crypto/hmac-sha256 @@ -0,0 +1,24 @@ +Description: +Compute hashed message authentication codes with SHA256. + +Files: +lib/hmac.h +lib/hmac-sha256.c + +Depends-on: +memxor +crypto/sha256 + +configure.ac: + +Makefile.am: +lib_SOURCES += hmac-sha256.c + +Include: +"hmac.h" + +License: +LGPLv2+ + +Maintainer: +Simon Josefsson diff --git a/modules/crypto/hmac-sha256-tests b/modules/crypto/hmac-sha256-tests new file mode 100644 index 000000000..1337a0561 --- /dev/null +++ b/modules/crypto/hmac-sha256-tests @@ -0,0 +1,10 @@ +Files: +tests/test-hmac-sha256.c + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-hmac-sha256 +check_PROGRAMS += test-hmac-sha256 diff --git a/modules/crypto/hmac-sha512 b/modules/crypto/hmac-sha512 new file mode 100644 index 000000000..cd302b8e0 --- /dev/null +++ b/modules/crypto/hmac-sha512 @@ -0,0 +1,24 @@ +Description: +Compute hashed message authentication codes with SHA512. + +Files: +lib/hmac.h +lib/hmac-sha512.c + +Depends-on: +memxor +crypto/sha512 + +configure.ac: + +Makefile.am: +lib_SOURCES += hmac-sha512.c + +Include: +"hmac.h" + +License: +LGPLv2+ + +Maintainer: +Simon Josefsson diff --git a/modules/crypto/hmac-sha512-tests b/modules/crypto/hmac-sha512-tests new file mode 100644 index 000000000..d10b7052c --- /dev/null +++ b/modules/crypto/hmac-sha512-tests @@ -0,0 +1,10 @@ +Files: +tests/test-hmac-sha512.c + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-hmac-sha512 +check_PROGRAMS += test-hmac-sha512 diff --git a/tests/test-gc-hmac-sha256.c b/tests/test-gc-hmac-sha256.c new file mode 100644 index 000000000..edb8d7dcc --- /dev/null +++ b/tests/test-gc-hmac-sha256.c @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2005, 2010-2013 Free Software Foundation, Inc. + * 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 3, 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, see . */ + +#include + +#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; + } + + { + char *key = + "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"; + size_t key_len = 20; + char *data = "Hi There"; + size_t data_len = 8; + char *digest = + "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32\xcf\xf7"; + char out[GC_SHA256_DIGEST_SIZE]; + + if (gc_hmac_sha256 (key, key_len, data, data_len, out) != 0) + { + printf ("call failure\n"); + return 1; + } + + if (memcmp (digest, out, GC_SHA256_DIGEST_SIZE) != 0) + { + size_t i; + printf ("hash 1 mismatch. expected:\n"); + for (i = 0; i < GC_SHA256_DIGEST_SIZE; i++) + printf ("%02x ", digest[i] & 0xFF); + printf ("\ncomputed:\n"); + for (i = 0; i < GC_SHA256_DIGEST_SIZE; i++) + printf ("%02x ", out[i] & 0xFF); + printf ("\n"); + return 1; + } + } + + gc_done (); + + return 0; +} diff --git a/tests/test-gc-hmac-sha512.c b/tests/test-gc-hmac-sha512.c new file mode 100644 index 000000000..c9bbf1a04 --- /dev/null +++ b/tests/test-gc-hmac-sha512.c @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2005, 2010-2013 Free Software Foundation, Inc. + * 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 3, 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, see . */ + +#include + +#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; + } + + { + char *key = + "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"; + size_t key_len = 20; + char *data = "Hi There"; + size_t data_len = 8; + char *digest = + "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20\x3a\x12\x68\x54"; + char out[GC_SHA512_DIGEST_SIZE]; + + if (gc_hmac_sha512 (key, key_len, data, data_len, out) != 0) + { + printf ("call failure\n"); + return 1; + } + + if (memcmp (digest, out, GC_SHA512_DIGEST_SIZE) != 0) + { + size_t i; + printf ("hash 1 mismatch. expected:\n"); + for (i = 0; i < GC_SHA512_DIGEST_SIZE; i++) + printf ("%02x ", digest[i] & 0xFF); + printf ("\ncomputed:\n"); + for (i = 0; i < GC_SHA512_DIGEST_SIZE; i++) + printf ("%02x ", out[i] & 0xFF); + printf ("\n"); + return 1; + } + } + + gc_done (); + + return 0; +} diff --git a/tests/test-hmac-sha256.c b/tests/test-hmac-sha256.c new file mode 100644 index 000000000..4eaa83d10 --- /dev/null +++ b/tests/test-hmac-sha256.c @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2005, 2010-2013 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 3 of the License, 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, see . */ + +/* Written by Simon Josefsson. Test vectors from RFC 4231. */ + +#include + +#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\x0b\x0b\x0b\x0b"; + size_t key_len = 20; + char *data = "Hi There"; + size_t data_len = 8; + char *digest = + "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32\xcf\xf7"; + char out[32]; + + if (hmac_sha256 (key, key_len, data, data_len, out) != 0) + { + printf ("call failure\n"); + return 1; + } + + if (memcmp (digest, out, 32) != 0) + { + size_t i; + printf ("hash 1 mismatch. expected:\n"); + for (i = 0; i < 32; i++) + printf ("%02x ", digest[i] & 0xFF); + printf ("\ncomputed:\n"); + for (i = 0; i < 32; 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 = + "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec\x38\x43"; + char out[32]; + + if (hmac_sha256 (key, key_len, data, data_len, out) != 0) + { + printf ("call failure\n"); + return 1; + } + + if (memcmp (digest, out, 32) != 0) + { + size_t i; + printf ("hash 2 mismatch. expected:\n"); + for (i = 0; i < 32; i++) + printf ("%02x ", digest[i] & 0xFF); + printf ("\ncomputed:\n"); + for (i = 0; i < 32; 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\xAA\xAA\xAA\xAA"; + size_t key_len = 20; + 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 = + "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5\x65\xfe"; + char out[32]; + + if (hmac_sha256 (key, key_len, data, data_len, out) != 0) + { + printf ("call failure\n"); + return 1; + } + + if (memcmp (digest, out, 32) != 0) + { + size_t i; + printf ("hash 3 mismatch. expected:\n"); + for (i = 0; i < 32; i++) + printf ("%02x ", digest[i] & 0xFF); + printf ("\ncomputed:\n"); + for (i = 0; i < 32; i++) + printf ("%02x ", out[i] & 0xFF); + printf ("\n"); + return 1; + } + } + + return 0; +} diff --git a/tests/test-hmac-sha512.c b/tests/test-hmac-sha512.c new file mode 100644 index 000000000..f403ff14c --- /dev/null +++ b/tests/test-hmac-sha512.c @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2005, 2010-2013 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 3 of the License, 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, see . */ + +/* Written by Simon Josefsson. Test vectors from RFC 4231. */ + +#include + +#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\x0b\x0b\x0b\x0b"; + size_t key_len = 20; + char *data = "Hi There"; + size_t data_len = 8; + char *digest = + "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20\x3a\x12\x68\x54"; + char out[64]; + + if (hmac_sha512 (key, key_len, data, data_len, out) != 0) + { + printf ("call failure\n"); + return 1; + } + + if (memcmp (digest, out, 64) != 0) + { + size_t i; + printf ("hash 1 mismatch. expected:\n"); + for (i = 0; i < 64; i++) + printf ("%02x ", digest[i] & 0xFF); + printf ("\ncomputed:\n"); + for (i = 0; i < 64; 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 = + "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a\x38\xbc\xe7\x37"; + char out[64]; + + if (hmac_sha512 (key, key_len, data, data_len, out) != 0) + { + printf ("call failure\n"); + return 1; + } + + if (memcmp (digest, out, 64) != 0) + { + size_t i; + printf ("hash 2 mismatch. expected:\n"); + for (i = 0; i < 64; i++) + printf ("%02x ", digest[i] & 0xFF); + printf ("\ncomputed:\n"); + for (i = 0; i < 64; 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\xAA\xAA\xAA\xAA"; + size_t key_len = 20; + 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 = + "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59\xe1\x32\x92\xfb"; + char out[64]; + + if (hmac_sha512 (key, key_len, data, data_len, out) != 0) + { + printf ("call failure\n"); + return 1; + } + + if (memcmp (digest, out, 64) != 0) + { + size_t i; + printf ("hash 3 mismatch. expected:\n"); + for (i = 0; i < 64; i++) + printf ("%02x ", digest[i] & 0xFF); + printf ("\ncomputed:\n"); + for (i = 0; i < 64; i++) + printf ("%02x ", out[i] & 0xFF); + printf ("\n"); + return 1; + } + } + + return 0; +}