Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  *  RIPE MD-160 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. /*
  25.  *  The RIPEMD-160 algorithm was designed by RIPE in 1996
  26.  *  http://homes.esat.kuleuven.be/~bosselae/mbedtls_ripemd160.html
  27.  *  http://ehash.iaik.tugraz.at/wiki/RIPEMD-160
  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_RIPEMD160_C)
  37.  
  38. #include "mbedtls/ripemd160.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_RIPEMD160_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_ripemd160_init( mbedtls_ripemd160_context *ctx )
  78. {
  79.     memset( ctx, 0, sizeof( mbedtls_ripemd160_context ) );
  80. }
  81.  
  82. void mbedtls_ripemd160_free( mbedtls_ripemd160_context *ctx )
  83. {
  84.     if( ctx == NULL )
  85.         return;
  86.  
  87.     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ripemd160_context ) );
  88. }
  89.  
  90. void mbedtls_ripemd160_clone( mbedtls_ripemd160_context *dst,
  91.                         const mbedtls_ripemd160_context *src )
  92. {
  93.     *dst = *src;
  94. }
  95.  
  96. /*
  97.  * RIPEMD-160 context setup
  98.  */
  99. int mbedtls_ripemd160_starts_ret( mbedtls_ripemd160_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.     ctx->state[4] = 0xC3D2E1F0;
  109.  
  110.     return( 0 );
  111. }
  112.  
  113. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  114. void mbedtls_ripemd160_starts( mbedtls_ripemd160_context *ctx )
  115. {
  116.     mbedtls_ripemd160_starts_ret( ctx );
  117. }
  118. #endif
  119.  
  120. #if !defined(MBEDTLS_RIPEMD160_PROCESS_ALT)
  121. /*
  122.  * Process one block
  123.  */
  124. int mbedtls_internal_ripemd160_process( mbedtls_ripemd160_context *ctx,
  125.                                         const unsigned char data[64] )
  126. {
  127.     uint32_t A, B, C, D, E, Ap, Bp, Cp, Dp, Ep, X[16];
  128.  
  129.     GET_UINT32_LE( X[ 0], data,  0 );
  130.     GET_UINT32_LE( X[ 1], data,  4 );
  131.     GET_UINT32_LE( X[ 2], data,  8 );
  132.     GET_UINT32_LE( X[ 3], data, 12 );
  133.     GET_UINT32_LE( X[ 4], data, 16 );
  134.     GET_UINT32_LE( X[ 5], data, 20 );
  135.     GET_UINT32_LE( X[ 6], data, 24 );
  136.     GET_UINT32_LE( X[ 7], data, 28 );
  137.     GET_UINT32_LE( X[ 8], data, 32 );
  138.     GET_UINT32_LE( X[ 9], data, 36 );
  139.     GET_UINT32_LE( X[10], data, 40 );
  140.     GET_UINT32_LE( X[11], data, 44 );
  141.     GET_UINT32_LE( X[12], data, 48 );
  142.     GET_UINT32_LE( X[13], data, 52 );
  143.     GET_UINT32_LE( X[14], data, 56 );
  144.     GET_UINT32_LE( X[15], data, 60 );
  145.  
  146.     A = Ap = ctx->state[0];
  147.     B = Bp = ctx->state[1];
  148.     C = Cp = ctx->state[2];
  149.     D = Dp = ctx->state[3];
  150.     E = Ep = ctx->state[4];
  151.  
  152. #define F1( x, y, z )   ( (x) ^ (y) ^ (z) )
  153. #define F2( x, y, z )   ( ( (x) & (y) ) | ( ~(x) & (z) ) )
  154. #define F3( x, y, z )   ( ( (x) | ~(y) ) ^ (z) )
  155. #define F4( x, y, z )   ( ( (x) & (z) ) | ( (y) & ~(z) ) )
  156. #define F5( x, y, z )   ( (x) ^ ( (y) | ~(z) ) )
  157.  
  158. #define S( x, n ) ( ( (x) << (n) ) | ( (x) >> (32 - (n)) ) )
  159.  
  160. #define P( a, b, c, d, e, r, s, f, k )                \
  161.     do                                                \
  162.     {                                                 \
  163.         (a) += f( (b), (c), (d) ) + X[r] + (k);       \
  164.         (a) = S( (a), (s) ) + (e);                    \
  165.         (c) = S( (c), 10 );                           \
  166.     } while( 0 )
  167.  
  168. #define P2( a, b, c, d, e, r, s, rp, sp )                               \
  169.     do                                                                  \
  170.     {                                                                   \
  171.         P( (a), (b), (c), (d), (e), (r), (s), F, K );                   \
  172.         P( a ## p, b ## p, c ## p, d ## p, e ## p,                      \
  173.            (rp), (sp), Fp, Kp );                                        \
  174.     } while( 0 )
  175.  
  176. #define F   F1
  177. #define K   0x00000000
  178. #define Fp  F5
  179. #define Kp  0x50A28BE6
  180.     P2( A, B, C, D, E,  0, 11,  5,  8 );
  181.     P2( E, A, B, C, D,  1, 14, 14,  9 );
  182.     P2( D, E, A, B, C,  2, 15,  7,  9 );
  183.     P2( C, D, E, A, B,  3, 12,  0, 11 );
  184.     P2( B, C, D, E, A,  4,  5,  9, 13 );
  185.     P2( A, B, C, D, E,  5,  8,  2, 15 );
  186.     P2( E, A, B, C, D,  6,  7, 11, 15 );
  187.     P2( D, E, A, B, C,  7,  9,  4,  5 );
  188.     P2( C, D, E, A, B,  8, 11, 13,  7 );
  189.     P2( B, C, D, E, A,  9, 13,  6,  7 );
  190.     P2( A, B, C, D, E, 10, 14, 15,  8 );
  191.     P2( E, A, B, C, D, 11, 15,  8, 11 );
  192.     P2( D, E, A, B, C, 12,  6,  1, 14 );
  193.     P2( C, D, E, A, B, 13,  7, 10, 14 );
  194.     P2( B, C, D, E, A, 14,  9,  3, 12 );
  195.     P2( A, B, C, D, E, 15,  8, 12,  6 );
  196. #undef F
  197. #undef K
  198. #undef Fp
  199. #undef Kp
  200.  
  201. #define F   F2
  202. #define K   0x5A827999
  203. #define Fp  F4
  204. #define Kp  0x5C4DD124
  205.     P2( E, A, B, C, D,  7,  7,  6,  9 );
  206.     P2( D, E, A, B, C,  4,  6, 11, 13 );
  207.     P2( C, D, E, A, B, 13,  8,  3, 15 );
  208.     P2( B, C, D, E, A,  1, 13,  7,  7 );
  209.     P2( A, B, C, D, E, 10, 11,  0, 12 );
  210.     P2( E, A, B, C, D,  6,  9, 13,  8 );
  211.     P2( D, E, A, B, C, 15,  7,  5,  9 );
  212.     P2( C, D, E, A, B,  3, 15, 10, 11 );
  213.     P2( B, C, D, E, A, 12,  7, 14,  7 );
  214.     P2( A, B, C, D, E,  0, 12, 15,  7 );
  215.     P2( E, A, B, C, D,  9, 15,  8, 12 );
  216.     P2( D, E, A, B, C,  5,  9, 12,  7 );
  217.     P2( C, D, E, A, B,  2, 11,  4,  6 );
  218.     P2( B, C, D, E, A, 14,  7,  9, 15 );
  219.     P2( A, B, C, D, E, 11, 13,  1, 13 );
  220.     P2( E, A, B, C, D,  8, 12,  2, 11 );
  221. #undef F
  222. #undef K
  223. #undef Fp
  224. #undef Kp
  225.  
  226. #define F   F3
  227. #define K   0x6ED9EBA1
  228. #define Fp  F3
  229. #define Kp  0x6D703EF3
  230.     P2( D, E, A, B, C,  3, 11, 15,  9 );
  231.     P2( C, D, E, A, B, 10, 13,  5,  7 );
  232.     P2( B, C, D, E, A, 14,  6,  1, 15 );
  233.     P2( A, B, C, D, E,  4,  7,  3, 11 );
  234.     P2( E, A, B, C, D,  9, 14,  7,  8 );
  235.     P2( D, E, A, B, C, 15,  9, 14,  6 );
  236.     P2( C, D, E, A, B,  8, 13,  6,  6 );
  237.     P2( B, C, D, E, A,  1, 15,  9, 14 );
  238.     P2( A, B, C, D, E,  2, 14, 11, 12 );
  239.     P2( E, A, B, C, D,  7,  8,  8, 13 );
  240.     P2( D, E, A, B, C,  0, 13, 12,  5 );
  241.     P2( C, D, E, A, B,  6,  6,  2, 14 );
  242.     P2( B, C, D, E, A, 13,  5, 10, 13 );
  243.     P2( A, B, C, D, E, 11, 12,  0, 13 );
  244.     P2( E, A, B, C, D,  5,  7,  4,  7 );
  245.     P2( D, E, A, B, C, 12,  5, 13,  5 );
  246. #undef F
  247. #undef K
  248. #undef Fp
  249. #undef Kp
  250.  
  251. #define F   F4
  252. #define K   0x8F1BBCDC
  253. #define Fp  F2
  254. #define Kp  0x7A6D76E9
  255.     P2( C, D, E, A, B,  1, 11,  8, 15 );
  256.     P2( B, C, D, E, A,  9, 12,  6,  5 );
  257.     P2( A, B, C, D, E, 11, 14,  4,  8 );
  258.     P2( E, A, B, C, D, 10, 15,  1, 11 );
  259.     P2( D, E, A, B, C,  0, 14,  3, 14 );
  260.     P2( C, D, E, A, B,  8, 15, 11, 14 );
  261.     P2( B, C, D, E, A, 12,  9, 15,  6 );
  262.     P2( A, B, C, D, E,  4,  8,  0, 14 );
  263.     P2( E, A, B, C, D, 13,  9,  5,  6 );
  264.     P2( D, E, A, B, C,  3, 14, 12,  9 );
  265.     P2( C, D, E, A, B,  7,  5,  2, 12 );
  266.     P2( B, C, D, E, A, 15,  6, 13,  9 );
  267.     P2( A, B, C, D, E, 14,  8,  9, 12 );
  268.     P2( E, A, B, C, D,  5,  6,  7,  5 );
  269.     P2( D, E, A, B, C,  6,  5, 10, 15 );
  270.     P2( C, D, E, A, B,  2, 12, 14,  8 );
  271. #undef F
  272. #undef K
  273. #undef Fp
  274. #undef Kp
  275.  
  276. #define F   F5
  277. #define K   0xA953FD4E
  278. #define Fp  F1
  279. #define Kp  0x00000000
  280.     P2( B, C, D, E, A,  4,  9, 12,  8 );
  281.     P2( A, B, C, D, E,  0, 15, 15,  5 );
  282.     P2( E, A, B, C, D,  5,  5, 10, 12 );
  283.     P2( D, E, A, B, C,  9, 11,  4,  9 );
  284.     P2( C, D, E, A, B,  7,  6,  1, 12 );
  285.     P2( B, C, D, E, A, 12,  8,  5,  5 );
  286.     P2( A, B, C, D, E,  2, 13,  8, 14 );
  287.     P2( E, A, B, C, D, 10, 12,  7,  6 );
  288.     P2( D, E, A, B, C, 14,  5,  6,  8 );
  289.     P2( C, D, E, A, B,  1, 12,  2, 13 );
  290.     P2( B, C, D, E, A,  3, 13, 13,  6 );
  291.     P2( A, B, C, D, E,  8, 14, 14,  5 );
  292.     P2( E, A, B, C, D, 11, 11,  0, 15 );
  293.     P2( D, E, A, B, C,  6,  8,  3, 13 );
  294.     P2( C, D, E, A, B, 15,  5,  9, 11 );
  295.     P2( B, C, D, E, A, 13,  6, 11, 11 );
  296. #undef F
  297. #undef K
  298. #undef Fp
  299. #undef Kp
  300.  
  301.     C             = ctx->state[1] + C + Dp;
  302.     ctx->state[1] = ctx->state[2] + D + Ep;
  303.     ctx->state[2] = ctx->state[3] + E + Ap;
  304.     ctx->state[3] = ctx->state[4] + A + Bp;
  305.     ctx->state[4] = ctx->state[0] + B + Cp;
  306.     ctx->state[0] = C;
  307.  
  308.     return( 0 );
  309. }
  310.  
  311. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  312. void mbedtls_ripemd160_process( mbedtls_ripemd160_context *ctx,
  313.                                 const unsigned char data[64] )
  314. {
  315.     mbedtls_internal_ripemd160_process( ctx, data );
  316. }
  317. #endif
  318. #endif /* !MBEDTLS_RIPEMD160_PROCESS_ALT */
  319.  
  320. /*
  321.  * RIPEMD-160 process buffer
  322.  */
  323. int mbedtls_ripemd160_update_ret( mbedtls_ripemd160_context *ctx,
  324.                                   const unsigned char *input,
  325.                                   size_t ilen )
  326. {
  327.     int ret;
  328.     size_t fill;
  329.     uint32_t left;
  330.  
  331.     if( ilen == 0 )
  332.         return( 0 );
  333.  
  334.     left = ctx->total[0] & 0x3F;
  335.     fill = 64 - left;
  336.  
  337.     ctx->total[0] += (uint32_t) ilen;
  338.     ctx->total[0] &= 0xFFFFFFFF;
  339.  
  340.     if( ctx->total[0] < (uint32_t) ilen )
  341.         ctx->total[1]++;
  342.  
  343.     if( left && ilen >= fill )
  344.     {
  345.         memcpy( (void *) (ctx->buffer + left), input, fill );
  346.  
  347.         if( ( ret = mbedtls_internal_ripemd160_process( ctx, ctx->buffer ) ) != 0 )
  348.             return( ret );
  349.  
  350.         input += fill;
  351.         ilen  -= fill;
  352.         left = 0;
  353.     }
  354.  
  355.     while( ilen >= 64 )
  356.     {
  357.         if( ( ret = mbedtls_internal_ripemd160_process( ctx, input ) ) != 0 )
  358.             return( ret );
  359.  
  360.         input += 64;
  361.         ilen  -= 64;
  362.     }
  363.  
  364.     if( ilen > 0 )
  365.     {
  366.         memcpy( (void *) (ctx->buffer + left), input, ilen );
  367.     }
  368.  
  369.     return( 0 );
  370. }
  371.  
  372. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  373. void mbedtls_ripemd160_update( mbedtls_ripemd160_context *ctx,
  374.                                const unsigned char *input,
  375.                                size_t ilen )
  376. {
  377.     mbedtls_ripemd160_update_ret( ctx, input, ilen );
  378. }
  379. #endif
  380.  
  381. static const unsigned char ripemd160_padding[64] =
  382. {
  383.  0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  384.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  385.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  386.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  387. };
  388.  
  389. /*
  390.  * RIPEMD-160 final digest
  391.  */
  392. int mbedtls_ripemd160_finish_ret( mbedtls_ripemd160_context *ctx,
  393.                                   unsigned char output[20] )
  394. {
  395.     int ret;
  396.     uint32_t last, padn;
  397.     uint32_t high, low;
  398.     unsigned char msglen[8];
  399.  
  400.     high = ( ctx->total[0] >> 29 )
  401.          | ( ctx->total[1] <<  3 );
  402.     low  = ( ctx->total[0] <<  3 );
  403.  
  404.     PUT_UINT32_LE( low,  msglen, 0 );
  405.     PUT_UINT32_LE( high, msglen, 4 );
  406.  
  407.     last = ctx->total[0] & 0x3F;
  408.     padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
  409.  
  410.     ret = mbedtls_ripemd160_update_ret( ctx, ripemd160_padding, padn );
  411.     if( ret != 0 )
  412.         return( ret );
  413.  
  414.     ret = mbedtls_ripemd160_update_ret( ctx, msglen, 8 );
  415.     if( ret != 0 )
  416.         return( ret );
  417.  
  418.     PUT_UINT32_LE( ctx->state[0], output,  0 );
  419.     PUT_UINT32_LE( ctx->state[1], output,  4 );
  420.     PUT_UINT32_LE( ctx->state[2], output,  8 );
  421.     PUT_UINT32_LE( ctx->state[3], output, 12 );
  422.     PUT_UINT32_LE( ctx->state[4], output, 16 );
  423.  
  424.     return( 0 );
  425. }
  426.  
  427. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  428. void mbedtls_ripemd160_finish( mbedtls_ripemd160_context *ctx,
  429.                                unsigned char output[20] )
  430. {
  431.     mbedtls_ripemd160_finish_ret( ctx, output );
  432. }
  433. #endif
  434.  
  435. #endif /* ! MBEDTLS_RIPEMD160_ALT */
  436.  
  437. /*
  438.  * output = RIPEMD-160( input buffer )
  439.  */
  440. int mbedtls_ripemd160_ret( const unsigned char *input,
  441.                            size_t ilen,
  442.                            unsigned char output[20] )
  443. {
  444.     int ret;
  445.     mbedtls_ripemd160_context ctx;
  446.  
  447.     mbedtls_ripemd160_init( &ctx );
  448.  
  449.     if( ( ret = mbedtls_ripemd160_starts_ret( &ctx ) ) != 0 )
  450.         goto exit;
  451.  
  452.     if( ( ret = mbedtls_ripemd160_update_ret( &ctx, input, ilen ) ) != 0 )
  453.         goto exit;
  454.  
  455.     if( ( ret = mbedtls_ripemd160_finish_ret( &ctx, output ) ) != 0 )
  456.         goto exit;
  457.  
  458. exit:
  459.     mbedtls_ripemd160_free( &ctx );
  460.  
  461.     return( ret );
  462. }
  463.  
  464. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  465. void mbedtls_ripemd160( const unsigned char *input,
  466.                         size_t ilen,
  467.                         unsigned char output[20] )
  468. {
  469.     mbedtls_ripemd160_ret( input, ilen, output );
  470. }
  471. #endif
  472.  
  473. #if defined(MBEDTLS_SELF_TEST)
  474. /*
  475.  * Test vectors from the RIPEMD-160 paper and
  476.  * http://homes.esat.kuleuven.be/~bosselae/mbedtls_ripemd160.html#HMAC
  477.  */
  478. #define TESTS   8
  479. static const unsigned char ripemd160_test_str[TESTS][81] =
  480. {
  481.     { "" },
  482.     { "a" },
  483.     { "abc" },
  484.     { "message digest" },
  485.     { "abcdefghijklmnopqrstuvwxyz" },
  486.     { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
  487.     { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
  488.     { "12345678901234567890123456789012345678901234567890123456789012"
  489.       "345678901234567890" },
  490. };
  491.  
  492. static const size_t ripemd160_test_strlen[TESTS] =
  493. {
  494.     0, 1, 3, 14, 26, 56, 62, 80
  495. };
  496.  
  497. static const unsigned char ripemd160_test_md[TESTS][20] =
  498. {
  499.     { 0x9c, 0x11, 0x85, 0xa5, 0xc5, 0xe9, 0xfc, 0x54, 0x61, 0x28,
  500.       0x08, 0x97, 0x7e, 0xe8, 0xf5, 0x48, 0xb2, 0x25, 0x8d, 0x31 },
  501.     { 0x0b, 0xdc, 0x9d, 0x2d, 0x25, 0x6b, 0x3e, 0xe9, 0xda, 0xae,
  502.       0x34, 0x7b, 0xe6, 0xf4, 0xdc, 0x83, 0x5a, 0x46, 0x7f, 0xfe },
  503.     { 0x8e, 0xb2, 0x08, 0xf7, 0xe0, 0x5d, 0x98, 0x7a, 0x9b, 0x04,
  504.       0x4a, 0x8e, 0x98, 0xc6, 0xb0, 0x87, 0xf1, 0x5a, 0x0b, 0xfc },
  505.     { 0x5d, 0x06, 0x89, 0xef, 0x49, 0xd2, 0xfa, 0xe5, 0x72, 0xb8,
  506.       0x81, 0xb1, 0x23, 0xa8, 0x5f, 0xfa, 0x21, 0x59, 0x5f, 0x36 },
  507.     { 0xf7, 0x1c, 0x27, 0x10, 0x9c, 0x69, 0x2c, 0x1b, 0x56, 0xbb,
  508.       0xdc, 0xeb, 0x5b, 0x9d, 0x28, 0x65, 0xb3, 0x70, 0x8d, 0xbc },
  509.     { 0x12, 0xa0, 0x53, 0x38, 0x4a, 0x9c, 0x0c, 0x88, 0xe4, 0x05,
  510.       0xa0, 0x6c, 0x27, 0xdc, 0xf4, 0x9a, 0xda, 0x62, 0xeb, 0x2b },
  511.     { 0xb0, 0xe2, 0x0b, 0x6e, 0x31, 0x16, 0x64, 0x02, 0x86, 0xed,
  512.       0x3a, 0x87, 0xa5, 0x71, 0x30, 0x79, 0xb2, 0x1f, 0x51, 0x89 },
  513.     { 0x9b, 0x75, 0x2e, 0x45, 0x57, 0x3d, 0x4b, 0x39, 0xf4, 0xdb,
  514.       0xd3, 0x32, 0x3c, 0xab, 0x82, 0xbf, 0x63, 0x32, 0x6b, 0xfb },
  515. };
  516.  
  517. /*
  518.  * Checkup routine
  519.  */
  520. int mbedtls_ripemd160_self_test( int verbose )
  521. {
  522.     int i, ret = 0;
  523.     unsigned char output[20];
  524.  
  525.     memset( output, 0, sizeof output );
  526.  
  527.     for( i = 0; i < TESTS; i++ )
  528.     {
  529.         if( verbose != 0 )
  530.             mbedtls_printf( "  RIPEMD-160 test #%d: ", i + 1 );
  531.  
  532.         ret = mbedtls_ripemd160_ret( ripemd160_test_str[i],
  533.                                      ripemd160_test_strlen[i], output );
  534.         if( ret != 0 )
  535.             goto fail;
  536.  
  537.         if( memcmp( output, ripemd160_test_md[i], 20 ) != 0 )
  538.         {
  539.             ret = 1;
  540.             goto fail;
  541.         }
  542.  
  543.         if( verbose != 0 )
  544.             mbedtls_printf( "passed\n" );
  545.     }
  546.  
  547.     if( verbose != 0 )
  548.         mbedtls_printf( "\n" );
  549.  
  550.     return( 0 );
  551.  
  552. fail:
  553.     if( verbose != 0 )
  554.         mbedtls_printf( "failed\n" );
  555.  
  556.     return( ret );
  557. }
  558.  
  559. #endif /* MBEDTLS_SELF_TEST */
  560.  
  561. #endif /* MBEDTLS_RIPEMD160_C */
  562.