Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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