Base16/64 improvements:

* Add define `WOLFSSL_BASE16` to explicitly expose base16 support.
* Add `./configure --enable-base16` option (disabled by default in configure, but enabled in coding.h when required internally).
* Added base16 tests in test.c `base16_test`.
* Enabled base64 decode tests when `WOLFSSL_BASE64_ENCODE` is not defined.
This commit is contained in:
David Garske 2018-03-22 10:36:56 -07:00
parent 2989c73411
commit 3bf325290d
4 changed files with 75 additions and 9 deletions

View File

@ -1759,6 +1759,18 @@ then
fi
# Base16
AC_ARG_ENABLE([base16],
[AS_HELP_STRING([--enable-base16],[Enable Base16 encoding/decoding (default: disabled)])],
[ ENABLED_BASE16=$enableval ],
[ ENABLED_BASE16=no ]
)
if test "$ENABLED_BASE16" = "yes"
then
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_BASE16"
fi
# DES3
AC_ARG_ENABLE([des3],
[AS_HELP_STRING([--enable-des3],[Enable DES3 (default: disabled)])],

View File

@ -344,12 +344,10 @@ int Base64_Encode_NoNl(const byte* in, word32 inLen, byte* out, word32* outLen)
return DoBase64_Encode(in, inLen, out, outLen, WC_NO_NL_ENC);
}
#endif /* defined(WOLFSSL_BASE64_ENCODE) */
#endif /* WOLFSSL_BASE64_ENCODE */
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
defined(HAVE_WEBSERVER) || defined(HAVE_FIPS) || \
defined(HAVE_ECC_CDH) || defined(HAVE_SELFTEST)
#ifdef WOLFSSL_BASE16
static
const byte hexDecode[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
@ -455,6 +453,6 @@ int Base16_Encode(const byte* in, word32 inLen, byte* out, word32* outLen)
return 0;
}
#endif /* (OPENSSL_EXTRA) || (HAVE_WEBSERVER) || (HAVE_FIPS) */
#endif /* WOLFSSL_BASE16 */
#endif /* NO_CODING */
#endif /* !NO_CODING */

View File

@ -218,6 +218,7 @@ typedef struct testVector {
int error_test(void);
int base64_test(void);
int base16_test(void);
int asn_test(void);
int md2_test(void);
int md5_test(void);
@ -455,12 +456,18 @@ initDefaultName();
else
printf( "error test passed!\n");
#if !defined(NO_CODING) && defined(WOLFSSL_BASE64_ENCODE)
#ifndef NO_CODING
if ( (ret = base64_test()) != 0)
return err_sys("base64 test failed!\n", ret);
else
printf( "base64 test passed!\n");
#ifdef WOLFSSL_BASE16
if ( (ret = base16_test()) != 0)
return err_sys("base16 test failed!\n", ret);
else
printf( "base16 test passed!\n");
#endif
#endif /* !NO_CODING */
#ifndef NO_ASN
if ( (ret = asn_test()) != 0)
@ -1114,7 +1121,8 @@ int error_test(void)
return 0;
}
#if !defined(NO_CODING) && defined(WOLFSSL_BASE64_ENCODE)
#ifndef NO_CODING
int base64_test(void)
{
int ret;
@ -1122,10 +1130,12 @@ int base64_test(void)
const byte goodEnd[] = "A+Gd \r\n";
byte out[128];
word32 outLen;
#ifdef WOLFSSL_BASE64_ENCODE
byte data[3];
word32 dataLen;
byte longData[79] = { 0 };
const byte symbols[] = "+/A=";
#endif
const byte badSmall[] = "AAA Gdj=";
const byte badLarge[] = "AAA~Gdj=";
const byte badEOL[] = "A+Gd ";
@ -1162,6 +1172,7 @@ int base64_test(void)
return -1214 - i;
}
#ifdef WOLFSSL_BASE64_ENCODE
/* Decode and encode all symbols - non-alphanumeric. */
dataLen = sizeof(data);
ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
@ -1206,10 +1217,49 @@ int base64_test(void)
ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
if (ret != 0)
return -1233;
#endif
return 0;
}
#endif
#ifdef WOLFSSL_BASE16
int base16_test(void)
{
int ret;
const byte testData[] = "SomeDataToEncode\n";
const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
byte encoded[40];
word32 encodedLen;
byte plain[40];
word32 len;
/* length returned includes null termination */
encodedLen = sizeof(encoded);
ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
if (ret != 0)
return -1234;
len = (word32)XSTRLEN((char*)encoded);
if (len != encodedLen - 1)
return -1235;
len = sizeof(plain);
ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
if (ret != 0)
return -1236;
if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
return -1237;
if (encodedLen != sizeof(encodedTestData) ||
XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
return -1238;
}
return 0;
}
#endif /* WOLFSSL_BASE16 */
#endif /* !NO_CODING */
#ifndef NO_ASN
int asn_test(void)

View File

@ -66,6 +66,12 @@ WOLFSSL_API int Base64_Decode(const byte* in, word32 inLen, byte* out,
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
defined(HAVE_WEBSERVER) || defined(HAVE_FIPS) || \
defined(HAVE_ECC_CDH) || defined(HAVE_SELFTEST)
#ifndef WOLFSSL_BASE16
#define WOLFSSL_BASE16
#endif
#endif
#ifdef WOLFSSL_BASE16
WOLFSSL_API
int Base16_Decode(const byte* in, word32 inLen, byte* out, word32* outLen);
WOLFSSL_API