Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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