This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
maclobdell
Date:
Wed May 18 19:06:32 2016 +0000
Revision:
0:f7c60d3e7b8a
clean version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maclobdell 0:f7c60d3e7b8a 1 /*
maclobdell 0:f7c60d3e7b8a 2 * RFC 1186/1320 compliant MD4 implementation
maclobdell 0:f7c60d3e7b8a 3 *
maclobdell 0:f7c60d3e7b8a 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
maclobdell 0:f7c60d3e7b8a 5 * SPDX-License-Identifier: Apache-2.0
maclobdell 0:f7c60d3e7b8a 6 *
maclobdell 0:f7c60d3e7b8a 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
maclobdell 0:f7c60d3e7b8a 8 * not use this file except in compliance with the License.
maclobdell 0:f7c60d3e7b8a 9 * You may obtain a copy of the License at
maclobdell 0:f7c60d3e7b8a 10 *
maclobdell 0:f7c60d3e7b8a 11 * http://www.apache.org/licenses/LICENSE-2.0
maclobdell 0:f7c60d3e7b8a 12 *
maclobdell 0:f7c60d3e7b8a 13 * Unless required by applicable law or agreed to in writing, software
maclobdell 0:f7c60d3e7b8a 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
maclobdell 0:f7c60d3e7b8a 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
maclobdell 0:f7c60d3e7b8a 16 * See the License for the specific language governing permissions and
maclobdell 0:f7c60d3e7b8a 17 * limitations under the License.
maclobdell 0:f7c60d3e7b8a 18 *
maclobdell 0:f7c60d3e7b8a 19 * This file is part of mbed TLS (https://tls.mbed.org)
maclobdell 0:f7c60d3e7b8a 20 */
maclobdell 0:f7c60d3e7b8a 21 /*
maclobdell 0:f7c60d3e7b8a 22 * The MD4 algorithm was designed by Ron Rivest in 1990.
maclobdell 0:f7c60d3e7b8a 23 *
maclobdell 0:f7c60d3e7b8a 24 * http://www.ietf.org/rfc/rfc1186.txt
maclobdell 0:f7c60d3e7b8a 25 * http://www.ietf.org/rfc/rfc1320.txt
maclobdell 0:f7c60d3e7b8a 26 */
maclobdell 0:f7c60d3e7b8a 27
maclobdell 0:f7c60d3e7b8a 28 #if !defined(MBEDTLS_CONFIG_FILE)
maclobdell 0:f7c60d3e7b8a 29 #include "mbedtls/config.h"
maclobdell 0:f7c60d3e7b8a 30 #else
maclobdell 0:f7c60d3e7b8a 31 #include MBEDTLS_CONFIG_FILE
maclobdell 0:f7c60d3e7b8a 32 #endif
maclobdell 0:f7c60d3e7b8a 33
maclobdell 0:f7c60d3e7b8a 34 #if defined(MBEDTLS_MD4_C)
maclobdell 0:f7c60d3e7b8a 35
maclobdell 0:f7c60d3e7b8a 36 #include "mbedtls/md4.h"
maclobdell 0:f7c60d3e7b8a 37
maclobdell 0:f7c60d3e7b8a 38 #include <string.h>
maclobdell 0:f7c60d3e7b8a 39
maclobdell 0:f7c60d3e7b8a 40 #if defined(MBEDTLS_SELF_TEST)
maclobdell 0:f7c60d3e7b8a 41 #if defined(MBEDTLS_PLATFORM_C)
maclobdell 0:f7c60d3e7b8a 42 #include "mbedtls/platform.h"
maclobdell 0:f7c60d3e7b8a 43 #else
maclobdell 0:f7c60d3e7b8a 44 #include <stdio.h>
maclobdell 0:f7c60d3e7b8a 45 #define mbedtls_printf printf
maclobdell 0:f7c60d3e7b8a 46 #endif /* MBEDTLS_PLATFORM_C */
maclobdell 0:f7c60d3e7b8a 47 #endif /* MBEDTLS_SELF_TEST */
maclobdell 0:f7c60d3e7b8a 48
maclobdell 0:f7c60d3e7b8a 49 #if !defined(MBEDTLS_MD4_ALT)
maclobdell 0:f7c60d3e7b8a 50
maclobdell 0:f7c60d3e7b8a 51 /* Implementation that should never be optimized out by the compiler */
maclobdell 0:f7c60d3e7b8a 52 static void mbedtls_zeroize( void *v, size_t n ) {
maclobdell 0:f7c60d3e7b8a 53 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
maclobdell 0:f7c60d3e7b8a 54 }
maclobdell 0:f7c60d3e7b8a 55
maclobdell 0:f7c60d3e7b8a 56 /*
maclobdell 0:f7c60d3e7b8a 57 * 32-bit integer manipulation macros (little endian)
maclobdell 0:f7c60d3e7b8a 58 */
maclobdell 0:f7c60d3e7b8a 59 #ifndef GET_UINT32_LE
maclobdell 0:f7c60d3e7b8a 60 #define GET_UINT32_LE(n,b,i) \
maclobdell 0:f7c60d3e7b8a 61 { \
maclobdell 0:f7c60d3e7b8a 62 (n) = ( (uint32_t) (b)[(i) ] ) \
maclobdell 0:f7c60d3e7b8a 63 | ( (uint32_t) (b)[(i) + 1] << 8 ) \
maclobdell 0:f7c60d3e7b8a 64 | ( (uint32_t) (b)[(i) + 2] << 16 ) \
maclobdell 0:f7c60d3e7b8a 65 | ( (uint32_t) (b)[(i) + 3] << 24 ); \
maclobdell 0:f7c60d3e7b8a 66 }
maclobdell 0:f7c60d3e7b8a 67 #endif
maclobdell 0:f7c60d3e7b8a 68
maclobdell 0:f7c60d3e7b8a 69 #ifndef PUT_UINT32_LE
maclobdell 0:f7c60d3e7b8a 70 #define PUT_UINT32_LE(n,b,i) \
maclobdell 0:f7c60d3e7b8a 71 { \
maclobdell 0:f7c60d3e7b8a 72 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
maclobdell 0:f7c60d3e7b8a 73 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
maclobdell 0:f7c60d3e7b8a 74 (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
maclobdell 0:f7c60d3e7b8a 75 (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
maclobdell 0:f7c60d3e7b8a 76 }
maclobdell 0:f7c60d3e7b8a 77 #endif
maclobdell 0:f7c60d3e7b8a 78
maclobdell 0:f7c60d3e7b8a 79 void mbedtls_md4_init( mbedtls_md4_context *ctx )
maclobdell 0:f7c60d3e7b8a 80 {
maclobdell 0:f7c60d3e7b8a 81 memset( ctx, 0, sizeof( mbedtls_md4_context ) );
maclobdell 0:f7c60d3e7b8a 82 }
maclobdell 0:f7c60d3e7b8a 83
maclobdell 0:f7c60d3e7b8a 84 void mbedtls_md4_free( mbedtls_md4_context *ctx )
maclobdell 0:f7c60d3e7b8a 85 {
maclobdell 0:f7c60d3e7b8a 86 if( ctx == NULL )
maclobdell 0:f7c60d3e7b8a 87 return;
maclobdell 0:f7c60d3e7b8a 88
maclobdell 0:f7c60d3e7b8a 89 mbedtls_zeroize( ctx, sizeof( mbedtls_md4_context ) );
maclobdell 0:f7c60d3e7b8a 90 }
maclobdell 0:f7c60d3e7b8a 91
maclobdell 0:f7c60d3e7b8a 92 void mbedtls_md4_clone( mbedtls_md4_context *dst,
maclobdell 0:f7c60d3e7b8a 93 const mbedtls_md4_context *src )
maclobdell 0:f7c60d3e7b8a 94 {
maclobdell 0:f7c60d3e7b8a 95 *dst = *src;
maclobdell 0:f7c60d3e7b8a 96 }
maclobdell 0:f7c60d3e7b8a 97
maclobdell 0:f7c60d3e7b8a 98 /*
maclobdell 0:f7c60d3e7b8a 99 * MD4 context setup
maclobdell 0:f7c60d3e7b8a 100 */
maclobdell 0:f7c60d3e7b8a 101 void mbedtls_md4_starts( mbedtls_md4_context *ctx )
maclobdell 0:f7c60d3e7b8a 102 {
maclobdell 0:f7c60d3e7b8a 103 ctx->total[0] = 0;
maclobdell 0:f7c60d3e7b8a 104 ctx->total[1] = 0;
maclobdell 0:f7c60d3e7b8a 105
maclobdell 0:f7c60d3e7b8a 106 ctx->state[0] = 0x67452301;
maclobdell 0:f7c60d3e7b8a 107 ctx->state[1] = 0xEFCDAB89;
maclobdell 0:f7c60d3e7b8a 108 ctx->state[2] = 0x98BADCFE;
maclobdell 0:f7c60d3e7b8a 109 ctx->state[3] = 0x10325476;
maclobdell 0:f7c60d3e7b8a 110 }
maclobdell 0:f7c60d3e7b8a 111
maclobdell 0:f7c60d3e7b8a 112 #if !defined(MBEDTLS_MD4_PROCESS_ALT)
maclobdell 0:f7c60d3e7b8a 113 void mbedtls_md4_process( mbedtls_md4_context *ctx, const unsigned char data[64] )
maclobdell 0:f7c60d3e7b8a 114 {
maclobdell 0:f7c60d3e7b8a 115 uint32_t X[16], A, B, C, D;
maclobdell 0:f7c60d3e7b8a 116
maclobdell 0:f7c60d3e7b8a 117 GET_UINT32_LE( X[ 0], data, 0 );
maclobdell 0:f7c60d3e7b8a 118 GET_UINT32_LE( X[ 1], data, 4 );
maclobdell 0:f7c60d3e7b8a 119 GET_UINT32_LE( X[ 2], data, 8 );
maclobdell 0:f7c60d3e7b8a 120 GET_UINT32_LE( X[ 3], data, 12 );
maclobdell 0:f7c60d3e7b8a 121 GET_UINT32_LE( X[ 4], data, 16 );
maclobdell 0:f7c60d3e7b8a 122 GET_UINT32_LE( X[ 5], data, 20 );
maclobdell 0:f7c60d3e7b8a 123 GET_UINT32_LE( X[ 6], data, 24 );
maclobdell 0:f7c60d3e7b8a 124 GET_UINT32_LE( X[ 7], data, 28 );
maclobdell 0:f7c60d3e7b8a 125 GET_UINT32_LE( X[ 8], data, 32 );
maclobdell 0:f7c60d3e7b8a 126 GET_UINT32_LE( X[ 9], data, 36 );
maclobdell 0:f7c60d3e7b8a 127 GET_UINT32_LE( X[10], data, 40 );
maclobdell 0:f7c60d3e7b8a 128 GET_UINT32_LE( X[11], data, 44 );
maclobdell 0:f7c60d3e7b8a 129 GET_UINT32_LE( X[12], data, 48 );
maclobdell 0:f7c60d3e7b8a 130 GET_UINT32_LE( X[13], data, 52 );
maclobdell 0:f7c60d3e7b8a 131 GET_UINT32_LE( X[14], data, 56 );
maclobdell 0:f7c60d3e7b8a 132 GET_UINT32_LE( X[15], data, 60 );
maclobdell 0:f7c60d3e7b8a 133
maclobdell 0:f7c60d3e7b8a 134 #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
maclobdell 0:f7c60d3e7b8a 135
maclobdell 0:f7c60d3e7b8a 136 A = ctx->state[0];
maclobdell 0:f7c60d3e7b8a 137 B = ctx->state[1];
maclobdell 0:f7c60d3e7b8a 138 C = ctx->state[2];
maclobdell 0:f7c60d3e7b8a 139 D = ctx->state[3];
maclobdell 0:f7c60d3e7b8a 140
maclobdell 0:f7c60d3e7b8a 141 #define F(x, y, z) ((x & y) | ((~x) & z))
maclobdell 0:f7c60d3e7b8a 142 #define P(a,b,c,d,x,s) { a += F(b,c,d) + x; a = S(a,s); }
maclobdell 0:f7c60d3e7b8a 143
maclobdell 0:f7c60d3e7b8a 144 P( A, B, C, D, X[ 0], 3 );
maclobdell 0:f7c60d3e7b8a 145 P( D, A, B, C, X[ 1], 7 );
maclobdell 0:f7c60d3e7b8a 146 P( C, D, A, B, X[ 2], 11 );
maclobdell 0:f7c60d3e7b8a 147 P( B, C, D, A, X[ 3], 19 );
maclobdell 0:f7c60d3e7b8a 148 P( A, B, C, D, X[ 4], 3 );
maclobdell 0:f7c60d3e7b8a 149 P( D, A, B, C, X[ 5], 7 );
maclobdell 0:f7c60d3e7b8a 150 P( C, D, A, B, X[ 6], 11 );
maclobdell 0:f7c60d3e7b8a 151 P( B, C, D, A, X[ 7], 19 );
maclobdell 0:f7c60d3e7b8a 152 P( A, B, C, D, X[ 8], 3 );
maclobdell 0:f7c60d3e7b8a 153 P( D, A, B, C, X[ 9], 7 );
maclobdell 0:f7c60d3e7b8a 154 P( C, D, A, B, X[10], 11 );
maclobdell 0:f7c60d3e7b8a 155 P( B, C, D, A, X[11], 19 );
maclobdell 0:f7c60d3e7b8a 156 P( A, B, C, D, X[12], 3 );
maclobdell 0:f7c60d3e7b8a 157 P( D, A, B, C, X[13], 7 );
maclobdell 0:f7c60d3e7b8a 158 P( C, D, A, B, X[14], 11 );
maclobdell 0:f7c60d3e7b8a 159 P( B, C, D, A, X[15], 19 );
maclobdell 0:f7c60d3e7b8a 160
maclobdell 0:f7c60d3e7b8a 161 #undef P
maclobdell 0:f7c60d3e7b8a 162 #undef F
maclobdell 0:f7c60d3e7b8a 163
maclobdell 0:f7c60d3e7b8a 164 #define F(x,y,z) ((x & y) | (x & z) | (y & z))
maclobdell 0:f7c60d3e7b8a 165 #define P(a,b,c,d,x,s) { a += F(b,c,d) + x + 0x5A827999; a = S(a,s); }
maclobdell 0:f7c60d3e7b8a 166
maclobdell 0:f7c60d3e7b8a 167 P( A, B, C, D, X[ 0], 3 );
maclobdell 0:f7c60d3e7b8a 168 P( D, A, B, C, X[ 4], 5 );
maclobdell 0:f7c60d3e7b8a 169 P( C, D, A, B, X[ 8], 9 );
maclobdell 0:f7c60d3e7b8a 170 P( B, C, D, A, X[12], 13 );
maclobdell 0:f7c60d3e7b8a 171 P( A, B, C, D, X[ 1], 3 );
maclobdell 0:f7c60d3e7b8a 172 P( D, A, B, C, X[ 5], 5 );
maclobdell 0:f7c60d3e7b8a 173 P( C, D, A, B, X[ 9], 9 );
maclobdell 0:f7c60d3e7b8a 174 P( B, C, D, A, X[13], 13 );
maclobdell 0:f7c60d3e7b8a 175 P( A, B, C, D, X[ 2], 3 );
maclobdell 0:f7c60d3e7b8a 176 P( D, A, B, C, X[ 6], 5 );
maclobdell 0:f7c60d3e7b8a 177 P( C, D, A, B, X[10], 9 );
maclobdell 0:f7c60d3e7b8a 178 P( B, C, D, A, X[14], 13 );
maclobdell 0:f7c60d3e7b8a 179 P( A, B, C, D, X[ 3], 3 );
maclobdell 0:f7c60d3e7b8a 180 P( D, A, B, C, X[ 7], 5 );
maclobdell 0:f7c60d3e7b8a 181 P( C, D, A, B, X[11], 9 );
maclobdell 0:f7c60d3e7b8a 182 P( B, C, D, A, X[15], 13 );
maclobdell 0:f7c60d3e7b8a 183
maclobdell 0:f7c60d3e7b8a 184 #undef P
maclobdell 0:f7c60d3e7b8a 185 #undef F
maclobdell 0:f7c60d3e7b8a 186
maclobdell 0:f7c60d3e7b8a 187 #define F(x,y,z) (x ^ y ^ z)
maclobdell 0:f7c60d3e7b8a 188 #define P(a,b,c,d,x,s) { a += F(b,c,d) + x + 0x6ED9EBA1; a = S(a,s); }
maclobdell 0:f7c60d3e7b8a 189
maclobdell 0:f7c60d3e7b8a 190 P( A, B, C, D, X[ 0], 3 );
maclobdell 0:f7c60d3e7b8a 191 P( D, A, B, C, X[ 8], 9 );
maclobdell 0:f7c60d3e7b8a 192 P( C, D, A, B, X[ 4], 11 );
maclobdell 0:f7c60d3e7b8a 193 P( B, C, D, A, X[12], 15 );
maclobdell 0:f7c60d3e7b8a 194 P( A, B, C, D, X[ 2], 3 );
maclobdell 0:f7c60d3e7b8a 195 P( D, A, B, C, X[10], 9 );
maclobdell 0:f7c60d3e7b8a 196 P( C, D, A, B, X[ 6], 11 );
maclobdell 0:f7c60d3e7b8a 197 P( B, C, D, A, X[14], 15 );
maclobdell 0:f7c60d3e7b8a 198 P( A, B, C, D, X[ 1], 3 );
maclobdell 0:f7c60d3e7b8a 199 P( D, A, B, C, X[ 9], 9 );
maclobdell 0:f7c60d3e7b8a 200 P( C, D, A, B, X[ 5], 11 );
maclobdell 0:f7c60d3e7b8a 201 P( B, C, D, A, X[13], 15 );
maclobdell 0:f7c60d3e7b8a 202 P( A, B, C, D, X[ 3], 3 );
maclobdell 0:f7c60d3e7b8a 203 P( D, A, B, C, X[11], 9 );
maclobdell 0:f7c60d3e7b8a 204 P( C, D, A, B, X[ 7], 11 );
maclobdell 0:f7c60d3e7b8a 205 P( B, C, D, A, X[15], 15 );
maclobdell 0:f7c60d3e7b8a 206
maclobdell 0:f7c60d3e7b8a 207 #undef F
maclobdell 0:f7c60d3e7b8a 208 #undef P
maclobdell 0:f7c60d3e7b8a 209
maclobdell 0:f7c60d3e7b8a 210 ctx->state[0] += A;
maclobdell 0:f7c60d3e7b8a 211 ctx->state[1] += B;
maclobdell 0:f7c60d3e7b8a 212 ctx->state[2] += C;
maclobdell 0:f7c60d3e7b8a 213 ctx->state[3] += D;
maclobdell 0:f7c60d3e7b8a 214 }
maclobdell 0:f7c60d3e7b8a 215 #endif /* !MBEDTLS_MD4_PROCESS_ALT */
maclobdell 0:f7c60d3e7b8a 216
maclobdell 0:f7c60d3e7b8a 217 /*
maclobdell 0:f7c60d3e7b8a 218 * MD4 process buffer
maclobdell 0:f7c60d3e7b8a 219 */
maclobdell 0:f7c60d3e7b8a 220 void mbedtls_md4_update( mbedtls_md4_context *ctx, const unsigned char *input, size_t ilen )
maclobdell 0:f7c60d3e7b8a 221 {
maclobdell 0:f7c60d3e7b8a 222 size_t fill;
maclobdell 0:f7c60d3e7b8a 223 uint32_t left;
maclobdell 0:f7c60d3e7b8a 224
maclobdell 0:f7c60d3e7b8a 225 if( ilen == 0 )
maclobdell 0:f7c60d3e7b8a 226 return;
maclobdell 0:f7c60d3e7b8a 227
maclobdell 0:f7c60d3e7b8a 228 left = ctx->total[0] & 0x3F;
maclobdell 0:f7c60d3e7b8a 229 fill = 64 - left;
maclobdell 0:f7c60d3e7b8a 230
maclobdell 0:f7c60d3e7b8a 231 ctx->total[0] += (uint32_t) ilen;
maclobdell 0:f7c60d3e7b8a 232 ctx->total[0] &= 0xFFFFFFFF;
maclobdell 0:f7c60d3e7b8a 233
maclobdell 0:f7c60d3e7b8a 234 if( ctx->total[0] < (uint32_t) ilen )
maclobdell 0:f7c60d3e7b8a 235 ctx->total[1]++;
maclobdell 0:f7c60d3e7b8a 236
maclobdell 0:f7c60d3e7b8a 237 if( left && ilen >= fill )
maclobdell 0:f7c60d3e7b8a 238 {
maclobdell 0:f7c60d3e7b8a 239 memcpy( (void *) (ctx->buffer + left),
maclobdell 0:f7c60d3e7b8a 240 (void *) input, fill );
maclobdell 0:f7c60d3e7b8a 241 mbedtls_md4_process( ctx, ctx->buffer );
maclobdell 0:f7c60d3e7b8a 242 input += fill;
maclobdell 0:f7c60d3e7b8a 243 ilen -= fill;
maclobdell 0:f7c60d3e7b8a 244 left = 0;
maclobdell 0:f7c60d3e7b8a 245 }
maclobdell 0:f7c60d3e7b8a 246
maclobdell 0:f7c60d3e7b8a 247 while( ilen >= 64 )
maclobdell 0:f7c60d3e7b8a 248 {
maclobdell 0:f7c60d3e7b8a 249 mbedtls_md4_process( ctx, input );
maclobdell 0:f7c60d3e7b8a 250 input += 64;
maclobdell 0:f7c60d3e7b8a 251 ilen -= 64;
maclobdell 0:f7c60d3e7b8a 252 }
maclobdell 0:f7c60d3e7b8a 253
maclobdell 0:f7c60d3e7b8a 254 if( ilen > 0 )
maclobdell 0:f7c60d3e7b8a 255 {
maclobdell 0:f7c60d3e7b8a 256 memcpy( (void *) (ctx->buffer + left),
maclobdell 0:f7c60d3e7b8a 257 (void *) input, ilen );
maclobdell 0:f7c60d3e7b8a 258 }
maclobdell 0:f7c60d3e7b8a 259 }
maclobdell 0:f7c60d3e7b8a 260
maclobdell 0:f7c60d3e7b8a 261 static const unsigned char md4_padding[64] =
maclobdell 0:f7c60d3e7b8a 262 {
maclobdell 0:f7c60d3e7b8a 263 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
maclobdell 0:f7c60d3e7b8a 264 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
maclobdell 0:f7c60d3e7b8a 265 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
maclobdell 0:f7c60d3e7b8a 266 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
maclobdell 0:f7c60d3e7b8a 267 };
maclobdell 0:f7c60d3e7b8a 268
maclobdell 0:f7c60d3e7b8a 269 /*
maclobdell 0:f7c60d3e7b8a 270 * MD4 final digest
maclobdell 0:f7c60d3e7b8a 271 */
maclobdell 0:f7c60d3e7b8a 272 void mbedtls_md4_finish( mbedtls_md4_context *ctx, unsigned char output[16] )
maclobdell 0:f7c60d3e7b8a 273 {
maclobdell 0:f7c60d3e7b8a 274 uint32_t last, padn;
maclobdell 0:f7c60d3e7b8a 275 uint32_t high, low;
maclobdell 0:f7c60d3e7b8a 276 unsigned char msglen[8];
maclobdell 0:f7c60d3e7b8a 277
maclobdell 0:f7c60d3e7b8a 278 high = ( ctx->total[0] >> 29 )
maclobdell 0:f7c60d3e7b8a 279 | ( ctx->total[1] << 3 );
maclobdell 0:f7c60d3e7b8a 280 low = ( ctx->total[0] << 3 );
maclobdell 0:f7c60d3e7b8a 281
maclobdell 0:f7c60d3e7b8a 282 PUT_UINT32_LE( low, msglen, 0 );
maclobdell 0:f7c60d3e7b8a 283 PUT_UINT32_LE( high, msglen, 4 );
maclobdell 0:f7c60d3e7b8a 284
maclobdell 0:f7c60d3e7b8a 285 last = ctx->total[0] & 0x3F;
maclobdell 0:f7c60d3e7b8a 286 padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
maclobdell 0:f7c60d3e7b8a 287
maclobdell 0:f7c60d3e7b8a 288 mbedtls_md4_update( ctx, (unsigned char *) md4_padding, padn );
maclobdell 0:f7c60d3e7b8a 289 mbedtls_md4_update( ctx, msglen, 8 );
maclobdell 0:f7c60d3e7b8a 290
maclobdell 0:f7c60d3e7b8a 291 PUT_UINT32_LE( ctx->state[0], output, 0 );
maclobdell 0:f7c60d3e7b8a 292 PUT_UINT32_LE( ctx->state[1], output, 4 );
maclobdell 0:f7c60d3e7b8a 293 PUT_UINT32_LE( ctx->state[2], output, 8 );
maclobdell 0:f7c60d3e7b8a 294 PUT_UINT32_LE( ctx->state[3], output, 12 );
maclobdell 0:f7c60d3e7b8a 295 }
maclobdell 0:f7c60d3e7b8a 296
maclobdell 0:f7c60d3e7b8a 297 #endif /* !MBEDTLS_MD4_ALT */
maclobdell 0:f7c60d3e7b8a 298
maclobdell 0:f7c60d3e7b8a 299 /*
maclobdell 0:f7c60d3e7b8a 300 * output = MD4( input buffer )
maclobdell 0:f7c60d3e7b8a 301 */
maclobdell 0:f7c60d3e7b8a 302 void mbedtls_md4( const unsigned char *input, size_t ilen, unsigned char output[16] )
maclobdell 0:f7c60d3e7b8a 303 {
maclobdell 0:f7c60d3e7b8a 304 mbedtls_md4_context ctx;
maclobdell 0:f7c60d3e7b8a 305
maclobdell 0:f7c60d3e7b8a 306 mbedtls_md4_init( &ctx );
maclobdell 0:f7c60d3e7b8a 307 mbedtls_md4_starts( &ctx );
maclobdell 0:f7c60d3e7b8a 308 mbedtls_md4_update( &ctx, input, ilen );
maclobdell 0:f7c60d3e7b8a 309 mbedtls_md4_finish( &ctx, output );
maclobdell 0:f7c60d3e7b8a 310 mbedtls_md4_free( &ctx );
maclobdell 0:f7c60d3e7b8a 311 }
maclobdell 0:f7c60d3e7b8a 312
maclobdell 0:f7c60d3e7b8a 313 #if defined(MBEDTLS_SELF_TEST)
maclobdell 0:f7c60d3e7b8a 314
maclobdell 0:f7c60d3e7b8a 315 /*
maclobdell 0:f7c60d3e7b8a 316 * RFC 1320 test vectors
maclobdell 0:f7c60d3e7b8a 317 */
maclobdell 0:f7c60d3e7b8a 318 static const char md4_test_str[7][81] =
maclobdell 0:f7c60d3e7b8a 319 {
maclobdell 0:f7c60d3e7b8a 320 { "" },
maclobdell 0:f7c60d3e7b8a 321 { "a" },
maclobdell 0:f7c60d3e7b8a 322 { "abc" },
maclobdell 0:f7c60d3e7b8a 323 { "message digest" },
maclobdell 0:f7c60d3e7b8a 324 { "abcdefghijklmnopqrstuvwxyz" },
maclobdell 0:f7c60d3e7b8a 325 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
maclobdell 0:f7c60d3e7b8a 326 { "12345678901234567890123456789012345678901234567890123456789012" \
maclobdell 0:f7c60d3e7b8a 327 "345678901234567890" }
maclobdell 0:f7c60d3e7b8a 328 };
maclobdell 0:f7c60d3e7b8a 329
maclobdell 0:f7c60d3e7b8a 330 static const unsigned char md4_test_sum[7][16] =
maclobdell 0:f7c60d3e7b8a 331 {
maclobdell 0:f7c60d3e7b8a 332 { 0x31, 0xD6, 0xCF, 0xE0, 0xD1, 0x6A, 0xE9, 0x31,
maclobdell 0:f7c60d3e7b8a 333 0xB7, 0x3C, 0x59, 0xD7, 0xE0, 0xC0, 0x89, 0xC0 },
maclobdell 0:f7c60d3e7b8a 334 { 0xBD, 0xE5, 0x2C, 0xB3, 0x1D, 0xE3, 0x3E, 0x46,
maclobdell 0:f7c60d3e7b8a 335 0x24, 0x5E, 0x05, 0xFB, 0xDB, 0xD6, 0xFB, 0x24 },
maclobdell 0:f7c60d3e7b8a 336 { 0xA4, 0x48, 0x01, 0x7A, 0xAF, 0x21, 0xD8, 0x52,
maclobdell 0:f7c60d3e7b8a 337 0x5F, 0xC1, 0x0A, 0xE8, 0x7A, 0xA6, 0x72, 0x9D },
maclobdell 0:f7c60d3e7b8a 338 { 0xD9, 0x13, 0x0A, 0x81, 0x64, 0x54, 0x9F, 0xE8,
maclobdell 0:f7c60d3e7b8a 339 0x18, 0x87, 0x48, 0x06, 0xE1, 0xC7, 0x01, 0x4B },
maclobdell 0:f7c60d3e7b8a 340 { 0xD7, 0x9E, 0x1C, 0x30, 0x8A, 0xA5, 0xBB, 0xCD,
maclobdell 0:f7c60d3e7b8a 341 0xEE, 0xA8, 0xED, 0x63, 0xDF, 0x41, 0x2D, 0xA9 },
maclobdell 0:f7c60d3e7b8a 342 { 0x04, 0x3F, 0x85, 0x82, 0xF2, 0x41, 0xDB, 0x35,
maclobdell 0:f7c60d3e7b8a 343 0x1C, 0xE6, 0x27, 0xE1, 0x53, 0xE7, 0xF0, 0xE4 },
maclobdell 0:f7c60d3e7b8a 344 { 0xE3, 0x3B, 0x4D, 0xDC, 0x9C, 0x38, 0xF2, 0x19,
maclobdell 0:f7c60d3e7b8a 345 0x9C, 0x3E, 0x7B, 0x16, 0x4F, 0xCC, 0x05, 0x36 }
maclobdell 0:f7c60d3e7b8a 346 };
maclobdell 0:f7c60d3e7b8a 347
maclobdell 0:f7c60d3e7b8a 348 /*
maclobdell 0:f7c60d3e7b8a 349 * Checkup routine
maclobdell 0:f7c60d3e7b8a 350 */
maclobdell 0:f7c60d3e7b8a 351 int mbedtls_md4_self_test( int verbose )
maclobdell 0:f7c60d3e7b8a 352 {
maclobdell 0:f7c60d3e7b8a 353 int i;
maclobdell 0:f7c60d3e7b8a 354 unsigned char md4sum[16];
maclobdell 0:f7c60d3e7b8a 355
maclobdell 0:f7c60d3e7b8a 356 for( i = 0; i < 7; i++ )
maclobdell 0:f7c60d3e7b8a 357 {
maclobdell 0:f7c60d3e7b8a 358 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 359 mbedtls_printf( " MD4 test #%d: ", i + 1 );
maclobdell 0:f7c60d3e7b8a 360
maclobdell 0:f7c60d3e7b8a 361 mbedtls_md4( (unsigned char *) md4_test_str[i],
maclobdell 0:f7c60d3e7b8a 362 strlen( md4_test_str[i] ), md4sum );
maclobdell 0:f7c60d3e7b8a 363
maclobdell 0:f7c60d3e7b8a 364 if( memcmp( md4sum, md4_test_sum[i], 16 ) != 0 )
maclobdell 0:f7c60d3e7b8a 365 {
maclobdell 0:f7c60d3e7b8a 366 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 367 mbedtls_printf( "failed\n" );
maclobdell 0:f7c60d3e7b8a 368
maclobdell 0:f7c60d3e7b8a 369 return( 1 );
maclobdell 0:f7c60d3e7b8a 370 }
maclobdell 0:f7c60d3e7b8a 371
maclobdell 0:f7c60d3e7b8a 372 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 373 mbedtls_printf( "passed\n" );
maclobdell 0:f7c60d3e7b8a 374 }
maclobdell 0:f7c60d3e7b8a 375
maclobdell 0:f7c60d3e7b8a 376 if( verbose != 0 )
maclobdell 0:f7c60d3e7b8a 377 mbedtls_printf( "\n" );
maclobdell 0:f7c60d3e7b8a 378
maclobdell 0:f7c60d3e7b8a 379 return( 0 );
maclobdell 0:f7c60d3e7b8a 380 }
maclobdell 0:f7c60d3e7b8a 381
maclobdell 0:f7c60d3e7b8a 382 #endif /* MBEDTLS_SELF_TEST */
maclobdell 0:f7c60d3e7b8a 383
maclobdell 0:f7c60d3e7b8a 384 #endif /* MBEDTLS_MD4_C */