mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
Parent:
0:5b88d5760320
updated based on mbed-os5.15.0

Who changed what in which revision?

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