BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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