From 6bc7ba1592bb1b360cac65c93baa3fd5c4ae1651 Mon Sep 17 00:00:00 2001 From: toddouska Date: Tue, 26 Mar 2013 12:36:39 -0700 Subject: [PATCH 1/4] change AesCBC end/dec to return status, will add failure cases with align checks --- ctaocrypt/src/aes.c | 20 ++++++++++++++------ cyassl/ctaocrypt/aes.h | 4 ++-- mcapi/crypto.c | 8 ++------ src/internal.c | 8 +++++--- src/ssl.c | 12 +++++++++--- 5 files changed, 32 insertions(+), 20 deletions(-) diff --git a/ctaocrypt/src/aes.c b/ctaocrypt/src/aes.c index 96d1dcc99..3a7ac6d0b 100644 --- a/ctaocrypt/src/aes.c +++ b/ctaocrypt/src/aes.c @@ -73,7 +73,7 @@ return AesSetIV(aes, iv); } - void AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) + int AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) { word32 *enc_key, *iv; CRYP_InitTypeDef AES_CRYP_InitStructure; @@ -174,9 +174,11 @@ /* disable crypto processor */ CRYP_Cmd(DISABLE); + + return 0; } - void AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz) + int AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz) { word32 *dec_key, *iv; CRYP_InitTypeDef AES_CRYP_InitStructure; @@ -293,6 +295,8 @@ /* disable crypto processor */ CRYP_Cmd(DISABLE); + + return 0; } #ifdef CYASSL_AES_COUNTER @@ -1678,7 +1682,7 @@ static void AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock) } -void AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) +int AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) { word32 blocks = sz / AES_BLOCK_SIZE; @@ -1702,7 +1706,7 @@ void AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) aes->rounds); /* store iv for next call */ XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE); - return; + return 0; } #endif @@ -1714,10 +1718,12 @@ void AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) out += AES_BLOCK_SIZE; in += AES_BLOCK_SIZE; } + + return 0; } -void AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz) +int AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz) { word32 blocks = sz / AES_BLOCK_SIZE; @@ -1744,7 +1750,7 @@ void AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz) aes->rounds); /* store iv for next call */ XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE); - return; + return 0; } #endif @@ -1757,6 +1763,8 @@ void AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz) out += AES_BLOCK_SIZE; in += AES_BLOCK_SIZE; } + + return 0; } diff --git a/cyassl/ctaocrypt/aes.h b/cyassl/ctaocrypt/aes.h index 18b7a035c..97f9cfd83 100644 --- a/cyassl/ctaocrypt/aes.h +++ b/cyassl/ctaocrypt/aes.h @@ -98,8 +98,8 @@ typedef struct Aes { CYASSL_API int AesSetKey(Aes* aes, const byte* key, word32 len, const byte* iv, int dir); CYASSL_API int AesSetIV(Aes* aes, const byte* iv); -CYASSL_API void AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz); -CYASSL_API void AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz); +CYASSL_API int AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz); +CYASSL_API int AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz); CYASSL_API void AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz); CYASSL_API void AesEncryptDirect(Aes* aes, byte* out, const byte* in); CYASSL_API void AesDecryptDirect(Aes* aes, byte* out, const byte* in); diff --git a/mcapi/crypto.c b/mcapi/crypto.c index 32aa43adf..e3508884f 100644 --- a/mcapi/crypto.c +++ b/mcapi/crypto.c @@ -435,9 +435,7 @@ int CRYPT_AES_CBC_Encrypt(CRYPT_AES_CTX* aes, unsigned char* out, if (aes == NULL || out == NULL || in == NULL) return BAD_FUNC_ARG; - AesCbcEncrypt((Aes*)aes, out, in, inSz); - - return 0; + return AesCbcEncrypt((Aes*)aes, out, in, inSz); } @@ -448,9 +446,7 @@ int CRYPT_AES_CBC_Decrypt(CRYPT_AES_CTX* aes, unsigned char* out, if (aes == NULL || out == NULL || in == NULL) return BAD_FUNC_ARG; - AesCbcDecrypt((Aes*)aes, out, in, inSz); - - return 0; + return AesCbcDecrypt((Aes*)aes, out, in, inSz); } diff --git a/src/internal.c b/src/internal.c index acc2dafd2..d4de841ea 100644 --- a/src/internal.c +++ b/src/internal.c @@ -3426,17 +3426,19 @@ static INLINE int Encrypt(CYASSL* ssl, byte* out, const byte* input, word32 sz) case aes: #ifdef CYASSL_AESNI if ((word)input % 16) { + int ret; byte* tmp = (byte*)XMALLOC(sz, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); if (tmp == NULL) return MEMORY_E; XMEMCPY(tmp, input, sz); - AesCbcEncrypt(ssl->encrypt.aes, tmp, tmp, sz); + ret = AesCbcEncrypt(ssl->encrypt.aes, tmp, tmp, sz); XMEMCPY(out, tmp, sz); XFREE(tmp, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); + return ret; break; } #endif - AesCbcEncrypt(ssl->encrypt.aes, out, input, sz); + return AesCbcEncrypt(ssl->encrypt.aes, out, input, sz); break; #endif @@ -3610,7 +3612,7 @@ static INLINE int Decrypt(CYASSL* ssl, byte* plain, const byte* input, #ifdef BUILD_AES case aes: - AesCbcDecrypt(ssl->decrypt.aes, plain, input, sz); + return AesCbcDecrypt(ssl->decrypt.aes, plain, input, sz); break; #endif diff --git a/src/ssl.c b/src/ssl.c index ade477e90..d336e44d7 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -4997,6 +4997,7 @@ int CyaSSL_set_compression(CYASSL* ssl) int CyaSSL_EVP_Cipher(CYASSL_EVP_CIPHER_CTX* ctx, byte* dst, byte* src, word32 len) { + int ret = 0; CYASSL_ENTER("CyaSSL_EVP_Cipher"); if (ctx == NULL || dst == NULL || src == NULL) { @@ -5016,9 +5017,9 @@ int CyaSSL_set_compression(CYASSL* ssl) case AES_256_CBC_TYPE : CYASSL_MSG("AES CBC"); if (ctx->enc) - AesCbcEncrypt(&ctx->cipher.aes, dst, src, len); + ret = AesCbcEncrypt(&ctx->cipher.aes, dst, src, len); else - AesCbcDecrypt(&ctx->cipher.aes, dst, src, len); + ret = AesCbcDecrypt(&ctx->cipher.aes, dst, src, len); break; #ifdef CYASSL_AES_COUNTER @@ -5056,7 +5057,12 @@ int CyaSSL_set_compression(CYASSL* ssl) CYASSL_MSG("bad type"); return 0; /* failure */ } - } + } + + if (ret != 0) { + CYASSL_MSG("CyaSSL_EVP_Cipher failure"); + return 0; /* failuer */ + } CYASSL_MSG("CyaSSL_EVP_Cipher success"); return 1; /* success */ From f601b7bfda1e4f8634bc6141ad5fc5f3c70afa22 Mon Sep 17 00:00:00 2001 From: toddouska Date: Tue, 26 Mar 2013 14:13:01 -0700 Subject: [PATCH 2/4] move aesni cbc encrypt align check down to crypto layer --- ctaocrypt/src/aes.c | 22 ++++++++++++++++++++++ ctaocrypt/src/error.c | 4 ++++ cyassl/ctaocrypt/error.h | 2 ++ src/internal.c | 14 -------------- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/ctaocrypt/src/aes.c b/ctaocrypt/src/aes.c index 3a7ac6d0b..84a82f6cf 100644 --- a/ctaocrypt/src/aes.c +++ b/ctaocrypt/src/aes.c @@ -1702,10 +1702,32 @@ int AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) printf("aes->rounds = %d\n", aes->rounds); printf("sz = %d\n", sz); #endif + + /* check alignment, decrypt doesn't need alignment */ + if ((word)in % 16) { + #ifndef NO_CYASSL_ALLOC_ALIGN + byte* tmp = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (tmp == NULL) return MEMORY_E; + + XMEMCPY(tmp, in, sz); + AES_CBC_encrypt(tmp, tmp, (byte*)aes->reg, sz, (byte*)aes->key, + aes->rounds); + /* store iv for next call */ + XMEMCPY(aes->reg, tmp + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE); + + XMEMCPY(out, tmp, sz); + XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER); + return 0; + #else + return BAD_ALIGN_E; + #endif + } + AES_CBC_encrypt(in, out, (byte*)aes->reg, sz, (byte*)aes->key, aes->rounds); /* store iv for next call */ XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE); + return 0; } #endif diff --git a/ctaocrypt/src/error.c b/ctaocrypt/src/error.c index b1041659e..1df102213 100644 --- a/ctaocrypt/src/error.c +++ b/ctaocrypt/src/error.c @@ -297,6 +297,10 @@ void CTaoCryptErrorString(int error, char* buffer) XSTRNCPY(buffer, "DeCompress error", max); break; + case BAD_ALIGN_E: + XSTRNCPY(buffer, "Bad alignment error, no alloc help", max); + break; + default: XSTRNCPY(buffer, "unknown error number", max); diff --git a/cyassl/ctaocrypt/error.h b/cyassl/ctaocrypt/error.h index 265019822..14a08af95 100644 --- a/cyassl/ctaocrypt/error.h +++ b/cyassl/ctaocrypt/error.h @@ -108,6 +108,8 @@ enum { DECOMPRESS_INIT_E = -185, /* DeCompress init error */ DECOMPRESS_E = -186, /* DeCompress error */ + BAD_ALIGN_E = -187, /* Bad alignment for operation, no alloc */ + MIN_CODE_E = -200 /* errors -101 - -199 */ }; diff --git a/src/internal.c b/src/internal.c index d4de841ea..3ec7f16ad 100644 --- a/src/internal.c +++ b/src/internal.c @@ -3424,20 +3424,6 @@ static INLINE int Encrypt(CYASSL* ssl, byte* out, const byte* input, word32 sz) #ifdef BUILD_AES case aes: - #ifdef CYASSL_AESNI - if ((word)input % 16) { - int ret; - byte* tmp = (byte*)XMALLOC(sz, ssl->heap, - DYNAMIC_TYPE_TMP_BUFFER); - if (tmp == NULL) return MEMORY_E; - XMEMCPY(tmp, input, sz); - ret = AesCbcEncrypt(ssl->encrypt.aes, tmp, tmp, sz); - XMEMCPY(out, tmp, sz); - XFREE(tmp, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); - return ret; - break; - } - #endif return AesCbcEncrypt(ssl->encrypt.aes, out, input, sz); break; #endif From 14b4bb3b0f26389ec7abfbb7641d64a3b84b8fb3 Mon Sep 17 00:00:00 2001 From: toddouska Date: Tue, 26 Mar 2013 14:42:09 -0700 Subject: [PATCH 3/4] change rabbit and hc128 to return values for key and process, will add error rets for alignment issues --- ctaocrypt/src/hc128.c | 7 +++++-- ctaocrypt/src/rabbit.c | 10 +++++++--- cyassl/ctaocrypt/hc128.h | 4 ++-- cyassl/ctaocrypt/rabbit.h | 4 ++-- src/internal.c | 14 ++++++++++---- src/keys.c | 38 ++++++++++++++++++++++++-------------- 6 files changed, 50 insertions(+), 27 deletions(-) diff --git a/ctaocrypt/src/hc128.c b/ctaocrypt/src/hc128.c index 8c3b34369..305f5c3c1 100644 --- a/ctaocrypt/src/hc128.c +++ b/ctaocrypt/src/hc128.c @@ -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; } diff --git a/ctaocrypt/src/rabbit.c b/ctaocrypt/src/rabbit.c index ee1b4d664..e36ae6bee 100644 --- a/ctaocrypt/src/rabbit.c +++ b/ctaocrypt/src/rabbit.c @@ -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; } diff --git a/cyassl/ctaocrypt/hc128.h b/cyassl/ctaocrypt/hc128.h index c395b0483..cdd70291a 100644 --- a/cyassl/ctaocrypt/hc128.h +++ b/cyassl/ctaocrypt/hc128.h @@ -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 diff --git a/cyassl/ctaocrypt/rabbit.h b/cyassl/ctaocrypt/rabbit.h index 93f9c7a95..97bca779b 100644 --- a/cyassl/ctaocrypt/rabbit.h +++ b/cyassl/ctaocrypt/rabbit.h @@ -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 diff --git a/src/internal.c b/src/internal.c index 3ec7f16ad..5df662ca8 100644 --- a/src/internal.c +++ b/src/internal.c @@ -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 diff --git a/src/keys.c b/src/keys.c index 14ebb0d93..4e9fcd81c 100644 --- a/src/keys.c +++ b/src/keys.c @@ -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; From 7d82bec7fc44d5fa9f68f5e0532ba2e9c2fbbd26 Mon Sep 17 00:00:00 2001 From: toddouska Date: Tue, 26 Mar 2013 18:16:15 -0700 Subject: [PATCH 4/4] do rabbit/hc128 alignment at crypto layer for non intel --- ctaocrypt/src/hc128.c | 58 ++++++++++++++++++++++++++++++++++++++-- ctaocrypt/src/rabbit.c | 60 +++++++++++++++++++++++++++++++++++++++--- src/internal.c | 34 ++---------------------- 3 files changed, 115 insertions(+), 37 deletions(-) diff --git a/ctaocrypt/src/hc128.c b/ctaocrypt/src/hc128.c index 305f5c3c1..cb13f7082 100644 --- a/ctaocrypt/src/hc128.c +++ b/ctaocrypt/src/hc128.c @@ -26,6 +26,8 @@ #ifdef HAVE_HC128 #include +#include +#include #ifdef NO_INLINE #include #else @@ -259,7 +261,7 @@ static void Hc128_SetIV(HC128* ctx, const byte* iv) } -int Hc128_SetKey(HC128* ctx, const byte* key, const byte* iv) +static INLINE int DoKey(HC128* ctx, const byte* key, const byte* iv) { word32 i; @@ -275,8 +277,31 @@ int Hc128_SetKey(HC128* ctx, const byte* key, const byte* iv) } +/* Key setup */ +int Hc128_SetKey(HC128* ctx, const byte* key, const byte* iv) +{ +#ifdef XSTREAM_ALIGN + if ((word)key % 4 || (word)iv % 4) { + int alignKey[4]; + int alignIv[4]; + + CYASSL_MSG("Hc128SetKey unaligned key/iv"); + + XMEMCPY(alignKey, key, sizeof(alignKey)); + XMEMCPY(alignIv, iv, sizeof(alignIv)); + + return DoKey(ctx, (const byte*)alignKey, (const byte*)alignIv); + } +#endif /* XSTREAM_ALIGN */ + + return DoKey(ctx, key, iv); +} + + + /* The following defines the encryption of data stream */ -int Hc128_Process(HC128* ctx, byte* output, const byte* input, word32 msglen) +static INLINE int DoProcess(HC128* ctx, byte* output, const byte* input, + word32 msglen) { word32 i, keystream[16]; @@ -324,6 +349,35 @@ int Hc128_Process(HC128* ctx, byte* output, const byte* input, word32 msglen) } +/* Encrypt/decrypt a message of any size */ +int Hc128_Process(HC128* ctx, byte* output, const byte* input, word32 msglen) +{ +#ifdef XSTREAM_ALIGN + if ((word)input % 4 || (word)output % 4) { + #ifndef NO_CYASSL_ALLOC_ALIGN + byte* tmp; + CYASSL_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 */ diff --git a/ctaocrypt/src/rabbit.c b/ctaocrypt/src/rabbit.c index e36ae6bee..e0f265866 100644 --- a/ctaocrypt/src/rabbit.c +++ b/ctaocrypt/src/rabbit.c @@ -26,6 +26,8 @@ #ifndef NO_RABBIT #include +#include +#include #ifdef NO_INLINE #include #else @@ -133,7 +135,7 @@ static void RabbitSetIV(Rabbit* ctx, const byte* iv) /* Key setup */ -int RabbitSetKey(Rabbit* ctx, const byte* key, const byte* iv) +static INLINE int DoKey(Rabbit* ctx, const byte* key, const byte* iv) { /* Temporary variables */ word32 k0, k1, k2, k3, i; @@ -188,10 +190,34 @@ int RabbitSetKey(Rabbit* ctx, const byte* key, const byte* iv) } -/* Encrypt/decrypt a message of any size */ -int RabbitProcess(Rabbit* ctx, byte* output, const byte* input, word32 msglen) +/* Key setup */ +int RabbitSetKey(Rabbit* ctx, const byte* key, const byte* iv) { +#ifdef XSTREAM_ALIGN + if ((word)key % 4 || (iv && (word)iv % 4)) { + int alignKey[4]; + int alignIv[2]; + CYASSL_MSG("RabbitSetKey unaligned key/iv"); + + XMEMCPY(alignKey, key, sizeof(alignKey)); + if (iv) { + XMEMCPY(alignIv, iv, sizeof(alignIv)); + iv = (const byte*)alignIv; + } + + return DoKey(ctx, (const byte*)alignKey, iv); + } +#endif /* XSTREAM_ALIGN */ + + return DoKey(ctx, key, iv); +} + + +/* Encrypt/decrypt a message of any size */ +static INLINE int DoProcess(Rabbit* ctx, byte* output, const byte* input, + word32 msglen) +{ /* Encrypt/decrypt all full blocks */ while (msglen >= 16) { /* Iterate the system */ @@ -246,5 +272,33 @@ int RabbitProcess(Rabbit* ctx, byte* output, const byte* input, word32 msglen) } +/* Encrypt/decrypt a message of any size */ +int RabbitProcess(Rabbit* ctx, byte* output, const byte* input, word32 msglen) +{ +#ifdef XSTREAM_ALIGN + if ((word)input % 4 || (word)output % 4) { + #ifndef NO_CYASSL_ALLOC_ALIGN + byte* tmp; + CYASSL_MSG("RabbitProcess 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); +} + #endif /* NO_RABBIT */ diff --git a/src/internal.c b/src/internal.c index 5df662ca8..7bb002842 100644 --- a/src/internal.c +++ b/src/internal.c @@ -3520,43 +3520,13 @@ static INLINE int Encrypt(CYASSL* ssl, byte* out, const byte* input, word32 sz) #ifdef HAVE_HC128 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); - 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 return Hc128_Process(ssl->encrypt.hc128, out, input, sz); break; #endif #ifdef BUILD_RABBIT 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); - rabRet = RabbitProcess(ssl->encrypt.rabbit, tmp, tmp, sz); - XMEMCPY(out, tmp, sz); - XFREE(tmp, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); - - return ret; - break; - } - #endif - RabbitProcess(ssl->encrypt.rabbit, out, input, sz); + return RabbitProcess(ssl->encrypt.rabbit, out, input, sz); break; #endif @@ -3694,7 +3664,7 @@ static INLINE int Decrypt(CYASSL* ssl, byte* plain, const byte* input, #ifdef BUILD_RABBIT case rabbit: - RabbitProcess(ssl->decrypt.rabbit, plain, input, sz); + return RabbitProcess(ssl->decrypt.rabbit, plain, input, sz); break; #endif