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