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