Merge pull request #5012 from julek-wolfssl/evp-enc-dec-block

Implement `EVP_EncodeBlock` and `EVP_DecodeBlock`
This commit is contained in:
David Garske 2022-04-01 12:24:23 -07:00 committed by GitHub
commit bd0e222fec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 0 deletions

View File

@ -3768,6 +3768,8 @@ static void test_wolfSSL_EVP_EncodeUpdate(void)
0 /* expected result code 0: fail */
);
AssertIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
/* meaningless parameter test */
AssertIntEQ(
@ -3807,6 +3809,15 @@ static void test_wolfSSL_EVP_EncodeUpdate(void)
(const char*)enc0,sizeof(enc0) ),
0);
XMEMSET( encOutBuff,0, sizeof(encOutBuff));
AssertIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
sizeof(enc0)-1);
AssertIntEQ(
XSTRNCMP(
(const char*)encOutBuff,
(const char*)enc0,sizeof(enc0) ),
0);
/* pass small size( < 48bytes ) input, then make sure they are not
* encoded and just stored in ctx
*/
@ -3986,6 +3997,8 @@ static void test_wolfSSL_EVP_DecodeUpdate(void)
);
AssertIntEQ( outl, 0);
AssertIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
/* pass zero length input */
AssertIntEQ(
@ -4031,6 +4044,11 @@ static void test_wolfSSL_EVP_DecodeUpdate(void)
AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
sizeof(plain2) -1 ),0);
AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
sizeof(plain2)-1);
AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
sizeof(plain2) -1 ),0);
/* decode correct base64 string which does not have '\n' in its last*/
const unsigned char enc3[] =
@ -4065,6 +4083,11 @@ static void test_wolfSSL_EVP_DecodeUpdate(void)
AssertIntEQ(outl,0 );
AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
sizeof(plain3)-1);
AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
sizeof(plain3) -1 ),0);
/* decode string which has a padding char ('=') in the illegal position*/
const unsigned char enc4[] =
@ -4084,6 +4107,8 @@ static void test_wolfSSL_EVP_DecodeUpdate(void)
);
AssertIntEQ(outl,0);
AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
/* small data decode test */
const unsigned char enc00[] = {"VG"};

View File

@ -9225,6 +9225,40 @@ void wolfSSL_EVP_ENCODE_CTX_free(WOLFSSL_EVP_ENCODE_CTX* ctx)
}
#endif /* WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE */
#if defined(WOLFSSL_BASE64_ENCODE)
/* Assume that out has enough space */
int wolfSSL_EVP_EncodeBlock(unsigned char *out, const unsigned char *in,
int inLen)
{
word32 ret = (word32)-1;
WOLFSSL_ENTER("wolfSSL_EVP_EncodeBlock");
if (out == NULL || in == NULL)
return WOLFSSL_FATAL_ERROR;
if (Base64_Encode(in, inLen, out, &ret) == 0)
return (int)ret;
else
return WOLFSSL_FATAL_ERROR;
}
/* Assume that out has enough space */
int wolfSSL_EVP_DecodeBlock(unsigned char *out, const unsigned char *in,
int inLen)
{
word32 ret = (word32)-1;
WOLFSSL_ENTER("wolfSSL_EVP_DecodeBlock");
if (out == NULL || in == NULL)
return WOLFSSL_FATAL_ERROR;
if (Base64_Decode(in, inLen, out, &ret) == 0)
return (int)ret;
else
return WOLFSSL_FATAL_ERROR;
}
/* wolfSSL_EVP_EncodeInit initializes specified WOLFSSL_EVP_ENCODE_CTX object
* for the subsequent wolfSSL_EVP_EncodeUpdate.
*/

View File

@ -456,6 +456,10 @@ WOLFSSL_API int wolfSSL_EVP_EncodeUpdate(WOLFSSL_EVP_ENCODE_CTX* ctx,
unsigned char*out, int *outl, const unsigned char*in, int inl);
WOLFSSL_API void wolfSSL_EVP_EncodeFinal(WOLFSSL_EVP_ENCODE_CTX* ctx,
unsigned char*out, int *outl);
WOLFSSL_API int wolfSSL_EVP_EncodeBlock(unsigned char *out,
const unsigned char *in, int inLen);
WOLFSSL_API int wolfSSL_EVP_DecodeBlock(unsigned char *out,
const unsigned char *in, int inLen);
#endif /* WOLFSSL_BASE64_ENCODE */
#if defined(WOLFSSL_BASE64_DECODE)
@ -1086,6 +1090,8 @@ WOLFSSL_API int wolfSSL_EVP_SignInit_ex(WOLFSSL_EVP_MD_CTX* ctx,
#define EVP_EncodeInit wolfSSL_EVP_EncodeInit
#define EVP_EncodeUpdate wolfSSL_EVP_EncodeUpdate
#define EVP_EncodeFinal wolfSSL_EVP_EncodeFinal
#define EVP_EncodeBlock wolfSSL_EVP_EncodeBlock
#define EVP_DecodeBlock wolfSSL_EVP_DecodeBlock
#endif /* WOLFSSL_BASE64_ENCODE */
#if defined(WOLFSSL_BASE64_DECODE)
#define EVP_DecodeInit wolfSSL_EVP_DecodeInit