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