mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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