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