diff --git a/src/headers/tomcrypt_custom.h b/src/headers/tomcrypt_custom.h index 7e58adf..107ae1a 100644 --- a/src/headers/tomcrypt_custom.h +++ b/src/headers/tomcrypt_custom.h @@ -383,6 +383,8 @@ /* Various tidbits of modern neatoness */ #define LTC_BASE64 +/* ... and it's URL safe version */ +#define LTC_BASE64_URL /* Keep LTC_NO_HKDF for compatibility reasons * superseeded by LTC_NO_MISC*/ diff --git a/src/headers/tomcrypt_misc.h b/src/headers/tomcrypt_misc.h index 47985ba..e1e4457 100644 --- a/src/headers/tomcrypt_misc.h +++ b/src/headers/tomcrypt_misc.h @@ -7,11 +7,13 @@ int base64_decode(const unsigned char *in, unsigned long len, unsigned char *out, unsigned long *outlen); #endif +#ifdef LTC_BASE64_URL int base64url_encode(const unsigned char *in, unsigned long len, unsigned char *out, unsigned long *outlen); int base64url_decode(const unsigned char *in, unsigned long len, unsigned char *out, unsigned long *outlen); +#endif /* ===> LTC_HKDF -- RFC5869 HMAC-based Key Derivation Function <=== */ #ifdef LTC_HKDF diff --git a/src/misc/base64/base64_decode.c b/src/misc/base64/base64_decode.c index 726a761..423dc43 100644 --- a/src/misc/base64/base64_decode.c +++ b/src/misc/base64/base64_decode.c @@ -17,8 +17,9 @@ */ -#ifdef LTC_BASE64 +#if defined(LTC_BASE64) || defined (LTC_BASE64_URL) +#if defined(LTC_BASE64) static const unsigned char map_base64[256] = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, @@ -42,7 +43,9 @@ static const unsigned char map_base64[256] = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 }; +#endif /* LTC_BASE64 */ +#if defined(LTC_BASE64_URL) static const unsigned char map_base64url[256] = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, @@ -66,8 +69,9 @@ static const unsigned char map_base64url[256] = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 }; +#endif /* LTC_BASE64_URL */ -int base64_decode_internal(const unsigned char *in, unsigned long inlen, +static int _base64_decode_internal(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, const unsigned char *map) { @@ -84,8 +88,8 @@ int base64_decode_internal(const unsigned char *in, unsigned long inlen, c = map[in[x]&0xFF]; if (c == 255) continue; /* the final = symbols are read and used to trim the remaining bytes */ - if (c == 254) { - c = 0; + if (c == 254) { + c = 0; /* prevent g < 0 which would potentially allow an overflow later */ if (--g < 0) { return CRYPT_INVALID_PACKET; @@ -98,8 +102,8 @@ int base64_decode_internal(const unsigned char *in, unsigned long inlen, t = (t<<6)|c; if (++y == 4) { - if (z + g > *outlen) { - return CRYPT_BUFFER_OVERFLOW; + if (z + g > *outlen) { + return CRYPT_BUFFER_OVERFLOW; } out[z++] = (unsigned char)((t>>16)&255); if (g > 1) out[z++] = (unsigned char)((t>>8)&255); @@ -114,6 +118,7 @@ int base64_decode_internal(const unsigned char *in, unsigned long inlen, return CRYPT_OK; } +#if defined(LTC_BASE64) /** base64 decode a block of memory @param in The base64 data to decode @@ -125,9 +130,11 @@ int base64_decode_internal(const unsigned char *in, unsigned long inlen, int base64_decode(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen) { - return base64_decode_internal(in, inlen, out, outlen, map_base64); + return _base64_decode_internal(in, inlen, out, outlen, map_base64); } +#endif /* LTC_BASE64 */ +#if defined(LTC_BASE64_URL) /** base64 (URL Safe, RFC 4648 section 5) decode a block of memory @param in The base64 data to decode @@ -139,8 +146,9 @@ int base64_decode(const unsigned char *in, unsigned long inlen, int base64url_decode(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen) { - return base64_decode_internal(in, inlen, out, outlen, map_base64url); + return _base64_decode_internal(in, inlen, out, outlen, map_base64url); } +#endif /* LTC_BASE64_URL */ #endif diff --git a/src/misc/base64/base64_encode.c b/src/misc/base64/base64_encode.c index a332761..0e1a7c1 100644 --- a/src/misc/base64/base64_encode.c +++ b/src/misc/base64/base64_encode.c @@ -17,15 +17,19 @@ */ -#ifdef LTC_BASE64 +#if defined(LTC_BASE64) || defined (LTC_BASE64_URL) +#if defined(LTC_BASE64) static const char *codes_base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +#endif /* LTC_BASE64 */ +#if defined(LTC_BASE64_URL) static const char *codes_base64url = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; +#endif /* LTC_BASE64_URL */ -int base64_encode_internal(const unsigned char *in, unsigned long inlen, +static int _base64_encode_internal(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, const char *codes, int pad) { @@ -75,6 +79,7 @@ int base64_encode_internal(const unsigned char *in, unsigned long inlen, return CRYPT_OK; } +#if defined(LTC_BASE64) /** base64 Encode a buffer (NUL terminated) @param in The input buffer to encode @@ -86,10 +91,12 @@ int base64_encode_internal(const unsigned char *in, unsigned long inlen, int base64_encode(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen) { - return base64_encode_internal(in, inlen, out, outlen, codes_base64, 1); + return _base64_encode_internal(in, inlen, out, outlen, codes_base64, 1); } +#endif /* LTC_BASE64 */ +#if defined(LTC_BASE64_URL) /** base64 (URL Safe, RFC 4648 section 5) Encode a buffer (NUL terminated) @param in The input buffer to encode @@ -101,8 +108,9 @@ int base64_encode(const unsigned char *in, unsigned long inlen, int base64url_encode(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen) { - return base64_encode_internal(in, inlen, out, outlen, codes_base64url, 0); + return _base64_encode_internal(in, inlen, out, outlen, codes_base64url, 0); } +#endif /* LTC_BASE64_URL */ #endif