added hmac functions
This commit is contained in:
@ -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/cry/blowfish.h>
|
||||
#include <qse/cry/kseed.h>
|
||||
|
@ -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/sha1.h>
|
||||
#include <qse/cry/md5.h>
|
||||
#include <qse/cry/hmac.h>
|
||||
#include <qse/cmn/path.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user