Mercurial > gnulib
changeset 37162:2da368bd636c
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
author | Simon Josefsson <simon@josefsson.org> |
---|---|
date | Mon, 26 Aug 2013 21:31:15 +0200 |
parents | eedfb62ad2f7 |
children | 94affa13dea1 |
files | ChangeLog lib/gc-gnulib.c lib/gc-libgcrypt.c lib/gc.h lib/hmac-sha256.c lib/hmac-sha512.c lib/hmac.h m4/gc-hmac-sha256.m4 m4/gc-hmac-sha512.m4 m4/gc-sha256.m4 m4/gc-sha512.m4 m4/sha256.m4 m4/sha512.m4 modules/crypto/gc-hmac-sha256 modules/crypto/gc-hmac-sha256-tests modules/crypto/gc-hmac-sha512 modules/crypto/gc-hmac-sha512-tests modules/crypto/hmac-sha256 modules/crypto/hmac-sha256-tests modules/crypto/hmac-sha512 modules/crypto/hmac-sha512-tests tests/test-gc-hmac-sha256.c tests/test-gc-hmac-sha512.c tests/test-hmac-sha256.c tests/test-hmac-sha512.c |
diffstat | 25 files changed, 881 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Sat Aug 24 10:29:35 2013 +0200 +++ b/ChangeLog Mon Aug 26 21:31:15 2013 +0200 @@ -1,3 +1,34 @@ +2013-08-26 Simon Josefsson <simon@josefsson.org> + + 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 <ueno@gnu.org> * m4/intl.m4: Update from gettext-0.18.3.1, which fixes a misuse
--- a/lib/gc-gnulib.c Sat Aug 24 10:29:35 2013 +0200 +++ b/lib/gc-gnulib.c Mon Aug 26 21:31:15 2013 +0200 @@ -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 @@ 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
--- a/lib/gc-libgcrypt.c Sat Aug 24 10:29:35 2013 +0200 +++ b/lib/gc-libgcrypt.c Mon Aug 26 21:31:15 2013 +0200 @@ -656,7 +656,7 @@ 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 @@ 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
--- a/lib/gc.h Sat Aug 24 10:29:35 2013 +0200 +++ b/lib/gc.h Mon Aug 26 21:31:15 2013 +0200 @@ -159,6 +159,10 @@ 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/hmac-sha256.c Mon Aug 26 21:31:15 2013 +0200 @@ -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 <http://www.gnu.org/licenses/>. */ + +/* Written by Simon Josefsson. */ + +#include <config.h> + +#include "hmac.h" + +#include "memxor.h" +#include "sha256.h" + +#include <string.h> + +#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; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/hmac-sha512.c Mon Aug 26 21:31:15 2013 +0200 @@ -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 <http://www.gnu.org/licenses/>. */ + +/* Written by Simon Josefsson. */ + +#include <config.h> + +#include "hmac.h" + +#include "memxor.h" +#include "sha512.h" + +#include <string.h> + +#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; +}
--- a/lib/hmac.h Sat Aug 24 10:29:35 2013 +0200 +++ b/lib/hmac.h Mon Aug 26 21:31:15 2013 +0200 @@ -37,4 +37,20 @@ 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 */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/m4/gc-hmac-sha256.m4 Mon Aug 26 21:31:15 2013 +0200 @@ -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 +])
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/m4/gc-hmac-sha512.m4 Mon Aug 26 21:31:15 2013 +0200 @@ -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 +])
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/m4/gc-sha256.m4 Mon Aug 26 21:31:15 2013 +0200 @@ -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]) +])
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/m4/gc-sha512.m4 Mon Aug 26 21:31:15 2013 +0200 @@ -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]) +])
--- a/m4/sha256.m4 Sat Aug 24 10:29:35 2013 +0200 +++ b/m4/sha256.m4 Mon Aug 26 21:31:15 2013 +0200 @@ -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 @@ [ dnl Prerequisites of lib/sha256.c. AC_REQUIRE([gl_BIGENDIAN]) + : ])
--- a/m4/sha512.m4 Sat Aug 24 10:29:35 2013 +0200 +++ b/m4/sha512.m4 Mon Aug 26 21:31:15 2013 +0200 @@ -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 @@ [ dnl Prerequisites of lib/sha512.c. AC_REQUIRE([gl_BIGENDIAN]) + : ])
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/crypto/gc-hmac-sha256 Mon Aug 26 21:31:15 2013 +0200 @@ -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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/crypto/gc-hmac-sha256-tests Mon Aug 26 21:31:15 2013 +0200 @@ -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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/crypto/gc-hmac-sha512 Mon Aug 26 21:31:15 2013 +0200 @@ -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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/crypto/gc-hmac-sha512-tests Mon Aug 26 21:31:15 2013 +0200 @@ -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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/crypto/hmac-sha256 Mon Aug 26 21:31:15 2013 +0200 @@ -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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/crypto/hmac-sha256-tests Mon Aug 26 21:31:15 2013 +0200 @@ -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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/crypto/hmac-sha512 Mon Aug 26 21:31:15 2013 +0200 @@ -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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/crypto/hmac-sha512-tests Mon Aug 26 21:31:15 2013 +0200 @@ -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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test-gc-hmac-sha256.c Mon Aug 26 21:31:15 2013 +0200 @@ -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 <http://www.gnu.org/licenses/>. */ + +#include <config.h> + +#include <stdio.h> +#include <string.h> +#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; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test-gc-hmac-sha512.c Mon Aug 26 21:31:15 2013 +0200 @@ -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 <http://www.gnu.org/licenses/>. */ + +#include <config.h> + +#include <stdio.h> +#include <string.h> +#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; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test-hmac-sha256.c Mon Aug 26 21:31:15 2013 +0200 @@ -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 <http://www.gnu.org/licenses/>. */ + +/* Written by Simon Josefsson. Test vectors from RFC 4231. */ + +#include <config.h> + +#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\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; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test-hmac-sha512.c Mon Aug 26 21:31:15 2013 +0200 @@ -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 <http://www.gnu.org/licenses/>. */ + +/* Written by Simon Josefsson. Test vectors from RFC 4231. */ + +#include <config.h> + +#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\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; +}