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