mbedtls ported to mbed-classic

Fork of mbedtls by Christopher Haster

Committer:
Brian Daniels
Date:
Thu Apr 07 11:11:18 2016 +0100
Revision:
4:bef26f687287
Parent:
1:24750b9ad5ef
Adding ported selftest test case

Who changed what in which revision?

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