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