mbedtls ported to mbed-classic

Fork of mbedtls by Christopher Haster

Committer:
Christopher Haster
Date:
Fri Jan 22 16:44:49 2016 -0600
Revision:
1:24750b9ad5ef
Initial move of mbedtls to mercurial

Who changed what in which revision?

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