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 | { |
| 78 | unsigned low_mask = ( c - low ) >> 8; |
| 79 | unsigned high_mask = ( c - high - 1 ) >> 8; |
| 80 | return( ~low_mask & high_mask & 0xff ); |
| 81 | } |
| 82 | |
Gilles Peskine | b44517e | 2021-07-28 14:31:39 +0200 | [diff] [blame] | 83 | /* Given a value in the range 0..63, return the corresponding Base64 digit. |
| 84 | * The implementation assumes that letters are consecutive (e.g. ASCII |
| 85 | * but not EBCDIC). |
| 86 | */ |
| 87 | static unsigned char enc_char( unsigned char val ) |
| 88 | { |
| 89 | unsigned char digit = 0; |
| 90 | /* For each range of values, if val is in that range, mask digit with |
| 91 | * the corresponding value. Since val can only be in a single range, |
| 92 | * only at most one masking will change digit. */ |
| 93 | digit |= mask_of_range( 0, 25, val ) & ( 'A' + val ); |
| 94 | digit |= mask_of_range( 26, 51, val ) & ( 'a' + val - 26 ); |
| 95 | digit |= mask_of_range( 52, 61, val ) & ( '0' + val - 52 ); |
| 96 | digit |= mask_of_range( 62, 62, val ) & '+'; |
| 97 | digit |= mask_of_range( 63, 63, val ) & '/'; |
| 98 | return( digit ); |
| 99 | } |
| 100 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 101 | /* |
| 102 | * Encode a buffer into base64 format |
| 103 | */ |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 104 | 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] | 105 | const unsigned char *src, size_t slen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 106 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 107 | size_t i, n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 108 | int C1, C2, C3; |
| 109 | unsigned char *p; |
| 110 | |
| 111 | if( slen == 0 ) |
Manuel Pégourié-Gonnard | 65fc6a8 | 2015-01-28 16:49:26 +0000 | [diff] [blame] | 112 | { |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 113 | *olen = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 114 | return( 0 ); |
Manuel Pégourié-Gonnard | 65fc6a8 | 2015-01-28 16:49:26 +0000 | [diff] [blame] | 115 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 116 | |
Manuel Pégourié-Gonnard | 0aa45c2 | 2015-09-30 16:30:28 +0200 | [diff] [blame] | 117 | n = slen / 3 + ( slen % 3 != 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 118 | |
Manuel Pégourié-Gonnard | 2d70834 | 2015-10-05 15:23:11 +0100 | [diff] [blame] | 119 | if( n > ( BASE64_SIZE_T_MAX - 1 ) / 4 ) |
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 | *olen = BASE64_SIZE_T_MAX; |
Manuel Pégourié-Gonnard | 0aa45c2 | 2015-09-30 16:30:28 +0200 | [diff] [blame] | 122 | return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Manuel Pégourié-Gonnard | 0aa45c2 | 2015-09-30 16:30:28 +0200 | [diff] [blame] | 125 | n *= 4; |
| 126 | |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 127 | if( ( dlen < n + 1 ) || ( NULL == dst ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 128 | { |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 129 | *olen = n + 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 133 | n = ( slen / 3 ) * 3; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 134 | |
| 135 | for( i = 0, p = dst; i < n; i += 3 ) |
| 136 | { |
| 137 | C1 = *src++; |
| 138 | C2 = *src++; |
| 139 | C3 = *src++; |
| 140 | |
Gilles Peskine | b44517e | 2021-07-28 14:31:39 +0200 | [diff] [blame] | 141 | *p++ = enc_char( ( C1 >> 2 ) & 0x3F ); |
| 142 | *p++ = enc_char( ( ( ( C1 & 3 ) << 4 ) + ( C2 >> 4 ) ) & 0x3F ); |
| 143 | *p++ = enc_char( ( ( ( C2 & 15 ) << 2 ) + ( C3 >> 6 ) ) & 0x3F ); |
| 144 | *p++ = enc_char( C3 & 0x3F ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | if( i < slen ) |
| 148 | { |
| 149 | C1 = *src++; |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 150 | C2 = ( ( i + 1 ) < slen ) ? *src++ : 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 151 | |
Gilles Peskine | b44517e | 2021-07-28 14:31:39 +0200 | [diff] [blame] | 152 | *p++ = enc_char( ( C1 >> 2 ) & 0x3F ); |
| 153 | *p++ = enc_char( ( ( ( C1 & 3 ) << 4 ) + ( C2 >> 4 ) ) & 0x3F ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 155 | if( ( i + 1 ) < slen ) |
Gilles Peskine | b44517e | 2021-07-28 14:31:39 +0200 | [diff] [blame] | 156 | *p++ = enc_char( ( ( C2 & 15 ) << 2 ) & 0x3F ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 157 | else *p++ = '='; |
| 158 | |
| 159 | *p++ = '='; |
| 160 | } |
| 161 | |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 162 | *olen = p - dst; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 163 | *p = 0; |
| 164 | |
| 165 | return( 0 ); |
| 166 | } |
| 167 | |
Gilles Peskine | f4a0a27 | 2021-07-28 13:54:02 +0200 | [diff] [blame] | 168 | /* Given a Base64 digit, return its value. |
| 169 | * If c is not a Base64 digit ('A'..'Z', 'a'..'z', '0'..'9', '+' or '/'), |
| 170 | * return -1. |
| 171 | * |
| 172 | * The implementation assumes that letters are consecutive (e.g. ASCII |
| 173 | * but not EBCDIC). |
| 174 | * |
| 175 | * The implementation is constant-flow (no branch or memory access depending |
| 176 | * on the value of c) unless the compiler inlines and optimizes a specific |
| 177 | * access. |
| 178 | */ |
| 179 | static signed char dec_value( unsigned char c ) |
| 180 | { |
| 181 | unsigned char val = 0; |
| 182 | /* For each range of digits, if c is in that range, mask val with |
| 183 | * the corresponding value. Since c can only be in a single range, |
| 184 | * only at most one masking will change val. Set val to one plus |
| 185 | * the desired value so that it stays 0 if c is in none of the ranges. */ |
| 186 | val |= mask_of_range( 'A', 'Z', c ) & ( c - 'A' + 0 + 1 ); |
| 187 | val |= mask_of_range( 'a', 'z', c ) & ( c - 'a' + 26 + 1 ); |
| 188 | val |= mask_of_range( '0', '9', c ) & ( c - '0' + 52 + 1 ); |
| 189 | val |= mask_of_range( '+', '+', c ) & ( c - '+' + 62 + 1 ); |
| 190 | val |= mask_of_range( '/', '/', c ) & ( c - '/' + 63 + 1 ); |
| 191 | /* At this point, val is 0 if c is an invalid digit and v+1 if c is |
| 192 | * a digit with the value v. */ |
| 193 | return( val - 1 ); |
| 194 | } |
| 195 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 196 | /* |
| 197 | * Decode a base64-formatted buffer |
| 198 | */ |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 199 | 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] | 200 | const unsigned char *src, size_t slen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 201 | { |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 202 | size_t i; /* index in source */ |
| 203 | size_t n; /* number of digits or trailing = in source */ |
| 204 | uint32_t x; /* value accumulator */ |
Gilles Peskine | 231b67a | 2021-07-30 12:56:21 +0200 | [diff] [blame^] | 205 | unsigned accumulated_digits = 0; |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 206 | unsigned equals = 0; |
| 207 | int spaces_present = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 208 | unsigned char *p; |
| 209 | |
Manuel Pégourié-Gonnard | 64938c6 | 2014-10-15 21:45:39 +0200 | [diff] [blame] | 210 | /* First pass: check for validity and get output length */ |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 211 | for( i = n = 0; i < slen; i++ ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 212 | { |
Manuel Pégourié-Gonnard | 64938c6 | 2014-10-15 21:45:39 +0200 | [diff] [blame] | 213 | /* Skip spaces before checking for EOL */ |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 214 | spaces_present = 0; |
Manuel Pégourié-Gonnard | 64938c6 | 2014-10-15 21:45:39 +0200 | [diff] [blame] | 215 | while( i < slen && src[i] == ' ' ) |
| 216 | { |
| 217 | ++i; |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 218 | spaces_present = 1; |
Manuel Pégourié-Gonnard | 64938c6 | 2014-10-15 21:45:39 +0200 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /* Spaces at end of buffer are OK */ |
| 222 | if( i == slen ) |
| 223 | break; |
| 224 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 225 | if( ( slen - i ) >= 2 && |
| 226 | src[i] == '\r' && src[i + 1] == '\n' ) |
| 227 | continue; |
| 228 | |
| 229 | if( src[i] == '\n' ) |
| 230 | continue; |
| 231 | |
Manuel Pégourié-Gonnard | 64938c6 | 2014-10-15 21:45:39 +0200 | [diff] [blame] | 232 | /* Space inside a line is an error */ |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 233 | if( spaces_present ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 234 | return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER ); |
Manuel Pégourié-Gonnard | 64938c6 | 2014-10-15 21:45:39 +0200 | [diff] [blame] | 235 | |
Gilles Peskine | a47fdcf | 2021-07-28 11:33:04 +0200 | [diff] [blame] | 236 | if( src[i] > 127 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 237 | return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 238 | |
Gilles Peskine | a47fdcf | 2021-07-28 11:33:04 +0200 | [diff] [blame] | 239 | if( src[i] == '=' ) |
| 240 | { |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 241 | if( ++equals > 2 ) |
Gilles Peskine | a47fdcf | 2021-07-28 11:33:04 +0200 | [diff] [blame] | 242 | return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER ); |
| 243 | } |
| 244 | else |
| 245 | { |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 246 | if( equals != 0 ) |
Gilles Peskine | a47fdcf | 2021-07-28 11:33:04 +0200 | [diff] [blame] | 247 | return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER ); |
Gilles Peskine | f4a0a27 | 2021-07-28 13:54:02 +0200 | [diff] [blame] | 248 | if( dec_value( src[i] ) < 0 ) |
Gilles Peskine | a47fdcf | 2021-07-28 11:33:04 +0200 | [diff] [blame] | 249 | return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER ); |
| 250 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 251 | n++; |
| 252 | } |
| 253 | |
| 254 | if( n == 0 ) |
Simon Butcher | a45aa13 | 2015-10-05 00:26:36 +0100 | [diff] [blame] | 255 | { |
| 256 | *olen = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 257 | return( 0 ); |
Simon Butcher | a45aa13 | 2015-10-05 00:26:36 +0100 | [diff] [blame] | 258 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 259 | |
Simon Butcher | a29c5e9e | 2017-02-02 08:46:53 +0000 | [diff] [blame] | 260 | /* The following expression is to calculate the following formula without |
| 261 | * risk of integer overflow in n: |
| 262 | * n = ( ( n * 6 ) + 7 ) >> 3; |
| 263 | */ |
Andres AG | 4623d83 | 2017-01-18 17:21:03 +0000 | [diff] [blame] | 264 | n = ( 6 * ( n >> 3 ) ) + ( ( 6 * ( n & 0x7 ) + 7 ) >> 3 ); |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 265 | n -= equals; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 266 | |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 267 | if( dst == NULL || dlen < n ) |
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 | *olen = n; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 270 | return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 273 | equals = 0; |
Gilles Peskine | 231b67a | 2021-07-30 12:56:21 +0200 | [diff] [blame^] | 274 | for( x = 0, p = dst; i > 0; i--, src++ ) |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 275 | { |
Manuel Pégourié-Gonnard | 64938c6 | 2014-10-15 21:45:39 +0200 | [diff] [blame] | 276 | if( *src == '\r' || *src == '\n' || *src == ' ' ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 277 | continue; |
| 278 | |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 279 | x = x << 6; |
Gilles Peskine | a47fdcf | 2021-07-28 11:33:04 +0200 | [diff] [blame] | 280 | if( *src == '=' ) |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 281 | ++equals; |
Gilles Peskine | a47fdcf | 2021-07-28 11:33:04 +0200 | [diff] [blame] | 282 | else |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 283 | x |= dec_value( *src ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 284 | |
Gilles Peskine | 231b67a | 2021-07-30 12:56:21 +0200 | [diff] [blame^] | 285 | if( ++accumulated_digits == 4 ) |
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 | accumulated_digits = 0; |
Gilles Peskine | ea96b3a | 2021-07-28 14:20:06 +0200 | [diff] [blame] | 288 | *p++ = (unsigned char)( x >> 16 ); |
| 289 | if( equals <= 1 ) *p++ = (unsigned char)( x >> 8 ); |
| 290 | if( equals <= 0 ) *p++ = (unsigned char)( x ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 294 | *olen = p - dst; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 295 | |
| 296 | return( 0 ); |
| 297 | } |
| 298 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 299 | #if defined(MBEDTLS_SELF_TEST) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 300 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 301 | static const unsigned char base64_test_dec[64] = |
| 302 | { |
| 303 | 0x24, 0x48, 0x6E, 0x56, 0x87, 0x62, 0x5A, 0xBD, |
| 304 | 0xBF, 0x17, 0xD9, 0xA2, 0xC4, 0x17, 0x1A, 0x01, |
| 305 | 0x94, 0xED, 0x8F, 0x1E, 0x11, 0xB3, 0xD7, 0x09, |
| 306 | 0x0C, 0xB6, 0xE9, 0x10, 0x6F, 0x22, 0xEE, 0x13, |
| 307 | 0xCA, 0xB3, 0x07, 0x05, 0x76, 0xC9, 0xFA, 0x31, |
| 308 | 0x6C, 0x08, 0x34, 0xFF, 0x8D, 0xC2, 0x6C, 0x38, |
| 309 | 0x00, 0x43, 0xE9, 0x54, 0x97, 0xAF, 0x50, 0x4B, |
| 310 | 0xD1, 0x41, 0xBA, 0x95, 0x31, 0x5A, 0x0B, 0x97 |
| 311 | }; |
| 312 | |
| 313 | static const unsigned char base64_test_enc[] = |
| 314 | "JEhuVodiWr2/F9mixBcaAZTtjx4Rs9cJDLbpEG8i7hPK" |
| 315 | "swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw=="; |
| 316 | |
| 317 | /* |
| 318 | * Checkup routine |
| 319 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 320 | int mbedtls_base64_self_test( int verbose ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 321 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 322 | size_t len; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 323 | const unsigned char *src; |
| 324 | unsigned char buffer[128]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 325 | |
| 326 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 327 | mbedtls_printf( " Base64 encoding test: " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 328 | |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 329 | src = base64_test_dec; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 330 | |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 331 | if( mbedtls_base64_encode( buffer, sizeof( buffer ), &len, src, 64 ) != 0 || |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 332 | memcmp( base64_test_enc, buffer, 88 ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 333 | { |
| 334 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 335 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 336 | |
| 337 | return( 1 ); |
| 338 | } |
| 339 | |
| 340 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 341 | mbedtls_printf( "passed\n Base64 decoding test: " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 342 | |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 343 | src = base64_test_enc; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 344 | |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 345 | if( mbedtls_base64_decode( buffer, sizeof( buffer ), &len, src, 88 ) != 0 || |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 346 | memcmp( base64_test_dec, buffer, 64 ) != 0 ) |
| 347 | { |
| 348 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 349 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 350 | |
| 351 | return( 1 ); |
| 352 | } |
| 353 | |
| 354 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 355 | mbedtls_printf( "passed\n\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 356 | |
| 357 | return( 0 ); |
| 358 | } |
| 359 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 360 | #endif /* MBEDTLS_SELF_TEST */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 361 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 362 | #endif /* MBEDTLS_BASE64_C */ |