mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
Parent:
0:5b88d5760320
updated based on mbed-os5.15.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 0:5b88d5760320 1 /*
kenjiArai 0:5b88d5760320 2 * RFC 1321 compliant MD5 implementation
kenjiArai 0:5b88d5760320 3 *
kenjiArai 0:5b88d5760320 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
kenjiArai 0:5b88d5760320 5 * SPDX-License-Identifier: Apache-2.0
kenjiArai 0:5b88d5760320 6 *
kenjiArai 0:5b88d5760320 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
kenjiArai 0:5b88d5760320 8 * not use this file except in compliance with the License.
kenjiArai 0:5b88d5760320 9 * You may obtain a copy of the License at
kenjiArai 0:5b88d5760320 10 *
kenjiArai 0:5b88d5760320 11 * http://www.apache.org/licenses/LICENSE-2.0
kenjiArai 0:5b88d5760320 12 *
kenjiArai 0:5b88d5760320 13 * Unless required by applicable law or agreed to in writing, software
kenjiArai 0:5b88d5760320 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
kenjiArai 0:5b88d5760320 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kenjiArai 0:5b88d5760320 16 * See the License for the specific language governing permissions and
kenjiArai 0:5b88d5760320 17 * limitations under the License.
kenjiArai 0:5b88d5760320 18 *
kenjiArai 0:5b88d5760320 19 * This file is part of mbed TLS (https://tls.mbed.org)
kenjiArai 0:5b88d5760320 20 */
kenjiArai 0:5b88d5760320 21 /*
kenjiArai 0:5b88d5760320 22 * The MD5 algorithm was designed by Ron Rivest in 1991.
kenjiArai 0:5b88d5760320 23 *
kenjiArai 0:5b88d5760320 24 * http://www.ietf.org/rfc/rfc1321.txt
kenjiArai 0:5b88d5760320 25 */
kenjiArai 0:5b88d5760320 26
kenjiArai 0:5b88d5760320 27 #if !defined(MBEDTLS_CONFIG_FILE)
kenjiArai 0:5b88d5760320 28 #include "mbedtls/config.h"
kenjiArai 0:5b88d5760320 29 #else
kenjiArai 0:5b88d5760320 30 #include MBEDTLS_CONFIG_FILE
kenjiArai 0:5b88d5760320 31 #endif
kenjiArai 0:5b88d5760320 32
kenjiArai 0:5b88d5760320 33 #if defined(MBEDTLS_MD5_C)
kenjiArai 0:5b88d5760320 34
kenjiArai 0:5b88d5760320 35 #include "mbedtls/md5.h"
kenjiArai 0:5b88d5760320 36 #include "mbedtls/platform_util.h"
kenjiArai 0:5b88d5760320 37
kenjiArai 0:5b88d5760320 38 #include <string.h>
kenjiArai 0:5b88d5760320 39
kenjiArai 0:5b88d5760320 40 #if defined(MBEDTLS_SELF_TEST)
kenjiArai 0:5b88d5760320 41 #if defined(MBEDTLS_PLATFORM_C)
kenjiArai 0:5b88d5760320 42 #include "mbedtls/platform.h"
kenjiArai 0:5b88d5760320 43 #else
kenjiArai 0:5b88d5760320 44 #include <stdio.h>
kenjiArai 0:5b88d5760320 45 #define mbedtls_printf printf
kenjiArai 0:5b88d5760320 46 #endif /* MBEDTLS_PLATFORM_C */
kenjiArai 0:5b88d5760320 47 #endif /* MBEDTLS_SELF_TEST */
kenjiArai 0:5b88d5760320 48
kenjiArai 0:5b88d5760320 49 #if !defined(MBEDTLS_MD5_ALT)
kenjiArai 0:5b88d5760320 50
kenjiArai 0:5b88d5760320 51 /*
kenjiArai 0:5b88d5760320 52 * 32-bit integer manipulation macros (little endian)
kenjiArai 0:5b88d5760320 53 */
kenjiArai 0:5b88d5760320 54 #ifndef GET_UINT32_LE
kenjiArai 0:5b88d5760320 55 #define GET_UINT32_LE(n,b,i) \
kenjiArai 0:5b88d5760320 56 { \
kenjiArai 0:5b88d5760320 57 (n) = ( (uint32_t) (b)[(i) ] ) \
kenjiArai 0:5b88d5760320 58 | ( (uint32_t) (b)[(i) + 1] << 8 ) \
kenjiArai 0:5b88d5760320 59 | ( (uint32_t) (b)[(i) + 2] << 16 ) \
kenjiArai 0:5b88d5760320 60 | ( (uint32_t) (b)[(i) + 3] << 24 ); \
kenjiArai 0:5b88d5760320 61 }
kenjiArai 0:5b88d5760320 62 #endif
kenjiArai 0:5b88d5760320 63
kenjiArai 0:5b88d5760320 64 #ifndef PUT_UINT32_LE
kenjiArai 0:5b88d5760320 65 #define PUT_UINT32_LE(n,b,i) \
kenjiArai 0:5b88d5760320 66 { \
kenjiArai 0:5b88d5760320 67 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
kenjiArai 0:5b88d5760320 68 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
kenjiArai 0:5b88d5760320 69 (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
kenjiArai 0:5b88d5760320 70 (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
kenjiArai 0:5b88d5760320 71 }
kenjiArai 0:5b88d5760320 72 #endif
kenjiArai 0:5b88d5760320 73
kenjiArai 0:5b88d5760320 74 void mbedtls_md5_init( mbedtls_md5_context *ctx )
kenjiArai 0:5b88d5760320 75 {
kenjiArai 0:5b88d5760320 76 memset( ctx, 0, sizeof( mbedtls_md5_context ) );
kenjiArai 0:5b88d5760320 77 }
kenjiArai 0:5b88d5760320 78
kenjiArai 0:5b88d5760320 79 void mbedtls_md5_free( mbedtls_md5_context *ctx )
kenjiArai 0:5b88d5760320 80 {
kenjiArai 0:5b88d5760320 81 if( ctx == NULL )
kenjiArai 0:5b88d5760320 82 return;
kenjiArai 0:5b88d5760320 83
kenjiArai 0:5b88d5760320 84 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md5_context ) );
kenjiArai 0:5b88d5760320 85 }
kenjiArai 0:5b88d5760320 86
kenjiArai 0:5b88d5760320 87 void mbedtls_md5_clone( mbedtls_md5_context *dst,
kenjiArai 0:5b88d5760320 88 const mbedtls_md5_context *src )
kenjiArai 0:5b88d5760320 89 {
kenjiArai 0:5b88d5760320 90 *dst = *src;
kenjiArai 0:5b88d5760320 91 }
kenjiArai 0:5b88d5760320 92
kenjiArai 0:5b88d5760320 93 /*
kenjiArai 0:5b88d5760320 94 * MD5 context setup
kenjiArai 0:5b88d5760320 95 */
kenjiArai 0:5b88d5760320 96 int mbedtls_md5_starts_ret( mbedtls_md5_context *ctx )
kenjiArai 0:5b88d5760320 97 {
kenjiArai 0:5b88d5760320 98 ctx->total[0] = 0;
kenjiArai 0:5b88d5760320 99 ctx->total[1] = 0;
kenjiArai 0:5b88d5760320 100
kenjiArai 0:5b88d5760320 101 ctx->state[0] = 0x67452301;
kenjiArai 0:5b88d5760320 102 ctx->state[1] = 0xEFCDAB89;
kenjiArai 0:5b88d5760320 103 ctx->state[2] = 0x98BADCFE;
kenjiArai 0:5b88d5760320 104 ctx->state[3] = 0x10325476;
kenjiArai 0:5b88d5760320 105
kenjiArai 0:5b88d5760320 106 return( 0 );
kenjiArai 0:5b88d5760320 107 }
kenjiArai 0:5b88d5760320 108
kenjiArai 0:5b88d5760320 109 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
kenjiArai 0:5b88d5760320 110 void mbedtls_md5_starts( mbedtls_md5_context *ctx )
kenjiArai 0:5b88d5760320 111 {
kenjiArai 0:5b88d5760320 112 mbedtls_md5_starts_ret( ctx );
kenjiArai 0:5b88d5760320 113 }
kenjiArai 0:5b88d5760320 114 #endif
kenjiArai 0:5b88d5760320 115
kenjiArai 0:5b88d5760320 116 #if !defined(MBEDTLS_MD5_PROCESS_ALT)
kenjiArai 0:5b88d5760320 117 int mbedtls_internal_md5_process( mbedtls_md5_context *ctx,
kenjiArai 0:5b88d5760320 118 const unsigned char data[64] )
kenjiArai 0:5b88d5760320 119 {
kenjiArai 0:5b88d5760320 120 uint32_t X[16], A, B, C, D;
kenjiArai 0:5b88d5760320 121
kenjiArai 0:5b88d5760320 122 GET_UINT32_LE( X[ 0], data, 0 );
kenjiArai 0:5b88d5760320 123 GET_UINT32_LE( X[ 1], data, 4 );
kenjiArai 0:5b88d5760320 124 GET_UINT32_LE( X[ 2], data, 8 );
kenjiArai 0:5b88d5760320 125 GET_UINT32_LE( X[ 3], data, 12 );
kenjiArai 0:5b88d5760320 126 GET_UINT32_LE( X[ 4], data, 16 );
kenjiArai 0:5b88d5760320 127 GET_UINT32_LE( X[ 5], data, 20 );
kenjiArai 0:5b88d5760320 128 GET_UINT32_LE( X[ 6], data, 24 );
kenjiArai 0:5b88d5760320 129 GET_UINT32_LE( X[ 7], data, 28 );
kenjiArai 0:5b88d5760320 130 GET_UINT32_LE( X[ 8], data, 32 );
kenjiArai 0:5b88d5760320 131 GET_UINT32_LE( X[ 9], data, 36 );
kenjiArai 0:5b88d5760320 132 GET_UINT32_LE( X[10], data, 40 );
kenjiArai 0:5b88d5760320 133 GET_UINT32_LE( X[11], data, 44 );
kenjiArai 0:5b88d5760320 134 GET_UINT32_LE( X[12], data, 48 );
kenjiArai 0:5b88d5760320 135 GET_UINT32_LE( X[13], data, 52 );
kenjiArai 0:5b88d5760320 136 GET_UINT32_LE( X[14], data, 56 );
kenjiArai 0:5b88d5760320 137 GET_UINT32_LE( X[15], data, 60 );
kenjiArai 0:5b88d5760320 138
kenjiArai 0:5b88d5760320 139 #define S(x,n) \
kenjiArai 0:5b88d5760320 140 ( ( (x) << (n) ) | ( ( (x) & 0xFFFFFFFF) >> ( 32 - (n) ) ) )
kenjiArai 0:5b88d5760320 141
kenjiArai 0:5b88d5760320 142 #define P(a,b,c,d,k,s,t) \
kenjiArai 0:5b88d5760320 143 do \
kenjiArai 0:5b88d5760320 144 { \
kenjiArai 0:5b88d5760320 145 (a) += F((b),(c),(d)) + X[(k)] + (t); \
kenjiArai 0:5b88d5760320 146 (a) = S((a),(s)) + (b); \
kenjiArai 0:5b88d5760320 147 } while( 0 )
kenjiArai 0:5b88d5760320 148
kenjiArai 0:5b88d5760320 149 A = ctx->state[0];
kenjiArai 0:5b88d5760320 150 B = ctx->state[1];
kenjiArai 0:5b88d5760320 151 C = ctx->state[2];
kenjiArai 0:5b88d5760320 152 D = ctx->state[3];
kenjiArai 0:5b88d5760320 153
kenjiArai 0:5b88d5760320 154 #define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
kenjiArai 0:5b88d5760320 155
kenjiArai 0:5b88d5760320 156 P( A, B, C, D, 0, 7, 0xD76AA478 );
kenjiArai 0:5b88d5760320 157 P( D, A, B, C, 1, 12, 0xE8C7B756 );
kenjiArai 0:5b88d5760320 158 P( C, D, A, B, 2, 17, 0x242070DB );
kenjiArai 0:5b88d5760320 159 P( B, C, D, A, 3, 22, 0xC1BDCEEE );
kenjiArai 0:5b88d5760320 160 P( A, B, C, D, 4, 7, 0xF57C0FAF );
kenjiArai 0:5b88d5760320 161 P( D, A, B, C, 5, 12, 0x4787C62A );
kenjiArai 0:5b88d5760320 162 P( C, D, A, B, 6, 17, 0xA8304613 );
kenjiArai 0:5b88d5760320 163 P( B, C, D, A, 7, 22, 0xFD469501 );
kenjiArai 0:5b88d5760320 164 P( A, B, C, D, 8, 7, 0x698098D8 );
kenjiArai 0:5b88d5760320 165 P( D, A, B, C, 9, 12, 0x8B44F7AF );
kenjiArai 0:5b88d5760320 166 P( C, D, A, B, 10, 17, 0xFFFF5BB1 );
kenjiArai 0:5b88d5760320 167 P( B, C, D, A, 11, 22, 0x895CD7BE );
kenjiArai 0:5b88d5760320 168 P( A, B, C, D, 12, 7, 0x6B901122 );
kenjiArai 0:5b88d5760320 169 P( D, A, B, C, 13, 12, 0xFD987193 );
kenjiArai 0:5b88d5760320 170 P( C, D, A, B, 14, 17, 0xA679438E );
kenjiArai 0:5b88d5760320 171 P( B, C, D, A, 15, 22, 0x49B40821 );
kenjiArai 0:5b88d5760320 172
kenjiArai 0:5b88d5760320 173 #undef F
kenjiArai 0:5b88d5760320 174
kenjiArai 0:5b88d5760320 175 #define F(x,y,z) ((y) ^ ((z) & ((x) ^ (y))))
kenjiArai 0:5b88d5760320 176
kenjiArai 0:5b88d5760320 177 P( A, B, C, D, 1, 5, 0xF61E2562 );
kenjiArai 0:5b88d5760320 178 P( D, A, B, C, 6, 9, 0xC040B340 );
kenjiArai 0:5b88d5760320 179 P( C, D, A, B, 11, 14, 0x265E5A51 );
kenjiArai 0:5b88d5760320 180 P( B, C, D, A, 0, 20, 0xE9B6C7AA );
kenjiArai 0:5b88d5760320 181 P( A, B, C, D, 5, 5, 0xD62F105D );
kenjiArai 0:5b88d5760320 182 P( D, A, B, C, 10, 9, 0x02441453 );
kenjiArai 0:5b88d5760320 183 P( C, D, A, B, 15, 14, 0xD8A1E681 );
kenjiArai 0:5b88d5760320 184 P( B, C, D, A, 4, 20, 0xE7D3FBC8 );
kenjiArai 0:5b88d5760320 185 P( A, B, C, D, 9, 5, 0x21E1CDE6 );
kenjiArai 0:5b88d5760320 186 P( D, A, B, C, 14, 9, 0xC33707D6 );
kenjiArai 0:5b88d5760320 187 P( C, D, A, B, 3, 14, 0xF4D50D87 );
kenjiArai 0:5b88d5760320 188 P( B, C, D, A, 8, 20, 0x455A14ED );
kenjiArai 0:5b88d5760320 189 P( A, B, C, D, 13, 5, 0xA9E3E905 );
kenjiArai 0:5b88d5760320 190 P( D, A, B, C, 2, 9, 0xFCEFA3F8 );
kenjiArai 0:5b88d5760320 191 P( C, D, A, B, 7, 14, 0x676F02D9 );
kenjiArai 0:5b88d5760320 192 P( B, C, D, A, 12, 20, 0x8D2A4C8A );
kenjiArai 0:5b88d5760320 193
kenjiArai 0:5b88d5760320 194 #undef F
kenjiArai 0:5b88d5760320 195
kenjiArai 0:5b88d5760320 196 #define F(x,y,z) ((x) ^ (y) ^ (z))
kenjiArai 0:5b88d5760320 197
kenjiArai 0:5b88d5760320 198 P( A, B, C, D, 5, 4, 0xFFFA3942 );
kenjiArai 0:5b88d5760320 199 P( D, A, B, C, 8, 11, 0x8771F681 );
kenjiArai 0:5b88d5760320 200 P( C, D, A, B, 11, 16, 0x6D9D6122 );
kenjiArai 0:5b88d5760320 201 P( B, C, D, A, 14, 23, 0xFDE5380C );
kenjiArai 0:5b88d5760320 202 P( A, B, C, D, 1, 4, 0xA4BEEA44 );
kenjiArai 0:5b88d5760320 203 P( D, A, B, C, 4, 11, 0x4BDECFA9 );
kenjiArai 0:5b88d5760320 204 P( C, D, A, B, 7, 16, 0xF6BB4B60 );
kenjiArai 0:5b88d5760320 205 P( B, C, D, A, 10, 23, 0xBEBFBC70 );
kenjiArai 0:5b88d5760320 206 P( A, B, C, D, 13, 4, 0x289B7EC6 );
kenjiArai 0:5b88d5760320 207 P( D, A, B, C, 0, 11, 0xEAA127FA );
kenjiArai 0:5b88d5760320 208 P( C, D, A, B, 3, 16, 0xD4EF3085 );
kenjiArai 0:5b88d5760320 209 P( B, C, D, A, 6, 23, 0x04881D05 );
kenjiArai 0:5b88d5760320 210 P( A, B, C, D, 9, 4, 0xD9D4D039 );
kenjiArai 0:5b88d5760320 211 P( D, A, B, C, 12, 11, 0xE6DB99E5 );
kenjiArai 0:5b88d5760320 212 P( C, D, A, B, 15, 16, 0x1FA27CF8 );
kenjiArai 0:5b88d5760320 213 P( B, C, D, A, 2, 23, 0xC4AC5665 );
kenjiArai 0:5b88d5760320 214
kenjiArai 0:5b88d5760320 215 #undef F
kenjiArai 0:5b88d5760320 216
kenjiArai 0:5b88d5760320 217 #define F(x,y,z) ((y) ^ ((x) | ~(z)))
kenjiArai 0:5b88d5760320 218
kenjiArai 0:5b88d5760320 219 P( A, B, C, D, 0, 6, 0xF4292244 );
kenjiArai 0:5b88d5760320 220 P( D, A, B, C, 7, 10, 0x432AFF97 );
kenjiArai 0:5b88d5760320 221 P( C, D, A, B, 14, 15, 0xAB9423A7 );
kenjiArai 0:5b88d5760320 222 P( B, C, D, A, 5, 21, 0xFC93A039 );
kenjiArai 0:5b88d5760320 223 P( A, B, C, D, 12, 6, 0x655B59C3 );
kenjiArai 0:5b88d5760320 224 P( D, A, B, C, 3, 10, 0x8F0CCC92 );
kenjiArai 0:5b88d5760320 225 P( C, D, A, B, 10, 15, 0xFFEFF47D );
kenjiArai 0:5b88d5760320 226 P( B, C, D, A, 1, 21, 0x85845DD1 );
kenjiArai 0:5b88d5760320 227 P( A, B, C, D, 8, 6, 0x6FA87E4F );
kenjiArai 0:5b88d5760320 228 P( D, A, B, C, 15, 10, 0xFE2CE6E0 );
kenjiArai 0:5b88d5760320 229 P( C, D, A, B, 6, 15, 0xA3014314 );
kenjiArai 0:5b88d5760320 230 P( B, C, D, A, 13, 21, 0x4E0811A1 );
kenjiArai 0:5b88d5760320 231 P( A, B, C, D, 4, 6, 0xF7537E82 );
kenjiArai 0:5b88d5760320 232 P( D, A, B, C, 11, 10, 0xBD3AF235 );
kenjiArai 0:5b88d5760320 233 P( C, D, A, B, 2, 15, 0x2AD7D2BB );
kenjiArai 0:5b88d5760320 234 P( B, C, D, A, 9, 21, 0xEB86D391 );
kenjiArai 0:5b88d5760320 235
kenjiArai 0:5b88d5760320 236 #undef F
kenjiArai 0:5b88d5760320 237
kenjiArai 0:5b88d5760320 238 ctx->state[0] += A;
kenjiArai 0:5b88d5760320 239 ctx->state[1] += B;
kenjiArai 0:5b88d5760320 240 ctx->state[2] += C;
kenjiArai 0:5b88d5760320 241 ctx->state[3] += D;
kenjiArai 0:5b88d5760320 242
kenjiArai 0:5b88d5760320 243 return( 0 );
kenjiArai 0:5b88d5760320 244 }
kenjiArai 0:5b88d5760320 245
kenjiArai 0:5b88d5760320 246 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
kenjiArai 0:5b88d5760320 247 void mbedtls_md5_process( mbedtls_md5_context *ctx,
kenjiArai 0:5b88d5760320 248 const unsigned char data[64] )
kenjiArai 0:5b88d5760320 249 {
kenjiArai 0:5b88d5760320 250 mbedtls_internal_md5_process( ctx, data );
kenjiArai 0:5b88d5760320 251 }
kenjiArai 0:5b88d5760320 252 #endif
kenjiArai 0:5b88d5760320 253 #endif /* !MBEDTLS_MD5_PROCESS_ALT */
kenjiArai 0:5b88d5760320 254
kenjiArai 0:5b88d5760320 255 /*
kenjiArai 0:5b88d5760320 256 * MD5 process buffer
kenjiArai 0:5b88d5760320 257 */
kenjiArai 0:5b88d5760320 258 int mbedtls_md5_update_ret( mbedtls_md5_context *ctx,
kenjiArai 0:5b88d5760320 259 const unsigned char *input,
kenjiArai 0:5b88d5760320 260 size_t ilen )
kenjiArai 0:5b88d5760320 261 {
kenjiArai 0:5b88d5760320 262 int ret;
kenjiArai 0:5b88d5760320 263 size_t fill;
kenjiArai 0:5b88d5760320 264 uint32_t left;
kenjiArai 0:5b88d5760320 265
kenjiArai 0:5b88d5760320 266 if( ilen == 0 )
kenjiArai 0:5b88d5760320 267 return( 0 );
kenjiArai 0:5b88d5760320 268
kenjiArai 0:5b88d5760320 269 left = ctx->total[0] & 0x3F;
kenjiArai 0:5b88d5760320 270 fill = 64 - left;
kenjiArai 0:5b88d5760320 271
kenjiArai 0:5b88d5760320 272 ctx->total[0] += (uint32_t) ilen;
kenjiArai 0:5b88d5760320 273 ctx->total[0] &= 0xFFFFFFFF;
kenjiArai 0:5b88d5760320 274
kenjiArai 0:5b88d5760320 275 if( ctx->total[0] < (uint32_t) ilen )
kenjiArai 0:5b88d5760320 276 ctx->total[1]++;
kenjiArai 0:5b88d5760320 277
kenjiArai 0:5b88d5760320 278 if( left && ilen >= fill )
kenjiArai 0:5b88d5760320 279 {
kenjiArai 0:5b88d5760320 280 memcpy( (void *) (ctx->buffer + left), input, fill );
kenjiArai 0:5b88d5760320 281 if( ( ret = mbedtls_internal_md5_process( ctx, ctx->buffer ) ) != 0 )
kenjiArai 0:5b88d5760320 282 return( ret );
kenjiArai 0:5b88d5760320 283
kenjiArai 0:5b88d5760320 284 input += fill;
kenjiArai 0:5b88d5760320 285 ilen -= fill;
kenjiArai 0:5b88d5760320 286 left = 0;
kenjiArai 0:5b88d5760320 287 }
kenjiArai 0:5b88d5760320 288
kenjiArai 0:5b88d5760320 289 while( ilen >= 64 )
kenjiArai 0:5b88d5760320 290 {
kenjiArai 0:5b88d5760320 291 if( ( ret = mbedtls_internal_md5_process( ctx, input ) ) != 0 )
kenjiArai 0:5b88d5760320 292 return( ret );
kenjiArai 0:5b88d5760320 293
kenjiArai 0:5b88d5760320 294 input += 64;
kenjiArai 0:5b88d5760320 295 ilen -= 64;
kenjiArai 0:5b88d5760320 296 }
kenjiArai 0:5b88d5760320 297
kenjiArai 0:5b88d5760320 298 if( ilen > 0 )
kenjiArai 0:5b88d5760320 299 {
kenjiArai 0:5b88d5760320 300 memcpy( (void *) (ctx->buffer + left), input, ilen );
kenjiArai 0:5b88d5760320 301 }
kenjiArai 0:5b88d5760320 302
kenjiArai 0:5b88d5760320 303 return( 0 );
kenjiArai 0:5b88d5760320 304 }
kenjiArai 0:5b88d5760320 305
kenjiArai 0:5b88d5760320 306 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
kenjiArai 0:5b88d5760320 307 void mbedtls_md5_update( mbedtls_md5_context *ctx,
kenjiArai 0:5b88d5760320 308 const unsigned char *input,
kenjiArai 0:5b88d5760320 309 size_t ilen )
kenjiArai 0:5b88d5760320 310 {
kenjiArai 0:5b88d5760320 311 mbedtls_md5_update_ret( ctx, input, ilen );
kenjiArai 0:5b88d5760320 312 }
kenjiArai 0:5b88d5760320 313 #endif
kenjiArai 0:5b88d5760320 314
kenjiArai 0:5b88d5760320 315 /*
kenjiArai 0:5b88d5760320 316 * MD5 final digest
kenjiArai 0:5b88d5760320 317 */
kenjiArai 0:5b88d5760320 318 int mbedtls_md5_finish_ret( mbedtls_md5_context *ctx,
kenjiArai 0:5b88d5760320 319 unsigned char output[16] )
kenjiArai 0:5b88d5760320 320 {
kenjiArai 0:5b88d5760320 321 int ret;
kenjiArai 0:5b88d5760320 322 uint32_t used;
kenjiArai 0:5b88d5760320 323 uint32_t high, low;
kenjiArai 0:5b88d5760320 324
kenjiArai 0:5b88d5760320 325 /*
kenjiArai 0:5b88d5760320 326 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
kenjiArai 0:5b88d5760320 327 */
kenjiArai 0:5b88d5760320 328 used = ctx->total[0] & 0x3F;
kenjiArai 0:5b88d5760320 329
kenjiArai 0:5b88d5760320 330 ctx->buffer[used++] = 0x80;
kenjiArai 0:5b88d5760320 331
kenjiArai 0:5b88d5760320 332 if( used <= 56 )
kenjiArai 0:5b88d5760320 333 {
kenjiArai 0:5b88d5760320 334 /* Enough room for padding + length in current block */
kenjiArai 0:5b88d5760320 335 memset( ctx->buffer + used, 0, 56 - used );
kenjiArai 0:5b88d5760320 336 }
kenjiArai 0:5b88d5760320 337 else
kenjiArai 0:5b88d5760320 338 {
kenjiArai 0:5b88d5760320 339 /* We'll need an extra block */
kenjiArai 0:5b88d5760320 340 memset( ctx->buffer + used, 0, 64 - used );
kenjiArai 0:5b88d5760320 341
kenjiArai 0:5b88d5760320 342 if( ( ret = mbedtls_internal_md5_process( ctx, ctx->buffer ) ) != 0 )
kenjiArai 0:5b88d5760320 343 return( ret );
kenjiArai 0:5b88d5760320 344
kenjiArai 0:5b88d5760320 345 memset( ctx->buffer, 0, 56 );
kenjiArai 0:5b88d5760320 346 }
kenjiArai 0:5b88d5760320 347
kenjiArai 0:5b88d5760320 348 /*
kenjiArai 0:5b88d5760320 349 * Add message length
kenjiArai 0:5b88d5760320 350 */
kenjiArai 0:5b88d5760320 351 high = ( ctx->total[0] >> 29 )
kenjiArai 0:5b88d5760320 352 | ( ctx->total[1] << 3 );
kenjiArai 0:5b88d5760320 353 low = ( ctx->total[0] << 3 );
kenjiArai 0:5b88d5760320 354
kenjiArai 0:5b88d5760320 355 PUT_UINT32_LE( low, ctx->buffer, 56 );
kenjiArai 0:5b88d5760320 356 PUT_UINT32_LE( high, ctx->buffer, 60 );
kenjiArai 0:5b88d5760320 357
kenjiArai 0:5b88d5760320 358 if( ( ret = mbedtls_internal_md5_process( ctx, ctx->buffer ) ) != 0 )
kenjiArai 0:5b88d5760320 359 return( ret );
kenjiArai 0:5b88d5760320 360
kenjiArai 0:5b88d5760320 361 /*
kenjiArai 0:5b88d5760320 362 * Output final state
kenjiArai 0:5b88d5760320 363 */
kenjiArai 0:5b88d5760320 364 PUT_UINT32_LE( ctx->state[0], output, 0 );
kenjiArai 0:5b88d5760320 365 PUT_UINT32_LE( ctx->state[1], output, 4 );
kenjiArai 0:5b88d5760320 366 PUT_UINT32_LE( ctx->state[2], output, 8 );
kenjiArai 0:5b88d5760320 367 PUT_UINT32_LE( ctx->state[3], output, 12 );
kenjiArai 0:5b88d5760320 368
kenjiArai 0:5b88d5760320 369 return( 0 );
kenjiArai 0:5b88d5760320 370 }
kenjiArai 0:5b88d5760320 371
kenjiArai 0:5b88d5760320 372 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
kenjiArai 0:5b88d5760320 373 void mbedtls_md5_finish( mbedtls_md5_context *ctx,
kenjiArai 0:5b88d5760320 374 unsigned char output[16] )
kenjiArai 0:5b88d5760320 375 {
kenjiArai 0:5b88d5760320 376 mbedtls_md5_finish_ret( ctx, output );
kenjiArai 0:5b88d5760320 377 }
kenjiArai 0:5b88d5760320 378 #endif
kenjiArai 0:5b88d5760320 379
kenjiArai 0:5b88d5760320 380 #endif /* !MBEDTLS_MD5_ALT */
kenjiArai 0:5b88d5760320 381
kenjiArai 0:5b88d5760320 382 /*
kenjiArai 0:5b88d5760320 383 * output = MD5( input buffer )
kenjiArai 0:5b88d5760320 384 */
kenjiArai 0:5b88d5760320 385 int mbedtls_md5_ret( const unsigned char *input,
kenjiArai 0:5b88d5760320 386 size_t ilen,
kenjiArai 0:5b88d5760320 387 unsigned char output[16] )
kenjiArai 0:5b88d5760320 388 {
kenjiArai 0:5b88d5760320 389 int ret;
kenjiArai 0:5b88d5760320 390 mbedtls_md5_context ctx;
kenjiArai 0:5b88d5760320 391
kenjiArai 0:5b88d5760320 392 mbedtls_md5_init( &ctx );
kenjiArai 0:5b88d5760320 393
kenjiArai 0:5b88d5760320 394 if( ( ret = mbedtls_md5_starts_ret( &ctx ) ) != 0 )
kenjiArai 0:5b88d5760320 395 goto exit;
kenjiArai 0:5b88d5760320 396
kenjiArai 0:5b88d5760320 397 if( ( ret = mbedtls_md5_update_ret( &ctx, input, ilen ) ) != 0 )
kenjiArai 0:5b88d5760320 398 goto exit;
kenjiArai 0:5b88d5760320 399
kenjiArai 0:5b88d5760320 400 if( ( ret = mbedtls_md5_finish_ret( &ctx, output ) ) != 0 )
kenjiArai 0:5b88d5760320 401 goto exit;
kenjiArai 0:5b88d5760320 402
kenjiArai 0:5b88d5760320 403 exit:
kenjiArai 0:5b88d5760320 404 mbedtls_md5_free( &ctx );
kenjiArai 0:5b88d5760320 405
kenjiArai 0:5b88d5760320 406 return( ret );
kenjiArai 0:5b88d5760320 407 }
kenjiArai 0:5b88d5760320 408
kenjiArai 0:5b88d5760320 409 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
kenjiArai 0:5b88d5760320 410 void mbedtls_md5( const unsigned char *input,
kenjiArai 0:5b88d5760320 411 size_t ilen,
kenjiArai 0:5b88d5760320 412 unsigned char output[16] )
kenjiArai 0:5b88d5760320 413 {
kenjiArai 0:5b88d5760320 414 mbedtls_md5_ret( input, ilen, output );
kenjiArai 0:5b88d5760320 415 }
kenjiArai 0:5b88d5760320 416 #endif
kenjiArai 0:5b88d5760320 417
kenjiArai 0:5b88d5760320 418 #if defined(MBEDTLS_SELF_TEST)
kenjiArai 0:5b88d5760320 419 /*
kenjiArai 0:5b88d5760320 420 * RFC 1321 test vectors
kenjiArai 0:5b88d5760320 421 */
kenjiArai 0:5b88d5760320 422 static const unsigned char md5_test_buf[7][81] =
kenjiArai 0:5b88d5760320 423 {
kenjiArai 0:5b88d5760320 424 { "" },
kenjiArai 0:5b88d5760320 425 { "a" },
kenjiArai 0:5b88d5760320 426 { "abc" },
kenjiArai 0:5b88d5760320 427 { "message digest" },
kenjiArai 0:5b88d5760320 428 { "abcdefghijklmnopqrstuvwxyz" },
kenjiArai 0:5b88d5760320 429 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
kenjiArai 0:5b88d5760320 430 { "12345678901234567890123456789012345678901234567890123456789012"
kenjiArai 0:5b88d5760320 431 "345678901234567890" }
kenjiArai 0:5b88d5760320 432 };
kenjiArai 0:5b88d5760320 433
kenjiArai 0:5b88d5760320 434 static const size_t md5_test_buflen[7] =
kenjiArai 0:5b88d5760320 435 {
kenjiArai 0:5b88d5760320 436 0, 1, 3, 14, 26, 62, 80
kenjiArai 0:5b88d5760320 437 };
kenjiArai 0:5b88d5760320 438
kenjiArai 0:5b88d5760320 439 static const unsigned char md5_test_sum[7][16] =
kenjiArai 0:5b88d5760320 440 {
kenjiArai 0:5b88d5760320 441 { 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04,
kenjiArai 0:5b88d5760320 442 0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E },
kenjiArai 0:5b88d5760320 443 { 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8,
kenjiArai 0:5b88d5760320 444 0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 },
kenjiArai 0:5b88d5760320 445 { 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0,
kenjiArai 0:5b88d5760320 446 0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 },
kenjiArai 0:5b88d5760320 447 { 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D,
kenjiArai 0:5b88d5760320 448 0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 },
kenjiArai 0:5b88d5760320 449 { 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00,
kenjiArai 0:5b88d5760320 450 0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B },
kenjiArai 0:5b88d5760320 451 { 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5,
kenjiArai 0:5b88d5760320 452 0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F },
kenjiArai 0:5b88d5760320 453 { 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55,
kenjiArai 0:5b88d5760320 454 0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A }
kenjiArai 0:5b88d5760320 455 };
kenjiArai 0:5b88d5760320 456
kenjiArai 0:5b88d5760320 457 /*
kenjiArai 0:5b88d5760320 458 * Checkup routine
kenjiArai 0:5b88d5760320 459 */
kenjiArai 0:5b88d5760320 460 int mbedtls_md5_self_test( int verbose )
kenjiArai 0:5b88d5760320 461 {
kenjiArai 0:5b88d5760320 462 int i, ret = 0;
kenjiArai 0:5b88d5760320 463 unsigned char md5sum[16];
kenjiArai 0:5b88d5760320 464
kenjiArai 0:5b88d5760320 465 for( i = 0; i < 7; i++ )
kenjiArai 0:5b88d5760320 466 {
kenjiArai 0:5b88d5760320 467 if( verbose != 0 )
kenjiArai 0:5b88d5760320 468 mbedtls_printf( " MD5 test #%d: ", i + 1 );
kenjiArai 0:5b88d5760320 469
kenjiArai 0:5b88d5760320 470 ret = mbedtls_md5_ret( md5_test_buf[i], md5_test_buflen[i], md5sum );
kenjiArai 0:5b88d5760320 471 if( ret != 0 )
kenjiArai 0:5b88d5760320 472 goto fail;
kenjiArai 0:5b88d5760320 473
kenjiArai 0:5b88d5760320 474 if( memcmp( md5sum, md5_test_sum[i], 16 ) != 0 )
kenjiArai 0:5b88d5760320 475 {
kenjiArai 0:5b88d5760320 476 ret = 1;
kenjiArai 0:5b88d5760320 477 goto fail;
kenjiArai 0:5b88d5760320 478 }
kenjiArai 0:5b88d5760320 479
kenjiArai 0:5b88d5760320 480 if( verbose != 0 )
kenjiArai 0:5b88d5760320 481 mbedtls_printf( "passed\n" );
kenjiArai 0:5b88d5760320 482 }
kenjiArai 0:5b88d5760320 483
kenjiArai 0:5b88d5760320 484 if( verbose != 0 )
kenjiArai 0:5b88d5760320 485 mbedtls_printf( "\n" );
kenjiArai 0:5b88d5760320 486
kenjiArai 0:5b88d5760320 487 return( 0 );
kenjiArai 0:5b88d5760320 488
kenjiArai 0:5b88d5760320 489 fail:
kenjiArai 0:5b88d5760320 490 if( verbose != 0 )
kenjiArai 0:5b88d5760320 491 mbedtls_printf( "failed\n" );
kenjiArai 0:5b88d5760320 492
kenjiArai 0:5b88d5760320 493 return( ret );
kenjiArai 0:5b88d5760320 494 }
kenjiArai 0:5b88d5760320 495
kenjiArai 0:5b88d5760320 496 #endif /* MBEDTLS_SELF_TEST */
kenjiArai 0:5b88d5760320 497
kenjiArai 0:5b88d5760320 498 #endif /* MBEDTLS_MD5_C */