The WDCInterface is is a drop-in replacement for an EthernetInterface class that allows the user to connect to the Internet with a Wistron NeWeb Corporation (WNC) M14A2A Series data module using the standard network Socket API's. This interface class is used in the AT&T Cellular IoT Starter Kit which is sold by Avnet (http://cloudconnectkits.org/product/att-cellular-iot-starter-kit).

Dependencies:   WncControllerK64F

Dependents:   WNCProximityMqtt Pubnub_ATT_IoT_SK_WNC_sync BluemixDemo BluemixQS ... more

See the WNCInterface README in the Wiki tab for detailed information on this library.

Committer:
JMF
Date:
Tue Nov 01 14:22:56 2016 +0000
Revision:
12:0071cb144c7a
Adding mbedtls files

Who changed what in which revision?

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