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