md4.c 14 KB

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