base64url.c
Go to the documentation of this file.
1 /**
2  * @file base64url.c
3  * @brief Base64url encoding scheme
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/crypto.h"
36 #include "encoding/base64url.h"
37 
38 //Check crypto library configuration
39 #if (BASE64URL_SUPPORT == ENABLED)
40 
41 //Base64url encoding table
42 static const char_t base64urlEncTable[64] =
43 {
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', '-', '_'
48 };
49 
50 //Base64url decoding table
51 static const uint8_t base64urlDecTable[128] =
52 {
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
61 };
62 
63 
64 /**
65  * @brief Base64url encoding algorithm
66  * @param[in] input Input data to encode
67  * @param[in] inputLen Length of the data to encode
68  * @param[out] output NULL-terminated string encoded with Base64url algorithm
69  * @param[out] outputLen Length of the encoded string (optional parameter)
70  **/
71 
72 void base64urlEncode(const void *input, size_t inputLen, char_t *output,
73  size_t *outputLen)
74 {
75  size_t n;
76  uint8_t a;
77  uint8_t b;
78  uint8_t c;
79  uint8_t d;
80  const uint8_t *p;
81 
82  //Point to the first byte of the input data
83  p = (const uint8_t *) input;
84 
85  //Divide the input stream into blocks of 3 bytes
86  n = inputLen / 3;
87 
88  //A full encoding quantum is always completed at the end of a quantity
89  if(inputLen == (n * 3 + 1))
90  {
91  //The final quantum of encoding input is exactly 8 bits
92  if(input != NULL && output != NULL)
93  {
94  //Read input data
95  a = (p[n * 3] & 0xFC) >> 2;
96  b = (p[n * 3] & 0x03) << 4;
97 
98  //The final unit of encoded output will be two characters
99  output[n * 4] = base64urlEncTable[a];
100  output[n * 4 + 1] = base64urlEncTable[b];
101  output[n * 4 + 2] = '\0';
102  }
103 
104  //Length of the encoded string (excluding the terminating NULL)
105  if(outputLen != NULL)
106  {
107  *outputLen = n * 4 + 2;
108  }
109  }
110  else if(inputLen == (n * 3 + 2))
111  {
112  //The final quantum of encoding input is exactly 16 bits
113  if(input != NULL && output != NULL)
114  {
115  //Read input data
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;
119 
120  //The final unit of encoded output will be three characters followed
121  //by one "=" padding character
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';
126  }
127 
128  //Length of the encoded string (excluding the terminating NULL)
129  if(outputLen != NULL)
130  {
131  *outputLen = n * 4 + 3;
132  }
133  }
134  else
135  {
136  //The final quantum of encoding input is an integral multiple of 24 bits
137  if(output != NULL)
138  {
139  //The final unit of encoded output will be an integral multiple of 4
140  //characters
141  output[n * 4] = '\0';
142  }
143 
144  //Length of the encoded string (excluding the terminating NULL)
145  if(outputLen != NULL)
146  {
147  *outputLen = n * 4;
148  }
149  }
150 
151  //If the output parameter is NULL, then the function calculates the
152  //length of the resulting Base64url string without copying any data
153  if(input != NULL && output != NULL)
154  {
155  //The input data is processed block by block
156  while(n-- > 0)
157  {
158  //Read input data
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;
163 
164  //Map each 3-byte block to 4 printable characters using the Base64url
165  //character set
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];
170  }
171  }
172 }
173 
174 
175 /**
176  * @brief Base64url decoding algorithm
177  * @param[in] input Base64url-encoded string
178  * @param[in] inputLen Length of the encoded string
179  * @param[out] output Resulting decoded data
180  * @param[out] outputLen Length of the decoded data
181  * @return Error code
182  **/
183 
184 error_t base64urlDecode(const char_t *input, size_t inputLen, void *output,
185  size_t *outputLen)
186 {
187  error_t error;
188  uint32_t value;
189  uint_t c;
190  size_t i;
191  size_t n;
192  uint8_t *p;
193 
194  //Check parameters
195  if(input == NULL && inputLen != 0)
197  if(outputLen == NULL)
199 
200  //Check the length of the input string
201  if((inputLen % 4) == 1)
202  return ERROR_INVALID_LENGTH;
203 
204  //Initialize status code
205  error = NO_ERROR;
206 
207  //Point to the buffer where to write the decoded data
208  p = (uint8_t *) output;
209 
210  //Initialize variables
211  n = 0;
212  value = 0;
213 
214  //Process the Base64url-encoded string
215  for(i = 0; i < inputLen && !error; i++)
216  {
217  //Get current character
218  c = (uint_t) input[i];
219 
220  //Check the value of the current character
221  if(c < 128 && base64urlDecTable[c] < 64)
222  {
223  //Decode the current character
224  value = (value << 6) | base64urlDecTable[c];
225 
226  //Divide the input stream into blocks of 4 characters
227  if((i % 4) == 3)
228  {
229  //Map each 4-character block to 3 bytes
230  if(p != NULL)
231  {
232  p[n] = (value >> 16) & 0xFF;
233  p[n + 1] = (value >> 8) & 0xFF;
234  p[n + 2] = value & 0xFF;
235  }
236 
237  //Adjust the length of the decoded data
238  n += 3;
239  //Decode next block
240  value = 0;
241  }
242  }
243  else
244  {
245  //Implementations must reject the encoded data if it contains
246  //characters outside the base alphabet
247  error = ERROR_INVALID_CHARACTER;
248  }
249  }
250 
251  //Check status code
252  if(!error)
253  {
254  //All trailing pad characters are omitted in Base64url
255  if((inputLen % 4) == 2)
256  {
257  //The last block contains only 1 byte
258  if(p != NULL)
259  {
260  //Decode the last byte
261  p[n] = (value >> 4) & 0xFF;
262  }
263 
264  //Adjust the length of the decoded data
265  n++;
266  }
267  else if((inputLen % 4) == 3)
268  {
269  //The last block contains only 2 bytes
270  if(p != NULL)
271  {
272  //Decode the last two bytes
273  p[n] = (value >> 10) & 0xFF;
274  p[n + 1] = (value >> 2) & 0xFF;
275  }
276 
277  //Adjust the length of the decoded data
278  n += 2;
279  }
280  else
281  {
282  //No pad characters in this case
283  }
284  }
285 
286  //Total number of bytes that have been written
287  *outputLen = n;
288 
289  //Return status code
290  return error;
291 }
292 
293 #endif
uint8_t b
Definition: nbns_common.h:104
uint8_t a
Definition: ndp.h:411
uint8_t p
Definition: ndp.h:300
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
error_t
Error codes.
Definition: error.h:43
void base64urlEncode(const void *input, size_t inputLen, char_t *output, size_t *outputLen)
Base64url encoding algorithm.
Definition: base64url.c:72
error_t base64urlDecode(const char_t *input, size_t inputLen, void *output, size_t *outputLen)
Base64url decoding algorithm.
Definition: base64url.c:184
@ ERROR_INVALID_LENGTH
Definition: error.h:111
General definitions for cryptographic algorithms.
Base64url encoding scheme.
@ ERROR_INVALID_CHARACTER
Definition: error.h:110
char char_t
Definition: compiler_port.h:48
uint8_t n
uint8_t value[]
Definition: tcp.h:369
unsigned int uint_t
Definition: compiler_port.h:50
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t c
Definition: ndp.h:514