mbed TLS library
Dependents: HTTPClient-SSL WS_SERVER
polarssl/md2.c@0:137634ff4186, 2015-06-11 (annotated)
- Committer:
- ansond
- Date:
- Thu Jun 11 03:27:03 2015 +0000
- Revision:
- 0:137634ff4186
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:137634ff4186 | 1 | /* |
ansond | 0:137634ff4186 | 2 | * RFC 1115/1319 compliant MD2 implementation |
ansond | 0:137634ff4186 | 3 | * |
ansond | 0:137634ff4186 | 4 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
ansond | 0:137634ff4186 | 5 | * |
ansond | 0:137634ff4186 | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
ansond | 0:137634ff4186 | 7 | * |
ansond | 0:137634ff4186 | 8 | * This program is free software; you can redistribute it and/or modify |
ansond | 0:137634ff4186 | 9 | * it under the terms of the GNU General Public License as published by |
ansond | 0:137634ff4186 | 10 | * the Free Software Foundation; either version 2 of the License, or |
ansond | 0:137634ff4186 | 11 | * (at your option) any later version. |
ansond | 0:137634ff4186 | 12 | * |
ansond | 0:137634ff4186 | 13 | * This program is distributed in the hope that it will be useful, |
ansond | 0:137634ff4186 | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ansond | 0:137634ff4186 | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ansond | 0:137634ff4186 | 16 | * GNU General Public License for more details. |
ansond | 0:137634ff4186 | 17 | * |
ansond | 0:137634ff4186 | 18 | * You should have received a copy of the GNU General Public License along |
ansond | 0:137634ff4186 | 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
ansond | 0:137634ff4186 | 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
ansond | 0:137634ff4186 | 21 | */ |
ansond | 0:137634ff4186 | 22 | /* |
ansond | 0:137634ff4186 | 23 | * The MD2 algorithm was designed by Ron Rivest in 1989. |
ansond | 0:137634ff4186 | 24 | * |
ansond | 0:137634ff4186 | 25 | * http://www.ietf.org/rfc/rfc1115.txt |
ansond | 0:137634ff4186 | 26 | * http://www.ietf.org/rfc/rfc1319.txt |
ansond | 0:137634ff4186 | 27 | */ |
ansond | 0:137634ff4186 | 28 | |
ansond | 0:137634ff4186 | 29 | #if !defined(POLARSSL_CONFIG_FILE) |
ansond | 0:137634ff4186 | 30 | #include "polarssl/config.h" |
ansond | 0:137634ff4186 | 31 | #else |
ansond | 0:137634ff4186 | 32 | #include POLARSSL_CONFIG_FILE |
ansond | 0:137634ff4186 | 33 | #endif |
ansond | 0:137634ff4186 | 34 | |
ansond | 0:137634ff4186 | 35 | #if defined(POLARSSL_MD2_C) |
ansond | 0:137634ff4186 | 36 | |
ansond | 0:137634ff4186 | 37 | #include "polarssl/md2.h" |
ansond | 0:137634ff4186 | 38 | |
ansond | 0:137634ff4186 | 39 | #include <string.h> |
ansond | 0:137634ff4186 | 40 | |
ansond | 0:137634ff4186 | 41 | #if defined(POLARSSL_FS_IO) |
ansond | 0:137634ff4186 | 42 | #include <stdio.h> |
ansond | 0:137634ff4186 | 43 | #endif |
ansond | 0:137634ff4186 | 44 | |
ansond | 0:137634ff4186 | 45 | #if defined(POLARSSL_SELF_TEST) |
ansond | 0:137634ff4186 | 46 | #if defined(POLARSSL_PLATFORM_C) |
ansond | 0:137634ff4186 | 47 | #include "polarssl/platform.h" |
ansond | 0:137634ff4186 | 48 | #else |
ansond | 0:137634ff4186 | 49 | #include <stdio.h> |
ansond | 0:137634ff4186 | 50 | #define polarssl_printf printf |
ansond | 0:137634ff4186 | 51 | #endif /* POLARSSL_PLATFORM_C */ |
ansond | 0:137634ff4186 | 52 | #endif /* POLARSSL_SELF_TEST */ |
ansond | 0:137634ff4186 | 53 | |
ansond | 0:137634ff4186 | 54 | /* Implementation that should never be optimized out by the compiler */ |
ansond | 0:137634ff4186 | 55 | static void polarssl_zeroize( void *v, size_t n ) { |
ansond | 0:137634ff4186 | 56 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
ansond | 0:137634ff4186 | 57 | } |
ansond | 0:137634ff4186 | 58 | |
ansond | 0:137634ff4186 | 59 | #if !defined(POLARSSL_MD2_ALT) |
ansond | 0:137634ff4186 | 60 | |
ansond | 0:137634ff4186 | 61 | static const unsigned char PI_SUBST[256] = |
ansond | 0:137634ff4186 | 62 | { |
ansond | 0:137634ff4186 | 63 | 0x29, 0x2E, 0x43, 0xC9, 0xA2, 0xD8, 0x7C, 0x01, 0x3D, 0x36, |
ansond | 0:137634ff4186 | 64 | 0x54, 0xA1, 0xEC, 0xF0, 0x06, 0x13, 0x62, 0xA7, 0x05, 0xF3, |
ansond | 0:137634ff4186 | 65 | 0xC0, 0xC7, 0x73, 0x8C, 0x98, 0x93, 0x2B, 0xD9, 0xBC, 0x4C, |
ansond | 0:137634ff4186 | 66 | 0x82, 0xCA, 0x1E, 0x9B, 0x57, 0x3C, 0xFD, 0xD4, 0xE0, 0x16, |
ansond | 0:137634ff4186 | 67 | 0x67, 0x42, 0x6F, 0x18, 0x8A, 0x17, 0xE5, 0x12, 0xBE, 0x4E, |
ansond | 0:137634ff4186 | 68 | 0xC4, 0xD6, 0xDA, 0x9E, 0xDE, 0x49, 0xA0, 0xFB, 0xF5, 0x8E, |
ansond | 0:137634ff4186 | 69 | 0xBB, 0x2F, 0xEE, 0x7A, 0xA9, 0x68, 0x79, 0x91, 0x15, 0xB2, |
ansond | 0:137634ff4186 | 70 | 0x07, 0x3F, 0x94, 0xC2, 0x10, 0x89, 0x0B, 0x22, 0x5F, 0x21, |
ansond | 0:137634ff4186 | 71 | 0x80, 0x7F, 0x5D, 0x9A, 0x5A, 0x90, 0x32, 0x27, 0x35, 0x3E, |
ansond | 0:137634ff4186 | 72 | 0xCC, 0xE7, 0xBF, 0xF7, 0x97, 0x03, 0xFF, 0x19, 0x30, 0xB3, |
ansond | 0:137634ff4186 | 73 | 0x48, 0xA5, 0xB5, 0xD1, 0xD7, 0x5E, 0x92, 0x2A, 0xAC, 0x56, |
ansond | 0:137634ff4186 | 74 | 0xAA, 0xC6, 0x4F, 0xB8, 0x38, 0xD2, 0x96, 0xA4, 0x7D, 0xB6, |
ansond | 0:137634ff4186 | 75 | 0x76, 0xFC, 0x6B, 0xE2, 0x9C, 0x74, 0x04, 0xF1, 0x45, 0x9D, |
ansond | 0:137634ff4186 | 76 | 0x70, 0x59, 0x64, 0x71, 0x87, 0x20, 0x86, 0x5B, 0xCF, 0x65, |
ansond | 0:137634ff4186 | 77 | 0xE6, 0x2D, 0xA8, 0x02, 0x1B, 0x60, 0x25, 0xAD, 0xAE, 0xB0, |
ansond | 0:137634ff4186 | 78 | 0xB9, 0xF6, 0x1C, 0x46, 0x61, 0x69, 0x34, 0x40, 0x7E, 0x0F, |
ansond | 0:137634ff4186 | 79 | 0x55, 0x47, 0xA3, 0x23, 0xDD, 0x51, 0xAF, 0x3A, 0xC3, 0x5C, |
ansond | 0:137634ff4186 | 80 | 0xF9, 0xCE, 0xBA, 0xC5, 0xEA, 0x26, 0x2C, 0x53, 0x0D, 0x6E, |
ansond | 0:137634ff4186 | 81 | 0x85, 0x28, 0x84, 0x09, 0xD3, 0xDF, 0xCD, 0xF4, 0x41, 0x81, |
ansond | 0:137634ff4186 | 82 | 0x4D, 0x52, 0x6A, 0xDC, 0x37, 0xC8, 0x6C, 0xC1, 0xAB, 0xFA, |
ansond | 0:137634ff4186 | 83 | 0x24, 0xE1, 0x7B, 0x08, 0x0C, 0xBD, 0xB1, 0x4A, 0x78, 0x88, |
ansond | 0:137634ff4186 | 84 | 0x95, 0x8B, 0xE3, 0x63, 0xE8, 0x6D, 0xE9, 0xCB, 0xD5, 0xFE, |
ansond | 0:137634ff4186 | 85 | 0x3B, 0x00, 0x1D, 0x39, 0xF2, 0xEF, 0xB7, 0x0E, 0x66, 0x58, |
ansond | 0:137634ff4186 | 86 | 0xD0, 0xE4, 0xA6, 0x77, 0x72, 0xF8, 0xEB, 0x75, 0x4B, 0x0A, |
ansond | 0:137634ff4186 | 87 | 0x31, 0x44, 0x50, 0xB4, 0x8F, 0xED, 0x1F, 0x1A, 0xDB, 0x99, |
ansond | 0:137634ff4186 | 88 | 0x8D, 0x33, 0x9F, 0x11, 0x83, 0x14 |
ansond | 0:137634ff4186 | 89 | }; |
ansond | 0:137634ff4186 | 90 | |
ansond | 0:137634ff4186 | 91 | void md2_init( md2_context *ctx ) |
ansond | 0:137634ff4186 | 92 | { |
ansond | 0:137634ff4186 | 93 | memset( ctx, 0, sizeof( md2_context ) ); |
ansond | 0:137634ff4186 | 94 | } |
ansond | 0:137634ff4186 | 95 | |
ansond | 0:137634ff4186 | 96 | void md2_free( md2_context *ctx ) |
ansond | 0:137634ff4186 | 97 | { |
ansond | 0:137634ff4186 | 98 | if( ctx == NULL ) |
ansond | 0:137634ff4186 | 99 | return; |
ansond | 0:137634ff4186 | 100 | |
ansond | 0:137634ff4186 | 101 | polarssl_zeroize( ctx, sizeof( md2_context ) ); |
ansond | 0:137634ff4186 | 102 | } |
ansond | 0:137634ff4186 | 103 | |
ansond | 0:137634ff4186 | 104 | /* |
ansond | 0:137634ff4186 | 105 | * MD2 context setup |
ansond | 0:137634ff4186 | 106 | */ |
ansond | 0:137634ff4186 | 107 | void md2_starts( md2_context *ctx ) |
ansond | 0:137634ff4186 | 108 | { |
ansond | 0:137634ff4186 | 109 | memset( ctx->cksum, 0, 16 ); |
ansond | 0:137634ff4186 | 110 | memset( ctx->state, 0, 46 ); |
ansond | 0:137634ff4186 | 111 | memset( ctx->buffer, 0, 16 ); |
ansond | 0:137634ff4186 | 112 | ctx->left = 0; |
ansond | 0:137634ff4186 | 113 | } |
ansond | 0:137634ff4186 | 114 | |
ansond | 0:137634ff4186 | 115 | void md2_process( md2_context *ctx ) |
ansond | 0:137634ff4186 | 116 | { |
ansond | 0:137634ff4186 | 117 | int i, j; |
ansond | 0:137634ff4186 | 118 | unsigned char t = 0; |
ansond | 0:137634ff4186 | 119 | |
ansond | 0:137634ff4186 | 120 | for( i = 0; i < 16; i++ ) |
ansond | 0:137634ff4186 | 121 | { |
ansond | 0:137634ff4186 | 122 | ctx->state[i + 16] = ctx->buffer[i]; |
ansond | 0:137634ff4186 | 123 | ctx->state[i + 32] = |
ansond | 0:137634ff4186 | 124 | (unsigned char)( ctx->buffer[i] ^ ctx->state[i]); |
ansond | 0:137634ff4186 | 125 | } |
ansond | 0:137634ff4186 | 126 | |
ansond | 0:137634ff4186 | 127 | for( i = 0; i < 18; i++ ) |
ansond | 0:137634ff4186 | 128 | { |
ansond | 0:137634ff4186 | 129 | for( j = 0; j < 48; j++ ) |
ansond | 0:137634ff4186 | 130 | { |
ansond | 0:137634ff4186 | 131 | ctx->state[j] = (unsigned char) |
ansond | 0:137634ff4186 | 132 | ( ctx->state[j] ^ PI_SUBST[t] ); |
ansond | 0:137634ff4186 | 133 | t = ctx->state[j]; |
ansond | 0:137634ff4186 | 134 | } |
ansond | 0:137634ff4186 | 135 | |
ansond | 0:137634ff4186 | 136 | t = (unsigned char)( t + i ); |
ansond | 0:137634ff4186 | 137 | } |
ansond | 0:137634ff4186 | 138 | |
ansond | 0:137634ff4186 | 139 | t = ctx->cksum[15]; |
ansond | 0:137634ff4186 | 140 | |
ansond | 0:137634ff4186 | 141 | for( i = 0; i < 16; i++ ) |
ansond | 0:137634ff4186 | 142 | { |
ansond | 0:137634ff4186 | 143 | ctx->cksum[i] = (unsigned char) |
ansond | 0:137634ff4186 | 144 | ( ctx->cksum[i] ^ PI_SUBST[ctx->buffer[i] ^ t] ); |
ansond | 0:137634ff4186 | 145 | t = ctx->cksum[i]; |
ansond | 0:137634ff4186 | 146 | } |
ansond | 0:137634ff4186 | 147 | } |
ansond | 0:137634ff4186 | 148 | |
ansond | 0:137634ff4186 | 149 | /* |
ansond | 0:137634ff4186 | 150 | * MD2 process buffer |
ansond | 0:137634ff4186 | 151 | */ |
ansond | 0:137634ff4186 | 152 | void md2_update( md2_context *ctx, const unsigned char *input, size_t ilen ) |
ansond | 0:137634ff4186 | 153 | { |
ansond | 0:137634ff4186 | 154 | size_t fill; |
ansond | 0:137634ff4186 | 155 | |
ansond | 0:137634ff4186 | 156 | while( ilen > 0 ) |
ansond | 0:137634ff4186 | 157 | { |
ansond | 0:137634ff4186 | 158 | if( ctx->left + ilen > 16 ) |
ansond | 0:137634ff4186 | 159 | fill = 16 - ctx->left; |
ansond | 0:137634ff4186 | 160 | else |
ansond | 0:137634ff4186 | 161 | fill = ilen; |
ansond | 0:137634ff4186 | 162 | |
ansond | 0:137634ff4186 | 163 | memcpy( ctx->buffer + ctx->left, input, fill ); |
ansond | 0:137634ff4186 | 164 | |
ansond | 0:137634ff4186 | 165 | ctx->left += fill; |
ansond | 0:137634ff4186 | 166 | input += fill; |
ansond | 0:137634ff4186 | 167 | ilen -= fill; |
ansond | 0:137634ff4186 | 168 | |
ansond | 0:137634ff4186 | 169 | if( ctx->left == 16 ) |
ansond | 0:137634ff4186 | 170 | { |
ansond | 0:137634ff4186 | 171 | ctx->left = 0; |
ansond | 0:137634ff4186 | 172 | md2_process( ctx ); |
ansond | 0:137634ff4186 | 173 | } |
ansond | 0:137634ff4186 | 174 | } |
ansond | 0:137634ff4186 | 175 | } |
ansond | 0:137634ff4186 | 176 | |
ansond | 0:137634ff4186 | 177 | /* |
ansond | 0:137634ff4186 | 178 | * MD2 final digest |
ansond | 0:137634ff4186 | 179 | */ |
ansond | 0:137634ff4186 | 180 | void md2_finish( md2_context *ctx, unsigned char output[16] ) |
ansond | 0:137634ff4186 | 181 | { |
ansond | 0:137634ff4186 | 182 | size_t i; |
ansond | 0:137634ff4186 | 183 | unsigned char x; |
ansond | 0:137634ff4186 | 184 | |
ansond | 0:137634ff4186 | 185 | x = (unsigned char)( 16 - ctx->left ); |
ansond | 0:137634ff4186 | 186 | |
ansond | 0:137634ff4186 | 187 | for( i = ctx->left; i < 16; i++ ) |
ansond | 0:137634ff4186 | 188 | ctx->buffer[i] = x; |
ansond | 0:137634ff4186 | 189 | |
ansond | 0:137634ff4186 | 190 | md2_process( ctx ); |
ansond | 0:137634ff4186 | 191 | |
ansond | 0:137634ff4186 | 192 | memcpy( ctx->buffer, ctx->cksum, 16 ); |
ansond | 0:137634ff4186 | 193 | md2_process( ctx ); |
ansond | 0:137634ff4186 | 194 | |
ansond | 0:137634ff4186 | 195 | memcpy( output, ctx->state, 16 ); |
ansond | 0:137634ff4186 | 196 | } |
ansond | 0:137634ff4186 | 197 | |
ansond | 0:137634ff4186 | 198 | #endif /* !POLARSSL_MD2_ALT */ |
ansond | 0:137634ff4186 | 199 | |
ansond | 0:137634ff4186 | 200 | /* |
ansond | 0:137634ff4186 | 201 | * output = MD2( input buffer ) |
ansond | 0:137634ff4186 | 202 | */ |
ansond | 0:137634ff4186 | 203 | void md2( const unsigned char *input, size_t ilen, unsigned char output[16] ) |
ansond | 0:137634ff4186 | 204 | { |
ansond | 0:137634ff4186 | 205 | md2_context ctx; |
ansond | 0:137634ff4186 | 206 | |
ansond | 0:137634ff4186 | 207 | md2_init( &ctx ); |
ansond | 0:137634ff4186 | 208 | md2_starts( &ctx ); |
ansond | 0:137634ff4186 | 209 | md2_update( &ctx, input, ilen ); |
ansond | 0:137634ff4186 | 210 | md2_finish( &ctx, output ); |
ansond | 0:137634ff4186 | 211 | md2_free( &ctx ); |
ansond | 0:137634ff4186 | 212 | } |
ansond | 0:137634ff4186 | 213 | |
ansond | 0:137634ff4186 | 214 | #if defined(POLARSSL_FS_IO) |
ansond | 0:137634ff4186 | 215 | /* |
ansond | 0:137634ff4186 | 216 | * output = MD2( file contents ) |
ansond | 0:137634ff4186 | 217 | */ |
ansond | 0:137634ff4186 | 218 | int md2_file( const char *path, unsigned char output[16] ) |
ansond | 0:137634ff4186 | 219 | { |
ansond | 0:137634ff4186 | 220 | FILE *f; |
ansond | 0:137634ff4186 | 221 | size_t n; |
ansond | 0:137634ff4186 | 222 | md2_context ctx; |
ansond | 0:137634ff4186 | 223 | unsigned char buf[1024]; |
ansond | 0:137634ff4186 | 224 | |
ansond | 0:137634ff4186 | 225 | if( ( f = fopen( path, "rb" ) ) == NULL ) |
ansond | 0:137634ff4186 | 226 | return( POLARSSL_ERR_MD2_FILE_IO_ERROR ); |
ansond | 0:137634ff4186 | 227 | |
ansond | 0:137634ff4186 | 228 | md2_init( &ctx ); |
ansond | 0:137634ff4186 | 229 | md2_starts( &ctx ); |
ansond | 0:137634ff4186 | 230 | |
ansond | 0:137634ff4186 | 231 | while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) |
ansond | 0:137634ff4186 | 232 | md2_update( &ctx, buf, n ); |
ansond | 0:137634ff4186 | 233 | |
ansond | 0:137634ff4186 | 234 | md2_finish( &ctx, output ); |
ansond | 0:137634ff4186 | 235 | md2_free( &ctx ); |
ansond | 0:137634ff4186 | 236 | |
ansond | 0:137634ff4186 | 237 | if( ferror( f ) != 0 ) |
ansond | 0:137634ff4186 | 238 | { |
ansond | 0:137634ff4186 | 239 | fclose( f ); |
ansond | 0:137634ff4186 | 240 | return( POLARSSL_ERR_MD2_FILE_IO_ERROR ); |
ansond | 0:137634ff4186 | 241 | } |
ansond | 0:137634ff4186 | 242 | |
ansond | 0:137634ff4186 | 243 | fclose( f ); |
ansond | 0:137634ff4186 | 244 | return( 0 ); |
ansond | 0:137634ff4186 | 245 | } |
ansond | 0:137634ff4186 | 246 | #endif /* POLARSSL_FS_IO */ |
ansond | 0:137634ff4186 | 247 | |
ansond | 0:137634ff4186 | 248 | /* |
ansond | 0:137634ff4186 | 249 | * MD2 HMAC context setup |
ansond | 0:137634ff4186 | 250 | */ |
ansond | 0:137634ff4186 | 251 | void md2_hmac_starts( md2_context *ctx, const unsigned char *key, |
ansond | 0:137634ff4186 | 252 | size_t keylen ) |
ansond | 0:137634ff4186 | 253 | { |
ansond | 0:137634ff4186 | 254 | size_t i; |
ansond | 0:137634ff4186 | 255 | unsigned char sum[16]; |
ansond | 0:137634ff4186 | 256 | |
ansond | 0:137634ff4186 | 257 | if( keylen > 16 ) |
ansond | 0:137634ff4186 | 258 | { |
ansond | 0:137634ff4186 | 259 | md2( key, keylen, sum ); |
ansond | 0:137634ff4186 | 260 | keylen = 16; |
ansond | 0:137634ff4186 | 261 | key = sum; |
ansond | 0:137634ff4186 | 262 | } |
ansond | 0:137634ff4186 | 263 | |
ansond | 0:137634ff4186 | 264 | memset( ctx->ipad, 0x36, 16 ); |
ansond | 0:137634ff4186 | 265 | memset( ctx->opad, 0x5C, 16 ); |
ansond | 0:137634ff4186 | 266 | |
ansond | 0:137634ff4186 | 267 | for( i = 0; i < keylen; i++ ) |
ansond | 0:137634ff4186 | 268 | { |
ansond | 0:137634ff4186 | 269 | ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] ); |
ansond | 0:137634ff4186 | 270 | ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] ); |
ansond | 0:137634ff4186 | 271 | } |
ansond | 0:137634ff4186 | 272 | |
ansond | 0:137634ff4186 | 273 | md2_starts( ctx ); |
ansond | 0:137634ff4186 | 274 | md2_update( ctx, ctx->ipad, 16 ); |
ansond | 0:137634ff4186 | 275 | |
ansond | 0:137634ff4186 | 276 | polarssl_zeroize( sum, sizeof( sum ) ); |
ansond | 0:137634ff4186 | 277 | } |
ansond | 0:137634ff4186 | 278 | |
ansond | 0:137634ff4186 | 279 | /* |
ansond | 0:137634ff4186 | 280 | * MD2 HMAC process buffer |
ansond | 0:137634ff4186 | 281 | */ |
ansond | 0:137634ff4186 | 282 | void md2_hmac_update( md2_context *ctx, const unsigned char *input, |
ansond | 0:137634ff4186 | 283 | size_t ilen ) |
ansond | 0:137634ff4186 | 284 | { |
ansond | 0:137634ff4186 | 285 | md2_update( ctx, input, ilen ); |
ansond | 0:137634ff4186 | 286 | } |
ansond | 0:137634ff4186 | 287 | |
ansond | 0:137634ff4186 | 288 | /* |
ansond | 0:137634ff4186 | 289 | * MD2 HMAC final digest |
ansond | 0:137634ff4186 | 290 | */ |
ansond | 0:137634ff4186 | 291 | void md2_hmac_finish( md2_context *ctx, unsigned char output[16] ) |
ansond | 0:137634ff4186 | 292 | { |
ansond | 0:137634ff4186 | 293 | unsigned char tmpbuf[16]; |
ansond | 0:137634ff4186 | 294 | |
ansond | 0:137634ff4186 | 295 | md2_finish( ctx, tmpbuf ); |
ansond | 0:137634ff4186 | 296 | md2_starts( ctx ); |
ansond | 0:137634ff4186 | 297 | md2_update( ctx, ctx->opad, 16 ); |
ansond | 0:137634ff4186 | 298 | md2_update( ctx, tmpbuf, 16 ); |
ansond | 0:137634ff4186 | 299 | md2_finish( ctx, output ); |
ansond | 0:137634ff4186 | 300 | |
ansond | 0:137634ff4186 | 301 | polarssl_zeroize( tmpbuf, sizeof( tmpbuf ) ); |
ansond | 0:137634ff4186 | 302 | } |
ansond | 0:137634ff4186 | 303 | |
ansond | 0:137634ff4186 | 304 | /* |
ansond | 0:137634ff4186 | 305 | * MD2 HMAC context reset |
ansond | 0:137634ff4186 | 306 | */ |
ansond | 0:137634ff4186 | 307 | void md2_hmac_reset( md2_context *ctx ) |
ansond | 0:137634ff4186 | 308 | { |
ansond | 0:137634ff4186 | 309 | md2_starts( ctx ); |
ansond | 0:137634ff4186 | 310 | md2_update( ctx, ctx->ipad, 16 ); |
ansond | 0:137634ff4186 | 311 | } |
ansond | 0:137634ff4186 | 312 | |
ansond | 0:137634ff4186 | 313 | /* |
ansond | 0:137634ff4186 | 314 | * output = HMAC-MD2( hmac key, input buffer ) |
ansond | 0:137634ff4186 | 315 | */ |
ansond | 0:137634ff4186 | 316 | void md2_hmac( const unsigned char *key, size_t keylen, |
ansond | 0:137634ff4186 | 317 | const unsigned char *input, size_t ilen, |
ansond | 0:137634ff4186 | 318 | unsigned char output[16] ) |
ansond | 0:137634ff4186 | 319 | { |
ansond | 0:137634ff4186 | 320 | md2_context ctx; |
ansond | 0:137634ff4186 | 321 | |
ansond | 0:137634ff4186 | 322 | md2_init( &ctx ); |
ansond | 0:137634ff4186 | 323 | md2_hmac_starts( &ctx, key, keylen ); |
ansond | 0:137634ff4186 | 324 | md2_hmac_update( &ctx, input, ilen ); |
ansond | 0:137634ff4186 | 325 | md2_hmac_finish( &ctx, output ); |
ansond | 0:137634ff4186 | 326 | md2_free( &ctx ); |
ansond | 0:137634ff4186 | 327 | } |
ansond | 0:137634ff4186 | 328 | |
ansond | 0:137634ff4186 | 329 | #if defined(POLARSSL_SELF_TEST) |
ansond | 0:137634ff4186 | 330 | |
ansond | 0:137634ff4186 | 331 | /* |
ansond | 0:137634ff4186 | 332 | * RFC 1319 test vectors |
ansond | 0:137634ff4186 | 333 | */ |
ansond | 0:137634ff4186 | 334 | static const char md2_test_str[7][81] = |
ansond | 0:137634ff4186 | 335 | { |
ansond | 0:137634ff4186 | 336 | { "" }, |
ansond | 0:137634ff4186 | 337 | { "a" }, |
ansond | 0:137634ff4186 | 338 | { "abc" }, |
ansond | 0:137634ff4186 | 339 | { "message digest" }, |
ansond | 0:137634ff4186 | 340 | { "abcdefghijklmnopqrstuvwxyz" }, |
ansond | 0:137634ff4186 | 341 | { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" }, |
ansond | 0:137634ff4186 | 342 | { "12345678901234567890123456789012345678901234567890123456789012" \ |
ansond | 0:137634ff4186 | 343 | "345678901234567890" } |
ansond | 0:137634ff4186 | 344 | }; |
ansond | 0:137634ff4186 | 345 | |
ansond | 0:137634ff4186 | 346 | static const unsigned char md2_test_sum[7][16] = |
ansond | 0:137634ff4186 | 347 | { |
ansond | 0:137634ff4186 | 348 | { 0x83, 0x50, 0xE5, 0xA3, 0xE2, 0x4C, 0x15, 0x3D, |
ansond | 0:137634ff4186 | 349 | 0xF2, 0x27, 0x5C, 0x9F, 0x80, 0x69, 0x27, 0x73 }, |
ansond | 0:137634ff4186 | 350 | { 0x32, 0xEC, 0x01, 0xEC, 0x4A, 0x6D, 0xAC, 0x72, |
ansond | 0:137634ff4186 | 351 | 0xC0, 0xAB, 0x96, 0xFB, 0x34, 0xC0, 0xB5, 0xD1 }, |
ansond | 0:137634ff4186 | 352 | { 0xDA, 0x85, 0x3B, 0x0D, 0x3F, 0x88, 0xD9, 0x9B, |
ansond | 0:137634ff4186 | 353 | 0x30, 0x28, 0x3A, 0x69, 0xE6, 0xDE, 0xD6, 0xBB }, |
ansond | 0:137634ff4186 | 354 | { 0xAB, 0x4F, 0x49, 0x6B, 0xFB, 0x2A, 0x53, 0x0B, |
ansond | 0:137634ff4186 | 355 | 0x21, 0x9F, 0xF3, 0x30, 0x31, 0xFE, 0x06, 0xB0 }, |
ansond | 0:137634ff4186 | 356 | { 0x4E, 0x8D, 0xDF, 0xF3, 0x65, 0x02, 0x92, 0xAB, |
ansond | 0:137634ff4186 | 357 | 0x5A, 0x41, 0x08, 0xC3, 0xAA, 0x47, 0x94, 0x0B }, |
ansond | 0:137634ff4186 | 358 | { 0xDA, 0x33, 0xDE, 0xF2, 0xA4, 0x2D, 0xF1, 0x39, |
ansond | 0:137634ff4186 | 359 | 0x75, 0x35, 0x28, 0x46, 0xC3, 0x03, 0x38, 0xCD }, |
ansond | 0:137634ff4186 | 360 | { 0xD5, 0x97, 0x6F, 0x79, 0xD8, 0x3D, 0x3A, 0x0D, |
ansond | 0:137634ff4186 | 361 | 0xC9, 0x80, 0x6C, 0x3C, 0x66, 0xF3, 0xEF, 0xD8 } |
ansond | 0:137634ff4186 | 362 | }; |
ansond | 0:137634ff4186 | 363 | |
ansond | 0:137634ff4186 | 364 | /* |
ansond | 0:137634ff4186 | 365 | * Checkup routine |
ansond | 0:137634ff4186 | 366 | */ |
ansond | 0:137634ff4186 | 367 | int md2_self_test( int verbose ) |
ansond | 0:137634ff4186 | 368 | { |
ansond | 0:137634ff4186 | 369 | int i; |
ansond | 0:137634ff4186 | 370 | unsigned char md2sum[16]; |
ansond | 0:137634ff4186 | 371 | |
ansond | 0:137634ff4186 | 372 | for( i = 0; i < 7; i++ ) |
ansond | 0:137634ff4186 | 373 | { |
ansond | 0:137634ff4186 | 374 | if( verbose != 0 ) |
ansond | 0:137634ff4186 | 375 | polarssl_printf( " MD2 test #%d: ", i + 1 ); |
ansond | 0:137634ff4186 | 376 | |
ansond | 0:137634ff4186 | 377 | md2( (unsigned char *) md2_test_str[i], |
ansond | 0:137634ff4186 | 378 | strlen( md2_test_str[i] ), md2sum ); |
ansond | 0:137634ff4186 | 379 | |
ansond | 0:137634ff4186 | 380 | if( memcmp( md2sum, md2_test_sum[i], 16 ) != 0 ) |
ansond | 0:137634ff4186 | 381 | { |
ansond | 0:137634ff4186 | 382 | if( verbose != 0 ) |
ansond | 0:137634ff4186 | 383 | polarssl_printf( "failed\n" ); |
ansond | 0:137634ff4186 | 384 | |
ansond | 0:137634ff4186 | 385 | return( 1 ); |
ansond | 0:137634ff4186 | 386 | } |
ansond | 0:137634ff4186 | 387 | |
ansond | 0:137634ff4186 | 388 | if( verbose != 0 ) |
ansond | 0:137634ff4186 | 389 | polarssl_printf( "passed\n" ); |
ansond | 0:137634ff4186 | 390 | } |
ansond | 0:137634ff4186 | 391 | |
ansond | 0:137634ff4186 | 392 | if( verbose != 0 ) |
ansond | 0:137634ff4186 | 393 | polarssl_printf( "\n" ); |
ansond | 0:137634ff4186 | 394 | |
ansond | 0:137634ff4186 | 395 | return( 0 ); |
ansond | 0:137634ff4186 | 396 | } |
ansond | 0:137634ff4186 | 397 | |
ansond | 0:137634ff4186 | 398 | #endif /* POLARSSL_SELF_TEST */ |
ansond | 0:137634ff4186 | 399 | |
ansond | 0:137634ff4186 | 400 | #endif /* POLARSSL_MD2_C */ |
ansond | 0:137634ff4186 | 401 |