nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nitsshukla
Date:
Fri Nov 04 12:06:04 2016 +0000
Revision:
7:3a65ef12ba31
Parent:
1:55a6170b404f
kghj;

Who changed what in which revision?

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