Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * VIA PadLock support functions |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | /* |
| 23 | * This implementation is based on the VIA PadLock Programming Guide: |
| 24 | * |
| 25 | * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/ |
| 26 | * programming_guide.pdf |
| 27 | */ |
| 28 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 29 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 31 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 32 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 33 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 34 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 35 | #if defined(MBEDTLS_PADLOCK_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 36 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 37 | #include "mbedtls/padlock.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 38 | |
Manuel Pégourié-Gonnard | 45ec8da | 2015-02-10 13:50:47 +0000 | [diff] [blame] | 39 | #include <string.h> |
| 40 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 41 | #if defined(MBEDTLS_HAVE_X86) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 42 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 43 | /* |
| 44 | * PadLock detection routine |
| 45 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 46 | int mbedtls_padlock_supports( int feature ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 47 | { |
| 48 | static int flags = -1; |
Paul Bakker | 53e1513 | 2013-12-30 20:43:40 +0100 | [diff] [blame] | 49 | int ebx = 0, edx = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 50 | |
| 51 | if( flags == -1 ) |
| 52 | { |
Manuel Pégourié-Gonnard | 87a8ffe | 2014-06-23 12:40:01 +0200 | [diff] [blame] | 53 | asm( "movl %%ebx, %0 \n\t" |
| 54 | "movl $0xC0000000, %%eax \n\t" |
| 55 | "cpuid \n\t" |
| 56 | "cmpl $0xC0000001, %%eax \n\t" |
| 57 | "movl $0, %%edx \n\t" |
| 58 | "jb unsupported \n\t" |
| 59 | "movl $0xC0000001, %%eax \n\t" |
| 60 | "cpuid \n\t" |
| 61 | "unsupported: \n\t" |
| 62 | "movl %%edx, %1 \n\t" |
| 63 | "movl %2, %%ebx \n\t" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 64 | : "=m" (ebx), "=m" (edx) |
| 65 | : "m" (ebx) |
| 66 | : "eax", "ecx", "edx" ); |
| 67 | |
| 68 | flags = edx; |
| 69 | } |
| 70 | |
| 71 | return( flags & feature ); |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * PadLock AES-ECB block en(de)cryption |
| 76 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 77 | int mbedtls_padlock_xcryptecb( mbedtls_aes_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 78 | int mode, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 79 | const unsigned char input[16], |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 80 | unsigned char output[16] ) |
| 81 | { |
Paul Bakker | 53e1513 | 2013-12-30 20:43:40 +0100 | [diff] [blame] | 82 | int ebx = 0; |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 83 | uint32_t *rk; |
| 84 | uint32_t *blk; |
| 85 | uint32_t *ctrl; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 86 | unsigned char buf[256]; |
| 87 | |
| 88 | rk = ctx->rk; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 89 | blk = MBEDTLS_PADLOCK_ALIGN16( buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 90 | memcpy( blk, input, 16 ); |
| 91 | |
| 92 | ctrl = blk + 4; |
| 93 | *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 ); |
| 94 | |
Manuel Pégourié-Gonnard | 87a8ffe | 2014-06-23 12:40:01 +0200 | [diff] [blame] | 95 | asm( "pushfl \n\t" |
| 96 | "popfl \n\t" |
| 97 | "movl %%ebx, %0 \n\t" |
| 98 | "movl $1, %%ecx \n\t" |
| 99 | "movl %2, %%edx \n\t" |
| 100 | "movl %3, %%ebx \n\t" |
| 101 | "movl %4, %%esi \n\t" |
| 102 | "movl %4, %%edi \n\t" |
| 103 | ".byte 0xf3,0x0f,0xa7,0xc8 \n\t" |
| 104 | "movl %1, %%ebx \n\t" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 105 | : "=m" (ebx) |
| 106 | : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk) |
Manuel Pégourié-Gonnard | cf20120 | 2015-04-02 10:46:55 +0100 | [diff] [blame] | 107 | : "memory", "ecx", "edx", "esi", "edi" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 108 | |
| 109 | memcpy( output, blk, 16 ); |
| 110 | |
| 111 | return( 0 ); |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * PadLock AES-CBC buffer en(de)cryption |
| 116 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 117 | int mbedtls_padlock_xcryptcbc( mbedtls_aes_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 118 | int mode, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 119 | size_t length, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 120 | unsigned char iv[16], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 121 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 122 | unsigned char *output ) |
| 123 | { |
Paul Bakker | 53e1513 | 2013-12-30 20:43:40 +0100 | [diff] [blame] | 124 | int ebx = 0; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 125 | size_t count; |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 126 | uint32_t *rk; |
| 127 | uint32_t *iw; |
| 128 | uint32_t *ctrl; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 129 | unsigned char buf[256]; |
| 130 | |
| 131 | if( ( (long) input & 15 ) != 0 || |
| 132 | ( (long) output & 15 ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 133 | return( MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 134 | |
| 135 | rk = ctx->rk; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 136 | iw = MBEDTLS_PADLOCK_ALIGN16( buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 137 | memcpy( iw, iv, 16 ); |
| 138 | |
| 139 | ctrl = iw + 4; |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 140 | *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode ^ 1 ) - 10 ) << 9 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 141 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 142 | count = ( length + 15 ) >> 4; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 143 | |
Manuel Pégourié-Gonnard | 87a8ffe | 2014-06-23 12:40:01 +0200 | [diff] [blame] | 144 | asm( "pushfl \n\t" |
| 145 | "popfl \n\t" |
| 146 | "movl %%ebx, %0 \n\t" |
| 147 | "movl %2, %%ecx \n\t" |
| 148 | "movl %3, %%edx \n\t" |
| 149 | "movl %4, %%ebx \n\t" |
| 150 | "movl %5, %%esi \n\t" |
| 151 | "movl %6, %%edi \n\t" |
| 152 | "movl %7, %%eax \n\t" |
| 153 | ".byte 0xf3,0x0f,0xa7,0xd0 \n\t" |
| 154 | "movl %1, %%ebx \n\t" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 155 | : "=m" (ebx) |
| 156 | : "m" (ebx), "m" (count), "m" (ctrl), |
| 157 | "m" (rk), "m" (input), "m" (output), "m" (iw) |
Manuel Pégourié-Gonnard | cf20120 | 2015-04-02 10:46:55 +0100 | [diff] [blame] | 158 | : "memory", "eax", "ecx", "edx", "esi", "edi" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 159 | |
| 160 | memcpy( iv, iw, 16 ); |
| 161 | |
| 162 | return( 0 ); |
| 163 | } |
| 164 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 165 | #endif /* MBEDTLS_HAVE_X86 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 166 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame^] | 167 | #endif /* MBEDTLS_PADLOCK_C */ |