Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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