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