md4.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. #include <stdio.h>
  40. #define mbedtls_printf printf
  41. #endif /* MBEDTLS_PLATFORM_C */
  42. #endif /* MBEDTLS_SELF_TEST */
  43. #if !defined(MBEDTLS_MD4_ALT)
  44. /* Implementation that should never be optimized out by the compiler */
  45. static void mbedtls_zeroize( void *v, size_t n ) {
  46. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  47. }
  48. /*
  49. * 32-bit integer manipulation macros (little endian)
  50. */
  51. #ifndef GET_UINT32_LE
  52. #define GET_UINT32_LE(n,b,i) \
  53. { \
  54. (n) = ( (uint32_t) (b)[(i) ] ) \
  55. | ( (uint32_t) (b)[(i) + 1] << 8 ) \
  56. | ( (uint32_t) (b)[(i) + 2] << 16 ) \
  57. | ( (uint32_t) (b)[(i) + 3] << 24 ); \
  58. }
  59. #endif
  60. #ifndef PUT_UINT32_LE
  61. #define PUT_UINT32_LE(n,b,i) \
  62. { \
  63. (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \
  64. (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \
  65. (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \
  66. (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \
  67. }
  68. #endif
  69. void mbedtls_md4_init( mbedtls_md4_context *ctx )
  70. {
  71. memset( ctx, 0, sizeof( mbedtls_md4_context ) );
  72. }
  73. void mbedtls_md4_free( mbedtls_md4_context *ctx )
  74. {
  75. if( ctx == NULL )
  76. return;
  77. mbedtls_zeroize( ctx, sizeof( mbedtls_md4_context ) );
  78. }
  79. void mbedtls_md4_clone( mbedtls_md4_context *dst,
  80. const mbedtls_md4_context *src )
  81. {
  82. *dst = *src;
  83. }
  84. /*
  85. * MD4 context setup
  86. */
  87. int mbedtls_md4_starts_ret( mbedtls_md4_context *ctx )
  88. {
  89. ctx->total[0] = 0;
  90. ctx->total[1] = 0;
  91. ctx->state[0] = 0x67452301;
  92. ctx->state[1] = 0xEFCDAB89;
  93. ctx->state[2] = 0x98BADCFE;
  94. ctx->state[3] = 0x10325476;
  95. return( 0 );
  96. }
  97. #if !defined(MBEDTLS_MD4_PROCESS_ALT)
  98. int mbedtls_internal_md4_process( mbedtls_md4_context *ctx,
  99. const unsigned char data[64] )
  100. {
  101. uint32_t X[16], A, B, C, D;
  102. GET_UINT32_LE( X[ 0], data, 0 );
  103. GET_UINT32_LE( X[ 1], data, 4 );
  104. GET_UINT32_LE( X[ 2], data, 8 );
  105. GET_UINT32_LE( X[ 3], data, 12 );
  106. GET_UINT32_LE( X[ 4], data, 16 );
  107. GET_UINT32_LE( X[ 5], data, 20 );
  108. GET_UINT32_LE( X[ 6], data, 24 );
  109. GET_UINT32_LE( X[ 7], data, 28 );
  110. GET_UINT32_LE( X[ 8], data, 32 );
  111. GET_UINT32_LE( X[ 9], data, 36 );
  112. GET_UINT32_LE( X[10], data, 40 );
  113. GET_UINT32_LE( X[11], data, 44 );
  114. GET_UINT32_LE( X[12], data, 48 );
  115. GET_UINT32_LE( X[13], data, 52 );
  116. GET_UINT32_LE( X[14], data, 56 );
  117. GET_UINT32_LE( X[15], data, 60 );
  118. #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
  119. A = ctx->state[0];
  120. B = ctx->state[1];
  121. C = ctx->state[2];
  122. D = ctx->state[3];
  123. #define F(x, y, z) ((x & y) | ((~x) & z))
  124. #define P(a,b,c,d,x,s) { a += F(b,c,d) + x; a = S(a,s); }
  125. P( A, B, C, D, X[ 0], 3 );
  126. P( D, A, B, C, X[ 1], 7 );
  127. P( C, D, A, B, X[ 2], 11 );
  128. P( B, C, D, A, X[ 3], 19 );
  129. P( A, B, C, D, X[ 4], 3 );
  130. P( D, A, B, C, X[ 5], 7 );
  131. P( C, D, A, B, X[ 6], 11 );
  132. P( B, C, D, A, X[ 7], 19 );
  133. P( A, B, C, D, X[ 8], 3 );
  134. P( D, A, B, C, X[ 9], 7 );
  135. P( C, D, A, B, X[10], 11 );
  136. P( B, C, D, A, X[11], 19 );
  137. P( A, B, C, D, X[12], 3 );
  138. P( D, A, B, C, X[13], 7 );
  139. P( C, D, A, B, X[14], 11 );
  140. P( B, C, D, A, X[15], 19 );
  141. #undef P
  142. #undef F
  143. #define F(x,y,z) ((x & y) | (x & z) | (y & z))
  144. #define P(a,b,c,d,x,s) { a += F(b,c,d) + x + 0x5A827999; a = S(a,s); }
  145. P( A, B, C, D, X[ 0], 3 );
  146. P( D, A, B, C, X[ 4], 5 );
  147. P( C, D, A, B, X[ 8], 9 );
  148. P( B, C, D, A, X[12], 13 );
  149. P( A, B, C, D, X[ 1], 3 );
  150. P( D, A, B, C, X[ 5], 5 );
  151. P( C, D, A, B, X[ 9], 9 );
  152. P( B, C, D, A, X[13], 13 );
  153. P( A, B, C, D, X[ 2], 3 );
  154. P( D, A, B, C, X[ 6], 5 );
  155. P( C, D, A, B, X[10], 9 );
  156. P( B, C, D, A, X[14], 13 );
  157. P( A, B, C, D, X[ 3], 3 );
  158. P( D, A, B, C, X[ 7], 5 );
  159. P( C, D, A, B, X[11], 9 );
  160. P( B, C, D, A, X[15], 13 );
  161. #undef P
  162. #undef F
  163. #define F(x,y,z) (x ^ y ^ z)
  164. #define P(a,b,c,d,x,s) { a += F(b,c,d) + x + 0x6ED9EBA1; a = S(a,s); }
  165. P( A, B, C, D, X[ 0], 3 );
  166. P( D, A, B, C, X[ 8], 9 );
  167. P( C, D, A, B, X[ 4], 11 );
  168. P( B, C, D, A, X[12], 15 );
  169. P( A, B, C, D, X[ 2], 3 );
  170. P( D, A, B, C, X[10], 9 );
  171. P( C, D, A, B, X[ 6], 11 );
  172. P( B, C, D, A, X[14], 15 );
  173. P( A, B, C, D, X[ 1], 3 );
  174. P( D, A, B, C, X[ 9], 9 );
  175. P( C, D, A, B, X[ 5], 11 );
  176. P( B, C, D, A, X[13], 15 );
  177. P( A, B, C, D, X[ 3], 3 );
  178. P( D, A, B, C, X[11], 9 );
  179. P( C, D, A, B, X[ 7], 11 );
  180. P( B, C, D, A, X[15], 15 );
  181. #undef F
  182. #undef P
  183. ctx->state[0] += A;
  184. ctx->state[1] += B;
  185. ctx->state[2] += C;
  186. ctx->state[3] += D;
  187. return( 0 );
  188. }
  189. #endif /* !MBEDTLS_MD4_PROCESS_ALT */
  190. /*
  191. * MD4 process buffer
  192. */
  193. int mbedtls_md4_update_ret( mbedtls_md4_context *ctx,
  194. const unsigned char *input,
  195. size_t ilen )
  196. {
  197. int ret;
  198. size_t fill;
  199. uint32_t left;
  200. if( ilen == 0 )
  201. return( 0 );
  202. left = ctx->total[0] & 0x3F;
  203. fill = 64 - left;
  204. ctx->total[0] += (uint32_t) ilen;
  205. ctx->total[0] &= 0xFFFFFFFF;
  206. if( ctx->total[0] < (uint32_t) ilen )
  207. ctx->total[1]++;
  208. if( left && ilen >= fill )
  209. {
  210. memcpy( (void *) (ctx->buffer + left),
  211. (void *) input, fill );
  212. if( ( ret = mbedtls_internal_md4_process( ctx, ctx->buffer ) ) != 0 )
  213. return( ret );
  214. input += fill;
  215. ilen -= fill;
  216. left = 0;
  217. }
  218. while( ilen >= 64 )
  219. {
  220. if( ( ret = mbedtls_internal_md4_process( ctx, input ) ) != 0 )
  221. return( ret );
  222. input += 64;
  223. ilen -= 64;
  224. }
  225. if( ilen > 0 )
  226. {
  227. memcpy( (void *) (ctx->buffer + left),
  228. (void *) input, ilen );
  229. }
  230. return( 0 );
  231. }
  232. static const unsigned char md4_padding[64] =
  233. {
  234. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  235. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  236. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  237. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  238. };
  239. /*
  240. * MD4 final digest
  241. */
  242. int mbedtls_md4_finish_ret( mbedtls_md4_context *ctx,
  243. unsigned char output[16] )
  244. {
  245. int ret;
  246. uint32_t last, padn;
  247. uint32_t high, low;
  248. unsigned char msglen[8];
  249. high = ( ctx->total[0] >> 29 )
  250. | ( ctx->total[1] << 3 );
  251. low = ( ctx->total[0] << 3 );
  252. PUT_UINT32_LE( low, msglen, 0 );
  253. PUT_UINT32_LE( high, msglen, 4 );
  254. last = ctx->total[0] & 0x3F;
  255. padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
  256. ret = mbedtls_md4_update_ret( ctx, (unsigned char *)md4_padding, padn );
  257. if( ret != 0 )
  258. return( ret );
  259. if( ( ret = mbedtls_md4_update_ret( ctx, msglen, 8 ) ) != 0 )
  260. return( ret );
  261. PUT_UINT32_LE( ctx->state[0], output, 0 );
  262. PUT_UINT32_LE( ctx->state[1], output, 4 );
  263. PUT_UINT32_LE( ctx->state[2], output, 8 );
  264. PUT_UINT32_LE( ctx->state[3], output, 12 );
  265. return( 0 );
  266. }
  267. #endif /* !MBEDTLS_MD4_ALT */
  268. /*
  269. * output = MD4( input buffer )
  270. */
  271. int mbedtls_md4_ret( const unsigned char *input,
  272. size_t ilen,
  273. unsigned char output[16] )
  274. {
  275. int ret;
  276. mbedtls_md4_context ctx;
  277. mbedtls_md4_init( &ctx );
  278. if( ( ret = mbedtls_md4_starts_ret( &ctx ) ) != 0 )
  279. goto exit;
  280. if( ( ret = mbedtls_md4_update_ret( &ctx, input, ilen ) ) != 0 )
  281. goto exit;
  282. if( ( ret = mbedtls_md4_finish_ret( &ctx, output ) ) != 0 )
  283. goto exit;
  284. exit:
  285. mbedtls_md4_free( &ctx );
  286. return( ret );
  287. }
  288. #if defined(MBEDTLS_SELF_TEST)
  289. /*
  290. * RFC 1320 test vectors
  291. */
  292. static const unsigned char md4_test_str[7][81] =
  293. {
  294. { "" },
  295. { "a" },
  296. { "abc" },
  297. { "message digest" },
  298. { "abcdefghijklmnopqrstuvwxyz" },
  299. { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
  300. { "12345678901234567890123456789012345678901234567890123456789012"
  301. "345678901234567890" }
  302. };
  303. static const size_t md4_test_strlen[7] =
  304. {
  305. 0, 1, 3, 14, 26, 62, 80
  306. };
  307. static const unsigned char md4_test_sum[7][16] =
  308. {
  309. { 0x31, 0xD6, 0xCF, 0xE0, 0xD1, 0x6A, 0xE9, 0x31,
  310. 0xB7, 0x3C, 0x59, 0xD7, 0xE0, 0xC0, 0x89, 0xC0 },
  311. { 0xBD, 0xE5, 0x2C, 0xB3, 0x1D, 0xE3, 0x3E, 0x46,
  312. 0x24, 0x5E, 0x05, 0xFB, 0xDB, 0xD6, 0xFB, 0x24 },
  313. { 0xA4, 0x48, 0x01, 0x7A, 0xAF, 0x21, 0xD8, 0x52,
  314. 0x5F, 0xC1, 0x0A, 0xE8, 0x7A, 0xA6, 0x72, 0x9D },
  315. { 0xD9, 0x13, 0x0A, 0x81, 0x64, 0x54, 0x9F, 0xE8,
  316. 0x18, 0x87, 0x48, 0x06, 0xE1, 0xC7, 0x01, 0x4B },
  317. { 0xD7, 0x9E, 0x1C, 0x30, 0x8A, 0xA5, 0xBB, 0xCD,
  318. 0xEE, 0xA8, 0xED, 0x63, 0xDF, 0x41, 0x2D, 0xA9 },
  319. { 0x04, 0x3F, 0x85, 0x82, 0xF2, 0x41, 0xDB, 0x35,
  320. 0x1C, 0xE6, 0x27, 0xE1, 0x53, 0xE7, 0xF0, 0xE4 },
  321. { 0xE3, 0x3B, 0x4D, 0xDC, 0x9C, 0x38, 0xF2, 0x19,
  322. 0x9C, 0x3E, 0x7B, 0x16, 0x4F, 0xCC, 0x05, 0x36 }
  323. };
  324. /*
  325. * Checkup routine
  326. */
  327. int mbedtls_md4_self_test( int verbose )
  328. {
  329. int i, ret = 0;
  330. unsigned char md4sum[16];
  331. for( i = 0; i < 7; i++ )
  332. {
  333. if( verbose != 0 )
  334. mbedtls_printf( " MD4 test #%d: ", i + 1 );
  335. ret = mbedtls_md4_ret( md4_test_str[i], md4_test_strlen[i], md4sum );
  336. if( ret != 0 )
  337. goto fail;
  338. if( memcmp( md4sum, md4_test_sum[i], 16 ) != 0 )
  339. {
  340. ret = 1;
  341. goto fail;
  342. }
  343. if( verbose != 0 )
  344. mbedtls_printf( "passed\n" );
  345. }
  346. if( verbose != 0 )
  347. mbedtls_printf( "\n" );
  348. return( 0 );
  349. fail:
  350. if( verbose != 0 )
  351. mbedtls_printf( "failed\n" );
  352. return( ret );
  353. }
  354. #endif /* MBEDTLS_SELF_TEST */
  355. #endif /* MBEDTLS_MD4_C */