md4.c 15 KB

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