chacha, dh, dsa, hc128 c files

This commit is contained in:
Jacob Barthelmeh
2014-12-17 14:37:13 -07:00
parent 0957d275b3
commit 473d1d18a6
6 changed files with 848 additions and 13 deletions

View File

@ -62,6 +62,6 @@ CYASSL_API int DsaPrivateKeyDecode(const byte* input, word32* inOutIdx, DsaKey*,
} /* extern "C" */
#endif
#endif /* CTAO_CRYPT_DSA_H */
#endif /* WOLF_CRYPT_DSA_H */
#endif /* NO_DSA */

View File

@ -334,7 +334,39 @@
#define Arc4FreeCavium wc_Arc4FreeCavium
#endif
/**/
/* for chacha reverse compatibility */
#ifdef HAVE_CHACHA
#define ChachaProcess wc_ChachaProcess
#define ChachaSetKey wc_ChachaSetKey
#define Chacha_SetIV wc_Chacha_SetIV
#endif
/* for DH reverse compatibility */
#ifndef NO_DH
#define WOLFSSL_BIT_SIZE CYASSL_BIT_SIZE /* @TODO*/
#define InitDhKey wc_InitDhKey
#define FreeDhKey wc_FreeDhKey
#define DhGenerateKeyPair wc_DhGenerateKeyPair
#endif
/* for DSA reverse compatibility */
#ifndef NO_DSA
#define InitDsaKey wc_InitDsaKey
#define FreeDsaKey wc_FreeDsaKey
#define DsaSign wc_DsaSign
#define DsaVerify wc_DsaVerify
#define DsaPublicKeyDecode wc_DsaPublicKeyDecode
#define DsaPrivateKeyDecode wc_DsaPrivateKeyDecode
#endif
/* for hc128 reverse compatibility */
#ifdef HAVE_HC128
#define NO_WOLFSSL_ALLOC_ALIGN NO_CYASSL_ALLOC_ALIGN /* @TODO*/
#define Hc128_Process wc_Hc128_Process
#define Hc128_SetKey wc_Hc128_SetKey
#endif
#ifdef __cplusplus
} /* extern "C" */

View File

