Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * RFC 1521 base64 encoding/decoding |
| 3 | * |
Bence Szépkúti | a2947ac | 2020-08-19 16:37:36 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | * |
| 7 | * This file is provided under the Apache License 2.0, or the |
| 8 | * GNU General Public License v2.0 or later. |
| 9 | * |
| 10 | * ********** |
| 11 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 24 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 25 | * ********** |
| 26 | * |
| 27 | * ********** |
| 28 | * GNU General Public License v2.0 or later: |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or modify |
| 31 | * it under the terms of the GNU General Public License as published by |
| 32 | * the Free Software Foundation; either version 2 of the License, or |
| 33 | * (at your option) any later version. |
| 34 | * |
| 35 | * This program is distributed in the hope that it will be useful, |
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | * GNU General Public License for more details. |
| 39 | * |
| 40 | * You should have received a copy of the GNU General Public License along |
| 41 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 42 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 43 | * |
| 44 | * ********** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 45 | */ |
| 46 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 47 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 48 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 49 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 50 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 51 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 52 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | #if defined(MBEDTLS_BASE64_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 54 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 55 | #include "mbedtls/base64.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 56 | |
Manuel Pégourié-Gonnard | 9386664 | 2015-06-22 19:21:23 +0200 | [diff] [blame] | 57 | #include <stdint.h> |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 58 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | #if defined(MBEDTLS_SELF_TEST) |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 60 | #include <string.h> |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 62 | #include "mbedtls/platform.h" |
Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 63 | #else |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 64 | #include <stdio.h> |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 65 | #define mbedtls_printf printf |
| 66 | #endif /* MBEDTLS_PLATFORM_C */ |
| 67 | #endif /* MBEDTLS_SELF_TEST */ |
Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 68 | |
Manuel Pégourié-Gonnard | 2d70834 | 2015-10-05 15:23:11 +0100 | [diff] [blame] | 69 | #define BASE64_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */ |
| 70 | |
Gilles Peskine | f4a0a27 | 2021-07-28 13:54:02 +0200 | [diff] [blame] | 71 | /* Return 0xff if low <= c <= high, 0 otherwise. |
| 72 | * |
| 73 | * Constant flow with respect to c. |
| 74 | */ |
| 75 | static unsigned char mask_of_range( unsigned char low, unsigned char high, |
| 76 | unsigned char c ) |
| 77 | { |
Gilles Peskine | bbf97cd | 2021-07-30 12:57:22 +0200 | [diff] [blame] | 78 | /* low_mask is: 0 if low <= c, 0x...ff if low > c */ |
| 79 | unsigned low_mask = ( (unsigned) c - low ) >> 8; |
Gilles Peskine | cda1281 | 2021-10-25 21:13:27 +0200 | [diff] [blame] | 80 | /* high_mask is: 0 if c <= high, 0x...ff if c > high */ |
Gilles Peskine | bbf97cd | 2021-07-30 12:57:22 +0200 | [diff] [blame] | 81 | unsigned high_mask = ( (unsigned) high - c ) >> 8; |
| 82 | return( ~( low_mask | high_mask ) & 0xff ); |
Gilles Peskine | f4a0a27 | 2021-07-28 13:54:02 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Gilles Peskine | b44517e | 2021-07-28 14:31:39 +0200 | [diff] [blame] | 85 | /* Given a value in the range 0..63, return the corresponding Base64 digit. |
| 86 | * The implementation assumes that letters are consecutive (e.g. ASCII |
| 87 | * but not EBCDIC). |
| 88 | */ |
| 89 | static unsigned char enc_char( unsigned char val ) |
| 90 | { |
| 91 | unsigned char digit = 0; |
| 92 | /* For each range of values, if val is in that range, mask digit with |
| 93 | * the corresponding value. Since val can only be in a single range, |
| 94 | * only at most one masking will change digit. */ |
| 95 | digit |= mask_of_range( 0, 25, val ) & ( 'A' + val ); |
| 96 | digit |= mask_of_range( 26, 51, val ) & ( 'a' + val - 26 ); |
| 97 | digit |= mask_of_range( 52, 61, val ) & ( '0' + val - 52 ); |
| 98 | digit |= mask_of_range( 62, 62, val ) & '+'; |
| 99 | digit |= mask_of_range( 63, 63, val ) & '/'; |
| 100 | return( digit ); |
| 101 | } |
| 102 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 103 | /* |
| 104 | * Encode a buffer into base64 format |
| 105 | */ |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 106 | int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 107 | const unsigned char *src, size_t slen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 108 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 109 | size_t i, n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 110 | int C1, C2, C3; |
| 111 | unsigned char *p; |
| 112 | |
| 113 | if( slen == 0 ) |
Manuel Pégourié-Gonnard | 65fc6a8 | 2015-01-28 16:49:26 +0000 | [diff] [blame] | 114 | { |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 115 | *olen = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 116 | return( 0 ); |
Manuel Pégourié-Gonnard | 65fc6a8 | 2015-01-28 16:49:26 +0000 | [diff] [blame] | 117 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 118 | |
Manuel Pégourié-Gonnard | 0aa45c2 | 2015-09-30 16:30:28 +0200 | [diff] [blame] | 119 | n = slen / 3 + ( slen % 3 != 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 120 | |
Manuel Pégourié-Gonnard | 2d70834 | 2015-10-05 15:23:11 +0100 | [diff] [blame] | 121 | if( n > ( BASE64_SIZE_T_MAX - 1 ) / 4 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 122 | { |
Manuel Pégourié-Gonnard | 2d70834 | 2015-10-05 15:23:11 +0100 | [diff] [blame] | 123 | *olen = BASE64_SIZE_T_MAX; |
Manuel Pégourié-Gonnard | 0aa45c2 | 2015-09-30 16:30:28 +0200 | [diff] [blame] | 124 | return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Manuel Pégourié-Gonnard | 0aa45c2 | 2015-09-30 16:30:28 +0200 | [diff] [blame] | 127 | n *= 4; |
| 128 | |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 129 | if( ( dlen < n + 1 ) || ( NULL == dst ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 130 | { |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 131 | *olen = n + 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 132 | return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 135 | n = ( slen / 3 ) * 3; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 136 | |
| 137 | for( i = 0, p = dst; i < n; i += 3 ) |
| 138 | { |
| 139 | C1 = *src++; |
| 140 | C2 = *src++; |
| 141 | C3 = *src++; |
| 142 | |
Gilles Peskine | b44517e | 2021-07-28 14:31:39 +0200 | [diff] [blame] | 143 | *p++ = enc_char( ( C1 >> 2 ) & 0x3F ); |
| 144 | *p++ = enc_char( ( ( ( C1 & 3 ) << 4 ) + ( C2 >> 4 ) ) & 0x3F ); |
| 145 | *p++ = enc_char( ( ( ( C2 & 15 ) << 2 ) + ( C3 >> 6 ) ) & 0x3F ); |
| 146 | *p++ = enc_char( C3 & 0x3F ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | if( i < slen ) |
| 150 | { |
| 151 | C1 = *src++; |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 152 | C2 = ( ( i + 1 ) < slen ) ? *src++ : 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 153 | |
Gilles Peskine | b44517e | 2021-07-28 14:31:39 +0200 | [diff] [blame] | 154 | *p++ = enc_char( ( C1 >> 2 ) & 0x3F ); |
| 155 | *p++ = enc_char( ( ( ( C1 & 3 ) << 4 ) + ( C2 >> 4 ) ) & 0x3F ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 156 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 157 | if( ( i + 1 ) < slen ) |
Gilles Peskine | b44517e | 2021-07-28 14:31:39 +0200 | [diff] [blame] | 158 | *p++ = enc_char( ( ( C2 & 15 ) << 2 ) & 0x3F ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 159 | else *p++ = '='; |
| 160 | |
| 161 | *p++ = '='; |
| 162 | } |
| 163 | |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 164 | *olen = p - dst; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 165 | *p = 0; |
| 166 | |
| 167 | return( 0 ); |
| 168 | } |
| 169 | |
Gilles Peskine | f4a0a27 | 2021-07-28 13:54:02 +0200 | [diff] [blame] | 170 | /* Given a Base64 digit, return its value. |
| 171 | * If c is not a Base64 digit ('A'..'Z', 'a'..'z', '0'..'9', '+' or '/'), |
| 172 | * return -1. |
| 173 | * |
| 174 | * The implementation assumes that letters are consecutive (e.g. ASCII |
| 175 | * but not EBCDIC). |
| 176 | * |
| 177 | * The implementation is constant-flow (no branch or memory access depending |
| 178 | * on the value of c) unless the compiler inlines and optimizes a specific |
| 179 | * access. |
| 180 | */ |
| 181 | static signed char dec_value( unsigned char c ) |
| 182 | { |
| 183 | unsigned char val = 0; |
| 184 | /* For each range of digits, if c is in that range, mask val with |
| 185 | * the corresponding value. Since c can only be in a single range, |
| 186 | * only at most one masking will change val. Set val to one plus |
| 187 | * the desired value so that it stays 0 if c is in none of the ranges. */ |
| 188 | val |= mask_of_range( 'A', 'Z', c ) & ( c - 'A' + 0 + 1 ); |
| 189 | val |= mask_of_range( 'a', 'z', c ) & ( c - 'a' + 26 + 1 ); |
| 190 | val |= mask_of_range( '0', '9', c ) & ( c - '0' + 52 + 1 ); |
| 191 | val |= mask_of_range( '+', '+', c ) & ( c - '+' + 62 + 1 ); |
| 192 | val |= mask_of_range( '/', '/', c ) & ( c - '/' + 63 + 1 ); |
| 193 | /* At this point, val is 0 if c is an invalid digit and v+1 if c is |
| 194 | * a digit with the value v. */ |
| 195 | return( val - 1 ); |
| 196 | } |
| 197 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 198 | /* |
| 199 | * Decode a base64-formatted buffer |
| 200 | */ |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 201 | int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 202 | const unsigned char *src, size_t slen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 203 | { |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 204 | size_t i; /* index in source */ |
| 205 | size_t n; /* number of digits or trailing = in source */ |
| 206 | uint32_t x; /* value accumulator */ |
Gilles Peskine | 231b67a | 2021-07-30 12:56:21 +0200 | [diff] [blame] | 207 | unsigned accumulated_digits = 0; |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 208 | unsigned equals = 0; |
| 209 | int spaces_present = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 210 | unsigned char *p; |
| 211 | |
Manuel Pégourié-Gonnard | 64938c6 | 2014-10-15 21:45:39 +0200 | [diff] [blame] | 212 | /* First pass: check for validity and get output length */ |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 213 | for( i = n = 0; i < slen; i++ ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 214 | { |
Manuel Pégourié-Gonnard | 64938c6 | 2014-10-15 21:45:39 +0200 | [diff] [blame] | 215 | /* Skip spaces before checking for EOL */ |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 216 | spaces_present = 0; |
Manuel Pégourié-Gonnard | 64938c6 | 2014-10-15 21:45:39 +0200 | [diff] [blame] | 217 | while( i < slen && src[i] == ' ' ) |
| 218 | { |
| 219 | ++i; |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 220 | spaces_present = 1; |
Manuel Pégourié-Gonnard | 64938c6 | 2014-10-15 21:45:39 +0200 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /* Spaces at end of buffer are OK */ |
| 224 | if( i == slen ) |
| 225 | break; |
| 226 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 227 | if( ( slen - i ) >= 2 && |
| 228 | src[i] == '\r' && src[i + 1] == '\n' ) |
| 229 | continue; |
| 230 | |
| 231 | if( src[i] == '\n' ) |
| 232 | continue; |
| 233 | |
Manuel Pégourié-Gonnard | 64938c6 | 2014-10-15 21:45:39 +0200 | [diff] [blame] | 234 | /* Space inside a line is an error */ |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 235 | if( spaces_present ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER ); |
Manuel Pégourié-Gonnard | 64938c6 | 2014-10-15 21:45:39 +0200 | [diff] [blame] | 237 | |
Gilles Peskine | a47fdcf | 2021-07-28 11:33:04 +0200 | [diff] [blame] | 238 | if( src[i] > 127 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 239 | return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 240 | |
Gilles Peskine | a47fdcf | 2021-07-28 11:33:04 +0200 | [diff] [blame] | 241 | if( src[i] == '=' ) |
| 242 | { |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 243 | if( ++equals > 2 ) |
Gilles Peskine | a47fdcf | 2021-07-28 11:33:04 +0200 | [diff] [blame] | 244 | return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER ); |
| 245 | } |
| 246 | else |
| 247 | { |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 248 | if( equals != 0 ) |
Gilles Peskine | a47fdcf | 2021-07-28 11:33:04 +0200 | [diff] [blame] | 249 | return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER ); |
Gilles Peskine | f4a0a27 | 2021-07-28 13:54:02 +0200 | [diff] [blame] | 250 | if( dec_value( src[i] ) < 0 ) |
Gilles Peskine | a47fdcf | 2021-07-28 11:33:04 +0200 | [diff] [blame] | 251 | return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER ); |
| 252 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 253 | n++; |
| 254 | } |
| 255 | |
| 256 | if( n == 0 ) |
Simon Butcher | a45aa13 | 2015-10-05 00:26:36 +0100 | [diff] [blame] | 257 | { |
| 258 | *olen = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 259 | return( 0 ); |
Simon Butcher | a45aa13 | 2015-10-05 00:26:36 +0100 | [diff] [blame] | 260 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 261 | |
Simon Butcher | a29c5e9e | 2017-02-02 08:46:53 +0000 | [diff] [blame] | 262 | /* The following expression is to calculate the following formula without |
| 263 | * risk of integer overflow in n: |
| 264 | * n = ( ( n * 6 ) + 7 ) >> 3; |
| 265 | */ |
Andres AG | 4623d83 | 2017-01-18 17:21:03 +0000 | [diff] [blame] | 266 | n = ( 6 * ( n >> 3 ) ) + ( ( 6 * ( n & 0x7 ) + 7 ) >> 3 ); |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 267 | n -= equals; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 268 | |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 269 | if( dst == NULL || dlen < n ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 270 | { |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 271 | *olen = n; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 272 | return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 275 | equals = 0; |
Gilles Peskine | 231b67a | 2021-07-30 12:56:21 +0200 | [diff] [blame] | 276 | for( x = 0, p = dst; i > 0; i--, src++ ) |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 277 | { |
Manuel Pégourié-Gonnard | 64938c6 | 2014-10-15 21:45:39 +0200 | [diff] [blame] | 278 | if( *src == '\r' || *src == '\n' || *src == ' ' ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 279 | continue; |
| 280 | |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 281 | x = x << 6; |
Gilles Peskine | a47fdcf | 2021-07-28 11:33:04 +0200 | [diff] [blame] | 282 | if( *src == '=' ) |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 283 | ++equals; |
Gilles Peskine | a47fdcf | 2021-07-28 11:33:04 +0200 | [diff] [blame] | 284 | else |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 285 | x |= dec_value( *src ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 286 | |
Gilles Peskine | 231b67a | 2021-07-30 12:56:21 +0200 | [diff] [blame] | 287 | if( ++accumulated_digits == 4 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 288 | { |
Gilles Peskine | 231b67a | 2021-07-30 12:56:21 +0200 | [diff] [blame] | 289 | accumulated_digits = 0; |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 290 | *p++ = (unsigned char)( x >> 16 ); |
| 291 | if( equals <= 1 ) *p++ = (unsigned char)( x >> 8 ); |
| 292 | if( equals <= 0 ) *p++ = (unsigned char)( x ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 296 | *olen = p - dst; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 297 | |
| 298 | return( 0 ); |
| 299 | } |
| 300 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 301 | #if defined(MBEDTLS_SELF_TEST) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 302 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 303 | static const unsigned char base64_test_dec[64] = |
| 304 | { |
| 305 | 0x24, 0x48, 0x6E, 0x56, 0x87, 0x62, 0x5A, 0xBD, |
| 306 | 0xBF, 0x17, 0xD9, 0xA2, 0xC4, 0x17, 0x1A, 0x01, |
| 307 | 0x94, 0xED, 0x8F, 0x1E, 0x11, 0xB3, 0xD7, 0x09, |
| 308 | 0x0C, 0xB6, 0xE9, 0x10, 0x6F, 0x22, 0xEE, 0x13, |
| 309 | 0xCA, 0xB3, 0x07, 0x05, 0x76, 0xC9, 0xFA, 0x31, |
| 310 | 0x6C, 0x08, 0x34, 0xFF, 0x8D, 0xC2, 0x6C, 0x38, |
| 311 | 0x00, 0x43, 0xE9, 0x54, 0x97, 0xAF, 0x50, 0x4B, |
| 312 | 0xD1, 0x41, 0xBA, 0x95, 0x31, 0x5A, 0x0B, 0x97 |
| 313 | }; |
| 314 | |
| 315 | static const unsigned char base64_test_enc[] = |
| 316 | "JEhuVodiWr2/F9mixBcaAZTtjx4Rs9cJDLbpEG8i7hPK" |
| 317 | "swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw=="; |
| 318 | |
| 319 | /* |
| 320 | * Checkup routine |
| 321 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 322 | int mbedtls_base64_self_test( int verbose ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 323 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 324 | size_t len; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 325 | const unsigned char *src; |
| 326 | unsigned char buffer[128]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 327 | |
| 328 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 329 | mbedtls_printf( " Base64 encoding test: " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 330 | |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 331 | src = base64_test_dec; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 332 | |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 333 | if( mbedtls_base64_encode( buffer, sizeof( buffer ), &len, src, 64 ) != 0 || |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 334 | memcmp( base64_test_enc, buffer, 88 ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 335 | { |
| 336 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 337 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 338 | |
| 339 | return( 1 ); |
| 340 | } |
| 341 | |
| 342 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 343 | mbedtls_printf( "passed\n Base64 decoding test: " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 344 | |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 345 | src = base64_test_enc; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 346 | |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 347 | if( mbedtls_base64_decode( buffer, sizeof( buffer ), &len, src, 88 ) != 0 || |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 348 | memcmp( base64_test_dec, buffer, 64 ) != 0 ) |
| 349 | { |
| 350 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 351 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 352 | |
| 353 | return( 1 ); |
| 354 | } |
| 355 | |
| 356 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 357 | mbedtls_printf( "passed\n\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 358 | |
| 359 | return( 0 ); |
| 360 | } |
| 361 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 362 | #endif /* MBEDTLS_SELF_TEST */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 363 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 364 | #endif /* MBEDTLS_BASE64_C */ |