BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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