change rabbit and hc128 to return values for key and process, will add error rets for alignment issues

This commit is contained in:
toddouska 2013-03-26 14:42:09 -07:00
parent f601b7bfda
commit 14b4bb3b0f
6 changed files with 50 additions and 27 deletions

View File

@ -259,7 +259,7 @@ static void Hc128_SetIV(HC128* ctx, const byte* iv)
}
void Hc128_SetKey(HC128* ctx, const byte* key, const byte* iv)
int Hc128_SetKey(HC128* ctx, const byte* key, const byte* iv)
{
word32 i;
@ -270,11 +270,13 @@ void Hc128_SetKey(HC128* ctx, const byte* key, const byte* iv)
for ( ; i < 8 ; i++) ctx->key[i] = ctx->key[i-4];
Hc128_SetIV(ctx, iv);
return 0;
}
/* The following defines the encryption of data stream */
void Hc128_Process(HC128* ctx, byte* output, const byte* input, word32 msglen)
int Hc128_Process(HC128* ctx, byte* output, const byte* input, word32 msglen)
{
word32 i, keystream[16];
@ -318,6 +320,7 @@ void Hc128_Process(HC128* ctx, byte* output, const byte* input, word32 msglen)
output[i] = input[i] ^ ((byte*)keystream)[i];
}
return 0;
}

View File

@ -133,7 +133,7 @@ static void RabbitSetIV(Rabbit* ctx, const byte* iv)
/* Key setup */
void RabbitSetKey(Rabbit* ctx, const byte* key, const byte* iv)
int RabbitSetKey(Rabbit* ctx, const byte* key, const byte* iv)
{
/* Temporary variables */
word32 k0, k1, k2, k3, i;
@ -182,12 +182,14 @@ void RabbitSetKey(Rabbit* ctx, const byte* key, const byte* iv)
}
ctx->workCtx.carry = ctx->masterCtx.carry;
if (iv) RabbitSetIV(ctx, iv);
if (iv) RabbitSetIV(ctx, iv);
return 0;
}
/* Encrypt/decrypt a message of any size */
void RabbitProcess(Rabbit* ctx, byte* output, const byte* input, word32 msglen)
int RabbitProcess(Rabbit* ctx, byte* output, const byte* input, word32 msglen)
{
/* Encrypt/decrypt all full blocks */
@ -239,6 +241,8 @@ void RabbitProcess(Rabbit* ctx, byte* output, const byte* input, word32 msglen)
output[i] = input[i] ^ buffer[i]; /* scan-build thinks buffer[i] */
/* is garbage, it is not! */
}
return 0;
}

View File

@ -47,8 +47,8 @@ typedef struct HC128 {
} HC128;
CYASSL_API void Hc128_Process(HC128*, byte*, const byte*, word32);
CYASSL_API void Hc128_SetKey(HC128*, const byte* key, const byte* iv);
CYASSL_API int Hc128_Process(HC128*, byte*, const byte*, word32);
CYASSL_API int Hc128_SetKey(HC128*, const byte* key, const byte* iv);
#ifdef __cplusplus

View File

@ -52,8 +52,8 @@ typedef struct Rabbit {
} Rabbit;
CYASSL_API void RabbitProcess(Rabbit*, byte*, const byte*, word32);
CYASSL_API void RabbitSetKey(Rabbit*, const byte* key, const byte* iv);
CYASSL_API int RabbitProcess(Rabbit*, byte*, const byte*, word32);
CYASSL_API int RabbitSetKey(Rabbit*, const byte* key, const byte* iv);
#ifdef __cplusplus

View File

