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