This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
maclobdell
Date:
Wed May 18 19:06:32 2016 +0000
Revision:
0:f7c60d3e7b8a
clean version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maclobdell 0:f7c60d3e7b8a 1 /*
maclobdell 0:f7c60d3e7b8a 2 * RFC 1321 compliant MD5 implementation
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 * The MD5 algorithm was designed by Ron Rivest in 1991.
maclobdell 0:f7c60d3e7b8a 23 *
maclobdell 0:f7c60d3e7b8a 24 * http://www.ietf.org/rfc/rfc1321.txt
maclobdell 0:f7c60d3e7b8a 25 */
maclobdell 0:f7c60d3e7b8a 26
maclobdell 0:f7c60d3e7b8a 27 #if !defined(MBEDTLS_CONFIG_FILE)
maclobdell 0:f7c60d3e7b8a 28 #include "mbedtls/config.h"
maclobdell 0:f7c60d3e7b8a 29 #else
maclobdell 0:f7c60d3e7b8a 30 #include MBEDTLS_CONFIG_FILE
maclobdell 0:f7c60d3e7b8a 31 #endif
maclobdell 0:f7c60d3e7b8a 32
maclobdell 0:f7c60d3e7b8a 33 #if defined(MBEDTLS_MD5_C)
maclobdell 0:f7c60d3e7b8a 34
maclobdell 0:f7c60d3e7b8a 35 #include "mbedtls/md5.h"
maclobdell 0:f7c60d3e7b8a 36
maclobdell 0:f7c60d3e7b8a 37 #include <string.h>
maclobdell 0:f7c60d3e7b8a 38
maclobdell 0:f7c60d3e7b8a 39 #if defined(MBEDTLS_SELF_TEST)
maclobdell 0:f7c60d3e7b8a 40 #if defined(MBEDTLS_PLATFORM_C)
maclobdell 0:f7c60d3e7b8a 41 #include "mbedtls/platform.h"
maclobdell 0:f7c60d3e7b8a 42 #else
maclobdell 0:f7c60d3e7b8a 43 #include <stdio.h>
maclobdell 0:f7c60d3e7b8a 44 #define mbedtls_printf printf
maclobdell 0:f7c60d3e7b8a 45 #endif /* MBEDTLS_PLATFORM_C */
maclobdell 0:f7c60d3e7b8a 46 #endif /* MBEDTLS_SELF_TEST */
maclobdell 0:f7c60d3e7b8a 47
maclobdell 0:f7c60d3e7b8a 48 #if !defined(MBEDTLS_MD5_ALT)
maclobdell 0:f7c60d3e7b8a 49
maclobdell 0:f7c60d3e7b8a 50 /* Implementation that should never be optimized out by the compiler */
maclobdell 0:f7c60d3e7b8a 51 static void mbedtls_zeroize( void *v, size_t n ) {
maclobdell 0:f7c60d3e7b8a 52 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
maclobdell 0:f7c60d3e7b8a 53 }
maclobdell 0:f7c60d3e7b8a 54
maclobdell 0:f7c60d3e7b8a 55 /*
maclobdell 0:f7c60d3e7b8a 56 * 32-bit integer manipulation macros (little endian)
maclobdell 0:f7c60d3e7b8a 57 */
maclobdell 0:f7c60d3e7b8a 58 #ifndef GET_UINT32_LE
maclobdell 0:f7c60d3e7b8a 59 #define GET_UINT32_LE(n,b,i) \
maclobdell 0:f7c60d3e7b8a 60 { \
maclobdell 0:f7c60d3e7b8a 61 (n) = ( (uint32_t) (b)[(i) ] ) \
maclobdell 0:f7c60d3e7b8a 62 | ( (uint32_t) (b)[(i) + 1] << 8 ) \
maclobdell 0:f7c60d3e7b8a 63 | ( (uint32_t) (b)[(i) + 2] << 16 ) \
maclobdell 0:f7c60d3e7b8a 64 | ( (uint32_t) (b)[(i) + 3] << 24 ); \
maclobdell 0:f7c60d3e7b8a 65 }
maclobdell 0:f7c60d3e7b8a 66 #endif
maclobdell 0:f7c60d3e7b8a 67
maclobdell 0:f7c60d3e7b8a 68 #ifndef PUT_UINT32_LE
maclobdell 0:f7c60d3e7b8a 69 #define PUT_UINT32_LE(n,b,i) \
maclobdell 0:f7c60d3e7b8a 70 { \
maclobdell 0:f7c60d3e7b8a 71 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
maclobdell 0:f7c60d3e7b8a 72 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
maclobdell 0:f7c60d3e7b8a 73 (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
maclobdell 0:f7c60d3e7b8a 74 (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
maclobdell 0:f7c60d3e7b8a 75 }
maclobdell 0:f7c60d3e7b8a 76 #endif
maclobdell 0:f7c60d3e7b8a 77
maclobdell 0:f7c60d3e7b8a 78 void mbedtls_md5_init( mbedtls_md5_context *ctx )
maclobdell 0:f7c60d3e7b8a 79 {
maclobdell 0:f7c60d3e7b8a 80 memset( ctx, 0, sizeof( mbedtls_md5_context ) );
maclobdell 0:f7c60d3e7b8a 81 }
maclobdell 0:f7c60d3e7b8a 82
maclobdell 0:f7c60d3e7b8a 83 void mbedtls_md5_free( mbedtls_md5_context *ctx )
maclobdell 0:f7c60d3e7b8a 84 {
maclobdell 0:f7c60d3e7b8a 85 if( ctx == NULL )
maclobdell 0:f7c60d3e7b8a 86 return;
maclobdell 0:f7c60d3e7b8a 87
maclobdell 0:f7c60d3e7b8a 88 mbedtls_zeroize( ctx, sizeof( mbedtls_md5_context ) );
maclobdell 0:f7c60d3e7b8a 89 }
maclobdell 0:f7c60d3e7b8a 90
maclobdell 0:f7c60d3e7b8a 91 void mbedtls_md5_clone( mbedtls_md5_context *dst,
maclobdell 0:f7c60d3e7b8a 92 const mbedtls_md5_context *src )
maclobdell 0:f7c60d3e7b8a 93 {
maclobdell 0:f7c60d3e7b8a 94 *dst = *src;
maclobdell 0:f7c60d3e7b8a 95 }
maclobdell 0:f7c60d3e7b8a 96
maclobdell 0:f7c60d3e7b8a 97 /*
maclobdell 0:f7c60d3e7b8a 98 * MD5 context setup
maclobdell 0:f7c60d3e7b8a 99 */
maclobdell 0:f7c60d3e7b8a 100 void mbedtls_md5_starts( mbedtls_md5_context *ctx )
maclobdell 0:f7c60d3e7b8a 101 {
maclobdell 0:f7c60d3e7b8a 102 ctx->total[0] = 0;
maclobdell 0:f7c60d3e7b8a 103 ctx->total[1] = 0;
maclobdell 0:f7c60d3e7b8a 104
maclobdell 0:f7c60d3e7b8a 105 ctx->state[0] = 0x67452301;
maclobdell 0:f7c60d3e7b8a 106 ctx->state[1] = 0xEFCDAB89;
maclobdell 0:f7c60d3e7b8a 107 ctx->state[2] = 0x98BADCFE;
maclobdell 0:f7c60d3e7b8a 108 ctx->state[3] = 0x10325476;
maclobdell 0:f7c60d3e7b8a 109 }
maclobdell 0:f7c60d3e7b8a 110
maclobdell 0:f7c60d3e7b8a 111 #if !defined(MBEDTLS_MD5_PROCESS_ALT)
maclobdell 0:f7c60d3e7b8a 112 void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[64] )
maclobdell 0:f7c60d3e7b8a 113 {
maclobdell 0:f7c60d3e7b8a 114 uint32_t X[16], A, B, C, D;
maclobdell 0:f7c60d3e7b8a 115
maclobdell 0:f7c60d3e7b8a 116 GET_UINT32_LE( X[ 0], data, 0 );
maclobdell 0:f7c60d3e7b8a 117 GET_UINT32_LE( X[ 1], data, 4 );
maclobdell 0:f7c60d3e7b8a 118 GET_UINT32_LE( X[ 2], data, 8 );
maclobdell 0:f7c60d3e7b8a 119 GET_UINT32_LE( X[ 3], data, 12 );
maclobdell 0:f7c60d3e7b8a 120 GET_UINT32_LE( X[ 4], data, 16 );
maclobdell 0:f7c60d3e7b8a 121 GET_UINT32_LE( X[ 5], data, 20 );
maclobdell 0:f7c60d3e7b8a 122 GET_UINT32_LE( X[ 6], data, 24 );
maclobdell 0:f7c60d3e7b8a 123 GET_UINT32_LE( X[ 7], data, 28 );
maclobdell 0:f7c60d3e7b8a 124 GET_UINT32_LE( X[ 8], data, 32 );
maclobdell 0:f7c60d3e7b8a 125 GET_UINT32_LE( X[ 9], data, 36 );
maclobdell 0:f7c60d3e7b8a 126 GET_UINT32_LE( X[10], data, 40 );
maclobdell 0:f7c60d3e7b8a 127 GET_UINT32_LE( X[11], data, 44 );
maclobdell 0:f7c60d3e7b8a 128 GET_UINT32_LE( X[12], data, 48 );
maclobdell 0:f7c60d3e7b8a 129 GET_UINT32_LE( X[13], data, 52 );
maclobdell 0:f7c60d3e7b8a 130 GET_UINT32_LE( X[14], data, 56 );
maclobdell 0:f7c60d3e7b8a 131 GET_UINT32_LE( X[15], data, 60 );
maclobdell 0:f7c60d3e7b8a 132
maclobdell 0:f7c60d3e7b8a 133 #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
maclobdell 0:f7c60d3e7b8a 134
maclobdell 0:f7c60d3e7b8a 135 #define P(a,b,c,d,k,s,t) \
maclobdell 0:f7c60d3e7b8a 136 { \
maclobdell 0:f7c60d3e7b8a 137 a += F(b,c,d) + X[k] + t; a = S(a,s) + b; \
maclobdell 0:f7c60d3e7b8a 138 }
maclobdell 0:f7c60d3e7b8a 139
maclobdell 0:f7c60d3e7b8a 140 A = ctx->state[0];
maclobdell 0:f7c60d3e7b8a 141 B = ctx->state[1];
maclobdell 0:f7c60d3e7b8a 142 C = ctx->state[2];
maclobdell 0:f7c60d3e7b8a 143 D = ctx->state[3];
maclobdell 0:f7c60d3e7b8a 144
maclobdell 0:f7c60d3e7b8a 145 #define F(x,y,z) (z ^ (x & (y ^ z)))
maclobdell 0:f7c60d3e7b8a 146
maclobdell 0:f7c60d3e7b8a 147 P( A, B, C, D, 0, 7, 0xD76AA478 );
maclobdell 0:f7c60d3e7b8a 148 P( D, A, B, C, 1, 12, 0xE8C7B756 );
maclobdell 0:f7c60d3e7b8a 149 P( C, D, A, B, 2, 17, 0x242070DB );
maclobdell 0:f7c60d3e7b8a 150 P( B, C, D, A, 3, 22, 0xC1BDCEEE );
maclobdell 0:f7c60d3e7b8a 151 P( A, B, C, D, 4, 7, 0xF57C0FAF );
maclobdell 0:f7c60d3e7b8a 152 P( D, A, B, C, 5, 12, 0x4787C62A );
maclobdell 0:f7c60d3e7b8a 153 P( C, D, A, B, 6, 17, 0xA8304613 );
maclobdell 0:f7c60d3e7b8a 154 P( B, C, D, A, 7, 22, 0xFD469501 );
maclobdell 0:f7c60d3e7b8a 155 P( A, B, C, D, 8, 7, 0x698098D8 );
maclobdell 0:f7c60d3e7b8a 156 P( D, A, B, C, 9, 12, 0x8B44F7AF );
maclobdell 0:f7c60d3e7b8a 157 P( C, D, A, B, 10, 17, 0xFFFF5BB1 );
maclobdell 0:f7c60d3e7b8a 158 P( B, C, D, A, 11, 22, 0x895CD7BE );
maclobdell 0:f7c60d3e7b8a 159 P( A, B, C, D, 12, 7, 0x6B901122 );
maclobdell 0:f7c60d3e7b8a 160 P( D, A, B, C, 13, 12, 0xFD987193 );
maclobdell 0:f7c60d3e7b8a 161 P( C, D, A, B, 14, 17, 0xA679438E );
maclobdell 0:f7c60d3e7b8a 162 P( B, C, D, A, 15, 22, 0x49B40821 );
maclobdell 0:f7c60d3e7b8a 163
maclobdell 0:f7c60d3e7b8a 164 #undef F
maclobdell 0:f7c60d3e7b8a 165
maclobdell 0:f7c60d3e7b8a 166 #define F(x,y,z) (y ^ (z & (x ^ y)))
maclobdell 0:f7c60d3e7b8a 167
maclobdell 0:f7c60d3e7b8a 168 P( A, B, C, D, 1, 5, 0xF61E2562 );
maclobdell 0:f7c60d3e7b8a 169 P( D, A, B, C, 6, 9, 0xC040B340 );
maclobdell 0:f7c60d3e7b8a 170 P( C, D, A, B, 11, 14, 0x265E5A51 );
maclobdell 0:f7c60d3e7b8a 171 P( B, C, D, A, 0, 20, 0xE9B6C7AA );
maclobdell 0:f7c60d3e7b8a 172 P( A, B, C, D, 5, 5, 0xD62F105D );
maclobdell 0:f7c60d3e7b8a 173 P( D, A, B, C, 10, 9, 0x02441453 );
maclobdell 0:f7c60d3e7b8a 174 P( C, D, A, B, 15, 14, 0xD8A1E681 );
maclobdell 0:f7c60d3e7b8a 175 P( B, C, D, A, 4, 20, 0xE7D3FBC8 );
maclobdell 0:f7c60d3e7b8a 176 P( A, B, C, D, 9, 5, 0x21E1CDE6 );
maclobdell 0:f7c60d3e7b8a 177 P( D, A, B, C, 14, 9, 0xC33707D6 );
maclobdell 0:f7c60d3e7b8a 178 P( C, D, A, B, 3, 14, 0xF4D50D87 );
maclobdell 0:f7c60d3e7b8a 179 P( B, C, D, A, 8, 20, 0x455A14ED );
maclobdell 0:f7c60d3e7b8a 180 P( A, B, C, D, 13, 5, 0xA9E3E905 );
maclobdell 0:f7c60d3e7b8a 181 P( D, A, B, C, 2, 9, 0xFCEFA3F8 );
maclobdell 0:f7c60d3e7b8a 182 P( C, D, A, B, 7, 14, 0x676F02D9 );
maclobdell 0:f7c60d3e7b8a 183 P( B, C, D, A, 12, 20, 0x8D2A4C8A );
maclobdell 0:f7c60d3e7b8a 184
maclobdell 0:f7c60d3e7b8a 185 #undef F
maclobdell 0:f7c60d3e7b8a 186
maclobdell 0:f7c60d3e7b8a 187 #define F(x,y,z) (x ^ y ^ z)
maclobdell 0:f7c60d3e7b8a 188
maclobdell 0:f7c60d3e7b8a 189 P( A, B, C, D, 5, 4, 0xFFFA3942 );
maclobdell 0:f7c60d3e7b8a 190 P( D, A, B, C, 8, 11, 0x8771F681 );
maclobdell 0:f7c60d3e7b8a 191 P( C, D, A, B, 11, 16, 0x6D9D6122 );
maclobdell 0:f7c60d3e7b8a 192 P( B, C, D, A, 14, 23, 0xFDE5380C );
maclobdell 0:f7c60d3e7b8a 193 P( A, B, C, D, 1, 4, 0xA4BEEA44 );
maclobdell 0:f7c60d3e7b8a 194 P( D, A, B, C, 4, 11, 0x4BDECFA9 );
maclobdell 0:f7c60d3e7b8a 195 P( C, D, A, B, 7, 16, 0xF6BB4B60 );
maclobdell 0:f7c60d3e7b8a 196 P( B, C, D, A, 10, 23, 0xBEBFBC70 );
maclobdell 0:f7c60d3e7b8a 197 P( A, B, C, D, 13, 4, 0x289B7EC6 );
maclobdell 0:f7c60d3e7b8a 198 P( D, A, B, C, 0, 11, 0xEAA127FA );
maclobdell 0:f7c60d3e7b8a 199 P( C, D, A, B, 3, 16, 0xD4EF3085 );
maclobdell 0:f7c60d3e7b8a 200 P( B, C, D, A, 6, 23, 0x04881D05 );
maclobdell 0:f7c60d3e7b8a 201 P( A, B, C, D, 9, 4, 0xD9D4D039 );
maclobdell 0:f7c60d3e7b8a 202 P( D, A, B, C, 12, 11, 0xE6DB99E5 );
maclobdell 0:f7c60d3e7b8a 203 P( C, D, A, B, 15, 16, 0x1FA27CF8 );
maclobdell 0:f7c60d3e7b8a 204 P( B, C, D, A, 2, 23, 0xC4AC5665 );
maclobdell 0:f7c60d3e7b8a 205
maclobdell 0:f7c60d3e7b8a 206 #undef F
maclobdell 0:f7c60d3e7b8a 207
maclobdell 0:f7c60d3e7b8a 208 #define F(x,y,z) (y ^ (x | ~z))
maclobdell 0:f7c60d3e7b8a 209
maclobdell 0:f7c60d3e7b8a 210 P( A, B, C, D, 0, 6, 0xF4292244 );
maclobdell 0:f7c60d3e7b8a 211 P( D, A, B, C, 7, 10, 0x432AFF97 );
maclobdell 0:f7c60d3e7b8a 212 P( C, D, A, B, 14, 15, 0xAB9423A7 );
maclobdell 0:f7c60d3e7b8a 213 P( B, C, D, A, 5, 21, 0xFC93A039 );
maclobdell 0:f7c60d3e7b8a 214 P( A, B, C, D, 12, 6, 0x655B59C3 );
maclobdell 0:f7c60d3e7b8a 215 P( D, A, B, C, 3, 10, 0x8F0CCC92 );
maclobdell 0:f7c60d3e7b8a 216 P( C, D, A, B, 10, 15, 0xFFEFF47D );
maclobdell 0:f7c60d3e7b8a 217 P( B, C, D, A, 1, 21, 0x85845DD1 );
maclobdell 0:f7c60d3e7b8a 218 P( A, B, C, D, 8, 6, 0x6FA87E4F );
maclobdell 0:f7c60d3e7b8a 219 P( D, A, B, C, 15, 10, 0xFE2CE6E0 );
maclobdell 0:f7c60d3e7b8a 220 P( C, D, A, B, 6, 15, 0xA3014314 );
maclobdell 0:f7c60d3e7b8a 221 P( B, C, D, A, 13, 21, 0x4E0811A1 );
maclobdell 0:f7c60d3e7b8a 222 P( A, B, C, D, 4, 6, 0xF7537E82 );
maclobdell 0:f7c60d3e7b8a 223 P( D, A, B, C, 11, 10, 0xBD3AF235 );
maclobdell 0:f7c60d3e7b8a 224 P( C, D, A, B, 2, 15, 0x2AD7D2BB );
maclobdell 0:f7c60d3e7b8a 225 P( B, C, D, A, 9, 21, 0xEB86D391 );
maclobdell 0:f7c60d3e7b8a 226
maclobdell 0:f7c60d3e7b8a 227 #undef F
maclobdell 0:f7c60d3e7b8a 228
maclobdell 0:f7c60d3e7b8a 229 ctx->state[0] += A;
maclobdell 0:f7c60d3e7b8a 230 ctx->state[1] += B;
maclobdell 0:f7c60d3e7b8a 231 ctx->state[2] += C;
maclobdell 0:f7c60d3e7b8a 232 ctx->state[3] += D;
maclobdell 0:f7c60d3e7b8a 233 }
maclobdell 0:f7c60d3e7b8a 234 #endif /* !MBEDTLS_MD5_PROCESS_ALT */
maclobdell 0:f7c60d3e7b8a 235
maclobdell 0:f7c60d3e7b8a 236 /*
maclobdell 0:f7c60d3e7b8a 237 * MD5 process buffer
maclobdell 0:f7c60d3e7b8a 238 */
maclobdell 0:f7c60d3e7b8a 239 void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen )
maclobdell 0:f7c60d3e7b8a 240 {
maclobdell 0:f7c60d3e7b8a 241 size_t fill;
maclobdell 0:f7c60d3e7b8a 242 uint32_t left;
maclobdell 0:f7c60d3e7b8a 243
maclobdell 0:f7c60d3e7b8a 244 if( ilen == 0 )
maclobdell 0:f7c60d3e7b8a 245 return;
maclobdell 0:f7c60d3e7b8a 246
maclobdell 0:f7c60d3e7b8a 247 left = ctx->total[0] & 0x3F;
maclobdell 0:f7c60d3e7b8a 248 fill = 64 - left;
maclobdell 0:f7c60d3e7b8a 249
maclobdell 0:f7c60d3e7b8a 250 ctx->total[0] += (uint32_t) ilen;
maclobdell 0:f7c60d3e7b8a 251 ctx->total[0] &= 0xFFFFFFFF;
maclobdell 0:f7c60d3e7b8a 252
maclobdell 0:f7c60d3e7b8a 253 if( ctx->total[0] < (uint32_t) ilen )
maclobdell 0:f7c60d3e7b8a 254 ctx->total[1]++;
maclobdell 0:f7c60d3e7b8a 255
maclobdell 0:f7c60d3e7b8a 256 if( left && ilen >= fill )
maclobdell 0:f7c60d3e7b8a 257 {
maclobdell 0:f7c60d3e7b8a 258 memcpy( (void *) (ctx->buffer + left), input, fill );
maclobdell 0:f7c60d3e7b8a 259 mbedtls_md5_process( ctx, ctx->buffer );
maclobdell 0:f7c60d3e7b8a 260 input += fill;
maclobdell 0:f7c60d3e7b8a 261 ilen -= fill;
maclobdell 0:f7c60d3e7b8a 262 left = 0;
maclobdell 0:f7c60d3e7b8a 263 }
maclobdell 0:f7c60d3e7b8a 264
maclobdell 0:f7c60d3e7b8a 265 while( ilen >= 64 )
maclobdell 0:f7c60d3e7b8a 266 {
maclobdell 0:f7c60d3e7b8a 267 mbedtls_md5_process( ctx, input );
maclobdell 0:f7c60d3e7b8a 268 input += 64;
maclobdell 0:f7c60d3e7b8a 269 ilen -= 64;
maclobdell 0:f7c60d3e7b8a 270 }
maclobdell 0:f7c60d3e7b8a 271
maclobdell 0:f7c60d3e7b8a 272 if( ilen > 0 )
maclobdell 0:f7c60d3e7b8a 273 {
maclobdell 0:f7c60d3e7b8a 274 memcpy( (void *) (ctx->buffer + left), input, ilen );
maclobdell 0:f7c60d3e7b8a 275 }
maclobdell 0:f7c60d3e7b8a 276 }
maclobdell 0:f7c60d3e7b8a 277
maclobdell 0:f7c60d3e7b8a 278 static const unsigned char md5_padding[64] =
maclobdell 0:f7c60d3e7b8a 279 {
maclobdell 0:f7c60d3e7b8a 280 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
maclobdell 0:f7c60d3e7b8a 281 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
maclobdell 0:f7c60d3e7b8a 282 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
maclobdell 0:f7c60d3e7b8a 283 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
maclobdell 0:f7c60d3e7b8a 284 };
maclobdell 0:f7c60d3e7b8a 285
maclobdell 0:f7c60d3e7b8a 286 /*
maclobdell 0:f7c60d3e7b8a 287 * MD5 final digest
maclobdell 0:f7c60d3e7b8a 288 */
maclobdell 0:f7c60d3e7b8a 289 void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] )
maclobdell 0:f7c60d3e7b8a 290 {
maclobdell 0:f7c60d3e7b8a 291 uint32_t last, padn;
maclobdell 0:f7c60d3e7b8a 292 uint32_t high, low;
maclobdell 0:f7c60d3e7b8a 293 unsigned char msglen[8];
maclobdell 0:f7c60d3e7b8a 294
maclobdell 0:f7c60d3e7b8a 295 high = ( ctx->total[0] >> 29 )
maclobdell 0:f7c60d3e7b8a 296 | ( ctx->total[1] << 3 );
maclobdell 0:f7c60d3e7b8a 297 low = ( ctx->total[0] << 3 );
maclobdell 0:f7c60d3e7b8a 298
maclobdell 0:f7c60d3e7b8a 299 PUT_UINT32_LE( low, msglen, 0 );
maclobdell 0:f7c60d3e7b8a 300 PUT_UINT32_LE( high, msglen, 4 );
maclobdell 0:f7c60d3e7b8a 301
maclobdell 0:f7c60d3e7b8a 302 last = ctx->total[0] & 0x3F;
maclobdell 0:f7c60d3e7b8a 303 padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
maclobdell 0:f7c60d3e7b8a 304
maclobdell 0:f7c60d3e7b8a 305 mbedtls_md5_update( ctx, md5_padding, padn );
maclobdell 0:f7c60d3e7b8a 306 mbedtls_md5_update( ctx, msglen, 8 );
maclobdell 0:f7c60d3e7b8a 307
maclobdell 0:f7c60d3e7b8a 308 PUT_UINT32_LE( ctx->state[0], output, 0 );
maclobdell 0:f7c60d3e7b8a 309 PUT_UINT32_LE( ctx->state[1], output, 4 );
maclobdell 0:f7c60d3e7b8a 310 PUT_UINT32_LE( ctx->state[2], output, 8 );
maclobdell 0:f7c60d3e7b8a 311 PUT_UINT32_LE( ctx->state[3], output, 12 );
maclobdell 0:f7c60d3e7b8a 312 }
maclobdell 0:f7c60d3e7b8a 313
maclobdell 0:f7c60d3e7b8a 314 #endif /* !MBEDTLS_MD5_ALT */
maclobdell 0:f7c60d3e7b8a 315
maclobdell 0:f7c60d3e7b8a 316 /*
maclobdell 0:f7c60d3e7b8a 317 * output = MD5( input buffer )
maclobdell 0:f7c60d3e7b8a 318 */
maclobdell 0:f7c60d3e7b8a 319 void mbedtls_md5( const unsigned char *input, size_t ilen, unsigned char output[16] )
maclobdell 0:f7c60d3e7b8a 320 {
maclobdell 0:f7c60d3e7b8a 321 mbedtls_md5_context ctx;
maclobdell 0:f7c60d3e7b8a 322
maclobdell 0:f7c60d3e7b8a 323 mbedtls_md5_init( &ctx );
maclobdell 0:f7c60d3e7b8a 324 mbedtls_md5_starts( &ctx );
maclobdell 0:f7c60d3e7b8a 325 mbedtls_md5_update( &ctx, input, ilen );
maclobdell 0:f7c60d3e7b8a 326 mbedtls_md5_finish( &ctx, output );
maclobdell 0:f7c60d3e7b8a 327 mbedtls_md5_free( &ctx );
maclobdell 0:f7c60d3e7b8a 328 }
maclobdell 0:f7c60d3e7b8a 329
maclobdell 0:f7c60d3e7b8a 330 #if defined(MBEDTLS_SELF_TEST)
maclobdell 0:f7c60d3e7b8a 331 /*
maclobdell 0:f7c60d3e7b8a 332 * RFC 1321 test vectors
maclobdell 0:f7c60d3e7b8a 333 */
maclobdell 0:f7c60d3e7b8a 334 static const unsigned char md5_test_buf[7][81] =
maclobdell 0:f7c60d3e7b8a 335 {
maclobdell 0:f7c60d3e7b8a 336 { "" },
maclobdell 0:f7c60d3e7b8a 337 { "a" },
maclobdell 0:f7c60d3e7b8a 338 { "abc" },
maclobdell 0:f7c60d3e7b8a 339 { "message digest" },
maclobdell 0:f7c60d3e7b8a 340 { "abcdefghijklmnopqrstuvwxyz" },
maclobdell 0:f7c60d3e7b8a 341 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
maclobdell 0:f7c60d3e7b8a 342 { "12345678901234567890123456789012345678901234567890123456789012" \
maclobdell 0:f7c60d3e7b8a 343 "345678901234567890" }
maclobdell 0:f7c60d3e7b8a 344 };
maclobdell 0:f7c60d3e7b8a 345
maclobdell 0:f7c60d3e7b8a 346 static const int md5_test_buflen[7] =
maclobdell 0:f7c60d3e7b8a 347 {
maclobdell 0:f7c60d3e7b8a 348 0, 1, 3, 14, 26, 62, 80
maclobdell 0:f7c60d3e7b8a 349 };
maclobdell 0:f7c60d3e7b8a 350
maclobdell 0:f7c60d3e7b8a 351 static const unsigned char md5_test_sum[7][16] =
maclobdell 0:f7c60d3e7b8a 352 {
maclobdell 0:f7c60d3e7b8a 353 { 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04,
maclobdell 0:f7c60d3e7b8a 354 0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E },
maclobdell 0:f7c60d3e7b8a 355 { 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8,
maclobdell 0:f7c60d3e7b8a 356 0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 },
maclobdell 0:f7c60d3e7b8a 357 { 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0,
maclobdell 0:f7c60d3e7b8a 358 0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 },
maclobdell 0:f7c60d3e7b8a 359 { 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D,
maclobdell 0:f7c60d3e7b8a 360 0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 },
maclobdell 0:f7c60d3e7b8a 361 { 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00,
maclobdell 0:f7c60d3e7b8a 362 0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B },
maclobdell 0:f7c60d3e7b8a 363 { 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5,
maclobdell 0:f7c60d3e7b8a 364 0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F },
maclobdell 0:f7c60d3e7b8a 365 { 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55,
maclobdell 0:f7c60d3e7b8a 366 0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A }
maclobdell 0:f7c60d3e7b8a 367 };
maclobdell 0:f7c60d3e7b8a 368
maclobdell 0:f7c60d3e7b8a 369 /*
maclobdell 0:f7c60d3e7b8a 370 * Checkup routine
maclobdell 0:f7c60d3e7b8a 371 */
maclobdell 0:f7c60d3e7b8a 372 int mbedtls_md5_self_test( int verbose )
maclobdell 0:f7c60d3e7b8a 373 {
maclobdell 0:f7c60d3e7b8a 374 int i;
maclobdell 0:f7c60d3e7b8a 375 unsigned char md5sum[16];
maclobdell 0:f7c60d3e7b8a 376
maclobdell 0:f7c60d3e7b8a 377 for( i = 0; i < 7; i++ )
maclobdell 0:f7c60d3e7b8a 378 {
maclobdell 0:f7c60d3e7b8a 379 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 380 mbedtls_printf( " MD5 test #%d: ", i + 1 );
maclobdell 0:f7c60d3e7b8a 381
maclobdell 0:f7c60d3e7b8a 382 mbedtls_md5( md5_test_buf[i], md5_test_buflen[i], md5sum );
maclobdell 0:f7c60d3e7b8a 383
maclobdell 0:f7c60d3e7b8a 384 if( memcmp( md5sum, md5_test_sum[i], 16 ) != 0 )
maclobdell 0:f7c60d3e7b8a 385 {
maclobdell 0:f7c60d3e7b8a 386 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 387 mbedtls_printf( "failed\n" );
maclobdell 0:f7c60d3e7b8a 388
maclobdell 0:f7c60d3e7b8a 389 return( 1 );
maclobdell 0:f7c60d3e7b8a 390 }
maclobdell 0:f7c60d3e7b8a 391
maclobdell 0:f7c60d3e7b8a 392 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 393 mbedtls_printf( "passed\n" );
maclobdell 0:f7c60d3e7b8a 394 }
maclobdell 0:f7c60d3e7b8a 395
maclobdell 0:f7c60d3e7b8a 396 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 397 mbedtls_printf( "\n" );
maclobdell 0:f7c60d3e7b8a 398
maclobdell 0:f7c60d3e7b8a 399 return( 0 );
maclobdell 0:f7c60d3e7b8a 400 }
maclobdell 0:f7c60d3e7b8a 401
maclobdell 0:f7c60d3e7b8a 402 #endif /* MBEDTLS_SELF_TEST */
maclobdell 0:f7c60d3e7b8a 403
maclobdell 0:f7c60d3e7b8a 404 #endif /* MBEDTLS_MD5_C */