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