This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
maclobdell
Date:
Wed May 18 19:06:32 2016 +0000
Revision:
0:f7c60d3e7b8a
clean version

Who changed what in which revision?

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