nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nexpaq
Date:
Sat Sep 17 16:32:05 2016 +0000
Revision:
1:55a6170b404f
checking in for sharing

Who changed what in which revision?

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