@ -2,14 +2,14 @@
*
* Copyright (C) 2006-2014 wolfSSL Inc.
*
* This file is part of CyaSSL.
* This file is part of wolfSSL. (formerly known as CyaSSL)
*
* CyaSSL is free software; you can redistribute it and/or modify
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* CyaSSL is distributed in the hope that it will be useful,
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
@ -75,7 +75,7 @@
* Set up iv(nonce). Earlier versions used 64 bits instead of 96, this version
* uses the typical AEAD 96 bit nonce and can do record sizes of 256 GB.
*/
int Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter)
int wc_Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter)
{
word32 temp[3]; /* used for alignment of memory */
XMEMSET(temp, 0, 12);
@ -110,7 +110,7 @@ static const word32 tau[4] = {0x61707865, 0x3120646e, 0x79622d36, 0x6b206574};
/**
* Key setup. 8 word iv (nonce)
*/
int Chacha_SetKey(ChaCha* ctx, const byte* key, word32 keySz)
int wc_Chacha_SetKey(ChaCha* ctx, const byte* key, word32 keySz)
{
const word32* constants;
const byte* k;
@ -121,7 +121,7 @@ int Chacha_SetKey(ChaCha* ctx, const byte* key, word32 keySz)
#ifdef XSTREAM_ALIGN
word32 alignKey[keySz / 4];
if ((cyassl_word)key % 4) {
CYASSL_MSG("ChachaSetKey unaligned key");
WOLFSSL_MSG("wc_ChachaSetKey unaligned key");
XMEMCPY(alignKey, key, sizeof(alignKey));
k = (byte*)alignKey;
}
@ -173,7 +173,7 @@ int Chacha_SetKey(ChaCha* ctx, const byte* key, word32 keySz)
/**
* Converts word into bytes with rotations having been done.
*/
static INLINE void Chacha_wordtobyte(word32 output[16], const word32 input[16])
static INLINE void wc_Chacha_wordtobyte(word32 output[16], const word32 input[16])
{
word32 x[16];
word32 i;
@ -205,7 +205,7 @@ static INLINE void Chacha_wordtobyte(word32 output[16], const word32 input[16])
/**
* Encrypt a stream of bytes
*/
static void Chacha_encrypt_bytes(ChaCha* ctx, const byte* m, byte* c,
static void wc_Chacha_encrypt_bytes(ChaCha* ctx, const byte* m, byte* c,
word32 bytes)
{
byte* output;
@ -216,7 +216,7 @@ static void Chacha_encrypt_bytes(ChaCha* ctx, const byte* m, byte* c,
if (!bytes) return;
for (;;) {
Chacha_wordtobyte(temp, ctx->X);
wc_Chacha_wordtobyte(temp, ctx->X);
ctx->X[12] = PLUSONE(ctx->X[12]);
if (bytes <= 64) {
for (i = 0; i < bytes; ++i) {
@ -236,12 +236,12 @@ static void Chacha_encrypt_bytes(ChaCha* ctx, const byte* m, byte* c,
/**
* API to encrypt/decrypt a message of any size.
*/
int Chacha_Process(ChaCha* ctx, byte* output, const byte* input, word32 msglen)
int wc_Chacha_Process(ChaCha* ctx, byte* output, const byte* input, word32 msglen)
{
if (ctx == NULL)
return BAD_FUNC_ARG;
Chacha_encrypt_bytes(ctx, input, output, msglen);
wc_Chacha_encrypt_bytes(ctx, input, output, msglen);
return 0;
}

178
wolfcrypt/src/dh.c Normal file
View File

@ -0,0 +1,178 @@
/* dh.c
*
* Copyright (C) 2006-2014 wolfSSL Inc.
*
* This file is part of wolfSSL. (formerly known as CyaSSL)
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cyassl/ctaocrypt/settings.h>
#ifndef NO_DH
#include <cyassl/ctaocrypt/dh.h>
#include <cyassl/ctaocrypt/error-crypt.h>
#ifndef USER_MATH_LIB
#include <math.h>
#define XPOW(x,y) pow((x),(y))
#define XLOG(x) log((x))
#else
/* user's own math lib */
#endif
#ifndef min
static INLINE word32 min(word32 a, word32 b)
{
return a > b ? b : a;
}
#endif /* min */
void wc_InitDhKey(DhKey* key)
{
(void)key;
/* TomsFastMath doesn't use memory allocation */
#ifndef USE_FAST_MATH
key->p.dp = 0;
key->g.dp = 0;
#endif
}
void wc_FreeDhKey(DhKey* key)
{
(void)key;
/* TomsFastMath doesn't use memory allocation */
#ifndef USE_FAST_MATH
mp_clear(&key->p);
mp_clear(&key->g);
#endif
}
static word32 DiscreteLogWorkFactor(word32 n)
{
/* assuming discrete log takes about the same time as factoring */
if (n<5)
return 0;
else
return (word32)(2.4 * XPOW((double)n, 1.0/3.0) *
XPOW(XLOG((double)n), 2.0/3.0) - 5);
}
static int GeneratePrivate(DhKey* key, RNG* rng, byte* priv, word32* privSz)
{
int ret;
word32 sz = mp_unsigned_bin_size(&key->p);
sz = min(sz, 2 * DiscreteLogWorkFactor(sz * WOLFSSL_BIT_SIZE) /
WOLFSSL_BIT_SIZE + 1);
ret = RNG_GenerateBlock(rng, priv, sz);
if (ret != 0)
return ret;
priv[0] |= 0x0C;
*privSz = sz;
return 0;
}
static int GeneratePublic(DhKey* key, const byte* priv, word32 privSz,
byte* pub, word32* pubSz)
{
int ret = 0;
mp_int x;
mp_int y;
if (mp_init_multi(&x, &y, 0, 0, 0, 0) != MP_OKAY)
return MP_INIT_E;
if (mp_read_unsigned_bin(&x, priv, privSz) != MP_OKAY)
ret = MP_READ_E;
if (ret == 0 && mp_exptmod(&key->g, &x, &key->p, &y) != MP_OKAY)
ret = MP_EXPTMOD_E;
if (ret == 0 && mp_to_unsigned_bin(&y, pub) != MP_OKAY)
ret = MP_TO_E;
if (ret == 0)
*pubSz = mp_unsigned_bin_size(&y);
mp_clear(&y);
mp_clear(&x);
return ret;
}
int wc_DhGenerateKeyPair(DhKey* key, RNG* rng, byte* priv, word32* privSz,
byte* pub, word32* pubSz)
{
int ret = GeneratePrivate(key, rng, priv, privSz);
return (ret != 0) ? ret : GeneratePublic(key, priv, *privSz, pub, pubSz);
}
int DhAgree(DhKey* key, byte* agree, word32* agreeSz, const byte* priv,
word32 privSz, const byte* otherPub, word32 pubSz)
{
int ret = 0;
mp_int x;
mp_int y;
mp_int z;
if (mp_init_multi(&x, &y, &z, 0, 0, 0) != MP_OKAY)
return MP_INIT_E;
if (mp_read_unsigned_bin(&x, priv, privSz) != MP_OKAY)
ret = MP_READ_E;
if (ret == 0 && mp_read_unsigned_bin(&y, otherPub, pubSz) != MP_OKAY)
ret = MP_READ_E;
if (ret == 0 && mp_exptmod(&y, &x, &key->p, &z) != MP_OKAY)
ret = MP_EXPTMOD_E;
if (ret == 0 && mp_to_unsigned_bin(&z, agree) != MP_OKAY)
ret = MP_TO_E;
if (ret == 0)
*agreeSz = mp_unsigned_bin_size(&z);
mp_clear(&z);
mp_clear(&y);
mp_clear(&x);
return ret;
}
#endif /* NO_DH */

226
wolfcrypt/src/dsa.c Normal file
View File

@ -0,0 +1,226 @@
/* dsa.c
*
* Copyright (C) 2006-2014 wolfSSL Inc.
*
* This file is part of wolfSSL. (formerly known as CyaSSL)
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cyassl/ctaocrypt/settings.h>
#ifndef NO_DSA
#include <cyassl/ctaocrypt/dsa.h>
#include <cyassl/ctaocrypt/sha.h>
#include <cyassl/ctaocrypt/random.h>
#include <cyassl/ctaocrypt/error-crypt.h>
enum {
DSA_HALF_SIZE = 20, /* r and s size */
DSA_SIG_SIZE = 40 /* signature size */
};
#ifndef min
static INLINE word32 min(word32 a, word32 b)
{
return a > b ? b : a;
}
#endif /* min */
void wc_InitDsaKey(DsaKey* key)
{
key->type = -1; /* haven't decided yet */
/* TomsFastMath doesn't use memory allocation */
#ifndef USE_FAST_MATH
key->p.dp = 0; /* public alloc parts */
key->q.dp = 0;
key->g.dp = 0;
key->y.dp = 0;
key->x.dp = 0; /* private alloc parts */
#endif
}
void wc_FreeDsaKey(DsaKey* key)
{
(void)key;
/* TomsFastMath doesn't use memory allocation */
#ifndef USE_FAST_MATH
if (key->type == DSA_PRIVATE)
mp_clear(&key->x);
mp_clear(&key->y);
mp_clear(&key->g);
mp_clear(&key->q);
mp_clear(&key->p);
#endif
}
int wc_DsaSign(const byte* digest, byte* out, DsaKey* key, RNG* rng)
{
mp_int k, kInv, r, s, H;
int ret, sz;
byte buffer[DSA_HALF_SIZE];
sz = min(sizeof(buffer), mp_unsigned_bin_size(&key->q));
/* generate k */
ret = RNG_GenerateBlock(rng, buffer, sz);
if (ret != 0)
return ret;
buffer[0] |= 0x0C;
if (mp_init_multi(&k, &kInv, &r, &s, &H, 0) != MP_OKAY)
return MP_INIT_E;
if (mp_read_unsigned_bin(&k, buffer, sz) != MP_OKAY)
ret = MP_READ_E;
if (ret == 0 && mp_cmp_d(&k, 1) != MP_GT)
ret = MP_CMP_E;
/* inverse k mod q */
if (ret == 0 && mp_invmod(&k, &key->q, &kInv) != MP_OKAY)
ret = MP_INVMOD_E;
/* generate r, r = (g exp k mod p) mod q */
if (ret == 0 && mp_exptmod(&key->g, &k, &key->p, &r) != MP_OKAY)
ret = MP_EXPTMOD_E;
if (ret == 0 && mp_mod(&r, &key->q, &r) != MP_OKAY)
ret = MP_MOD_E;
/* generate H from sha digest */
if (ret == 0 && mp_read_unsigned_bin(&H, digest,SHA_DIGEST_SIZE) != MP_OKAY)
ret = MP_READ_E;
/* generate s, s = (kInv * (H + x*r)) % q */
if (ret == 0 && mp_mul(&key->x, &r, &s) != MP_OKAY)
ret = MP_MUL_E;
if (ret == 0 && mp_add(&s, &H, &s) != MP_OKAY)
ret = MP_ADD_E;
if (ret == 0 && mp_mulmod(&s, &kInv, &key->q, &s) != MP_OKAY)
ret = MP_MULMOD_E;
/* write out */
if (ret == 0) {
int rSz = mp_unsigned_bin_size(&r);
int sSz = mp_unsigned_bin_size(&s);
if (rSz == DSA_HALF_SIZE - 1) {
out[0] = 0;
out++;
}
if (mp_to_unsigned_bin(&r, out) != MP_OKAY)
ret = MP_TO_E;
else {
if (sSz == DSA_HALF_SIZE - 1) {
out[rSz] = 0;
out++;
}
ret = mp_to_unsigned_bin(&s, out + rSz);
}
}
mp_clear(&H);
mp_clear(&s);
mp_clear(&r);
mp_clear(&kInv);
mp_clear(&k);
return ret;
}
int wc_DsaVerify(const byte* digest, const byte* sig, DsaKey* key, int* answer)
{
mp_int w, u1, u2, v, r, s;
int ret = 0;
if (mp_init_multi(&w, &u1, &u2, &v, &r, &s) != MP_OKAY)
return MP_INIT_E;
/* set r and s from signature */
if (mp_read_unsigned_bin(&r, sig, DSA_HALF_SIZE) != MP_OKAY ||
mp_read_unsigned_bin(&s, sig + DSA_HALF_SIZE, DSA_HALF_SIZE) != MP_OKAY)
ret = MP_READ_E;
/* sanity checks */
/* put H into u1 from sha digest */
if (ret == 0 && mp_read_unsigned_bin(&u1,digest,SHA_DIGEST_SIZE) != MP_OKAY)
ret = MP_READ_E;
/* w = s invmod q */
if (ret == 0 && mp_invmod(&s, &key->q, &w) != MP_OKAY)
ret = MP_INVMOD_E;
/* u1 = (H * w) % q */
if (ret == 0 && mp_mulmod(&u1, &w, &key->q, &u1) != MP_OKAY)
ret = MP_MULMOD_E;
/* u2 = (r * w) % q */
if (ret == 0 && mp_mulmod(&r, &w, &key->q, &u2) != MP_OKAY)
ret = MP_MULMOD_E;
/* verify v = ((g^u1 * y^u2) mod p) mod q */
if (ret == 0 && mp_exptmod(&key->g, &u1, &key->p, &u1) != MP_OKAY)
ret = MP_EXPTMOD_E;
if (ret == 0 && mp_exptmod(&key->y, &u2, &key->p, &u2) != MP_OKAY)
ret = MP_EXPTMOD_E;
if (ret == 0 && mp_mulmod(&u1, &u2, &key->p, &v) != MP_OKAY)
ret = MP_MULMOD_E;
if (ret == 0 && mp_mod(&v, &key->q, &v) != MP_OKAY)
ret = MP_MULMOD_E;
/* do they match */
if (ret == 0 && mp_cmp(&r, &v) == MP_EQ)
*answer = 1;
else
*answer = 0;
mp_clear(&s);
mp_clear(&r);
mp_clear(&u1);
mp_clear(&u2);
mp_clear(&w);
mp_clear(&v);
return ret;
}
#endif /* NO_DSA */

399
wolfcrypt/src/hc128.c Normal file
View File

@ -0,0 +1,399 @@
/* hc128.c
*
* Copyright (C) 2006-2014 wolfSSL Inc.
*
* This file is part of wolfSSL. (formerly known as CyaSSL)
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cyassl/ctaocrypt/settings.h>
#ifdef HAVE_HC128
#include <cyassl/ctaocrypt/hc128.h>
#include <cyassl/ctaocrypt/error-crypt.h>
#include <cyassl/ctaocrypt/logging.h>
#ifdef NO_INLINE
#include <cyassl/ctaocrypt/hc128.h>
#include <cyassl/ctaocrypt/misc.h>
#else
#include <ctaocrypt/src/misc.c>
#endif
#ifdef BIG_ENDIAN_ORDER
#define LITTLE32(x) ByteReverseWord32(x)
#else
#define LITTLE32(x) (x)
#endif
/*h1 function*/
#define h1(ctx, x, y) { \
byte a,c; \
a = (byte) (x); \
c = (byte) ((x) >> 16); \
y = (ctx->T[512+a])+(ctx->T[512+256+c]); \
}
/*h2 function*/
#define h2(ctx, x, y) { \
byte a,c; \
a = (byte) (x); \
c = (byte) ((x) >> 16); \
y = (ctx->T[a])+(ctx->T[256+c]); \
}
/*one step of HC-128, update P and generate 32 bits keystream*/
#define step_P(ctx,u,v,a,b,c,d,n){ \
word32 tem0,tem1,tem2,tem3; \
h1((ctx),(ctx->X[(d)]),tem3); \
tem0 = rotrFixed((ctx->T[(v)]),23); \
tem1 = rotrFixed((ctx->X[(c)]),10); \
tem2 = rotrFixed((ctx->X[(b)]),8); \
(ctx->T[(u)]) += tem2+(tem0 ^ tem1); \
(ctx->X[(a)]) = (ctx->T[(u)]); \
(n) = tem3 ^ (ctx->T[(u)]) ; \
}
/*one step of HC-128, update Q and generate 32 bits keystream*/
#define step_Q(ctx,u,v,a,b,c,d,n){ \
word32 tem0,tem1,tem2,tem3; \
h2((ctx),(ctx->Y[(d)]),tem3); \
tem0 = rotrFixed((ctx->T[(v)]),(32-23)); \
tem1 = rotrFixed((ctx->Y[(c)]),(32-10)); \
tem2 = rotrFixed((ctx->Y[(b)]),(32-8)); \
(ctx->T[(u)]) += tem2 + (tem0 ^ tem1); \
(ctx->Y[(a)]) = (ctx->T[(u)]); \
(n) = tem3 ^ (ctx->T[(u)]) ; \
}
/*16 steps of HC-128, generate 512 bits keystream*/
static void generate_keystream(HC128* ctx, word32* keystream)
{
word32 cc,dd;
cc = ctx->counter1024 & 0x1ff;
dd = (cc+16)&0x1ff;
if (ctx->counter1024 < 512)
{
ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
step_P(ctx, cc+0, cc+1, 0, 6, 13,4, keystream[0]);
step_P(ctx, cc+1, cc+2, 1, 7, 14,5, keystream[1]);
step_P(ctx, cc+2, cc+3, 2, 8, 15,6, keystream[2]);
step_P(ctx, cc+3, cc+4, 3, 9, 0, 7, keystream[3]);
step_P(ctx, cc+4, cc+5, 4, 10,1, 8, keystream[4]);
step_P(ctx, cc+5, cc+6, 5, 11,2, 9, keystream[5]);
step_P(ctx, cc+6, cc+7, 6, 12,3, 10,keystream[6]);
step_P(ctx, cc+7, cc+8, 7, 13,4, 11,keystream[7]);
step_P(ctx, cc+8, cc+9, 8, 14,5, 12,keystream[8]);
step_P(ctx, cc+9, cc+10,9, 15,6, 13,keystream[9]);
step_P(ctx, cc+10,cc+11,10,0, 7, 14,keystream[10]);
step_P(ctx, cc+11,cc+12,11,1, 8, 15,keystream[11]);
step_P(ctx, cc+12,cc+13,12,2, 9, 0, keystream[12]);
step_P(ctx, cc+13,cc+14,13,3, 10,1, keystream[13]);
step_P(ctx, cc+14,cc+15,14,4, 11,2, keystream[14]);
step_P(ctx, cc+15,dd+0, 15,5, 12,3, keystream[15]);
}
else
{
ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
step_Q(ctx, 512+cc+0, 512+cc+1, 0, 6, 13,4, keystream[0]);
step_Q(ctx, 512+cc+1, 512+cc+2, 1, 7, 14,5, keystream[1]);
step_Q(ctx, 512+cc+2, 512+cc+3, 2, 8, 15,6, keystream[2]);
step_Q(ctx, 512+cc+3, 512+cc+4, 3, 9, 0, 7, keystream[3]);
step_Q(ctx, 512+cc+4, 512+cc+5, 4, 10,1, 8, keystream[4]);
step_Q(ctx, 512+cc+5, 512+cc+6, 5, 11,2, 9, keystream[5]);
step_Q(ctx, 512+cc+6, 512+cc+7, 6, 12,3, 10,keystream[6]);
step_Q(ctx, 512+cc+7, 512+cc+8, 7, 13,4, 11,keystream[7]);
step_Q(ctx, 512+cc+8, 512+cc+9, 8, 14,5, 12,keystream[8]);
step_Q(ctx, 512+cc+9, 512+cc+10,9, 15,6, 13,keystream[9]);
step_Q(ctx, 512+cc+10,512+cc+11,10,0, 7, 14,keystream[10]);
step_Q(ctx, 512+cc+11,512+cc+12,11,1, 8, 15,keystream[11]);
step_Q(ctx, 512+cc+12,512+cc+13,12,2, 9, 0, keystream[12]);
step_Q(ctx, 512+cc+13,512+cc+14,13,3, 10,1, keystream[13]);
step_Q(ctx, 512+cc+14,512+cc+15,14,4, 11,2, keystream[14]);
step_Q(ctx, 512+cc+15,512+dd+0, 15,5, 12,3, keystream[15]);
}
}
/* The following defines the initialization functions */
#define f1(x) (rotrFixed((x),7) ^ rotrFixed((x),18) ^ ((x) >> 3))
#define f2(x) (rotrFixed((x),17) ^ rotrFixed((x),19) ^ ((x) >> 10))
/*update table P*/
#define update_P(ctx,u,v,a,b,c,d){ \
word32 tem0,tem1,tem2,tem3; \
tem0 = rotrFixed((ctx->T[(v)]),23); \
tem1 = rotrFixed((ctx->X[(c)]),10); \
tem2 = rotrFixed((ctx->X[(b)]),8); \
h1((ctx),(ctx->X[(d)]),tem3); \
(ctx->T[(u)]) = ((ctx->T[(u)]) + tem2+(tem0^tem1)) ^ tem3; \
(ctx->X[(a)]) = (ctx->T[(u)]); \
}
/*update table Q*/
#define update_Q(ctx,u,v,a,b,c,d){ \
word32 tem0,tem1,tem2,tem3; \
tem0 = rotrFixed((ctx->T[(v)]),(32-23)); \
tem1 = rotrFixed((ctx->Y[(c)]),(32-10)); \
tem2 = rotrFixed((ctx->Y[(b)]),(32-8)); \
h2((ctx),(ctx->Y[(d)]),tem3); \
(ctx->T[(u)]) = ((ctx->T[(u)]) + tem2+(tem0^tem1)) ^ tem3; \
(ctx->Y[(a)]) = (ctx->T[(u)]); \
}
/*16 steps of HC-128, without generating keystream, */
/*but use the outputs to update P and Q*/
static void setup_update(HC128* ctx) /*each time 16 steps*/
{
word32 cc,dd;
cc = ctx->counter1024 & 0x1ff;
dd = (cc+16)&0x1ff;
if (ctx->counter1024 < 512)
{
ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
update_P(ctx, cc+0, cc+1, 0, 6, 13, 4);
update_P(ctx, cc+1, cc+2, 1, 7, 14, 5);
update_P(ctx, cc+2, cc+3, 2, 8, 15, 6);
update_P(ctx, cc+3, cc+4, 3, 9, 0, 7);
update_P(ctx, cc+4, cc+5, 4, 10,1, 8);
update_P(ctx, cc+5, cc+6, 5, 11,2, 9);
update_P(ctx, cc+6, cc+7, 6, 12,3, 10);
update_P(ctx, cc+7, cc+8, 7, 13,4, 11);
update_P(ctx, cc+8, cc+9, 8, 14,5, 12);
update_P(ctx, cc+9, cc+10,9, 15,6, 13);
update_P(ctx, cc+10,cc+11,10,0, 7, 14);
update_P(ctx, cc+11,cc+12,11,1, 8, 15);
update_P(ctx, cc+12,cc+13,12,2, 9, 0);
update_P(ctx, cc+13,cc+14,13,3, 10, 1);
update_P(ctx, cc+14,cc+15,14,4, 11, 2);
update_P(ctx, cc+15,dd+0, 15,5, 12, 3);
}
else
{
ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
update_Q(ctx, 512+cc+0, 512+cc+1, 0, 6, 13, 4);
update_Q(ctx, 512+cc+1, 512+cc+2, 1, 7, 14, 5);
update_Q(ctx, 512+cc+2, 512+cc+3, 2, 8, 15, 6);
update_Q(ctx, 512+cc+3, 512+cc+4, 3, 9, 0, 7);
update_Q(ctx, 512+cc+4, 512+cc+5, 4, 10,1, 8);
update_Q(ctx, 512+cc+5, 512+cc+6, 5, 11,2, 9);
update_Q(ctx, 512+cc+6, 512+cc+7, 6, 12,3, 10);
update_Q(ctx, 512+cc+7, 512+cc+8, 7, 13,4, 11);
update_Q(ctx, 512+cc+8, 512+cc+9, 8, 14,5, 12);
update_Q(ctx, 512+cc+9, 512+cc+10,9, 15,6, 13);
update_Q(ctx, 512+cc+10,512+cc+11,10,0, 7, 14);
update_Q(ctx, 512+cc+11,512+cc+12,11,1, 8, 15);
update_Q(ctx, 512+cc+12,512+cc+13,12,2, 9, 0);
update_Q(ctx, 512+cc+13,512+cc+14,13,3, 10, 1);
update_Q(ctx, 512+cc+14,512+cc+15,14,4, 11, 2);
update_Q(ctx, 512+cc+15,512+dd+0, 15,5, 12, 3);
}
}
/* for the 128-bit key: key[0]...key[15]
* key[0] is the least significant byte of ctx->key[0] (K_0);
* key[3] is the most significant byte of ctx->key[0] (K_0);
* ...
* key[12] is the least significant byte of ctx->key[3] (K_3)
* key[15] is the most significant byte of ctx->key[3] (K_3)
*
* for the 128-bit iv: iv[0]...iv[15]
* iv[0] is the least significant byte of ctx->iv[0] (IV_0);
* iv[3] is the most significant byte of ctx->iv[0] (IV_0);
* ...
* iv[12] is the least significant byte of ctx->iv[3] (IV_3)
* iv[15] is the most significant byte of ctx->iv[3] (IV_3)
*/
static void Hc128_SetIV(HC128* ctx, const byte* inIv)
{
word32 i;
word32 iv[4];
if (inIv)
XMEMCPY(iv, inIv, sizeof(iv));
else
XMEMSET(iv, 0, sizeof(iv));
for (i = 0; i < (128 >> 5); i++)
ctx->iv[i] = LITTLE32(iv[i]);
for (; i < 8; i++) ctx->iv[i] = ctx->iv[i-4];
/* expand the key and IV into the table T */
/* (expand the key and IV into the table P and Q) */
for (i = 0; i < 8; i++) ctx->T[i] = ctx->key[i];
for (i = 8; i < 16; i++) ctx->T[i] = ctx->iv[i-8];
for (i = 16; i < (256+16); i++)
ctx->T[i] = f2(ctx->T[i-2]) + ctx->T[i-7] + f1(ctx->T[i-15]) +
ctx->T[i-16]+i;
for (i = 0; i < 16; i++) ctx->T[i] = ctx->T[256+i];
for (i = 16; i < 1024; i++)
ctx->T[i] = f2(ctx->T[i-2]) + ctx->T[i-7] + f1(ctx->T[i-15]) +
ctx->T[i-16]+256+i;
/* initialize counter1024, X and Y */
ctx->counter1024 = 0;
for (i = 0; i < 16; i++) ctx->X[i] = ctx->T[512-16+i];
for (i = 0; i < 16; i++) ctx->Y[i] = ctx->T[512+512-16+i];
/* run the cipher 1024 steps before generating the output */
for (i = 0; i < 64; i++) setup_update(ctx);
}
static INLINE int DoKey(HC128* ctx, const byte* key, const byte* iv)
{
word32 i;
/* Key size in bits 128 */
for (i = 0; i < (128 >> 5); i++)
ctx->key[i] = LITTLE32(((word32*)key)[i]);
for ( ; i < 8 ; i++) ctx->key[i] = ctx->key[i-4];
Hc128_SetIV(ctx, iv);
return 0;
}
/* Key setup */
int wc_Hc128_SetKey(HC128* ctx, const byte* key, const byte* iv)
{
#ifdef XSTREAM_ALIGN
if ((cyassl_word)key % 4) {
int alignKey[4];
/* iv gets aligned in SetIV */
WOLFSSL_MSG("Hc128SetKey unaligned key");
XMEMCPY(alignKey, key, sizeof(alignKey));
return DoKey(ctx, (const byte*)alignKey, iv);
}
#endif /* XSTREAM_ALIGN */
return DoKey(ctx, key, iv);
}
/* The following defines the encryption of data stream */
static INLINE int DoProcess(HC128* ctx, byte* output, const byte* input,
word32 msglen)
{
word32 i, keystream[16];
for ( ; msglen >= 64; msglen -= 64, input += 64, output += 64)
{
generate_keystream(ctx, keystream);
/* unroll loop */
((word32*)output)[0] = ((word32*)input)[0] ^ LITTLE32(keystream[0]);
((word32*)output)[1] = ((word32*)input)[1] ^ LITTLE32(keystream[1]);
((word32*)output)[2] = ((word32*)input)[2] ^ LITTLE32(keystream[2]);
((word32*)output)[3] = ((word32*)input)[3] ^ LITTLE32(keystream[3]);
((word32*)output)[4] = ((word32*)input)[4] ^ LITTLE32(keystream[4]);
((word32*)output)[5] = ((word32*)input)[5] ^ LITTLE32(keystream[5]);
((word32*)output)[6] = ((word32*)input)[6] ^ LITTLE32(keystream[6]);
((word32*)output)[7] = ((word32*)input)[7] ^ LITTLE32(keystream[7]);
((word32*)output)[8] = ((word32*)input)[8] ^ LITTLE32(keystream[8]);
((word32*)output)[9] = ((word32*)input)[9] ^ LITTLE32(keystream[9]);
((word32*)output)[10] = ((word32*)input)[10] ^ LITTLE32(keystream[10]);
((word32*)output)[11] = ((word32*)input)[11] ^ LITTLE32(keystream[11]);
((word32*)output)[12] = ((word32*)input)[12] ^ LITTLE32(keystream[12]);
((word32*)output)[13] = ((word32*)input)[13] ^ LITTLE32(keystream[13]);
((word32*)output)[14] = ((word32*)input)[14] ^ LITTLE32(keystream[14]);
((word32*)output)[15] = ((word32*)input)[15] ^ LITTLE32(keystream[15]);
}
if (msglen > 0)
{
XMEMSET(keystream, 0, sizeof(keystream)); /* hush the static analysis */
generate_keystream(ctx, keystream);
#ifdef BIG_ENDIAN_ORDER
{
word32 wordsLeft = msglen / sizeof(word32);
if (msglen % sizeof(word32)) wordsLeft++;
ByteReverseWords(keystream, keystream, wordsLeft * sizeof(word32));
}
#endif
for (i = 0; i < msglen; i++)
output[i] = input[i] ^ ((byte*)keystream)[i];
}
return 0;
}
/* Encrypt/decrypt a message of any size */
int wc_Hc128_Process(HC128* ctx, byte* output, const byte* input, word32 msglen)
{
#ifdef XSTREAM_ALIGN
if ((cyassl_word)input % 4 || (cyassl_word)output % 4) {
#ifndef NO_WOLFSSL_ALLOC_ALIGN
byte* tmp;
WOLFSSL_MSG("Hc128Process unaligned");
tmp = (byte*)XMALLOC(msglen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (tmp == NULL) return MEMORY_E;
XMEMCPY(tmp, input, msglen);
DoProcess(ctx, tmp, tmp, msglen);
XMEMCPY(output, tmp, msglen);
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return 0;
#else
return BAD_ALIGN_E;
#endif
}
#endif /* XSTREAM_ALIGN */
return DoProcess(ctx, output, input, msglen);
}
#else /* HAVE_HC128 */
#ifdef _MSC_VER
/* 4206 warning for blank file */
#pragma warning(disable: 4206)
#endif
#endif /* HAVE_HC128 */