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