change AesCBC end/dec to return status, will add failure cases with align checks

This commit is contained in:
toddouska
2013-03-26 12:36:39 -07:00
parent 8e53c7a62e
commit 6bc7ba1592
5 changed files with 32 additions and 20 deletions

View File

@@ -73,7 +73,7 @@
return AesSetIV(aes, iv); 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; word32 *enc_key, *iv;
CRYP_InitTypeDef AES_CRYP_InitStructure; CRYP_InitTypeDef AES_CRYP_InitStructure;
@@ -174,9 +174,11 @@
/* disable crypto processor */ /* disable crypto processor */
CRYP_Cmd(DISABLE); 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; word32 *dec_key, *iv;
CRYP_InitTypeDef AES_CRYP_InitStructure; CRYP_InitTypeDef AES_CRYP_InitStructure;
@@ -293,6 +295,8 @@
/* disable crypto processor */ /* disable crypto processor */
CRYP_Cmd(DISABLE); CRYP_Cmd(DISABLE);
return 0;
} }
#ifdef CYASSL_AES_COUNTER #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; word32 blocks = sz / AES_BLOCK_SIZE;
@@ -1702,7 +1706,7 @@ void AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
aes->rounds); aes->rounds);
/* store iv for next call */ /* store iv for next call */
XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE); XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
return; return 0;
} }
#endif #endif
@@ -1714,10 +1718,12 @@ void AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
out += AES_BLOCK_SIZE; out += AES_BLOCK_SIZE;
in += 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; word32 blocks = sz / AES_BLOCK_SIZE;
@@ -1744,7 +1750,7 @@ void AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
aes->rounds); aes->rounds);
/* store iv for next call */ /* store iv for next call */
XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE); XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
return; return 0;
} }
#endif #endif
@@ -1757,6 +1763,8 @@ void AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
out += AES_BLOCK_SIZE; out += AES_BLOCK_SIZE;
in += AES_BLOCK_SIZE; in += AES_BLOCK_SIZE;
} }
return 0;
} }

View File

@@ -98,8 +98,8 @@ typedef struct Aes {
CYASSL_API int AesSetKey(Aes* aes, const byte* key, word32 len, const byte* iv, CYASSL_API int AesSetKey(Aes* aes, const byte* key, word32 len, const byte* iv,
int dir); int dir);
CYASSL_API int AesSetIV(Aes* aes, const byte* iv); CYASSL_API int AesSetIV(Aes* aes, const byte* iv);
CYASSL_API void AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz); CYASSL_API int 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 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 AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz);
CYASSL_API void AesEncryptDirect(Aes* aes, byte* out, const byte* in); CYASSL_API void AesEncryptDirect(Aes* aes, byte* out, const byte* in);
CYASSL_API void AesDecryptDirect(Aes* aes, byte* out, const byte* in); CYASSL_API void AesDecryptDirect(Aes* aes, byte* out, const byte* in);

View File

@@ -435,9 +435,7 @@ int CRYPT_AES_CBC_Encrypt(CRYPT_AES_CTX* aes, unsigned char* out,
if (aes == NULL || out == NULL || in == NULL) if (aes == NULL || out == NULL || in == NULL)
return BAD_FUNC_ARG; return BAD_FUNC_ARG;
AesCbcEncrypt((Aes*)aes, out, in, inSz); return AesCbcEncrypt((Aes*)aes, out, in, inSz);
return 0;
} }
@@ -448,9 +446,7 @@ int CRYPT_AES_CBC_Decrypt(CRYPT_AES_CTX* aes, unsigned char* out,
if (aes == NULL || out == NULL || in == NULL) if (aes == NULL || out == NULL || in == NULL)
return BAD_FUNC_ARG; return BAD_FUNC_ARG;
AesCbcDecrypt((Aes*)aes, out, in, inSz); return AesCbcDecrypt((Aes*)aes, out, in, inSz);
return 0;
} }

View File

@@ -3426,17 +3426,19 @@ static INLINE int Encrypt(CYASSL* ssl, byte* out, const byte* input, word32 sz)
case aes: case aes:
#ifdef CYASSL_AESNI #ifdef CYASSL_AESNI
if ((word)input % 16) { if ((word)input % 16) {
int ret;
byte* tmp = (byte*)XMALLOC(sz, ssl->heap, byte* tmp = (byte*)XMALLOC(sz, ssl->heap,
DYNAMIC_TYPE_TMP_BUFFER); DYNAMIC_TYPE_TMP_BUFFER);
if (tmp == NULL) return MEMORY_E; if (tmp == NULL) return MEMORY_E;
XMEMCPY(tmp, input, sz); XMEMCPY(tmp, input, sz);
AesCbcEncrypt(ssl->encrypt.aes, tmp, tmp, sz); ret = AesCbcEncrypt(ssl->encrypt.aes, tmp, tmp, sz);
XMEMCPY(out, tmp, sz); XMEMCPY(out, tmp, sz);
XFREE(tmp, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
break; break;
} }
#endif #endif
AesCbcEncrypt(ssl->encrypt.aes, out, input, sz); return AesCbcEncrypt(ssl->encrypt.aes, out, input, sz);
break; break;
#endif #endif
@@ -3610,7 +3612,7 @@ static INLINE int Decrypt(CYASSL* ssl, byte* plain, const byte* input,
#ifdef BUILD_AES #ifdef BUILD_AES
case aes: case aes:
AesCbcDecrypt(ssl->decrypt.aes, plain, input, sz); return AesCbcDecrypt(ssl->decrypt.aes, plain, input, sz);
break; break;
#endif #endif

View File

@@ -4997,6 +4997,7 @@ int CyaSSL_set_compression(CYASSL* ssl)
int CyaSSL_EVP_Cipher(CYASSL_EVP_CIPHER_CTX* ctx, byte* dst, byte* src, int CyaSSL_EVP_Cipher(CYASSL_EVP_CIPHER_CTX* ctx, byte* dst, byte* src,
word32 len) word32 len)
{ {
int ret = 0;
CYASSL_ENTER("CyaSSL_EVP_Cipher"); CYASSL_ENTER("CyaSSL_EVP_Cipher");
if (ctx == NULL || dst == NULL || src == NULL) { if (ctx == NULL || dst == NULL || src == NULL) {
@@ -5016,9 +5017,9 @@ int CyaSSL_set_compression(CYASSL* ssl)
case AES_256_CBC_TYPE : case AES_256_CBC_TYPE :
CYASSL_MSG("AES CBC"); CYASSL_MSG("AES CBC");
if (ctx->enc) if (ctx->enc)
AesCbcEncrypt(&ctx->cipher.aes, dst, src, len); ret = AesCbcEncrypt(&ctx->cipher.aes, dst, src, len);
else else
AesCbcDecrypt(&ctx->cipher.aes, dst, src, len); ret = AesCbcDecrypt(&ctx->cipher.aes, dst, src, len);
break; break;
#ifdef CYASSL_AES_COUNTER #ifdef CYASSL_AES_COUNTER
@@ -5058,6 +5059,11 @@ int CyaSSL_set_compression(CYASSL* ssl)
} }
} }
if (ret != 0) {
CYASSL_MSG("CyaSSL_EVP_Cipher failure");
return 0; /* failuer */
}
CYASSL_MSG("CyaSSL_EVP_Cipher success"); CYASSL_MSG("CyaSSL_EVP_Cipher success");
return 1; /* success */ return 1; /* success */
} }