Rename the HMAC-SHA1 code.

This commit is contained in:
Dag-Erling Smørgrav 2014-07-10 23:57:47 +00:00 committed by des
parent b0ff5af2ba
commit 63bcbcca20
8 changed files with 94 additions and 59 deletions

View file

@ -8,6 +8,7 @@ cryb_HEADERS = \
bitwise.h \
digest.h \
hmac.h \
hmac_sha1.h \
hotp.h \
md.h \
md2.h \

View file

@ -1,6 +1,5 @@
/*-
* Copyright (c) 2012 The University of Oslo
* Copyright (c) 2012 Dag-Erling Smørgrav
* Copyright (c) 2014 Dag-Erling Smørgrav
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -27,29 +26,12 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Author: Dag-Erling Smørgrav <des@des.no>
* Sponsor: the University of Oslo
*
* $Cryb$
*/
#ifndef CRYB_HMAC_H_INCLUDED
#define CRYB_HMAC_H_INCLUDED
#include <cryb/sha1.h>
#define HMAC_LEN 20
typedef struct {
sha1_ctx sha1_ctx;
uint8_t key[64];
size_t keylen;
} hmac_ctx;
void hmac_init(hmac_ctx *, const uint8_t *, size_t);
void hmac_update(hmac_ctx *, const uint8_t *, size_t);
void hmac_final(hmac_ctx *, uint8_t *);
void hmac_complete(const uint8_t *, size_t, const uint8_t *, size_t, uint8_t *);
#include <cryb/hmac_sha1.h>
#endif

55
include/cryb/hmac_sha1.h Normal file
View file

@ -0,0 +1,55 @@
/*-
* Copyright (c) 2012-2014 The University of Oslo
* 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.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
*
* $Cryb$
*/
#ifndef CRYB_HMAC_SHA1_H_INCLUDED
#define CRYB_HMAC_SHA1_H_INCLUDED
#include <cryb/sha1.h>
#define HMAC_SHA1_MAC_LEN 20
#define hmac_sha1_ctx cryb_hmac_sha1_ctx
#define hmac_sha1_init cryb_hmac_sha1_init
#define hmac_sha1_update cryb_hmac_sha1_update
#define hmac_sha1_final cryb_hmac_sha1_final
#define hmac_sha1_complete cryb_hmac_sha1_complete
typedef struct {
sha1_ctx sha1_ctx;
uint8_t key[64];
} hmac_sha1_ctx;
void hmac_sha1_init(hmac_sha1_ctx *, const void *, size_t);
void hmac_sha1_update(hmac_sha1_ctx *, const void *, size_t);
void hmac_sha1_final(hmac_sha1_ctx *, void *);
void hmac_sha1_complete(const void *, size_t, const void *, size_t, void *);
#endif

View file

@ -5,7 +5,7 @@ AM_CPPFLAGS = -I$(top_srcdir)/include
lib_LTLIBRARIES = libcryb-mac.la
libcryb_mac_la_SOURCES = \
hmac.c
hmac_sha1.c
libcryb_mac_la_LIBADD = \
$(top_builddir)/lib/digest/libcryb-digest.la

View file

@ -45,14 +45,12 @@
#endif
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <cryb/sha1.h>
#include <cryb/hmac.h>
#include <cryb/hmac_sha1.h>
void
hmac_init(hmac_ctx *ctx, const uint8_t *key, size_t keylen)
hmac_sha1_init(hmac_sha1_ctx *ctx, const void *key, size_t keylen)
{
uint8_t ipad[64];
@ -77,14 +75,14 @@ hmac_init(hmac_ctx *ctx, const uint8_t *key, size_t keylen)
}
void
hmac_update(hmac_ctx *ctx, const uint8_t *buf, size_t len)
hmac_sha1_update(hmac_sha1_ctx *ctx, const void *buf, size_t len)
{
sha1_update(&ctx->sha1_ctx, buf, len);
}
void
hmac_final(hmac_ctx *ctx, uint8_t *mac)
hmac_sha1_final(hmac_sha1_ctx *ctx, void *mac)
{
uint8_t digest[20], opad[64];
@ -99,12 +97,12 @@ hmac_final(hmac_ctx *ctx, uint8_t *mac)
}
void
hmac_complete(const uint8_t *key, size_t keylen,
const uint8_t *buf, size_t len, uint8_t *mac)
hmac_sha1_complete(const void *key, size_t keylen,
const void *buf, size_t len, void *mac)
{
hmac_ctx ctx;
hmac_sha1_ctx ctx;
hmac_init(&ctx, key, keylen);
hmac_update(&ctx, buf, len);
hmac_final(&ctx, mac);
hmac_sha1_init(&ctx, key, keylen);
hmac_sha1_update(&ctx, buf, len);
hmac_sha1_final(&ctx, mac);
}

View file

