diff --git a/configure.ac b/configure.ac index 36ff5e062..3fe2399ea 100644 --- a/configure.ac +++ b/configure.ac @@ -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)])], diff --git a/wolfcrypt/src/coding.c b/wolfcrypt/src/coding.c index 5a3296d2e..8797d48b6 100644 --- a/wolfcrypt/src/coding.c +++ b/wolfcrypt/src/coding.c @@ -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 */ diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 95c9c6218..c7f262c1b 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -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) diff --git a/wolfssl/wolfcrypt/coding.h b/wolfssl/wolfcrypt/coding.h index fbf3c02c2..edce5eb0e 100644 --- a/wolfssl/wolfcrypt/coding.h +++ b/wolfssl/wolfcrypt/coding.h @@ -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