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