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