Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

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