mbed TLS Build

Dependents:   Slave-prot-prod

Committer:
markrad
Date:
Thu Jan 05 00:18:44 2017 +0000
Revision:
0:cdf462088d13
Initial commit

Who changed what in which revision?

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