nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nitsshukla
Date:
Fri Nov 04 12:06:04 2016 +0000
Revision:
7:3a65ef12ba31
Parent:
1:55a6170b404f
kghj;

Who changed what in which revision?

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