mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 11:cada08fc8a70 1 /*
mbedAustin 11:cada08fc8a70 2 * VIA PadLock support functions
mbedAustin 11:cada08fc8a70 3 *
mbedAustin 11:cada08fc8a70 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
mbedAustin 11:cada08fc8a70 5 * SPDX-License-Identifier: Apache-2.0
mbedAustin 11:cada08fc8a70 6 *
mbedAustin 11:cada08fc8a70 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
mbedAustin 11:cada08fc8a70 8 * not use this file except in compliance with the License.
mbedAustin 11:cada08fc8a70 9 * You may obtain a copy of the License at
mbedAustin 11:cada08fc8a70 10 *
mbedAustin 11:cada08fc8a70 11 * http://www.apache.org/licenses/LICENSE-2.0
mbedAustin 11:cada08fc8a70 12 *
mbedAustin 11:cada08fc8a70 13 * Unless required by applicable law or agreed to in writing, software
mbedAustin 11:cada08fc8a70 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
mbedAustin 11:cada08fc8a70 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbedAustin 11:cada08fc8a70 16 * See the License for the specific language governing permissions and
mbedAustin 11:cada08fc8a70 17 * limitations under the License.
mbedAustin 11:cada08fc8a70 18 *
mbedAustin 11:cada08fc8a70 19 * This file is part of mbed TLS (https://tls.mbed.org)
mbedAustin 11:cada08fc8a70 20 */
mbedAustin 11:cada08fc8a70 21 /*
mbedAustin 11:cada08fc8a70 22 * This implementation is based on the VIA PadLock Programming Guide:
mbedAustin 11:cada08fc8a70 23 *
mbedAustin 11:cada08fc8a70 24 * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
mbedAustin 11:cada08fc8a70 25 * programming_guide.pdf
mbedAustin 11:cada08fc8a70 26 */
mbedAustin 11:cada08fc8a70 27
mbedAustin 11:cada08fc8a70 28 #if !defined(MBEDTLS_CONFIG_FILE)
mbedAustin 11:cada08fc8a70 29 #include "mbedtls/config.h"
mbedAustin 11:cada08fc8a70 30 #else
mbedAustin 11:cada08fc8a70 31 #include MBEDTLS_CONFIG_FILE
mbedAustin 11:cada08fc8a70 32 #endif
mbedAustin 11:cada08fc8a70 33
mbedAustin 11:cada08fc8a70 34 #if defined(MBEDTLS_PADLOCK_C)
mbedAustin 11:cada08fc8a70 35
mbedAustin 11:cada08fc8a70 36 #include "mbedtls/padlock.h"
mbedAustin 11:cada08fc8a70 37
mbedAustin 11:cada08fc8a70 38 #include <string.h>
mbedAustin 11:cada08fc8a70 39
mbedAustin 11:cada08fc8a70 40 #ifndef asm
mbedAustin 11:cada08fc8a70 41 #define asm __asm
mbedAustin 11:cada08fc8a70 42 #endif
mbedAustin 11:cada08fc8a70 43
mbedAustin 11:cada08fc8a70 44 #if defined(MBEDTLS_HAVE_X86)
mbedAustin 11:cada08fc8a70 45
mbedAustin 11:cada08fc8a70 46 /*
mbedAustin 11:cada08fc8a70 47 * PadLock detection routine
mbedAustin 11:cada08fc8a70 48 */
mbedAustin 11:cada08fc8a70 49 int mbedtls_padlock_has_support( int feature )
mbedAustin 11:cada08fc8a70 50 {
mbedAustin 11:cada08fc8a70 51 static int flags = -1;
mbedAustin 11:cada08fc8a70 52 int ebx = 0, edx = 0;
mbedAustin 11:cada08fc8a70 53
mbedAustin 11:cada08fc8a70 54 if( flags == -1 )
mbedAustin 11:cada08fc8a70 55 {
mbedAustin 11:cada08fc8a70 56 asm( "movl %%ebx, %0 \n\t"
mbedAustin 11:cada08fc8a70 57 "movl $0xC0000000, %%eax \n\t"
mbedAustin 11:cada08fc8a70 58 "cpuid \n\t"
mbedAustin 11:cada08fc8a70 59 "cmpl $0xC0000001, %%eax \n\t"
mbedAustin 11:cada08fc8a70 60 "movl $0, %%edx \n\t"
mbedAustin 11:cada08fc8a70 61 "jb unsupported \n\t"
mbedAustin 11:cada08fc8a70 62 "movl $0xC0000001, %%eax \n\t"
mbedAustin 11:cada08fc8a70 63 "cpuid \n\t"
mbedAustin 11:cada08fc8a70 64 "unsupported: \n\t"
mbedAustin 11:cada08fc8a70 65 "movl %%edx, %1 \n\t"
mbedAustin 11:cada08fc8a70 66 "movl %2, %%ebx \n\t"
mbedAustin 11:cada08fc8a70 67 : "=m" (ebx), "=m" (edx)
mbedAustin 11:cada08fc8a70 68 : "m" (ebx)
mbedAustin 11:cada08fc8a70 69 : "eax", "ecx", "edx" );
mbedAustin 11:cada08fc8a70 70
mbedAustin 11:cada08fc8a70 71 flags = edx;
mbedAustin 11:cada08fc8a70 72 }
mbedAustin 11:cada08fc8a70 73
mbedAustin 11:cada08fc8a70 74 return( flags & feature );
mbedAustin 11:cada08fc8a70 75 }
mbedAustin 11:cada08fc8a70 76
mbedAustin 11:cada08fc8a70 77 /*
mbedAustin 11:cada08fc8a70 78 * PadLock AES-ECB block en(de)cryption
mbedAustin 11:cada08fc8a70 79 */
mbedAustin 11:cada08fc8a70 80 int mbedtls_padlock_xcryptecb( mbedtls_aes_context *ctx,
mbedAustin 11:cada08fc8a70 81 int mode,
mbedAustin 11:cada08fc8a70 82 const unsigned char input[16],
mbedAustin 11:cada08fc8a70 83 unsigned char output[16] )
mbedAustin 11:cada08fc8a70 84 {
mbedAustin 11:cada08fc8a70 85 int ebx = 0;
mbedAustin 11:cada08fc8a70 86 uint32_t *rk;
mbedAustin 11:cada08fc8a70 87 uint32_t *blk;
mbedAustin 11:cada08fc8a70 88 uint32_t *ctrl;
mbedAustin 11:cada08fc8a70 89 unsigned char buf[256];
mbedAustin 11:cada08fc8a70 90
mbedAustin 11:cada08fc8a70 91 rk = ctx->rk;
mbedAustin 11:cada08fc8a70 92 blk = MBEDTLS_PADLOCK_ALIGN16( buf );
mbedAustin 11:cada08fc8a70 93 memcpy( blk, input, 16 );
mbedAustin 11:cada08fc8a70 94
mbedAustin 11:cada08fc8a70 95 ctrl = blk + 4;
mbedAustin 11:cada08fc8a70 96 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 );
mbedAustin 11:cada08fc8a70 97
mbedAustin 11:cada08fc8a70 98 asm( "pushfl \n\t"
mbedAustin 11:cada08fc8a70 99 "popfl \n\t"
mbedAustin 11:cada08fc8a70 100 "movl %%ebx, %0 \n\t"
mbedAustin 11:cada08fc8a70 101 "movl $1, %%ecx \n\t"
mbedAustin 11:cada08fc8a70 102 "movl %2, %%edx \n\t"
mbedAustin 11:cada08fc8a70 103 "movl %3, %%ebx \n\t"
mbedAustin 11:cada08fc8a70 104 "movl %4, %%esi \n\t"
mbedAustin 11:cada08fc8a70 105 "movl %4, %%edi \n\t"
mbedAustin 11:cada08fc8a70 106 ".byte 0xf3,0x0f,0xa7,0xc8 \n\t"
mbedAustin 11:cada08fc8a70 107 "movl %1, %%ebx \n\t"
mbedAustin 11:cada08fc8a70 108 : "=m" (ebx)
mbedAustin 11:cada08fc8a70 109 : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
mbedAustin 11:cada08fc8a70 110 : "memory", "ecx", "edx", "esi", "edi" );
mbedAustin 11:cada08fc8a70 111
mbedAustin 11:cada08fc8a70 112 memcpy( output, blk, 16 );
mbedAustin 11:cada08fc8a70 113
mbedAustin 11:cada08fc8a70 114 return( 0 );
mbedAustin 11:cada08fc8a70 115 }
mbedAustin 11:cada08fc8a70 116
mbedAustin 11:cada08fc8a70 117 /*
mbedAustin 11:cada08fc8a70 118 * PadLock AES-CBC buffer en(de)cryption
mbedAustin 11:cada08fc8a70 119 */
mbedAustin 11:cada08fc8a70 120 int mbedtls_padlock_xcryptcbc( mbedtls_aes_context *ctx,
mbedAustin 11:cada08fc8a70 121 int mode,
mbedAustin 11:cada08fc8a70 122 size_t length,
mbedAustin 11:cada08fc8a70 123 unsigned char iv[16],
mbedAustin 11:cada08fc8a70 124 const unsigned char *input,
mbedAustin 11:cada08fc8a70 125 unsigned char *output )
mbedAustin 11:cada08fc8a70 126 {
mbedAustin 11:cada08fc8a70 127 int ebx = 0;
mbedAustin 11:cada08fc8a70 128 size_t count;
mbedAustin 11:cada08fc8a70 129 uint32_t *rk;
mbedAustin 11:cada08fc8a70 130 uint32_t *iw;
mbedAustin 11:cada08fc8a70 131 uint32_t *ctrl;
mbedAustin 11:cada08fc8a70 132 unsigned char buf[256];
mbedAustin 11:cada08fc8a70 133
mbedAustin 11:cada08fc8a70 134 if( ( (long) input & 15 ) != 0 ||
mbedAustin 11:cada08fc8a70 135 ( (long) output & 15 ) != 0 )
mbedAustin 11:cada08fc8a70 136 return( MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED );
mbedAustin 11:cada08fc8a70 137
mbedAustin 11:cada08fc8a70 138 rk = ctx->rk;
mbedAustin 11:cada08fc8a70 139 iw = MBEDTLS_PADLOCK_ALIGN16( buf );
mbedAustin 11:cada08fc8a70 140 memcpy( iw, iv, 16 );
mbedAustin 11:cada08fc8a70 141
mbedAustin 11:cada08fc8a70 142 ctrl = iw + 4;
mbedAustin 11:cada08fc8a70 143 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode ^ 1 ) - 10 ) << 9 );
mbedAustin 11:cada08fc8a70 144
mbedAustin 11:cada08fc8a70 145 count = ( length + 15 ) >> 4;
mbedAustin 11:cada08fc8a70 146
mbedAustin 11:cada08fc8a70 147 asm( "pushfl \n\t"
mbedAustin 11:cada08fc8a70 148 "popfl \n\t"
mbedAustin 11:cada08fc8a70 149 "movl %%ebx, %0 \n\t"
mbedAustin 11:cada08fc8a70 150 "movl %2, %%ecx \n\t"
mbedAustin 11:cada08fc8a70 151 "movl %3, %%edx \n\t"
mbedAustin 11:cada08fc8a70 152 "movl %4, %%ebx \n\t"
mbedAustin 11:cada08fc8a70 153 "movl %5, %%esi \n\t"
mbedAustin 11:cada08fc8a70 154 "movl %6, %%edi \n\t"
mbedAustin 11:cada08fc8a70 155 "movl %7, %%eax \n\t"
mbedAustin 11:cada08fc8a70 156 ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
mbedAustin 11:cada08fc8a70 157 "movl %1, %%ebx \n\t"
mbedAustin 11:cada08fc8a70 158 : "=m" (ebx)
mbedAustin 11:cada08fc8a70 159 : "m" (ebx), "m" (count), "m" (ctrl),
mbedAustin 11:cada08fc8a70 160 "m" (rk), "m" (input), "m" (output), "m" (iw)
mbedAustin 11:cada08fc8a70 161 : "memory", "eax", "ecx", "edx", "esi", "edi" );
mbedAustin 11:cada08fc8a70 162
mbedAustin 11:cada08fc8a70 163 memcpy( iv, iw, 16 );
mbedAustin 11:cada08fc8a70 164
mbedAustin 11:cada08fc8a70 165 return( 0 );
mbedAustin 11:cada08fc8a70 166 }
mbedAustin 11:cada08fc8a70 167
mbedAustin 11:cada08fc8a70 168 #endif /* MBEDTLS_HAVE_X86 */
mbedAustin 11:cada08fc8a70 169
mbedAustin 11:cada08fc8a70 170 #endif /* MBEDTLS_PADLOCK_C */