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