Lee Shen / FTHR_USB_serial_qSPI
Committer:
boonshen
Date:
Tue Mar 13 21:12:00 2018 +0000
Revision:
0:a35c40f49345
MAX32630FTHR QuadSPI test

Who changed what in which revision?

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