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 * VIA PadLock support functions
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 * This implementation is based on the VIA PadLock Programming Guide:
nexpaq 1:55a6170b404f 23 *
nexpaq 1:55a6170b404f 24 * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
nexpaq 1:55a6170b404f 25 * programming_guide.pdf
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_PADLOCK_C)
nexpaq 1:55a6170b404f 35
nexpaq 1:55a6170b404f 36 #include "mbedtls/padlock.h"
nexpaq 1:55a6170b404f 37
nexpaq 1:55a6170b404f 38 #include <string.h>
nexpaq 1:55a6170b404f 39
nexpaq 1:55a6170b404f 40 #ifndef asm
nexpaq 1:55a6170b404f 41 #define asm __asm
nexpaq 1:55a6170b404f 42 #endif
nexpaq 1:55a6170b404f 43
nexpaq 1:55a6170b404f 44 #if defined(MBEDTLS_HAVE_X86)
nexpaq 1:55a6170b404f 45
nexpaq 1:55a6170b404f 46 /*
nexpaq 1:55a6170b404f 47 * PadLock detection routine
nexpaq 1:55a6170b404f 48 */
nexpaq 1:55a6170b404f 49 int mbedtls_padlock_has_support( int feature )
nexpaq 1:55a6170b404f 50 {
nexpaq 1:55a6170b404f 51 static int flags = -1;
nexpaq 1:55a6170b404f 52 int ebx = 0, edx = 0;
nexpaq 1:55a6170b404f 53
nexpaq 1:55a6170b404f 54 if( flags == -1 )
nexpaq 1:55a6170b404f 55 {
nexpaq 1:55a6170b404f 56 asm( "movl %%ebx, %0 \n\t"
nexpaq 1:55a6170b404f 57 "movl $0xC0000000, %%eax \n\t"
nexpaq 1:55a6170b404f 58 "cpuid \n\t"
nexpaq 1:55a6170b404f 59 "cmpl $0xC0000001, %%eax \n\t"
nexpaq 1:55a6170b404f 60 "movl $0, %%edx \n\t"
nexpaq 1:55a6170b404f 61 "jb unsupported \n\t"
nexpaq 1:55a6170b404f 62 "movl $0xC0000001, %%eax \n\t"
nexpaq 1:55a6170b404f 63 "cpuid \n\t"
nexpaq 1:55a6170b404f 64 "unsupported: \n\t"
nexpaq 1:55a6170b404f 65 "movl %%edx, %1 \n\t"
nexpaq 1:55a6170b404f 66 "movl %2, %%ebx \n\t"
nexpaq 1:55a6170b404f 67 : "=m" (ebx), "=m" (edx)
nexpaq 1:55a6170b404f 68 : "m" (ebx)
nexpaq 1:55a6170b404f 69 : "eax", "ecx", "edx" );
nexpaq 1:55a6170b404f 70
nexpaq 1:55a6170b404f 71 flags = edx;
nexpaq 1:55a6170b404f 72 }
nexpaq 1:55a6170b404f 73
nexpaq 1:55a6170b404f 74 return( flags & feature );
nexpaq 1:55a6170b404f 75 }
nexpaq 1:55a6170b404f 76
nexpaq 1:55a6170b404f 77 /*
nexpaq 1:55a6170b404f 78 * PadLock AES-ECB block en(de)cryption
nexpaq 1:55a6170b404f 79 */
nexpaq 1:55a6170b404f 80 int mbedtls_padlock_xcryptecb( mbedtls_aes_context *ctx,
nexpaq 1:55a6170b404f 81 int mode,
nexpaq 1:55a6170b404f 82 const unsigned char input[16],
nexpaq 1:55a6170b404f 83 unsigned char output[16] )
nexpaq 1:55a6170b404f 84 {
nexpaq 1:55a6170b404f 85 int ebx = 0;
nexpaq 1:55a6170b404f 86 uint32_t *rk;
nexpaq 1:55a6170b404f 87 uint32_t *blk;
nexpaq 1:55a6170b404f 88 uint32_t *ctrl;
nexpaq 1:55a6170b404f 89 unsigned char buf[256];
nexpaq 1:55a6170b404f 90
nexpaq 1:55a6170b404f 91 rk = ctx->rk;
nexpaq 1:55a6170b404f 92 blk = MBEDTLS_PADLOCK_ALIGN16( buf );
nexpaq 1:55a6170b404f 93 memcpy( blk, input, 16 );
nexpaq 1:55a6170b404f 94
nexpaq 1:55a6170b404f 95 ctrl = blk + 4;
nexpaq 1:55a6170b404f 96 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 );
nexpaq 1:55a6170b404f 97
nexpaq 1:55a6170b404f 98 asm( "pushfl \n\t"
nexpaq 1:55a6170b404f 99 "popfl \n\t"
nexpaq 1:55a6170b404f 100 "movl %%ebx, %0 \n\t"
nexpaq 1:55a6170b404f 101 "movl $1, %%ecx \n\t"
nexpaq 1:55a6170b404f 102 "movl %2, %%edx \n\t"
nexpaq 1:55a6170b404f 103 "movl %3, %%ebx \n\t"
nexpaq 1:55a6170b404f 104 "movl %4, %%esi \n\t"
nexpaq 1:55a6170b404f 105 "movl %4, %%edi \n\t"
nexpaq 1:55a6170b404f 106 ".byte 0xf3,0x0f,0xa7,0xc8 \n\t"
nexpaq 1:55a6170b404f 107 "movl %1, %%ebx \n\t"
nexpaq 1:55a6170b404f 108 : "=m" (ebx)
nexpaq 1:55a6170b404f 109 : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
nexpaq 1:55a6170b404f 110 : "memory", "ecx", "edx", "esi", "edi" );
nexpaq 1:55a6170b404f 111
nexpaq 1:55a6170b404f 112 memcpy( output, blk, 16 );
nexpaq 1:55a6170b404f 113
nexpaq 1:55a6170b404f 114 return( 0 );
nexpaq 1:55a6170b404f 115 }
nexpaq 1:55a6170b404f 116
nexpaq 1:55a6170b404f 117 /*
nexpaq 1:55a6170b404f 118 * PadLock AES-CBC buffer en(de)cryption
nexpaq 1:55a6170b404f 119 */
nexpaq 1:55a6170b404f 120 int mbedtls_padlock_xcryptcbc( mbedtls_aes_context *ctx,
nexpaq 1:55a6170b404f 121 int mode,
nexpaq 1:55a6170b404f 122 size_t length,
nexpaq 1:55a6170b404f 123 unsigned char iv[16],
nexpaq 1:55a6170b404f 124 const unsigned char *input,
nexpaq 1:55a6170b404f 125 unsigned char *output )
nexpaq 1:55a6170b404f 126 {
nexpaq 1:55a6170b404f 127 int ebx = 0;
nexpaq 1:55a6170b404f 128 size_t count;
nexpaq 1:55a6170b404f 129 uint32_t *rk;
nexpaq 1:55a6170b404f 130 uint32_t *iw;
nexpaq 1:55a6170b404f 131 uint32_t *ctrl;
nexpaq 1:55a6170b404f 132 unsigned char buf[256];
nexpaq 1:55a6170b404f 133
nexpaq 1:55a6170b404f 134 if( ( (long) input & 15 ) != 0 ||
nexpaq 1:55a6170b404f 135 ( (long) output & 15 ) != 0 )
nexpaq 1:55a6170b404f 136 return( MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED );
nexpaq 1:55a6170b404f 137
nexpaq 1:55a6170b404f 138 rk = ctx->rk;
nexpaq 1:55a6170b404f 139 iw = MBEDTLS_PADLOCK_ALIGN16( buf );
nexpaq 1:55a6170b404f 140 memcpy( iw, iv, 16 );
nexpaq 1:55a6170b404f 141
nexpaq 1:55a6170b404f 142 ctrl = iw + 4;
nexpaq 1:55a6170b404f 143 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode ^ 1 ) - 10 ) << 9 );
nexpaq 1:55a6170b404f 144
nexpaq 1:55a6170b404f 145 count = ( length + 15 ) >> 4;
nexpaq 1:55a6170b404f 146
nexpaq 1:55a6170b404f 147 asm( "pushfl \n\t"
nexpaq 1:55a6170b404f 148 "popfl \n\t"
nexpaq 1:55a6170b404f 149 "movl %%ebx, %0 \n\t"
nexpaq 1:55a6170b404f 150 "movl %2, %%ecx \n\t"
nexpaq 1:55a6170b404f 151 "movl %3, %%edx \n\t"
nexpaq 1:55a6170b404f 152 "movl %4, %%ebx \n\t"
nexpaq 1:55a6170b404f 153 "movl %5, %%esi \n\t"
nexpaq 1:55a6170b404f 154 "movl %6, %%edi \n\t"
nexpaq 1:55a6170b404f 155 "movl %7, %%eax \n\t"
nexpaq 1:55a6170b404f 156 ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
nexpaq 1:55a6170b404f 157 "movl %1, %%ebx \n\t"
nexpaq 1:55a6170b404f 158 : "=m" (ebx)
nexpaq 1:55a6170b404f 159 : "m" (ebx), "m" (count), "m" (ctrl),
nexpaq 1:55a6170b404f 160 "m" (rk), "m" (input), "m" (output), "m" (iw)
nexpaq 1:55a6170b404f 161 : "memory", "eax", "ecx", "edx", "esi", "edi" );
nexpaq 1:55a6170b404f 162
nexpaq 1:55a6170b404f 163 memcpy( iv, iw, 16 );
nexpaq 1:55a6170b404f 164
nexpaq 1:55a6170b404f 165 return( 0 );
nexpaq 1:55a6170b404f 166 }
nexpaq 1:55a6170b404f 167
nexpaq 1:55a6170b404f 168 #endif /* MBEDTLS_HAVE_X86 */
nexpaq 1:55a6170b404f 169
nexpaq 1:55a6170b404f 170 #endif /* MBEDTLS_PADLOCK_C */