added hmac functions

This commit is contained in:
hyung-hwan 2019-08-15 08:25:17 +00:00
parent 4d53acaa94
commit 06712ddd4a
9 changed files with 188 additions and 65 deletions

View File

@ -2,6 +2,7 @@ pkgincludedir = $(includedir)/qse/cry
pkginclude_HEADERS = \ pkginclude_HEADERS = \
blowfish.h \ blowfish.h \
hmac.h \
kseed.h \ kseed.h \
md5.h \ md5.h \
sha1.h \ sha1.h \

View File

@ -341,6 +341,7 @@ top_builddir = @top_builddir@
top_srcdir = @top_srcdir@ top_srcdir = @top_srcdir@
pkginclude_HEADERS = \ pkginclude_HEADERS = \
blowfish.h \ blowfish.h \
hmac.h \
kseed.h \ kseed.h \
md5.h \ md5.h \
sha1.h \ sha1.h \

View File

@ -30,13 +30,14 @@
#include <qse/types.h> #include <qse/types.h>
#include <qse/macros.h> #include <qse/macros.h>
#define QSE_MD5_DIGEST_LEN 16 #define QSE_MD5_DIGEST_LEN (16)
#define QSE_MD5_BLOCK_LEN (64)
struct qse_md5_t struct qse_md5_t
{ {
qse_uint32_t count[2]; qse_uint32_t count[2];
qse_uint32_t state[4]; 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; typedef struct qse_md5_t qse_md5_t;

View File

@ -28,16 +28,17 @@
#ifndef _QSE_CRY_SHA1_H_ #ifndef _QSE_CRY_SHA1_H_
#define _QSE_CRY_SHA1_H_ #define _QSE_CRY_SHA1_H_
#define QSE_SHA1_DIGEST_LEN 20
#include <qse/types.h> #include <qse/types.h>
#include <qse/macros.h> #include <qse/macros.h>
#define QSE_SHA1_DIGEST_LEN (20)
#define QSE_SHA1_BLOCK_LEN (64)
struct qse_sha1_t struct qse_sha1_t
{ {
qse_uint32_t state[5]; qse_uint32_t state[5];
qse_uint32_t count[2]; 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; typedef struct qse_sha1_t qse_sha1_t;

View File

@ -66,7 +66,6 @@
#include <qse/types.h> #include <qse/types.h>
#include <qse/macros.h> #include <qse/macros.h>
/*** SHA-256/384/512 Various Length Definitions ***********************/
#define QSE_SHA256_BLOCK_LEN (64) #define QSE_SHA256_BLOCK_LEN (64)
#define QSE_SHA256_DIGEST_LEN (32) #define QSE_SHA256_DIGEST_LEN (32)
#define QSE_SHA384_BLOCK_LEN (128) #define QSE_SHA384_BLOCK_LEN (128)
@ -74,9 +73,6 @@
#define QSE_SHA512_BLOCK_LEN (128) #define QSE_SHA512_BLOCK_LEN (128)
#define QSE_SHA512_DIGEST_LEN (64) #define QSE_SHA512_DIGEST_LEN (64)
/*** SHA-256/384/512 Context Structures *******************************/
struct qse_sha256_t struct qse_sha256_t
{ {
qse_uint32_t state[8]; qse_uint32_t state[8];

View File

@ -9,6 +9,7 @@ AM_CPPFLAGS = \
lib_LTLIBRARIES = libqsecry.la lib_LTLIBRARIES = libqsecry.la
libqsecry_la_SOURCES = \ libqsecry_la_SOURCES = \
blowfish.c \ blowfish.c \
hmac.c \
kseed.c \ kseed.c \
md5.c \ md5.c \
sha1.c \ sha1.c \

View File

@ -134,8 +134,8 @@ am__uninstall_files_from_dir = { \
am__installdirs = "$(DESTDIR)$(libdir)" am__installdirs = "$(DESTDIR)$(libdir)"
LTLIBRARIES = $(lib_LTLIBRARIES) LTLIBRARIES = $(lib_LTLIBRARIES)
am_libqsecry_la_OBJECTS = libqsecry_la-blowfish.lo \ am_libqsecry_la_OBJECTS = libqsecry_la-blowfish.lo \
libqsecry_la-kseed.lo libqsecry_la-md5.lo libqsecry_la-sha1.lo \ libqsecry_la-hmac.lo libqsecry_la-kseed.lo libqsecry_la-md5.lo \
libqsecry_la-sha2.lo libqsecry_la-sha1.lo libqsecry_la-sha2.lo
libqsecry_la_OBJECTS = $(am_libqsecry_la_OBJECTS) libqsecry_la_OBJECTS = $(am_libqsecry_la_OBJECTS)
AM_V_lt = $(am__v_lt_@AM_V@) AM_V_lt = $(am__v_lt_@AM_V@)
am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@)
@ -160,6 +160,7 @@ DEFAULT_INCLUDES =
depcomp = $(SHELL) $(top_srcdir)/ac/depcomp depcomp = $(SHELL) $(top_srcdir)/ac/depcomp
am__maybe_remake_depfiles = depfiles am__maybe_remake_depfiles = depfiles
am__depfiles_remade = ./$(DEPDIR)/libqsecry_la-blowfish.Plo \ am__depfiles_remade = ./$(DEPDIR)/libqsecry_la-blowfish.Plo \
./$(DEPDIR)/libqsecry_la-hmac.Plo \
./$(DEPDIR)/libqsecry_la-kseed.Plo \ ./$(DEPDIR)/libqsecry_la-kseed.Plo \
./$(DEPDIR)/libqsecry_la-md5.Plo \ ./$(DEPDIR)/libqsecry_la-md5.Plo \
./$(DEPDIR)/libqsecry_la-sha1.Plo \ ./$(DEPDIR)/libqsecry_la-sha1.Plo \
@ -386,6 +387,7 @@ AM_CPPFLAGS = \
lib_LTLIBRARIES = libqsecry.la lib_LTLIBRARIES = libqsecry.la
libqsecry_la_SOURCES = \ libqsecry_la_SOURCES = \
blowfish.c \ blowfish.c \
hmac.c \
kseed.c \ kseed.c \
md5.c \ md5.c \
sha1.c \ sha1.c \
@ -474,6 +476,7 @@ distclean-compile:
-rm -f *.tab.c -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-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-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-md5.Plo@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libqsecry_la-sha1.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@ @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 @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 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_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 @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 distclean: distclean-am
-rm -f ./$(DEPDIR)/libqsecry_la-blowfish.Plo -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-kseed.Plo
-rm -f ./$(DEPDIR)/libqsecry_la-md5.Plo -rm -f ./$(DEPDIR)/libqsecry_la-md5.Plo
-rm -f ./$(DEPDIR)/libqsecry_la-sha1.Plo -rm -f ./$(DEPDIR)/libqsecry_la-sha1.Plo
@ -726,6 +737,7 @@ installcheck-am:
maintainer-clean: maintainer-clean-am maintainer-clean: maintainer-clean-am
-rm -f ./$(DEPDIR)/libqsecry_la-blowfish.Plo -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-kseed.Plo
-rm -f ./$(DEPDIR)/libqsecry_la-md5.Plo -rm -f ./$(DEPDIR)/libqsecry_la-md5.Plo
-rm -f ./$(DEPDIR)/libqsecry_la-sha1.Plo -rm -f ./$(DEPDIR)/libqsecry_la-sha1.Plo

View File

@ -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 <qse/si/sio.h> #include <qse/si/sio.h>
#include <qse/cry/blowfish.h> #include <qse/cry/blowfish.h>
#include <qse/cry/kseed.h> #include <qse/cry/kseed.h>

View File

@ -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 <qse/cry/sha2.h> #include <qse/cry/sha2.h>
#include <qse/cry/sha1.h> #include <qse/cry/sha1.h>
#include <qse/cry/md5.h> #include <qse/cry/md5.h>
#include <qse/cry/hmac.h>
#include <qse/cmn/path.h> #include <qse/cmn/path.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
@ -11,10 +38,10 @@
#define READ_BUF_SIZE (32768) #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; qse_sha512_t md;
unsigned char buf[READ_BUF_SIZE]; qse_uint8_t buf[READ_BUF_SIZE];
ssize_t n; ssize_t n;
qse_sha512_initialize (&md); qse_sha512_initialize (&md);
@ -24,13 +51,13 @@ static void sha512sum (int fd, unsigned char digest[QSE_SHA512_DIGEST_LEN])
if (n <= 0) break; if (n <= 0) break;
qse_sha512_update (&md, buf, n); 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; qse_sha384_t md;
unsigned char buf[READ_BUF_SIZE]; qse_uint8_t buf[READ_BUF_SIZE];
ssize_t n; ssize_t n;
qse_sha384_initialize (&md); qse_sha384_initialize (&md);
@ -40,13 +67,13 @@ static void sha384sum (int fd, unsigned char digest[QSE_SHA384_DIGEST_LEN])
if (n <= 0) break; if (n <= 0) break;
qse_sha384_update (&md, buf, n); 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; qse_sha256_t md;
unsigned char buf[READ_BUF_SIZE]; qse_uint8_t buf[READ_BUF_SIZE];
ssize_t n; ssize_t n;
qse_sha256_initialize (&md); qse_sha256_initialize (&md);
@ -56,13 +83,13 @@ static void sha256sum (int fd, unsigned char digest[QSE_SHA256_DIGEST_LEN])
if (n <= 0) break; if (n <= 0) break;
qse_sha256_update (&md, buf, n); 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; qse_sha1_t md;
unsigned char buf[READ_BUF_SIZE]; qse_uint8_t buf[READ_BUF_SIZE];
ssize_t n; ssize_t n;
qse_sha1_initialize (&md); qse_sha1_initialize (&md);
@ -72,13 +99,13 @@ static void sha1sum (int fd, unsigned char digest[QSE_SHA1_DIGEST_LEN])
if (n <= 0) break; if (n <= 0) break;
qse_sha1_update (&md, buf, n); 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; qse_md5_t md;
unsigned char buf[READ_BUF_SIZE]; qse_uint8_t buf[READ_BUF_SIZE];
ssize_t n; ssize_t n;
qse_md5_initialize (&md); qse_md5_initialize (&md);
@ -88,7 +115,23 @@ static void md5sum (int fd, unsigned char digest[QSE_MD5_DIGEST_LEN])
if (n <= 0) break; if (n <= 0) break;
qse_md5_update (&md, buf, n); 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, " -2 sha256\n");
fprintf (stderr, " -3 sha384\n"); fprintf (stderr, " -3 sha384\n");
fprintf (stderr, " -5 sha512\n"); fprintf (stderr, " -5 sha512\n");
fprintf (stderr, " -k specify hmac key and enable hmac\n");
} }
int main (int argc, char* argv[]) int main (int argc, char* argv[])
{ {
unsigned char digest[QSE_SHA512_DIGEST_LEN]; qse_uint8_t digest[QSE_HMAC_MAX_DIGEST_LEN];
int digest_len = QSE_SHA512_DIGEST_LEN; qse_size_t digest_len;
void (*sha_func) (int fd, unsigned char* digest) = sha512sum; 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; int i, j;
while (1) while (1)
{ {
int c; int c;
c = getopt(argc, argv, ":hm1235"); c = getopt(argc, argv, ":hk:m1235");
if (c == -1) break; if (c == -1) break;
switch (c) switch (c)
@ -123,28 +169,32 @@ int main (int argc, char* argv[])
print_usage (qse_mbsbasename(argv[0])); print_usage (qse_mbsbasename(argv[0]));
return 0; return 0;
case 'k':
hmac_key = optarg;
break;
case 'm': case 'm':
digest_len = QSE_MD5_DIGEST_LEN; hmac_sha_type = QSE_HMAC_MD5;
sha_func = md5sum; sha_func = md5sum;
break; break;
case '1': case '1':
digest_len = QSE_SHA1_DIGEST_LEN; hmac_sha_type = QSE_HMAC_SHA1;
sha_func = sha1sum; sha_func = sha1sum;
break; break;
case '2': case '2':
digest_len = QSE_SHA256_DIGEST_LEN; hmac_sha_type = QSE_HMAC_SHA256;
sha_func = sha256sum; sha_func = sha256sum;
break; break;
case '3': case '3':
digest_len = QSE_SHA384_DIGEST_LEN; hmac_sha_type = QSE_HMAC_SHA384;
sha_func = sha384sum; sha_func = sha384sum;
break; break;
case '5': case '5':
digest_len = QSE_SHA512_DIGEST_LEN; hmac_sha_type = QSE_HMAC_SHA512;
sha_func = sha512sum; sha_func = sha512sum;
break; break;
@ -157,9 +207,12 @@ int main (int argc, char* argv[])
if (hmac_key)
{
if (optind >= argc) if (optind >= argc)
{ {
sha_func (STDIN_FILENO, digest); 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++) for (j = 0; j < digest_len; j++)
printf ("%02x", digest[j]); printf ("%02x", digest[j]);
printf (" -\n"); printf (" -\n");
@ -173,7 +226,7 @@ int main (int argc, char* argv[])
fd = strcmp(argv[i], "-")? open(argv[i], O_RDONLY): STDIN_FILENO; fd = strcmp(argv[i], "-")? open(argv[i], O_RDONLY): STDIN_FILENO;
if (fd >= 0) if (fd >= 0)
{ {
sha_func (fd, digest); 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++) for (j = 0; j < digest_len; j++)
printf ("%02x", digest[j]); printf ("%02x", digest[j]);
printf (" %s\n", argv[i]); printf (" %s\n", argv[i]);
@ -185,7 +238,38 @@ int main (int argc, char* argv[])
} }
} }
} }
}
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; return 0;
} }