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