A net removal of 240 lines.
* lib/hmac.c: A new parameterized single implementation.
* lib/hmac-md5.c: Define parameters and include implementation.
* lib/hmac-sha1.c: Likewise.
* lib/hmac-sha256.c: Likewise.
* lib/hmac-sha512.c: Likewise.
* modules/crypto/hmac-md5: Reference the new implementation file.
* modules/crypto/hmac-sha1: Likewise.
* modules/crypto/hmac-sha256: Likewise.
* modules/crypto/hmac-sha512: Likewise.
* tests/test-hmac-md5.c: Refactor common code to a single function.
* tests/test-hmac-sha1.c: Likewise.
* tests/test-hmac-sha256.c: Likewise.
* tests/test-hmac-sha512.c: Likewise.
+2018-09-30 Pádraig Brady <P@draigBrady.com>
+
+ hmac-*: refactor to remove repetitive code
+ * lib/hmac.c: A new parameterized single implementation.
+ * lib/hmac-md5.c: Define parameters and include implementation.
+ * lib/hmac-sha1.c: Likewise.
+ * lib/hmac-sha256.c: Likewise.
+ * lib/hmac-sha512.c: Likewise.
+ * modules/crypto/hmac-md5: Reference the new implementation file.
+ * modules/crypto/hmac-sha1: Likewise.
+ * modules/crypto/hmac-sha256: Likewise.
+ * modules/crypto/hmac-sha512: Likewise.
+ * tests/test-hmac-md5.c: Refactor common code to a single function.
+ * tests/test-hmac-sha1.c: Likewise.
+ * tests/test-hmac-sha256.c: Likewise.
+ * tests/test-hmac-sha512.c: Likewise.
+
2018-09-30 Zhang Qing <zhangqingl@126.com>
hmac-sha512: fix hash for keys > blocksize (128 bytes)
/* hmac-md5.c -- hashed message authentication codes
- Copyright (C) 2005-2006, 2009-2018 Free Software Foundation, Inc.
+ Copyright (C) 2018 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
You should have received a copy of the GNU General Public License
along with this program; if not, see <https://www.gnu.org/licenses/>. */
-/* Written by Simon Josefsson. */
-
#include <config.h>
#include "hmac.h"
-#include "memxor.h"
#include "md5.h"
-#include <string.h>
-
-#define IPAD 0x36
-#define OPAD 0x5c
-
-int
-hmac_md5 (const void *key, size_t keylen,
- const void *in, size_t inlen, void *resbuf)
-{
- struct md5_ctx inner;
- struct md5_ctx outer;
- char optkeybuf[16];
- char block[64];
- char innerhash[16];
-
- /* Reduce the key's size, so that it becomes <= 64 bytes large. */
-
- if (keylen > 64)
- {
- struct md5_ctx keyhash;
-
- md5_init_ctx (&keyhash);
- md5_process_bytes (key, keylen, &keyhash);
- md5_finish_ctx (&keyhash, optkeybuf);
-
- key = optkeybuf;
- keylen = 16;
- }
-
- /* Compute INNERHASH from KEY and IN. */
-
- md5_init_ctx (&inner);
-
- memset (block, IPAD, sizeof (block));
- memxor (block, key, keylen);
-
- md5_process_block (block, 64, &inner);
- md5_process_bytes (in, inlen, &inner);
-
- md5_finish_ctx (&inner, innerhash);
-
- /* Compute result from KEY and INNERHASH. */
-
- md5_init_ctx (&outer);
-
- memset (block, OPAD, sizeof (block));
- memxor (block, key, keylen);
-
- md5_process_block (block, 64, &outer);
- md5_process_bytes (innerhash, 16, &outer);
-
- md5_finish_ctx (&outer, resbuf);
-
- return 0;
-}
+#define GL_HMAC_NAME 5
+#define GL_HMAC_BLOCKSIZE 64
+#define GL_HMAC_HASHSIZE 16
+#include "hmac.c"
/* hmac-sha1.c -- hashed message authentication codes
- Copyright (C) 2005-2006, 2009-2018 Free Software Foundation, Inc.
+ Copyright (C) 2018 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
You should have received a copy of the GNU General Public License
along with this program; if not, see <https://www.gnu.org/licenses/>. */
-/* Written by Simon Josefsson. */
-
#include <config.h>
#include "hmac.h"
-#include "memxor.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];
-
- /* Reduce the key's size, so that it becomes <= 64 bytes large. */
-
- 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;
- }
-
- /* Compute INNERHASH from KEY and IN. */
-
- 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);
-
- /* Compute result from KEY and 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;
-}
+#define GL_HMAC_NAME 1
+#define GL_HMAC_BLOCKSIZE 64
+#define GL_HMAC_HASHSIZE 20
+#include "hmac.c"
/* hmac-sha256.c -- hashed message authentication codes
- Copyright (C) 2005-2006, 2009-2018 Free Software Foundation, Inc.
+ Copyright (C) 2018 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
You should have received a copy of the GNU General Public License
along with this program; if not, see <https://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;
-}
+#define GL_HMAC_NAME 256
+#define GL_HMAC_BLOCKSIZE 64
+#define GL_HMAC_HASHSIZE 32
+#include "hmac.c"
/* hmac-sha512.c -- hashed message authentication codes
- Copyright (C) 2005-2006, 2009-2018 Free Software Foundation, Inc.
+ Copyright (C) 2018 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
You should have received a copy of the GNU General Public License
along with this program; if not, see <https://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 = 64;
- }
-
- /* 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;
-}
+#define GL_HMAC_NAME 512
+#define GL_HMAC_BLOCKSIZE 128
+#define GL_HMAC_HASHSIZE 64
+#include "hmac.c"
--- /dev/null
+/* hmac.c -- hashed message authentication codes
+ Copyright (C) 2005-2006, 2009-2018 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 <https://www.gnu.org/licenses/>. */
+
+#include <string.h>
+
+#include "memxor.h"
+
+#define IPAD 0x36
+#define OPAD 0x5c
+
+/* Concatenate two preprocessor tokens. */
+#define _GLHMAC_CONCAT_(prefix, suffix) prefix##suffix
+#define _GLHMAC_CONCAT(prefix, suffix) _GLHMAC_CONCAT_ (prefix, suffix)
+
+#if GL_HMAC_NAME == 5
+# define HMAC_ALG md5
+#else
+# define HMAC_ALG _GLHMAC_CONCAT (sha, GL_HMAC_NAME)
+#endif
+
+#define GL_HMAC_CTX _GLHMAC_CONCAT (HMAC_ALG, _ctx)
+#define GL_HMAC_FN _GLHMAC_CONCAT (hmac_, HMAC_ALG)
+#define GL_HMAC_FN_INIT _GLHMAC_CONCAT (HMAC_ALG, _init_ctx)
+#define GL_HMAC_FN_BLOC _GLHMAC_CONCAT (HMAC_ALG, _process_block)
+#define GL_HMAC_FN_PROC _GLHMAC_CONCAT (HMAC_ALG, _process_bytes)
+#define GL_HMAC_FN_FINI _GLHMAC_CONCAT (HMAC_ALG, _finish_ctx)
+
+static void
+hmac_hash (const void *key, size_t keylen,
+ const void *in, size_t inlen,
+ int pad, void *resbuf)
+{
+ struct GL_HMAC_CTX hmac_ctx;
+ char block[GL_HMAC_BLOCKSIZE];
+
+ memset (block, pad, sizeof block);
+ memxor (block, key, keylen);
+
+ GL_HMAC_FN_INIT (&hmac_ctx);
+ GL_HMAC_FN_BLOC (block, sizeof block, &hmac_ctx);
+ GL_HMAC_FN_PROC (in, inlen, &hmac_ctx);
+ GL_HMAC_FN_FINI (&hmac_ctx, resbuf);
+}
+
+int
+GL_HMAC_FN (const void *key, size_t keylen,
+ const void *in, size_t inlen, void *resbuf)
+{
+ char optkeybuf[GL_HMAC_HASHSIZE];
+ char innerhash[GL_HMAC_HASHSIZE];
+
+ /* Ensure key size is <= block size. */
+ if (keylen > GL_HMAC_BLOCKSIZE)
+ {
+ struct GL_HMAC_CTX keyhash;
+
+ GL_HMAC_FN_INIT (&keyhash);
+ GL_HMAC_FN_PROC (key, keylen, &keyhash);
+ GL_HMAC_FN_FINI (&keyhash, optkeybuf);
+
+ key = optkeybuf;
+ /* zero padding of the key to the block size
+ is implicit in the memxor. */
+ keylen = sizeof optkeybuf;
+ }
+
+ /* Compute INNERHASH from KEY and IN. */
+ hmac_hash (key, keylen, in, inlen, IPAD, innerhash);
+
+ /* Compute result from KEY and INNERHASH. */
+ hmac_hash (key, keylen, innerhash, sizeof innerhash, OPAD, resbuf);
+
+ return 0;
+}
Files:
lib/hmac.h
+lib/hmac.c
lib/hmac-md5.c
Depends-on:
Files:
lib/hmac.h
+lib/hmac.c
lib/hmac-sha1.c
Depends-on:
Files:
lib/hmac.h
+lib/hmac.c
lib/hmac-sha256.c
Depends-on:
Files:
lib/hmac.h
+lib/hmac.c
lib/hmac-sha512.c
Depends-on:
#include "hmac.h"
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
/* Test vectors from RFC 2104. */
+static void
+hmac_check (const void *key, size_t key_len,
+ const void *data, size_t data_len, const char *digest)
+{
+ char out[16];
+
+ if (hmac_md5 (key, key_len, data, data_len, out) != 0)
+ {
+ printf ("call failure\n");
+ exit (1);
+ }
+
+ if (memcmp (digest, out, 16) != 0)
+ {
+ size_t i;
+ printf ("hash 1 mismatch. expected:\n");
+ for (i = 0; i < 16; i++)
+ printf ("%02x ", digest[i] & 0xFF);
+ printf ("\ncomputed:\n");
+ for (i = 0; i < 16; i++)
+ printf ("%02x ", out[i] & 0xFF);
+ printf ("\n");
+ exit (1);
+ }
+}
+
int
main (int argc, char *argv[])
{
{
- /*
- key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
- key_len = 16 bytes
- data = "Hi There"
- data_len = 8 bytes
- digest = 0x9294727a3638bb1c13f48ef8158bfc9d
- */
- char *key =
- "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
- size_t key_len = 16;
+ char key[16];
+ size_t key_len = sizeof key;
+ memset (key, '\x0b', sizeof key);
char *data = "Hi There";
size_t data_len = 8;
char *digest =
"\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc\x9d";
- char out[16];
-
- if (hmac_md5 (key, key_len, data, data_len, out) != 0)
- {
- printf ("call failure\n");
- return 1;
- }
-
- if (memcmp (digest, out, 16) != 0)
- {
- size_t i;
- printf ("hash 1 mismatch. expected:\n");
- for (i = 0; i < 16; i++)
- printf ("%02x ", digest[i] & 0xFF);
- printf ("\ncomputed:\n");
- for (i = 0; i < 16; i++)
- printf ("%02x ", out[i] & 0xFF);
- printf ("\n");
- return 1;
- }
+ hmac_check (key, key_len, data, data_len, digest);
}
{
- /*
- key = "Jefe"
- data = "what do ya want for nothing?"
- data_len = 28 bytes
- digest = 0x750c783e6ab0b503eaa86e310a5db738
- */
char *key = "Jefe";
size_t key_len = 4;
char *data = "what do ya want for nothing?";
size_t data_len = 28;
char *digest =
"\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38";
- char out[16];
-
- if (hmac_md5 (key, key_len, data, data_len, out) != 0)
- {
- printf ("call failure\n");
- return 1;
- }
-
- if (memcmp (digest, out, 16) != 0)
- {
- size_t i;
- printf ("hash 2 mismatch. expected:\n");
- for (i = 0; i < 16; i++)
- printf ("%02x ", digest[i] & 0xFF);
- printf ("\ncomputed:\n");
- for (i = 0; i < 16; i++)
- printf ("%02x ", out[i] & 0xFF);
- printf ("\n");
- return 1;
- }
+ hmac_check (key, key_len, data, data_len, digest);
}
{
- /*
- key = 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
- key_len 16 bytes
- data = 0xDDDDDDDDDDDDDDDDDDDD...
- ..DDDDDDDDDDDDDDDDDDDD...
- ..DDDDDDDDDDDDDDDDDDDD...
- ..DDDDDDDDDDDDDDDDDDDD...
- ..DDDDDDDDDDDDDDDDDDDD
- data_len = 50 bytes
- digest = 0x56be34521d144c88dbb8c733f0e8b3f6
- */
- 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 key[16];
+ size_t key_len = sizeof key;
+ memset (key, '\xAA', sizeof key);
+ char data[50];
+ size_t data_len = sizeof data;
+ memset (data, '\xDD', sizeof data);
char *digest =
"\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3\xf6";
- char out[16];
-
- if (hmac_md5 (key, key_len, data, data_len, out) != 0)
- {
- printf ("call failure\n");
- return 1;
- }
+ hmac_check (key, key_len, data, data_len, digest);
+ }
- if (memcmp (digest, out, 16) != 0)
- {
- size_t i;
- printf ("hash 3 mismatch. expected:\n");
- for (i = 0; i < 16; i++)
- printf ("%02x ", digest[i] & 0xFF);
- printf ("\ncomputed:\n");
- for (i = 0; i < 16; i++)
- printf ("%02x ", out[i] & 0xFF);
- printf ("\n");
- return 1;
- }
+ {
+ char key[65];
+ size_t key_len = sizeof key;
+ memset (key, '\x0b', sizeof key);
+ char *data = "Hi There";
+ size_t data_len = 8;
+ char *digest =
+ "\xd6\x07\x5b\xee\x4d\x91\x80\xd8\xd1\xa2\x99\x29\x5e\x7c\xc9\xcb";
+ hmac_check (key, key_len, data, data_len, digest);
}
return 0;
#include "hmac.h"
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
+static void
+hmac_check (const void *key, size_t key_len,
+ const void *data, size_t data_len, const char *digest)
+{
+ char out[20];
+
+ if (hmac_sha1 (key, key_len, data, data_len, out) != 0)
+ {
+ printf ("call failure\n");
+ exit (1);
+ }
+
+ if (memcmp (digest, out, 20) != 0)
+ {
+ size_t i;
+ printf ("hash 1 mismatch. 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");
+ exit (1);
+ }
+}
+
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 key[16];
+ size_t key_len = sizeof key;
+ memset (key, '\x0b', sizeof key);
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 mismatch. 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;
- }
+ "\x67\x5b\x0b\x3a\x1b\x4d\xdf\x4e\x12\x48\x72\xda\x6c\x2f\x63\x2b"
+ "\xfe\xd9\x57\xe9";
+ hmac_check (key, key_len, data, data_len, digest);
}
{
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 mismatch. 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;
- }
+ "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c"
+ "\x25\x9a\x7c\x79";
+ hmac_check (key, key_len, data, data_len, digest);
}
{
- 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 key[20];
+ size_t key_len = sizeof key;
+ memset (key, '\xAA', sizeof key);
+ char data[50];
+ size_t data_len = sizeof data;
+ memset (data, '\xDD', sizeof data);
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 mismatch. 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;
- }
+ "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f"
+ "\x63\xf1\x75\xd3";
+ hmac_check (key, key_len, data, data_len, digest);
}
+ {
+ char key[65];
+ size_t key_len = sizeof key;
+ memset (key, '\x0b', sizeof key);
+ char *data = "Hi There";
+ size_t data_len = 8;
+ char *digest =
+ "\x29\xda\xa9\xe9\xcc\x4b\x9f\x09\x48\x29\xdc\xd4\x03\xc0\x69\x27"
+ "\xd8\xa9\x53\x93";
+ hmac_check (key, key_len, data, data_len, digest);
+ }
return 0;
}
#include "hmac.h"
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
+static void
+hmac_check (const void *key, size_t key_len,
+ const void *data, size_t data_len, const char *digest)
+{
+ char out[32];
+
+ if (hmac_sha256 (key, key_len, data, data_len, out) != 0)
+ {
+ printf ("call failure\n");
+ exit (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");
+ exit (1);
+ }
+}
+
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 key[20];
+ size_t key_len = sizeof key;
+ memset (key, '\x0b', sizeof key);
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;
- }
+ "\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";
+ hmac_check (key, key_len, data, data_len, digest);
}
{
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;
- }
+ "\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";
+ hmac_check (key, key_len, data, data_len, digest);
}
{
- 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 key[20];
+ size_t key_len = sizeof key;
+ memset (key, '\xAA', sizeof key);
+ char data[50];
+ size_t data_len = sizeof data;
+ memset (data, '\xDD', sizeof data);
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;
- }
+ "\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";
+ hmac_check (key, key_len, data, data_len, digest);
+ }
- 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;
- }
+ {
+ char key[65];
+ size_t key_len = sizeof key;
+ memset (key, '\x0b', sizeof key);
+ char *data = "Hi There";
+ size_t data_len = 8;
+ char *digest =
+ "\x72\x7b\x82\xfb\xa2\x64\x39\x3c\x5d\x67\xfd\x6d\x6a\xd7\x83\xe9"
+ "\x01\x9a\x1f\xa6\xa8\x57\xfc\xcb\x70\xf5\x85\x2f\x04\xbe\x5d\x5d";
+ hmac_check (key, key_len, data, data_len, digest);
}
return 0;
#include "hmac.h"
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
+static void
+hmac_check (const void *key, size_t key_len,
+ const void *data, size_t data_len, const char *digest)
+{
+ char out[64];
+
+ if (hmac_sha512 (key, key_len, data, data_len, out) != 0)
+ {
+ printf ("call failure\n");
+ exit (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");
+ exit (1);
+ }
+}
+
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 key[20];
+ size_t key_len = sizeof key;
+ memset (key, '\x0b', sizeof key);
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;
- }
+ "\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";
+ hmac_check (key, key_len, data, data_len, digest);
}
{
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;
- }
+ "\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";
+ hmac_check (key, key_len, data, data_len, digest);
}
{
- 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 key[20];
+ size_t key_len = sizeof key;
+ memset (key, '\xAA', sizeof key);
+ char data[50];
+ size_t data_len = sizeof data;
+ memset (data, '\xDD', sizeof data);
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;
- }
+ "\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";
+ hmac_check (key, key_len, data, data_len, digest);
}
{
char *data = "Hi There";
size_t data_len = 8;
char *digest =
- "\xaa\x1c\x23\xfe\x04\x0c\x4f\x3e\x65\x45\xa9\x15\x4e\x33\x9d\x17\xff\xb5\x27\x2e\x0a\x54\x5b\x84\xd3\x8b\x9b\xf8\xe2\xc7\x46\x4d\xf2\xd6\x2b\xb5\x00\x05\x57\x68\x6f\x85\x10\xeb\x43\x02\xa0\xca\xe6\xb5\xdd\x1f\x37\x00\xbe\xae\xde\x75\x5f\x86\xfd\xbe\xb4\x8f";
- 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;
- }
+ "\xaa\x1c\x23\xfe\x04\x0c\x4f\x3e\x65\x45\xa9\x15\x4e\x33\x9d\x17"
+ "\xff\xb5\x27\x2e\x0a\x54\x5b\x84\xd3\x8b\x9b\xf8\xe2\xc7\x46\x4d"
+ "\xf2\xd6\x2b\xb5\x00\x05\x57\x68\x6f\x85\x10\xeb\x43\x02\xa0\xca"
+ "\xe6\xb5\xdd\x1f\x37\x00\xbe\xae\xde\x75\x5f\x86\xfd\xbe\xb4\x8f";
+ hmac_check (key, key_len, data, data_len, digest);
}
return 0;