The WDCInterface is is a drop-in replacement for an EthernetInterface class that allows the user to connect to the Internet with a Wistron NeWeb Corporation (WNC) M14A2A Series data module using the standard network Socket API's. This interface class is used in the AT&T Cellular IoT Starter Kit which is sold by Avnet (http://cloudconnectkits.org/product/att-cellular-iot-starter-kit).
Dependencies: WncControllerK64F
Dependents: WNCProximityMqtt Pubnub_ATT_IoT_SK_WNC_sync BluemixDemo BluemixQS ... more
See the WNCInterface README in the Wiki tab for detailed information on this library.
mbedtls/source/md5.c@29:b278b745fb4f, 2017-03-24 (annotated)
- Committer:
- JMF
- Date:
- Fri Mar 24 22:26:23 2017 +0000
- Revision:
- 29:b278b745fb4f
- Parent:
- 12:0071cb144c7a
updated Class name of TCPSocketConnection to WncTCPSocketConnection;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 12:0071cb144c7a | 1 | /* |
JMF | 12:0071cb144c7a | 2 | * RFC 1321 compliant MD5 implementation |
JMF | 12:0071cb144c7a | 3 | * |
JMF | 12:0071cb144c7a | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
JMF | 12:0071cb144c7a | 5 | * SPDX-License-Identifier: Apache-2.0 |
JMF | 12:0071cb144c7a | 6 | * |
JMF | 12:0071cb144c7a | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
JMF | 12:0071cb144c7a | 8 | * not use this file except in compliance with the License. |
JMF | 12:0071cb144c7a | 9 | * You may obtain a copy of the License at |
JMF | 12:0071cb144c7a | 10 | * |
JMF | 12:0071cb144c7a | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
JMF | 12:0071cb144c7a | 12 | * |
JMF | 12:0071cb144c7a | 13 | * Unless required by applicable law or agreed to in writing, software |
JMF | 12:0071cb144c7a | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
JMF | 12:0071cb144c7a | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
JMF | 12:0071cb144c7a | 16 | * See the License for the specific language governing permissions and |
JMF | 12:0071cb144c7a | 17 | * limitations under the License. |
JMF | 12:0071cb144c7a | 18 | * |
JMF | 12:0071cb144c7a | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
JMF | 12:0071cb144c7a | 20 | */ |
JMF | 12:0071cb144c7a | 21 | /* |
JMF | 12:0071cb144c7a | 22 | * The MD5 algorithm was designed by Ron Rivest in 1991. |
JMF | 12:0071cb144c7a | 23 | * |
JMF | 12:0071cb144c7a | 24 | * http://www.ietf.org/rfc/rfc1321.txt |
JMF | 12:0071cb144c7a | 25 | */ |
JMF | 12:0071cb144c7a | 26 | |
JMF | 12:0071cb144c7a | 27 | #if !defined(MBEDTLS_CONFIG_FILE) |
JMF | 12:0071cb144c7a | 28 | #include "mbedtls/config.h" |
JMF | 12:0071cb144c7a | 29 | #else |
JMF | 12:0071cb144c7a | 30 | #include MBEDTLS_CONFIG_FILE |
JMF | 12:0071cb144c7a | 31 | #endif |
JMF | 12:0071cb144c7a | 32 | |
JMF | 12:0071cb144c7a | 33 | #if defined(MBEDTLS_MD5_C) |
JMF | 12:0071cb144c7a | 34 | |
JMF | 12:0071cb144c7a | 35 | #include "mbedtls/md5.h" |
JMF | 12:0071cb144c7a | 36 | |
JMF | 12:0071cb144c7a | 37 | #include <string.h> |
JMF | 12:0071cb144c7a | 38 | |
JMF | 12:0071cb144c7a | 39 | #if defined(MBEDTLS_SELF_TEST) |
JMF | 12:0071cb144c7a | 40 | #if defined(MBEDTLS_PLATFORM_C) |
JMF | 12:0071cb144c7a | 41 | #include "mbedtls/platform.h" |
JMF | 12:0071cb144c7a | 42 | #else |
JMF | 12:0071cb144c7a | 43 | #include <stdio.h> |
JMF | 12:0071cb144c7a | 44 | #define mbedtls_printf pc.printf |
JMF | 12:0071cb144c7a | 45 | #endif /* MBEDTLS_PLATFORM_C */ |
JMF | 12:0071cb144c7a | 46 | #endif /* MBEDTLS_SELF_TEST */ |
JMF | 12:0071cb144c7a | 47 | |
JMF | 12:0071cb144c7a | 48 | #if !defined(MBEDTLS_MD5_ALT) |
JMF | 12:0071cb144c7a | 49 | |
JMF | 12:0071cb144c7a | 50 | /* Implementation that should never be optimized out by the compiler */ |
JMF | 12:0071cb144c7a | 51 | static void mbedtls_zeroize( void *v, size_t n ) { |
JMF | 12:0071cb144c7a | 52 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
JMF | 12:0071cb144c7a | 53 | } |
JMF | 12:0071cb144c7a | 54 | |
JMF | 12:0071cb144c7a | 55 | /* |
JMF | 12:0071cb144c7a | 56 | * 32-bit integer manipulation macros (little endian) |
JMF | 12:0071cb144c7a | 57 | */ |
JMF | 12:0071cb144c7a | 58 | #ifndef GET_UINT32_LE |
JMF | 12:0071cb144c7a | 59 | #define GET_UINT32_LE(n,b,i) \ |
JMF | 12:0071cb144c7a | 60 | { \ |
JMF | 12:0071cb144c7a | 61 | (n) = ( (uint32_t) (b)[(i) ] ) \ |
JMF | 12:0071cb144c7a | 62 | | ( (uint32_t) (b)[(i) + 1] << 8 ) \ |
JMF | 12:0071cb144c7a | 63 | | ( (uint32_t) (b)[(i) + 2] << 16 ) \ |
JMF | 12:0071cb144c7a | 64 | | ( (uint32_t) (b)[(i) + 3] << 24 ); \ |
JMF | 12:0071cb144c7a | 65 | } |
JMF | 12:0071cb144c7a | 66 | #endif |
JMF | 12:0071cb144c7a | 67 | |
JMF | 12:0071cb144c7a | 68 | #ifndef PUT_UINT32_LE |
JMF | 12:0071cb144c7a | 69 | #define PUT_UINT32_LE(n,b,i) \ |
JMF | 12:0071cb144c7a | 70 | { \ |
JMF | 12:0071cb144c7a | 71 | (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ |
JMF | 12:0071cb144c7a | 72 | (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ |
JMF | 12:0071cb144c7a | 73 | (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ |
JMF | 12:0071cb144c7a | 74 | (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ |
JMF | 12:0071cb144c7a | 75 | } |
JMF | 12:0071cb144c7a | 76 | #endif |
JMF | 12:0071cb144c7a | 77 | |
JMF | 12:0071cb144c7a | 78 | void mbedtls_md5_init( mbedtls_md5_context *ctx ) |
JMF | 12:0071cb144c7a | 79 | { |
JMF | 12:0071cb144c7a | 80 | memset( ctx, 0, sizeof( mbedtls_md5_context ) ); |
JMF | 12:0071cb144c7a | 81 | } |
JMF | 12:0071cb144c7a | 82 | |
JMF | 12:0071cb144c7a | 83 | void mbedtls_md5_free( mbedtls_md5_context *ctx ) |
JMF | 12:0071cb144c7a | 84 | { |
JMF | 12:0071cb144c7a | 85 | if( ctx == NULL ) |
JMF | 12:0071cb144c7a | 86 | return; |
JMF | 12:0071cb144c7a | 87 | |
JMF | 12:0071cb144c7a | 88 | mbedtls_zeroize( ctx, sizeof( mbedtls_md5_context ) ); |
JMF | 12:0071cb144c7a | 89 | } |
JMF | 12:0071cb144c7a | 90 | |
JMF | 12:0071cb144c7a | 91 | void mbedtls_md5_clone( mbedtls_md5_context *dst, |
JMF | 12:0071cb144c7a | 92 | const mbedtls_md5_context *src ) |
JMF | 12:0071cb144c7a | 93 | { |
JMF | 12:0071cb144c7a | 94 | *dst = *src; |
JMF | 12:0071cb144c7a | 95 | } |
JMF | 12:0071cb144c7a | 96 | |
JMF | 12:0071cb144c7a | 97 | /* |
JMF | 12:0071cb144c7a | 98 | * MD5 context setup |
JMF | 12:0071cb144c7a | 99 | */ |
JMF | 12:0071cb144c7a | 100 | void mbedtls_md5_starts( mbedtls_md5_context *ctx ) |
JMF | 12:0071cb144c7a | 101 | { |
JMF | 12:0071cb144c7a | 102 | ctx->total[0] = 0; |
JMF | 12:0071cb144c7a | 103 | ctx->total[1] = 0; |
JMF | 12:0071cb144c7a | 104 | |
JMF | 12:0071cb144c7a | 105 | ctx->state[0] = 0x67452301; |
JMF | 12:0071cb144c7a | 106 | ctx->state[1] = 0xEFCDAB89; |
JMF | 12:0071cb144c7a | 107 | ctx->state[2] = 0x98BADCFE; |
JMF | 12:0071cb144c7a | 108 | ctx->state[3] = 0x10325476; |
JMF | 12:0071cb144c7a | 109 | } |
JMF | 12:0071cb144c7a | 110 | |
JMF | 12:0071cb144c7a | 111 | #if !defined(MBEDTLS_MD5_PROCESS_ALT) |
JMF | 12:0071cb144c7a | 112 | void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[64] ) |
JMF | 12:0071cb144c7a | 113 | { |
JMF | 12:0071cb144c7a | 114 | uint32_t X[16], A, B, C, D; |
JMF | 12:0071cb144c7a | 115 | |
JMF | 12:0071cb144c7a | 116 | GET_UINT32_LE( X[ 0], data, 0 ); |
JMF | 12:0071cb144c7a | 117 | GET_UINT32_LE( X[ 1], data, 4 ); |
JMF | 12:0071cb144c7a | 118 | GET_UINT32_LE( X[ 2], data, 8 ); |
JMF | 12:0071cb144c7a | 119 | GET_UINT32_LE( X[ 3], data, 12 ); |
JMF | 12:0071cb144c7a | 120 | GET_UINT32_LE( X[ 4], data, 16 ); |
JMF | 12:0071cb144c7a | 121 | GET_UINT32_LE( X[ 5], data, 20 ); |
JMF | 12:0071cb144c7a | 122 | GET_UINT32_LE( X[ 6], data, 24 ); |
JMF | 12:0071cb144c7a | 123 | GET_UINT32_LE( X[ 7], data, 28 ); |
JMF | 12:0071cb144c7a | 124 | GET_UINT32_LE( X[ 8], data, 32 ); |
JMF | 12:0071cb144c7a | 125 | GET_UINT32_LE( X[ 9], data, 36 ); |
JMF | 12:0071cb144c7a | 126 | GET_UINT32_LE( X[10], data, 40 ); |
JMF | 12:0071cb144c7a | 127 | GET_UINT32_LE( X[11], data, 44 ); |
JMF | 12:0071cb144c7a | 128 | GET_UINT32_LE( X[12], data, 48 ); |
JMF | 12:0071cb144c7a | 129 | GET_UINT32_LE( X[13], data, 52 ); |
JMF | 12:0071cb144c7a | 130 | GET_UINT32_LE( X[14], data, 56 ); |
JMF | 12:0071cb144c7a | 131 | GET_UINT32_LE( X[15], data, 60 ); |
JMF | 12:0071cb144c7a | 132 | |
JMF | 12:0071cb144c7a | 133 | #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) |
JMF | 12:0071cb144c7a | 134 | |
JMF | 12:0071cb144c7a | 135 | #define P(a,b,c,d,k,s,t) \ |
JMF | 12:0071cb144c7a | 136 | { \ |
JMF | 12:0071cb144c7a | 137 | a += F(b,c,d) + X[k] + t; a = S(a,s) + b; \ |
JMF | 12:0071cb144c7a | 138 | } |
JMF | 12:0071cb144c7a | 139 | |
JMF | 12:0071cb144c7a | 140 | A = ctx->state[0]; |
JMF | 12:0071cb144c7a | 141 | B = ctx->state[1]; |
JMF | 12:0071cb144c7a | 142 | C = ctx->state[2]; |
JMF | 12:0071cb144c7a | 143 | D = ctx->state[3]; |
JMF | 12:0071cb144c7a | 144 | |
JMF | 12:0071cb144c7a | 145 | #define F(x,y,z) (z ^ (x & (y ^ z))) |
JMF | 12:0071cb144c7a | 146 | |
JMF | 12:0071cb144c7a | 147 | P( A, B, C, D, 0, 7, 0xD76AA478 ); |
JMF | 12:0071cb144c7a | 148 | P( D, A, B, C, 1, 12, 0xE8C7B756 ); |
JMF | 12:0071cb144c7a | 149 | P( C, D, A, B, 2, 17, 0x242070DB ); |
JMF | 12:0071cb144c7a | 150 | P( B, C, D, A, 3, 22, 0xC1BDCEEE ); |
JMF | 12:0071cb144c7a | 151 | P( A, B, C, D, 4, 7, 0xF57C0FAF ); |
JMF | 12:0071cb144c7a | 152 | P( D, A, B, C, 5, 12, 0x4787C62A ); |
JMF | 12:0071cb144c7a | 153 | P( C, D, A, B, 6, 17, 0xA8304613 ); |
JMF | 12:0071cb144c7a | 154 | P( B, C, D, A, 7, 22, 0xFD469501 ); |
JMF | 12:0071cb144c7a | 155 | P( A, B, C, D, 8, 7, 0x698098D8 ); |
JMF | 12:0071cb144c7a | 156 | P( D, A, B, C, 9, 12, 0x8B44F7AF ); |
JMF | 12:0071cb144c7a | 157 | P( C, D, A, B, 10, 17, 0xFFFF5BB1 ); |
JMF | 12:0071cb144c7a | 158 | P( B, C, D, A, 11, 22, 0x895CD7BE ); |
JMF | 12:0071cb144c7a | 159 | P( A, B, C, D, 12, 7, 0x6B901122 ); |
JMF | 12:0071cb144c7a | 160 | P( D, A, B, C, 13, 12, 0xFD987193 ); |
JMF | 12:0071cb144c7a | 161 | P( C, D, A, B, 14, 17, 0xA679438E ); |
JMF | 12:0071cb144c7a | 162 | P( B, C, D, A, 15, 22, 0x49B40821 ); |
JMF | 12:0071cb144c7a | 163 | |
JMF | 12:0071cb144c7a | 164 | #undef F |
JMF | 12:0071cb144c7a | 165 | |
JMF | 12:0071cb144c7a | 166 | #define F(x,y,z) (y ^ (z & (x ^ y))) |
JMF | 12:0071cb144c7a | 167 | |
JMF | 12:0071cb144c7a | 168 | P( A, B, C, D, 1, 5, 0xF61E2562 ); |
JMF | 12:0071cb144c7a | 169 | P( D, A, B, C, 6, 9, 0xC040B340 ); |
JMF | 12:0071cb144c7a | 170 | P( C, D, A, B, 11, 14, 0x265E5A51 ); |
JMF | 12:0071cb144c7a | 171 | P( B, C, D, A, 0, 20, 0xE9B6C7AA ); |
JMF | 12:0071cb144c7a | 172 | P( A, B, C, D, 5, 5, 0xD62F105D ); |
JMF | 12:0071cb144c7a | 173 | P( D, A, B, C, 10, 9, 0x02441453 ); |
JMF | 12:0071cb144c7a | 174 | P( C, D, A, B, 15, 14, 0xD8A1E681 ); |
JMF | 12:0071cb144c7a | 175 | P( B, C, D, A, 4, 20, 0xE7D3FBC8 ); |
JMF | 12:0071cb144c7a | 176 | P( A, B, C, D, 9, 5, 0x21E1CDE6 ); |
JMF | 12:0071cb144c7a | 177 | P( D, A, B, C, 14, 9, 0xC33707D6 ); |
JMF | 12:0071cb144c7a | 178 | P( C, D, A, B, 3, 14, 0xF4D50D87 ); |
JMF | 12:0071cb144c7a | 179 | P( B, C, D, A, 8, 20, 0x455A14ED ); |
JMF | 12:0071cb144c7a | 180 | P( A, B, C, D, 13, 5, 0xA9E3E905 ); |
JMF | 12:0071cb144c7a | 181 | P( D, A, B, C, 2, 9, 0xFCEFA3F8 ); |
JMF | 12:0071cb144c7a | 182 | P( C, D, A, B, 7, 14, 0x676F02D9 ); |
JMF | 12:0071cb144c7a | 183 | P( B, C, D, A, 12, 20, 0x8D2A4C8A ); |
JMF | 12:0071cb144c7a | 184 | |
JMF | 12:0071cb144c7a | 185 | #undef F |
JMF | 12:0071cb144c7a | 186 | |
JMF | 12:0071cb144c7a | 187 | #define F(x,y,z) (x ^ y ^ z) |
JMF | 12:0071cb144c7a | 188 | |
JMF | 12:0071cb144c7a | 189 | P( A, B, C, D, 5, 4, 0xFFFA3942 ); |
JMF | 12:0071cb144c7a | 190 | P( D, A, B, C, 8, 11, 0x8771F681 ); |
JMF | 12:0071cb144c7a | 191 | P( C, D, A, B, 11, 16, 0x6D9D6122 ); |
JMF | 12:0071cb144c7a | 192 | P( B, C, D, A, 14, 23, 0xFDE5380C ); |
JMF | 12:0071cb144c7a | 193 | P( A, B, C, D, 1, 4, 0xA4BEEA44 ); |
JMF | 12:0071cb144c7a | 194 | P( D, A, B, C, 4, 11, 0x4BDECFA9 ); |
JMF | 12:0071cb144c7a | 195 | P( C, D, A, B, 7, 16, 0xF6BB4B60 ); |
JMF | 12:0071cb144c7a | 196 | P( B, C, D, A, 10, 23, 0xBEBFBC70 ); |
JMF | 12:0071cb144c7a | 197 | P( A, B, C, D, 13, 4, 0x289B7EC6 ); |
JMF | 12:0071cb144c7a | 198 | P( D, A, B, C, 0, 11, 0xEAA127FA ); |
JMF | 12:0071cb144c7a | 199 | P( C, D, A, B, 3, 16, 0xD4EF3085 ); |
JMF | 12:0071cb144c7a | 200 | P( B, C, D, A, 6, 23, 0x04881D05 ); |
JMF | 12:0071cb144c7a | 201 | P( A, B, C, D, 9, 4, 0xD9D4D039 ); |
JMF | 12:0071cb144c7a | 202 | P( D, A, B, C, 12, 11, 0xE6DB99E5 ); |
JMF | 12:0071cb144c7a | 203 | P( C, D, A, B, 15, 16, 0x1FA27CF8 ); |
JMF | 12:0071cb144c7a | 204 | P( B, C, D, A, 2, 23, 0xC4AC5665 ); |
JMF | 12:0071cb144c7a | 205 | |
JMF | 12:0071cb144c7a | 206 | #undef F |
JMF | 12:0071cb144c7a | 207 | |
JMF | 12:0071cb144c7a | 208 | #define F(x,y,z) (y ^ (x | ~z)) |
JMF | 12:0071cb144c7a | 209 | |
JMF | 12:0071cb144c7a | 210 | P( A, B, C, D, 0, 6, 0xF4292244 ); |
JMF | 12:0071cb144c7a | 211 | P( D, A, B, C, 7, 10, 0x432AFF97 ); |
JMF | 12:0071cb144c7a | 212 | P( C, D, A, B, 14, 15, 0xAB9423A7 ); |
JMF | 12:0071cb144c7a | 213 | P( B, C, D, A, 5, 21, 0xFC93A039 ); |
JMF | 12:0071cb144c7a | 214 | P( A, B, C, D, 12, 6, 0x655B59C3 ); |
JMF | 12:0071cb144c7a | 215 | P( D, A, B, C, 3, 10, 0x8F0CCC92 ); |
JMF | 12:0071cb144c7a | 216 | P( C, D, A, B, 10, 15, 0xFFEFF47D ); |
JMF | 12:0071cb144c7a | 217 | P( B, C, D, A, 1, 21, 0x85845DD1 ); |
JMF | 12:0071cb144c7a | 218 | P( A, B, C, D, 8, 6, 0x6FA87E4F ); |
JMF | 12:0071cb144c7a | 219 | P( D, A, B, C, 15, 10, 0xFE2CE6E0 ); |
JMF | 12:0071cb144c7a | 220 | P( C, D, A, B, 6, 15, 0xA3014314 ); |
JMF | 12:0071cb144c7a | 221 | P( B, C, D, A, 13, 21, 0x4E0811A1 ); |
JMF | 12:0071cb144c7a | 222 | P( A, B, C, D, 4, 6, 0xF7537E82 ); |
JMF | 12:0071cb144c7a | 223 | P( D, A, B, C, 11, 10, 0xBD3AF235 ); |
JMF | 12:0071cb144c7a | 224 | P( C, D, A, B, 2, 15, 0x2AD7D2BB ); |
JMF | 12:0071cb144c7a | 225 | P( B, C, D, A, 9, 21, 0xEB86D391 ); |
JMF | 12:0071cb144c7a | 226 | |
JMF | 12:0071cb144c7a | 227 | #undef F |
JMF | 12:0071cb144c7a | 228 | |
JMF | 12:0071cb144c7a | 229 | ctx->state[0] += A; |
JMF | 12:0071cb144c7a | 230 | ctx->state[1] += B; |
JMF | 12:0071cb144c7a | 231 | ctx->state[2] += C; |
JMF | 12:0071cb144c7a | 232 | ctx->state[3] += D; |
JMF | 12:0071cb144c7a | 233 | } |
JMF | 12:0071cb144c7a | 234 | #endif /* !MBEDTLS_MD5_PROCESS_ALT */ |
JMF | 12:0071cb144c7a | 235 | |
JMF | 12:0071cb144c7a | 236 | /* |
JMF | 12:0071cb144c7a | 237 | * MD5 process buffer |
JMF | 12:0071cb144c7a | 238 | */ |
JMF | 12:0071cb144c7a | 239 | void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen ) |
JMF | 12:0071cb144c7a | 240 | { |
JMF | 12:0071cb144c7a | 241 | size_t fill; |
JMF | 12:0071cb144c7a | 242 | uint32_t left; |
JMF | 12:0071cb144c7a | 243 | |
JMF | 12:0071cb144c7a | 244 | if( ilen == 0 ) |
JMF | 12:0071cb144c7a | 245 | return; |
JMF | 12:0071cb144c7a | 246 | |
JMF | 12:0071cb144c7a | 247 | left = ctx->total[0] & 0x3F; |
JMF | 12:0071cb144c7a | 248 | fill = 64 - left; |
JMF | 12:0071cb144c7a | 249 | |
JMF | 12:0071cb144c7a | 250 | ctx->total[0] += (uint32_t) ilen; |
JMF | 12:0071cb144c7a | 251 | ctx->total[0] &= 0xFFFFFFFF; |
JMF | 12:0071cb144c7a | 252 | |
JMF | 12:0071cb144c7a | 253 | if( ctx->total[0] < (uint32_t) ilen ) |
JMF | 12:0071cb144c7a | 254 | ctx->total[1]++; |
JMF | 12:0071cb144c7a | 255 | |
JMF | 12:0071cb144c7a | 256 | if( left && ilen >= fill ) |
JMF | 12:0071cb144c7a | 257 | { |
JMF | 12:0071cb144c7a | 258 | memcpy( (void *) (ctx->buffer + left), input, fill ); |
JMF | 12:0071cb144c7a | 259 | mbedtls_md5_process( ctx, ctx->buffer ); |
JMF | 12:0071cb144c7a | 260 | input += fill; |
JMF | 12:0071cb144c7a | 261 | ilen -= fill; |
JMF | 12:0071cb144c7a | 262 | left = 0; |
JMF | 12:0071cb144c7a | 263 | } |
JMF | 12:0071cb144c7a | 264 | |
JMF | 12:0071cb144c7a | 265 | while( ilen >= 64 ) |
JMF | 12:0071cb144c7a | 266 | { |
JMF | 12:0071cb144c7a | 267 | mbedtls_md5_process( ctx, input ); |
JMF | 12:0071cb144c7a | 268 | input += 64; |
JMF | 12:0071cb144c7a | 269 | ilen -= 64; |
JMF | 12:0071cb144c7a | 270 | } |
JMF | 12:0071cb144c7a | 271 | |
JMF | 12:0071cb144c7a | 272 | if( ilen > 0 ) |
JMF | 12:0071cb144c7a | 273 | { |
JMF | 12:0071cb144c7a | 274 | memcpy( (void *) (ctx->buffer + left), input, ilen ); |
JMF | 12:0071cb144c7a | 275 | } |
JMF | 12:0071cb144c7a | 276 | } |
JMF | 12:0071cb144c7a | 277 | |
JMF | 12:0071cb144c7a | 278 | static const unsigned char md5_padding[64] = |
JMF | 12:0071cb144c7a | 279 | { |
JMF | 12:0071cb144c7a | 280 | 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
JMF | 12:0071cb144c7a | 281 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
JMF | 12:0071cb144c7a | 282 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
JMF | 12:0071cb144c7a | 283 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
JMF | 12:0071cb144c7a | 284 | }; |
JMF | 12:0071cb144c7a | 285 | |
JMF | 12:0071cb144c7a | 286 | /* |
JMF | 12:0071cb144c7a | 287 | * MD5 final digest |
JMF | 12:0071cb144c7a | 288 | */ |
JMF | 12:0071cb144c7a | 289 | void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] ) |
JMF | 12:0071cb144c7a | 290 | { |
JMF | 12:0071cb144c7a | 291 | uint32_t last, padn; |
JMF | 12:0071cb144c7a | 292 | uint32_t high, low; |
JMF | 12:0071cb144c7a | 293 | unsigned char msglen[8]; |
JMF | 12:0071cb144c7a | 294 | |
JMF | 12:0071cb144c7a | 295 | high = ( ctx->total[0] >> 29 ) |
JMF | 12:0071cb144c7a | 296 | | ( ctx->total[1] << 3 ); |
JMF | 12:0071cb144c7a | 297 | low = ( ctx->total[0] << 3 ); |
JMF | 12:0071cb144c7a | 298 | |
JMF | 12:0071cb144c7a | 299 | PUT_UINT32_LE( low, msglen, 0 ); |
JMF | 12:0071cb144c7a | 300 | PUT_UINT32_LE( high, msglen, 4 ); |
JMF | 12:0071cb144c7a | 301 | |
JMF | 12:0071cb144c7a | 302 | last = ctx->total[0] & 0x3F; |
JMF | 12:0071cb144c7a | 303 | padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); |
JMF | 12:0071cb144c7a | 304 | |
JMF | 12:0071cb144c7a | 305 | mbedtls_md5_update( ctx, md5_padding, padn ); |
JMF | 12:0071cb144c7a | 306 | mbedtls_md5_update( ctx, msglen, 8 ); |
JMF | 12:0071cb144c7a | 307 | |
JMF | 12:0071cb144c7a | 308 | PUT_UINT32_LE( ctx->state[0], output, 0 ); |
JMF | 12:0071cb144c7a | 309 | PUT_UINT32_LE( ctx->state[1], output, 4 ); |
JMF | 12:0071cb144c7a | 310 | PUT_UINT32_LE( ctx->state[2], output, 8 ); |
JMF | 12:0071cb144c7a | 311 | PUT_UINT32_LE( ctx->state[3], output, 12 ); |
JMF | 12:0071cb144c7a | 312 | } |
JMF | 12:0071cb144c7a | 313 | |
JMF | 12:0071cb144c7a | 314 | #endif /* !MBEDTLS_MD5_ALT */ |
JMF | 12:0071cb144c7a | 315 | |
JMF | 12:0071cb144c7a | 316 | /* |
JMF | 12:0071cb144c7a | 317 | * output = MD5( input buffer ) |
JMF | 12:0071cb144c7a | 318 | */ |
JMF | 12:0071cb144c7a | 319 | void mbedtls_md5( const unsigned char *input, size_t ilen, unsigned char output[16] ) |
JMF | 12:0071cb144c7a | 320 | { |
JMF | 12:0071cb144c7a | 321 | mbedtls_md5_context ctx; |
JMF | 12:0071cb144c7a | 322 | |
JMF | 12:0071cb144c7a | 323 | mbedtls_md5_init( &ctx ); |
JMF | 12:0071cb144c7a | 324 | mbedtls_md5_starts( &ctx ); |
JMF | 12:0071cb144c7a | 325 | mbedtls_md5_update( &ctx, input, ilen ); |
JMF | 12:0071cb144c7a | 326 | mbedtls_md5_finish( &ctx, output ); |
JMF | 12:0071cb144c7a | 327 | mbedtls_md5_free( &ctx ); |
JMF | 12:0071cb144c7a | 328 | } |
JMF | 12:0071cb144c7a | 329 | |
JMF | 12:0071cb144c7a | 330 | #if defined(MBEDTLS_SELF_TEST) |
JMF | 12:0071cb144c7a | 331 | /* |
JMF | 12:0071cb144c7a | 332 | * RFC 1321 test vectors |
JMF | 12:0071cb144c7a | 333 | */ |
JMF | 12:0071cb144c7a | 334 | static const unsigned char md5_test_buf[7][81] = |
JMF | 12:0071cb144c7a | 335 | { |
JMF | 12:0071cb144c7a | 336 | { "" }, |
JMF | 12:0071cb144c7a | 337 | { "a" }, |
JMF | 12:0071cb144c7a | 338 | { "abc" }, |
JMF | 12:0071cb144c7a | 339 | { "message digest" }, |
JMF | 12:0071cb144c7a | 340 | { "abcdefghijklmnopqrstuvwxyz" }, |
JMF | 12:0071cb144c7a | 341 | { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" }, |
JMF | 12:0071cb144c7a | 342 | { "12345678901234567890123456789012345678901234567890123456789012" \ |
JMF | 12:0071cb144c7a | 343 | "345678901234567890" } |
JMF | 12:0071cb144c7a | 344 | }; |
JMF | 12:0071cb144c7a | 345 | |
JMF | 12:0071cb144c7a | 346 | static const int md5_test_buflen[7] = |
JMF | 12:0071cb144c7a | 347 | { |
JMF | 12:0071cb144c7a | 348 | 0, 1, 3, 14, 26, 62, 80 |
JMF | 12:0071cb144c7a | 349 | }; |
JMF | 12:0071cb144c7a | 350 | |
JMF | 12:0071cb144c7a | 351 | static const unsigned char md5_test_sum[7][16] = |
JMF | 12:0071cb144c7a | 352 | { |
JMF | 12:0071cb144c7a | 353 | { 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04, |
JMF | 12:0071cb144c7a | 354 | 0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E }, |
JMF | 12:0071cb144c7a | 355 | { 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8, |
JMF | 12:0071cb144c7a | 356 | 0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 }, |
JMF | 12:0071cb144c7a | 357 | { 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0, |
JMF | 12:0071cb144c7a | 358 | 0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 }, |
JMF | 12:0071cb144c7a | 359 | { 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D, |
JMF | 12:0071cb144c7a | 360 | 0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 }, |
JMF | 12:0071cb144c7a | 361 | { 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00, |
JMF | 12:0071cb144c7a | 362 | 0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B }, |
JMF | 12:0071cb144c7a | 363 | { 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5, |
JMF | 12:0071cb144c7a | 364 | 0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F }, |
JMF | 12:0071cb144c7a | 365 | { 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55, |
JMF | 12:0071cb144c7a | 366 | 0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A } |
JMF | 12:0071cb144c7a | 367 | }; |
JMF | 12:0071cb144c7a | 368 | |
JMF | 12:0071cb144c7a | 369 | /* |
JMF | 12:0071cb144c7a | 370 | * Checkup routine |
JMF | 12:0071cb144c7a | 371 | */ |
JMF | 12:0071cb144c7a | 372 | int mbedtls_md5_self_test( int verbose ) |
JMF | 12:0071cb144c7a | 373 | { |
JMF | 12:0071cb144c7a | 374 | int i; |
JMF | 12:0071cb144c7a | 375 | unsigned char md5sum[16]; |
JMF | 12:0071cb144c7a | 376 | |
JMF | 12:0071cb144c7a | 377 | for( i = 0; i < 7; i++ ) |
JMF | 12:0071cb144c7a | 378 | { |
JMF | 12:0071cb144c7a | 379 | if( verbose != 0 ) |
JMF | 12:0071cb144c7a | 380 | mbedtls_printf( " MD5 test #%d: ", i + 1 ); |
JMF | 12:0071cb144c7a | 381 | |
JMF | 12:0071cb144c7a | 382 | mbedtls_md5( md5_test_buf[i], md5_test_buflen[i], md5sum ); |
JMF | 12:0071cb144c7a | 383 | |
JMF | 12:0071cb144c7a | 384 | if( memcmp( md5sum, md5_test_sum[i], 16 ) != 0 ) |
JMF | 12:0071cb144c7a | 385 | { |
JMF | 12:0071cb144c7a | 386 | if( verbose != 0 ) |
JMF | 12:0071cb144c7a | 387 | mbedtls_printf( "failed\n" ); |
JMF | 12:0071cb144c7a | 388 | |
JMF | 12:0071cb144c7a | 389 | return( 1 ); |
JMF | 12:0071cb144c7a | 390 | } |
JMF | 12:0071cb144c7a | 391 | |
JMF | 12:0071cb144c7a | 392 | if( verbose != 0 ) |
JMF | 12:0071cb144c7a | 393 | mbedtls_printf( "passed\n" ); |
JMF | 12:0071cb144c7a | 394 | } |
JMF | 12:0071cb144c7a | 395 | |
JMF | 12:0071cb144c7a | 396 | if( verbose != 0 ) |
JMF | 12:0071cb144c7a | 397 | mbedtls_printf( "\n" ); |
JMF | 12:0071cb144c7a | 398 | |
JMF | 12:0071cb144c7a | 399 | return( 0 ); |
JMF | 12:0071cb144c7a | 400 | } |
JMF | 12:0071cb144c7a | 401 | |
JMF | 12:0071cb144c7a | 402 | #endif /* MBEDTLS_SELF_TEST */ |
JMF | 12:0071cb144c7a | 403 | |
JMF | 12:0071cb144c7a | 404 | #endif /* MBEDTLS_MD5_C */ |