mbed TLS library
Dependents: HTTPClient-SSL WS_SERVER
polarssl/ripemd160.c@0:137634ff4186, 2015-06-11 (annotated)
- Committer:
- ansond
- Date:
- Thu Jun 11 03:27:03 2015 +0000
- Revision:
- 0:137634ff4186
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:137634ff4186 | 1 | /* |
ansond | 0:137634ff4186 | 2 | * RIPE MD-160 implementation |
ansond | 0:137634ff4186 | 3 | * |
ansond | 0:137634ff4186 | 4 | * Copyright (C) 2014-2014, ARM Limited, All Rights Reserved |
ansond | 0:137634ff4186 | 5 | * |
ansond | 0:137634ff4186 | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
ansond | 0:137634ff4186 | 7 | * |
ansond | 0:137634ff4186 | 8 | * This program is free software; you can redistribute it and/or modify |
ansond | 0:137634ff4186 | 9 | * it under the terms of the GNU General Public License as published by |
ansond | 0:137634ff4186 | 10 | * the Free Software Foundation; either version 2 of the License, or |
ansond | 0:137634ff4186 | 11 | * (at your option) any later version. |
ansond | 0:137634ff4186 | 12 | * |
ansond | 0:137634ff4186 | 13 | * This program is distributed in the hope that it will be useful, |
ansond | 0:137634ff4186 | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ansond | 0:137634ff4186 | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ansond | 0:137634ff4186 | 16 | * GNU General Public License for more details. |
ansond | 0:137634ff4186 | 17 | * |
ansond | 0:137634ff4186 | 18 | * You should have received a copy of the GNU General Public License along |
ansond | 0:137634ff4186 | 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
ansond | 0:137634ff4186 | 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
ansond | 0:137634ff4186 | 21 | */ |
ansond | 0:137634ff4186 | 22 | |
ansond | 0:137634ff4186 | 23 | /* |
ansond | 0:137634ff4186 | 24 | * The RIPEMD-160 algorithm was designed by RIPE in 1996 |
ansond | 0:137634ff4186 | 25 | * http://homes.esat.kuleuven.be/~bosselae/ripemd160.html |
ansond | 0:137634ff4186 | 26 | * http://ehash.iaik.tugraz.at/wiki/RIPEMD-160 |
ansond | 0:137634ff4186 | 27 | */ |
ansond | 0:137634ff4186 | 28 | |
ansond | 0:137634ff4186 | 29 | #if !defined(POLARSSL_CONFIG_FILE) |
ansond | 0:137634ff4186 | 30 | #include "polarssl/config.h" |
ansond | 0:137634ff4186 | 31 | #else |
ansond | 0:137634ff4186 | 32 | #include POLARSSL_CONFIG_FILE |
ansond | 0:137634ff4186 | 33 | #endif |
ansond | 0:137634ff4186 | 34 | |
ansond | 0:137634ff4186 | 35 | #if defined(POLARSSL_RIPEMD160_C) |
ansond | 0:137634ff4186 | 36 | |
ansond | 0:137634ff4186 | 37 | #include "polarssl/ripemd160.h" |
ansond | 0:137634ff4186 | 38 | |
ansond | 0:137634ff4186 | 39 | #include <string.h> |
ansond | 0:137634ff4186 | 40 | |
ansond | 0:137634ff4186 | 41 | #if defined(POLARSSL_FS_IO) |
ansond | 0:137634ff4186 | 42 | #include <stdio.h> |
ansond | 0:137634ff4186 | 43 | #endif |
ansond | 0:137634ff4186 | 44 | |
ansond | 0:137634ff4186 | 45 | #if defined(POLARSSL_SELF_TEST) |
ansond | 0:137634ff4186 | 46 | #if defined(POLARSSL_PLATFORM_C) |
ansond | 0:137634ff4186 | 47 | #include "polarssl/platform.h" |
ansond | 0:137634ff4186 | 48 | #else |
ansond | 0:137634ff4186 | 49 | #include <stdio.h> |
ansond | 0:137634ff4186 | 50 | #define polarssl_printf printf |
ansond | 0:137634ff4186 | 51 | #endif /* POLARSSL_PLATFORM_C */ |
ansond | 0:137634ff4186 | 52 | #endif /* POLARSSL_SELF_TEST */ |
ansond | 0:137634ff4186 | 53 | |
ansond | 0:137634ff4186 | 54 | /* |
ansond | 0:137634ff4186 | 55 | * 32-bit integer manipulation macros (little endian) |
ansond | 0:137634ff4186 | 56 | */ |
ansond | 0:137634ff4186 | 57 | #ifndef GET_UINT32_LE |
ansond | 0:137634ff4186 | 58 | #define GET_UINT32_LE(n,b,i) \ |
ansond | 0:137634ff4186 | 59 | { \ |
ansond | 0:137634ff4186 | 60 | (n) = ( (uint32_t) (b)[(i) ] ) \ |
ansond | 0:137634ff4186 | 61 | | ( (uint32_t) (b)[(i) + 1] << 8 ) \ |
ansond | 0:137634ff4186 | 62 | | ( (uint32_t) (b)[(i) + 2] << 16 ) \ |
ansond | 0:137634ff4186 | 63 | | ( (uint32_t) (b)[(i) + 3] << 24 ); \ |
ansond | 0:137634ff4186 | 64 | } |
ansond | 0:137634ff4186 | 65 | #endif |
ansond | 0:137634ff4186 | 66 | |
ansond | 0:137634ff4186 | 67 | #ifndef PUT_UINT32_LE |
ansond | 0:137634ff4186 | 68 | #define PUT_UINT32_LE(n,b,i) \ |
ansond | 0:137634ff4186 | 69 | { \ |
ansond | 0:137634ff4186 | 70 | (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ |
ansond | 0:137634ff4186 | 71 | (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ |
ansond | 0:137634ff4186 | 72 | (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ |
ansond | 0:137634ff4186 | 73 | (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ |
ansond | 0:137634ff4186 | 74 | } |
ansond | 0:137634ff4186 | 75 | #endif |
ansond | 0:137634ff4186 | 76 | |
ansond | 0:137634ff4186 | 77 | /* Implementation that should never be optimized out by the compiler */ |
ansond | 0:137634ff4186 | 78 | static void polarssl_zeroize( void *v, size_t n ) { |
ansond | 0:137634ff4186 | 79 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
ansond | 0:137634ff4186 | 80 | } |
ansond | 0:137634ff4186 | 81 | |
ansond | 0:137634ff4186 | 82 | void ripemd160_init( ripemd160_context *ctx ) |
ansond | 0:137634ff4186 | 83 | { |
ansond | 0:137634ff4186 | 84 | memset( ctx, 0, sizeof( ripemd160_context ) ); |
ansond | 0:137634ff4186 | 85 | } |
ansond | 0:137634ff4186 | 86 | |
ansond | 0:137634ff4186 | 87 | void ripemd160_free( ripemd160_context *ctx ) |
ansond | 0:137634ff4186 | 88 | { |
ansond | 0:137634ff4186 | 89 | if( ctx == NULL ) |
ansond | 0:137634ff4186 | 90 | return; |
ansond | 0:137634ff4186 | 91 | |
ansond | 0:137634ff4186 | 92 | polarssl_zeroize( ctx, sizeof( ripemd160_context ) ); |
ansond | 0:137634ff4186 | 93 | } |
ansond | 0:137634ff4186 | 94 | |
ansond | 0:137634ff4186 | 95 | /* |
ansond | 0:137634ff4186 | 96 | * RIPEMD-160 context setup |
ansond | 0:137634ff4186 | 97 | */ |
ansond | 0:137634ff4186 | 98 | void ripemd160_starts( ripemd160_context *ctx ) |
ansond | 0:137634ff4186 | 99 | { |
ansond | 0:137634ff4186 | 100 | ctx->total[0] = 0; |
ansond | 0:137634ff4186 | 101 | ctx->total[1] = 0; |
ansond | 0:137634ff4186 | 102 | |
ansond | 0:137634ff4186 | 103 | ctx->state[0] = 0x67452301; |
ansond | 0:137634ff4186 | 104 | ctx->state[1] = 0xEFCDAB89; |
ansond | 0:137634ff4186 | 105 | ctx->state[2] = 0x98BADCFE; |
ansond | 0:137634ff4186 | 106 | ctx->state[3] = 0x10325476; |
ansond | 0:137634ff4186 | 107 | ctx->state[4] = 0xC3D2E1F0; |
ansond | 0:137634ff4186 | 108 | } |
ansond | 0:137634ff4186 | 109 | |
ansond | 0:137634ff4186 | 110 | /* |
ansond | 0:137634ff4186 | 111 | * Process one block |
ansond | 0:137634ff4186 | 112 | */ |
ansond | 0:137634ff4186 | 113 | void ripemd160_process( ripemd160_context *ctx, const unsigned char data[64] ) |
ansond | 0:137634ff4186 | 114 | { |
ansond | 0:137634ff4186 | 115 | uint32_t A, B, C, D, E, Ap, Bp, Cp, Dp, Ep, X[16]; |
ansond | 0:137634ff4186 | 116 | |
ansond | 0:137634ff4186 | 117 | GET_UINT32_LE( X[ 0], data, 0 ); |
ansond | 0:137634ff4186 | 118 | GET_UINT32_LE( X[ 1], data, 4 ); |
ansond | 0:137634ff4186 | 119 | GET_UINT32_LE( X[ 2], data, 8 ); |
ansond | 0:137634ff4186 | 120 | GET_UINT32_LE( X[ 3], data, 12 ); |
ansond | 0:137634ff4186 | 121 | GET_UINT32_LE( X[ 4], data, 16 ); |
ansond | 0:137634ff4186 | 122 | GET_UINT32_LE( X[ 5], data, 20 ); |
ansond | 0:137634ff4186 | 123 | GET_UINT32_LE( X[ 6], data, 24 ); |
ansond | 0:137634ff4186 | 124 | GET_UINT32_LE( X[ 7], data, 28 ); |
ansond | 0:137634ff4186 | 125 | GET_UINT32_LE( X[ 8], data, 32 ); |
ansond | 0:137634ff4186 | 126 | GET_UINT32_LE( X[ 9], data, 36 ); |
ansond | 0:137634ff4186 | 127 | GET_UINT32_LE( X[10], data, 40 ); |
ansond | 0:137634ff4186 | 128 | GET_UINT32_LE( X[11], data, 44 ); |
ansond | 0:137634ff4186 | 129 | GET_UINT32_LE( X[12], data, 48 ); |
ansond | 0:137634ff4186 | 130 | GET_UINT32_LE( X[13], data, 52 ); |
ansond | 0:137634ff4186 | 131 | GET_UINT32_LE( X[14], data, 56 ); |
ansond | 0:137634ff4186 | 132 | GET_UINT32_LE( X[15], data, 60 ); |
ansond | 0:137634ff4186 | 133 | |
ansond | 0:137634ff4186 | 134 | A = Ap = ctx->state[0]; |
ansond | 0:137634ff4186 | 135 | B = Bp = ctx->state[1]; |
ansond | 0:137634ff4186 | 136 | C = Cp = ctx->state[2]; |
ansond | 0:137634ff4186 | 137 | D = Dp = ctx->state[3]; |
ansond | 0:137634ff4186 | 138 | E = Ep = ctx->state[4]; |
ansond | 0:137634ff4186 | 139 | |
ansond | 0:137634ff4186 | 140 | #define F1( x, y, z ) ( x ^ y ^ z ) |
ansond | 0:137634ff4186 | 141 | #define F2( x, y, z ) ( ( x & y ) | ( ~x & z ) ) |
ansond | 0:137634ff4186 | 142 | #define F3( x, y, z ) ( ( x | ~y ) ^ z ) |
ansond | 0:137634ff4186 | 143 | #define F4( x, y, z ) ( ( x & z ) | ( y & ~z ) ) |
ansond | 0:137634ff4186 | 144 | #define F5( x, y, z ) ( x ^ ( y | ~z ) ) |
ansond | 0:137634ff4186 | 145 | |
ansond | 0:137634ff4186 | 146 | #define S( x, n ) ( ( x << n ) | ( x >> (32 - n) ) ) |
ansond | 0:137634ff4186 | 147 | |
ansond | 0:137634ff4186 | 148 | #define P( a, b, c, d, e, r, s, f, k ) \ |
ansond | 0:137634ff4186 | 149 | a += f( b, c, d ) + X[r] + k; \ |
ansond | 0:137634ff4186 | 150 | a = S( a, s ) + e; \ |
ansond | 0:137634ff4186 | 151 | c = S( c, 10 ); |
ansond | 0:137634ff4186 | 152 | |
ansond | 0:137634ff4186 | 153 | #define P2( a, b, c, d, e, r, s, rp, sp ) \ |
ansond | 0:137634ff4186 | 154 | P( a, b, c, d, e, r, s, F, K ); \ |
ansond | 0:137634ff4186 | 155 | P( a ## p, b ## p, c ## p, d ## p, e ## p, rp, sp, Fp, Kp ); |
ansond | 0:137634ff4186 | 156 | |
ansond | 0:137634ff4186 | 157 | #define F F1 |
ansond | 0:137634ff4186 | 158 | #define K 0x00000000 |
ansond | 0:137634ff4186 | 159 | #define Fp F5 |
ansond | 0:137634ff4186 | 160 | #define Kp 0x50A28BE6 |
ansond | 0:137634ff4186 | 161 | P2( A, B, C, D, E, 0, 11, 5, 8 ); |
ansond | 0:137634ff4186 | 162 | P2( E, A, B, C, D, 1, 14, 14, 9 ); |
ansond | 0:137634ff4186 | 163 | P2( D, E, A, B, C, 2, 15, 7, 9 ); |
ansond | 0:137634ff4186 | 164 | P2( C, D, E, A, B, 3, 12, 0, 11 ); |
ansond | 0:137634ff4186 | 165 | P2( B, C, D, E, A, 4, 5, 9, 13 ); |
ansond | 0:137634ff4186 | 166 | P2( A, B, C, D, E, 5, 8, 2, 15 ); |
ansond | 0:137634ff4186 | 167 | P2( E, A, B, C, D, 6, 7, 11, 15 ); |
ansond | 0:137634ff4186 | 168 | P2( D, E, A, B, C, 7, 9, 4, 5 ); |
ansond | 0:137634ff4186 | 169 | P2( C, D, E, A, B, 8, 11, 13, 7 ); |
ansond | 0:137634ff4186 | 170 | P2( B, C, D, E, A, 9, 13, 6, 7 ); |
ansond | 0:137634ff4186 | 171 | P2( A, B, C, D, E, 10, 14, 15, 8 ); |
ansond | 0:137634ff4186 | 172 | P2( E, A, B, C, D, 11, 15, 8, 11 ); |
ansond | 0:137634ff4186 | 173 | P2( D, E, A, B, C, 12, 6, 1, 14 ); |
ansond | 0:137634ff4186 | 174 | P2( C, D, E, A, B, 13, 7, 10, 14 ); |
ansond | 0:137634ff4186 | 175 | P2( B, C, D, E, A, 14, 9, 3, 12 ); |
ansond | 0:137634ff4186 | 176 | P2( A, B, C, D, E, 15, 8, 12, 6 ); |
ansond | 0:137634ff4186 | 177 | #undef F |
ansond | 0:137634ff4186 | 178 | #undef K |
ansond | 0:137634ff4186 | 179 | #undef Fp |
ansond | 0:137634ff4186 | 180 | #undef Kp |
ansond | 0:137634ff4186 | 181 | |
ansond | 0:137634ff4186 | 182 | #define F F2 |
ansond | 0:137634ff4186 | 183 | #define K 0x5A827999 |
ansond | 0:137634ff4186 | 184 | #define Fp F4 |
ansond | 0:137634ff4186 | 185 | #define Kp 0x5C4DD124 |
ansond | 0:137634ff4186 | 186 | P2( E, A, B, C, D, 7, 7, 6, 9 ); |
ansond | 0:137634ff4186 | 187 | P2( D, E, A, B, C, 4, 6, 11, 13 ); |
ansond | 0:137634ff4186 | 188 | P2( C, D, E, A, B, 13, 8, 3, 15 ); |
ansond | 0:137634ff4186 | 189 | P2( B, C, D, E, A, 1, 13, 7, 7 ); |
ansond | 0:137634ff4186 | 190 | P2( A, B, C, D, E, 10, 11, 0, 12 ); |
ansond | 0:137634ff4186 | 191 | P2( E, A, B, C, D, 6, 9, 13, 8 ); |
ansond | 0:137634ff4186 | 192 | P2( D, E, A, B, C, 15, 7, 5, 9 ); |
ansond | 0:137634ff4186 | 193 | P2( C, D, E, A, B, 3, 15, 10, 11 ); |
ansond | 0:137634ff4186 | 194 | P2( B, C, D, E, A, 12, 7, 14, 7 ); |
ansond | 0:137634ff4186 | 195 | P2( A, B, C, D, E, 0, 12, 15, 7 ); |
ansond | 0:137634ff4186 | 196 | P2( E, A, B, C, D, 9, 15, 8, 12 ); |
ansond | 0:137634ff4186 | 197 | P2( D, E, A, B, C, 5, 9, 12, 7 ); |
ansond | 0:137634ff4186 | 198 | P2( C, D, E, A, B, 2, 11, 4, 6 ); |
ansond | 0:137634ff4186 | 199 | P2( B, C, D, E, A, 14, 7, 9, 15 ); |
ansond | 0:137634ff4186 | 200 | P2( A, B, C, D, E, 11, 13, 1, 13 ); |
ansond | 0:137634ff4186 | 201 | P2( E, A, B, C, D, 8, 12, 2, 11 ); |
ansond | 0:137634ff4186 | 202 | #undef F |
ansond | 0:137634ff4186 | 203 | #undef K |
ansond | 0:137634ff4186 | 204 | #undef Fp |
ansond | 0:137634ff4186 | 205 | #undef Kp |
ansond | 0:137634ff4186 | 206 | |
ansond | 0:137634ff4186 | 207 | #define F F3 |
ansond | 0:137634ff4186 | 208 | #define K 0x6ED9EBA1 |
ansond | 0:137634ff4186 | 209 | #define Fp F3 |
ansond | 0:137634ff4186 | 210 | #define Kp 0x6D703EF3 |
ansond | 0:137634ff4186 | 211 | P2( D, E, A, B, C, 3, 11, 15, 9 ); |
ansond | 0:137634ff4186 | 212 | P2( C, D, E, A, B, 10, 13, 5, 7 ); |
ansond | 0:137634ff4186 | 213 | P2( B, C, D, E, A, 14, 6, 1, 15 ); |
ansond | 0:137634ff4186 | 214 | P2( A, B, C, D, E, 4, 7, 3, 11 ); |
ansond | 0:137634ff4186 | 215 | P2( E, A, B, C, D, 9, 14, 7, 8 ); |
ansond | 0:137634ff4186 | 216 | P2( D, E, A, B, C, 15, 9, 14, 6 ); |
ansond | 0:137634ff4186 | 217 | P2( C, D, E, A, B, 8, 13, 6, 6 ); |
ansond | 0:137634ff4186 | 218 | P2( B, C, D, E, A, 1, 15, 9, 14 ); |
ansond | 0:137634ff4186 | 219 | P2( A, B, C, D, E, 2, 14, 11, 12 ); |
ansond | 0:137634ff4186 | 220 | P2( E, A, B, C, D, 7, 8, 8, 13 ); |
ansond | 0:137634ff4186 | 221 | P2( D, E, A, B, C, 0, 13, 12, 5 ); |
ansond | 0:137634ff4186 | 222 | P2( C, D, E, A, B, 6, 6, 2, 14 ); |
ansond | 0:137634ff4186 | 223 | P2( B, C, D, E, A, 13, 5, 10, 13 ); |
ansond | 0:137634ff4186 | 224 | P2( A, B, C, D, E, 11, 12, 0, 13 ); |
ansond | 0:137634ff4186 | 225 | P2( E, A, B, C, D, 5, 7, 4, 7 ); |
ansond | 0:137634ff4186 | 226 | P2( D, E, A, B, C, 12, 5, 13, 5 ); |
ansond | 0:137634ff4186 | 227 | #undef F |
ansond | 0:137634ff4186 | 228 | #undef K |
ansond | 0:137634ff4186 | 229 | #undef Fp |
ansond | 0:137634ff4186 | 230 | #undef Kp |
ansond | 0:137634ff4186 | 231 | |
ansond | 0:137634ff4186 | 232 | #define F F4 |
ansond | 0:137634ff4186 | 233 | #define K 0x8F1BBCDC |
ansond | 0:137634ff4186 | 234 | #define Fp F2 |
ansond | 0:137634ff4186 | 235 | #define Kp 0x7A6D76E9 |
ansond | 0:137634ff4186 | 236 | P2( C, D, E, A, B, 1, 11, 8, 15 ); |
ansond | 0:137634ff4186 | 237 | P2( B, C, D, E, A, 9, 12, 6, 5 ); |
ansond | 0:137634ff4186 | 238 | P2( A, B, C, D, E, 11, 14, 4, 8 ); |
ansond | 0:137634ff4186 | 239 | P2( E, A, B, C, D, 10, 15, 1, 11 ); |
ansond | 0:137634ff4186 | 240 | P2( D, E, A, B, C, 0, 14, 3, 14 ); |
ansond | 0:137634ff4186 | 241 | P2( C, D, E, A, B, 8, 15, 11, 14 ); |
ansond | 0:137634ff4186 | 242 | P2( B, C, D, E, A, 12, 9, 15, 6 ); |
ansond | 0:137634ff4186 | 243 | P2( A, B, C, D, E, 4, 8, 0, 14 ); |
ansond | 0:137634ff4186 | 244 | P2( E, A, B, C, D, 13, 9, 5, 6 ); |
ansond | 0:137634ff4186 | 245 | P2( D, E, A, B, C, 3, 14, 12, 9 ); |
ansond | 0:137634ff4186 | 246 | P2( C, D, E, A, B, 7, 5, 2, 12 ); |
ansond | 0:137634ff4186 | 247 | P2( B, C, D, E, A, 15, 6, 13, 9 ); |
ansond | 0:137634ff4186 | 248 | P2( A, B, C, D, E, 14, 8, 9, 12 ); |
ansond | 0:137634ff4186 | 249 | P2( E, A, B, C, D, 5, 6, 7, 5 ); |
ansond | 0:137634ff4186 | 250 | P2( D, E, A, B, C, 6, 5, 10, 15 ); |
ansond | 0:137634ff4186 | 251 | P2( C, D, E, A, B, 2, 12, 14, 8 ); |
ansond | 0:137634ff4186 | 252 | #undef F |
ansond | 0:137634ff4186 | 253 | #undef K |
ansond | 0:137634ff4186 | 254 | #undef Fp |
ansond | 0:137634ff4186 | 255 | #undef Kp |
ansond | 0:137634ff4186 | 256 | |
ansond | 0:137634ff4186 | 257 | #define F F5 |
ansond | 0:137634ff4186 | 258 | #define K 0xA953FD4E |
ansond | 0:137634ff4186 | 259 | #define Fp F1 |
ansond | 0:137634ff4186 | 260 | #define Kp 0x00000000 |
ansond | 0:137634ff4186 | 261 | P2( B, C, D, E, A, 4, 9, 12, 8 ); |
ansond | 0:137634ff4186 | 262 | P2( A, B, C, D, E, 0, 15, 15, 5 ); |
ansond | 0:137634ff4186 | 263 | P2( E, A, B, C, D, 5, 5, 10, 12 ); |
ansond | 0:137634ff4186 | 264 | P2( D, E, A, B, C, 9, 11, 4, 9 ); |
ansond | 0:137634ff4186 | 265 | P2( C, D, E, A, B, 7, 6, 1, 12 ); |
ansond | 0:137634ff4186 | 266 | P2( B, C, D, E, A, 12, 8, 5, 5 ); |
ansond | 0:137634ff4186 | 267 | P2( A, B, C, D, E, 2, 13, 8, 14 ); |
ansond | 0:137634ff4186 | 268 | P2( E, A, B, C, D, 10, 12, 7, 6 ); |
ansond | 0:137634ff4186 | 269 | P2( D, E, A, B, C, 14, 5, 6, 8 ); |
ansond | 0:137634ff4186 | 270 | P2( C, D, E, A, B, 1, 12, 2, 13 ); |
ansond | 0:137634ff4186 | 271 | P2( B, C, D, E, A, 3, 13, 13, 6 ); |
ansond | 0:137634ff4186 | 272 | P2( A, B, C, D, E, 8, 14, 14, 5 ); |
ansond | 0:137634ff4186 | 273 | P2( E, A, B, C, D, 11, 11, 0, 15 ); |
ansond | 0:137634ff4186 | 274 | P2( D, E, A, B, C, 6, 8, 3, 13 ); |
ansond | 0:137634ff4186 | 275 | P2( C, D, E, A, B, 15, 5, 9, 11 ); |
ansond | 0:137634ff4186 | 276 | P2( B, C, D, E, A, 13, 6, 11, 11 ); |
ansond | 0:137634ff4186 | 277 | #undef F |
ansond | 0:137634ff4186 | 278 | #undef K |
ansond | 0:137634ff4186 | 279 | #undef Fp |
ansond | 0:137634ff4186 | 280 | #undef Kp |
ansond | 0:137634ff4186 | 281 | |
ansond | 0:137634ff4186 | 282 | C = ctx->state[1] + C + Dp; |
ansond | 0:137634ff4186 | 283 | ctx->state[1] = ctx->state[2] + D + Ep; |
ansond | 0:137634ff4186 | 284 | ctx->state[2] = ctx->state[3] + E + Ap; |
ansond | 0:137634ff4186 | 285 | ctx->state[3] = ctx->state[4] + A + Bp; |
ansond | 0:137634ff4186 | 286 | ctx->state[4] = ctx->state[0] + B + Cp; |
ansond | 0:137634ff4186 | 287 | ctx->state[0] = C; |
ansond | 0:137634ff4186 | 288 | } |
ansond | 0:137634ff4186 | 289 | |
ansond | 0:137634ff4186 | 290 | /* |
ansond | 0:137634ff4186 | 291 | * RIPEMD-160 process buffer |
ansond | 0:137634ff4186 | 292 | */ |
ansond | 0:137634ff4186 | 293 | void ripemd160_update( ripemd160_context *ctx, |
ansond | 0:137634ff4186 | 294 | const unsigned char *input, size_t ilen ) |
ansond | 0:137634ff4186 | 295 | { |
ansond | 0:137634ff4186 | 296 | size_t fill; |
ansond | 0:137634ff4186 | 297 | uint32_t left; |
ansond | 0:137634ff4186 | 298 | |
ansond | 0:137634ff4186 | 299 | if( ilen == 0 ) |
ansond | 0:137634ff4186 | 300 | return; |
ansond | 0:137634ff4186 | 301 | |
ansond | 0:137634ff4186 | 302 | left = ctx->total[0] & 0x3F; |
ansond | 0:137634ff4186 | 303 | fill = 64 - left; |
ansond | 0:137634ff4186 | 304 | |
ansond | 0:137634ff4186 | 305 | ctx->total[0] += (uint32_t) ilen; |
ansond | 0:137634ff4186 | 306 | ctx->total[0] &= 0xFFFFFFFF; |
ansond | 0:137634ff4186 | 307 | |
ansond | 0:137634ff4186 | 308 | if( ctx->total[0] < (uint32_t) ilen ) |
ansond | 0:137634ff4186 | 309 | ctx->total[1]++; |
ansond | 0:137634ff4186 | 310 | |
ansond | 0:137634ff4186 | 311 | if( left && ilen >= fill ) |
ansond | 0:137634ff4186 | 312 | { |
ansond | 0:137634ff4186 | 313 | memcpy( (void *) (ctx->buffer + left), input, fill ); |
ansond | 0:137634ff4186 | 314 | ripemd160_process( ctx, ctx->buffer ); |
ansond | 0:137634ff4186 | 315 | input += fill; |
ansond | 0:137634ff4186 | 316 | ilen -= fill; |
ansond | 0:137634ff4186 | 317 | left = 0; |
ansond | 0:137634ff4186 | 318 | } |
ansond | 0:137634ff4186 | 319 | |
ansond | 0:137634ff4186 | 320 | while( ilen >= 64 ) |
ansond | 0:137634ff4186 | 321 | { |
ansond | 0:137634ff4186 | 322 | ripemd160_process( ctx, input ); |
ansond | 0:137634ff4186 | 323 | input += 64; |
ansond | 0:137634ff4186 | 324 | ilen -= 64; |
ansond | 0:137634ff4186 | 325 | } |
ansond | 0:137634ff4186 | 326 | |
ansond | 0:137634ff4186 | 327 | if( ilen > 0 ) |
ansond | 0:137634ff4186 | 328 | { |
ansond | 0:137634ff4186 | 329 | memcpy( (void *) (ctx->buffer + left), input, ilen ); |
ansond | 0:137634ff4186 | 330 | } |
ansond | 0:137634ff4186 | 331 | } |
ansond | 0:137634ff4186 | 332 | |
ansond | 0:137634ff4186 | 333 | static const unsigned char ripemd160_padding[64] = |
ansond | 0:137634ff4186 | 334 | { |
ansond | 0:137634ff4186 | 335 | 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
ansond | 0:137634ff4186 | 336 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
ansond | 0:137634ff4186 | 337 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
ansond | 0:137634ff4186 | 338 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
ansond | 0:137634ff4186 | 339 | }; |
ansond | 0:137634ff4186 | 340 | |
ansond | 0:137634ff4186 | 341 | /* |
ansond | 0:137634ff4186 | 342 | * RIPEMD-160 final digest |
ansond | 0:137634ff4186 | 343 | */ |
ansond | 0:137634ff4186 | 344 | void ripemd160_finish( ripemd160_context *ctx, unsigned char output[20] ) |
ansond | 0:137634ff4186 | 345 | { |
ansond | 0:137634ff4186 | 346 | uint32_t last, padn; |
ansond | 0:137634ff4186 | 347 | uint32_t high, low; |
ansond | 0:137634ff4186 | 348 | unsigned char msglen[8]; |
ansond | 0:137634ff4186 | 349 | |
ansond | 0:137634ff4186 | 350 | high = ( ctx->total[0] >> 29 ) |
ansond | 0:137634ff4186 | 351 | | ( ctx->total[1] << 3 ); |
ansond | 0:137634ff4186 | 352 | low = ( ctx->total[0] << 3 ); |
ansond | 0:137634ff4186 | 353 | |
ansond | 0:137634ff4186 | 354 | PUT_UINT32_LE( low, msglen, 0 ); |
ansond | 0:137634ff4186 | 355 | PUT_UINT32_LE( high, msglen, 4 ); |
ansond | 0:137634ff4186 | 356 | |
ansond | 0:137634ff4186 | 357 | last = ctx->total[0] & 0x3F; |
ansond | 0:137634ff4186 | 358 | padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); |
ansond | 0:137634ff4186 | 359 | |
ansond | 0:137634ff4186 | 360 | ripemd160_update( ctx, ripemd160_padding, padn ); |
ansond | 0:137634ff4186 | 361 | ripemd160_update( ctx, msglen, 8 ); |
ansond | 0:137634ff4186 | 362 | |
ansond | 0:137634ff4186 | 363 | PUT_UINT32_LE( ctx->state[0], output, 0 ); |
ansond | 0:137634ff4186 | 364 | PUT_UINT32_LE( ctx->state[1], output, 4 ); |
ansond | 0:137634ff4186 | 365 | PUT_UINT32_LE( ctx->state[2], output, 8 ); |
ansond | 0:137634ff4186 | 366 | PUT_UINT32_LE( ctx->state[3], output, 12 ); |
ansond | 0:137634ff4186 | 367 | PUT_UINT32_LE( ctx->state[4], output, 16 ); |
ansond | 0:137634ff4186 | 368 | } |
ansond | 0:137634ff4186 | 369 | |
ansond | 0:137634ff4186 | 370 | /* |
ansond | 0:137634ff4186 | 371 | * output = RIPEMD-160( input buffer ) |
ansond | 0:137634ff4186 | 372 | */ |
ansond | 0:137634ff4186 | 373 | void ripemd160( const unsigned char *input, size_t ilen, |
ansond | 0:137634ff4186 | 374 | unsigned char output[20] ) |
ansond | 0:137634ff4186 | 375 | { |
ansond | 0:137634ff4186 | 376 | ripemd160_context ctx; |
ansond | 0:137634ff4186 | 377 | |
ansond | 0:137634ff4186 | 378 | ripemd160_init( &ctx ); |
ansond | 0:137634ff4186 | 379 | ripemd160_starts( &ctx ); |
ansond | 0:137634ff4186 | 380 | ripemd160_update( &ctx, input, ilen ); |
ansond | 0:137634ff4186 | 381 | ripemd160_finish( &ctx, output ); |
ansond | 0:137634ff4186 | 382 | ripemd160_free( &ctx ); |
ansond | 0:137634ff4186 | 383 | } |
ansond | 0:137634ff4186 | 384 | |
ansond | 0:137634ff4186 | 385 | #if defined(POLARSSL_FS_IO) |
ansond | 0:137634ff4186 | 386 | /* |
ansond | 0:137634ff4186 | 387 | * output = RIPEMD-160( file contents ) |
ansond | 0:137634ff4186 | 388 | */ |
ansond | 0:137634ff4186 | 389 | int ripemd160_file( const char *path, unsigned char output[20] ) |
ansond | 0:137634ff4186 | 390 | { |
ansond | 0:137634ff4186 | 391 | FILE *f; |
ansond | 0:137634ff4186 | 392 | size_t n; |
ansond | 0:137634ff4186 | 393 | ripemd160_context ctx; |
ansond | 0:137634ff4186 | 394 | unsigned char buf[1024]; |
ansond | 0:137634ff4186 | 395 | |
ansond | 0:137634ff4186 | 396 | if( ( f = fopen( path, "rb" ) ) == NULL ) |
ansond | 0:137634ff4186 | 397 | return( POLARSSL_ERR_RIPEMD160_FILE_IO_ERROR ); |
ansond | 0:137634ff4186 | 398 | |
ansond | 0:137634ff4186 | 399 | ripemd160_init( &ctx ); |
ansond | 0:137634ff4186 | 400 | ripemd160_starts( &ctx ); |
ansond | 0:137634ff4186 | 401 | |
ansond | 0:137634ff4186 | 402 | while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) |
ansond | 0:137634ff4186 | 403 | ripemd160_update( &ctx, buf, n ); |
ansond | 0:137634ff4186 | 404 | |
ansond | 0:137634ff4186 | 405 | ripemd160_finish( &ctx, output ); |
ansond | 0:137634ff4186 | 406 | ripemd160_free( &ctx ); |
ansond | 0:137634ff4186 | 407 | |
ansond | 0:137634ff4186 | 408 | if( ferror( f ) != 0 ) |
ansond | 0:137634ff4186 | 409 | { |
ansond | 0:137634ff4186 | 410 | fclose( f ); |
ansond | 0:137634ff4186 | 411 | return( POLARSSL_ERR_RIPEMD160_FILE_IO_ERROR ); |
ansond | 0:137634ff4186 | 412 | } |
ansond | 0:137634ff4186 | 413 | |
ansond | 0:137634ff4186 | 414 | fclose( f ); |
ansond | 0:137634ff4186 | 415 | return( 0 ); |
ansond | 0:137634ff4186 | 416 | } |
ansond | 0:137634ff4186 | 417 | #endif /* POLARSSL_FS_IO */ |
ansond | 0:137634ff4186 | 418 | |
ansond | 0:137634ff4186 | 419 | /* |
ansond | 0:137634ff4186 | 420 | * RIPEMD-160 HMAC context setup |
ansond | 0:137634ff4186 | 421 | */ |
ansond | 0:137634ff4186 | 422 | void ripemd160_hmac_starts( ripemd160_context *ctx, |
ansond | 0:137634ff4186 | 423 | const unsigned char *key, size_t keylen ) |
ansond | 0:137634ff4186 | 424 | { |
ansond | 0:137634ff4186 | 425 | size_t i; |
ansond | 0:137634ff4186 | 426 | unsigned char sum[20]; |
ansond | 0:137634ff4186 | 427 | |
ansond | 0:137634ff4186 | 428 | if( keylen > 64 ) |
ansond | 0:137634ff4186 | 429 | { |
ansond | 0:137634ff4186 | 430 | ripemd160( key, keylen, sum ); |
ansond | 0:137634ff4186 | 431 | keylen = 20; |
ansond | 0:137634ff4186 | 432 | key = sum; |
ansond | 0:137634ff4186 | 433 | } |
ansond | 0:137634ff4186 | 434 | |
ansond | 0:137634ff4186 | 435 | memset( ctx->ipad, 0x36, 64 ); |
ansond | 0:137634ff4186 | 436 | memset( ctx->opad, 0x5C, 64 ); |
ansond | 0:137634ff4186 | 437 | |
ansond | 0:137634ff4186 | 438 | for( i = 0; i < keylen; i++ ) |
ansond | 0:137634ff4186 | 439 | { |
ansond | 0:137634ff4186 | 440 | ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] ); |
ansond | 0:137634ff4186 | 441 | ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] ); |
ansond | 0:137634ff4186 | 442 | } |
ansond | 0:137634ff4186 | 443 | |
ansond | 0:137634ff4186 | 444 | ripemd160_starts( ctx ); |
ansond | 0:137634ff4186 | 445 | ripemd160_update( ctx, ctx->ipad, 64 ); |
ansond | 0:137634ff4186 | 446 | |
ansond | 0:137634ff4186 | 447 | polarssl_zeroize( sum, sizeof( sum ) ); |
ansond | 0:137634ff4186 | 448 | } |
ansond | 0:137634ff4186 | 449 | |
ansond | 0:137634ff4186 | 450 | /* |
ansond | 0:137634ff4186 | 451 | * RIPEMD-160 HMAC process buffer |
ansond | 0:137634ff4186 | 452 | */ |
ansond | 0:137634ff4186 | 453 | void ripemd160_hmac_update( ripemd160_context *ctx, |
ansond | 0:137634ff4186 | 454 | const unsigned char *input, size_t ilen ) |
ansond | 0:137634ff4186 | 455 | { |
ansond | 0:137634ff4186 | 456 | ripemd160_update( ctx, input, ilen ); |
ansond | 0:137634ff4186 | 457 | } |
ansond | 0:137634ff4186 | 458 | |
ansond | 0:137634ff4186 | 459 | /* |
ansond | 0:137634ff4186 | 460 | * RIPEMD-160 HMAC final digest |
ansond | 0:137634ff4186 | 461 | */ |
ansond | 0:137634ff4186 | 462 | void ripemd160_hmac_finish( ripemd160_context *ctx, unsigned char output[20] ) |
ansond | 0:137634ff4186 | 463 | { |
ansond | 0:137634ff4186 | 464 | unsigned char tmpbuf[20]; |
ansond | 0:137634ff4186 | 465 | |
ansond | 0:137634ff4186 | 466 | ripemd160_finish( ctx, tmpbuf ); |
ansond | 0:137634ff4186 | 467 | ripemd160_starts( ctx ); |
ansond | 0:137634ff4186 | 468 | ripemd160_update( ctx, ctx->opad, 64 ); |
ansond | 0:137634ff4186 | 469 | ripemd160_update( ctx, tmpbuf, 20 ); |
ansond | 0:137634ff4186 | 470 | ripemd160_finish( ctx, output ); |
ansond | 0:137634ff4186 | 471 | |
ansond | 0:137634ff4186 | 472 | polarssl_zeroize( tmpbuf, sizeof( tmpbuf ) ); |
ansond | 0:137634ff4186 | 473 | } |
ansond | 0:137634ff4186 | 474 | |
ansond | 0:137634ff4186 | 475 | /* |
ansond | 0:137634ff4186 | 476 | * RIPEMD-160 HMAC context reset |
ansond | 0:137634ff4186 | 477 | */ |
ansond | 0:137634ff4186 | 478 | void ripemd160_hmac_reset( ripemd160_context *ctx ) |
ansond | 0:137634ff4186 | 479 | { |
ansond | 0:137634ff4186 | 480 | ripemd160_starts( ctx ); |
ansond | 0:137634ff4186 | 481 | ripemd160_update( ctx, ctx->ipad, 64 ); |
ansond | 0:137634ff4186 | 482 | } |
ansond | 0:137634ff4186 | 483 | |
ansond | 0:137634ff4186 | 484 | /* |
ansond | 0:137634ff4186 | 485 | * output = HMAC-RIPEMD-160( hmac key, input buffer ) |
ansond | 0:137634ff4186 | 486 | */ |
ansond | 0:137634ff4186 | 487 | void ripemd160_hmac( const unsigned char *key, size_t keylen, |
ansond | 0:137634ff4186 | 488 | const unsigned char *input, size_t ilen, |
ansond | 0:137634ff4186 | 489 | unsigned char output[20] ) |
ansond | 0:137634ff4186 | 490 | { |
ansond | 0:137634ff4186 | 491 | ripemd160_context ctx; |
ansond | 0:137634ff4186 | 492 | |
ansond | 0:137634ff4186 | 493 | ripemd160_init( &ctx ); |
ansond | 0:137634ff4186 | 494 | ripemd160_hmac_starts( &ctx, key, keylen ); |
ansond | 0:137634ff4186 | 495 | ripemd160_hmac_update( &ctx, input, ilen ); |
ansond | 0:137634ff4186 | 496 | ripemd160_hmac_finish( &ctx, output ); |
ansond | 0:137634ff4186 | 497 | ripemd160_free( &ctx ); |
ansond | 0:137634ff4186 | 498 | } |
ansond | 0:137634ff4186 | 499 | |
ansond | 0:137634ff4186 | 500 | |
ansond | 0:137634ff4186 | 501 | #if defined(POLARSSL_SELF_TEST) |
ansond | 0:137634ff4186 | 502 | /* |
ansond | 0:137634ff4186 | 503 | * Test vectors from the RIPEMD-160 paper and |
ansond | 0:137634ff4186 | 504 | * http://homes.esat.kuleuven.be/~bosselae/ripemd160.html#HMAC |
ansond | 0:137634ff4186 | 505 | */ |
ansond | 0:137634ff4186 | 506 | #define TESTS 8 |
ansond | 0:137634ff4186 | 507 | #define KEYS 2 |
ansond | 0:137634ff4186 | 508 | static const char *ripemd160_test_input[TESTS] = |
ansond | 0:137634ff4186 | 509 | { |
ansond | 0:137634ff4186 | 510 | "", |
ansond | 0:137634ff4186 | 511 | "a", |
ansond | 0:137634ff4186 | 512 | "abc", |
ansond | 0:137634ff4186 | 513 | "message digest", |
ansond | 0:137634ff4186 | 514 | "abcdefghijklmnopqrstuvwxyz", |
ansond | 0:137634ff4186 | 515 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
ansond | 0:137634ff4186 | 516 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", |
ansond | 0:137634ff4186 | 517 | "1234567890123456789012345678901234567890" |
ansond | 0:137634ff4186 | 518 | "1234567890123456789012345678901234567890", |
ansond | 0:137634ff4186 | 519 | }; |
ansond | 0:137634ff4186 | 520 | |
ansond | 0:137634ff4186 | 521 | static const unsigned char ripemd160_test_md[TESTS][20] = |
ansond | 0:137634ff4186 | 522 | { |
ansond | 0:137634ff4186 | 523 | { 0x9c, 0x11, 0x85, 0xa5, 0xc5, 0xe9, 0xfc, 0x54, 0x61, 0x28, |
ansond | 0:137634ff4186 | 524 | 0x08, 0x97, 0x7e, 0xe8, 0xf5, 0x48, 0xb2, 0x25, 0x8d, 0x31 }, |
ansond | 0:137634ff4186 | 525 | { 0x0b, 0xdc, 0x9d, 0x2d, 0x25, 0x6b, 0x3e, 0xe9, 0xda, 0xae, |
ansond | 0:137634ff4186 | 526 | 0x34, 0x7b, 0xe6, 0xf4, 0xdc, 0x83, 0x5a, 0x46, 0x7f, 0xfe }, |
ansond | 0:137634ff4186 | 527 | { 0x8e, 0xb2, 0x08, 0xf7, 0xe0, 0x5d, 0x98, 0x7a, 0x9b, 0x04, |
ansond | 0:137634ff4186 | 528 | 0x4a, 0x8e, 0x98, 0xc6, 0xb0, 0x87, 0xf1, 0x5a, 0x0b, 0xfc }, |
ansond | 0:137634ff4186 | 529 | { 0x5d, 0x06, 0x89, 0xef, 0x49, 0xd2, 0xfa, 0xe5, 0x72, 0xb8, |
ansond | 0:137634ff4186 | 530 | 0x81, 0xb1, 0x23, 0xa8, 0x5f, 0xfa, 0x21, 0x59, 0x5f, 0x36 }, |
ansond | 0:137634ff4186 | 531 | { 0xf7, 0x1c, 0x27, 0x10, 0x9c, 0x69, 0x2c, 0x1b, 0x56, 0xbb, |
ansond | 0:137634ff4186 | 532 | 0xdc, 0xeb, 0x5b, 0x9d, 0x28, 0x65, 0xb3, 0x70, 0x8d, 0xbc }, |
ansond | 0:137634ff4186 | 533 | { 0x12, 0xa0, 0x53, 0x38, 0x4a, 0x9c, 0x0c, 0x88, 0xe4, 0x05, |
ansond | 0:137634ff4186 | 534 | 0xa0, 0x6c, 0x27, 0xdc, 0xf4, 0x9a, 0xda, 0x62, 0xeb, 0x2b }, |
ansond | 0:137634ff4186 | 535 | { 0xb0, 0xe2, 0x0b, 0x6e, 0x31, 0x16, 0x64, 0x02, 0x86, 0xed, |
ansond | 0:137634ff4186 | 536 | 0x3a, 0x87, 0xa5, 0x71, 0x30, 0x79, 0xb2, 0x1f, 0x51, 0x89 }, |
ansond | 0:137634ff4186 | 537 | { 0x9b, 0x75, 0x2e, 0x45, 0x57, 0x3d, 0x4b, 0x39, 0xf4, 0xdb, |
ansond | 0:137634ff4186 | 538 | 0xd3, 0x32, 0x3c, 0xab, 0x82, 0xbf, 0x63, 0x32, 0x6b, 0xfb }, |
ansond | 0:137634ff4186 | 539 | }; |
ansond | 0:137634ff4186 | 540 | |
ansond | 0:137634ff4186 | 541 | static const unsigned char ripemd160_test_hmac[KEYS][TESTS][20] = |
ansond | 0:137634ff4186 | 542 | { |
ansond | 0:137634ff4186 | 543 | { |
ansond | 0:137634ff4186 | 544 | { 0xcf, 0x38, 0x76, 0x77, 0xbf, 0xda, 0x84, 0x83, 0xe6, 0x3b, |
ansond | 0:137634ff4186 | 545 | 0x57, 0xe0, 0x6c, 0x3b, 0x5e, 0xcd, 0x8b, 0x7f, 0xc0, 0x55 }, |
ansond | 0:137634ff4186 | 546 | { 0x0d, 0x35, 0x1d, 0x71, 0xb7, 0x8e, 0x36, 0xdb, 0xb7, 0x39, |
ansond | 0:137634ff4186 | 547 | 0x1c, 0x81, 0x0a, 0x0d, 0x2b, 0x62, 0x40, 0xdd, 0xba, 0xfc }, |
ansond | 0:137634ff4186 | 548 | { 0xf7, 0xef, 0x28, 0x8c, 0xb1, 0xbb, 0xcc, 0x61, 0x60, 0xd7, |
ansond | 0:137634ff4186 | 549 | 0x65, 0x07, 0xe0, 0xa3, 0xbb, 0xf7, 0x12, 0xfb, 0x67, 0xd6 }, |
ansond | 0:137634ff4186 | 550 | { 0xf8, 0x36, 0x62, 0xcc, 0x8d, 0x33, 0x9c, 0x22, 0x7e, 0x60, |
ansond | 0:137634ff4186 | 551 | 0x0f, 0xcd, 0x63, 0x6c, 0x57, 0xd2, 0x57, 0x1b, 0x1c, 0x34 }, |
ansond | 0:137634ff4186 | 552 | { 0x84, 0x3d, 0x1c, 0x4e, 0xb8, 0x80, 0xac, 0x8a, 0xc0, 0xc9, |
ansond | 0:137634ff4186 | 553 | 0xc9, 0x56, 0x96, 0x50, 0x79, 0x57, 0xd0, 0x15, 0x5d, 0xdb }, |
ansond | 0:137634ff4186 | 554 | { 0x60, 0xf5, 0xef, 0x19, 0x8a, 0x2d, 0xd5, 0x74, 0x55, 0x45, |
ansond | 0:137634ff4186 | 555 | 0xc1, 0xf0, 0xc4, 0x7a, 0xa3, 0xfb, 0x57, 0x76, 0xf8, 0x81 }, |
ansond | 0:137634ff4186 | 556 | { 0xe4, 0x9c, 0x13, 0x6a, 0x9e, 0x56, 0x27, 0xe0, 0x68, 0x1b, |
ansond | 0:137634ff4186 | 557 | 0x80, 0x8a, 0x3b, 0x97, 0xe6, 0xa6, 0xe6, 0x61, 0xae, 0x79 }, |
ansond | 0:137634ff4186 | 558 | { 0x31, 0xbe, 0x3c, 0xc9, 0x8c, 0xee, 0x37, 0xb7, 0x9b, 0x06, |
ansond | 0:137634ff4186 | 559 | 0x19, 0xe3, 0xe1, 0xc2, 0xbe, 0x4f, 0x1a, 0xa5, 0x6e, 0x6c }, |
ansond | 0:137634ff4186 | 560 | }, |
ansond | 0:137634ff4186 | 561 | { |
ansond | 0:137634ff4186 | 562 | { 0xfe, 0x69, 0xa6, 0x6c, 0x74, 0x23, 0xee, 0xa9, 0xc8, 0xfa, |
ansond | 0:137634ff4186 | 563 | 0x2e, 0xff, 0x8d, 0x9d, 0xaf, 0xb4, 0xf1, 0x7a, 0x62, 0xf5 }, |
ansond | 0:137634ff4186 | 564 | { 0x85, 0x74, 0x3e, 0x89, 0x9b, 0xc8, 0x2d, 0xbf, 0xa3, 0x6f, |
ansond | 0:137634ff4186 | 565 | 0xaa, 0xa7, 0xa2, 0x5b, 0x7c, 0xfd, 0x37, 0x24, 0x32, 0xcd }, |
ansond | 0:137634ff4186 | 566 | { 0x6e, 0x4a, 0xfd, 0x50, 0x1f, 0xa6, 0xb4, 0xa1, 0x82, 0x3c, |
ansond | 0:137634ff4186 | 567 | 0xa3, 0xb1, 0x0b, 0xd9, 0xaa, 0x0b, 0xa9, 0x7b, 0xa1, 0x82 }, |
ansond | 0:137634ff4186 | 568 | { 0x2e, 0x06, 0x6e, 0x62, 0x4b, 0xad, 0xb7, 0x6a, 0x18, 0x4c, |
ansond | 0:137634ff4186 | 569 | 0x8f, 0x90, 0xfb, 0xa0, 0x53, 0x33, 0x0e, 0x65, 0x0e, 0x92 }, |
ansond | 0:137634ff4186 | 570 | { 0x07, 0xe9, 0x42, 0xaa, 0x4e, 0x3c, 0xd7, 0xc0, 0x4d, 0xed, |
ansond | 0:137634ff4186 | 571 | 0xc1, 0xd4, 0x6e, 0x2e, 0x8c, 0xc4, 0xc7, 0x41, 0xb3, 0xd9 }, |
ansond | 0:137634ff4186 | 572 | { 0xb6, 0x58, 0x23, 0x18, 0xdd, 0xcf, 0xb6, 0x7a, 0x53, 0xa6, |
ansond | 0:137634ff4186 | 573 | 0x7d, 0x67, 0x6b, 0x8a, 0xd8, 0x69, 0xad, 0xed, 0x62, 0x9a }, |
ansond | 0:137634ff4186 | 574 | { 0xf1, 0xbe, 0x3e, 0xe8, 0x77, 0x70, 0x31, 0x40, 0xd3, 0x4f, |
ansond | 0:137634ff4186 | 575 | 0x97, 0xea, 0x1a, 0xb3, 0xa0, 0x7c, 0x14, 0x13, 0x33, 0xe2 }, |
ansond | 0:137634ff4186 | 576 | { 0x85, 0xf1, 0x64, 0x70, 0x3e, 0x61, 0xa6, 0x31, 0x31, 0xbe, |
ansond | 0:137634ff4186 | 577 | 0x7e, 0x45, 0x95, 0x8e, 0x07, 0x94, 0x12, 0x39, 0x04, 0xf9 }, |
ansond | 0:137634ff4186 | 578 | }, |
ansond | 0:137634ff4186 | 579 | }; |
ansond | 0:137634ff4186 | 580 | |
ansond | 0:137634ff4186 | 581 | static const unsigned char ripemd160_test_key[KEYS][20] = |
ansond | 0:137634ff4186 | 582 | { |
ansond | 0:137634ff4186 | 583 | { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, |
ansond | 0:137634ff4186 | 584 | 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x01, 0x23, 0x45, 0x67 }, |
ansond | 0:137634ff4186 | 585 | { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, |
ansond | 0:137634ff4186 | 586 | 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x00, 0x11, 0x22, 0x33 }, |
ansond | 0:137634ff4186 | 587 | }; |
ansond | 0:137634ff4186 | 588 | |
ansond | 0:137634ff4186 | 589 | /* |
ansond | 0:137634ff4186 | 590 | * Checkup routine |
ansond | 0:137634ff4186 | 591 | */ |
ansond | 0:137634ff4186 | 592 | int ripemd160_self_test( int verbose ) |
ansond | 0:137634ff4186 | 593 | { |
ansond | 0:137634ff4186 | 594 | int i, j; |
ansond | 0:137634ff4186 | 595 | unsigned char output[20]; |
ansond | 0:137634ff4186 | 596 | |
ansond | 0:137634ff4186 | 597 | memset( output, 0, sizeof output ); |
ansond | 0:137634ff4186 | 598 | |
ansond | 0:137634ff4186 | 599 | for( i = 0; i < TESTS; i++ ) |
ansond | 0:137634ff4186 | 600 | { |
ansond | 0:137634ff4186 | 601 | if( verbose != 0 ) |
ansond | 0:137634ff4186 | 602 | polarssl_printf( " RIPEMD-160 test #%d: ", i + 1 ); |
ansond | 0:137634ff4186 | 603 | |
ansond | 0:137634ff4186 | 604 | ripemd160( (const unsigned char *) ripemd160_test_input[i], |
ansond | 0:137634ff4186 | 605 | strlen( ripemd160_test_input[i] ), |
ansond | 0:137634ff4186 | 606 | output ); |
ansond | 0:137634ff4186 | 607 | |
ansond | 0:137634ff4186 | 608 | if( memcmp( output, ripemd160_test_md[i], 20 ) != 0 ) |
ansond | 0:137634ff4186 | 609 | { |
ansond | 0:137634ff4186 | 610 | if( verbose != 0 ) |
ansond | 0:137634ff4186 | 611 | polarssl_printf( "failed\n" ); |
ansond | 0:137634ff4186 | 612 | |
ansond | 0:137634ff4186 | 613 | return( 1 ); |
ansond | 0:137634ff4186 | 614 | } |
ansond | 0:137634ff4186 | 615 | |
ansond | 0:137634ff4186 | 616 | if( verbose != 0 ) |
ansond | 0:137634ff4186 | 617 | polarssl_printf( "passed\n" ); |
ansond | 0:137634ff4186 | 618 | |
ansond | 0:137634ff4186 | 619 | for( j = 0; j < KEYS; j++ ) |
ansond | 0:137634ff4186 | 620 | { |
ansond | 0:137634ff4186 | 621 | if( verbose != 0 ) |
ansond | 0:137634ff4186 | 622 | polarssl_printf( " HMAC-RIPEMD-160 test #%d, key #%d: ", |
ansond | 0:137634ff4186 | 623 | i + 1, j + 1 ); |
ansond | 0:137634ff4186 | 624 | |
ansond | 0:137634ff4186 | 625 | ripemd160_hmac( ripemd160_test_key[j], 20, |
ansond | 0:137634ff4186 | 626 | (const unsigned char *) ripemd160_test_input[i], |
ansond | 0:137634ff4186 | 627 | strlen( ripemd160_test_input[i] ), |
ansond | 0:137634ff4186 | 628 | output ); |
ansond | 0:137634ff4186 | 629 | |
ansond | 0:137634ff4186 | 630 | if( memcmp( output, ripemd160_test_hmac[j][i], 20 ) != 0 ) |
ansond | 0:137634ff4186 | 631 | { |
ansond | 0:137634ff4186 | 632 | if( verbose != 0 ) |
ansond | 0:137634ff4186 | 633 | polarssl_printf( "failed\n" ); |
ansond | 0:137634ff4186 | 634 | |
ansond | 0:137634ff4186 | 635 | return( 1 ); |
ansond | 0:137634ff4186 | 636 | } |
ansond | 0:137634ff4186 | 637 | |
ansond | 0:137634ff4186 | 638 | if( verbose != 0 ) |
ansond | 0:137634ff4186 | 639 | polarssl_printf( "passed\n" ); |
ansond | 0:137634ff4186 | 640 | } |
ansond | 0:137634ff4186 | 641 | |
ansond | 0:137634ff4186 | 642 | if( verbose != 0 ) |
ansond | 0:137634ff4186 | 643 | polarssl_printf( "\n" ); |
ansond | 0:137634ff4186 | 644 | } |
ansond | 0:137634ff4186 | 645 | |
ansond | 0:137634ff4186 | 646 | return( 0 ); |
ansond | 0:137634ff4186 | 647 | } |
ansond | 0:137634ff4186 | 648 | |
ansond | 0:137634ff4186 | 649 | #endif /* POLARSSL_SELF_TEST */ |
ansond | 0:137634ff4186 | 650 | |
ansond | 0:137634ff4186 | 651 | #endif /* POLARSSL_RIPEMD160_C */ |
ansond | 0:137634ff4186 | 652 |