md4.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * RFC 1186/1320 compliant MD4 implementation
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. /*
  22. * The MD4 algorithm was designed by Ron Rivest in 1990.
  23. *
  24. * http://www.ietf.org/rfc/rfc1186.txt
  25. * http://www.ietf.org/rfc/rfc1320.txt
  26. */
  27. #if !defined(MBEDTLS_CONFIG_FILE)
  28. #include "mbedtls/config.h"
  29. #else
  30. #include MBEDTLS_CONFIG_FILE
  31. #endif
  32. #if defined(MBEDTLS_MD4_C)
  33. #include "mbedtls/md4.h"
  34. #include <string.h>
  35. #if defined(MBEDTLS_SELF_TEST)
  36. #if defined(MBEDTLS_PLATFORM_C)
  37. #include "mbedtls/platform.h"
  38. #else
  39. #ifdef PRINTF_STDLIB
  40. #include <stdio.h>
  41. #endif
  42. #ifdef PRINTF_CUSTOM
  43. #include "tinystdio.h"
  44. #endif
  45. #define mbedtls_printf printf
  46. #endif /* MBEDTLS_PLATFORM_C */
  47. #endif /* MBEDTLS_SELF_TEST */
  48. #if !defined(MBEDTLS_MD4_ALT)
  49. /* Implementation that should never be optimized out by the compiler */
  50. static void mbedtls_zeroize( void *v, size_t n ) {
  51. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  52. }
  53. /*
  54. * 32-bit integer manipulation macros (little endian)
  55. */
  56. #ifndef GET_UINT32_LE
  57. #define GET_UINT32_LE(n,b,i) \
  58. { \
  59. (n) = ( (uint32_t) (b)[(i) ] ) \
  60. | ( (uint32_t) (b)[(i) + 1] << 8 ) \
  61. | ( (uint32_t) (b)[(i) + 2] << 16 ) \
  62. | ( (uint32_t) (b)[(i) + 3] << 24 ); \
  63. }
  64. #endif
  65. #ifndef PUT_UINT32_LE
  66. #define PUT_UINT32_LE(n,b,i) \
  67. { \
  68. (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
  69. (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
  70. (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
  71. (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
  72. }
  73. #endif
  74. void mbedtls_md4_init( mbedtls_md4_context *ctx )
  75. {
  76. memset( ctx, 0, sizeof( mbedtls_md4_context ) );
  77. }
  78. void mbedtls_md4_free( mbedtls_md4_context *ctx )
  79. {
  80. if( ctx == NULL )
  81. return;
  82. mbedtls_zeroize( ctx, sizeof( mbedtls_md4_context ) );
  83. }
  84. void mbedtls_md4_clone( mbedtls_md4_context *dst,
  85. const mbedtls_md4_context *src )
  86. {
  87. *dst = *src;
  88. }
  89. /*
  90. * MD4 context setup
  91. */
  92. void mbedtls_md4_starts( mbedtls_md4_context *ctx )
  93. {
  94. ctx->total[0] = 0;
  95. ctx->total[1] = 0;
  96. ctx->state[0] = 0x67452301;
  97. ctx->state[1] = 0xEFCDAB89;
  98. ctx->state[2] = 0x98BADCFE;
  99. ctx->state[3] = 0x10325476;
  100. }
  101. #if !defined(MBEDTLS_MD4_PROCESS_ALT)
  102. void mbedtls_md4_process( mbedtls_md4_context *ctx, const unsigned char data[64] )
  103. {
  104. uint32_t X[16], A, B, C, D;
  105. GET_UINT32_LE( X[ 0], data, 0 );
  106. GET_UINT32_LE( X[ 1], data, 4 );
  107. GET_UINT32_LE( X[ 2], data, 8 );
  108. GET_UINT32_LE( X[ 3], data, 12 );
  109. GET_UINT32_LE( X[ 4], data, 16 );
  110. GET_UINT32_LE( X[ 5], data, 20 );
  111. GET_UINT32_LE( X[ 6], data, 24 );
  112. GET_UINT32_LE( X[ 7], data, 28 );
  113. GET_UINT32_LE( X[ 8], data, 32 );
  114. GET_UINT32_LE( X[ 9], data, 36 );
  115. GET_UINT32_LE( X[10], data, 40 );
  116. GET_UINT32_LE( X[11], data, 44 );
  117. GET_UINT32_LE( X[12], data, 48 );
  118. GET_UINT32_LE( X[13], data, 52 );
  119. GET_UINT32_LE( X[14], data, 56 );
  120. GET_UINT32_LE( X[15], data, 60 );
  121. #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
  122. A = ctx->state[0];
  123. B = ctx->state[1];
  124. C = ctx->state[2];
  125. D = ctx->state[3];
  126. #define F(x, y, z) ((x & y) | ((~x) & z))
  127. #define P(a,b,c,d,x,s) { a += F(b,c,d) + x; a = S(a,s); }
  128. P( A, B, C, D, X[ 0], 3 );
  129. P( D, A, B, C, X[ 1], 7 );
  130. P( C, D, A, B, X[ 2], 11 );
  131. P( B, C, D, A, X[ 3], 19 );
  132. P( A, B, C, D, X[ 4], 3 );
  133. P( D, A, B, C, X[ 5], 7 );
  134. P( C, D, A, B, X[ 6], 11 );
  135. P( B, C, D, A, X[ 7], 19 );
  136. P( A, B, C, D, X[ 8], 3 );
  137. P( D, A, B, C, X[ 9], 7 );
  138. P( C, D, A, B, X[10], 11 );
  139. P( B, C, D, A, X[11], 19 );
  140. P( A, B, C, D, X[12], 3 );
  141. P( D, A, B, C, X[13], 7 );
  142. P( C, D, A, B, X[14], 11 );
  143. P( B, C, D, A, X[15], 19 );
  144. #undef P
  145. #undef F
  146. #define F(x,y,z) ((x & y) | (x & z) | (y & z))
  147. #define P(a,b,c,d,x,s) { a += F(b,c,d) + x + 0x5A827999; a = S(a,s); }
  148. P( A, B, C, D, X[ 0], 3 );
  149. P( D, A, B, C, X[ 4], 5 );
  150. P( C, D, A, B, X[ 8], 9 );
  151. P( B, C, D, A, X[12], 13 );
  152. P( A, B, C, D, X[ 1], 3 );
  153. P( D, A, B, C, X[ 5], 5 );
  154. P( C, D, A, B, X[ 9], 9 );
  155. P( B, C, D, A, X[13], 13 );
  156. P( A, B, C, D, X[ 2], 3 );
  157. P( D, A, B, C, X[ 6], 5 );
  158. P( C, D, A, B, X[10], 9 );
  159. P( B, C, D, A, X[14], 13 );
  160. P( A, B, C, D, X[ 3], 3 );
  161. P( D, A, B, C, X[ 7], 5 );
  162. P( C, D, A, B, X[11], 9 );
  163. P( B, C, D, A, X[15], 13 );
  164. #undef P
  165. #undef F
  166. #define F(x,y,z) (x ^ y ^ z)
  167. #define P(a,b,c,d,x,s) { a += F(b,c,d) + x + 0x6ED9EBA1; a = S(a,s); }
  168. P( A, B, C, D, X[ 0], 3 );
  169. P( D, A, B, C, X[ 8], 9 );
  170. P( C, D, A, B, X[ 4], 11 );
  171. P( B, C, D, A, X[12], 15 );
  172. P( A, B, C, D, X[ 2], 3 );
  173. P( D, A, B, C, X[10], 9 );
  174. P( C, D, A, B, X[ 6], 11 );
  175. P( B, C, D, A, X[14], 15 );
  176. P( A, B, C, D, X[ 1], 3 );
  177. P( D, A, B, C, X[ 9], 9 );
  178. P( C, D, A, B, X[ 5], 11 );
  179. P( B, C, D, A, X[13], 15 );
  180. P( A, B, C, D, X[ 3], 3 );
  181. P( D, A, B, C, X[11], 9 );
  182. P( C, D, A, B, X[ 7], 11 );
  183. P( B, C, D, A, X[15], 15 );
  184. #undef F
  185. #undef P
  186. ctx->state[0] += A;
  187. ctx->state[1] += B;
  188. ctx->state[2] += C;
  189. ctx->state[3] += D;
  190. }
  191. #endif /* !MBEDTLS_MD4_PROCESS_ALT */
  192. /*
  193. * MD4 process buffer
  194. */
  195. void mbedtls_md4_update( mbedtls_md4_context *ctx, const unsigned char *input, size_t ilen )
  196. {
  197. size_t fill;
  198. uint32_t left;
  199. if( ilen == 0 )
  200. return;
  201. left = ctx->total[0] & 0x3F;
  202. fill = 64 - left;
  203. ctx->total[0] += (uint32_t) ilen;
  204. ctx->total[0] &= 0xFFFFFFFF;
  205. if( ctx->total[0] < (uint32_t) ilen )
  206. ctx->total[1]++;
  207. if( left && ilen >= fill )
  208. {
  209. memcpy( (void *) (ctx->buffer + left),
  210. (void *) input, fill );
  211. mbedtls_md4_process( ctx, ctx->buffer );
  212. input += fill;
  213. ilen -= fill;
  214. left = 0;
  215. }
  216. while( ilen >= 64 )
  217. {
  218. mbedtls_md4_process( ctx, input );
  219. input += 64;
  220. ilen -= 64;
  221. }
  222. if( ilen > 0 )
  223. {
  224. memcpy( (void *) (ctx->buffer + left),
  225. (void *) input, ilen );
  226. }
  227. }
  228. static const unsigned char md4_padding[64] =
  229. {
  230. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  231. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  232. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  233. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  234. };
  235. /*
  236. * MD4 final digest
  237. */
  238. void mbedtls_md4_finish( mbedtls_md4_context *ctx, unsigned char output[16] )
  239. {
  240. uint32_t last, padn;
  241. uint32_t high, low;
  242. unsigned char msglen[8];
  243. high = ( ctx->total[0] >> 29 )
  244. | ( ctx->total[1] << 3 );
  245. low = ( ctx->total[0] << 3 );
  246. PUT_UINT32_LE( low, msglen, 0 );
  247. PUT_UINT32_LE( high, msglen, 4 );
  248. last = ctx->total[0] & 0x3F;
  249. padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
  250. mbedtls_md4_update( ctx, (unsigned char *) md4_padding, padn );
  251. mbedtls_md4_update( ctx, msglen, 8 );
  252. PUT_UINT32_LE( ctx->state[0], output, 0 );
  253. PUT_UINT32_LE( ctx->state[1], output, 4 );
  254. PUT_UINT32_LE( ctx->state[2], output, 8 );
  255. PUT_UINT32_LE( ctx->state[3], output, 12 );
  256. }
  257. #endif /* !MBEDTLS_MD4_ALT */
  258. /*
  259. * output = MD4( input buffer )
  260. */
  261. void mbedtls_md4( const unsigned char *input, size_t ilen, unsigned char output[16] )
  262. {
  263. mbedtls_md4_context ctx;
  264. mbedtls_md4_init( &ctx );
  265. mbedtls_md4_starts( &ctx );
  266. mbedtls_md4_update( &ctx, input, ilen );
  267. mbedtls_md4_finish( &ctx, output );
  268. mbedtls_md4_free( &ctx );
  269. }
  270. #if defined(MBEDTLS_SELF_TEST)
  271. /*
  272. * RFC 1320 test vectors
  273. */
  274. static const char md4_test_str[7][81] =
  275. {
  276. { "" },
  277. { "a" },
  278. { "abc" },
  279. { "message digest" },
  280. { "abcdefghijklmnopqrstuvwxyz" },
  281. { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
  282. { "12345678901234567890123456789012345678901234567890123456789012" \
  283. "345678901234567890" }
  284. };
  285. static const unsigned char md4_test_sum[7][16] =
  286. {
  287. { 0x31, 0xD6, 0xCF, 0xE0, 0xD1, 0x6A, 0xE9, 0x31,
  288. 0xB7, 0x3C, 0x59, 0xD7, 0xE0, 0xC0, 0x89, 0xC0 },
  289. { 0xBD, 0xE5, 0x2C, 0xB3, 0x1D, 0xE3, 0x3E, 0x46,
  290. 0x24, 0x5E, 0x05, 0xFB, 0xDB, 0xD6, 0xFB, 0x24 },
  291. { 0xA4, 0x48, 0x01, 0x7A, 0xAF, 0x21, 0xD8, 0x52,
  292. 0x5F, 0xC1, 0x0A, 0xE8, 0x7A, 0xA6, 0x72, 0x9D },
  293. { 0xD9, 0x13, 0x0A, 0x81, 0x64, 0x54, 0x9F, 0xE8,
  294. 0x18, 0x87, 0x48, 0x06, 0xE1, 0xC7, 0x01, 0x4B },
  295. { 0xD7, 0x9E, 0x1C, 0x30, 0x8A, 0xA5, 0xBB, 0xCD,
  296. 0xEE, 0xA8, 0xED, 0x63, 0xDF, 0x41, 0x2D, 0xA9 },
  297. { 0x04, 0x3F, 0x85, 0x82, 0xF2, 0x41, 0xDB, 0x35,
  298. 0x1C, 0xE6, 0x27, 0xE1, 0x53, 0xE7, 0xF0, 0xE4 },
  299. { 0xE3, 0x3B, 0x4D, 0xDC, 0x9C, 0x38, 0xF2, 0x19,
  300. 0x9C, 0x3E, 0x7B, 0x16, 0x4F, 0xCC, 0x05, 0x36 }
  301. };
  302. /*
  303. * Checkup routine
  304. */
  305. int mbedtls_md4_self_test( int verbose )
  306. {
  307. int i;
  308. unsigned char md4sum[16];
  309. for( i = 0; i < 7; i++ )
  310. {
  311. if( verbose != 0 )
  312. mbedtls_printf( " MD4 test #%d: ", i + 1 );
  313. mbedtls_md4( (unsigned char *) md4_test_str[i],
  314. strlen( md4_test_str[i] ), md4sum );
  315. if( memcmp( md4sum, md4_test_sum[i], 16 ) != 0 )
  316. {
  317. if( verbose != 0 )
  318. mbedtls_printf( "failed\n" );
  319. return( 1 );
  320. }
  321. if( verbose != 0 )
  322. mbedtls_printf( "passed\n" );
  323. }
  324. if( verbose != 0 )
  325. mbedtls_printf( "\n" );
  326. return( 0 );
  327. }
  328. #endif /* MBEDTLS_SELF_TEST */
  329. #endif /* MBEDTLS_MD4_C */