mirror of
https://github.com/cryb-to/cryb-to.git
synced 2024-12-22 04:21:08 +00:00
Implement HMAC-SHA{256,384,512} and add unit tests. Remove pre-existing
HMAC implementations from the libcperciva and XySSL code.
This commit is contained in:
parent
17d3517f3a
commit
6aa7f9a0e6
19 changed files with 1133 additions and 366 deletions
|
@ -9,6 +9,9 @@ cryb_HEADERS = \
|
|||
digest.h \
|
||||
hmac.h \
|
||||
hmac_sha1.h \
|
||||
hmac_sha256.h \
|
||||
hmac_sha384.h \
|
||||
hmac_sha512.h \
|
||||
hotp.h \
|
||||
md.h \
|
||||
md2.h \
|
||||
|
|
|
@ -33,5 +33,8 @@
|
|||
#define CRYB_HMAC_H_INCLUDED
|
||||
|
||||
#include <cryb/hmac_sha1.h>
|
||||
#include <cryb/hmac_sha256.h>
|
||||
#include <cryb/hmac_sha384.h>
|
||||
#include <cryb/hmac_sha512.h>
|
||||
|
||||
#endif
|
||||
|
|
55
include/cryb/hmac_sha256.h
Normal file
55
include/cryb/hmac_sha256.h
Normal 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_SHA256_H_INCLUDED
|
||||
#define CRYB_HMAC_SHA256_H_INCLUDED
|
||||
|
||||
#include <cryb/sha256.h>
|
||||
|
||||
#define HMAC_SHA256_MAC_LEN 32
|
||||
|
||||
#define hmac_sha256_ctx cryb_hmac_sha256_ctx
|
||||
#define hmac_sha256_init cryb_hmac_sha256_init
|
||||
#define hmac_sha256_update cryb_hmac_sha256_update
|
||||
#define hmac_sha256_final cryb_hmac_sha256_final
|
||||
#define hmac_sha256_complete cryb_hmac_sha256_complete
|
||||
|
||||
typedef struct {
|
||||
sha256_ctx ictx;
|
||||
sha256_ctx octx;
|
||||
} hmac_sha256_ctx;
|
||||
|
||||
void hmac_sha256_init(hmac_sha256_ctx *, const void *, size_t);
|
||||
void hmac_sha256_update(hmac_sha256_ctx *, const void *, size_t);
|
||||
void hmac_sha256_final(hmac_sha256_ctx *, void *);
|
||||
void hmac_sha256_complete(const void *, size_t, const void *, size_t, void *);
|
||||
|
||||
#endif
|
55
include/cryb/hmac_sha384.h
Normal file
55
include/cryb/hmac_sha384.h
Normal 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_SHA384_H_INCLUDED
|
||||
#define CRYB_HMAC_SHA384_H_INCLUDED
|
||||
|
||||
#include <cryb/sha384.h>
|
||||
|
||||
#define HMAC_SHA384_MAC_LEN 48
|
||||
|
||||
#define hmac_sha384_ctx cryb_hmac_sha384_ctx
|
||||
#define hmac_sha384_init cryb_hmac_sha384_init
|
||||
#define hmac_sha384_update cryb_hmac_sha384_update
|
||||
#define hmac_sha384_final cryb_hmac_sha384_final
|
||||
#define hmac_sha384_complete cryb_hmac_sha384_complete
|
||||
|
||||
typedef struct {
|
||||
sha384_ctx ictx;
|
||||
sha384_ctx octx;
|
||||
} hmac_sha384_ctx;
|
||||
|
||||
void hmac_sha384_init(hmac_sha384_ctx *, const void *, size_t);
|
||||
void hmac_sha384_update(hmac_sha384_ctx *, const void *, size_t);
|
||||
void hmac_sha384_final(hmac_sha384_ctx *, void *);
|
||||
void hmac_sha384_complete(const void *, size_t, const void *, size_t, void *);
|
||||
|
||||
#endif
|
55
include/cryb/hmac_sha512.h
Normal file
55
include/cryb/hmac_sha512.h
Normal 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_SHA512_H_INCLUDED
|
||||
#define CRYB_HMAC_SHA512_H_INCLUDED
|
||||
|
||||
#include <cryb/sha512.h>
|
||||
|
||||
#define HMAC_SHA512_MAC_LEN 64
|
||||
|
||||
#define hmac_sha512_ctx cryb_hmac_sha512_ctx
|
||||
#define hmac_sha512_init cryb_hmac_sha512_init
|
||||
#define hmac_sha512_update cryb_hmac_sha512_update
|
||||
#define hmac_sha512_final cryb_hmac_sha512_final
|
||||
#define hmac_sha512_complete cryb_hmac_sha512_complete
|
||||
|
||||
typedef struct {
|
||||
sha512_ctx ictx;
|
||||
sha512_ctx octx;
|
||||
} hmac_sha512_ctx;
|
||||
|
||||
void hmac_sha512_init(hmac_sha512_ctx *, const void *, size_t);
|
||||
void hmac_sha512_update(hmac_sha512_ctx *, const void *, size_t);
|
||||
void hmac_sha512_final(hmac_sha512_ctx *, void *);
|
||||
void hmac_sha512_complete(const void *, size_t, const void *, size_t, void *);
|
||||
|
||||
#endif
|
|
@ -47,12 +47,6 @@
|
|||
|
||||
extern digest_algorithm sha256_digest;
|
||||
|
||||
#define hmac_sha256_init cryb_hmac_sha256_init
|
||||
#define hmac_sha256_update cryb_hmac_sha256_update
|
||||
#define hmac_sha256_final cryb_hmac_sha256_final
|
||||
#define hmac_sha256_complete cryb_hmac_sha256_complete
|
||||
#define hmac_sha256_ctx cryb_hmac_sha256_ctx
|
||||
|
||||
/* Context structure for SHA256 operations. */
|
||||
typedef struct {
|
||||
uint32_t state[8];
|
||||
|
@ -85,39 +79,6 @@ void sha256_final(sha256_ctx *, uint8_t[SHA256_DIGEST_LEN]);
|
|||
*/
|
||||
void sha256_complete(const void *, size_t, uint8_t[SHA256_DIGEST_LEN]);
|
||||
|
||||
/* Context structure for HMAC-SHA256 operations. */
|
||||
typedef struct {
|
||||
sha256_ctx ictx;
|
||||
sha256_ctx octx;
|
||||
} hmac_sha256_ctx;
|
||||
|
||||
/**
|
||||
* hmac_sha256_init(ctx, K, Klen):
|
||||
* Initialize the HMAC-SHA256 context ${ctx} with ${Klen} bytes of key from
|
||||
* ${K}.
|
||||
*/
|
||||
void hmac_sha256_init(hmac_sha256_ctx *, const void *, size_t);
|
||||
|
||||
/**
|
||||
* hmac_sha256_update(ctx, in, len):
|
||||
* Input ${len} bytes from ${in} into the HMAC-SHA256 context ${ctx}.
|
||||
*/
|
||||
void hmac_sha256_update(hmac_sha256_ctx *, const void *, size_t);
|
||||
|
||||
/**
|
||||
* hmac_sha256_final(ctx, digest):
|
||||
* Output the HMAC-SHA256 of the data input to the context ${ctx} into the
|
||||
* buffer ${digest}.
|
||||
*/
|
||||
void hmac_sha256_final(hmac_sha256_ctx *, uint8_t[SHA256_DIGEST_LEN]);
|
||||
|
||||
/**
|
||||
* hmac_sha256_complete(K, Klen, in, len, digest):
|
||||
* Compute the HMAC-SHA256 of ${len} bytes from ${in} using the key ${K} of
|
||||
* length ${Klen}, and write the result to ${digest}.
|
||||
*/
|
||||
void hmac_sha256_complete(const void *, size_t, const void *, size_t, uint8_t[32]);
|
||||
|
||||
/**
|
||||
* PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
|
||||
* Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
|
||||
|
|
|
@ -54,9 +54,6 @@ typedef struct
|
|||
uint64_t total[2]; /*!< number of bytes processed */
|
||||
uint64_t state[8]; /*!< intermediate digest state */
|
||||
uint8_t buffer[128]; /*!< data block being processed */
|
||||
|
||||
uint8_t ipad[128]; /*!< HMAC: inner padding */
|
||||
uint8_t opad[128]; /*!< HMAC: outer padding */
|
||||
}
|
||||
sha384_ctx;
|
||||
|
||||
|
@ -98,45 +95,6 @@ void sha384_final( sha384_ctx *ctx, unsigned char output[64] );
|
|||
void sha384_complete( const void *input, int ilen,
|
||||
unsigned char output[64] );
|
||||
|
||||
/**
|
||||
* \brief SHA-384 HMAC context setup
|
||||
*
|
||||
* \param ctx HMAC context to be initialized
|
||||
* \param key HMAC secret key
|
||||
* \param keylen length of the HMAC key
|
||||
*/
|
||||
void sha384_hmac_init( sha384_ctx *ctx, unsigned char *key, int keylen );
|
||||
|
||||
/**
|
||||
* \brief SHA-384 HMAC process buffer
|
||||
*
|
||||
* \param ctx HMAC context
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
*/
|
||||
void sha384_hmac_update( sha384_ctx *ctx, unsigned char *input, int ilen );
|
||||
|
||||
/**
|
||||
* \brief SHA-384 HMAC final digest
|
||||
*
|
||||
* \param ctx HMAC context
|
||||
* \param output SHA-384/384 HMAC checksum result
|
||||
*/
|
||||
void sha384_hmac_final( sha384_ctx *ctx, unsigned char output[64] );
|
||||
|
||||
/**
|
||||
* \brief Output = HMAC-SHA-384( hmac key, input buffer )
|
||||
*
|
||||
* \param key HMAC secret key
|
||||
* \param keylen length of the HMAC key
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
* \param output HMAC-SHA-384/384 result
|
||||
*/
|
||||
void sha384_hmac_complete( unsigned char *key, int keylen,
|
||||
unsigned char *input, int ilen,
|
||||
unsigned char output[64] );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -54,9 +54,6 @@ typedef struct
|
|||
uint64_t total[2]; /*!< number of bytes processed */
|
||||
uint64_t state[8]; /*!< intermediate digest state */
|
||||
uint8_t buffer[128]; /*!< data block being processed */
|
||||
|
||||
uint8_t ipad[128]; /*!< HMAC: inner padding */
|
||||
uint8_t opad[128]; /*!< HMAC: outer padding */
|
||||
}
|
||||
sha512_ctx;
|
||||
|
||||
|
@ -98,45 +95,6 @@ void sha512_final( sha512_ctx *ctx, unsigned char output[64] );
|
|||
void sha512_complete( const void *input, int ilen,
|
||||
unsigned char output[64] );
|
||||
|
||||
/**
|
||||
* \brief SHA-512 HMAC context setup
|
||||
*
|
||||
* \param ctx HMAC context to be initialized
|
||||
* \param key HMAC secret key
|
||||
* \param keylen length of the HMAC key
|
||||
*/
|
||||
void sha512_hmac_init( sha512_ctx *ctx, unsigned char *key, int keylen );
|
||||
|
||||
/**
|
||||
* \brief SHA-512 HMAC process buffer
|
||||
*
|
||||
* \param ctx HMAC context
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
*/
|
||||
void sha512_hmac_update( sha512_ctx *ctx, unsigned char *input, int ilen );
|
||||
|
||||
/**
|
||||
* \brief SHA-512 HMAC final digest
|
||||
*
|
||||
* \param ctx HMAC context
|
||||
* \param output SHA-384/512 HMAC checksum result
|
||||
*/
|
||||
void sha512_hmac_final( sha512_ctx *ctx, unsigned char output[64] );
|
||||
|
||||
/**
|
||||
* \brief Output = HMAC-SHA-512( hmac key, input buffer )
|
||||
*
|
||||
* \param key HMAC secret key
|
||||
* \param keylen length of the HMAC key
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
* \param output HMAC-SHA-384/512 result
|
||||
*/
|
||||
void sha512_hmac_complete( unsigned char *key, int keylen,
|
||||
unsigned char *input, int ilen,
|
||||
unsigned char output[64] );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -316,98 +316,7 @@ sha256_complete(const void * in, size_t len, uint8_t digest[SHA256_DIGEST_LEN])
|
|||
sha256_final(&ctx, digest);
|
||||
}
|
||||
|
||||
/**
|
||||
* hmac_sha256_init(ctx, K, Klen):
|
||||
* Initialize the HMAC-SHA256 context ${ctx} with ${Klen} bytes of key from
|
||||
* ${K}.
|
||||
*/
|
||||
void
|
||||
hmac_sha256_init(hmac_sha256_ctx * ctx, const void * _K, size_t Klen)
|
||||
{
|
||||
uint8_t pad[64];
|
||||
uint8_t khash[SHA256_DIGEST_LEN];
|
||||
const uint8_t * K = _K;
|
||||
size_t i;
|
||||
|
||||
/* If Klen > 64, the key is really SHA256(K). */
|
||||
if (Klen > 64) {
|
||||
sha256_init(&ctx->ictx);
|
||||
sha256_update(&ctx->ictx, K, Klen);
|
||||
sha256_final(&ctx->ictx, khash);
|
||||
K = khash;
|
||||
Klen = sizeof khash;
|
||||
}
|
||||
|
||||
/* Inner SHA256 operation is SHA256(K xor [block of 0x36] || data). */
|
||||
sha256_init(&ctx->ictx);
|
||||
memset(pad, 0x36, 64);
|
||||
for (i = 0; i < Klen; i++)
|
||||
pad[i] ^= K[i];
|
||||
sha256_update(&ctx->ictx, pad, 64);
|
||||
|
||||
/* Outer SHA256 operation is SHA256(K xor [block of 0x5c] || hash). */
|
||||
sha256_init(&ctx->octx);
|
||||
memset(pad, 0x5c, 64);
|
||||
for (i = 0; i < Klen; i++)
|
||||
pad[i] ^= K[i];
|
||||
sha256_update(&ctx->octx, pad, 64);
|
||||
|
||||
/* Clean the stack. */
|
||||
memset(khash, 0, sizeof khash);
|
||||
memset(pad, 0, 64);
|
||||
}
|
||||
|
||||
/**
|
||||
* hmac_sha256_update(ctx, in, len):
|
||||
* Input ${len} bytes from ${in} into the HMAC-SHA256 context ${ctx}.
|
||||
*/
|
||||
void
|
||||
hmac_sha256_update(hmac_sha256_ctx * ctx, const void *in, size_t len)
|
||||
{
|
||||
|
||||
/* Feed data to the inner SHA256 operation. */
|
||||
sha256_update(&ctx->ictx, in, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* hmac_sha256_final(ctx, digest):
|
||||
* Output the HMAC-SHA256 of the data input to the context ${ctx} into the
|
||||
* buffer ${digest}.
|
||||
*/
|
||||
void
|
||||
hmac_sha256_final(hmac_sha256_ctx * ctx, uint8_t digest[SHA256_DIGEST_LEN])
|
||||
{
|
||||
uint8_t ihash[SHA256_DIGEST_LEN];
|
||||
|
||||
/* Finish the inner SHA256 operation. */
|
||||
sha256_final(&ctx->ictx, ihash);
|
||||
|
||||
/* Feed the inner hash to the outer SHA256 operation. */
|
||||
sha256_update(&ctx->octx, ihash, sizeof ihash);
|
||||
|
||||
/* Finish the outer SHA256 operation. */
|
||||
sha256_final(&ctx->octx, digest);
|
||||
|
||||
/* Clean the stack. */
|
||||
memset(ihash, 0, sizeof ihash);
|
||||
}
|
||||
|
||||
/**
|
||||
* hmac_sha256_complete(K, Klen, in, len, digest):
|
||||
* Compute the HMAC-SHA256 of ${len} bytes from ${in} using the key ${K} of
|
||||
* length ${Klen}, and write the result to ${digest}.
|
||||
*/
|
||||
void
|
||||
hmac_sha256_complete(const void * K, size_t Klen, const void * in, size_t len,
|
||||
uint8_t digest[SHA256_DIGEST_LEN])
|
||||
{
|
||||
hmac_sha256_ctx ctx;
|
||||
|
||||
hmac_sha256_init(&ctx, K, Klen);
|
||||
hmac_sha256_update(&ctx, in, len);
|
||||
hmac_sha256_final(&ctx, digest);
|
||||
}
|
||||
|
||||
#if 0
|
||||
/**
|
||||
* PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
|
||||
* Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
|
||||
|
@ -464,6 +373,7 @@ pbkdf2_sha256(const uint8_t * passwd, size_t passwdlen, const uint8_t * salt,
|
|||
/* Clean PShctx, since we never called _final on it. */
|
||||
memset(&PShctx, 0, sizeof(hmac_sha256_ctx));
|
||||
}
|
||||
#endif
|
||||
|
||||
digest_algorithm sha256_digest = {
|
||||
.name = "sha256",
|
||||
|
|
|
@ -309,80 +309,6 @@ void sha384_complete( const void *input, int ilen,
|
|||
memset( &ctx, 0, sizeof( sha384_ctx ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-384 HMAC context setup
|
||||
*/
|
||||
void sha384_hmac_init( sha384_ctx *ctx, unsigned char *key, int keylen )
|
||||
{
|
||||
int i;
|
||||
unsigned char sum[64];
|
||||
|
||||
if( keylen > 128 )
|
||||
{
|
||||
sha384_complete( key, keylen, sum );
|
||||
keylen = SHA384_DIGEST_LEN;
|
||||
key = sum;
|
||||
}
|
||||
|
||||
memset( ctx->ipad, 0x36, 128 );
|
||||
memset( ctx->opad, 0x5C, 128 );
|
||||
|
||||
for( i = 0; i < keylen; i++ )
|
||||
{
|
||||
ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] );
|
||||
ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] );
|
||||
}
|
||||
|
||||
sha384_init( ctx );
|
||||
sha384_update( ctx, ctx->ipad, 128 );
|
||||
|
||||
memset( sum, 0, sizeof( sum ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-384 HMAC process buffer
|
||||
*/
|
||||
void sha384_hmac_update( sha384_ctx *ctx,
|
||||
unsigned char *input, int ilen )
|
||||
{
|
||||
sha384_update( ctx, input, ilen );
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-384 HMAC final digest
|
||||
*/
|
||||
void sha384_hmac_final( sha384_ctx *ctx, unsigned char output[64] )
|
||||
{
|
||||
int hlen;
|
||||
unsigned char tmpbuf[64];
|
||||
|
||||
hlen = SHA384_DIGEST_LEN;
|
||||
|
||||
sha384_final( ctx, tmpbuf );
|
||||
sha384_init( ctx );
|
||||
sha384_update( ctx, ctx->opad, 128 );
|
||||
sha384_update( ctx, tmpbuf, hlen );
|
||||
sha384_final( ctx, output );
|
||||
|
||||
memset( tmpbuf, 0, sizeof( tmpbuf ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* output = HMAC-SHA-384( hmac key, input buffer )
|
||||
*/
|
||||
void sha384_hmac_complete( unsigned char *key, int keylen,
|
||||
unsigned char *input, int ilen,
|
||||
unsigned char output[64] )
|
||||
{
|
||||
sha384_ctx ctx;
|
||||
|
||||
sha384_hmac_init( &ctx, key, keylen );
|
||||
sha384_hmac_update( &ctx, input, ilen );
|
||||
sha384_hmac_final( &ctx, output );
|
||||
|
||||
memset( &ctx, 0, sizeof( sha384_ctx ) );
|
||||
}
|
||||
|
||||
digest_algorithm sha384_digest = {
|
||||
.name = "sha384",
|
||||
.contextlen = sizeof sha384_digest,
|
||||
|
|
|
@ -312,80 +312,6 @@ void sha512_complete( const void *input, int ilen,
|
|||
memset( &ctx, 0, sizeof( sha512_ctx ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-512 HMAC context setup
|
||||
*/
|
||||
void sha512_hmac_init( sha512_ctx *ctx, unsigned char *key, int keylen )
|
||||
{
|
||||
int i;
|
||||
unsigned char sum[64];
|
||||
|
||||
if( keylen > 128 )
|
||||
{
|
||||
sha512_complete( key, keylen, sum );
|
||||
keylen = SHA512_DIGEST_LEN;
|
||||
key = sum;
|
||||
}
|
||||
|
||||
memset( ctx->ipad, 0x36, 128 );
|
||||
memset( ctx->opad, 0x5C, 128 );
|
||||
|
||||
for( i = 0; i < keylen; i++ )
|
||||
{
|
||||
ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] );
|
||||
ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] );
|
||||
}
|
||||
|
||||
sha512_init( ctx );
|
||||
sha512_update( ctx, ctx->ipad, 128 );
|
||||
|
||||
memset( sum, 0, sizeof( sum ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-512 HMAC process buffer
|
||||
*/
|
||||
void sha512_hmac_update( sha512_ctx *ctx,
|
||||
unsigned char *input, int ilen )
|
||||
{
|
||||
sha512_update( ctx, input, ilen );
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-512 HMAC final digest
|
||||
*/
|
||||
void sha512_hmac_final( sha512_ctx *ctx, unsigned char output[64] )
|
||||
{
|
||||
int hlen;
|
||||
unsigned char tmpbuf[64];
|
||||
|
||||
hlen = SHA512_DIGEST_LEN;
|
||||
|
||||
sha512_final( ctx, tmpbuf );
|
||||
sha512_init( ctx );
|
||||
sha512_update( ctx, ctx->opad, 128 );
|
||||
sha512_update( ctx, tmpbuf, hlen );
|
||||
sha512_final( ctx, output );
|
||||
|
||||
memset( tmpbuf, 0, sizeof( tmpbuf ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* output = HMAC-SHA-512( hmac key, input buffer )
|
||||
*/
|
||||
void sha512_hmac_complete( unsigned char *key, int keylen,
|
||||
unsigned char *input, int ilen,
|
||||
unsigned char output[64] )
|
||||
{
|
||||
sha512_ctx ctx;
|
||||
|
||||
sha512_hmac_init( &ctx, key, keylen );
|
||||
sha512_hmac_update( &ctx, input, ilen );
|
||||
sha512_hmac_final( &ctx, output );
|
||||
|
||||
memset( &ctx, 0, sizeof( sha512_ctx ) );
|
||||
}
|
||||
|
||||
digest_algorithm sha512_digest = {
|
||||
.name = "sha512",
|
||||
.contextlen = sizeof sha512_digest,
|
||||
|
|
|
@ -5,7 +5,10 @@ AM_CPPFLAGS = -I$(top_srcdir)/include
|
|||
lib_LTLIBRARIES = libcryb-mac.la
|
||||
|
||||
libcryb_mac_la_SOURCES = \
|
||||
hmac_sha1.c
|
||||
hmac_sha1.c \
|
||||
hmac_sha256.c \
|
||||
hmac_sha384.c \
|
||||
hmac_sha512.c
|
||||
|
||||
libcryb_mac_la_LIBADD = \
|
||||
$(top_builddir)/lib/digest/libcryb-digest.la
|
||||
|
|
108
lib/mac/hmac_sha256.c
Normal file
108
lib/mac/hmac_sha256.c
Normal file
|
@ -0,0 +1,108 @@
|
|||
/*-
|
||||
* Copyright (c) 2012 The University of Oslo
|
||||
* Copyright (c) 2012 Dag-Erling Smørgrav
|
||||
* 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.
|
||||
*
|
||||
* Author: Dag-Erling Smørgrav <des@des.no>
|
||||
* Sponsor: the University of Oslo
|
||||
*
|
||||
* $Cryb$
|
||||
*/
|
||||
|
||||
#include "cryb/impl.h"
|
||||
|
||||
#ifdef HAVE_SYS_ENDIAN_H
|
||||
#include <sys/endian.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_ENDIAN_H
|
||||
#define _BSD_SOURCE
|
||||
#include <endian.h>
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <cryb/hmac_sha256.h>
|
||||
|
||||
void
|
||||
hmac_sha256_init(hmac_sha256_ctx *ctx, const void *key, size_t keylen)
|
||||
{
|
||||
uint8_t keybuf[SHA256_BLOCK_LEN], pad[SHA256_BLOCK_LEN];
|
||||
|
||||
/* prepare key */
|
||||
memset(keybuf, 0, sizeof keybuf);
|
||||
if (keylen > sizeof keybuf)
|
||||
sha256_complete(key, keylen, keybuf);
|
||||
else
|
||||
memcpy(keybuf, key, keylen);
|
||||
|
||||
/* input pad */
|
||||
for (unsigned int i = 0; i < sizeof pad; ++i)
|
||||
pad[i] = 0x36 ^ keybuf[i];
|
||||
sha256_init(&ctx->ictx);
|
||||
sha256_update(&ctx->ictx, pad, sizeof pad);
|
||||
|
||||
/* output pad */
|
||||
for (unsigned int i = 0; i < sizeof pad; ++i)
|
||||
pad[i] = 0x5c ^ keybuf[i];
|
||||
sha256_init(&ctx->octx);
|
||||
sha256_update(&ctx->octx, pad, sizeof pad);
|
||||
|
||||
/* hide the evidence */
|
||||
memset(keybuf, 0, sizeof keybuf);
|
||||
memset(pad, 0, sizeof pad);
|
||||
}
|
||||
|
||||
void
|
||||
hmac_sha256_update(hmac_sha256_ctx *ctx, const void *buf, size_t len)
|
||||
{
|
||||
|
||||
sha256_update(&ctx->ictx, buf, len);
|
||||
}
|
||||
|
||||
void
|
||||
hmac_sha256_final(hmac_sha256_ctx *ctx, void *mac)
|
||||
{
|
||||
uint8_t digest[SHA256_DIGEST_LEN];
|
||||
|
||||
sha256_final(&ctx->ictx, digest);
|
||||
sha256_update(&ctx->octx, digest, sizeof digest);
|
||||
sha256_final(&ctx->octx, mac);
|
||||
memset(ctx, 0, sizeof *ctx);
|
||||
}
|
||||
|
||||
void
|
||||
hmac_sha256_complete(const void *key, size_t keylen,
|
||||
const void *buf, size_t len, void *mac)
|
||||
{
|
||||
hmac_sha256_ctx ctx;
|
||||
|
||||
hmac_sha256_init(&ctx, key, keylen);
|
||||
hmac_sha256_update(&ctx, buf, len);
|
||||
hmac_sha256_final(&ctx, mac);
|
||||
}
|
108
lib/mac/hmac_sha384.c
Normal file
108
lib/mac/hmac_sha384.c
Normal file
|
@ -0,0 +1,108 @@
|
|||
/*-
|
||||
* Copyright (c) 2012 The University of Oslo
|
||||
* Copyright (c) 2012 Dag-Erling Smørgrav
|
||||
* 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.
|
||||
*
|
||||
* Author: Dag-Erling Smørgrav <des@des.no>
|
||||
* Sponsor: the University of Oslo
|
||||
*
|
||||
* $Cryb$
|
||||
*/
|
||||
|
||||
#include "cryb/impl.h"
|
||||
|
||||
#ifdef HAVE_SYS_ENDIAN_H
|
||||
#include <sys/endian.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_ENDIAN_H
|
||||
#define _BSD_SOURCE
|
||||
#include <endian.h>
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <cryb/hmac_sha384.h>
|
||||
|
||||
void
|
||||
hmac_sha384_init(hmac_sha384_ctx *ctx, const void *key, size_t keylen)
|
||||
{
|
||||
uint8_t keybuf[SHA384_BLOCK_LEN], pad[SHA384_BLOCK_LEN];
|
||||
|
||||
/* prepare key */
|
||||
memset(keybuf, 0, sizeof keybuf);
|
||||
if (keylen > sizeof keybuf)
|
||||
sha384_complete(key, keylen, keybuf);
|
||||
else
|
||||
memcpy(keybuf, key, keylen);
|
||||
|
||||
/* input pad */
|
||||
for (unsigned int i = 0; i < sizeof pad; ++i)
|
||||
pad[i] = 0x36 ^ keybuf[i];
|
||||
sha384_init(&ctx->ictx);
|
||||
sha384_update(&ctx->ictx, pad, sizeof pad);
|
||||
|
||||
/* output pad */
|
||||
for (unsigned int i = 0; i < sizeof pad; ++i)
|
||||
pad[i] = 0x5c ^ keybuf[i];
|
||||
sha384_init(&ctx->octx);
|
||||
sha384_update(&ctx->octx, pad, sizeof pad);
|
||||
|
||||
/* hide the evidence */
|
||||
memset(keybuf, 0, sizeof keybuf);
|
||||
memset(pad, 0, sizeof pad);
|
||||
}
|
||||
|
||||
void
|
||||
hmac_sha384_update(hmac_sha384_ctx *ctx, const void *buf, size_t len)
|
||||
{
|
||||
|
||||
sha384_update(&ctx->ictx, buf, len);
|
||||
}
|
||||
|
||||
void
|
||||
hmac_sha384_final(hmac_sha384_ctx *ctx, void *mac)
|
||||
{
|
||||
uint8_t digest[SHA384_DIGEST_LEN];
|
||||
|
||||
sha384_final(&ctx->ictx, digest);
|
||||
sha384_update(&ctx->octx, digest, sizeof digest);
|
||||
sha384_final(&ctx->octx, mac);
|
||||
memset(ctx, 0, sizeof *ctx);
|
||||
}
|
||||
|
||||
void
|
||||
hmac_sha384_complete(const void *key, size_t keylen,
|
||||
const void *buf, size_t len, void *mac)
|
||||
{
|
||||
hmac_sha384_ctx ctx;
|
||||
|
||||
hmac_sha384_init(&ctx, key, keylen);
|
||||
hmac_sha384_update(&ctx, buf, len);
|
||||
hmac_sha384_final(&ctx, mac);
|
||||
}
|
108
lib/mac/hmac_sha512.c
Normal file
108
lib/mac/hmac_sha512.c
Normal file
|
@ -0,0 +1,108 @@
|
|||
/*-
|
||||
* Copyright (c) 2012 The University of Oslo
|
||||
* Copyright (c) 2012 Dag-Erling Smørgrav
|
||||
* 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.
|
||||
*
|
||||
* Author: Dag-Erling Smørgrav <des@des.no>
|
||||
* Sponsor: the University of Oslo
|
||||
*
|
||||
* $Cryb$
|
||||
*/
|
||||
|
||||
#include "cryb/impl.h"
|
||||
|
||||
#ifdef HAVE_SYS_ENDIAN_H
|
||||
#include <sys/endian.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_ENDIAN_H
|
||||
#define _BSD_SOURCE
|
||||
#include <endian.h>
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <cryb/hmac_sha512.h>
|
||||
|
||||
void
|
||||
hmac_sha512_init(hmac_sha512_ctx *ctx, const void *key, size_t keylen)
|
||||
{
|
||||
uint8_t keybuf[SHA512_BLOCK_LEN], pad[SHA512_BLOCK_LEN];
|
||||
|
||||
/* prepare key */
|
||||
memset(keybuf, 0, sizeof keybuf);
|
||||
if (keylen > sizeof keybuf)
|
||||
sha512_complete(key, keylen, keybuf);
|
||||
else
|
||||
memcpy(keybuf, key, keylen);
|
||||
|
||||
/* input pad */
|
||||
for (unsigned int i = 0; i < sizeof pad; ++i)
|
||||
pad[i] = 0x36 ^ keybuf[i];
|
||||
sha512_init(&ctx->ictx);
|
||||
sha512_update(&ctx->ictx, pad, sizeof pad);
|
||||
|
||||
/* output pad */
|
||||
for (unsigned int i = 0; i < sizeof pad; ++i)
|
||||
pad[i] = 0x5c ^ keybuf[i];
|
||||
sha512_init(&ctx->octx);
|
||||
sha512_update(&ctx->octx, pad, sizeof pad);
|
||||
|
||||
/* hide the evidence */
|
||||
memset(keybuf, 0, sizeof keybuf);
|
||||
memset(pad, 0, sizeof pad);
|
||||
}
|
||||
|
||||
void
|
||||
hmac_sha512_update(hmac_sha512_ctx *ctx, const void *buf, size_t len)
|
||||
{
|
||||
|
||||
sha512_update(&ctx->ictx, buf, len);
|
||||
}
|
||||
|
||||
void
|
||||
hmac_sha512_final(hmac_sha512_ctx *ctx, void *mac)
|
||||
{
|
||||
uint8_t digest[SHA512_DIGEST_LEN];
|
||||
|
||||
sha512_final(&ctx->ictx, digest);
|
||||
sha512_update(&ctx->octx, digest, sizeof digest);
|
||||
sha512_final(&ctx->octx, mac);
|
||||
memset(ctx, 0, sizeof *ctx);
|
||||
}
|
||||
|
||||
void
|
||||
hmac_sha512_complete(const void *key, size_t keylen,
|
||||
const void *buf, size_t len, void *mac)
|
||||
{
|
||||
hmac_sha512_ctx ctx;
|
||||
|
||||
hmac_sha512_init(&ctx, key, keylen);
|
||||
hmac_sha512_update(&ctx, buf, len);
|
||||
hmac_sha512_final(&ctx, mac);
|
||||
}
|
|
@ -79,12 +79,21 @@ t_sha512_openssl_LDADD = $(OPENSSL_LDADD)
|
|||
endif
|
||||
|
||||
# libcryb-mac
|
||||
TESTS += t_hmac_sha1
|
||||
TESTS += t_hmac_sha1 t_hmac_sha256 t_hmac_sha384 t_hmac_sha512
|
||||
if WITH_OPENSSL
|
||||
TESTS += t_hmac_sha1_openssl
|
||||
TESTS += t_hmac_sha1_openssl t_hmac_sha256_openssl t_hmac_sha384_openssl t_hmac_sha512_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)
|
||||
t_hmac_sha256_openssl_SOURCES = t_hmac_sha256.c
|
||||
t_hmac_sha256_openssl_CFLAGS = $(OPENSSL_INCLUDES) $(OPENSSL_CFLAGS)
|
||||
t_hmac_sha256_openssl_LDADD = $(OPENSSL_LDADD)
|
||||
t_hmac_sha384_openssl_SOURCES = t_hmac_sha384.c
|
||||
t_hmac_sha384_openssl_CFLAGS = $(OPENSSL_INCLUDES) $(OPENSSL_CFLAGS)
|
||||
t_hmac_sha384_openssl_LDADD = $(OPENSSL_LDADD)
|
||||
t_hmac_sha512_openssl_SOURCES = t_hmac_sha512.c
|
||||
t_hmac_sha512_openssl_CFLAGS = $(OPENSSL_INCLUDES) $(OPENSSL_CFLAGS)
|
||||
t_hmac_sha512_openssl_LDADD = $(OPENSSL_LDADD)
|
||||
endif
|
||||
|
||||
check_PROGRAMS = $(TESTS)
|
||||
|
|
197
t/t_hmac_sha256.c
Normal file
197
t/t_hmac_sha256.c
Normal file
|
@ -0,0 +1,197 @@
|
|||
/*-
|
||||
* Copyright (c) 2012 The University of Oslo
|
||||
* Copyright (c) 2012 Dag-Erling Smørgrav
|
||||
* 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.
|
||||
*
|
||||
* Author: Dag-Erling Smørgrav <des@des.no>
|
||||
* Sponsor: the University of Oslo
|
||||
*
|
||||
* $Cryb$
|
||||
*/
|
||||
|
||||
#include "cryb/impl.h"
|
||||
|
||||
#include <err.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "t.h"
|
||||
|
||||
#if WITH_OPENSSL
|
||||
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/hmac.h>
|
||||
|
||||
#define HMAC_SHA256_MAC_LEN 32
|
||||
|
||||
static void
|
||||
t_hmac_sha256_complete(const void *key, size_t keylen,
|
||||
const void *msg, size_t msglen, uint8_t *mac)
|
||||
{
|
||||
HMAC_CTX ctx;
|
||||
|
||||
HMAC_CTX_init(&ctx);
|
||||
HMAC_Init_ex(&ctx, key, keylen, EVP_sha256(), NULL);
|
||||
HMAC_Update(&ctx, msg, msglen);
|
||||
HMAC_Final(&ctx, mac, NULL);
|
||||
HMAC_CTX_cleanup(&ctx);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <cryb/sha256.h>
|
||||
#include <cryb/hmac.h>
|
||||
|
||||
#define t_hmac_sha256_complete(key, keylen, msg, msglen, mac) \
|
||||
hmac_sha256_complete(key, keylen, msg, msglen, mac)
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Test vectors from NIST CSRC
|
||||
* http://csrc.nist.gov/groups/ST/toolkit/examples.html
|
||||
*/
|
||||
static struct t_vector {
|
||||
const char *desc;
|
||||
const uint8_t *key;
|
||||
size_t keylen;
|
||||
const char *msg;
|
||||
const uint8_t mac[HMAC_SHA256_MAC_LEN];
|
||||
} t_hmac_sha256_vectors[] = {
|
||||
{
|
||||
"zero-length key, zero-length message",
|
||||
t_zero,
|
||||
0,
|
||||
"",
|
||||
{
|
||||
0xb6, 0x13, 0x67, 0x9a, 0x08, 0x14, 0xd9, 0xec,
|
||||
0x77, 0x2f, 0x95, 0xd7, 0x78, 0xc3, 0x5f, 0xc5,
|
||||
0xff, 0x16, 0x97, 0xc4, 0x93, 0x71, 0x56, 0x53,
|
||||
0xc6, 0xc7, 0x12, 0x14, 0x42, 0x92, 0xc5, 0xad,
|
||||
},
|
||||
},
|
||||
{
|
||||
"NIST CSRC example 1 (64-byte key)",
|
||||
t_seq8,
|
||||
64,
|
||||
"Sample message for keylen=blocklen",
|
||||
{
|
||||
0x8b, 0xb9, 0xa1, 0xdb, 0x98, 0x06, 0xf2, 0x0d,
|
||||
0xf7, 0xf7, 0x7b, 0x82, 0x13, 0x8c, 0x79, 0x14,
|
||||
0xd1, 0x74, 0xd5, 0x9e, 0x13, 0xdc, 0x4d, 0x01,
|
||||
0x69, 0xc9, 0x05, 0x7b, 0x13, 0x3e, 0x1d, 0x62,
|
||||
},
|
||||
},
|
||||
{
|
||||
"NIST CSRC example 2 (32-byte key)",
|
||||
t_seq8,
|
||||
32,
|
||||
"Sample message for keylen<blocklen",
|
||||
{
|
||||
0xa2, 0x8c, 0xf4, 0x31, 0x30, 0xee, 0x69, 0x6a,
|
||||
0x98, 0xf1, 0x4a, 0x37, 0x67, 0x8b, 0x56, 0xbc,
|
||||
0xfc, 0xbd, 0xd9, 0xe5, 0xcf, 0x69, 0x71, 0x7f,
|
||||
0xec, 0xf5, 0x48, 0x0f, 0x0e, 0xbd, 0xf7, 0x90,
|
||||
},
|
||||
},
|
||||
{
|
||||
"NIST CSRC example 3 (100-byte key)",
|
||||
t_seq8,
|
||||
100,
|
||||
/* yes, the text should say keylen>blocklen */
|
||||
"Sample message for keylen=blocklen",
|
||||
{
|
||||
0xbd, 0xcc, 0xb6, 0xc7, 0x2d, 0xde, 0xad, 0xb5,
|
||||
0x00, 0xae, 0x76, 0x83, 0x86, 0xcb, 0x38, 0xcc,
|
||||
0x41, 0xc6, 0x3d, 0xbb, 0x08, 0x78, 0xdd, 0xb9,
|
||||
0xc7, 0xa3, 0x8a, 0x43, 0x1b, 0x78, 0x37, 0x8d,
|
||||
},
|
||||
},
|
||||
{
|
||||
"NIST CSRC example 4 (49-byte key)",
|
||||
t_seq8,
|
||||
49,
|
||||
"Sample message for keylen<blocklen, with truncated tag",
|
||||
{
|
||||
0x27, 0xa8, 0xb1, 0x57, 0x83, 0x9e, 0xfe, 0xac,
|
||||
0x98, 0xdf, 0x07, 0x0b, 0x33, 0x1d, 0x59, 0x36,
|
||||
0x18, 0xdd, 0xb9, 0x85, 0xd4, 0x03, 0xc0, 0xc7,
|
||||
0x86, 0xd2, 0x3b, 0x5d, 0x13, 0x2e, 0x57, 0xc7,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
/*
|
||||
* Unit test: compute the HMAC signature of the specified string with the
|
||||
* specified key and compare it to the expected result.
|
||||
*/
|
||||
static int
|
||||
t_hmac_sha256_vector(char **desc CRYB_UNUSED, void *arg)
|
||||
{
|
||||
struct t_vector *vector = (struct t_vector *)arg;
|
||||
uint8_t mac[HMAC_SHA256_MAC_LEN];
|
||||
|
||||
t_hmac_sha256_complete(vector->key, vector->keylen,
|
||||
(const uint8_t *)vector->msg, strlen(vector->msg),
|
||||
mac);
|
||||
if (memcmp(mac, vector->mac, HMAC_SHA256_MAC_LEN) != 0) {
|
||||
t_verbose("expected ");
|
||||
t_verbose_hex(vector->mac, HMAC_SHA256_MAC_LEN);
|
||||
t_verbose("\n");
|
||||
t_verbose("got ");
|
||||
t_verbose_hex(mac, HMAC_SHA256_MAC_LEN);
|
||||
t_verbose("\n");
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
* Boilerplate
|
||||
*/
|
||||
|
||||
int
|
||||
t_prepare(int argc, char *argv[])
|
||||
{
|
||||
int i, n;
|
||||
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
n = sizeof t_hmac_sha256_vectors / sizeof t_hmac_sha256_vectors[0];
|
||||
for (i = 0; i < n; ++i)
|
||||
t_add_test(t_hmac_sha256_vector, &t_hmac_sha256_vectors[i],
|
||||
t_hmac_sha256_vectors[i].desc);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
t_cleanup(void)
|
||||
{
|
||||
}
|
207
t/t_hmac_sha384.c
Normal file
207
t/t_hmac_sha384.c
Normal file
|
@ -0,0 +1,207 @@
|
|||
/*-
|
||||
* Copyright (c) 2012 The University of Oslo
|
||||
* Copyright (c) 2012 Dag-Erling Smørgrav
|
||||
* 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.
|
||||
*
|
||||
* Author: Dag-Erling Smørgrav <des@des.no>
|
||||
* Sponsor: the University of Oslo
|
||||
*
|
||||
* $Cryb$
|
||||
*/
|
||||
|
||||
#include "cryb/impl.h"
|
||||
|
||||
#include <err.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "t.h"
|
||||
|
||||
#if WITH_OPENSSL
|
||||
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/hmac.h>
|
||||
|
||||
#define HMAC_SHA384_MAC_LEN 48
|
||||
|
||||
static void
|
||||
t_hmac_sha384_complete(const void *key, size_t keylen,
|
||||
const void *msg, size_t msglen, uint8_t *mac)
|
||||
{
|
||||
HMAC_CTX ctx;
|
||||
|
||||
HMAC_CTX_init(&ctx);
|
||||
HMAC_Init_ex(&ctx, key, keylen, EVP_sha384(), NULL);
|
||||
HMAC_Update(&ctx, msg, msglen);
|
||||
HMAC_Final(&ctx, mac, NULL);
|
||||
HMAC_CTX_cleanup(&ctx);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <cryb/sha384.h>
|
||||
#include <cryb/hmac.h>
|
||||
|
||||
#define t_hmac_sha384_complete(key, keylen, msg, msglen, mac) \
|
||||
hmac_sha384_complete(key, keylen, msg, msglen, mac)
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Test vectors from NIST CSRC
|
||||
* http://csrc.nist.gov/groups/ST/toolkit/examples.html
|
||||
*/
|
||||
static struct t_vector {
|
||||
const char *desc;
|
||||
const uint8_t *key;
|
||||
size_t keylen;
|
||||
const char *msg;
|
||||
const uint8_t mac[HMAC_SHA384_MAC_LEN];
|
||||
} t_hmac_sha384_vectors[] = {
|
||||
{
|
||||
"zero-length key, zero-length message",
|
||||
t_zero,
|
||||
0,
|
||||
"",
|
||||
{
|
||||
0x6c, 0x1f, 0x2e, 0xe9, 0x38, 0xfa, 0xd2, 0xe2,
|
||||
0x4b, 0xd9, 0x12, 0x98, 0x47, 0x43, 0x82, 0xca,
|
||||
0x21, 0x8c, 0x75, 0xdb, 0x3d, 0x83, 0xe1, 0x14,
|
||||
0xb3, 0xd4, 0x36, 0x77, 0x76, 0xd1, 0x4d, 0x35,
|
||||
0x51, 0x28, 0x9e, 0x75, 0xe8, 0x20, 0x9c, 0xd4,
|
||||
0xb7, 0x92, 0x30, 0x28, 0x40, 0x23, 0x4a, 0xdc,
|
||||
},
|
||||
},
|
||||
{
|
||||
"NIST CSRC example 1 (128-byte key)",
|
||||
t_seq8,
|
||||
128,
|
||||
"Sample message for keylen=blocklen",
|
||||
{
|
||||
0x63, 0xc5, 0xda, 0xa5, 0xe6, 0x51, 0x84, 0x7c,
|
||||
0xa8, 0x97, 0xc9, 0x58, 0x14, 0xab, 0x83, 0x0b,
|
||||
0xed, 0xed, 0xc7, 0xd2, 0x5e, 0x83, 0xee, 0xf9,
|
||||
0x19, 0x5c, 0xd4, 0x58, 0x57, 0xa3, 0x7f, 0x44,
|
||||
0x89, 0x47, 0x85, 0x8f, 0x5a, 0xf5, 0x0c, 0xc2,
|
||||
0xb1, 0xb7, 0x30, 0xdd, 0xf2, 0x96, 0x71, 0xa9,
|
||||
},
|
||||
},
|
||||
{
|
||||
"NIST CSRC example 2 (48-byte key)",
|
||||
t_seq8,
|
||||
48,
|
||||
"Sample message for keylen<blocklen",
|
||||
{
|
||||
0x6e, 0xb2, 0x42, 0xbd, 0xbb, 0x58, 0x2c, 0xa1,
|
||||
0x7b, 0xeb, 0xfa, 0x48, 0x1b, 0x1e, 0x23, 0x21,
|
||||
0x14, 0x64, 0xd2, 0xb7, 0xf8, 0xc2, 0x0b, 0x9f,
|
||||
0xf2, 0x20, 0x16, 0x37, 0xb9, 0x36, 0x46, 0xaf,
|
||||
0x5a, 0xe9, 0xac, 0x31, 0x6e, 0x98, 0xdb, 0x45,
|
||||
0xd9, 0xca, 0xe7, 0x73, 0x67, 0x5e, 0xee, 0xd0,
|
||||
},
|
||||
},
|
||||
{
|
||||
"NIST CSRC example 3 (200-byte key)",
|
||||
t_seq8,
|
||||
200,
|
||||
/* yes, the text should say keylen>blocklen */
|
||||
"Sample message for keylen=blocklen",
|
||||
{
|
||||
0x5b, 0x66, 0x44, 0x36, 0xdf, 0x69, 0xb0, 0xca,
|
||||
0x22, 0x55, 0x12, 0x31, 0xa3, 0xf0, 0xa3, 0xd5,
|
||||
0xb4, 0xf9, 0x79, 0x91, 0x71, 0x3c, 0xfa, 0x84,
|
||||
0xbf, 0xf4, 0xd0, 0x79, 0x2e, 0xff, 0x96, 0xc2,
|
||||
0x7d, 0xcc, 0xbb, 0xb6, 0xf7, 0x9b, 0x65, 0xd5,
|
||||
0x48, 0xb4, 0x0e, 0x85, 0x64, 0xce, 0xf5, 0x94,
|
||||
},
|
||||
},
|
||||
{
|
||||
"NIST CSRC example 4 (49-byte key)",
|
||||
t_seq8,
|
||||
49,
|
||||
"Sample message for keylen<blocklen, with truncated tag",
|
||||
{
|
||||
0xc4, 0x81, 0x30, 0xd3, 0xdf, 0x70, 0x3d, 0xd7,
|
||||
0xcd, 0xaa, 0x56, 0x80, 0x0d, 0xfb, 0xd2, 0xba,
|
||||
0x24, 0x58, 0x32, 0x0e, 0x6e, 0x1f, 0x98, 0xfe,
|
||||
0xc8, 0xad, 0x9f, 0x57, 0xf4, 0x38, 0x00, 0xdf,
|
||||
0x36, 0x15, 0xce, 0xb1, 0x9a, 0xb6, 0x48, 0xe1,
|
||||
0xec, 0xdd, 0x8c, 0x73, 0x0a, 0xf9, 0x5c, 0x8a,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
/*
|
||||
* Unit test: compute the HMAC signature of the specified string with the
|
||||
* specified key and compare it to the expected result.
|
||||
*/
|
||||
static int
|
||||
t_hmac_sha384_vector(char **desc CRYB_UNUSED, void *arg)
|
||||
{
|
||||
struct t_vector *vector = (struct t_vector *)arg;
|
||||
uint8_t mac[HMAC_SHA384_MAC_LEN];
|
||||
|
||||
t_hmac_sha384_complete(vector->key, vector->keylen,
|
||||
(const uint8_t *)vector->msg, strlen(vector->msg),
|
||||
mac);
|
||||
if (memcmp(mac, vector->mac, HMAC_SHA384_MAC_LEN) != 0) {
|
||||
t_verbose("expected ");
|
||||
t_verbose_hex(vector->mac, HMAC_SHA384_MAC_LEN);
|
||||
t_verbose("\n");
|
||||
t_verbose("got ");
|
||||
t_verbose_hex(mac, HMAC_SHA384_MAC_LEN);
|
||||
t_verbose("\n");
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
* Boilerplate
|
||||
*/
|
||||
|
||||
int
|
||||
t_prepare(int argc, char *argv[])
|
||||
{
|
||||
int i, n;
|
||||
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
n = sizeof t_hmac_sha384_vectors / sizeof t_hmac_sha384_vectors[0];
|
||||
for (i = 0; i < n; ++i)
|
||||
t_add_test(t_hmac_sha384_vector, &t_hmac_sha384_vectors[i],
|
||||
t_hmac_sha384_vectors[i].desc);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
t_cleanup(void)
|
||||
{
|
||||
}
|
217
t/t_hmac_sha512.c
Normal file
217
t/t_hmac_sha512.c
Normal file
|
@ -0,0 +1,217 @@
|
|||
/*-
|
||||
* Copyright (c) 2012 The University of Oslo
|
||||
* Copyright (c) 2012 Dag-Erling Smørgrav
|
||||
* 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.
|
||||
*
|
||||
* Author: Dag-Erling Smørgrav <des@des.no>
|
||||
* Sponsor: the University of Oslo
|
||||
*
|
||||
* $Cryb$
|
||||
*/
|
||||
|
||||
#include "cryb/impl.h"
|
||||
|
||||
#include <err.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "t.h"
|
||||
|
||||
#if WITH_OPENSSL
|
||||
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/hmac.h>
|
||||
|
||||
#define HMAC_SHA512_MAC_LEN 64
|
||||
|
||||
static void
|
||||
t_hmac_sha512_complete(const void *key, size_t keylen,
|
||||
const void *msg, size_t msglen, uint8_t *mac)
|
||||
{
|
||||
HMAC_CTX ctx;
|
||||
|
||||
HMAC_CTX_init(&ctx);
|
||||
HMAC_Init_ex(&ctx, key, keylen, EVP_sha512(), NULL);
|
||||
HMAC_Update(&ctx, msg, msglen);
|
||||
HMAC_Final(&ctx, mac, NULL);
|
||||
HMAC_CTX_cleanup(&ctx);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <cryb/sha512.h>
|
||||
#include <cryb/hmac.h>
|
||||
|
||||
#define t_hmac_sha512_complete(key, keylen, msg, msglen, mac) \
|
||||
hmac_sha512_complete(key, keylen, msg, msglen, mac)
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Test vectors from NIST CSRC
|
||||
* http://csrc.nist.gov/groups/ST/toolkit/examples.html
|
||||
*/
|
||||
static struct t_vector {
|
||||
const char *desc;
|
||||
const uint8_t *key;
|
||||
size_t keylen;
|
||||
const char *msg;
|
||||
const uint8_t mac[HMAC_SHA512_MAC_LEN];
|
||||
} t_hmac_sha512_vectors[] = {
|
||||
{
|
||||
"zero-length key, zero-length message",
|
||||
t_zero,
|
||||
0,
|
||||
"",
|
||||
{
|
||||
0xb9, 0x36, 0xce, 0xe8, 0x6c, 0x9f, 0x87, 0xaa,
|
||||
0x5d, 0x3c, 0x6f, 0x2e, 0x84, 0xcb, 0x5a, 0x42,
|
||||
0x39, 0xa5, 0xfe, 0x50, 0x48, 0x0a, 0x6e, 0xc6,
|
||||
0x6b, 0x70, 0xab, 0x5b, 0x1f, 0x4a, 0xc6, 0x73,
|
||||
0x0c, 0x6c, 0x51, 0x54, 0x21, 0xb3, 0x27, 0xec,
|
||||
0x1d, 0x69, 0x40, 0x2e, 0x53, 0xdf, 0xb4, 0x9a,
|
||||
0xd7, 0x38, 0x1e, 0xb0, 0x67, 0xb3, 0x38, 0xfd,
|
||||
0x7b, 0x0c, 0xb2, 0x22, 0x47, 0x22, 0x5d, 0x47,
|
||||
},
|
||||
},
|
||||
{
|
||||
"NIST CSRC example 1 (128-byte key)",
|
||||
t_seq8,
|
||||
128,
|
||||
"Sample message for keylen=blocklen",
|
||||
{
|
||||
0xfc, 0x25, 0xe2, 0x40, 0x65, 0x8c, 0xa7, 0x85,
|
||||
0xb7, 0xa8, 0x11, 0xa8, 0xd3, 0xf7, 0xb4, 0xca,
|
||||
0x48, 0xcf, 0xa2, 0x6a, 0x8a, 0x36, 0x6b, 0xf2,
|
||||
0xcd, 0x1f, 0x83, 0x6b, 0x05, 0xfc, 0xb0, 0x24,
|
||||
0xbd, 0x36, 0x85, 0x30, 0x81, 0x81, 0x1d, 0x6c,
|
||||
0xea, 0x42, 0x16, 0xeb, 0xad, 0x79, 0xda, 0x1c,
|
||||
0xfc, 0xb9, 0x5e, 0xa4, 0x58, 0x6b, 0x8a, 0x0c,
|
||||
0xe3, 0x56, 0x59, 0x6a, 0x55, 0xfb, 0x13, 0x47,
|
||||
},
|
||||
},
|
||||
{
|
||||
"NIST CSRC example 2 (64-byte key)",
|
||||
t_seq8,
|
||||
64,
|
||||
"Sample message for keylen<blocklen",
|
||||
{
|
||||
0xfd, 0x44, 0xc1, 0x8b, 0xda, 0x0b, 0xb0, 0xa6,
|
||||
0xce, 0x0e, 0x82, 0xb0, 0x31, 0xbf, 0x28, 0x18,
|
||||
0xf6, 0x53, 0x9b, 0xd5, 0x6e, 0xc0, 0x0b, 0xdc,
|
||||
0x10, 0xa8, 0xa2, 0xd7, 0x30, 0xb3, 0x63, 0x4d,
|
||||
0xe2, 0x54, 0x5d, 0x63, 0x9b, 0x0f, 0x2c, 0xf7,
|
||||
0x10, 0xd0, 0x69, 0x2c, 0x72, 0xa1, 0x89, 0x6f,
|
||||
0x1f, 0x21, 0x1c, 0x2b, 0x92, 0x2d, 0x1a, 0x96,
|
||||
0xc3, 0x92, 0xe0, 0x7e, 0x7e, 0xa9, 0xfe, 0xdc,
|
||||
},
|
||||
},
|
||||
{
|
||||
"NIST CSRC example 3 (200-byte key)",
|
||||
t_seq8,
|
||||
200,
|
||||
/* yes, the text should say keylen>blocklen */
|
||||
"Sample message for keylen=blocklen",
|
||||
{
|
||||
0xd9, 0x3e, 0xc8, 0xd2, 0xde, 0x1a, 0xd2, 0xa9,
|
||||
0x95, 0x7c, 0xb9, 0xb8, 0x3f, 0x14, 0xe7, 0x6a,
|
||||
0xd6, 0xb5, 0xe0, 0xcc, 0xe2, 0x85, 0x07, 0x9a,
|
||||
0x12, 0x7d, 0x3b, 0x14, 0xbc, 0xcb, 0x7a, 0xa7,
|
||||
0x28, 0x6d, 0x4a, 0xc0, 0xd4, 0xce, 0x64, 0x21,
|
||||
0x5f, 0x2b, 0xc9, 0xe6, 0x87, 0x0b, 0x33, 0xd9,
|
||||
0x74, 0x38, 0xbe, 0x4a, 0xaa, 0x20, 0xcd, 0xa5,
|
||||
0xc5, 0xa9, 0x12, 0xb4, 0x8b, 0x8e, 0x27, 0xf3,
|
||||
},
|
||||
},
|
||||
{
|
||||
"NIST CSRC example 4 (49-byte key)",
|
||||
t_seq8,
|
||||
49,
|
||||
"Sample message for keylen<blocklen, with truncated tag",
|
||||
{
|
||||
0x00, 0xf3, 0xe9, 0xa7, 0x7b, 0xb0, 0xf0, 0x6d,
|
||||
0xe1, 0x5f, 0x16, 0x06, 0x03, 0xe4, 0x2b, 0x50,
|
||||
0x28, 0x75, 0x88, 0x08, 0x59, 0x66, 0x64, 0xc0,
|
||||
0x3e, 0x1a, 0xb8, 0xfb, 0x2b, 0x07, 0x67, 0x78,
|
||||
0x05, 0x63, 0xae, 0xdc, 0x64, 0x49, 0x60, 0xd4,
|
||||
0xf0, 0xc0, 0xc5, 0xd2, 0x39, 0xf6, 0x7a, 0x2a,
|
||||
0x61, 0xb1, 0x41, 0xe8, 0xc8, 0x71, 0xf3, 0xd4,
|
||||
0x0d, 0xb2, 0xc6, 0x05, 0x58, 0x8d, 0xab, 0x92,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
/*
|
||||
* Unit test: compute the HMAC signature of the specified string with the
|
||||
* specified key and compare it to the expected result.
|
||||
*/
|
||||
static int
|
||||
t_hmac_sha512_vector(char **desc CRYB_UNUSED, void *arg)
|
||||
{
|
||||
struct t_vector *vector = (struct t_vector *)arg;
|
||||
uint8_t mac[HMAC_SHA512_MAC_LEN];
|
||||
|
||||
t_hmac_sha512_complete(vector->key, vector->keylen,
|
||||
(const uint8_t *)vector->msg, strlen(vector->msg),
|
||||
mac);
|
||||
if (memcmp(mac, vector->mac, HMAC_SHA512_MAC_LEN) != 0) {
|
||||
t_verbose("expected ");
|
||||
t_verbose_hex(vector->mac, HMAC_SHA512_MAC_LEN);
|
||||
t_verbose("\n");
|
||||
t_verbose("got ");
|
||||
t_verbose_hex(mac, HMAC_SHA512_MAC_LEN);
|
||||
t_verbose("\n");
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
* Boilerplate
|
||||
*/
|
||||
|
||||
int
|
||||
t_prepare(int argc, char *argv[])
|
||||
{
|
||||
int i, n;
|
||||
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
n = sizeof t_hmac_sha512_vectors / sizeof t_hmac_sha512_vectors[0];
|
||||
for (i = 0; i < n; ++i)
|
||||
t_add_test(t_hmac_sha512_vector, &t_hmac_sha512_vectors[i],
|
||||
t_hmac_sha512_vectors[i].desc);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
t_cleanup(void)
|
||||
{
|
||||
}
|
Loading…
Reference in a new issue