Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Simon Cooksey 0:fb7af294d5d9 1 /*
Simon Cooksey 0:fb7af294d5d9 2 * RFC 1115/1319 compliant MD2 implementation
Simon Cooksey 0:fb7af294d5d9 3 *
Simon Cooksey 0:fb7af294d5d9 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Simon Cooksey 0:fb7af294d5d9 5 * SPDX-License-Identifier: Apache-2.0
Simon Cooksey 0:fb7af294d5d9 6 *
Simon Cooksey 0:fb7af294d5d9 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Simon Cooksey 0:fb7af294d5d9 8 * not use this file except in compliance with the License.
Simon Cooksey 0:fb7af294d5d9 9 * You may obtain a copy of the License at
Simon Cooksey 0:fb7af294d5d9 10 *
Simon Cooksey 0:fb7af294d5d9 11 * http://www.apache.org/licenses/LICENSE-2.0
Simon Cooksey 0:fb7af294d5d9 12 *
Simon Cooksey 0:fb7af294d5d9 13 * Unless required by applicable law or agreed to in writing, software
Simon Cooksey 0:fb7af294d5d9 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Simon Cooksey 0:fb7af294d5d9 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Simon Cooksey 0:fb7af294d5d9 16 * See the License for the specific language governing permissions and
Simon Cooksey 0:fb7af294d5d9 17 * limitations under the License.
Simon Cooksey 0:fb7af294d5d9 18 *
Simon Cooksey 0:fb7af294d5d9 19 * This file is part of mbed TLS (https://tls.mbed.org)
Simon Cooksey 0:fb7af294d5d9 20 */
Simon Cooksey 0:fb7af294d5d9 21 /*
Simon Cooksey 0:fb7af294d5d9 22 * The MD2 algorithm was designed by Ron Rivest in 1989.
Simon Cooksey 0:fb7af294d5d9 23 *
Simon Cooksey 0:fb7af294d5d9 24 * http://www.ietf.org/rfc/rfc1115.txt
Simon Cooksey 0:fb7af294d5d9 25 * http://www.ietf.org/rfc/rfc1319.txt
Simon Cooksey 0:fb7af294d5d9 26 */
Simon Cooksey 0:fb7af294d5d9 27
Simon Cooksey 0:fb7af294d5d9 28 #if !defined(MBEDTLS_CONFIG_FILE)
Simon Cooksey 0:fb7af294d5d9 29 #include "mbedtls/config.h"
Simon Cooksey 0:fb7af294d5d9 30 #else
Simon Cooksey 0:fb7af294d5d9 31 #include MBEDTLS_CONFIG_FILE
Simon Cooksey 0:fb7af294d5d9 32 #endif
Simon Cooksey 0:fb7af294d5d9 33
Simon Cooksey 0:fb7af294d5d9 34 #if defined(MBEDTLS_MD2_C)
Simon Cooksey 0:fb7af294d5d9 35
Simon Cooksey 0:fb7af294d5d9 36 #include "mbedtls/md2.h"
Simon Cooksey 0:fb7af294d5d9 37
Simon Cooksey 0:fb7af294d5d9 38 #include <string.h>
Simon Cooksey 0:fb7af294d5d9 39
Simon Cooksey 0:fb7af294d5d9 40 #if defined(MBEDTLS_SELF_TEST)
Simon Cooksey 0:fb7af294d5d9 41 #if defined(MBEDTLS_PLATFORM_C)
Simon Cooksey 0:fb7af294d5d9 42 #include "mbedtls/platform.h"
Simon Cooksey 0:fb7af294d5d9 43 #else
Simon Cooksey 0:fb7af294d5d9 44 #include <stdio.h>
Simon Cooksey 0:fb7af294d5d9 45 #define mbedtls_printf printf
Simon Cooksey 0:fb7af294d5d9 46 #endif /* MBEDTLS_PLATFORM_C */
Simon Cooksey 0:fb7af294d5d9 47 #endif /* MBEDTLS_SELF_TEST */
Simon Cooksey 0:fb7af294d5d9 48
Simon Cooksey 0:fb7af294d5d9 49 #if !defined(MBEDTLS_MD2_ALT)
Simon Cooksey 0:fb7af294d5d9 50
Simon Cooksey 0:fb7af294d5d9 51 /* Implementation that should never be optimized out by the compiler */
Simon Cooksey 0:fb7af294d5d9 52 static void mbedtls_zeroize( void *v, size_t n ) {
Simon Cooksey 0:fb7af294d5d9 53 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
Simon Cooksey 0:fb7af294d5d9 54 }
Simon Cooksey 0:fb7af294d5d9 55
Simon Cooksey 0:fb7af294d5d9 56 static const unsigned char PI_SUBST[256] =
Simon Cooksey 0:fb7af294d5d9 57 {
Simon Cooksey 0:fb7af294d5d9 58 0x29, 0x2E, 0x43, 0xC9, 0xA2, 0xD8, 0x7C, 0x01, 0x3D, 0x36,
Simon Cooksey 0:fb7af294d5d9 59 0x54, 0xA1, 0xEC, 0xF0, 0x06, 0x13, 0x62, 0xA7, 0x05, 0xF3,
Simon Cooksey 0:fb7af294d5d9 60 0xC0, 0xC7, 0x73, 0x8C, 0x98, 0x93, 0x2B, 0xD9, 0xBC, 0x4C,
Simon Cooksey 0:fb7af294d5d9 61 0x82, 0xCA, 0x1E, 0x9B, 0x57, 0x3C, 0xFD, 0xD4, 0xE0, 0x16,
Simon Cooksey 0:fb7af294d5d9 62 0x67, 0x42, 0x6F, 0x18, 0x8A, 0x17, 0xE5, 0x12, 0xBE, 0x4E,
Simon Cooksey 0:fb7af294d5d9 63 0xC4, 0xD6, 0xDA, 0x9E, 0xDE, 0x49, 0xA0, 0xFB, 0xF5, 0x8E,
Simon Cooksey 0:fb7af294d5d9 64 0xBB, 0x2F, 0xEE, 0x7A, 0xA9, 0x68, 0x79, 0x91, 0x15, 0xB2,
Simon Cooksey 0:fb7af294d5d9 65 0x07, 0x3F, 0x94, 0xC2, 0x10, 0x89, 0x0B, 0x22, 0x5F, 0x21,
Simon Cooksey 0:fb7af294d5d9 66 0x80, 0x7F, 0x5D, 0x9A, 0x5A, 0x90, 0x32, 0x27, 0x35, 0x3E,
Simon Cooksey 0:fb7af294d5d9 67 0xCC, 0xE7, 0xBF, 0xF7, 0x97, 0x03, 0xFF, 0x19, 0x30, 0xB3,
Simon Cooksey 0:fb7af294d5d9 68 0x48, 0xA5, 0xB5, 0xD1, 0xD7, 0x5E, 0x92, 0x2A, 0xAC, 0x56,
Simon Cooksey 0:fb7af294d5d9 69 0xAA, 0xC6, 0x4F, 0xB8, 0x38, 0xD2, 0x96, 0xA4, 0x7D, 0xB6,
Simon Cooksey 0:fb7af294d5d9 70 0x76, 0xFC, 0x6B, 0xE2, 0x9C, 0x74, 0x04, 0xF1, 0x45, 0x9D,
Simon Cooksey 0:fb7af294d5d9 71 0x70, 0x59, 0x64, 0x71, 0x87, 0x20, 0x86, 0x5B, 0xCF, 0x65,
Simon Cooksey 0:fb7af294d5d9 72 0xE6, 0x2D, 0xA8, 0x02, 0x1B, 0x60, 0x25, 0xAD, 0xAE, 0xB0,
Simon Cooksey 0:fb7af294d5d9 73 0xB9, 0xF6, 0x1C, 0x46, 0x61, 0x69, 0x34, 0x40, 0x7E, 0x0F,
Simon Cooksey 0:fb7af294d5d9 74 0x55, 0x47, 0xA3, 0x23, 0xDD, 0x51, 0xAF, 0x3A, 0xC3, 0x5C,
Simon Cooksey 0:fb7af294d5d9 75 0xF9, 0xCE, 0xBA, 0xC5, 0xEA, 0x26, 0x2C, 0x53, 0x0D, 0x6E,
Simon Cooksey 0:fb7af294d5d9 76 0x85, 0x28, 0x84, 0x09, 0xD3, 0xDF, 0xCD, 0xF4, 0x41, 0x81,
Simon Cooksey 0:fb7af294d5d9 77 0x4D, 0x52, 0x6A, 0xDC, 0x37, 0xC8, 0x6C, 0xC1, 0xAB, 0xFA,
Simon Cooksey 0:fb7af294d5d9 78 0x24, 0xE1, 0x7B, 0x08, 0x0C, 0xBD, 0xB1, 0x4A, 0x78, 0x88,
Simon Cooksey 0:fb7af294d5d9 79 0x95, 0x8B, 0xE3, 0x63, 0xE8, 0x6D, 0xE9, 0xCB, 0xD5, 0xFE,
Simon Cooksey 0:fb7af294d5d9 80 0x3B, 0x00, 0x1D, 0x39, 0xF2, 0xEF, 0xB7, 0x0E, 0x66, 0x58,
Simon Cooksey 0:fb7af294d5d9 81 0xD0, 0xE4, 0xA6, 0x77, 0x72, 0xF8, 0xEB, 0x75, 0x4B, 0x0A,
Simon Cooksey 0:fb7af294d5d9 82 0x31, 0x44, 0x50, 0xB4, 0x8F, 0xED, 0x1F, 0x1A, 0xDB, 0x99,
Simon Cooksey 0:fb7af294d5d9 83 0x8D, 0x33, 0x9F, 0x11, 0x83, 0x14
Simon Cooksey 0:fb7af294d5d9 84 };
Simon Cooksey 0:fb7af294d5d9 85
Simon Cooksey 0:fb7af294d5d9 86 void mbedtls_md2_init( mbedtls_md2_context *ctx )
Simon Cooksey 0:fb7af294d5d9 87 {
Simon Cooksey 0:fb7af294d5d9 88 memset( ctx, 0, sizeof( mbedtls_md2_context ) );
Simon Cooksey 0:fb7af294d5d9 89 }
Simon Cooksey 0:fb7af294d5d9 90
Simon Cooksey 0:fb7af294d5d9 91 void mbedtls_md2_free( mbedtls_md2_context *ctx )
Simon Cooksey 0:fb7af294d5d9 92 {
Simon Cooksey 0:fb7af294d5d9 93 if( ctx == NULL )
Simon Cooksey 0:fb7af294d5d9 94 return;
Simon Cooksey 0:fb7af294d5d9 95
Simon Cooksey 0:fb7af294d5d9 96 mbedtls_zeroize( ctx, sizeof( mbedtls_md2_context ) );
Simon Cooksey 0:fb7af294d5d9 97 }
Simon Cooksey 0:fb7af294d5d9 98
Simon Cooksey 0:fb7af294d5d9 99 void mbedtls_md2_clone( mbedtls_md2_context *dst,
Simon Cooksey 0:fb7af294d5d9 100 const mbedtls_md2_context *src )
Simon Cooksey 0:fb7af294d5d9 101 {
Simon Cooksey 0:fb7af294d5d9 102 *dst = *src;
Simon Cooksey 0:fb7af294d5d9 103 }
Simon Cooksey 0:fb7af294d5d9 104
Simon Cooksey 0:fb7af294d5d9 105 /*
Simon Cooksey 0:fb7af294d5d9 106 * MD2 context setup
Simon Cooksey 0:fb7af294d5d9 107 */
Simon Cooksey 0:fb7af294d5d9 108 void mbedtls_md2_starts( mbedtls_md2_context *ctx )
Simon Cooksey 0:fb7af294d5d9 109 {
Simon Cooksey 0:fb7af294d5d9 110 memset( ctx->cksum, 0, 16 );
Simon Cooksey 0:fb7af294d5d9 111 memset( ctx->state, 0, 46 );
Simon Cooksey 0:fb7af294d5d9 112 memset( ctx->buffer, 0, 16 );
Simon Cooksey 0:fb7af294d5d9 113 ctx->left = 0;
Simon Cooksey 0:fb7af294d5d9 114 }
Simon Cooksey 0:fb7af294d5d9 115
Simon Cooksey 0:fb7af294d5d9 116 #if !defined(MBEDTLS_MD2_PROCESS_ALT)
Simon Cooksey 0:fb7af294d5d9 117 void mbedtls_md2_process( mbedtls_md2_context *ctx )
Simon Cooksey 0:fb7af294d5d9 118 {
Simon Cooksey 0:fb7af294d5d9 119 int i, j;
Simon Cooksey 0:fb7af294d5d9 120 unsigned char t = 0;
Simon Cooksey 0:fb7af294d5d9 121
Simon Cooksey 0:fb7af294d5d9 122 for( i = 0; i < 16; i++ )
Simon Cooksey 0:fb7af294d5d9 123 {
Simon Cooksey 0:fb7af294d5d9 124 ctx->state[i + 16] = ctx->buffer[i];
Simon Cooksey 0:fb7af294d5d9 125 ctx->state[i + 32] =
Simon Cooksey 0:fb7af294d5d9 126 (unsigned char)( ctx->buffer[i] ^ ctx->state[i]);
Simon Cooksey 0:fb7af294d5d9 127 }
Simon Cooksey 0:fb7af294d5d9 128
Simon Cooksey 0:fb7af294d5d9 129 for( i = 0; i < 18; i++ )
Simon Cooksey 0:fb7af294d5d9 130 {
Simon Cooksey 0:fb7af294d5d9 131 for( j = 0; j < 48; j++ )
Simon Cooksey 0:fb7af294d5d9 132 {
Simon Cooksey 0:fb7af294d5d9 133 ctx->state[j] = (unsigned char)
Simon Cooksey 0:fb7af294d5d9 134 ( ctx->state[j] ^ PI_SUBST[t] );
Simon Cooksey 0:fb7af294d5d9 135 t = ctx->state[j];
Simon Cooksey 0:fb7af294d5d9 136 }
Simon Cooksey 0:fb7af294d5d9 137
Simon Cooksey 0:fb7af294d5d9 138 t = (unsigned char)( t + i );
Simon Cooksey 0:fb7af294d5d9 139 }
Simon Cooksey 0:fb7af294d5d9 140
Simon Cooksey 0:fb7af294d5d9 141 t = ctx->cksum[15];
Simon Cooksey 0:fb7af294d5d9 142
Simon Cooksey 0:fb7af294d5d9 143 for( i = 0; i < 16; i++ )
Simon Cooksey 0:fb7af294d5d9 144 {
Simon Cooksey 0:fb7af294d5d9 145 ctx->cksum[i] = (unsigned char)
Simon Cooksey 0:fb7af294d5d9 146 ( ctx->cksum[i] ^ PI_SUBST[ctx->buffer[i] ^ t] );
Simon Cooksey 0:fb7af294d5d9 147 t = ctx->cksum[i];
Simon Cooksey 0:fb7af294d5d9 148 }
Simon Cooksey 0:fb7af294d5d9 149 }
Simon Cooksey 0:fb7af294d5d9 150 #endif /* !MBEDTLS_MD2_PROCESS_ALT */
Simon Cooksey 0:fb7af294d5d9 151
Simon Cooksey 0:fb7af294d5d9 152 /*
Simon Cooksey 0:fb7af294d5d9 153 * MD2 process buffer
Simon Cooksey 0:fb7af294d5d9 154 */
Simon Cooksey 0:fb7af294d5d9 155 void mbedtls_md2_update( mbedtls_md2_context *ctx, const unsigned char *input, size_t ilen )
Simon Cooksey 0:fb7af294d5d9 156 {
Simon Cooksey 0:fb7af294d5d9 157 size_t fill;
Simon Cooksey 0:fb7af294d5d9 158
Simon Cooksey 0:fb7af294d5d9 159 while( ilen > 0 )
Simon Cooksey 0:fb7af294d5d9 160 {
Simon Cooksey 0:fb7af294d5d9 161 if( ctx->left + ilen > 16 )
Simon Cooksey 0:fb7af294d5d9 162 fill = 16 - ctx->left;
Simon Cooksey 0:fb7af294d5d9 163 else
Simon Cooksey 0:fb7af294d5d9 164 fill = ilen;
Simon Cooksey 0:fb7af294d5d9 165
Simon Cooksey 0:fb7af294d5d9 166 memcpy( ctx->buffer + ctx->left, input, fill );
Simon Cooksey 0:fb7af294d5d9 167
Simon Cooksey 0:fb7af294d5d9 168 ctx->left += fill;
Simon Cooksey 0:fb7af294d5d9 169 input += fill;
Simon Cooksey 0:fb7af294d5d9 170 ilen -= fill;
Simon Cooksey 0:fb7af294d5d9 171
Simon Cooksey 0:fb7af294d5d9 172 if( ctx->left == 16 )
Simon Cooksey 0:fb7af294d5d9 173 {
Simon Cooksey 0:fb7af294d5d9 174 ctx->left = 0;
Simon Cooksey 0:fb7af294d5d9 175 mbedtls_md2_process( ctx );
Simon Cooksey 0:fb7af294d5d9 176 }
Simon Cooksey 0:fb7af294d5d9 177 }
Simon Cooksey 0:fb7af294d5d9 178 }
Simon Cooksey 0:fb7af294d5d9 179
Simon Cooksey 0:fb7af294d5d9 180 /*
Simon Cooksey 0:fb7af294d5d9 181 * MD2 final digest
Simon Cooksey 0:fb7af294d5d9 182 */
Simon Cooksey 0:fb7af294d5d9 183 void mbedtls_md2_finish( mbedtls_md2_context *ctx, unsigned char output[16] )
Simon Cooksey 0:fb7af294d5d9 184 {
Simon Cooksey 0:fb7af294d5d9 185 size_t i;
Simon Cooksey 0:fb7af294d5d9 186 unsigned char x;
Simon Cooksey 0:fb7af294d5d9 187
Simon Cooksey 0:fb7af294d5d9 188 x = (unsigned char)( 16 - ctx->left );
Simon Cooksey 0:fb7af294d5d9 189
Simon Cooksey 0:fb7af294d5d9 190 for( i = ctx->left; i < 16; i++ )
Simon Cooksey 0:fb7af294d5d9 191 ctx->buffer[i] = x;
Simon Cooksey 0:fb7af294d5d9 192
Simon Cooksey 0:fb7af294d5d9 193 mbedtls_md2_process( ctx );
Simon Cooksey 0:fb7af294d5d9 194
Simon Cooksey 0:fb7af294d5d9 195 memcpy( ctx->buffer, ctx->cksum, 16 );
Simon Cooksey 0:fb7af294d5d9 196 mbedtls_md2_process( ctx );
Simon Cooksey 0:fb7af294d5d9 197
Simon Cooksey 0:fb7af294d5d9 198 memcpy( output, ctx->state, 16 );
Simon Cooksey 0:fb7af294d5d9 199 }
Simon Cooksey 0:fb7af294d5d9 200
Simon Cooksey 0:fb7af294d5d9 201 #endif /* !MBEDTLS_MD2_ALT */
Simon Cooksey 0:fb7af294d5d9 202
Simon Cooksey 0:fb7af294d5d9 203 /*
Simon Cooksey 0:fb7af294d5d9 204 * output = MD2( input buffer )
Simon Cooksey 0:fb7af294d5d9 205 */
Simon Cooksey 0:fb7af294d5d9 206 void mbedtls_md2( const unsigned char *input, size_t ilen, unsigned char output[16] )
Simon Cooksey 0:fb7af294d5d9 207 {
Simon Cooksey 0:fb7af294d5d9 208 mbedtls_md2_context ctx;
Simon Cooksey 0:fb7af294d5d9 209
Simon Cooksey 0:fb7af294d5d9 210 mbedtls_md2_init( &ctx );
Simon Cooksey 0:fb7af294d5d9 211 mbedtls_md2_starts( &ctx );
Simon Cooksey 0:fb7af294d5d9 212 mbedtls_md2_update( &ctx, input, ilen );
Simon Cooksey 0:fb7af294d5d9 213 mbedtls_md2_finish( &ctx, output );
Simon Cooksey 0:fb7af294d5d9 214 mbedtls_md2_free( &ctx );
Simon Cooksey 0:fb7af294d5d9 215 }
Simon Cooksey 0:fb7af294d5d9 216
Simon Cooksey 0:fb7af294d5d9 217 #if defined(MBEDTLS_SELF_TEST)
Simon Cooksey 0:fb7af294d5d9 218
Simon Cooksey 0:fb7af294d5d9 219 /*
Simon Cooksey 0:fb7af294d5d9 220 * RFC 1319 test vectors
Simon Cooksey 0:fb7af294d5d9 221 */
Simon Cooksey 0:fb7af294d5d9 222 static const char md2_test_str[7][81] =
Simon Cooksey 0:fb7af294d5d9 223 {
Simon Cooksey 0:fb7af294d5d9 224 { "" },
Simon Cooksey 0:fb7af294d5d9 225 { "a" },
Simon Cooksey 0:fb7af294d5d9 226 { "abc" },
Simon Cooksey 0:fb7af294d5d9 227 { "message digest" },
Simon Cooksey 0:fb7af294d5d9 228 { "abcdefghijklmnopqrstuvwxyz" },
Simon Cooksey 0:fb7af294d5d9 229 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
Simon Cooksey 0:fb7af294d5d9 230 { "12345678901234567890123456789012345678901234567890123456789012" \
Simon Cooksey 0:fb7af294d5d9 231 "345678901234567890" }
Simon Cooksey 0:fb7af294d5d9 232 };
Simon Cooksey 0:fb7af294d5d9 233
Simon Cooksey 0:fb7af294d5d9 234 static const unsigned char md2_test_sum[7][16] =
Simon Cooksey 0:fb7af294d5d9 235 {
Simon Cooksey 0:fb7af294d5d9 236 { 0x83, 0x50, 0xE5, 0xA3, 0xE2, 0x4C, 0x15, 0x3D,
Simon Cooksey 0:fb7af294d5d9 237 0xF2, 0x27, 0x5C, 0x9F, 0x80, 0x69, 0x27, 0x73 },
Simon Cooksey 0:fb7af294d5d9 238 { 0x32, 0xEC, 0x01, 0xEC, 0x4A, 0x6D, 0xAC, 0x72,
Simon Cooksey 0:fb7af294d5d9 239 0xC0, 0xAB, 0x96, 0xFB, 0x34, 0xC0, 0xB5, 0xD1 },
Simon Cooksey 0:fb7af294d5d9 240 { 0xDA, 0x85, 0x3B, 0x0D, 0x3F, 0x88, 0xD9, 0x9B,
Simon Cooksey 0:fb7af294d5d9 241 0x30, 0x28, 0x3A, 0x69, 0xE6, 0xDE, 0xD6, 0xBB },
Simon Cooksey 0:fb7af294d5d9 242 { 0xAB, 0x4F, 0x49, 0x6B, 0xFB, 0x2A, 0x53, 0x0B,
Simon Cooksey 0:fb7af294d5d9 243 0x21, 0x9F, 0xF3, 0x30, 0x31, 0xFE, 0x06, 0xB0 },
Simon Cooksey 0:fb7af294d5d9 244 { 0x4E, 0x8D, 0xDF, 0xF3, 0x65, 0x02, 0x92, 0xAB,
Simon Cooksey 0:fb7af294d5d9 245 0x5A, 0x41, 0x08, 0xC3, 0xAA, 0x47, 0x94, 0x0B },
Simon Cooksey 0:fb7af294d5d9 246 { 0xDA, 0x33, 0xDE, 0xF2, 0xA4, 0x2D, 0xF1, 0x39,
Simon Cooksey 0:fb7af294d5d9 247 0x75, 0x35, 0x28, 0x46, 0xC3, 0x03, 0x38, 0xCD },
Simon Cooksey 0:fb7af294d5d9 248 { 0xD5, 0x97, 0x6F, 0x79, 0xD8, 0x3D, 0x3A, 0x0D,
Simon Cooksey 0:fb7af294d5d9 249 0xC9, 0x80, 0x6C, 0x3C, 0x66, 0xF3, 0xEF, 0xD8 }
Simon Cooksey 0:fb7af294d5d9 250 };
Simon Cooksey 0:fb7af294d5d9 251
Simon Cooksey 0:fb7af294d5d9 252 /*
Simon Cooksey 0:fb7af294d5d9 253 * Checkup routine
Simon Cooksey 0:fb7af294d5d9 254 */
Simon Cooksey 0:fb7af294d5d9 255 int mbedtls_md2_self_test( int verbose )
Simon Cooksey 0:fb7af294d5d9 256 {
Simon Cooksey 0:fb7af294d5d9 257 int i;
Simon Cooksey 0:fb7af294d5d9 258 unsigned char md2sum[16];
Simon Cooksey 0:fb7af294d5d9 259
Simon Cooksey 0:fb7af294d5d9 260 for( i = 0; i < 7; i++ )
Simon Cooksey 0:fb7af294d5d9 261 {
Simon Cooksey 0:fb7af294d5d9 262 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 263 mbedtls_printf( " MD2 test #%d: ", i + 1 );
Simon Cooksey 0:fb7af294d5d9 264
Simon Cooksey 0:fb7af294d5d9 265 mbedtls_md2( (unsigned char *) md2_test_str[i],
Simon Cooksey 0:fb7af294d5d9 266 strlen( md2_test_str[i] ), md2sum );
Simon Cooksey 0:fb7af294d5d9 267
Simon Cooksey 0:fb7af294d5d9 268 if( memcmp( md2sum, md2_test_sum[i], 16 ) != 0 )
Simon Cooksey 0:fb7af294d5d9 269 {
Simon Cooksey 0:fb7af294d5d9 270 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 271 mbedtls_printf( "failed\n" );
Simon Cooksey 0:fb7af294d5d9 272
Simon Cooksey 0:fb7af294d5d9 273 return( 1 );
Simon Cooksey 0:fb7af294d5d9 274 }
Simon Cooksey 0:fb7af294d5d9 275
Simon Cooksey 0:fb7af294d5d9 276 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 277 mbedtls_printf( "passed\n" );
Simon Cooksey 0:fb7af294d5d9 278 }
Simon Cooksey 0:fb7af294d5d9 279
Simon Cooksey 0:fb7af294d5d9 280 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 281 mbedtls_printf( "\n" );
Simon Cooksey 0:fb7af294d5d9 282
Simon Cooksey 0:fb7af294d5d9 283 return( 0 );
Simon Cooksey 0:fb7af294d5d9 284 }
Simon Cooksey 0:fb7af294d5d9 285
Simon Cooksey 0:fb7af294d5d9 286 #endif /* MBEDTLS_SELF_TEST */
Simon Cooksey 0:fb7af294d5d9 287
Simon Cooksey 0:fb7af294d5d9 288 #endif /* MBEDTLS_MD2_C */