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