mbed TLS library
Dependents: HTTPClient-SSL WS_SERVER
ripemd160.c
00001 /* 00002 * RIPE MD-160 implementation 00003 * 00004 * Copyright (C) 2014-2014, ARM Limited, All Rights Reserved 00005 * 00006 * This file is part of mbed TLS (https://tls.mbed.org) 00007 * 00008 * This program is free software; you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License as published by 00010 * the Free Software Foundation; either version 2 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * This program is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License along 00019 * with this program; if not, write to the Free Software Foundation, Inc., 00020 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00021 */ 00022 00023 /* 00024 * The RIPEMD-160 algorithm was designed by RIPE in 1996 00025 * http://homes.esat.kuleuven.be/~bosselae/ripemd160.html 00026 * http://ehash.iaik.tugraz.at/wiki/RIPEMD-160 00027 */ 00028 00029 #if !defined(POLARSSL_CONFIG_FILE) 00030 #include "polarssl/config.h" 00031 #else 00032 #include POLARSSL_CONFIG_FILE 00033 #endif 00034 00035 #if defined(POLARSSL_RIPEMD160_C) 00036 00037 #include "polarssl/ripemd160.h" 00038 00039 #include <string.h> 00040 00041 #if defined(POLARSSL_FS_IO) 00042 #include <stdio.h> 00043 #endif 00044 00045 #if defined(POLARSSL_SELF_TEST) 00046 #if defined(POLARSSL_PLATFORM_C) 00047 #include "polarssl/platform.h" 00048 #else 00049 #include <stdio.h> 00050 #define polarssl_printf printf 00051 #endif /* POLARSSL_PLATFORM_C */ 00052 #endif /* POLARSSL_SELF_TEST */ 00053 00054 /* 00055 * 32-bit integer manipulation macros (little endian) 00056 */ 00057 #ifndef GET_UINT32_LE 00058 #define GET_UINT32_LE(n,b,i) \ 00059 { \ 00060 (n) = ( (uint32_t) (b)[(i) ] ) \ 00061 | ( (uint32_t) (b)[(i) + 1] << 8 ) \ 00062 | ( (uint32_t) (b)[(i) + 2] << 16 ) \ 00063 | ( (uint32_t) (b)[(i) + 3] << 24 ); \ 00064 } 00065 #endif 00066 00067 #ifndef PUT_UINT32_LE 00068 #define PUT_UINT32_LE(n,b,i) \ 00069 { \ 00070 (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ 00071 (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ 00072 (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ 00073 (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ 00074 } 00075 #endif 00076 00077 /* Implementation that should never be optimized out by the compiler */ 00078 static void polarssl_zeroize( void *v, size_t n ) { 00079 volatile unsigned char *p = v; while( n-- ) *p++ = 0; 00080 } 00081 00082 void ripemd160_init( ripemd160_context *ctx ) 00083 { 00084 memset( ctx, 0, sizeof( ripemd160_context ) ); 00085 } 00086 00087 void ripemd160_free( ripemd160_context *ctx ) 00088 { 00089 if( ctx == NULL ) 00090 return; 00091 00092 polarssl_zeroize( ctx, sizeof( ripemd160_context ) ); 00093 } 00094 00095 /* 00096 * RIPEMD-160 context setup 00097 */ 00098 void ripemd160_starts( ripemd160_context *ctx ) 00099 { 00100 ctx->total [0] = 0; 00101 ctx->total [1] = 0; 00102 00103 ctx->state [0] = 0x67452301; 00104 ctx->state [1] = 0xEFCDAB89; 00105 ctx->state [2] = 0x98BADCFE; 00106 ctx->state [3] = 0x10325476; 00107 ctx->state [4] = 0xC3D2E1F0; 00108 } 00109 00110 /* 00111 * Process one block 00112 */ 00113 void ripemd160_process( ripemd160_context *ctx, const unsigned char data[64] ) 00114 { 00115 uint32_t A, B, C, D, E, Ap, Bp, Cp, Dp, Ep, X[16]; 00116 00117 GET_UINT32_LE( X[ 0], data, 0 ); 00118 GET_UINT32_LE( X[ 1], data, 4 ); 00119 GET_UINT32_LE( X[ 2], data, 8 ); 00120 GET_UINT32_LE( X[ 3], data, 12 ); 00121 GET_UINT32_LE( X[ 4], data, 16 ); 00122 GET_UINT32_LE( X[ 5], data, 20 ); 00123 GET_UINT32_LE( X[ 6], data, 24 ); 00124 GET_UINT32_LE( X[ 7], data, 28 ); 00125 GET_UINT32_LE( X[ 8], data, 32 ); 00126 GET_UINT32_LE( X[ 9], data, 36 ); 00127 GET_UINT32_LE( X[10], data, 40 ); 00128 GET_UINT32_LE( X[11], data, 44 ); 00129 GET_UINT32_LE( X[12], data, 48 ); 00130 GET_UINT32_LE( X[13], data, 52 ); 00131 GET_UINT32_LE( X[14], data, 56 ); 00132 GET_UINT32_LE( X[15], data, 60 ); 00133 00134 A = Ap = ctx->state [0]; 00135 B = Bp = ctx->state [1]; 00136 C = Cp = ctx->state [2]; 00137 D = Dp = ctx->state [3]; 00138 E = Ep = ctx->state [4]; 00139 00140 #define F1( x, y, z ) ( x ^ y ^ z ) 00141 #define F2( x, y, z ) ( ( x & y ) | ( ~x & z ) ) 00142 #define F3( x, y, z ) ( ( x | ~y ) ^ z ) 00143 #define F4( x, y, z ) ( ( x & z ) | ( y & ~z ) ) 00144 #define F5( x, y, z ) ( x ^ ( y | ~z ) ) 00145 00146 #define S( x, n ) ( ( x << n ) | ( x >> (32 - n) ) ) 00147 00148 #define P( a, b, c, d, e, r, s, f, k ) \ 00149 a += f( b, c, d ) + X[r] + k; \ 00150 a = S( a, s ) + e; \ 00151 c = S( c, 10 ); 00152 00153 #define P2( a, b, c, d, e, r, s, rp, sp ) \ 00154 P( a, b, c, d, e, r, s, F, K ); \ 00155 P( a ## p, b ## p, c ## p, d ## p, e ## p, rp, sp, Fp, Kp ); 00156 00157 #define F F1 00158 #define K 0x00000000 00159 #define Fp F5 00160 #define Kp 0x50A28BE6 00161 P2( A, B, C, D, E, 0, 11, 5, 8 ); 00162 P2( E, A, B, C, D, 1, 14, 14, 9 ); 00163 P2( D, E, A, B, C, 2, 15, 7, 9 ); 00164 P2( C, D, E, A, B, 3, 12, 0, 11 ); 00165 P2( B, C, D, E, A, 4, 5, 9, 13 ); 00166 P2( A, B, C, D, E, 5, 8, 2, 15 ); 00167 P2( E, A, B, C, D, 6, 7, 11, 15 ); 00168 P2( D, E, A, B, C, 7, 9, 4, 5 ); 00169 P2( C, D, E, A, B, 8, 11, 13, 7 ); 00170 P2( B, C, D, E, A, 9, 13, 6, 7 ); 00171 P2( A, B, C, D, E, 10, 14, 15, 8 ); 00172 P2( E, A, B, C, D, 11, 15, 8, 11 ); 00173 P2( D, E, A, B, C, 12, 6, 1, 14 ); 00174 P2( C, D, E, A, B, 13, 7, 10, 14 ); 00175 P2( B, C, D, E, A, 14, 9, 3, 12 ); 00176 P2( A, B, C, D, E, 15, 8, 12, 6 ); 00177 #undef F 00178 #undef K 00179 #undef Fp 00180 #undef Kp 00181 00182 #define F F2 00183 #define K 0x5A827999 00184 #define Fp F4 00185 #define Kp 0x5C4DD124 00186 P2( E, A, B, C, D, 7, 7, 6, 9 ); 00187 P2( D, E, A, B, C, 4, 6, 11, 13 ); 00188 P2( C, D, E, A, B, 13, 8, 3, 15 ); 00189 P2( B, C, D, E, A, 1, 13, 7, 7 ); 00190 P2( A, B, C, D, E, 10, 11, 0, 12 ); 00191 P2( E, A, B, C, D, 6, 9, 13, 8 ); 00192 P2( D, E, A, B, C, 15, 7, 5, 9 ); 00193 P2( C, D, E, A, B, 3, 15, 10, 11 ); 00194 P2( B, C, D, E, A, 12, 7, 14, 7 ); 00195 P2( A, B, C, D, E, 0, 12, 15, 7 ); 00196 P2( E, A, B, C, D, 9, 15, 8, 12 ); 00197 P2( D, E, A, B, C, 5, 9, 12, 7 ); 00198 P2( C, D, E, A, B, 2, 11, 4, 6 ); 00199 P2( B, C, D, E, A, 14, 7, 9, 15 ); 00200 P2( A, B, C, D, E, 11, 13, 1, 13 ); 00201 P2( E, A, B, C, D, 8, 12, 2, 11 ); 00202 #undef F 00203 #undef K 00204 #undef Fp 00205 #undef Kp 00206 00207 #define F F3 00208 #define K 0x6ED9EBA1 00209 #define Fp F3 00210 #define Kp 0x6D703EF3 00211 P2( D, E, A, B, C, 3, 11, 15, 9 ); 00212 P2( C, D, E, A, B, 10, 13, 5, 7 ); 00213 P2( B, C, D, E, A, 14, 6, 1, 15 ); 00214 P2( A, B, C, D, E, 4, 7, 3, 11 ); 00215 P2( E, A, B, C, D, 9, 14, 7, 8 ); 00216 P2( D, E, A, B, C, 15, 9, 14, 6 ); 00217 P2( C, D, E, A, B, 8, 13, 6, 6 ); 00218 P2( B, C, D, E, A, 1, 15, 9, 14 ); 00219 P2( A, B, C, D, E, 2, 14, 11, 12 ); 00220 P2( E, A, B, C, D, 7, 8, 8, 13 ); 00221 P2( D, E, A, B, C, 0, 13, 12, 5 ); 00222 P2( C, D, E, A, B, 6, 6, 2, 14 ); 00223 P2( B, C, D, E, A, 13, 5, 10, 13 ); 00224 P2( A, B, C, D, E, 11, 12, 0, 13 ); 00225 P2( E, A, B, C, D, 5, 7, 4, 7 ); 00226 P2( D, E, A, B, C, 12, 5, 13, 5 ); 00227 #undef F 00228 #undef K 00229 #undef Fp 00230 #undef Kp 00231 00232 #define F F4 00233 #define K 0x8F1BBCDC 00234 #define Fp F2 00235 #define Kp 0x7A6D76E9 00236 P2( C, D, E, A, B, 1, 11, 8, 15 ); 00237 P2( B, C, D, E, A, 9, 12, 6, 5 ); 00238 P2( A, B, C, D, E, 11, 14, 4, 8 ); 00239 P2( E, A, B, C, D, 10, 15, 1, 11 ); 00240 P2( D, E, A, B, C, 0, 14, 3, 14 ); 00241 P2( C, D, E, A, B, 8, 15, 11, 14 ); 00242 P2( B, C, D, E, A, 12, 9, 15, 6 ); 00243 P2( A, B, C, D, E, 4, 8, 0, 14 ); 00244 P2( E, A, B, C, D, 13, 9, 5, 6 ); 00245 P2( D, E, A, B, C, 3, 14, 12, 9 ); 00246 P2( C, D, E, A, B, 7, 5, 2, 12 ); 00247 P2( B, C, D, E, A, 15, 6, 13, 9 ); 00248 P2( A, B, C, D, E, 14, 8, 9, 12 ); 00249 P2( E, A, B, C, D, 5, 6, 7, 5 ); 00250 P2( D, E, A, B, C, 6, 5, 10, 15 ); 00251 P2( C, D, E, A, B, 2, 12, 14, 8 ); 00252 #undef F 00253 #undef K 00254 #undef Fp 00255 #undef Kp 00256 00257 #define F F5 00258 #define K 0xA953FD4E 00259 #define Fp F1 00260 #define Kp 0x00000000 00261 P2( B, C, D, E, A, 4, 9, 12, 8 ); 00262 P2( A, B, C, D, E, 0, 15, 15, 5 ); 00263 P2( E, A, B, C, D, 5, 5, 10, 12 ); 00264 P2( D, E, A, B, C, 9, 11, 4, 9 ); 00265 P2( C, D, E, A, B, 7, 6, 1, 12 ); 00266 P2( B, C, D, E, A, 12, 8, 5, 5 ); 00267 P2( A, B, C, D, E, 2, 13, 8, 14 ); 00268 P2( E, A, B, C, D, 10, 12, 7, 6 ); 00269 P2( D, E, A, B, C, 14, 5, 6, 8 ); 00270 P2( C, D, E, A, B, 1, 12, 2, 13 ); 00271 P2( B, C, D, E, A, 3, 13, 13, 6 ); 00272 P2( A, B, C, D, E, 8, 14, 14, 5 ); 00273 P2( E, A, B, C, D, 11, 11, 0, 15 ); 00274 P2( D, E, A, B, C, 6, 8, 3, 13 ); 00275 P2( C, D, E, A, B, 15, 5, 9, 11 ); 00276 P2( B, C, D, E, A, 13, 6, 11, 11 ); 00277 #undef F 00278 #undef K 00279 #undef Fp 00280 #undef Kp 00281 00282 C = ctx->state [1] + C + Dp; 00283 ctx->state [1] = ctx->state [2] + D + Ep; 00284 ctx->state [2] = ctx->state [3] + E + Ap; 00285 ctx->state [3] = ctx->state [4] + A + Bp; 00286 ctx->state [4] = ctx->state [0] + B + Cp; 00287 ctx->state [0] = C; 00288 } 00289 00290 /* 00291 * RIPEMD-160 process buffer 00292 */ 00293 void ripemd160_update( ripemd160_context *ctx, 00294 const unsigned char *input, size_t ilen ) 00295 { 00296 size_t fill; 00297 uint32_t left; 00298 00299 if( ilen == 0 ) 00300 return; 00301 00302 left = ctx->total [0] & 0x3F; 00303 fill = 64 - left; 00304 00305 ctx->total [0] += (uint32_t) ilen; 00306 ctx->total [0] &= 0xFFFFFFFF; 00307 00308 if( ctx->total [0] < (uint32_t) ilen ) 00309 ctx->total [1]++; 00310 00311 if( left && ilen >= fill ) 00312 { 00313 memcpy( (void *) (ctx->buffer + left), input, fill ); 00314 ripemd160_process( ctx, ctx->buffer ); 00315 input += fill; 00316 ilen -= fill; 00317 left = 0; 00318 } 00319 00320 while( ilen >= 64 ) 00321 { 00322 ripemd160_process( ctx, input ); 00323 input += 64; 00324 ilen -= 64; 00325 } 00326 00327 if( ilen > 0 ) 00328 { 00329 memcpy( (void *) (ctx->buffer + left), input, ilen ); 00330 } 00331 } 00332 00333 static const unsigned char ripemd160_padding[64] = 00334 { 00335 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00336 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00337 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00338 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 00339 }; 00340 00341 /* 00342 * RIPEMD-160 final digest 00343 */ 00344 void ripemd160_finish( ripemd160_context *ctx, unsigned char output[20] ) 00345 { 00346 uint32_t last, padn; 00347 uint32_t high, low; 00348 unsigned char msglen[8]; 00349 00350 high = ( ctx->total [0] >> 29 ) 00351 | ( ctx->total [1] << 3 ); 00352 low = ( ctx->total [0] << 3 ); 00353 00354 PUT_UINT32_LE( low, msglen, 0 ); 00355 PUT_UINT32_LE( high, msglen, 4 ); 00356 00357 last = ctx->total [0] & 0x3F; 00358 padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); 00359 00360 ripemd160_update( ctx, ripemd160_padding, padn ); 00361 ripemd160_update( ctx, msglen, 8 ); 00362 00363 PUT_UINT32_LE( ctx->state [0], output, 0 ); 00364 PUT_UINT32_LE( ctx->state [1], output, 4 ); 00365 PUT_UINT32_LE( ctx->state [2], output, 8 ); 00366 PUT_UINT32_LE( ctx->state [3], output, 12 ); 00367 PUT_UINT32_LE( ctx->state [4], output, 16 ); 00368 } 00369 00370 /* 00371 * output = RIPEMD-160( input buffer ) 00372 */ 00373 void ripemd160( const unsigned char *input, size_t ilen, 00374 unsigned char output[20] ) 00375 { 00376 ripemd160_context ctx; 00377 00378 ripemd160_init( &ctx ); 00379 ripemd160_starts( &ctx ); 00380 ripemd160_update( &ctx, input, ilen ); 00381 ripemd160_finish( &ctx, output ); 00382 ripemd160_free( &ctx ); 00383 } 00384 00385 #if defined(POLARSSL_FS_IO) 00386 /* 00387 * output = RIPEMD-160( file contents ) 00388 */ 00389 int ripemd160_file( const char *path, unsigned char output[20] ) 00390 { 00391 FILE *f; 00392 size_t n; 00393 ripemd160_context ctx; 00394 unsigned char buf[1024]; 00395 00396 if( ( f = fopen( path, "rb" ) ) == NULL ) 00397 return( POLARSSL_ERR_RIPEMD160_FILE_IO_ERROR ); 00398 00399 ripemd160_init( &ctx ); 00400 ripemd160_starts( &ctx ); 00401 00402 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) 00403 ripemd160_update( &ctx, buf, n ); 00404 00405 ripemd160_finish( &ctx, output ); 00406 ripemd160_free( &ctx ); 00407 00408 if( ferror( f ) != 0 ) 00409 { 00410 fclose( f ); 00411 return( POLARSSL_ERR_RIPEMD160_FILE_IO_ERROR ); 00412 } 00413 00414 fclose( f ); 00415 return( 0 ); 00416 } 00417 #endif /* POLARSSL_FS_IO */ 00418 00419 /* 00420 * RIPEMD-160 HMAC context setup 00421 */ 00422 void ripemd160_hmac_starts( ripemd160_context *ctx, 00423 const unsigned char *key, size_t keylen ) 00424 { 00425 size_t i; 00426 unsigned char sum[20]; 00427 00428 if( keylen > 64 ) 00429 { 00430 ripemd160( key, keylen, sum ); 00431 keylen = 20; 00432 key = sum; 00433 } 00434 00435 memset( ctx->ipad , 0x36, 64 ); 00436 memset( ctx->opad , 0x5C, 64 ); 00437 00438 for( i = 0; i < keylen; i++ ) 00439 { 00440 ctx->ipad [i] = (unsigned char)( ctx->ipad [i] ^ key[i] ); 00441 ctx->opad [i] = (unsigned char)( ctx->opad [i] ^ key[i] ); 00442 } 00443 00444 ripemd160_starts( ctx ); 00445 ripemd160_update( ctx, ctx->ipad , 64 ); 00446 00447 polarssl_zeroize( sum, sizeof( sum ) ); 00448 } 00449 00450 /* 00451 * RIPEMD-160 HMAC process buffer 00452 */ 00453 void ripemd160_hmac_update( ripemd160_context *ctx, 00454 const unsigned char *input, size_t ilen ) 00455 { 00456 ripemd160_update( ctx, input, ilen ); 00457 } 00458 00459 /* 00460 * RIPEMD-160 HMAC final digest 00461 */ 00462 void ripemd160_hmac_finish( ripemd160_context *ctx, unsigned char output[20] ) 00463 { 00464 unsigned char tmpbuf[20]; 00465 00466 ripemd160_finish( ctx, tmpbuf ); 00467 ripemd160_starts( ctx ); 00468 ripemd160_update( ctx, ctx->opad , 64 ); 00469 ripemd160_update( ctx, tmpbuf, 20 ); 00470 ripemd160_finish( ctx, output ); 00471 00472 polarssl_zeroize( tmpbuf, sizeof( tmpbuf ) ); 00473 } 00474 00475 /* 00476 * RIPEMD-160 HMAC context reset 00477 */ 00478 void ripemd160_hmac_reset( ripemd160_context *ctx ) 00479 { 00480 ripemd160_starts( ctx ); 00481 ripemd160_update( ctx, ctx->ipad , 64 ); 00482 } 00483 00484 /* 00485 * output = HMAC-RIPEMD-160( hmac key, input buffer ) 00486 */ 00487 void ripemd160_hmac( const unsigned char *key, size_t keylen, 00488 const unsigned char *input, size_t ilen, 00489 unsigned char output[20] ) 00490 { 00491 ripemd160_context ctx; 00492 00493 ripemd160_init( &ctx ); 00494 ripemd160_hmac_starts( &ctx, key, keylen ); 00495 ripemd160_hmac_update( &ctx, input, ilen ); 00496 ripemd160_hmac_finish( &ctx, output ); 00497 ripemd160_free( &ctx ); 00498 } 00499 00500 00501 #if defined(POLARSSL_SELF_TEST) 00502 /* 00503 * Test vectors from the RIPEMD-160 paper and 00504 * http://homes.esat.kuleuven.be/~bosselae/ripemd160.html#HMAC 00505 */ 00506 #define TESTS 8 00507 #define KEYS 2 00508 static const char *ripemd160_test_input[TESTS] = 00509 { 00510 "", 00511 "a", 00512 "abc", 00513 "message digest", 00514 "abcdefghijklmnopqrstuvwxyz", 00515 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 00516 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 00517 "1234567890123456789012345678901234567890" 00518 "1234567890123456789012345678901234567890", 00519 }; 00520 00521 static const unsigned char ripemd160_test_md[TESTS][20] = 00522 { 00523 { 0x9c, 0x11, 0x85, 0xa5, 0xc5, 0xe9, 0xfc, 0x54, 0x61, 0x28, 00524 0x08, 0x97, 0x7e, 0xe8, 0xf5, 0x48, 0xb2, 0x25, 0x8d, 0x31 }, 00525 { 0x0b, 0xdc, 0x9d, 0x2d, 0x25, 0x6b, 0x3e, 0xe9, 0xda, 0xae, 00526 0x34, 0x7b, 0xe6, 0xf4, 0xdc, 0x83, 0x5a, 0x46, 0x7f, 0xfe }, 00527 { 0x8e, 0xb2, 0x08, 0xf7, 0xe0, 0x5d, 0x98, 0x7a, 0x9b, 0x04, 00528 0x4a, 0x8e, 0x98, 0xc6, 0xb0, 0x87, 0xf1, 0x5a, 0x0b, 0xfc }, 00529 { 0x5d, 0x06, 0x89, 0xef, 0x49, 0xd2, 0xfa, 0xe5, 0x72, 0xb8, 00530 0x81, 0xb1, 0x23, 0xa8, 0x5f, 0xfa, 0x21, 0x59, 0x5f, 0x36 }, 00531 { 0xf7, 0x1c, 0x27, 0x10, 0x9c, 0x69, 0x2c, 0x1b, 0x56, 0xbb, 00532 0xdc, 0xeb, 0x5b, 0x9d, 0x28, 0x65, 0xb3, 0x70, 0x8d, 0xbc }, 00533 { 0x12, 0xa0, 0x53, 0x38, 0x4a, 0x9c, 0x0c, 0x88, 0xe4, 0x05, 00534 0xa0, 0x6c, 0x27, 0xdc, 0xf4, 0x9a, 0xda, 0x62, 0xeb, 0x2b }, 00535 { 0xb0, 0xe2, 0x0b, 0x6e, 0x31, 0x16, 0x64, 0x02, 0x86, 0xed, 00536 0x3a, 0x87, 0xa5, 0x71, 0x30, 0x79, 0xb2, 0x1f, 0x51, 0x89 }, 00537 { 0x9b, 0x75, 0x2e, 0x45, 0x57, 0x3d, 0x4b, 0x39, 0xf4, 0xdb, 00538 0xd3, 0x32, 0x3c, 0xab, 0x82, 0xbf, 0x63, 0x32, 0x6b, 0xfb }, 00539 }; 00540 00541 static const unsigned char ripemd160_test_hmac[KEYS][TESTS][20] = 00542 { 00543 { 00544 { 0xcf, 0x38, 0x76, 0x77, 0xbf, 0xda, 0x84, 0x83, 0xe6, 0x3b, 00545 0x57, 0xe0, 0x6c, 0x3b, 0x5e, 0xcd, 0x8b, 0x7f, 0xc0, 0x55 }, 00546 { 0x0d, 0x35, 0x1d, 0x71, 0xb7, 0x8e, 0x36, 0xdb, 0xb7, 0x39, 00547 0x1c, 0x81, 0x0a, 0x0d, 0x2b, 0x62, 0x40, 0xdd, 0xba, 0xfc }, 00548 { 0xf7, 0xef, 0x28, 0x8c, 0xb1, 0xbb, 0xcc, 0x61, 0x60, 0xd7, 00549 0x65, 0x07, 0xe0, 0xa3, 0xbb, 0xf7, 0x12, 0xfb, 0x67, 0xd6 }, 00550 { 0xf8, 0x36, 0x62, 0xcc, 0x8d, 0x33, 0x9c, 0x22, 0x7e, 0x60, 00551 0x0f, 0xcd, 0x63, 0x6c, 0x57, 0xd2, 0x57, 0x1b, 0x1c, 0x34 }, 00552 { 0x84, 0x3d, 0x1c, 0x4e, 0xb8, 0x80, 0xac, 0x8a, 0xc0, 0xc9, 00553 0xc9, 0x56, 0x96, 0x50, 0x79, 0x57, 0xd0, 0x15, 0x5d, 0xdb }, 00554 { 0x60, 0xf5, 0xef, 0x19, 0x8a, 0x2d, 0xd5, 0x74, 0x55, 0x45, 00555 0xc1, 0xf0, 0xc4, 0x7a, 0xa3, 0xfb, 0x57, 0x76, 0xf8, 0x81 }, 00556 { 0xe4, 0x9c, 0x13, 0x6a, 0x9e, 0x56, 0x27, 0xe0, 0x68, 0x1b, 00557 0x80, 0x8a, 0x3b, 0x97, 0xe6, 0xa6, 0xe6, 0x61, 0xae, 0x79 }, 00558 { 0x31, 0xbe, 0x3c, 0xc9, 0x8c, 0xee, 0x37, 0xb7, 0x9b, 0x06, 00559 0x19, 0xe3, 0xe1, 0xc2, 0xbe, 0x4f, 0x1a, 0xa5, 0x6e, 0x6c }, 00560 }, 00561 { 00562 { 0xfe, 0x69, 0xa6, 0x6c, 0x74, 0x23, 0xee, 0xa9, 0xc8, 0xfa, 00563 0x2e, 0xff, 0x8d, 0x9d, 0xaf, 0xb4, 0xf1, 0x7a, 0x62, 0xf5 }, 00564 { 0x85, 0x74, 0x3e, 0x89, 0x9b, 0xc8, 0x2d, 0xbf, 0xa3, 0x6f, 00565 0xaa, 0xa7, 0xa2, 0x5b, 0x7c, 0xfd, 0x37, 0x24, 0x32, 0xcd }, 00566 { 0x6e, 0x4a, 0xfd, 0x50, 0x1f, 0xa6, 0xb4, 0xa1, 0x82, 0x3c, 00567 0xa3, 0xb1, 0x0b, 0xd9, 0xaa, 0x0b, 0xa9, 0x7b, 0xa1, 0x82 }, 00568 { 0x2e, 0x06, 0x6e, 0x62, 0x4b, 0xad, 0xb7, 0x6a, 0x18, 0x4c, 00569 0x8f, 0x90, 0xfb, 0xa0, 0x53, 0x33, 0x0e, 0x65, 0x0e, 0x92 }, 00570 { 0x07, 0xe9, 0x42, 0xaa, 0x4e, 0x3c, 0xd7, 0xc0, 0x4d, 0xed, 00571 0xc1, 0xd4, 0x6e, 0x2e, 0x8c, 0xc4, 0xc7, 0x41, 0xb3, 0xd9 }, 00572 { 0xb6, 0x58, 0x23, 0x18, 0xdd, 0xcf, 0xb6, 0x7a, 0x53, 0xa6, 00573 0x7d, 0x67, 0x6b, 0x8a, 0xd8, 0x69, 0xad, 0xed, 0x62, 0x9a }, 00574 { 0xf1, 0xbe, 0x3e, 0xe8, 0x77, 0x70, 0x31, 0x40, 0xd3, 0x4f, 00575 0x97, 0xea, 0x1a, 0xb3, 0xa0, 0x7c, 0x14, 0x13, 0x33, 0xe2 }, 00576 { 0x85, 0xf1, 0x64, 0x70, 0x3e, 0x61, 0xa6, 0x31, 0x31, 0xbe, 00577 0x7e, 0x45, 0x95, 0x8e, 0x07, 0x94, 0x12, 0x39, 0x04, 0xf9 }, 00578 }, 00579 }; 00580 00581 static const unsigned char ripemd160_test_key[KEYS][20] = 00582 { 00583 { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 00584 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x01, 0x23, 0x45, 0x67 }, 00585 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 00586 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x00, 0x11, 0x22, 0x33 }, 00587 }; 00588 00589 /* 00590 * Checkup routine 00591 */ 00592 int ripemd160_self_test( int verbose ) 00593 { 00594 int i, j; 00595 unsigned char output[20]; 00596 00597 memset( output, 0, sizeof output ); 00598 00599 for( i = 0; i < TESTS; i++ ) 00600 { 00601 if( verbose != 0 ) 00602 polarssl_printf( " RIPEMD-160 test #%d: ", i + 1 ); 00603 00604 ripemd160( (const unsigned char *) ripemd160_test_input[i], 00605 strlen( ripemd160_test_input[i] ), 00606 output ); 00607 00608 if( memcmp( output, ripemd160_test_md[i], 20 ) != 0 ) 00609 { 00610 if( verbose != 0 ) 00611 polarssl_printf( "failed\n" ); 00612 00613 return( 1 ); 00614 } 00615 00616 if( verbose != 0 ) 00617 polarssl_printf( "passed\n" ); 00618 00619 for( j = 0; j < KEYS; j++ ) 00620 { 00621 if( verbose != 0 ) 00622 polarssl_printf( " HMAC-RIPEMD-160 test #%d, key #%d: ", 00623 i + 1, j + 1 ); 00624 00625 ripemd160_hmac( ripemd160_test_key[j], 20, 00626 (const unsigned char *) ripemd160_test_input[i], 00627 strlen( ripemd160_test_input[i] ), 00628 output ); 00629 00630 if( memcmp( output, ripemd160_test_hmac[j][i], 20 ) != 0 ) 00631 { 00632 if( verbose != 0 ) 00633 polarssl_printf( "failed\n" ); 00634 00635 return( 1 ); 00636 } 00637 00638 if( verbose != 0 ) 00639 polarssl_printf( "passed\n" ); 00640 } 00641 00642 if( verbose != 0 ) 00643 polarssl_printf( "\n" ); 00644 } 00645 00646 return( 0 ); 00647 } 00648 00649 #endif /* POLARSSL_SELF_TEST */ 00650 00651 #endif /* POLARSSL_RIPEMD160_C */ 00652
Generated on Tue Jul 12 2022 13:50:38 by 1.7.2