md4.c 13 KB

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