mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
Parent:
0:5b88d5760320
updated based on mbed-os5.15.0

Who changed what in which revision?

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