Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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