BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:fbdae7e6d805 1 /*
borlanic 0:fbdae7e6d805 2 * RFC 1521 base64 encoding/decoding
borlanic 0:fbdae7e6d805 3 *
borlanic 0:fbdae7e6d805 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
borlanic 0:fbdae7e6d805 5 * SPDX-License-Identifier: Apache-2.0
borlanic 0:fbdae7e6d805 6 *
borlanic 0:fbdae7e6d805 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
borlanic 0:fbdae7e6d805 8 * not use this file except in compliance with the License.
borlanic 0:fbdae7e6d805 9 * You may obtain a copy of the License at
borlanic 0:fbdae7e6d805 10 *
borlanic 0:fbdae7e6d805 11 * http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:fbdae7e6d805 12 *
borlanic 0:fbdae7e6d805 13 * Unless required by applicable law or agreed to in writing, software
borlanic 0:fbdae7e6d805 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
borlanic 0:fbdae7e6d805 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:fbdae7e6d805 16 * See the License for the specific language governing permissions and
borlanic 0:fbdae7e6d805 17 * limitations under the License.
borlanic 0:fbdae7e6d805 18 *
borlanic 0:fbdae7e6d805 19 * This file is part of mbed TLS (https://tls.mbed.org)
borlanic 0:fbdae7e6d805 20 */
borlanic 0:fbdae7e6d805 21
borlanic 0:fbdae7e6d805 22 #if !defined(MBEDTLS_CONFIG_FILE)
borlanic 0:fbdae7e6d805 23 #include "mbedtls/config.h"
borlanic 0:fbdae7e6d805 24 #else
borlanic 0:fbdae7e6d805 25 #include MBEDTLS_CONFIG_FILE
borlanic 0:fbdae7e6d805 26 #endif
borlanic 0:fbdae7e6d805 27
borlanic 0:fbdae7e6d805 28 #if defined(MBEDTLS_BASE64_C)
borlanic 0:fbdae7e6d805 29
borlanic 0:fbdae7e6d805 30 #include "mbedtls/base64.h"
borlanic 0:fbdae7e6d805 31
borlanic 0:fbdae7e6d805 32 #include <stdint.h>
borlanic 0:fbdae7e6d805 33
borlanic 0:fbdae7e6d805 34 #if defined(MBEDTLS_SELF_TEST)
borlanic 0:fbdae7e6d805 35 #include <string.h>
borlanic 0:fbdae7e6d805 36 #if defined(MBEDTLS_PLATFORM_C)
borlanic 0:fbdae7e6d805 37 #include "mbedtls/platform.h"
borlanic 0:fbdae7e6d805 38 #else
borlanic 0:fbdae7e6d805 39 #include <stdio.h>
borlanic 0:fbdae7e6d805 40 #define mbedtls_printf printf
borlanic 0:fbdae7e6d805 41 #endif /* MBEDTLS_PLATFORM_C */
borlanic 0:fbdae7e6d805 42 #endif /* MBEDTLS_SELF_TEST */
borlanic 0:fbdae7e6d805 43
borlanic 0:fbdae7e6d805 44 static const unsigned char base64_enc_map[64] =
borlanic 0:fbdae7e6d805 45 {
borlanic 0:fbdae7e6d805 46 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
borlanic 0:fbdae7e6d805 47 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
borlanic 0:fbdae7e6d805 48 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
borlanic 0:fbdae7e6d805 49 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
borlanic 0:fbdae7e6d805 50 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
borlanic 0:fbdae7e6d805 51 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
borlanic 0:fbdae7e6d805 52 '8', '9', '+', '/'
borlanic 0:fbdae7e6d805 53 };
borlanic 0:fbdae7e6d805 54
borlanic 0:fbdae7e6d805 55 static const unsigned char base64_dec_map[128] =
borlanic 0:fbdae7e6d805 56 {
borlanic 0:fbdae7e6d805 57 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
borlanic 0:fbdae7e6d805 58 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
borlanic 0:fbdae7e6d805 59 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
borlanic 0:fbdae7e6d805 60 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
borlanic 0:fbdae7e6d805 61 127, 127, 127, 62, 127, 127, 127, 63, 52, 53,
borlanic 0:fbdae7e6d805 62 54, 55, 56, 57, 58, 59, 60, 61, 127, 127,
borlanic 0:fbdae7e6d805 63 127, 64, 127, 127, 127, 0, 1, 2, 3, 4,
borlanic 0:fbdae7e6d805 64 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
borlanic 0:fbdae7e6d805 65 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
borlanic 0:fbdae7e6d805 66 25, 127, 127, 127, 127, 127, 127, 26, 27, 28,
borlanic 0:fbdae7e6d805 67 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
borlanic 0:fbdae7e6d805 68 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
borlanic 0:fbdae7e6d805 69 49, 50, 51, 127, 127, 127, 127, 127
borlanic 0:fbdae7e6d805 70 };
borlanic 0:fbdae7e6d805 71
borlanic 0:fbdae7e6d805 72 #define BASE64_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
borlanic 0:fbdae7e6d805 73
borlanic 0:fbdae7e6d805 74 /*
borlanic 0:fbdae7e6d805 75 * Encode a buffer into base64 format
borlanic 0:fbdae7e6d805 76 */
borlanic 0:fbdae7e6d805 77 int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
borlanic 0:fbdae7e6d805 78 const unsigned char *src, size_t slen )
borlanic 0:fbdae7e6d805 79 {
borlanic 0:fbdae7e6d805 80 size_t i, n;
borlanic 0:fbdae7e6d805 81 int C1, C2, C3;
borlanic 0:fbdae7e6d805 82 unsigned char *p;
borlanic 0:fbdae7e6d805 83
borlanic 0:fbdae7e6d805 84 if( slen == 0 )
borlanic 0:fbdae7e6d805 85 {
borlanic 0:fbdae7e6d805 86 *olen = 0;
borlanic 0:fbdae7e6d805 87 return( 0 );
borlanic 0:fbdae7e6d805 88 }
borlanic 0:fbdae7e6d805 89
borlanic 0:fbdae7e6d805 90 n = slen / 3 + ( slen % 3 != 0 );
borlanic 0:fbdae7e6d805 91
borlanic 0:fbdae7e6d805 92 if( n > ( BASE64_SIZE_T_MAX - 1 ) / 4 )
borlanic 0:fbdae7e6d805 93 {
borlanic 0:fbdae7e6d805 94 *olen = BASE64_SIZE_T_MAX;
borlanic 0:fbdae7e6d805 95 return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
borlanic 0:fbdae7e6d805 96 }
borlanic 0:fbdae7e6d805 97
borlanic 0:fbdae7e6d805 98 n *= 4;
borlanic 0:fbdae7e6d805 99
borlanic 0:fbdae7e6d805 100 if( ( dlen < n + 1 ) || ( NULL == dst ) )
borlanic 0:fbdae7e6d805 101 {
borlanic 0:fbdae7e6d805 102 *olen = n + 1;
borlanic 0:fbdae7e6d805 103 return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
borlanic 0:fbdae7e6d805 104 }
borlanic 0:fbdae7e6d805 105
borlanic 0:fbdae7e6d805 106 n = ( slen / 3 ) * 3;
borlanic 0:fbdae7e6d805 107
borlanic 0:fbdae7e6d805 108 for( i = 0, p = dst; i < n; i += 3 )
borlanic 0:fbdae7e6d805 109 {
borlanic 0:fbdae7e6d805 110 C1 = *src++;
borlanic 0:fbdae7e6d805 111 C2 = *src++;
borlanic 0:fbdae7e6d805 112 C3 = *src++;
borlanic 0:fbdae7e6d805 113
borlanic 0:fbdae7e6d805 114 *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
borlanic 0:fbdae7e6d805 115 *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
borlanic 0:fbdae7e6d805 116 *p++ = base64_enc_map[(((C2 & 15) << 2) + (C3 >> 6)) & 0x3F];
borlanic 0:fbdae7e6d805 117 *p++ = base64_enc_map[C3 & 0x3F];
borlanic 0:fbdae7e6d805 118 }
borlanic 0:fbdae7e6d805 119
borlanic 0:fbdae7e6d805 120 if( i < slen )
borlanic 0:fbdae7e6d805 121 {
borlanic 0:fbdae7e6d805 122 C1 = *src++;
borlanic 0:fbdae7e6d805 123 C2 = ( ( i + 1 ) < slen ) ? *src++ : 0;
borlanic 0:fbdae7e6d805 124
borlanic 0:fbdae7e6d805 125 *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
borlanic 0:fbdae7e6d805 126 *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
borlanic 0:fbdae7e6d805 127
borlanic 0:fbdae7e6d805 128 if( ( i + 1 ) < slen )
borlanic 0:fbdae7e6d805 129 *p++ = base64_enc_map[((C2 & 15) << 2) & 0x3F];
borlanic 0:fbdae7e6d805 130 else *p++ = '=';
borlanic 0:fbdae7e6d805 131
borlanic 0:fbdae7e6d805 132 *p++ = '=';
borlanic 0:fbdae7e6d805 133 }
borlanic 0:fbdae7e6d805 134
borlanic 0:fbdae7e6d805 135 *olen = p - dst;
borlanic 0:fbdae7e6d805 136 *p = 0;
borlanic 0:fbdae7e6d805 137
borlanic 0:fbdae7e6d805 138 return( 0 );
borlanic 0:fbdae7e6d805 139 }
borlanic 0:fbdae7e6d805 140
borlanic 0:fbdae7e6d805 141 /*
borlanic 0:fbdae7e6d805 142 * Decode a base64-formatted buffer
borlanic 0:fbdae7e6d805 143 */
borlanic 0:fbdae7e6d805 144 int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
borlanic 0:fbdae7e6d805 145 const unsigned char *src, size_t slen )
borlanic 0:fbdae7e6d805 146 {
borlanic 0:fbdae7e6d805 147 size_t i, n;
borlanic 0:fbdae7e6d805 148 uint32_t j, x;
borlanic 0:fbdae7e6d805 149 unsigned char *p;
borlanic 0:fbdae7e6d805 150
borlanic 0:fbdae7e6d805 151 /* First pass: check for validity and get output length */
borlanic 0:fbdae7e6d805 152 for( i = n = j = 0; i < slen; i++ )
borlanic 0:fbdae7e6d805 153 {
borlanic 0:fbdae7e6d805 154 /* Skip spaces before checking for EOL */
borlanic 0:fbdae7e6d805 155 x = 0;
borlanic 0:fbdae7e6d805 156 while( i < slen && src[i] == ' ' )
borlanic 0:fbdae7e6d805 157 {
borlanic 0:fbdae7e6d805 158 ++i;
borlanic 0:fbdae7e6d805 159 ++x;
borlanic 0:fbdae7e6d805 160 }
borlanic 0:fbdae7e6d805 161
borlanic 0:fbdae7e6d805 162 /* Spaces at end of buffer are OK */
borlanic 0:fbdae7e6d805 163 if( i == slen )
borlanic 0:fbdae7e6d805 164 break;
borlanic 0:fbdae7e6d805 165
borlanic 0:fbdae7e6d805 166 if( ( slen - i ) >= 2 &&
borlanic 0:fbdae7e6d805 167 src[i] == '\r' && src[i + 1] == '\n' )
borlanic 0:fbdae7e6d805 168 continue;
borlanic 0:fbdae7e6d805 169
borlanic 0:fbdae7e6d805 170 if( src[i] == '\n' )
borlanic 0:fbdae7e6d805 171 continue;
borlanic 0:fbdae7e6d805 172
borlanic 0:fbdae7e6d805 173 /* Space inside a line is an error */
borlanic 0:fbdae7e6d805 174 if( x != 0 )
borlanic 0:fbdae7e6d805 175 return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
borlanic 0:fbdae7e6d805 176
borlanic 0:fbdae7e6d805 177 if( src[i] == '=' && ++j > 2 )
borlanic 0:fbdae7e6d805 178 return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
borlanic 0:fbdae7e6d805 179
borlanic 0:fbdae7e6d805 180 if( src[i] > 127 || base64_dec_map[src[i]] == 127 )
borlanic 0:fbdae7e6d805 181 return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
borlanic 0:fbdae7e6d805 182
borlanic 0:fbdae7e6d805 183 if( base64_dec_map[src[i]] < 64 && j != 0 )
borlanic 0:fbdae7e6d805 184 return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
borlanic 0:fbdae7e6d805 185
borlanic 0:fbdae7e6d805 186 n++;
borlanic 0:fbdae7e6d805 187 }
borlanic 0:fbdae7e6d805 188
borlanic 0:fbdae7e6d805 189 if( n == 0 )
borlanic 0:fbdae7e6d805 190 {
borlanic 0:fbdae7e6d805 191 *olen = 0;
borlanic 0:fbdae7e6d805 192 return( 0 );
borlanic 0:fbdae7e6d805 193 }
borlanic 0:fbdae7e6d805 194
borlanic 0:fbdae7e6d805 195 /* The following expression is to calculate the following formula without
borlanic 0:fbdae7e6d805 196 * risk of integer overflow in n:
borlanic 0:fbdae7e6d805 197 * n = ( ( n * 6 ) + 7 ) >> 3;
borlanic 0:fbdae7e6d805 198 */
borlanic 0:fbdae7e6d805 199 n = ( 6 * ( n >> 3 ) ) + ( ( 6 * ( n & 0x7 ) + 7 ) >> 3 );
borlanic 0:fbdae7e6d805 200 n -= j;
borlanic 0:fbdae7e6d805 201
borlanic 0:fbdae7e6d805 202 if( dst == NULL || dlen < n )
borlanic 0:fbdae7e6d805 203 {
borlanic 0:fbdae7e6d805 204 *olen = n;
borlanic 0:fbdae7e6d805 205 return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
borlanic 0:fbdae7e6d805 206 }
borlanic 0:fbdae7e6d805 207
borlanic 0:fbdae7e6d805 208 for( j = 3, n = x = 0, p = dst; i > 0; i--, src++ )
borlanic 0:fbdae7e6d805 209 {
borlanic 0:fbdae7e6d805 210 if( *src == '\r' || *src == '\n' || *src == ' ' )
borlanic 0:fbdae7e6d805 211 continue;
borlanic 0:fbdae7e6d805 212
borlanic 0:fbdae7e6d805 213 j -= ( base64_dec_map[*src] == 64 );
borlanic 0:fbdae7e6d805 214 x = ( x << 6 ) | ( base64_dec_map[*src] & 0x3F );
borlanic 0:fbdae7e6d805 215
borlanic 0:fbdae7e6d805 216 if( ++n == 4 )
borlanic 0:fbdae7e6d805 217 {
borlanic 0:fbdae7e6d805 218 n = 0;
borlanic 0:fbdae7e6d805 219 if( j > 0 ) *p++ = (unsigned char)( x >> 16 );
borlanic 0:fbdae7e6d805 220 if( j > 1 ) *p++ = (unsigned char)( x >> 8 );
borlanic 0:fbdae7e6d805 221 if( j > 2 ) *p++ = (unsigned char)( x );
borlanic 0:fbdae7e6d805 222 }
borlanic 0:fbdae7e6d805 223 }
borlanic 0:fbdae7e6d805 224
borlanic 0:fbdae7e6d805 225 *olen = p - dst;
borlanic 0:fbdae7e6d805 226
borlanic 0:fbdae7e6d805 227 return( 0 );
borlanic 0:fbdae7e6d805 228 }
borlanic 0:fbdae7e6d805 229
borlanic 0:fbdae7e6d805 230 #if defined(MBEDTLS_SELF_TEST)
borlanic 0:fbdae7e6d805 231
borlanic 0:fbdae7e6d805 232 static const unsigned char base64_test_dec[64] =
borlanic 0:fbdae7e6d805 233 {
borlanic 0:fbdae7e6d805 234 0x24, 0x48, 0x6E, 0x56, 0x87, 0x62, 0x5A, 0xBD,
borlanic 0:fbdae7e6d805 235 0xBF, 0x17, 0xD9, 0xA2, 0xC4, 0x17, 0x1A, 0x01,
borlanic 0:fbdae7e6d805 236 0x94, 0xED, 0x8F, 0x1E, 0x11, 0xB3, 0xD7, 0x09,
borlanic 0:fbdae7e6d805 237 0x0C, 0xB6, 0xE9, 0x10, 0x6F, 0x22, 0xEE, 0x13,
borlanic 0:fbdae7e6d805 238 0xCA, 0xB3, 0x07, 0x05, 0x76, 0xC9, 0xFA, 0x31,
borlanic 0:fbdae7e6d805 239 0x6C, 0x08, 0x34, 0xFF, 0x8D, 0xC2, 0x6C, 0x38,
borlanic 0:fbdae7e6d805 240 0x00, 0x43, 0xE9, 0x54, 0x97, 0xAF, 0x50, 0x4B,
borlanic 0:fbdae7e6d805 241 0xD1, 0x41, 0xBA, 0x95, 0x31, 0x5A, 0x0B, 0x97
borlanic 0:fbdae7e6d805 242 };
borlanic 0:fbdae7e6d805 243
borlanic 0:fbdae7e6d805 244 static const unsigned char base64_test_enc[] =
borlanic 0:fbdae7e6d805 245 "JEhuVodiWr2/F9mixBcaAZTtjx4Rs9cJDLbpEG8i7hPK"
borlanic 0:fbdae7e6d805 246 "swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw==";
borlanic 0:fbdae7e6d805 247
borlanic 0:fbdae7e6d805 248 /*
borlanic 0:fbdae7e6d805 249 * Checkup routine
borlanic 0:fbdae7e6d805 250 */
borlanic 0:fbdae7e6d805 251 int mbedtls_base64_self_test( int verbose )
borlanic 0:fbdae7e6d805 252 {
borlanic 0:fbdae7e6d805 253 size_t len;
borlanic 0:fbdae7e6d805 254 const unsigned char *src;
borlanic 0:fbdae7e6d805 255 unsigned char buffer[128];
borlanic 0:fbdae7e6d805 256
borlanic 0:fbdae7e6d805 257 if( verbose != 0 )
borlanic 0:fbdae7e6d805 258 mbedtls_printf( " Base64 encoding test: " );
borlanic 0:fbdae7e6d805 259
borlanic 0:fbdae7e6d805 260 src = base64_test_dec;
borlanic 0:fbdae7e6d805 261
borlanic 0:fbdae7e6d805 262 if( mbedtls_base64_encode( buffer, sizeof( buffer ), &len, src, 64 ) != 0 ||
borlanic 0:fbdae7e6d805 263 memcmp( base64_test_enc, buffer, 88 ) != 0 )
borlanic 0:fbdae7e6d805 264 {
borlanic 0:fbdae7e6d805 265 if( verbose != 0 )
borlanic 0:fbdae7e6d805 266 mbedtls_printf( "failed\n" );
borlanic 0:fbdae7e6d805 267
borlanic 0:fbdae7e6d805 268 return( 1 );
borlanic 0:fbdae7e6d805 269 }
borlanic 0:fbdae7e6d805 270
borlanic 0:fbdae7e6d805 271 if( verbose != 0 )
borlanic 0:fbdae7e6d805 272 mbedtls_printf( "passed\n Base64 decoding test: " );
borlanic 0:fbdae7e6d805 273
borlanic 0:fbdae7e6d805 274 src = base64_test_enc;
borlanic 0:fbdae7e6d805 275
borlanic 0:fbdae7e6d805 276 if( mbedtls_base64_decode( buffer, sizeof( buffer ), &len, src, 88 ) != 0 ||
borlanic 0:fbdae7e6d805 277 memcmp( base64_test_dec, buffer, 64 ) != 0 )
borlanic 0:fbdae7e6d805 278 {
borlanic 0:fbdae7e6d805 279 if( verbose != 0 )
borlanic 0:fbdae7e6d805 280 mbedtls_printf( "failed\n" );
borlanic 0:fbdae7e6d805 281
borlanic 0:fbdae7e6d805 282 return( 1 );
borlanic 0:fbdae7e6d805 283 }
borlanic 0:fbdae7e6d805 284
borlanic 0:fbdae7e6d805 285 if( verbose != 0 )
borlanic 0:fbdae7e6d805 286 mbedtls_printf( "passed\n\n" );
borlanic 0:fbdae7e6d805 287
borlanic 0:fbdae7e6d805 288 return( 0 );
borlanic 0:fbdae7e6d805 289 }
borlanic 0:fbdae7e6d805 290
borlanic 0:fbdae7e6d805 291 #endif /* MBEDTLS_SELF_TEST */
borlanic 0:fbdae7e6d805 292
borlanic 0:fbdae7e6d805 293 #endif /* MBEDTLS_BASE64_C */