diff --git a/qse/include/qse/cry/Makefile.am b/qse/include/qse/cry/Makefile.am index d86584c2..0109204d 100644 --- a/qse/include/qse/cry/Makefile.am +++ b/qse/include/qse/cry/Makefile.am @@ -2,6 +2,7 @@ pkgincludedir = $(includedir)/qse/cry pkginclude_HEADERS = \ blowfish.h \ + hmac.h \ kseed.h \ md5.h \ sha1.h \ diff --git a/qse/include/qse/cry/Makefile.in b/qse/include/qse/cry/Makefile.in index 490d2c21..36f29e2e 100644 --- a/qse/include/qse/cry/Makefile.in +++ b/qse/include/qse/cry/Makefile.in @@ -341,6 +341,7 @@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ pkginclude_HEADERS = \ blowfish.h \ + hmac.h \ kseed.h \ md5.h \ sha1.h \ diff --git a/qse/include/qse/cry/md5.h b/qse/include/qse/cry/md5.h index a80e90c2..5c1f86ba 100644 --- a/qse/include/qse/cry/md5.h +++ b/qse/include/qse/cry/md5.h @@ -30,13 +30,14 @@ #include #include -#define QSE_MD5_DIGEST_LEN 16 +#define QSE_MD5_DIGEST_LEN (16) +#define QSE_MD5_BLOCK_LEN (64) struct qse_md5_t { qse_uint32_t count[2]; qse_uint32_t state[4]; - qse_uint8_t buffer[64]; + qse_uint8_t buffer[QSE_MD5_BLOCK_LEN]; }; typedef struct qse_md5_t qse_md5_t; diff --git a/qse/include/qse/cry/sha1.h b/qse/include/qse/cry/sha1.h index aeea58b9..66048102 100644 --- a/qse/include/qse/cry/sha1.h +++ b/qse/include/qse/cry/sha1.h @@ -28,16 +28,17 @@ #ifndef _QSE_CRY_SHA1_H_ #define _QSE_CRY_SHA1_H_ -#define QSE_SHA1_DIGEST_LEN 20 - #include #include +#define QSE_SHA1_DIGEST_LEN (20) +#define QSE_SHA1_BLOCK_LEN (64) + struct qse_sha1_t { qse_uint32_t state[5]; qse_uint32_t count[2]; - qse_uint8_t buffer[64]; + qse_uint8_t buffer[QSE_SHA1_BLOCK_LEN]; }; typedef struct qse_sha1_t qse_sha1_t; diff --git a/qse/include/qse/cry/sha2.h b/qse/include/qse/cry/sha2.h index aaa99097..a3ca73a8 100644 --- a/qse/include/qse/cry/sha2.h +++ b/qse/include/qse/cry/sha2.h @@ -66,22 +66,18 @@ #include #include -/*** SHA-256/384/512 Various Length Definitions ***********************/ -#define QSE_SHA256_BLOCK_LEN (64) -#define QSE_SHA256_DIGEST_LEN (32) -#define QSE_SHA384_BLOCK_LEN (128) -#define QSE_SHA384_DIGEST_LEN (48) -#define QSE_SHA512_BLOCK_LEN (128) -#define QSE_SHA512_DIGEST_LEN (64) - - -/*** SHA-256/384/512 Context Structures *******************************/ +#define QSE_SHA256_BLOCK_LEN (64) +#define QSE_SHA256_DIGEST_LEN (32) +#define QSE_SHA384_BLOCK_LEN (128) +#define QSE_SHA384_DIGEST_LEN (48) +#define QSE_SHA512_BLOCK_LEN (128) +#define QSE_SHA512_DIGEST_LEN (64) struct qse_sha256_t { - qse_uint32_t state[8]; + qse_uint32_t state[8]; qse_uint64_t bitcount; - qse_uint8_t buffer[QSE_SHA256_BLOCK_LEN]; + qse_uint8_t buffer[QSE_SHA256_BLOCK_LEN]; }; typedef struct qse_sha256_t qse_sha256_t; diff --git a/qse/lib/cry/Makefile.am b/qse/lib/cry/Makefile.am index 3d4f345e..425f2745 100644 --- a/qse/lib/cry/Makefile.am +++ b/qse/lib/cry/Makefile.am @@ -9,6 +9,7 @@ AM_CPPFLAGS = \ lib_LTLIBRARIES = libqsecry.la libqsecry_la_SOURCES = \ blowfish.c \ + hmac.c \ kseed.c \ md5.c \ sha1.c \ diff --git a/qse/lib/cry/Makefile.in b/qse/lib/cry/Makefile.in index 5cb9d56f..f4c3d088 100644 --- a/qse/lib/cry/Makefile.in +++ b/qse/lib/cry/Makefile.in @@ -134,8 +134,8 @@ am__uninstall_files_from_dir = { \ am__installdirs = "$(DESTDIR)$(libdir)" LTLIBRARIES = $(lib_LTLIBRARIES) am_libqsecry_la_OBJECTS = libqsecry_la-blowfish.lo \ - libqsecry_la-kseed.lo libqsecry_la-md5.lo libqsecry_la-sha1.lo \ - libqsecry_la-sha2.lo + libqsecry_la-hmac.lo libqsecry_la-kseed.lo libqsecry_la-md5.lo \ + libqsecry_la-sha1.lo libqsecry_la-sha2.lo libqsecry_la_OBJECTS = $(am_libqsecry_la_OBJECTS) AM_V_lt = $(am__v_lt_@AM_V@) am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) @@ -160,6 +160,7 @@ DEFAULT_INCLUDES = depcomp = $(SHELL) $(top_srcdir)/ac/depcomp am__maybe_remake_depfiles = depfiles am__depfiles_remade = ./$(DEPDIR)/libqsecry_la-blowfish.Plo \ + ./$(DEPDIR)/libqsecry_la-hmac.Plo \ ./$(DEPDIR)/libqsecry_la-kseed.Plo \ ./$(DEPDIR)/libqsecry_la-md5.Plo \ ./$(DEPDIR)/libqsecry_la-sha1.Plo \ @@ -386,6 +387,7 @@ AM_CPPFLAGS = \ lib_LTLIBRARIES = libqsecry.la libqsecry_la_SOURCES = \ blowfish.c \ + hmac.c \ kseed.c \ md5.c \ sha1.c \ @@ -474,6 +476,7 @@ distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libqsecry_la-blowfish.Plo@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libqsecry_la-hmac.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libqsecry_la-kseed.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libqsecry_la-md5.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libqsecry_la-sha1.Plo@am__quote@ # am--include-marker @@ -513,6 +516,13 @@ libqsecry_la-blowfish.lo: blowfish.c @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libqsecry_la_CFLAGS) $(CFLAGS) -c -o libqsecry_la-blowfish.lo `test -f 'blowfish.c' || echo '$(srcdir)/'`blowfish.c +libqsecry_la-hmac.lo: hmac.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libqsecry_la_CFLAGS) $(CFLAGS) -MT libqsecry_la-hmac.lo -MD -MP -MF $(DEPDIR)/libqsecry_la-hmac.Tpo -c -o libqsecry_la-hmac.lo `test -f 'hmac.c' || echo '$(srcdir)/'`hmac.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libqsecry_la-hmac.Tpo $(DEPDIR)/libqsecry_la-hmac.Plo +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='hmac.c' object='libqsecry_la-hmac.lo' libtool=yes @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libqsecry_la_CFLAGS) $(CFLAGS) -c -o libqsecry_la-hmac.lo `test -f 'hmac.c' || echo '$(srcdir)/'`hmac.c + libqsecry_la-kseed.lo: kseed.c @am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libqsecry_la_CFLAGS) $(CFLAGS) -MT libqsecry_la-kseed.lo -MD -MP -MF $(DEPDIR)/libqsecry_la-kseed.Tpo -c -o libqsecry_la-kseed.lo `test -f 'kseed.c' || echo '$(srcdir)/'`kseed.c @am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libqsecry_la-kseed.Tpo $(DEPDIR)/libqsecry_la-kseed.Plo @@ -676,6 +686,7 @@ clean-am: clean-generic clean-libLTLIBRARIES clean-libtool \ distclean: distclean-am -rm -f ./$(DEPDIR)/libqsecry_la-blowfish.Plo + -rm -f ./$(DEPDIR)/libqsecry_la-hmac.Plo -rm -f ./$(DEPDIR)/libqsecry_la-kseed.Plo -rm -f ./$(DEPDIR)/libqsecry_la-md5.Plo -rm -f ./$(DEPDIR)/libqsecry_la-sha1.Plo @@ -726,6 +737,7 @@ installcheck-am: maintainer-clean: maintainer-clean-am -rm -f ./$(DEPDIR)/libqsecry_la-blowfish.Plo + -rm -f ./$(DEPDIR)/libqsecry_la-hmac.Plo -rm -f ./$(DEPDIR)/libqsecry_la-kseed.Plo -rm -f ./$(DEPDIR)/libqsecry_la-md5.Plo -rm -f ./$(DEPDIR)/libqsecry_la-sha1.Plo diff --git a/qse/samples/cry/bf01.c b/qse/samples/cry/bf01.c index 5f9bf2e9..78311019 100644 --- a/qse/samples/cry/bf01.c +++ b/qse/samples/cry/bf01.c @@ -1,3 +1,29 @@ +/* + * $Id$ + * + Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + #include #include #include diff --git a/qse/samples/cry/sha01.c b/qse/samples/cry/sha01.c index 8e2d591c..7541b248 100644 --- a/qse/samples/cry/sha01.c +++ b/qse/samples/cry/sha01.c @@ -1,6 +1,33 @@ +/* + * $Id$ + * + Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + #include #include #include +#include #include #include #include @@ -11,10 +38,10 @@ #define READ_BUF_SIZE (32768) -static void sha512sum (int fd, unsigned char digest[QSE_SHA512_DIGEST_LEN]) +static qse_size_t sha512sum (int fd, qse_uint8_t* digest, qse_size_t size) { qse_sha512_t md; - unsigned char buf[READ_BUF_SIZE]; + qse_uint8_t buf[READ_BUF_SIZE]; ssize_t n; qse_sha512_initialize (&md); @@ -24,13 +51,13 @@ static void sha512sum (int fd, unsigned char digest[QSE_SHA512_DIGEST_LEN]) if (n <= 0) break; qse_sha512_update (&md, buf, n); } - qse_sha512_digest (&md, digest, QSE_SHA512_DIGEST_LEN); + return qse_sha512_digest(&md, digest, size); } -static void sha384sum (int fd, unsigned char digest[QSE_SHA384_DIGEST_LEN]) +static qse_size_t sha384sum (int fd, qse_uint8_t* digest, qse_size_t size) { qse_sha384_t md; - unsigned char buf[READ_BUF_SIZE]; + qse_uint8_t buf[READ_BUF_SIZE]; ssize_t n; qse_sha384_initialize (&md); @@ -40,13 +67,13 @@ static void sha384sum (int fd, unsigned char digest[QSE_SHA384_DIGEST_LEN]) if (n <= 0) break; qse_sha384_update (&md, buf, n); } - qse_sha384_digest (&md, digest, QSE_SHA384_DIGEST_LEN); + return qse_sha384_digest(&md, digest, size); } -static void sha256sum (int fd, unsigned char digest[QSE_SHA256_DIGEST_LEN]) +static qse_size_t sha256sum (int fd, qse_uint8_t* digest, qse_size_t size) { qse_sha256_t md; - unsigned char buf[READ_BUF_SIZE]; + qse_uint8_t buf[READ_BUF_SIZE]; ssize_t n; qse_sha256_initialize (&md); @@ -56,13 +83,13 @@ static void sha256sum (int fd, unsigned char digest[QSE_SHA256_DIGEST_LEN]) if (n <= 0) break; qse_sha256_update (&md, buf, n); } - qse_sha256_digest (&md, digest, QSE_SHA256_DIGEST_LEN); + return qse_sha256_digest(&md, digest, size); } -static void sha1sum (int fd, unsigned char digest[QSE_SHA1_DIGEST_LEN]) +static qse_size_t sha1sum (int fd, qse_uint8_t* digest, qse_size_t size) { qse_sha1_t md; - unsigned char buf[READ_BUF_SIZE]; + qse_uint8_t buf[READ_BUF_SIZE]; ssize_t n; qse_sha1_initialize (&md); @@ -72,13 +99,13 @@ static void sha1sum (int fd, unsigned char digest[QSE_SHA1_DIGEST_LEN]) if (n <= 0) break; qse_sha1_update (&md, buf, n); } - qse_sha1_digest (&md, digest, QSE_SHA1_DIGEST_LEN); + return qse_sha1_digest(&md, digest, size); } -static void md5sum (int fd, unsigned char digest[QSE_MD5_DIGEST_LEN]) +static qse_size_t md5sum (int fd, qse_uint8_t* digest, qse_size_t size) { qse_md5_t md; - unsigned char buf[READ_BUF_SIZE]; + qse_uint8_t buf[READ_BUF_SIZE]; ssize_t n; qse_md5_initialize (&md); @@ -88,7 +115,23 @@ static void md5sum (int fd, unsigned char digest[QSE_MD5_DIGEST_LEN]) if (n <= 0) break; qse_md5_update (&md, buf, n); } - qse_md5_digest (&md, digest, QSE_MD5_DIGEST_LEN); + return qse_md5_digest(&md, digest, size); +} + +static qse_size_t hmac (int fd, qse_hmac_sha_type_t sha_type, const qse_uint8_t* key, qse_size_t keylen, qse_uint8_t* digest, qse_size_t size) +{ + qse_hmac_t md; + qse_uint8_t buf[READ_BUF_SIZE]; + ssize_t n; + + qse_hmac_initialize (&md, sha_type, key, keylen); + while (1) + { + n = read(fd, buf, sizeof(buf)); + if (n <= 0) break; + qse_hmac_update (&md, buf, n); + } + return qse_hmac_digest(&md, digest, size); } @@ -101,20 +144,23 @@ static void print_usage (const char* argv0) fprintf (stderr, " -2 sha256\n"); fprintf (stderr, " -3 sha384\n"); fprintf (stderr, " -5 sha512\n"); + fprintf (stderr, " -k specify hmac key and enable hmac\n"); } int main (int argc, char* argv[]) { - unsigned char digest[QSE_SHA512_DIGEST_LEN]; - int digest_len = QSE_SHA512_DIGEST_LEN; - void (*sha_func) (int fd, unsigned char* digest) = sha512sum; + qse_uint8_t digest[QSE_HMAC_MAX_DIGEST_LEN]; + qse_size_t digest_len; + qse_size_t (*sha_func) (int fd, qse_uint8_t* digest, qse_size_t len) = sha512sum; + const char* hmac_key = NULL; + qse_hmac_sha_type_t hmac_sha_type = QSE_HMAC_SHA512; int i, j; while (1) { int c; - c = getopt(argc, argv, ":hm1235"); + c = getopt(argc, argv, ":hk:m1235"); if (c == -1) break; switch (c) @@ -123,28 +169,32 @@ int main (int argc, char* argv[]) print_usage (qse_mbsbasename(argv[0])); return 0; + case 'k': + hmac_key = optarg; + break; + case 'm': - digest_len = QSE_MD5_DIGEST_LEN; + hmac_sha_type = QSE_HMAC_MD5; sha_func = md5sum; break; case '1': - digest_len = QSE_SHA1_DIGEST_LEN; + hmac_sha_type = QSE_HMAC_SHA1; sha_func = sha1sum; break; case '2': - digest_len = QSE_SHA256_DIGEST_LEN; + hmac_sha_type = QSE_HMAC_SHA256; sha_func = sha256sum; break; case '3': - digest_len = QSE_SHA384_DIGEST_LEN; + hmac_sha_type = QSE_HMAC_SHA384; sha_func = sha384sum; break; case '5': - digest_len = QSE_SHA512_DIGEST_LEN; + hmac_sha_type = QSE_HMAC_SHA512; sha_func = sha512sum; break; @@ -157,35 +207,69 @@ int main (int argc, char* argv[]) - if (optind >= argc) + if (hmac_key) { - sha_func (STDIN_FILENO, digest); - for (j = 0; j < digest_len; j++) - printf ("%02x", digest[j]); - printf (" -\n"); - } - else - { - int fd; - - for (i = optind; i < argc; i++) + if (optind >= argc) { - fd = strcmp(argv[i], "-")? open(argv[i], O_RDONLY): STDIN_FILENO; - if (fd >= 0) + QSE_STATIC_ASSERT (QSE_SIZEOF(digest) >= QSE_HMAC_MAX_DIGEST_LEN); + digest_len = hmac(STDIN_FILENO, hmac_sha_type, (const qse_uint8_t*)hmac_key, strlen(hmac_key), digest, QSE_SIZEOF(digest)); + for (j = 0; j < digest_len; j++) + printf ("%02x", digest[j]); + printf (" -\n"); + } + else + { + int fd; + + for (i = optind; i < argc; i++) { - sha_func (fd, digest); - for (j = 0; j < digest_len; j++) - printf ("%02x", digest[j]); - printf (" %s\n", argv[i]); - if (strcmp(argv[i], "-")) close (fd); - } - else - { - fprintf (stderr, "%s: %s\n", argv[i], strerror(errno)); + fd = strcmp(argv[i], "-")? open(argv[i], O_RDONLY): STDIN_FILENO; + if (fd >= 0) + { + digest_len = hmac(fd, hmac_sha_type, (const qse_uint8_t*)hmac_key, strlen(hmac_key), digest, QSE_SIZEOF(digest)); + for (j = 0; j < digest_len; j++) + printf ("%02x", digest[j]); + printf (" %s\n", argv[i]); + if (strcmp(argv[i], "-")) close (fd); + } + else + { + fprintf (stderr, "%s: %s\n", argv[i], strerror(errno)); + } } } } + else + { + if (optind >= argc) + { + digest_len = sha_func(STDIN_FILENO, digest, QSE_SIZEOF(digest)); + for (j = 0; j < digest_len; j++) + printf ("%02x", digest[j]); + printf (" -\n"); + } + else + { + int fd; + for (i = optind; i < argc; i++) + { + fd = strcmp(argv[i], "-")? open(argv[i], O_RDONLY): STDIN_FILENO; + if (fd >= 0) + { + digest_len = sha_func(fd, digest, QSE_SIZEOF(digest)); + for (j = 0; j < digest_len; j++) + printf ("%02x", digest[j]); + printf (" %s\n", argv[i]); + if (strcmp(argv[i], "-")) close (fd); + } + else + { + fprintf (stderr, "%s: %s\n", argv[i], strerror(errno)); + } + } + } + } return 0; }