Let crypto_pwhash_scryptxsalsa208sha256 use crypto_auth_hmacsha256

This commit is contained in:
Frank Denis
2014-04-10 23:12:27 -07:00
parent 9fa62b38a4
commit a2fc728956
11 changed files with 226 additions and 225 deletions
+2 -2
View File
@@ -65,8 +65,8 @@ libsodium_la_SOURCES = \
crypto_pwhash/scryptxsalsa208sha256/crypto_scrypt-common.c \
crypto_pwhash/scryptxsalsa208sha256/crypto_scrypt.h \
crypto_pwhash/scryptxsalsa208sha256/scrypt_platform.c \
crypto_pwhash/scryptxsalsa208sha256/sha256.c \
crypto_pwhash/scryptxsalsa208sha256/sha256.h \
crypto_pwhash/scryptxsalsa208sha256/pbkdf2-sha256.c \
crypto_pwhash/scryptxsalsa208sha256/pbkdf2-sha256.h \
crypto_pwhash/scryptxsalsa208sha256/sysendian.h \
crypto_pwhash/scryptxsalsa208sha256/nosse/pwhash_scryptxsalsa208sha256.c \
crypto_pwhash/scryptxsalsa208sha256/sse/pwhash_scryptxsalsa208sha256.c \
@@ -38,21 +38,8 @@
#include <stdlib.h>
#include <string.h>
typedef struct crypto_hmac_sha256_state {
crypto_hash_sha256_state ictx;
crypto_hash_sha256_state octx;
} crypto_hmac_sha256_state;
void _SHA256_Init(crypto_hash_sha256_state *);
void _SHA256_Update(crypto_hash_sha256_state *, const void *, size_t);
void _SHA256_Final(unsigned char [32], crypto_hash_sha256_state *);
static void HMAC__SHA256_Init(crypto_hmac_sha256_state *, const void *, size_t);
static void HMAC__SHA256_Update(crypto_hmac_sha256_state *, const void *, size_t);
static void HMAC__SHA256_Final(unsigned char [32], crypto_hmac_sha256_state *);
static void
HMAC__SHA256_Init(crypto_hmac_sha256_state * ctx, const void * _K, size_t Klen)
HMAC__SHA256_Init(crypto_auth_hmacsha256_state * ctx, const void * _K, size_t Klen)
{
unsigned char pad[64];
unsigned char khash[32];
@@ -60,55 +47,88 @@ HMAC__SHA256_Init(crypto_hmac_sha256_state * ctx, const void * _K, size_t Klen)
size_t i;
if (Klen > 64) {
_SHA256_Init(&ctx->ictx);
_SHA256_Update(&ctx->ictx, K, Klen);
_SHA256_Final(khash, &ctx->ictx);
crypto_hash_sha256_init(&ctx->ictx);
crypto_hash_sha256_update(&ctx->ictx, K, Klen);
crypto_hash_sha256_final(&ctx->ictx, khash);
K = khash;
Klen = 32;
}
_SHA256_Init(&ctx->ictx);
crypto_hash_sha256_init(&ctx->ictx);
memset(pad, 0x36, 64);
for (i = 0; i < Klen; i++) {
pad[i] ^= K[i];
}
_SHA256_Update(&ctx->ictx, pad, 64);
crypto_hash_sha256_update(&ctx->ictx, pad, 64);
_SHA256_Init(&ctx->octx);
crypto_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);
crypto_hash_sha256_update(&ctx->octx, pad, 64);
sodium_memzero(khash, 32);
sodium_memzero((void *) khash, 32);
}
static void
HMAC__SHA256_Update(crypto_hmac_sha256_state * ctx, const void *in, size_t len)
HMAC__SHA256_Update(crypto_auth_hmacsha256_state * ctx, const void *in, size_t len)
{
_SHA256_Update(&ctx->ictx, in, len);
crypto_hash_sha256_update(&ctx->ictx, in, len);
}
static void
HMAC__SHA256_Final(unsigned char digest[32], crypto_hmac_sha256_state * ctx)
HMAC__SHA256_Final(unsigned char digest[32], crypto_auth_hmacsha256_state * ctx)
{
unsigned char ihash[32];
_SHA256_Final(ihash, &ctx->ictx);
_SHA256_Update(&ctx->octx, ihash, 32);
_SHA256_Final(digest, &ctx->octx);
crypto_hash_sha256_final(&ctx->ictx, ihash);
crypto_hash_sha256_update(&ctx->octx, ihash, 32);
crypto_hash_sha256_final(&ctx->octx, digest);
sodium_memzero(ihash, 32);
sodium_memzero((void *) ihash, 32);
}
int
crypto_auth_hmacsha256_init(crypto_auth_hmacsha256_state *state,
const unsigned char *key,
const size_t keylen)
{
HMAC__SHA256_Init(state, key, keylen);
return 0;
}
int
crypto_auth_hmacsha256_update(crypto_auth_hmacsha256_state *state,
const unsigned char *in,
unsigned long long inlen)
{
if (inlen > SIZE_MAX) {
sodium_memzero((void *) state, sizeof *state);
return -1;
}
HMAC__SHA256_Update(state, (const void *) in, (size_t) inlen);
return 0;
}
int
crypto_auth_hmacsha256_final(crypto_auth_hmacsha256_state *state,
unsigned char *out)
{
HMAC__SHA256_Final(out, state);
return 0;
}
int
crypto_auth(unsigned char *out, const unsigned char *in,
unsigned long long inlen, const unsigned char *k)
{
crypto_hmac_sha256_state ctx;
crypto_auth_hmacsha256_state ctx;
if (inlen > SIZE_MAX) {
memset(out, 0, crypto_auth_BYTES);
sodium_memzero((void *) out, crypto_auth_BYTES);
return -1;
}
HMAC__SHA256_Init(&ctx, k, crypto_auth_KEYBYTES);
@@ -2,6 +2,9 @@
#include "crypto_hash_sha256.h"
#define crypto_hash crypto_hash_sha256
#define crypto_hash_init crypto_hash_sha256_init
#define crypto_hash_update crypto_hash_sha256_update
#define crypto_hash_final crypto_hash_sha256_final
#define crypto_hash_BYTES crypto_hash_sha256_BYTES
#define crypto_hash_PRIMITIVE "sha256"
#define crypto_hash_IMPLEMENTATION crypto_hash_sha256_IMPLEMENTATION
@@ -191,8 +191,8 @@ SHA256_Transform(uint32_t * state, const unsigned char block[64])
state[i] += S[i];
}
sodium_memzero(W, 256);
sodium_memzero(S, 32);
sodium_memzero((void *) W, 256);
sodium_memzero((void *) S, 32);
t0 = t1 = 0;
}
@@ -273,22 +273,52 @@ _SHA256_Final(unsigned char digest[32], crypto_hash_sha256_state * ctx)
{
SHA256_Pad(ctx);
be32enc_vect(digest, ctx->state, 32);
sodium_memzero((void *)ctx, sizeof(*ctx));
sodium_memzero((void *) ctx, sizeof *ctx);
}
int
crypto_hash_sha256_init(crypto_hash_sha256_state *state)
{
_SHA256_Init(state);
return 0;
}
int
crypto_hash_sha256_update(crypto_hash_sha256_state *state,
const unsigned char *in,
unsigned long long inlen)
{
if (inlen > SIZE_MAX) {
sodium_memzero(state, sizeof *state);
return -1;
}
_SHA256_Update(state, (const void *) in, (size_t) inlen);
return 0;
}
int
crypto_hash_sha256_final(crypto_hash_sha256_state *state,
unsigned char *out)
{
_SHA256_Final(out, state);
return 0;
}
int
crypto_hash(unsigned char *out, const unsigned char *in,
unsigned long long inlen)
{
crypto_hash_sha256_state ctx;
crypto_hash_sha256_state state;
if (inlen > SIZE_MAX) {
memset(out, 0, crypto_hash_BYTES);
crypto_hash_sha256_init(&state);
if (crypto_hash_sha256_update(&state, (const void *) in, inlen) != 0) {
sodium_memzero(out, crypto_hash_BYTES);
return -1;
}
_SHA256_Init(&ctx);
_SHA256_Update(&ctx, (const void *) in, (size_t) inlen);
_SHA256_Final(out, &ctx);
crypto_hash_sha256_final(&state, out);
return 0;
}
@@ -33,7 +33,7 @@
#include <stdlib.h>
#include <string.h>
#include "../sha256.h"
#include "../pbkdf2-sha256.h"
#include "../sysendian.h"
#include "../crypto_scrypt.h"
@@ -0,0 +1,94 @@
/*-
* 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 <sys/types.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "crypto_auth_hmacsha256.h"
#include "pbkdf2-sha256.h"
#include "utils.h"
static inline void
be32enc(void *pp, uint32_t x)
{
uint8_t * p = (uint8_t *)pp;
p[3] = x & 0xff;
p[2] = (x >> 8) & 0xff;
p[1] = (x >> 16) & 0xff;
p[0] = (x >> 24) & 0xff;
}
/**
* PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
* Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
* write the output to buf. The value dkLen must be at most 32 * (2^32 - 1).
*/
void
PBKDF2_SHA256(const uint8_t * passwd, size_t passwdlen, const uint8_t * salt,
size_t saltlen, uint64_t c, uint8_t * buf, size_t dkLen)
{
crypto_auth_hmacsha256_state PShctx, hctx;
size_t i;
uint8_t ivec[4];
uint8_t U[32];
uint8_t T[32];
uint64_t j;
int k;
size_t clen;
crypto_auth_hmacsha256_init(&PShctx, passwd, passwdlen);
crypto_auth_hmacsha256_update(&PShctx, salt, saltlen);
for (i = 0; i * 32 < dkLen; i++) {
be32enc(ivec, (uint32_t)(i + 1));
memcpy(&hctx, &PShctx, sizeof(crypto_auth_hmacsha256_state));
crypto_auth_hmacsha256_update(&hctx, ivec, 4);
crypto_auth_hmacsha256_final(&hctx, U);
memcpy(T, U, 32);
for (j = 2; j <= c; j++) {
crypto_auth_hmacsha256_init(&hctx, passwd, passwdlen);
crypto_auth_hmacsha256_update(&hctx, U, 32);
crypto_auth_hmacsha256_final(&hctx, U);
for (k = 0; k < 32; k++) {
T[k] ^= U[k];
}
}
clen = dkLen - i * 32;
if (clen > 32) {
clen = 32;
}
memcpy(&buf[i * 32], T, clen);
}
sodium_memzero((void *) &PShctx, sizeof PShctx);
}
@@ -23,7 +23,6 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD: src/lib/libmd/sha256.h,v 1.2 2006/01/17 15:35:56 phk Exp $
*/
#ifndef _SHA256_H_
@@ -33,27 +32,14 @@
#include <stdint.h>
#include "crypto_hash_sha256.h"
typedef struct HMAC_SHA256Context {
crypto_hash_sha256_state ictx;
crypto_hash_sha256_state octx;
} HMAC_SHA256_CTX;
void _SHA256_Init(crypto_hash_sha256_state *);
void _SHA256_Update(crypto_hash_sha256_state *, const void *, size_t);
void _SHA256_Final(unsigned char [32], crypto_hash_sha256_state *);
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(unsigned char [32], HMAC_SHA256_CTX *);
#include "crypto_auth_hmacsha256.h"
/**
* PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
* Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
* write the output to buf. The value dkLen must be at most 32 * (2^32 - 1).
*/
void PBKDF2_SHA256(const uint8_t *, size_t, const uint8_t *, size_t,
uint64_t, uint8_t *, size_t);
void PBKDF2_SHA256(const uint8_t *, size_t, const uint8_t *, size_t,
uint64_t, uint8_t *, size_t);
#endif /* !_SHA256_H_ */
@@ -1,164 +0,0 @@
/*-
* 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 <sys/types.h>
#include <stdint.h>
#include <string.h>
#include "sha256.h"
static inline void
be32enc(void *pp, uint32_t x)
{
uint8_t * p = (uint8_t *)pp;
p[3] = x & 0xff;
p[2] = (x >> 8) & 0xff;
p[1] = (x >> 16) & 0xff;
p[0] = (x >> 24) & 0xff;
}
/* Initialize an HMAC-SHA256 operation with the given key. */
void
HMAC__SHA256_Init(HMAC_SHA256_CTX * ctx, const void * _K, size_t Klen)
{
unsigned char pad[64];
unsigned char khash[32];
const unsigned char * K = (const unsigned char *) _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(khash, &ctx->ictx);
K = khash;
Klen = 32;
}
/* 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, 32);
}
/* Add bytes to the HMAC-SHA256 operation. */
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);
}
/* Finish an HMAC-SHA256 operation. */
void
HMAC__SHA256_Final(unsigned char digest[32], HMAC_SHA256_CTX * ctx)
{
unsigned char ihash[32];
/* Finish the inner SHA256 operation. */
_SHA256_Final(ihash, &ctx->ictx);
/* Feed the inner hash to the outer SHA256 operation. */
_SHA256_Update(&ctx->octx, ihash, 32);
/* Finish the outer SHA256 operation. */
_SHA256_Final(digest, &ctx->octx);
/* Clean the stack. */
memset(ihash, 0, 32);
}
/**
* PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
* Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
* write the output to buf. The value dkLen must be at most 32 * (2^32 - 1).
*/
void
PBKDF2_SHA256(const uint8_t * passwd, size_t passwdlen, const uint8_t * salt,
size_t saltlen, uint64_t c, uint8_t * buf, size_t dkLen)
{
HMAC_SHA256_CTX PShctx, hctx;
size_t i;
uint8_t ivec[4];
uint8_t U[32];
uint8_t T[32];
uint64_t j;
int k;
size_t clen;
/* Compute HMAC state after processing P and S. */
HMAC__SHA256_Init(&PShctx, passwd, passwdlen);
HMAC__SHA256_Update(&PShctx, salt, saltlen);
/* Iterate through the blocks. */
for (i = 0; i * 32 < dkLen; i++) {
/* Generate INT(i + 1). */
be32enc(ivec, (uint32_t)(i + 1));
/* Compute U_1 = PRF(P, S || INT(i)). */
memcpy(&hctx, &PShctx, sizeof(HMAC_SHA256_CTX));
HMAC__SHA256_Update(&hctx, ivec, 4);
HMAC__SHA256_Final(U, &hctx);
/* T_i = U_1 ... */
memcpy(T, U, 32);
for (j = 2; j <= c; j++) {
/* Compute U_j. */
HMAC__SHA256_Init(&hctx, passwd, passwdlen);
HMAC__SHA256_Update(&hctx, U, 32);
HMAC__SHA256_Final(U, &hctx);
/* ... xor U_j ... */
for (k = 0; k < 32; k++)
T[k] ^= U[k];
}
/* Copy as many bytes as necessary into buf. */
clen = dkLen - i * 32;
if (clen > 32)
clen = 32;
memcpy(&buf[i * 32], T, clen);
}
/* Clean PShctx, since we never called _Final on it. */
memset(&PShctx, 0, sizeof(HMAC_SHA256_CTX));
}
@@ -38,7 +38,7 @@
#include <stdlib.h>
#include <string.h>
#include "../sha256.h"
#include "../pbkdf2-sha256.h"
#include "../sysendian.h"
#include "../crypto_scrypt.h"
@@ -2,6 +2,7 @@
#define crypto_auth_hmacsha256_H
#include <stddef.h>
#include "crypto_hash_sha256.h"
#include "export.h"
#define crypto_auth_hmacsha256_BYTES 32U
@@ -14,6 +15,11 @@
extern "C" {
#endif
typedef struct crypto_auth_hmacsha256_state {
crypto_hash_sha256_state ictx;
crypto_hash_sha256_state octx;
} crypto_auth_hmacsha256_state;
SODIUM_EXPORT
size_t crypto_auth_hmacsha256_bytes(void);
@@ -29,6 +35,20 @@ int crypto_auth_hmacsha256(unsigned char *,const unsigned char *,unsigned long l
SODIUM_EXPORT
int crypto_auth_hmacsha256_verify(const unsigned char *,const unsigned char *,unsigned long long,const unsigned char *);
SODIUM_EXPORT
int crypto_auth_hmacsha256_init(crypto_auth_hmacsha256_state *state,
const unsigned char *key,
const size_t keylen);
SODIUM_EXPORT
int crypto_auth_hmacsha256_update(crypto_auth_hmacsha256_state *state,
const unsigned char *in,
unsigned long long inlen);
SODIUM_EXPORT
int crypto_auth_hmacsha256_final(crypto_auth_hmacsha256_state *state,
unsigned char *out);
#ifdef __cplusplus
}
#endif
@@ -32,6 +32,18 @@ const char * crypto_hash_sha256_primitive(void);
SODIUM_EXPORT
int crypto_hash_sha256(unsigned char *,const unsigned char *,unsigned long long);
SODIUM_EXPORT
int crypto_hash_sha256_init(crypto_hash_sha256_state *state);
SODIUM_EXPORT
int crypto_hash_sha256_update(crypto_hash_sha256_state *state,
const unsigned char *in,
unsigned long long inlen);
SODIUM_EXPORT
int crypto_hash_sha256_final(crypto_hash_sha256_state *state,
unsigned char *out);
#ifdef __cplusplus
}
#endif