md4.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * RFC 1186/1320 compliant MD4 implementation
  3. *
  4. * Copyright (C) 2006-2014, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. /*
  26. * The MD4 algorithm was designed by Ron Rivest in 1990.
  27. *
  28. * http://www.ietf.org/rfc/rfc1186.txt
  29. * http://www.ietf.org/rfc/rfc1320.txt
  30. */
  31. #if !defined(POLARSSL_CONFIG_FILE)
  32. #include "polarssl/config.h"
  33. #else
  34. #include POLARSSL_CONFIG_FILE
  35. #endif
  36. #if defined(POLARSSL_MD4_C)
  37. #include "polarssl/md4.h"
  38. #if defined(POLARSSL_FS_IO) || defined(POLARSSL_SELF_TEST)
  39. #include <stdio.h>
  40. #endif
  41. #if defined(POLARSSL_PLATFORM_C)
  42. #include "polarssl/platform.h"
  43. #else
  44. #define polarssl_printf printf
  45. #endif
  46. /* Implementation that should never be optimized out by the compiler */
  47. static void polarssl_zeroize( void *v, size_t n ) {
  48. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  49. }
  50. #if !defined(POLARSSL_MD4_ALT)
  51. /*
  52. * 32-bit integer manipulation macros (little endian)
  53. */
  54. #ifndef GET_UINT32_LE
  55. #define GET_UINT32_LE(n,b,i) \
  56. { \
  57. (n) = ( (uint32_t) (b)[(i) ] ) \
  58. | ( (uint32_t) (b)[(i) + 1] << 8 ) \
  59. | ( (uint32_t) (b)[(i) + 2] << 16 ) \
  60. | ( (uint32_t) (b)[(i) + 3] << 24 ); \
  61. }
  62. #endif
  63. #ifndef PUT_UINT32_LE
  64. #define PUT_UINT32_LE(n,b,i) \
  65. { \
  66. (b)[(i) ] = (unsigned char) ( (n) ); \
  67. (b)[(i) + 1] = (unsigned char) ( (n) >> 8 ); \
  68. (b)[(i) + 2] = (unsigned char) ( (n) >> 16 ); \
  69. (b)[(i) + 3] = (unsigned char) ( (n) >> 24 ); \
  70. }
  71. #endif
  72. void md4_init( md4_context *ctx )
  73. {
  74. memset( ctx, 0, sizeof( md4_context ) );
  75. }
  76. void md4_free( md4_context *ctx )
  77. {
  78. if( ctx == NULL )
  79. return;
  80. polarssl_zeroize( ctx, sizeof( md4_context ) );
  81. }
  82. /*
  83. * MD4 context setup
  84. */
  85. void md4_starts( md4_context *ctx )
  86. {
  87. ctx->total[0] = 0;
  88. ctx->total[1] = 0;
  89. ctx->state[0] = 0x67452301;
  90. ctx->state[1] = 0xEFCDAB89;
  91. ctx->state[2] = 0x98BADCFE;
  92. ctx->state[3] = 0x10325476;
  93. }
  94. void md4_process( md4_context *ctx, const unsigned char data[64] )
  95. {
  96. uint32_t X[16], A, B, C, D;
  97. GET_UINT32_LE( X[ 0], data, 0 );
  98. GET_UINT32_LE( X[ 1], data, 4 );
  99. GET_UINT32_LE( X[ 2], data, 8 );
  100. GET_UINT32_LE( X[ 3], data, 12 );
  101. GET_UINT32_LE( X[ 4], data, 16 );
  102. GET_UINT32_LE( X[ 5], data, 20 );
  103. GET_UINT32_LE( X[ 6], data, 24 );
  104. GET_UINT32_LE( X[ 7], data, 28 );
  105. GET_UINT32_LE( X[ 8], data, 32 );
  106. GET_UINT32_LE( X[ 9], data, 36 );
  107. GET_UINT32_LE( X[10], data, 40 );
  108. GET_UINT32_LE( X[11], data, 44 );
  109. GET_UINT32_LE( X[12], data, 48 );
  110. GET_UINT32_LE( X[13], data, 52 );
  111. GET_UINT32_LE( X[14], data, 56 );
  112. GET_UINT32_LE( X[15], data, 60 );
  113. #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
  114. A = ctx->state[0];
  115. B = ctx->state[1];
  116. C = ctx->state[2];
  117. D = ctx->state[3];
  118. #define F(x, y, z) ((x & y) | ((~x) & z))
  119. #define P(a,b,c,d,x,s) { a += F(b,c,d) + x; a = S(a,s); }
  120. P( A, B, C, D, X[ 0], 3 );
  121. P( D, A, B, C, X[ 1], 7 );
  122. P( C, D, A, B, X[ 2], 11 );
  123. P( B, C, D, A, X[ 3], 19 );
  124. P( A, B, C, D, X[ 4], 3 );
  125. P( D, A, B, C, X[ 5], 7 );
  126. P( C, D, A, B, X[ 6], 11 );
  127. P( B, C, D, A, X[ 7], 19 );
  128. P( A, B, C, D, X[ 8], 3 );
  129. P( D, A, B, C, X[ 9], 7 );
  130. P( C, D, A, B, X[10], 11 );
  131. P( B, C, D, A, X[11], 19 );
  132. P( A, B, C, D, X[12], 3 );
  133. P( D, A, B, C, X[13], 7 );
  134. P( C, D, A, B, X[14], 11 );
  135. P( B, C, D, A, X[15], 19 );
  136. #undef P
  137. #undef F
  138. #define F(x,y,z) ((x & y) | (x & z) | (y & z))
  139. #define P(a,b,c,d,x,s) { a += F(b,c,d) + x + 0x5A827999; a = S(a,s); }
  140. P( A, B, C, D, X[ 0], 3 );
  141. P( D, A, B, C, X[ 4], 5 );
  142. P( C, D, A, B, X[ 8], 9 );
  143. P( B, C, D, A, X[12], 13 );
  144. P( A, B, C, D, X[ 1], 3 );
  145. P( D, A, B, C, X[ 5], 5 );
  146. P( C, D, A, B, X[ 9], 9 );
  147. P( B, C, D, A, X[13], 13 );
  148. P( A, B, C, D, X[ 2], 3 );
  149. P( D, A, B, C, X[ 6], 5 );
  150. P( C, D, A, B, X[10], 9 );
  151. P( B, C, D, A, X[14], 13 );
  152. P( A, B, C, D, X[ 3], 3 );
  153. P( D, A, B, C, X[ 7], 5 );
  154. P( C, D, A, B, X[11], 9 );
  155. P( B, C, D, A, X[15], 13 );
  156. #undef P
  157. #undef F
  158. #define F(x,y,z) (x ^ y ^ z)
  159. #define P(a,b,c,d,x,s) { a += F(b,c,d) + x + 0x6ED9EBA1; a = S(a,s); }
  160. P( A, B, C, D, X[ 0], 3 );
  161. P( D, A, B, C, X[ 8], 9 );
  162. P( C, D, A, B, X[ 4], 11 );
  163. P( B, C, D, A, X[12], 15 );
  164. P( A, B, C, D, X[ 2], 3 );
  165. P( D, A, B, C, X[10], 9 );
  166. P( C, D, A, B, X[ 6], 11 );
  167. P( B, C, D, A, X[14], 15 );
  168. P( A, B, C, D, X[ 1], 3 );
  169. P( D, A, B, C, X[ 9], 9 );
  170. P( C, D, A, B, X[ 5], 11 );
  171. P( B, C, D, A, X[13], 15 );
  172. P( A, B, C, D, X[ 3], 3 );
  173. P( D, A, B, C, X[11], 9 );
  174. P( C, D, A, B, X[ 7], 11 );
  175. P( B, C, D, A, X[15], 15 );
  176. #undef F
  177. #undef P
  178. ctx->state[0] += A;
  179. ctx->state[1] += B;
  180. ctx->state[2] += C;
  181. ctx->state[3] += D;
  182. }
  183. /*
  184. * MD4 process buffer
  185. */
  186. void md4_update( md4_context *ctx, const unsigned char *input, size_t ilen )
  187. {
  188. size_t fill;
  189. uint32_t left;
  190. if( ilen == 0 )
  191. return;
  192. left = ctx->total[0] & 0x3F;
  193. fill = 64 - left;
  194. ctx->total[0] += (uint32_t) ilen;
  195. ctx->total[0] &= 0xFFFFFFFF;
  196. if( ctx->total[0] < (uint32_t) ilen )
  197. ctx->total[1]++;
  198. if( left && ilen >= fill )
  199. {
  200. memcpy( (void *) (ctx->buffer + left),
  201. (void *) input, fill );
  202. md4_process( ctx, ctx->buffer );
  203. input += fill;
  204. ilen -= fill;
  205. left = 0;
  206. }
  207. while( ilen >= 64 )
  208. {
  209. md4_process( ctx, input );
  210. input += 64;
  211. ilen -= 64;
  212. }
  213. if( ilen > 0 )
  214. {
  215. memcpy( (void *) (ctx->buffer + left),
  216. (void *) input, ilen );
  217. }
  218. }
  219. static const unsigned char md4_padding[64] =
  220. {
  221. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  222. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  223. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  224. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  225. };
  226. /*
  227. * MD4 final digest
  228. */
  229. void md4_finish( md4_context *ctx, unsigned char output[16] )
  230. {
  231. uint32_t last, padn;
  232. uint32_t high, low;
  233. unsigned char msglen[8];
  234. high = ( ctx->total[0] >> 29 )
  235. | ( ctx->total[1] << 3 );
  236. low = ( ctx->total[0] << 3 );
  237. PUT_UINT32_LE( low, msglen, 0 );
  238. PUT_UINT32_LE( high, msglen, 4 );
  239. last = ctx->total[0] & 0x3F;
  240. padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
  241. md4_update( ctx, (unsigned char *) md4_padding, padn );
  242. md4_update( ctx, msglen, 8 );
  243. PUT_UINT32_LE( ctx->state[0], output, 0 );
  244. PUT_UINT32_LE( ctx->state[1], output, 4 );
  245. PUT_UINT32_LE( ctx->state[2], output, 8 );
  246. PUT_UINT32_LE( ctx->state[3], output, 12 );
  247. }
  248. #endif /* !POLARSSL_MD4_ALT */
  249. /*
  250. * output = MD4( input buffer )
  251. */
  252. void md4( const unsigned char *input, size_t ilen, unsigned char output[16] )
  253. {
  254. md4_context ctx;
  255. md4_init( &ctx );
  256. md4_starts( &ctx );
  257. md4_update( &ctx, input, ilen );
  258. md4_finish( &ctx, output );
  259. md4_free( &ctx );
  260. }
  261. #if defined(POLARSSL_FS_IO)
  262. /*
  263. * output = MD4( file contents )
  264. */
  265. int md4_file( const char *path, unsigned char output[16] )
  266. {
  267. FILE *f;
  268. size_t n;
  269. md4_context ctx;
  270. unsigned char buf[1024];
  271. if( ( f = fopen( path, "rb" ) ) == NULL )
  272. return( POLARSSL_ERR_MD4_FILE_IO_ERROR );
  273. md4_init( &ctx );
  274. md4_starts( &ctx );
  275. while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
  276. md4_update( &ctx, buf, n );
  277. md4_finish( &ctx, output );
  278. md4_free( &ctx );
  279. if( ferror( f ) != 0 )
  280. {
  281. fclose( f );
  282. return( POLARSSL_ERR_MD4_FILE_IO_ERROR );
  283. }
  284. fclose( f );
  285. return( 0 );
  286. }
  287. #endif /* POLARSSL_FS_IO */
  288. /*
  289. * MD4 HMAC context setup
  290. */
  291. void md4_hmac_starts( md4_context *ctx, const unsigned char *key,
  292. size_t keylen )
  293. {
  294. size_t i;
  295. unsigned char sum[16];
  296. if( keylen > 64 )
  297. {
  298. md4( key, keylen, sum );
  299. keylen = 16;
  300. key = sum;
  301. }
  302. memset( ctx->ipad, 0x36, 64 );
  303. memset( ctx->opad, 0x5C, 64 );
  304. for( i = 0; i < keylen; i++ )
  305. {
  306. ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] );
  307. ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] );
  308. }
  309. md4_starts( ctx );
  310. md4_update( ctx, ctx->ipad, 64 );
  311. polarssl_zeroize( sum, sizeof( sum ) );
  312. }
  313. /*
  314. * MD4 HMAC process buffer
  315. */
  316. void md4_hmac_update( md4_context *ctx, const unsigned char *input,
  317. size_t ilen )
  318. {
  319. md4_update( ctx, input, ilen );
  320. }
  321. /*
  322. * MD4 HMAC final digest
  323. */
  324. void md4_hmac_finish( md4_context *ctx, unsigned char output[16] )
  325. {
  326. unsigned char tmpbuf[16];
  327. md4_finish( ctx, tmpbuf );
  328. md4_starts( ctx );
  329. md4_update( ctx, ctx->opad, 64 );
  330. md4_update( ctx, tmpbuf, 16 );
  331. md4_finish( ctx, output );
  332. polarssl_zeroize( tmpbuf, sizeof( tmpbuf ) );
  333. }
  334. /*
  335. * MD4 HMAC context reset
  336. */
  337. void md4_hmac_reset( md4_context *ctx )
  338. {
  339. md4_starts( ctx );
  340. md4_update( ctx, ctx->ipad, 64 );
  341. }
  342. /*
  343. * output = HMAC-MD4( hmac key, input buffer )
  344. */
  345. void md4_hmac( const unsigned char *key, size_t keylen,
  346. const unsigned char *input, size_t ilen,
  347. unsigned char output[16] )
  348. {
  349. md4_context ctx;
  350. md4_init( &ctx );
  351. md4_hmac_starts( &ctx, key, keylen );
  352. md4_hmac_update( &ctx, input, ilen );
  353. md4_hmac_finish( &ctx, output );
  354. md4_free( &ctx );
  355. }
  356. #if defined(POLARSSL_SELF_TEST)
  357. /*
  358. * RFC 1320 test vectors
  359. */
  360. static const char md4_test_str[7][81] =
  361. {
  362. { "" },
  363. { "a" },
  364. { "abc" },
  365. { "message digest" },
  366. { "abcdefghijklmnopqrstuvwxyz" },
  367. { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
  368. { "12345678901234567890123456789012345678901234567890123456789012" \
  369. "345678901234567890" }
  370. };
  371. static const unsigned char md4_test_sum[7][16] =
  372. {
  373. { 0x31, 0xD6, 0xCF, 0xE0, 0xD1, 0x6A, 0xE9, 0x31,
  374. 0xB7, 0x3C, 0x59, 0xD7, 0xE0, 0xC0, 0x89, 0xC0 },
  375. { 0xBD, 0xE5, 0x2C, 0xB3, 0x1D, 0xE3, 0x3E, 0x46,
  376. 0x24, 0x5E, 0x05, 0xFB, 0xDB, 0xD6, 0xFB, 0x24 },
  377. { 0xA4, 0x48, 0x01, 0x7A, 0xAF, 0x21, 0xD8, 0x52,
  378. 0x5F, 0xC1, 0x0A, 0xE8, 0x7A, 0xA6, 0x72, 0x9D },
  379. { 0xD9, 0x13, 0x0A, 0x81, 0x64, 0x54, 0x9F, 0xE8,
  380. 0x18, 0x87, 0x48, 0x06, 0xE1, 0xC7, 0x01, 0x4B },
  381. { 0xD7, 0x9E, 0x1C, 0x30, 0x8A, 0xA5, 0xBB, 0xCD,
  382. 0xEE, 0xA8, 0xED, 0x63, 0xDF, 0x41, 0x2D, 0xA9 },
  383. { 0x04, 0x3F, 0x85, 0x82, 0xF2, 0x41, 0xDB, 0x35,
  384. 0x1C, 0xE6, 0x27, 0xE1, 0x53, 0xE7, 0xF0, 0xE4 },
  385. { 0xE3, 0x3B, 0x4D, 0xDC, 0x9C, 0x38, 0xF2, 0x19,
  386. 0x9C, 0x3E, 0x7B, 0x16, 0x4F, 0xCC, 0x05, 0x36 }
  387. };
  388. /*
  389. * Checkup routine
  390. */
  391. int md4_self_test( int verbose )
  392. {
  393. int i;
  394. unsigned char md4sum[16];
  395. for( i = 0; i < 7; i++ )
  396. {
  397. if( verbose != 0 )
  398. polarssl_printf( " MD4 test #%d: ", i + 1 );
  399. md4( (unsigned char *) md4_test_str[i],
  400. strlen( md4_test_str[i] ), md4sum );
  401. if( memcmp( md4sum, md4_test_sum[i], 16 ) != 0 )
  402. {
  403. if( verbose != 0 )
  404. polarssl_printf( "failed\n" );
  405. return( 1 );
  406. }
  407. if( verbose != 0 )
  408. polarssl_printf( "passed\n" );
  409. }
  410. if( verbose != 0 )
  411. polarssl_printf( "\n" );
  412. return( 0 );
  413. }
  414. #endif /* POLARSSL_SELF_TEST */
  415. #endif /* POLARSSL_MD4_C */