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-2019 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 1.9.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  size_t i;
190  size_t n;
191  uint8_t *p;
192 
193  //Check parameters
194  if(input == NULL && inputLen != 0)
196  if(outputLen == NULL)
198 
199  //Check the length of the input string
200  if((inputLen % 4) == 1)
201  return ERROR_INVALID_LENGTH;
202 
203  //Initialize status code
204  error = NO_ERROR;
205 
206  //Point to the buffer where to write the decoded data
207  p = (uint8_t *) output;
208 
209  //Initialize variables
210  value = 0;
211  n = 0;
212 
213  //Process the Base64url-encoded string
214  for(i = 0; i < inputLen && !error; i++)
215  {
216  //Check the value of the current character
217  if(input[i] > 127 || base64urlDecTable[input[i]] > 63)
218  {
219  //The current character does not belong to the Base64url character set
220  error = ERROR_INVALID_CHARACTER;
221  }
222  else
223  {
224  //Decode the current character
225  value = (value << 6) | base64urlDecTable[input[i]];
226 
227  //Divide the input stream into blocks of 4 characters
228  if((i % 4) == 3)
229  {
230  //Map each 4-character block to 3 bytes
231  if(p != NULL)
232  {
233  p[n] = (value >> 16) & 0xFF;
234  p[n + 1] = (value >> 8) & 0xFF;
235  p[n + 2] = value & 0xFF;
236  }
237 
238  //Adjust the length of the decoded data
239  n += 3;
240  //Decode next block
241  value = 0;
242  }
243  }
244  }
245 
246  //Check status code
247  if(!error)
248  {
249  //All trailing pad characters are omitted in Base64url
250  if((inputLen % 4) == 2)
251  {
252  //The last block contains only 1 byte
253  if(p != NULL)
254  {
255  //Decode the last byte
256  p[n] = (value >> 4) & 0xFF;
257  }
258 
259  //Adjust the length of the decoded data
260  n++;
261  }
262  else if((inputLen % 4) == 3)
263  {
264  //The last block contains only 2 bytes
265  if(p != NULL)
266  {
267  //Decode the last two bytes
268  p[n] = (value >> 10) & 0xFF;
269  p[n + 1] = (value >> 2) & 0xFF;
270  }
271 
272  //Adjust the length of the decoded data
273  n += 2;
274  }
275  else
276  {
277  //No pad characters in this case
278  }
279  }
280 
281  //Total number of bytes that have been written
282  *outputLen = n;
283 
284  //Return status code
285  return error;
286 }
287 
288 #endif
Base64url encoding scheme.
char char_t
Definition: compiler_port.h:43
error_t base64urlDecode(const char_t *input, size_t inputLen, void *output, size_t *outputLen)
Base64url decoding algorithm.
Definition: base64url.c:184
uint8_t c
Definition: ndp.h:513
uint8_t p
Definition: ndp.h:298
General definitions for cryptographic algorithms.
Invalid parameter.
Definition: error.h:47
void base64urlEncode(const void *input, size_t inputLen, char_t *output, size_t *outputLen)
Base64url encoding algorithm.
Definition: base64url.c:72
uint8_t a
Definition: ndp.h:410
Success.
Definition: error.h:44
error_t
Error codes.
Definition: error.h:42
uint8_t value[]
Definition: dtls_misc.h:143
uint8_t n
uint8_t b[6]
Definition: dtls_misc.h:132