1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-23 20:15:19 -07:00

Since auth_hmac_sha512256 is just truncated hmac_sha512, expose hmac_sha512

This commit is contained in:
Frank Denis 2014-04-14 21:14:59 -07:00 committed by evoskuil
parent d83a528953
commit 3c322ca7b9
13 changed files with 245 additions and 94 deletions

View File

@ -318,6 +318,7 @@
<ClInclude Include="src\libsodium\include\sodium\core.h" />
<ClInclude Include="src\libsodium\include\sodium\crypto_auth.h" />
<ClInclude Include="src\libsodium\include\sodium\crypto_auth_hmacsha256.h" />
<ClInclude Include="src\libsodium\include\sodium\crypto_auth_hmacsha512.h" />
<ClInclude Include="src\libsodium\include\sodium\crypto_auth_hmacsha512256.h" />
<ClInclude Include="src\libsodium\include\sodium\crypto_box.h" />
<ClInclude Include="src\libsodium\include\sodium\crypto_box_curve25519xsalsa20poly1305.h" />
@ -374,6 +375,9 @@
<ClCompile Include="src\libsodium\crypto_auth\hmacsha256\auth_hmacsha256_api.c" />
<ClCompile Include="src\libsodium\crypto_auth\hmacsha256\cp\hmac_hmacsha256.c" />
<ClCompile Include="src\libsodium\crypto_auth\hmacsha256\cp\verify_hmacsha256.c" />
<ClCompile Include="src\libsodium\crypto_auth\hmacsha512\auth_hmacsha512_api.c" />
<ClCompile Include="src\libsodium\crypto_auth\hmacsha512\cp\hmac_hmacsha512.c" />
<ClCompile Include="src\libsodium\crypto_auth\hmacsha512\cp\verify_hmacsha512.c" />
<ClCompile Include="src\libsodium\crypto_auth\hmacsha512256\auth_hmacsha512256_api.c" />
<ClCompile Include="src\libsodium\crypto_auth\hmacsha512256\cp\hmac_hmacsha512256.c" />
<ClCompile Include="src\libsodium\crypto_auth\hmacsha512256\cp\verify_hmacsha512256.c" />

View File

@ -24,6 +24,9 @@
<ClInclude Include="src\libsodium\include\sodium\crypto_auth_hmacsha256.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\libsodium\include\sodium\crypto_auth_hmacsha512.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\libsodium\include\sodium\crypto_auth_hmacsha512256.h">
<Filter>Header Files</Filter>
</ClInclude>
@ -190,6 +193,9 @@
<ClCompile Include="src\libsodium\crypto_auth\hmacsha256\auth_hmacsha256_api.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\libsodium\crypto_auth\hmacsha512\auth_hmacsha512_api.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\libsodium\crypto_auth\hmacsha512256\auth_hmacsha512256_api.c">
<Filter>Source Files</Filter>
</ClCompile>
@ -199,12 +205,18 @@
<ClCompile Include="src\libsodium\crypto_auth\hmacsha256\cp\hmac_hmacsha256.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\libsodium\crypto_auth\hmacsha512\cp\hmac_hmacsha512.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\libsodium\crypto_auth\hmacsha512256\cp\hmac_hmacsha512256.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\libsodium\crypto_auth\hmacsha256\ref\verify_hmacsha256.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\libsodium\crypto_auth\hmacsha512\cp\verify_hmacsha512.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\libsodium\crypto_auth\hmacsha512256\cp\verify_hmacsha512256.c">
<Filter>Source Files</Filter>
</ClCompile>

View File

@ -7,6 +7,10 @@ libsodium_la_SOURCES = \
crypto_auth/hmacsha256/cp/api.h \
crypto_auth/hmacsha256/cp/hmac_hmacsha256.c \
crypto_auth/hmacsha256/cp/verify_hmacsha256.c \
crypto_auth/hmacsha512/auth_hmacsha512_api.c \
crypto_auth/hmacsha512/cp/api.h \
crypto_auth/hmacsha512/cp/hmac_hmacsha512.c \
crypto_auth/hmacsha512/cp/verify_hmacsha512.c \
crypto_auth/hmacsha512256/auth_hmacsha512256_api.c \
crypto_auth/hmacsha512256/cp/api.h \
crypto_auth/hmacsha512256/cp/hmac_hmacsha512256.c \

View File

@ -0,0 +1,16 @@
#include "crypto_auth_hmacsha512.h"
size_t
crypto_auth_hmacsha512_bytes(void) {
return crypto_auth_hmacsha512_BYTES;
}
size_t
crypto_auth_hmacsha512_keybytes(void) {
return crypto_auth_hmacsha512_KEYBYTES;
}
const char *
crypto_auth_hmacsha512_primitive(void) {
return "hmacsha512";
}

View File

