Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

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