@ -3522,17 +3522,20 @@ static INLINE int Encrypt(CYASSL* ssl, byte* out, const byte* input, word32 sz)
case hc128:
#ifdef XSTREAM_ALIGNMENT
if ((word)input % 4) {
int hcRet;
byte* tmp = (byte*)XMALLOC(sz, ssl->heap,
DYNAMIC_TYPE_TMP_BUFFER);
if (tmp == NULL) return MEMORY_E;
XMEMCPY(tmp, input, sz);
Hc128_Process(ssl->encrypt.hc128, tmp, tmp, sz);
ret = Hc128_Process(ssl->encrypt.hc128, tmp, tmp, sz);
XMEMCPY(out, tmp, sz);
XFREE(tmp, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
break;
}
#endif
Hc128_Process(ssl->encrypt.hc128, out, input, sz);
return Hc128_Process(ssl->encrypt.hc128, out, input, sz);
break;
#endif
@ -3540,13 +3543,16 @@ static INLINE int Encrypt(CYASSL* ssl, byte* out, const byte* input, word32 sz)
case rabbit:
#ifdef XSTREAM_ALIGNMENT
if ((word)input % 4) {
int rabRet;
byte* tmp = (byte*)XMALLOC(sz, ssl->heap,
DYNAMIC_TYPE_TMP_BUFFER);
if (tmp == NULL) return MEMORY_E;
XMEMCPY(tmp, input, sz);
RabbitProcess(ssl->encrypt.rabbit, tmp, tmp, sz);
rabRet = RabbitProcess(ssl->encrypt.rabbit, tmp, tmp, sz);
XMEMCPY(out, tmp, sz);
XFREE(tmp, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
break;
}
#endif
@ -3682,7 +3688,7 @@ static INLINE int Decrypt(CYASSL* ssl, byte* plain, const byte* input,
#ifdef HAVE_HC128
case hc128:
Hc128_Process(ssl->decrypt.hc128, plain, input, sz);
return Hc128_Process(ssl->decrypt.hc128, plain, input, sz);
break;
#endif

View File

@ -1452,6 +1452,7 @@ static int SetKeys(Ciphers* enc, Ciphers* dec, Keys* keys, CipherSpecs* specs,
#ifdef HAVE_HC128
if (specs->bulk_cipher_algorithm == hc128) {
int hcRet;
enc->hc128 = (HC128*)XMALLOC(sizeof(HC128), heap, DYNAMIC_TYPE_CIPHER);
if (enc->hc128 == NULL)
return MEMORY_E;
@ -1459,16 +1460,20 @@ static int SetKeys(Ciphers* enc, Ciphers* dec, Keys* keys, CipherSpecs* specs,
if (dec->hc128 == NULL)
return MEMORY_E;
if (side == CLIENT_END) {
Hc128_SetKey(enc->hc128, keys->client_write_key,
keys->client_write_IV);
Hc128_SetKey(dec->hc128, keys->server_write_key,
keys->server_write_IV);
hcRet = Hc128_SetKey(enc->hc128, keys->client_write_key,
keys->client_write_IV);
if (hcRet != 0) return hcRet;
hcRet = Hc128_SetKey(dec->hc128, keys->server_write_key,
keys->server_write_IV);
if (hcRet != 0) return hcRet;
}
else {
Hc128_SetKey(enc->hc128, keys->server_write_key,
keys->server_write_IV);
Hc128_SetKey(dec->hc128, keys->client_write_key,
keys->client_write_IV);
hcRet = Hc128_SetKey(enc->hc128, keys->server_write_key,
keys->server_write_IV);
if (hcRet != 0) return hcRet;
hcRet = Hc128_SetKey(dec->hc128, keys->client_write_key,
keys->client_write_IV);
if (hcRet != 0) return hcRet;
}
enc->setup = 1;
dec->setup = 1;
@ -1477,6 +1482,7 @@ static int SetKeys(Ciphers* enc, Ciphers* dec, Keys* keys, CipherSpecs* specs,
#ifdef BUILD_RABBIT
if (specs->bulk_cipher_algorithm == rabbit) {
int rabRet;
enc->rabbit = (Rabbit*)XMALLOC(sizeof(Rabbit),heap,DYNAMIC_TYPE_CIPHER);
if (enc->rabbit == NULL)
return MEMORY_E;
@ -1484,16 +1490,20 @@ static int SetKeys(Ciphers* enc, Ciphers* dec, Keys* keys, CipherSpecs* specs,
if (dec->rabbit == NULL)
return MEMORY_E;
if (side == CLIENT_END) {
RabbitSetKey(enc->rabbit, keys->client_write_key,
keys->client_write_IV);
RabbitSetKey(dec->rabbit, keys->server_write_key,
keys->server_write_IV);
rabRet = RabbitSetKey(enc->rabbit, keys->client_write_key,
keys->client_write_IV);
if (rabRet != 0) return rabRet;
rabRet = RabbitSetKey(dec->rabbit, keys->server_write_key,
keys->server_write_IV);
if (rabRet != 0) return rabRet;
}
else {
RabbitSetKey(enc->rabbit, keys->server_write_key,
rabRet = RabbitSetKey(enc->rabbit, keys->server_write_key,
keys->server_write_IV);
RabbitSetKey(dec->rabbit, keys->client_write_key,
if (rabRet != 0) return rabRet;
rabRet = RabbitSetKey(dec->rabbit, keys->client_write_key,
keys->client_write_IV);
if (rabRet != 0) return rabRet;
}
enc->setup = 1;
dec->setup = 1;