@ -0,0 +1,10 @@
#include "crypto_auth_hmacsha512.h"
#define crypto_auth crypto_auth_hmacsha512
#define crypto_auth_verify crypto_auth_hmacsha512_verify
#define crypto_auth_BYTES crypto_auth_hmacsha512_BYTES
#define crypto_auth_KEYBYTES crypto_auth_hmacsha512_KEYBYTES
#define crypto_auth_PRIMITIVE "hmacsha512"
#define crypto_auth_IMPLEMENTATION crypto_auth_hmacsha512_IMPLEMENTATION
#define crypto_auth_VERSION crypto_auth_hmacsha512_VERSION

View File

@ -0,0 +1,110 @@
/*-
* Copyright 2005,2007,2009 Colin Percival
* 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 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.
*
*/
#include "api.h"
#include "crypto_auth_hmacsha512.h"
#include "crypto_hash_sha512.h"
#include "utils.h"
#include <sys/types.h>
#include <stdint.h>
#include <string.h>
int
crypto_auth_hmacsha512_init(crypto_auth_hmacsha512_state *state,
const unsigned char *key,
size_t keylen)
{
unsigned char pad[128];
unsigned char khash[64];
size_t i;
if (keylen > 128) {
crypto_hash_sha512_init(&state->ictx);
crypto_hash_sha512_update(&state->ictx, key, keylen);
crypto_hash_sha512_final(&state->ictx, khash);
key = khash;
keylen = 64;
}
crypto_hash_sha512_init(&state->ictx);
memset(pad, 0x36, 128);
for (i = 0; i < keylen; i++) {
pad[i] ^= key[i];
}
crypto_hash_sha512_update(&state->ictx, pad, 128);
crypto_hash_sha512_init(&state->octx);
memset(pad, 0x5c, 128);
for (i = 0; i < keylen; i++) {
pad[i] ^= key[i];
}
crypto_hash_sha512_update(&state->octx, pad, 128);
sodium_memzero((void *) khash, 64);
return 0;
}
int
crypto_auth_hmacsha512_update(crypto_auth_hmacsha512_state *state,
const unsigned char *in,
unsigned long long inlen)
{
crypto_hash_sha512_update(&state->ictx, in, inlen);
return 0;
}
int
crypto_auth_hmacsha512_final(crypto_auth_hmacsha512_state *state,
unsigned char *out)
{
unsigned char ihash[64];
crypto_hash_sha512_final(&state->ictx, ihash);
crypto_hash_sha512_update(&state->octx, ihash, 64);
crypto_hash_sha512_final(&state->octx, out);
sodium_memzero((void *) ihash, 64);
return 0;
}
int
crypto_auth(unsigned char *out, const unsigned char *in,
unsigned long long inlen, const unsigned char *k)
{
crypto_auth_hmacsha512_state state;
crypto_auth_hmacsha512_init(&state, k, crypto_auth_KEYBYTES);
crypto_auth_hmacsha512_update(&state, in, inlen);
crypto_auth_hmacsha512_final(&state, out);
return 0;
}

View File

@ -0,0 +1,10 @@
#include "api.h"
#include "crypto_verify_64.h"
int crypto_auth_verify(const unsigned char *h, const unsigned char *in,
unsigned long long inlen, const unsigned char *k)
{
unsigned char correct[64];
crypto_auth(correct,in,inlen,k);
return crypto_verify_64(h,correct);
}

View File

