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