@ -37,8 +37,7 @@
#include <stdint.h>
#include <string.h>
#include <cryb/sha1.h>
#include <cryb/hmac.h>
#include <cryb/hmac_sha1.h>
#include <cryb/oath.h>
#define StToNum(St) (St)
@ -62,7 +61,7 @@ DT(const uint8_t *String)
unsigned int
oath_hotp(const uint8_t *K, size_t Klen, uint64_t seq, unsigned int Digit)
{
hmac_ctx ctx;
hmac_sha1_ctx ctx;
uint8_t C[8];
uint8_t HS[20];
uint32_t Sbits, Snum;
@ -74,9 +73,9 @@ oath_hotp(const uint8_t *K, size_t Klen, uint64_t seq, unsigned int Digit)
}
/* HS = HMAC-SHA-1(K,C) */
hmac_init(&ctx, K, Klen);
hmac_update(&ctx, (const uint8_t *)&C, sizeof C);
hmac_final(&ctx, HS);
hmac_sha1_init(&ctx, K, Klen);
hmac_sha1_update(&ctx, (const uint8_t *)&C, sizeof C);
hmac_sha1_final(&ctx, HS);
Sbits = DT(HS);
Snum = StToNum(Sbits);

View file

@ -83,12 +83,12 @@ t_sha512_openssl_LDADD = $(OPENSSL_LDADD)
endif
# libcryb-mac
TESTS += t_hmac
TESTS += t_hmac_sha1
if WITH_OPENSSL
TESTS += t_hmac_openssl
t_hmac_openssl_SOURCES = t_hmac.c
t_hmac_openssl_CFLAGS = $(OPENSSL_INCLUDES) $(OPENSSL_CFLAGS)
t_hmac_openssl_LDADD = $(OPENSSL_LDADD)
TESTS += t_hmac_sha1_openssl
t_hmac_sha1_openssl_SOURCES = t_hmac_sha1.c
t_hmac_sha1_openssl_CFLAGS = $(OPENSSL_INCLUDES) $(OPENSSL_CFLAGS)
t_hmac_sha1_openssl_LDADD = $(OPENSSL_LDADD)
endif
check_PROGRAMS = $(TESTS)

View file

@ -48,10 +48,10 @@
#include <openssl/evp.h>
#include <openssl/hmac.h>
#define HMAC_LEN 20
#define HMAC_SHA1_MAC_LEN 20
static void
t_hmac_complete(const void *key, size_t keylen,
t_hmac_sha1_complete(const void *key, size_t keylen,
const void *msg, size_t msglen, uint8_t *mac)
{
HMAC_CTX ctx;
@ -68,8 +68,8 @@ t_hmac_complete(const void *key, size_t keylen,
#include <cryb/sha1.h>
#include <cryb/hmac.h>
#define t_hmac_complete(key, keylen, msg, msglen, mac) \
hmac_complete(key, keylen, msg, msglen, mac)
#define t_hmac_sha1_complete(key, keylen, msg, msglen, mac) \
hmac_sha1_complete(key, keylen, msg, msglen, mac)
#endif
@ -81,8 +81,8 @@ static struct t_vector {
uint8_t key[100];
size_t keylen;
const char *msg;
const uint8_t mac[HMAC_LEN];
} t_hmac_vectors[] = {
const uint8_t mac[HMAC_SHA1_MAC_LEN];
} t_hmac_sha1_vectors[] = {
{
"zero-length key, zero-length message",
{ },
@ -180,20 +180,20 @@ static struct t_vector {
* specified key and compare it to the expected result.
*/
static int
t_hmac_vector(char **desc CRYB_UNUSED, void *arg)
t_hmac_sha1_vector(char **desc CRYB_UNUSED, void *arg)
{
struct t_vector *vector = (struct t_vector *)arg;
uint8_t mac[HMAC_LEN];
uint8_t mac[HMAC_SHA1_MAC_LEN];
t_hmac_complete(vector->key, vector->keylen,
t_hmac_sha1_complete(vector->key, vector->keylen,
(const uint8_t *)vector->msg, strlen(vector->msg),
mac);
if (memcmp(mac, vector->mac, HMAC_LEN) != 0) {
if (memcmp(mac, vector->mac, HMAC_SHA1_MAC_LEN) != 0) {
t_verbose("expected ");
t_verbose_hex(vector->mac, HMAC_LEN);
t_verbose_hex(vector->mac, HMAC_SHA1_MAC_LEN);
t_verbose("\n");
t_verbose("got ");
t_verbose_hex(mac, HMAC_LEN);
t_verbose_hex(mac, HMAC_SHA1_MAC_LEN);
t_verbose("\n");
return (0);
}
@ -212,10 +212,10 @@ t_prepare(int argc, char *argv[])
(void)argc;
(void)argv;
n = sizeof t_hmac_vectors / sizeof t_hmac_vectors[0];
n = sizeof t_hmac_sha1_vectors / sizeof t_hmac_sha1_vectors[0];
for (i = 0; i < n; ++i)
t_add_test(t_hmac_vector, &t_hmac_vectors[i],
t_hmac_vectors[i].desc);
t_add_test(t_hmac_sha1_vector, &t_hmac_sha1_vectors[i],
t_hmac_sha1_vectors[i].desc);
return (0);
}