mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

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