Further cleanup of the Base64 and Base16 encoding start/min value.

This commit is contained in:
David Garske
2018-10-02 14:38:08 -07:00
parent be2f68d183
commit cfba86d3ab

View File

@ -37,7 +37,8 @@ enum {
BAD = 0xFF, /* invalid encoding */
PAD = '=',
PEM_LINE_SZ = 64,
START_HEX = 0x2B
BASE64_MIN = 0x2B,
BASE16_MIN = 0x30,
};
@ -60,7 +61,7 @@ int Base64_Decode(const byte* in, word32 inLen, byte* out, word32* outLen)
word32 i = 0;
word32 j = 0;
word32 plainSz = inLen - ((inLen + (PEM_LINE_SZ - 1)) / PEM_LINE_SZ );
const byte maxIdx = (byte)sizeof(base64Decode) + START_HEX - 1;
const byte maxIdx = (byte)sizeof(base64Decode) + BASE64_MIN - 1;
plainSz = (plainSz * 3 + 3) / 4;
if (plainSz > *outLen) return BAD_FUNC_ARG;
@ -82,7 +83,7 @@ int Base64_Decode(const byte* in, word32 inLen, byte* out, word32* outLen)
if (e4 == PAD)
pad4 = 1;
if (e1 < START_HEX || e2 < START_HEX || e3 < START_HEX || e4 < START_HEX) {
if (e1 < BASE64_MIN || e2 < BASE64_MIN || e3 < BASE64_MIN || e4 < BASE64_MIN) {
WOLFSSL_MSG("Bad Base64 Decode data, too small");
return ASN_INPUT_E;
}
@ -92,10 +93,10 @@ int Base64_Decode(const byte* in, word32 inLen, byte* out, word32* outLen)
return ASN_INPUT_E;
}
e1 = base64Decode[e1 - START_HEX];
e2 = base64Decode[e2 - START_HEX];
e3 = (e3 == PAD) ? 0 : base64Decode[e3 - START_HEX];
e4 = (e4 == PAD) ? 0 : base64Decode[e4 - START_HEX];
e1 = base64Decode[e1 - BASE64_MIN];
e2 = base64Decode[e2 - BASE64_MIN];
e3 = (e3 == PAD) ? 0 : base64Decode[e3 - BASE64_MIN];
e4 = (e4 == PAD) ? 0 : base64Decode[e4 - BASE64_MIN];
b1 = (byte)((e1 << 2) | (e2 >> 4));
b2 = (byte)(((e2 & 0xF) << 4) | (e3 >> 2));
@ -282,7 +283,7 @@ static int DoBase64_Encode(const byte* in, word32 inLen, byte* out,
inLen -= 3;
/* Insert newline after PEM_LINE_SZ, unless no \n requested */
if (escaped != WC_NO_NL_ENC && (++n % (PEM_LINE_SZ/4)) == 0 && inLen){
if (escaped != WC_NO_NL_ENC && (++n % (PEM_LINE_SZ/4)) == 0 && inLen) {
ret = CEscape(escaped, '\n', out, &i, *outLen, 1, getSzOnly);
if (ret != 0) break;
}
@ -370,7 +371,7 @@ int Base16_Decode(const byte* in, word32 inLen, byte* out, word32* outLen)
return BAD_FUNC_ARG;
if (inLen == 1 && *outLen && in) {
byte b = in[inIdx++] - 0x30; /* 0 starts at 0x30 */
byte b = in[inIdx++] - BASE16_MIN; /* 0 starts at 0x30 */
/* sanity check */
if (b >= sizeof(hexDecode)/sizeof(hexDecode[0]))
@ -394,8 +395,8 @@ int Base16_Decode(const byte* in, word32 inLen, byte* out, word32* outLen)
return BAD_FUNC_ARG;
while (inLen) {
byte b = in[inIdx++] - 0x30; /* 0 starts at 0x30 */
byte b2 = in[inIdx++] - 0x30;
byte b = in[inIdx++] - BASE16_MIN; /* 0 starts at 0x30 */
byte b2 = in[inIdx++] - BASE16_MIN;
/* sanity checks */
if (b >= sizeof(hexDecode)/sizeof(hexDecode[0]))