Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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