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