Hannes Tschofenig
/
aes-gcm-test-program
Example program to test AES-GCM functionality. Used for a workshop
SSL/library/ripemd160.c@0:796d0f61a05b, 2018-09-27 (annotated)
- Committer:
- HannesTschofenig
- Date:
- Thu Sep 27 06:34:22 2018 +0000
- Revision:
- 0:796d0f61a05b
Example AES-GCM test program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
HannesTschofenig | 0:796d0f61a05b | 1 | /* |
HannesTschofenig | 0:796d0f61a05b | 2 | * RIPE MD-160 implementation |
HannesTschofenig | 0:796d0f61a05b | 3 | * |
HannesTschofenig | 0:796d0f61a05b | 4 | * Copyright (C) 2014-2014, Brainspark B.V. |
HannesTschofenig | 0:796d0f61a05b | 5 | * |
HannesTschofenig | 0:796d0f61a05b | 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
HannesTschofenig | 0:796d0f61a05b | 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
HannesTschofenig | 0:796d0f61a05b | 8 | * |
HannesTschofenig | 0:796d0f61a05b | 9 | * All rights reserved. |
HannesTschofenig | 0:796d0f61a05b | 10 | * |
HannesTschofenig | 0:796d0f61a05b | 11 | * This program is free software; you can redistribute it and/or modify |
HannesTschofenig | 0:796d0f61a05b | 12 | * it under the terms of the GNU General Public License as published by |
HannesTschofenig | 0:796d0f61a05b | 13 | * the Free Software Foundation; either version 2 of the License, or |
HannesTschofenig | 0:796d0f61a05b | 14 | * (at your option) any later version. |
HannesTschofenig | 0:796d0f61a05b | 15 | * |
HannesTschofenig | 0:796d0f61a05b | 16 | * This program is distributed in the hope that it will be useful, |
HannesTschofenig | 0:796d0f61a05b | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
HannesTschofenig | 0:796d0f61a05b | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
HannesTschofenig | 0:796d0f61a05b | 19 | * GNU General Public License for more details. |
HannesTschofenig | 0:796d0f61a05b | 20 | * |
HannesTschofenig | 0:796d0f61a05b | 21 | * You should have received a copy of the GNU General Public License along |
HannesTschofenig | 0:796d0f61a05b | 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
HannesTschofenig | 0:796d0f61a05b | 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
HannesTschofenig | 0:796d0f61a05b | 24 | */ |
HannesTschofenig | 0:796d0f61a05b | 25 | |
HannesTschofenig | 0:796d0f61a05b | 26 | /* |
HannesTschofenig | 0:796d0f61a05b | 27 | * The RIPEMD-160 algorithm was designed by RIPE in 1996 |
HannesTschofenig | 0:796d0f61a05b | 28 | * http://homes.esat.kuleuven.be/~bosselae/ripemd160.html |
HannesTschofenig | 0:796d0f61a05b | 29 | * http://ehash.iaik.tugraz.at/wiki/RIPEMD-160 |
HannesTschofenig | 0:796d0f61a05b | 30 | */ |
HannesTschofenig | 0:796d0f61a05b | 31 | |
HannesTschofenig | 0:796d0f61a05b | 32 | #if !defined(POLARSSL_CONFIG_FILE) |
HannesTschofenig | 0:796d0f61a05b | 33 | #include "polarssl/config.h" |
HannesTschofenig | 0:796d0f61a05b | 34 | #else |
HannesTschofenig | 0:796d0f61a05b | 35 | #include POLARSSL_CONFIG_FILE |
HannesTschofenig | 0:796d0f61a05b | 36 | #endif |
HannesTschofenig | 0:796d0f61a05b | 37 | |
HannesTschofenig | 0:796d0f61a05b | 38 | #if defined(POLARSSL_RIPEMD160_C) |
HannesTschofenig | 0:796d0f61a05b | 39 | |
HannesTschofenig | 0:796d0f61a05b | 40 | #include "polarssl/ripemd160.h" |
HannesTschofenig | 0:796d0f61a05b | 41 | |
HannesTschofenig | 0:796d0f61a05b | 42 | #if defined(POLARSSL_FS_IO) || defined(POLARSSL_SELF_TEST) |
HannesTschofenig | 0:796d0f61a05b | 43 | #include <stdio.h> |
HannesTschofenig | 0:796d0f61a05b | 44 | #endif |
HannesTschofenig | 0:796d0f61a05b | 45 | |
HannesTschofenig | 0:796d0f61a05b | 46 | #if defined(POLARSSL_SELF_TEST) |
HannesTschofenig | 0:796d0f61a05b | 47 | #include <string.h> |
HannesTschofenig | 0:796d0f61a05b | 48 | #endif |
HannesTschofenig | 0:796d0f61a05b | 49 | |
HannesTschofenig | 0:796d0f61a05b | 50 | #if defined(POLARSSL_PLATFORM_C) |
HannesTschofenig | 0:796d0f61a05b | 51 | #include "polarssl/platform.h" |
HannesTschofenig | 0:796d0f61a05b | 52 | #else |
HannesTschofenig | 0:796d0f61a05b | 53 | #define polarssl_printf printf |
HannesTschofenig | 0:796d0f61a05b | 54 | #endif |
HannesTschofenig | 0:796d0f61a05b | 55 | |
HannesTschofenig | 0:796d0f61a05b | 56 | /* |
HannesTschofenig | 0:796d0f61a05b | 57 | * 32-bit integer manipulation macros (little endian) |
HannesTschofenig | 0:796d0f61a05b | 58 | */ |
HannesTschofenig | 0:796d0f61a05b | 59 | #ifndef GET_UINT32_LE |
HannesTschofenig | 0:796d0f61a05b | 60 | #define GET_UINT32_LE(n,b,i) \ |
HannesTschofenig | 0:796d0f61a05b | 61 | { \ |
HannesTschofenig | 0:796d0f61a05b | 62 | (n) = ( (uint32_t) (b)[(i) ] ) \ |
HannesTschofenig | 0:796d0f61a05b | 63 | | ( (uint32_t) (b)[(i) + 1] << 8 ) \ |
HannesTschofenig | 0:796d0f61a05b | 64 | | ( (uint32_t) (b)[(i) + 2] << 16 ) \ |
HannesTschofenig | 0:796d0f61a05b | 65 | | ( (uint32_t) (b)[(i) + 3] << 24 ); \ |
HannesTschofenig | 0:796d0f61a05b | 66 | } |
HannesTschofenig | 0:796d0f61a05b | 67 | #endif |
HannesTschofenig | 0:796d0f61a05b | 68 | |
HannesTschofenig | 0:796d0f61a05b | 69 | #ifndef PUT_UINT32_LE |
HannesTschofenig | 0:796d0f61a05b | 70 | #define PUT_UINT32_LE(n,b,i) \ |
HannesTschofenig | 0:796d0f61a05b | 71 | { \ |
HannesTschofenig | 0:796d0f61a05b | 72 | (b)[(i) ] = (unsigned char) ( (n) ); \ |
HannesTschofenig | 0:796d0f61a05b | 73 | (b)[(i) + 1] = (unsigned char) ( (n) >> 8 ); \ |
HannesTschofenig | 0:796d0f61a05b | 74 | (b)[(i) + 2] = (unsigned char) ( (n) >> 16 ); \ |
HannesTschofenig | 0:796d0f61a05b | 75 | (b)[(i) + 3] = (unsigned char) ( (n) >> 24 ); \ |
HannesTschofenig | 0:796d0f61a05b | 76 | } |
HannesTschofenig | 0:796d0f61a05b | 77 | #endif |
HannesTschofenig | 0:796d0f61a05b | 78 | |
HannesTschofenig | 0:796d0f61a05b | 79 | /* |
HannesTschofenig | 0:796d0f61a05b | 80 | * RIPEMD-160 context setup |
HannesTschofenig | 0:796d0f61a05b | 81 | */ |
HannesTschofenig | 0:796d0f61a05b | 82 | void ripemd160_starts( ripemd160_context *ctx ) |
HannesTschofenig | 0:796d0f61a05b | 83 | { |
HannesTschofenig | 0:796d0f61a05b | 84 | ctx->total[0] = 0; |
HannesTschofenig | 0:796d0f61a05b | 85 | ctx->total[1] = 0; |
HannesTschofenig | 0:796d0f61a05b | 86 | |
HannesTschofenig | 0:796d0f61a05b | 87 | ctx->state[0] = 0x67452301; |
HannesTschofenig | 0:796d0f61a05b | 88 | ctx->state[1] = 0xEFCDAB89; |
HannesTschofenig | 0:796d0f61a05b | 89 | ctx->state[2] = 0x98BADCFE; |
HannesTschofenig | 0:796d0f61a05b | 90 | ctx->state[3] = 0x10325476; |
HannesTschofenig | 0:796d0f61a05b | 91 | ctx->state[4] = 0xC3D2E1F0; |
HannesTschofenig | 0:796d0f61a05b | 92 | } |
HannesTschofenig | 0:796d0f61a05b | 93 | |
HannesTschofenig | 0:796d0f61a05b | 94 | /* |
HannesTschofenig | 0:796d0f61a05b | 95 | * Process one block |
HannesTschofenig | 0:796d0f61a05b | 96 | */ |
HannesTschofenig | 0:796d0f61a05b | 97 | void ripemd160_process( ripemd160_context *ctx, const unsigned char data[64] ) |
HannesTschofenig | 0:796d0f61a05b | 98 | { |
HannesTschofenig | 0:796d0f61a05b | 99 | uint32_t A, B, C, D, E, Ap, Bp, Cp, Dp, Ep, X[16]; |
HannesTschofenig | 0:796d0f61a05b | 100 | |
HannesTschofenig | 0:796d0f61a05b | 101 | GET_UINT32_LE( X[ 0], data, 0 ); |
HannesTschofenig | 0:796d0f61a05b | 102 | GET_UINT32_LE( X[ 1], data, 4 ); |
HannesTschofenig | 0:796d0f61a05b | 103 | GET_UINT32_LE( X[ 2], data, 8 ); |
HannesTschofenig | 0:796d0f61a05b | 104 | GET_UINT32_LE( X[ 3], data, 12 ); |
HannesTschofenig | 0:796d0f61a05b | 105 | GET_UINT32_LE( X[ 4], data, 16 ); |
HannesTschofenig | 0:796d0f61a05b | 106 | GET_UINT32_LE( X[ 5], data, 20 ); |
HannesTschofenig | 0:796d0f61a05b | 107 | GET_UINT32_LE( X[ 6], data, 24 ); |
HannesTschofenig | 0:796d0f61a05b | 108 | GET_UINT32_LE( X[ 7], data, 28 ); |
HannesTschofenig | 0:796d0f61a05b | 109 | GET_UINT32_LE( X[ 8], data, 32 ); |
HannesTschofenig | 0:796d0f61a05b | 110 | GET_UINT32_LE( X[ 9], data, 36 ); |
HannesTschofenig | 0:796d0f61a05b | 111 | GET_UINT32_LE( X[10], data, 40 ); |
HannesTschofenig | 0:796d0f61a05b | 112 | GET_UINT32_LE( X[11], data, 44 ); |
HannesTschofenig | 0:796d0f61a05b | 113 | GET_UINT32_LE( X[12], data, 48 ); |
HannesTschofenig | 0:796d0f61a05b | 114 | GET_UINT32_LE( X[13], data, 52 ); |
HannesTschofenig | 0:796d0f61a05b | 115 | GET_UINT32_LE( X[14], data, 56 ); |
HannesTschofenig | 0:796d0f61a05b | 116 | GET_UINT32_LE( X[15], data, 60 ); |
HannesTschofenig | 0:796d0f61a05b | 117 | |
HannesTschofenig | 0:796d0f61a05b | 118 | A = Ap = ctx->state[0]; |
HannesTschofenig | 0:796d0f61a05b | 119 | B = Bp = ctx->state[1]; |
HannesTschofenig | 0:796d0f61a05b | 120 | C = Cp = ctx->state[2]; |
HannesTschofenig | 0:796d0f61a05b | 121 | D = Dp = ctx->state[3]; |
HannesTschofenig | 0:796d0f61a05b | 122 | E = Ep = ctx->state[4]; |
HannesTschofenig | 0:796d0f61a05b | 123 | |
HannesTschofenig | 0:796d0f61a05b | 124 | #define F1( x, y, z ) ( x ^ y ^ z ) |
HannesTschofenig | 0:796d0f61a05b | 125 | #define F2( x, y, z ) ( ( x & y ) | ( ~x & z ) ) |
HannesTschofenig | 0:796d0f61a05b | 126 | #define F3( x, y, z ) ( ( x | ~y ) ^ z ) |
HannesTschofenig | 0:796d0f61a05b | 127 | #define F4( x, y, z ) ( ( x & z ) | ( y & ~z ) ) |
HannesTschofenig | 0:796d0f61a05b | 128 | #define F5( x, y, z ) ( x ^ ( y | ~z ) ) |
HannesTschofenig | 0:796d0f61a05b | 129 | |
HannesTschofenig | 0:796d0f61a05b | 130 | #define S( x, n ) ( ( x << n ) | ( x >> (32 - n) ) ) |
HannesTschofenig | 0:796d0f61a05b | 131 | |
HannesTschofenig | 0:796d0f61a05b | 132 | #define P( a, b, c, d, e, r, s, f, k ) \ |
HannesTschofenig | 0:796d0f61a05b | 133 | a += f( b, c, d ) + X[r] + k; \ |
HannesTschofenig | 0:796d0f61a05b | 134 | a = S( a, s ) + e; \ |
HannesTschofenig | 0:796d0f61a05b | 135 | c = S( c, 10 ); |
HannesTschofenig | 0:796d0f61a05b | 136 | |
HannesTschofenig | 0:796d0f61a05b | 137 | #define P2( a, b, c, d, e, r, s, rp, sp ) \ |
HannesTschofenig | 0:796d0f61a05b | 138 | P( a, b, c, d, e, r, s, F, K ); \ |
HannesTschofenig | 0:796d0f61a05b | 139 | P( a ## p, b ## p, c ## p, d ## p, e ## p, rp, sp, Fp, Kp ); |
HannesTschofenig | 0:796d0f61a05b | 140 | |
HannesTschofenig | 0:796d0f61a05b | 141 | #define F F1 |
HannesTschofenig | 0:796d0f61a05b | 142 | #define K 0x00000000 |
HannesTschofenig | 0:796d0f61a05b | 143 | #define Fp F5 |
HannesTschofenig | 0:796d0f61a05b | 144 | #define Kp 0x50A28BE6 |
HannesTschofenig | 0:796d0f61a05b | 145 | P2( A, B, C, D, E, 0, 11, 5, 8 ); |
HannesTschofenig | 0:796d0f61a05b | 146 | P2( E, A, B, C, D, 1, 14, 14, 9 ); |
HannesTschofenig | 0:796d0f61a05b | 147 | P2( D, E, A, B, C, 2, 15, 7, 9 ); |
HannesTschofenig | 0:796d0f61a05b | 148 | P2( C, D, E, A, B, 3, 12, 0, 11 ); |
HannesTschofenig | 0:796d0f61a05b | 149 | P2( B, C, D, E, A, 4, 5, 9, 13 ); |
HannesTschofenig | 0:796d0f61a05b | 150 | P2( A, B, C, D, E, 5, 8, 2, 15 ); |
HannesTschofenig | 0:796d0f61a05b | 151 | P2( E, A, B, C, D, 6, 7, 11, 15 ); |
HannesTschofenig | 0:796d0f61a05b | 152 | P2( D, E, A, B, C, 7, 9, 4, 5 ); |
HannesTschofenig | 0:796d0f61a05b | 153 | P2( C, D, E, A, B, 8, 11, 13, 7 ); |
HannesTschofenig | 0:796d0f61a05b | 154 | P2( B, C, D, E, A, 9, 13, 6, 7 ); |
HannesTschofenig | 0:796d0f61a05b | 155 | P2( A, B, C, D, E, 10, 14, 15, 8 ); |
HannesTschofenig | 0:796d0f61a05b | 156 | P2( E, A, B, C, D, 11, 15, 8, 11 ); |
HannesTschofenig | 0:796d0f61a05b | 157 | P2( D, E, A, B, C, 12, 6, 1, 14 ); |
HannesTschofenig | 0:796d0f61a05b | 158 | P2( C, D, E, A, B, 13, 7, 10, 14 ); |
HannesTschofenig | 0:796d0f61a05b | 159 | P2( B, C, D, E, A, 14, 9, 3, 12 ); |
HannesTschofenig | 0:796d0f61a05b | 160 | P2( A, B, C, D, E, 15, 8, 12, 6 ); |
HannesTschofenig | 0:796d0f61a05b | 161 | #undef F |
HannesTschofenig | 0:796d0f61a05b | 162 | #undef K |
HannesTschofenig | 0:796d0f61a05b | 163 | #undef Fp |
HannesTschofenig | 0:796d0f61a05b | 164 | #undef Kp |
HannesTschofenig | 0:796d0f61a05b | 165 | |
HannesTschofenig | 0:796d0f61a05b | 166 | #define F F2 |
HannesTschofenig | 0:796d0f61a05b | 167 | #define K 0x5A827999 |
HannesTschofenig | 0:796d0f61a05b | 168 | #define Fp F4 |
HannesTschofenig | 0:796d0f61a05b | 169 | #define Kp 0x5C4DD124 |
HannesTschofenig | 0:796d0f61a05b | 170 | P2( E, A, B, C, D, 7, 7, 6, 9 ); |
HannesTschofenig | 0:796d0f61a05b | 171 | P2( D, E, A, B, C, 4, 6, 11, 13 ); |
HannesTschofenig | 0:796d0f61a05b | 172 | P2( C, D, E, A, B, 13, 8, 3, 15 ); |
HannesTschofenig | 0:796d0f61a05b | 173 | P2( B, C, D, E, A, 1, 13, 7, 7 ); |
HannesTschofenig | 0:796d0f61a05b | 174 | P2( A, B, C, D, E, 10, 11, 0, 12 ); |
HannesTschofenig | 0:796d0f61a05b | 175 | P2( E, A, B, C, D, 6, 9, 13, 8 ); |
HannesTschofenig | 0:796d0f61a05b | 176 | P2( D, E, A, B, C, 15, 7, 5, 9 ); |
HannesTschofenig | 0:796d0f61a05b | 177 | P2( C, D, E, A, B, 3, 15, 10, 11 ); |
HannesTschofenig | 0:796d0f61a05b | 178 | P2( B, C, D, E, A, 12, 7, 14, 7 ); |
HannesTschofenig | 0:796d0f61a05b | 179 | P2( A, B, C, D, E, 0, 12, 15, 7 ); |
HannesTschofenig | 0:796d0f61a05b | 180 | P2( E, A, B, C, D, 9, 15, 8, 12 ); |
HannesTschofenig | 0:796d0f61a05b | 181 | P2( D, E, A, B, C, 5, 9, 12, 7 ); |
HannesTschofenig | 0:796d0f61a05b | 182 | P2( C, D, E, A, B, 2, 11, 4, 6 ); |
HannesTschofenig | 0:796d0f61a05b | 183 | P2( B, C, D, E, A, 14, 7, 9, 15 ); |
HannesTschofenig | 0:796d0f61a05b | 184 | P2( A, B, C, D, E, 11, 13, 1, 13 ); |
HannesTschofenig | 0:796d0f61a05b | 185 | P2( E, A, B, C, D, 8, 12, 2, 11 ); |
HannesTschofenig | 0:796d0f61a05b | 186 | #undef F |
HannesTschofenig | 0:796d0f61a05b | 187 | #undef K |
HannesTschofenig | 0:796d0f61a05b | 188 | #undef Fp |
HannesTschofenig | 0:796d0f61a05b | 189 | #undef Kp |
HannesTschofenig | 0:796d0f61a05b | 190 | |
HannesTschofenig | 0:796d0f61a05b | 191 | #define F F3 |
HannesTschofenig | 0:796d0f61a05b | 192 | #define K 0x6ED9EBA1 |
HannesTschofenig | 0:796d0f61a05b | 193 | #define Fp F3 |
HannesTschofenig | 0:796d0f61a05b | 194 | #define Kp 0x6D703EF3 |
HannesTschofenig | 0:796d0f61a05b | 195 | P2( D, E, A, B, C, 3, 11, 15, 9 ); |
HannesTschofenig | 0:796d0f61a05b | 196 | P2( C, D, E, A, B, 10, 13, 5, 7 ); |
HannesTschofenig | 0:796d0f61a05b | 197 | P2( B, C, D, E, A, 14, 6, 1, 15 ); |
HannesTschofenig | 0:796d0f61a05b | 198 | P2( A, B, C, D, E, 4, 7, 3, 11 ); |
HannesTschofenig | 0:796d0f61a05b | 199 | P2( E, A, B, C, D, 9, 14, 7, 8 ); |
HannesTschofenig | 0:796d0f61a05b | 200 | P2( D, E, A, B, C, 15, 9, 14, 6 ); |
HannesTschofenig | 0:796d0f61a05b | 201 | P2( C, D, E, A, B, 8, 13, 6, 6 ); |
HannesTschofenig | 0:796d0f61a05b | 202 | P2( B, C, D, E, A, 1, 15, 9, 14 ); |
HannesTschofenig | 0:796d0f61a05b | 203 | P2( A, B, C, D, E, 2, 14, 11, 12 ); |
HannesTschofenig | 0:796d0f61a05b | 204 | P2( E, A, B, C, D, 7, 8, 8, 13 ); |
HannesTschofenig | 0:796d0f61a05b | 205 | P2( D, E, A, B, C, 0, 13, 12, 5 ); |
HannesTschofenig | 0:796d0f61a05b | 206 | P2( C, D, E, A, B, 6, 6, 2, 14 ); |
HannesTschofenig | 0:796d0f61a05b | 207 | P2( B, C, D, E, A, 13, 5, 10, 13 ); |
HannesTschofenig | 0:796d0f61a05b | 208 | P2( A, B, C, D, E, 11, 12, 0, 13 ); |
HannesTschofenig | 0:796d0f61a05b | 209 | P2( E, A, B, C, D, 5, 7, 4, 7 ); |
HannesTschofenig | 0:796d0f61a05b | 210 | P2( D, E, A, B, C, 12, 5, 13, 5 ); |
HannesTschofenig | 0:796d0f61a05b | 211 | #undef F |
HannesTschofenig | 0:796d0f61a05b | 212 | #undef K |
HannesTschofenig | 0:796d0f61a05b | 213 | #undef Fp |
HannesTschofenig | 0:796d0f61a05b | 214 | #undef Kp |
HannesTschofenig | 0:796d0f61a05b | 215 | |
HannesTschofenig | 0:796d0f61a05b | 216 | #define F F4 |
HannesTschofenig | 0:796d0f61a05b | 217 | #define K 0x8F1BBCDC |
HannesTschofenig | 0:796d0f61a05b | 218 | #define Fp F2 |
HannesTschofenig | 0:796d0f61a05b | 219 | #define Kp 0x7A6D76E9 |
HannesTschofenig | 0:796d0f61a05b | 220 | P2( C, D, E, A, B, 1, 11, 8, 15 ); |
HannesTschofenig | 0:796d0f61a05b | 221 | P2( B, C, D, E, A, 9, 12, 6, 5 ); |
HannesTschofenig | 0:796d0f61a05b | 222 | P2( A, B, C, D, E, 11, 14, 4, 8 ); |
HannesTschofenig | 0:796d0f61a05b | 223 | P2( E, A, B, C, D, 10, 15, 1, 11 ); |
HannesTschofenig | 0:796d0f61a05b | 224 | P2( D, E, A, B, C, 0, 14, 3, 14 ); |
HannesTschofenig | 0:796d0f61a05b | 225 | P2( C, D, E, A, B, 8, 15, 11, 14 ); |
HannesTschofenig | 0:796d0f61a05b | 226 | P2( B, C, D, E, A, 12, 9, 15, 6 ); |
HannesTschofenig | 0:796d0f61a05b | 227 | P2( A, B, C, D, E, 4, 8, 0, 14 ); |
HannesTschofenig | 0:796d0f61a05b | 228 | P2( E, A, B, C, D, 13, 9, 5, 6 ); |
HannesTschofenig | 0:796d0f61a05b | 229 | P2( D, E, A, B, C, 3, 14, 12, 9 ); |
HannesTschofenig | 0:796d0f61a05b | 230 | P2( C, D, E, A, B, 7, 5, 2, 12 ); |
HannesTschofenig | 0:796d0f61a05b | 231 | P2( B, C, D, E, A, 15, 6, 13, 9 ); |
HannesTschofenig | 0:796d0f61a05b | 232 | P2( A, B, C, D, E, 14, 8, 9, 12 ); |
HannesTschofenig | 0:796d0f61a05b | 233 | P2( E, A, B, C, D, 5, 6, 7, 5 ); |
HannesTschofenig | 0:796d0f61a05b | 234 | P2( D, E, A, B, C, 6, 5, 10, 15 ); |
HannesTschofenig | 0:796d0f61a05b | 235 | P2( C, D, E, A, B, 2, 12, 14, 8 ); |
HannesTschofenig | 0:796d0f61a05b | 236 | #undef F |
HannesTschofenig | 0:796d0f61a05b | 237 | #undef K |
HannesTschofenig | 0:796d0f61a05b | 238 | #undef Fp |
HannesTschofenig | 0:796d0f61a05b | 239 | #undef Kp |
HannesTschofenig | 0:796d0f61a05b | 240 | |
HannesTschofenig | 0:796d0f61a05b | 241 | #define F F5 |
HannesTschofenig | 0:796d0f61a05b | 242 | #define K 0xA953FD4E |
HannesTschofenig | 0:796d0f61a05b | 243 | #define Fp F1 |
HannesTschofenig | 0:796d0f61a05b | 244 | #define Kp 0x00000000 |
HannesTschofenig | 0:796d0f61a05b | 245 | P2( B, C, D, E, A, 4, 9, 12, 8 ); |
HannesTschofenig | 0:796d0f61a05b | 246 | P2( A, B, C, D, E, 0, 15, 15, 5 ); |
HannesTschofenig | 0:796d0f61a05b | 247 | P2( E, A, B, C, D, 5, 5, 10, 12 ); |
HannesTschofenig | 0:796d0f61a05b | 248 | P2( D, E, A, B, C, 9, 11, 4, 9 ); |
HannesTschofenig | 0:796d0f61a05b | 249 | P2( C, D, E, A, B, 7, 6, 1, 12 ); |
HannesTschofenig | 0:796d0f61a05b | 250 | P2( B, C, D, E, A, 12, 8, 5, 5 ); |
HannesTschofenig | 0:796d0f61a05b | 251 | P2( A, B, C, D, E, 2, 13, 8, 14 ); |
HannesTschofenig | 0:796d0f61a05b | 252 | P2( E, A, B, C, D, 10, 12, 7, 6 ); |
HannesTschofenig | 0:796d0f61a05b | 253 | P2( D, E, A, B, C, 14, 5, 6, 8 ); |
HannesTschofenig | 0:796d0f61a05b | 254 | P2( C, D, E, A, B, 1, 12, 2, 13 ); |
HannesTschofenig | 0:796d0f61a05b | 255 | P2( B, C, D, E, A, 3, 13, 13, 6 ); |
HannesTschofenig | 0:796d0f61a05b | 256 | P2( A, B, C, D, E, 8, 14, 14, 5 ); |
HannesTschofenig | 0:796d0f61a05b | 257 | P2( E, A, B, C, D, 11, 11, 0, 15 ); |
HannesTschofenig | 0:796d0f61a05b | 258 | P2( D, E, A, B, C, 6, 8, 3, 13 ); |
HannesTschofenig | 0:796d0f61a05b | 259 | P2( C, D, E, A, B, 15, 5, 9, 11 ); |
HannesTschofenig | 0:796d0f61a05b | 260 | P2( B, C, D, E, A, 13, 6, 11, 11 ); |
HannesTschofenig | 0:796d0f61a05b | 261 | #undef F |
HannesTschofenig | 0:796d0f61a05b | 262 | #undef K |
HannesTschofenig | 0:796d0f61a05b | 263 | #undef Fp |
HannesTschofenig | 0:796d0f61a05b | 264 | #undef Kp |
HannesTschofenig | 0:796d0f61a05b | 265 | |
HannesTschofenig | 0:796d0f61a05b | 266 | C = ctx->state[1] + C + Dp; |
HannesTschofenig | 0:796d0f61a05b | 267 | ctx->state[1] = ctx->state[2] + D + Ep; |
HannesTschofenig | 0:796d0f61a05b | 268 | ctx->state[2] = ctx->state[3] + E + Ap; |
HannesTschofenig | 0:796d0f61a05b | 269 | ctx->state[3] = ctx->state[4] + A + Bp; |
HannesTschofenig | 0:796d0f61a05b | 270 | ctx->state[4] = ctx->state[0] + B + Cp; |
HannesTschofenig | 0:796d0f61a05b | 271 | ctx->state[0] = C; |
HannesTschofenig | 0:796d0f61a05b | 272 | } |
HannesTschofenig | 0:796d0f61a05b | 273 | |
HannesTschofenig | 0:796d0f61a05b | 274 | /* |
HannesTschofenig | 0:796d0f61a05b | 275 | * RIPEMD-160 process buffer |
HannesTschofenig | 0:796d0f61a05b | 276 | */ |
HannesTschofenig | 0:796d0f61a05b | 277 | void ripemd160_update( ripemd160_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 278 | const unsigned char *input, size_t ilen ) |
HannesTschofenig | 0:796d0f61a05b | 279 | { |
HannesTschofenig | 0:796d0f61a05b | 280 | size_t fill; |
HannesTschofenig | 0:796d0f61a05b | 281 | uint32_t left; |
HannesTschofenig | 0:796d0f61a05b | 282 | |
HannesTschofenig | 0:796d0f61a05b | 283 | if( ilen <= 0 ) |
HannesTschofenig | 0:796d0f61a05b | 284 | return; |
HannesTschofenig | 0:796d0f61a05b | 285 | |
HannesTschofenig | 0:796d0f61a05b | 286 | left = ctx->total[0] & 0x3F; |
HannesTschofenig | 0:796d0f61a05b | 287 | fill = 64 - left; |
HannesTschofenig | 0:796d0f61a05b | 288 | |
HannesTschofenig | 0:796d0f61a05b | 289 | ctx->total[0] += (uint32_t) ilen; |
HannesTschofenig | 0:796d0f61a05b | 290 | ctx->total[0] &= 0xFFFFFFFF; |
HannesTschofenig | 0:796d0f61a05b | 291 | |
HannesTschofenig | 0:796d0f61a05b | 292 | if( ctx->total[0] < (uint32_t) ilen ) |
HannesTschofenig | 0:796d0f61a05b | 293 | ctx->total[1]++; |
HannesTschofenig | 0:796d0f61a05b | 294 | |
HannesTschofenig | 0:796d0f61a05b | 295 | if( left && ilen >= fill ) |
HannesTschofenig | 0:796d0f61a05b | 296 | { |
HannesTschofenig | 0:796d0f61a05b | 297 | memcpy( (void *) (ctx->buffer + left), input, fill ); |
HannesTschofenig | 0:796d0f61a05b | 298 | ripemd160_process( ctx, ctx->buffer ); |
HannesTschofenig | 0:796d0f61a05b | 299 | input += fill; |
HannesTschofenig | 0:796d0f61a05b | 300 | ilen -= fill; |
HannesTschofenig | 0:796d0f61a05b | 301 | left = 0; |
HannesTschofenig | 0:796d0f61a05b | 302 | } |
HannesTschofenig | 0:796d0f61a05b | 303 | |
HannesTschofenig | 0:796d0f61a05b | 304 | while( ilen >= 64 ) |
HannesTschofenig | 0:796d0f61a05b | 305 | { |
HannesTschofenig | 0:796d0f61a05b | 306 | ripemd160_process( ctx, input ); |
HannesTschofenig | 0:796d0f61a05b | 307 | input += 64; |
HannesTschofenig | 0:796d0f61a05b | 308 | ilen -= 64; |
HannesTschofenig | 0:796d0f61a05b | 309 | } |
HannesTschofenig | 0:796d0f61a05b | 310 | |
HannesTschofenig | 0:796d0f61a05b | 311 | if( ilen > 0 ) |
HannesTschofenig | 0:796d0f61a05b | 312 | { |
HannesTschofenig | 0:796d0f61a05b | 313 | memcpy( (void *) (ctx->buffer + left), input, ilen ); |
HannesTschofenig | 0:796d0f61a05b | 314 | } |
HannesTschofenig | 0:796d0f61a05b | 315 | } |
HannesTschofenig | 0:796d0f61a05b | 316 | |
HannesTschofenig | 0:796d0f61a05b | 317 | static const unsigned char ripemd160_padding[64] = |
HannesTschofenig | 0:796d0f61a05b | 318 | { |
HannesTschofenig | 0:796d0f61a05b | 319 | 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
HannesTschofenig | 0:796d0f61a05b | 320 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
HannesTschofenig | 0:796d0f61a05b | 321 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
HannesTschofenig | 0:796d0f61a05b | 322 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
HannesTschofenig | 0:796d0f61a05b | 323 | }; |
HannesTschofenig | 0:796d0f61a05b | 324 | |
HannesTschofenig | 0:796d0f61a05b | 325 | /* |
HannesTschofenig | 0:796d0f61a05b | 326 | * RIPEMD-160 final digest |
HannesTschofenig | 0:796d0f61a05b | 327 | */ |
HannesTschofenig | 0:796d0f61a05b | 328 | void ripemd160_finish( ripemd160_context *ctx, unsigned char output[20] ) |
HannesTschofenig | 0:796d0f61a05b | 329 | { |
HannesTschofenig | 0:796d0f61a05b | 330 | uint32_t last, padn; |
HannesTschofenig | 0:796d0f61a05b | 331 | uint32_t high, low; |
HannesTschofenig | 0:796d0f61a05b | 332 | unsigned char msglen[8]; |
HannesTschofenig | 0:796d0f61a05b | 333 | |
HannesTschofenig | 0:796d0f61a05b | 334 | high = ( ctx->total[0] >> 29 ) |
HannesTschofenig | 0:796d0f61a05b | 335 | | ( ctx->total[1] << 3 ); |
HannesTschofenig | 0:796d0f61a05b | 336 | low = ( ctx->total[0] << 3 ); |
HannesTschofenig | 0:796d0f61a05b | 337 | |
HannesTschofenig | 0:796d0f61a05b | 338 | PUT_UINT32_LE( low, msglen, 0 ); |
HannesTschofenig | 0:796d0f61a05b | 339 | PUT_UINT32_LE( high, msglen, 4 ); |
HannesTschofenig | 0:796d0f61a05b | 340 | |
HannesTschofenig | 0:796d0f61a05b | 341 | last = ctx->total[0] & 0x3F; |
HannesTschofenig | 0:796d0f61a05b | 342 | padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); |
HannesTschofenig | 0:796d0f61a05b | 343 | |
HannesTschofenig | 0:796d0f61a05b | 344 | ripemd160_update( ctx, ripemd160_padding, padn ); |
HannesTschofenig | 0:796d0f61a05b | 345 | ripemd160_update( ctx, msglen, 8 ); |
HannesTschofenig | 0:796d0f61a05b | 346 | |
HannesTschofenig | 0:796d0f61a05b | 347 | PUT_UINT32_LE( ctx->state[0], output, 0 ); |
HannesTschofenig | 0:796d0f61a05b | 348 | PUT_UINT32_LE( ctx->state[1], output, 4 ); |
HannesTschofenig | 0:796d0f61a05b | 349 | PUT_UINT32_LE( ctx->state[2], output, 8 ); |
HannesTschofenig | 0:796d0f61a05b | 350 | PUT_UINT32_LE( ctx->state[3], output, 12 ); |
HannesTschofenig | 0:796d0f61a05b | 351 | PUT_UINT32_LE( ctx->state[4], output, 16 ); |
HannesTschofenig | 0:796d0f61a05b | 352 | } |
HannesTschofenig | 0:796d0f61a05b | 353 | |
HannesTschofenig | 0:796d0f61a05b | 354 | /* |
HannesTschofenig | 0:796d0f61a05b | 355 | * output = RIPEMD-160( input buffer ) |
HannesTschofenig | 0:796d0f61a05b | 356 | */ |
HannesTschofenig | 0:796d0f61a05b | 357 | void ripemd160( const unsigned char *input, size_t ilen, |
HannesTschofenig | 0:796d0f61a05b | 358 | unsigned char output[20] ) |
HannesTschofenig | 0:796d0f61a05b | 359 | { |
HannesTschofenig | 0:796d0f61a05b | 360 | ripemd160_context ctx; |
HannesTschofenig | 0:796d0f61a05b | 361 | |
HannesTschofenig | 0:796d0f61a05b | 362 | ripemd160_starts( &ctx ); |
HannesTschofenig | 0:796d0f61a05b | 363 | ripemd160_update( &ctx, input, ilen ); |
HannesTschofenig | 0:796d0f61a05b | 364 | ripemd160_finish( &ctx, output ); |
HannesTschofenig | 0:796d0f61a05b | 365 | |
HannesTschofenig | 0:796d0f61a05b | 366 | memset( &ctx, 0, sizeof( ripemd160_context ) ); |
HannesTschofenig | 0:796d0f61a05b | 367 | } |
HannesTschofenig | 0:796d0f61a05b | 368 | |
HannesTschofenig | 0:796d0f61a05b | 369 | #if defined(POLARSSL_FS_IO) |
HannesTschofenig | 0:796d0f61a05b | 370 | /* |
HannesTschofenig | 0:796d0f61a05b | 371 | * output = RIPEMD-160( file contents ) |
HannesTschofenig | 0:796d0f61a05b | 372 | */ |
HannesTschofenig | 0:796d0f61a05b | 373 | int ripemd160_file( const char *path, unsigned char output[20] ) |
HannesTschofenig | 0:796d0f61a05b | 374 | { |
HannesTschofenig | 0:796d0f61a05b | 375 | FILE *f; |
HannesTschofenig | 0:796d0f61a05b | 376 | size_t n; |
HannesTschofenig | 0:796d0f61a05b | 377 | ripemd160_context ctx; |
HannesTschofenig | 0:796d0f61a05b | 378 | unsigned char buf[1024]; |
HannesTschofenig | 0:796d0f61a05b | 379 | |
HannesTschofenig | 0:796d0f61a05b | 380 | if( ( f = fopen( path, "rb" ) ) == NULL ) |
HannesTschofenig | 0:796d0f61a05b | 381 | return( POLARSSL_ERR_RIPEMD160_FILE_IO_ERROR ); |
HannesTschofenig | 0:796d0f61a05b | 382 | |
HannesTschofenig | 0:796d0f61a05b | 383 | ripemd160_starts( &ctx ); |
HannesTschofenig | 0:796d0f61a05b | 384 | |
HannesTschofenig | 0:796d0f61a05b | 385 | while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) |
HannesTschofenig | 0:796d0f61a05b | 386 | ripemd160_update( &ctx, buf, n ); |
HannesTschofenig | 0:796d0f61a05b | 387 | |
HannesTschofenig | 0:796d0f61a05b | 388 | ripemd160_finish( &ctx, output ); |
HannesTschofenig | 0:796d0f61a05b | 389 | |
HannesTschofenig | 0:796d0f61a05b | 390 | memset( &ctx, 0, sizeof( ripemd160_context ) ); |
HannesTschofenig | 0:796d0f61a05b | 391 | |
HannesTschofenig | 0:796d0f61a05b | 392 | if( ferror( f ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 393 | { |
HannesTschofenig | 0:796d0f61a05b | 394 | fclose( f ); |
HannesTschofenig | 0:796d0f61a05b | 395 | return( POLARSSL_ERR_RIPEMD160_FILE_IO_ERROR ); |
HannesTschofenig | 0:796d0f61a05b | 396 | } |
HannesTschofenig | 0:796d0f61a05b | 397 | |
HannesTschofenig | 0:796d0f61a05b | 398 | fclose( f ); |
HannesTschofenig | 0:796d0f61a05b | 399 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 400 | } |
HannesTschofenig | 0:796d0f61a05b | 401 | #endif /* POLARSSL_FS_IO */ |
HannesTschofenig | 0:796d0f61a05b | 402 | |
HannesTschofenig | 0:796d0f61a05b | 403 | /* |
HannesTschofenig | 0:796d0f61a05b | 404 | * RIPEMD-160 HMAC context setup |
HannesTschofenig | 0:796d0f61a05b | 405 | */ |
HannesTschofenig | 0:796d0f61a05b | 406 | void ripemd160_hmac_starts( ripemd160_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 407 | const unsigned char *key, size_t keylen ) |
HannesTschofenig | 0:796d0f61a05b | 408 | { |
HannesTschofenig | 0:796d0f61a05b | 409 | size_t i; |
HannesTschofenig | 0:796d0f61a05b | 410 | unsigned char sum[20]; |
HannesTschofenig | 0:796d0f61a05b | 411 | |
HannesTschofenig | 0:796d0f61a05b | 412 | if( keylen > 64 ) |
HannesTschofenig | 0:796d0f61a05b | 413 | { |
HannesTschofenig | 0:796d0f61a05b | 414 | ripemd160( key, keylen, sum ); |
HannesTschofenig | 0:796d0f61a05b | 415 | keylen = 20; |
HannesTschofenig | 0:796d0f61a05b | 416 | key = sum; |
HannesTschofenig | 0:796d0f61a05b | 417 | } |
HannesTschofenig | 0:796d0f61a05b | 418 | |
HannesTschofenig | 0:796d0f61a05b | 419 | memset( ctx->ipad, 0x36, 64 ); |
HannesTschofenig | 0:796d0f61a05b | 420 | memset( ctx->opad, 0x5C, 64 ); |
HannesTschofenig | 0:796d0f61a05b | 421 | |
HannesTschofenig | 0:796d0f61a05b | 422 | for( i = 0; i < keylen; i++ ) |
HannesTschofenig | 0:796d0f61a05b | 423 | { |
HannesTschofenig | 0:796d0f61a05b | 424 | ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] ); |
HannesTschofenig | 0:796d0f61a05b | 425 | ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] ); |
HannesTschofenig | 0:796d0f61a05b | 426 | } |
HannesTschofenig | 0:796d0f61a05b | 427 | |
HannesTschofenig | 0:796d0f61a05b | 428 | ripemd160_starts( ctx ); |
HannesTschofenig | 0:796d0f61a05b | 429 | ripemd160_update( ctx, ctx->ipad, 64 ); |
HannesTschofenig | 0:796d0f61a05b | 430 | |
HannesTschofenig | 0:796d0f61a05b | 431 | memset( sum, 0, sizeof( sum ) ); |
HannesTschofenig | 0:796d0f61a05b | 432 | } |
HannesTschofenig | 0:796d0f61a05b | 433 | |
HannesTschofenig | 0:796d0f61a05b | 434 | /* |
HannesTschofenig | 0:796d0f61a05b | 435 | * RIPEMD-160 HMAC process buffer |
HannesTschofenig | 0:796d0f61a05b | 436 | */ |
HannesTschofenig | 0:796d0f61a05b | 437 | void ripemd160_hmac_update( ripemd160_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 438 | const unsigned char *input, size_t ilen ) |
HannesTschofenig | 0:796d0f61a05b | 439 | { |
HannesTschofenig | 0:796d0f61a05b | 440 | ripemd160_update( ctx, input, ilen ); |
HannesTschofenig | 0:796d0f61a05b | 441 | } |
HannesTschofenig | 0:796d0f61a05b | 442 | |
HannesTschofenig | 0:796d0f61a05b | 443 | /* |
HannesTschofenig | 0:796d0f61a05b | 444 | * RIPEMD-160 HMAC final digest |
HannesTschofenig | 0:796d0f61a05b | 445 | */ |
HannesTschofenig | 0:796d0f61a05b | 446 | void ripemd160_hmac_finish( ripemd160_context *ctx, unsigned char output[20] ) |
HannesTschofenig | 0:796d0f61a05b | 447 | { |
HannesTschofenig | 0:796d0f61a05b | 448 | unsigned char tmpbuf[20]; |
HannesTschofenig | 0:796d0f61a05b | 449 | |
HannesTschofenig | 0:796d0f61a05b | 450 | ripemd160_finish( ctx, tmpbuf ); |
HannesTschofenig | 0:796d0f61a05b | 451 | ripemd160_starts( ctx ); |
HannesTschofenig | 0:796d0f61a05b | 452 | ripemd160_update( ctx, ctx->opad, 64 ); |
HannesTschofenig | 0:796d0f61a05b | 453 | ripemd160_update( ctx, tmpbuf, 20 ); |
HannesTschofenig | 0:796d0f61a05b | 454 | ripemd160_finish( ctx, output ); |
HannesTschofenig | 0:796d0f61a05b | 455 | |
HannesTschofenig | 0:796d0f61a05b | 456 | memset( tmpbuf, 0, sizeof( tmpbuf ) ); |
HannesTschofenig | 0:796d0f61a05b | 457 | } |
HannesTschofenig | 0:796d0f61a05b | 458 | |
HannesTschofenig | 0:796d0f61a05b | 459 | /* |
HannesTschofenig | 0:796d0f61a05b | 460 | * RIPEMD-160 HMAC context reset |
HannesTschofenig | 0:796d0f61a05b | 461 | */ |
HannesTschofenig | 0:796d0f61a05b | 462 | void ripemd160_hmac_reset( ripemd160_context *ctx ) |
HannesTschofenig | 0:796d0f61a05b | 463 | { |
HannesTschofenig | 0:796d0f61a05b | 464 | ripemd160_starts( ctx ); |
HannesTschofenig | 0:796d0f61a05b | 465 | ripemd160_update( ctx, ctx->ipad, 64 ); |
HannesTschofenig | 0:796d0f61a05b | 466 | } |
HannesTschofenig | 0:796d0f61a05b | 467 | |
HannesTschofenig | 0:796d0f61a05b | 468 | /* |
HannesTschofenig | 0:796d0f61a05b | 469 | * output = HMAC-RIPEMD-160( hmac key, input buffer ) |
HannesTschofenig | 0:796d0f61a05b | 470 | */ |
HannesTschofenig | 0:796d0f61a05b | 471 | void ripemd160_hmac( const unsigned char *key, size_t keylen, |
HannesTschofenig | 0:796d0f61a05b | 472 | const unsigned char *input, size_t ilen, |
HannesTschofenig | 0:796d0f61a05b | 473 | unsigned char output[20] ) |
HannesTschofenig | 0:796d0f61a05b | 474 | { |
HannesTschofenig | 0:796d0f61a05b | 475 | ripemd160_context ctx; |
HannesTschofenig | 0:796d0f61a05b | 476 | |
HannesTschofenig | 0:796d0f61a05b | 477 | ripemd160_hmac_starts( &ctx, key, keylen ); |
HannesTschofenig | 0:796d0f61a05b | 478 | ripemd160_hmac_update( &ctx, input, ilen ); |
HannesTschofenig | 0:796d0f61a05b | 479 | ripemd160_hmac_finish( &ctx, output ); |
HannesTschofenig | 0:796d0f61a05b | 480 | |
HannesTschofenig | 0:796d0f61a05b | 481 | memset( &ctx, 0, sizeof( ripemd160_context ) ); |
HannesTschofenig | 0:796d0f61a05b | 482 | } |
HannesTschofenig | 0:796d0f61a05b | 483 | |
HannesTschofenig | 0:796d0f61a05b | 484 | |
HannesTschofenig | 0:796d0f61a05b | 485 | #if defined(POLARSSL_SELF_TEST) |
HannesTschofenig | 0:796d0f61a05b | 486 | /* |
HannesTschofenig | 0:796d0f61a05b | 487 | * Test vectors from the RIPEMD-160 paper and |
HannesTschofenig | 0:796d0f61a05b | 488 | * http://homes.esat.kuleuven.be/~bosselae/ripemd160.html#HMAC |
HannesTschofenig | 0:796d0f61a05b | 489 | */ |
HannesTschofenig | 0:796d0f61a05b | 490 | #define TESTS 8 |
HannesTschofenig | 0:796d0f61a05b | 491 | #define KEYS 2 |
HannesTschofenig | 0:796d0f61a05b | 492 | static const char *ripemd160_test_input[TESTS] = |
HannesTschofenig | 0:796d0f61a05b | 493 | { |
HannesTschofenig | 0:796d0f61a05b | 494 | "", |
HannesTschofenig | 0:796d0f61a05b | 495 | "a", |
HannesTschofenig | 0:796d0f61a05b | 496 | "abc", |
HannesTschofenig | 0:796d0f61a05b | 497 | "message digest", |
HannesTschofenig | 0:796d0f61a05b | 498 | "abcdefghijklmnopqrstuvwxyz", |
HannesTschofenig | 0:796d0f61a05b | 499 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
HannesTschofenig | 0:796d0f61a05b | 500 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", |
HannesTschofenig | 0:796d0f61a05b | 501 | "1234567890123456789012345678901234567890" |
HannesTschofenig | 0:796d0f61a05b | 502 | "1234567890123456789012345678901234567890", |
HannesTschofenig | 0:796d0f61a05b | 503 | }; |
HannesTschofenig | 0:796d0f61a05b | 504 | |
HannesTschofenig | 0:796d0f61a05b | 505 | static const unsigned char ripemd160_test_md[TESTS][20] = |
HannesTschofenig | 0:796d0f61a05b | 506 | { |
HannesTschofenig | 0:796d0f61a05b | 507 | { 0x9c, 0x11, 0x85, 0xa5, 0xc5, 0xe9, 0xfc, 0x54, 0x61, 0x28, |
HannesTschofenig | 0:796d0f61a05b | 508 | 0x08, 0x97, 0x7e, 0xe8, 0xf5, 0x48, 0xb2, 0x25, 0x8d, 0x31 }, |
HannesTschofenig | 0:796d0f61a05b | 509 | { 0x0b, 0xdc, 0x9d, 0x2d, 0x25, 0x6b, 0x3e, 0xe9, 0xda, 0xae, |
HannesTschofenig | 0:796d0f61a05b | 510 | 0x34, 0x7b, 0xe6, 0xf4, 0xdc, 0x83, 0x5a, 0x46, 0x7f, 0xfe }, |
HannesTschofenig | 0:796d0f61a05b | 511 | { 0x8e, 0xb2, 0x08, 0xf7, 0xe0, 0x5d, 0x98, 0x7a, 0x9b, 0x04, |
HannesTschofenig | 0:796d0f61a05b | 512 | 0x4a, 0x8e, 0x98, 0xc6, 0xb0, 0x87, 0xf1, 0x5a, 0x0b, 0xfc }, |
HannesTschofenig | 0:796d0f61a05b | 513 | { 0x5d, 0x06, 0x89, 0xef, 0x49, 0xd2, 0xfa, 0xe5, 0x72, 0xb8, |
HannesTschofenig | 0:796d0f61a05b | 514 | 0x81, 0xb1, 0x23, 0xa8, 0x5f, 0xfa, 0x21, 0x59, 0x5f, 0x36 }, |
HannesTschofenig | 0:796d0f61a05b | 515 | { 0xf7, 0x1c, 0x27, 0x10, 0x9c, 0x69, 0x2c, 0x1b, 0x56, 0xbb, |
HannesTschofenig | 0:796d0f61a05b | 516 | 0xdc, 0xeb, 0x5b, 0x9d, 0x28, 0x65, 0xb3, 0x70, 0x8d, 0xbc }, |
HannesTschofenig | 0:796d0f61a05b | 517 | { 0x12, 0xa0, 0x53, 0x38, 0x4a, 0x9c, 0x0c, 0x88, 0xe4, 0x05, |
HannesTschofenig | 0:796d0f61a05b | 518 | 0xa0, 0x6c, 0x27, 0xdc, 0xf4, 0x9a, 0xda, 0x62, 0xeb, 0x2b }, |
HannesTschofenig | 0:796d0f61a05b | 519 | { 0xb0, 0xe2, 0x0b, 0x6e, 0x31, 0x16, 0x64, 0x02, 0x86, 0xed, |
HannesTschofenig | 0:796d0f61a05b | 520 | 0x3a, 0x87, 0xa5, 0x71, 0x30, 0x79, 0xb2, 0x1f, 0x51, 0x89 }, |
HannesTschofenig | 0:796d0f61a05b | 521 | { 0x9b, 0x75, 0x2e, 0x45, 0x57, 0x3d, 0x4b, 0x39, 0xf4, 0xdb, |
HannesTschofenig | 0:796d0f61a05b | 522 | 0xd3, 0x32, 0x3c, 0xab, 0x82, 0xbf, 0x63, 0x32, 0x6b, 0xfb }, |
HannesTschofenig | 0:796d0f61a05b | 523 | }; |
HannesTschofenig | 0:796d0f61a05b | 524 | |
HannesTschofenig | 0:796d0f61a05b | 525 | static const unsigned char ripemd160_test_hmac[KEYS][TESTS][20] = |
HannesTschofenig | 0:796d0f61a05b | 526 | { |
HannesTschofenig | 0:796d0f61a05b | 527 | { |
HannesTschofenig | 0:796d0f61a05b | 528 | { 0xcf, 0x38, 0x76, 0x77, 0xbf, 0xda, 0x84, 0x83, 0xe6, 0x3b, |
HannesTschofenig | 0:796d0f61a05b | 529 | 0x57, 0xe0, 0x6c, 0x3b, 0x5e, 0xcd, 0x8b, 0x7f, 0xc0, 0x55 }, |
HannesTschofenig | 0:796d0f61a05b | 530 | { 0x0d, 0x35, 0x1d, 0x71, 0xb7, 0x8e, 0x36, 0xdb, 0xb7, 0x39, |
HannesTschofenig | 0:796d0f61a05b | 531 | 0x1c, 0x81, 0x0a, 0x0d, 0x2b, 0x62, 0x40, 0xdd, 0xba, 0xfc }, |
HannesTschofenig | 0:796d0f61a05b | 532 | { 0xf7, 0xef, 0x28, 0x8c, 0xb1, 0xbb, 0xcc, 0x61, 0x60, 0xd7, |
HannesTschofenig | 0:796d0f61a05b | 533 | 0x65, 0x07, 0xe0, 0xa3, 0xbb, 0xf7, 0x12, 0xfb, 0x67, 0xd6 }, |
HannesTschofenig | 0:796d0f61a05b | 534 | { 0xf8, 0x36, 0x62, 0xcc, 0x8d, 0x33, 0x9c, 0x22, 0x7e, 0x60, |
HannesTschofenig | 0:796d0f61a05b | 535 | 0x0f, 0xcd, 0x63, 0x6c, 0x57, 0xd2, 0x57, 0x1b, 0x1c, 0x34 }, |
HannesTschofenig | 0:796d0f61a05b | 536 | { 0x84, 0x3d, 0x1c, 0x4e, 0xb8, 0x80, 0xac, 0x8a, 0xc0, 0xc9, |
HannesTschofenig | 0:796d0f61a05b | 537 | 0xc9, 0x56, 0x96, 0x50, 0x79, 0x57, 0xd0, 0x15, 0x5d, 0xdb }, |
HannesTschofenig | 0:796d0f61a05b | 538 | { 0x60, 0xf5, 0xef, 0x19, 0x8a, 0x2d, 0xd5, 0x74, 0x55, 0x45, |
HannesTschofenig | 0:796d0f61a05b | 539 | 0xc1, 0xf0, 0xc4, 0x7a, 0xa3, 0xfb, 0x57, 0x76, 0xf8, 0x81 }, |
HannesTschofenig | 0:796d0f61a05b | 540 | { 0xe4, 0x9c, 0x13, 0x6a, 0x9e, 0x56, 0x27, 0xe0, 0x68, 0x1b, |
HannesTschofenig | 0:796d0f61a05b | 541 | 0x80, 0x8a, 0x3b, 0x97, 0xe6, 0xa6, 0xe6, 0x61, 0xae, 0x79 }, |
HannesTschofenig | 0:796d0f61a05b | 542 | { 0x31, 0xbe, 0x3c, 0xc9, 0x8c, 0xee, 0x37, 0xb7, 0x9b, 0x06, |
HannesTschofenig | 0:796d0f61a05b | 543 | 0x19, 0xe3, 0xe1, 0xc2, 0xbe, 0x4f, 0x1a, 0xa5, 0x6e, 0x6c }, |
HannesTschofenig | 0:796d0f61a05b | 544 | }, |
HannesTschofenig | 0:796d0f61a05b | 545 | { |
HannesTschofenig | 0:796d0f61a05b | 546 | { 0xfe, 0x69, 0xa6, 0x6c, 0x74, 0x23, 0xee, 0xa9, 0xc8, 0xfa, |
HannesTschofenig | 0:796d0f61a05b | 547 | 0x2e, 0xff, 0x8d, 0x9d, 0xaf, 0xb4, 0xf1, 0x7a, 0x62, 0xf5 }, |
HannesTschofenig | 0:796d0f61a05b | 548 | { 0x85, 0x74, 0x3e, 0x89, 0x9b, 0xc8, 0x2d, 0xbf, 0xa3, 0x6f, |
HannesTschofenig | 0:796d0f61a05b | 549 | 0xaa, 0xa7, 0xa2, 0x5b, 0x7c, 0xfd, 0x37, 0x24, 0x32, 0xcd }, |
HannesTschofenig | 0:796d0f61a05b | 550 | { 0x6e, 0x4a, 0xfd, 0x50, 0x1f, 0xa6, 0xb4, 0xa1, 0x82, 0x3c, |
HannesTschofenig | 0:796d0f61a05b | 551 | 0xa3, 0xb1, 0x0b, 0xd9, 0xaa, 0x0b, 0xa9, 0x7b, 0xa1, 0x82 }, |
HannesTschofenig | 0:796d0f61a05b | 552 | { 0x2e, 0x06, 0x6e, 0x62, 0x4b, 0xad, 0xb7, 0x6a, 0x18, 0x4c, |
HannesTschofenig | 0:796d0f61a05b | 553 | 0x8f, 0x90, 0xfb, 0xa0, 0x53, 0x33, 0x0e, 0x65, 0x0e, 0x92 }, |
HannesTschofenig | 0:796d0f61a05b | 554 | { 0x07, 0xe9, 0x42, 0xaa, 0x4e, 0x3c, 0xd7, 0xc0, 0x4d, 0xed, |
HannesTschofenig | 0:796d0f61a05b | 555 | 0xc1, 0xd4, 0x6e, 0x2e, 0x8c, 0xc4, 0xc7, 0x41, 0xb3, 0xd9 }, |
HannesTschofenig | 0:796d0f61a05b | 556 | { 0xb6, 0x58, 0x23, 0x18, 0xdd, 0xcf, 0xb6, 0x7a, 0x53, 0xa6, |
HannesTschofenig | 0:796d0f61a05b | 557 | 0x7d, 0x67, 0x6b, 0x8a, 0xd8, 0x69, 0xad, 0xed, 0x62, 0x9a }, |
HannesTschofenig | 0:796d0f61a05b | 558 | { 0xf1, 0xbe, 0x3e, 0xe8, 0x77, 0x70, 0x31, 0x40, 0xd3, 0x4f, |
HannesTschofenig | 0:796d0f61a05b | 559 | 0x97, 0xea, 0x1a, 0xb3, 0xa0, 0x7c, 0x14, 0x13, 0x33, 0xe2 }, |
HannesTschofenig | 0:796d0f61a05b | 560 | { 0x85, 0xf1, 0x64, 0x70, 0x3e, 0x61, 0xa6, 0x31, 0x31, 0xbe, |
HannesTschofenig | 0:796d0f61a05b | 561 | 0x7e, 0x45, 0x95, 0x8e, 0x07, 0x94, 0x12, 0x39, 0x04, 0xf9 }, |
HannesTschofenig | 0:796d0f61a05b | 562 | }, |
HannesTschofenig | 0:796d0f61a05b | 563 | }; |
HannesTschofenig | 0:796d0f61a05b | 564 | |
HannesTschofenig | 0:796d0f61a05b | 565 | static const unsigned char ripemd160_test_key[KEYS][20] = |
HannesTschofenig | 0:796d0f61a05b | 566 | { |
HannesTschofenig | 0:796d0f61a05b | 567 | { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, |
HannesTschofenig | 0:796d0f61a05b | 568 | 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x01, 0x23, 0x45, 0x67 }, |
HannesTschofenig | 0:796d0f61a05b | 569 | { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, |
HannesTschofenig | 0:796d0f61a05b | 570 | 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x00, 0x11, 0x22, 0x33 }, |
HannesTschofenig | 0:796d0f61a05b | 571 | }; |
HannesTschofenig | 0:796d0f61a05b | 572 | |
HannesTschofenig | 0:796d0f61a05b | 573 | /* |
HannesTschofenig | 0:796d0f61a05b | 574 | * Checkup routine |
HannesTschofenig | 0:796d0f61a05b | 575 | */ |
HannesTschofenig | 0:796d0f61a05b | 576 | int ripemd160_self_test( int verbose ) |
HannesTschofenig | 0:796d0f61a05b | 577 | { |
HannesTschofenig | 0:796d0f61a05b | 578 | int i, j; |
HannesTschofenig | 0:796d0f61a05b | 579 | unsigned char output[20]; |
HannesTschofenig | 0:796d0f61a05b | 580 | |
HannesTschofenig | 0:796d0f61a05b | 581 | memset( output, 0, sizeof output ); |
HannesTschofenig | 0:796d0f61a05b | 582 | |
HannesTschofenig | 0:796d0f61a05b | 583 | for( i = 0; i < TESTS; i++ ) |
HannesTschofenig | 0:796d0f61a05b | 584 | { |
HannesTschofenig | 0:796d0f61a05b | 585 | if( verbose != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 586 | polarssl_printf( " RIPEMD-160 test #%d: ", i + 1 ); |
HannesTschofenig | 0:796d0f61a05b | 587 | |
HannesTschofenig | 0:796d0f61a05b | 588 | ripemd160( (const unsigned char *) ripemd160_test_input[i], |
HannesTschofenig | 0:796d0f61a05b | 589 | strlen( ripemd160_test_input[i] ), |
HannesTschofenig | 0:796d0f61a05b | 590 | output ); |
HannesTschofenig | 0:796d0f61a05b | 591 | |
HannesTschofenig | 0:796d0f61a05b | 592 | if( memcmp( output, ripemd160_test_md[i], 20 ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 593 | { |
HannesTschofenig | 0:796d0f61a05b | 594 | if( verbose != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 595 | polarssl_printf( "failed\n" ); |
HannesTschofenig | 0:796d0f61a05b | 596 | |
HannesTschofenig | 0:796d0f61a05b | 597 | return( 1 ); |
HannesTschofenig | 0:796d0f61a05b | 598 | } |
HannesTschofenig | 0:796d0f61a05b | 599 | |
HannesTschofenig | 0:796d0f61a05b | 600 | if( verbose != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 601 | polarssl_printf( "passed\n" ); |
HannesTschofenig | 0:796d0f61a05b | 602 | |
HannesTschofenig | 0:796d0f61a05b | 603 | for( j = 0; j < KEYS; j++ ) |
HannesTschofenig | 0:796d0f61a05b | 604 | { |
HannesTschofenig | 0:796d0f61a05b | 605 | if( verbose != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 606 | polarssl_printf( " HMAC-RIPEMD-160 test #%d, key #%d: ", |
HannesTschofenig | 0:796d0f61a05b | 607 | i + 1, j + 1 ); |
HannesTschofenig | 0:796d0f61a05b | 608 | |
HannesTschofenig | 0:796d0f61a05b | 609 | ripemd160_hmac( ripemd160_test_key[j], 20, |
HannesTschofenig | 0:796d0f61a05b | 610 | (const unsigned char *) ripemd160_test_input[i], |
HannesTschofenig | 0:796d0f61a05b | 611 | strlen( ripemd160_test_input[i] ), |
HannesTschofenig | 0:796d0f61a05b | 612 | output ); |
HannesTschofenig | 0:796d0f61a05b | 613 | |
HannesTschofenig | 0:796d0f61a05b | 614 | if( memcmp( output, ripemd160_test_hmac[j][i], 20 ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 615 | { |
HannesTschofenig | 0:796d0f61a05b | 616 | if( verbose != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 617 | polarssl_printf( "failed\n" ); |
HannesTschofenig | 0:796d0f61a05b | 618 | |
HannesTschofenig | 0:796d0f61a05b | 619 | return( 1 ); |
HannesTschofenig | 0:796d0f61a05b | 620 | } |
HannesTschofenig | 0:796d0f61a05b | 621 | |
HannesTschofenig | 0:796d0f61a05b | 622 | if( verbose != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 623 | polarssl_printf( "passed\n" ); |
HannesTschofenig | 0:796d0f61a05b | 624 | } |
HannesTschofenig | 0:796d0f61a05b | 625 | |
HannesTschofenig | 0:796d0f61a05b | 626 | if( verbose != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 627 | polarssl_printf( "\n" ); |
HannesTschofenig | 0:796d0f61a05b | 628 | } |
HannesTschofenig | 0:796d0f61a05b | 629 | |
HannesTschofenig | 0:796d0f61a05b | 630 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 631 | } |
HannesTschofenig | 0:796d0f61a05b | 632 | |
HannesTschofenig | 0:796d0f61a05b | 633 | #endif /* POLARSSL_SELF_TEST */ |
HannesTschofenig | 0:796d0f61a05b | 634 | |
HannesTschofenig | 0:796d0f61a05b | 635 | #endif /* POLARSSL_RIPEMD160_C */ |
HannesTschofenig | 0:796d0f61a05b | 636 | |
HannesTschofenig | 0:796d0f61a05b | 637 |