The WDCInterface is is a drop-in replacement for an EthernetInterface class that allows the user to connect to the Internet with a Wistron NeWeb Corporation (WNC) M14A2A Series data module using the standard network Socket API's. This interface class is used in the AT&T Cellular IoT Starter Kit which is sold by Avnet (http://cloudconnectkits.org/product/att-cellular-iot-starter-kit).

Dependencies:   WncControllerK64F

Dependents:   WNCProximityMqtt Pubnub_ATT_IoT_SK_WNC_sync BluemixDemo BluemixQS ... more

See the WNCInterface README in the Wiki tab for detailed information on this library.

Committer:
JMF
Date:
Tue Nov 01 14:22:56 2016 +0000
Revision:
12:0071cb144c7a
Adding mbedtls files

Who changed what in which revision?

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