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