@ -1,33 +1,7 @@
/*-
* Copyright 2005,2007,2009 Colin Percival
* 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 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.
*
*/
#include "api.h"
#include "crypto_auth_hmacsha512256.h"
#include "crypto_auth_hmacsha512.h"
#include "crypto_hash_sha512.h"
#include "utils.h"
@ -36,66 +10,6 @@
#include <stdint.h>
#include <string.h>
int
crypto_auth_hmacsha512_init(crypto_auth_hmacsha512_state *state,
const unsigned char *key,
size_t keylen)
{
unsigned char pad[128];
unsigned char khash[64];
size_t i;
if (keylen > 128) {
crypto_hash_sha512_init(&state->ictx);
crypto_hash_sha512_update(&state->ictx, key, keylen);
crypto_hash_sha512_final(&state->ictx, khash);
key = khash;
keylen = 64;
}
crypto_hash_sha512_init(&state->ictx);
memset(pad, 0x36, 128);
for (i = 0; i < keylen; i++) {
pad[i] ^= key[i];
}
crypto_hash_sha512_update(&state->ictx, pad, 128);
crypto_hash_sha512_init(&state->octx);
memset(pad, 0x5c, 128);
for (i = 0; i < keylen; i++) {
pad[i] ^= key[i];
}
crypto_hash_sha512_update(&state->octx, pad, 128);
sodium_memzero((void *) khash, 64);
return 0;
}
int
crypto_auth_hmacsha512_update(crypto_auth_hmacsha512_state *state,
const unsigned char *in,
unsigned long long inlen)
{
crypto_hash_sha512_update(&state->ictx, in, inlen);
return 0;
}
int
crypto_auth_hmacsha512_final(crypto_auth_hmacsha512_state *state,
unsigned char *out)
{
unsigned char ihash[64];
crypto_hash_sha512_final(&state->ictx, ihash);
crypto_hash_sha512_update(&state->octx, ihash, 64);
crypto_hash_sha512_final(&state->octx, out);
sodium_memzero((void *) ihash, 64);
return 0;
}
int
crypto_auth_hmacsha512256_init(crypto_auth_hmacsha512256_state *state,
const unsigned char *key,

View File

@ -1,7 +1,8 @@
#include "api.h"
#include "crypto_verify_32.h"
int crypto_auth_verify(const unsigned char *h,const unsigned char *in,unsigned long long inlen,const unsigned char *k)
int crypto_auth_verify(const unsigned char *h, const unsigned char *in,
unsigned long long inlen, const unsigned char *k)
{
unsigned char correct[32];
crypto_auth(correct,in,inlen,k);

View File

@ -4,6 +4,7 @@ SODIUM_EXPORT = \
sodium/core.h \
sodium/crypto_auth.h \
sodium/crypto_auth_hmacsha256.h \
sodium/crypto_auth_hmacsha512.h \
sodium/crypto_auth_hmacsha512256.h \
sodium/crypto_box.h \
sodium/crypto_box_curve25519xsalsa20poly1305.h \

View File

@ -5,6 +5,7 @@
#include <sodium/core.h>
#include <sodium/crypto_auth.h>
#include <sodium/crypto_auth_hmacsha256.h>
#include <sodium/crypto_auth_hmacsha512.h>
#include <sodium/crypto_auth_hmacsha512256.h>
#include <sodium/crypto_box.h>
#include <sodium/crypto_box_curve25519xsalsa20poly1305.h>

View File

@ -0,0 +1,59 @@
#ifndef crypto_auth_hmacsha512_H
#define crypto_auth_hmacsha512_H
#include <stddef.h>
#include "crypto_hash_sha512.h"
#include "export.h"
#define crypto_auth_hmacsha512_BYTES 64U
#define crypto_auth_hmacsha512_KEYBYTES 32U
#ifdef __cplusplus
# if __GNUC__
# pragma GCC diagnostic ignored "-Wlong-long"
# endif
extern "C" {
#endif
typedef struct crypto_auth_hmacsha512_state {
crypto_hash_sha512_state ictx;
crypto_hash_sha512_state octx;
} crypto_auth_hmacsha512_state;
SODIUM_EXPORT
size_t crypto_auth_hmacsha512_bytes(void);
SODIUM_EXPORT
size_t crypto_auth_hmacsha512_keybytes(void);
SODIUM_EXPORT
const char * crypto_auth_hmacsha512_primitive(void);
SODIUM_EXPORT
int crypto_auth_hmacsha512(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *);
SODIUM_EXPORT
int crypto_auth_hmacsha512_verify(const unsigned char *,const unsigned char *,unsigned long long,const unsigned char *);
SODIUM_EXPORT
int crypto_auth_hmacsha512_init(crypto_auth_hmacsha512_state *state,
const unsigned char *key,
size_t keylen);
SODIUM_EXPORT
int crypto_auth_hmacsha512_update(crypto_auth_hmacsha512_state *state,
const unsigned char *in,
unsigned long long inlen);
SODIUM_EXPORT
int crypto_auth_hmacsha512_final(crypto_auth_hmacsha512_state *state,
unsigned char *out);
#ifdef __cplusplus
}
#endif
#define crypto_auth_hmacsha512_cp crypto_auth_hmacsha512
#define crypto_auth_hmacsha512_cp_verify crypto_auth_hmacsha512_verify
#endif

View File

@ -2,7 +2,7 @@
#define crypto_auth_hmacsha512256_H
#include <stddef.h>
#include "crypto_hash_sha512.h"
#include "crypto_auth_hmacsha512.h"
#include "export.h"
#define crypto_auth_hmacsha512256_BYTES 32U
@ -15,11 +15,6 @@
extern "C" {
#endif
typedef struct crypto_auth_hmacsha512_state {
crypto_hash_sha512_state ictx;
crypto_hash_sha512_state octx;
} crypto_auth_hmacsha512_state;
typedef struct crypto_auth_hmacsha512_state crypto_auth_hmacsha512256_state;
SODIUM_EXPORT
@ -37,6 +32,20 @@ int crypto_auth_hmacsha512256(unsigned char *,const unsigned char *,unsigned lon
SODIUM_EXPORT
int crypto_auth_hmacsha512256_verify(const unsigned char *,const unsigned char *,unsigned long long,const unsigned char *);
SODIUM_EXPORT
int crypto_auth_hmacsha512256_init(crypto_auth_hmacsha512256_state *state,
const unsigned char *key,
size_t keylen);
SODIUM_EXPORT
int crypto_auth_hmacsha512256_update(crypto_auth_hmacsha512256_state *state,
const unsigned char *in,
unsigned long long inlen);
SODIUM_EXPORT
int crypto_auth_hmacsha512256_final(crypto_auth_hmacsha512256_state *state,
unsigned char *out);
#ifdef __cplusplus
}
#endif