32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
39 #if (BASE64URL_SUPPORT == ENABLED)
42 static const char_t base64urlEncTable[64] =
44 'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
45 'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
'a',
'b',
'c',
'd',
'e',
'f',
46 'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
47 'w',
'x',
'y',
'z',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'-',
'_'
51 static const uint8_t base64urlDecTable[128] =
53 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
54 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
55 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xFF,
56 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
57 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
58 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F,
59 0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
60 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
83 p = (
const uint8_t *) input;
89 if(inputLen == (
n * 3 + 1))
92 if(input != NULL && output != NULL)
95 a = (
p[
n * 3] & 0xFC) >> 2;
96 b = (
p[
n * 3] & 0x03) << 4;
99 output[
n * 4] = base64urlEncTable[
a];
100 output[
n * 4 + 1] = base64urlEncTable[
b];
101 output[
n * 4 + 2] =
'\0';
105 if(outputLen != NULL)
107 *outputLen =
n * 4 + 2;
110 else if(inputLen == (
n * 3 + 2))
113 if(input != NULL && output != NULL)
116 a = (
p[
n * 3] & 0xFC) >> 2;
117 b = ((
p[
n * 3] & 0x03) << 4) | ((
p[
n * 3 + 1] & 0xF0) >> 4);
118 c = (
p[
n * 3 + 1] & 0x0F) << 2;
122 output[
n * 4] = base64urlEncTable[
a];
123 output[
n * 4 + 1] = base64urlEncTable[
b];
124 output[
n * 4 + 2] = base64urlEncTable[
c];
125 output[
n * 4 + 3] =
'\0';
129 if(outputLen != NULL)
131 *outputLen =
n * 4 + 3;
141 output[
n * 4] =
'\0';
145 if(outputLen != NULL)
153 if(input != NULL && output != NULL)
159 a = (
p[
n * 3] & 0xFC) >> 2;
160 b = ((
p[
n * 3] & 0x03) << 4) | ((
p[
n * 3 + 1] & 0xF0) >> 4);
161 c = ((
p[
n * 3 + 1] & 0x0F) << 2) | ((
p[
n * 3 + 2] & 0xC0) >> 6);
162 d =
p[
n * 3 + 2] & 0x3F;
166 output[
n * 4] = base64urlEncTable[
a];
167 output[
n * 4 + 1] = base64urlEncTable[
b];
168 output[
n * 4 + 2] = base64urlEncTable[
c];
169 output[
n * 4 + 3] = base64urlEncTable[d];
195 if(input == NULL && inputLen != 0)
197 if(outputLen == NULL)
201 if((inputLen % 4) == 1)
208 p = (uint8_t *) output;
215 for(i = 0; i < inputLen && !error; i++)
221 if(
c < 128 && base64urlDecTable[
c] < 64)
233 p[
n + 1] = (
value >> 8) & 0xFF;
255 if((inputLen % 4) == 2)
267 else if((inputLen % 4) == 3)
274 p[
n + 1] = (
value >> 2) & 0xFF;