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 | 860b516 | 2015-01-28 17:12:07 +0000 | [diff] [blame^] | 6 | * This file is part of mbed TLS (https://polarssl.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 | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 29 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 30 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 31 | #else |
| 32 | #include POLARSSL_CONFIG_FILE |
| 33 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 34 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 35 | #if defined(POLARSSL_PADLOCK_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 36 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 37 | #include "polarssl/padlock.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 38 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 39 | #if defined(POLARSSL_HAVE_X86) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 40 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 41 | /* |
| 42 | * PadLock detection routine |
| 43 | */ |
| 44 | int padlock_supports( int feature ) |
| 45 | { |
| 46 | static int flags = -1; |
Paul Bakker | 53e1513 | 2013-12-30 20:43:40 +0100 | [diff] [blame] | 47 | int ebx = 0, edx = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 48 | |
| 49 | if( flags == -1 ) |
| 50 | { |
Manuel Pégourié-Gonnard | 87a8ffe | 2014-06-23 12:40:01 +0200 | [diff] [blame] | 51 | asm( "movl %%ebx, %0 \n\t" |
| 52 | "movl $0xC0000000, %%eax \n\t" |
| 53 | "cpuid \n\t" |
| 54 | "cmpl $0xC0000001, %%eax \n\t" |
| 55 | "movl $0, %%edx \n\t" |
| 56 | "jb unsupported \n\t" |
| 57 | "movl $0xC0000001, %%eax \n\t" |
| 58 | "cpuid \n\t" |
| 59 | "unsupported: \n\t" |
| 60 | "movl %%edx, %1 \n\t" |
| 61 | "movl %2, %%ebx \n\t" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 62 | : "=m" (ebx), "=m" (edx) |
| 63 | : "m" (ebx) |
| 64 | : "eax", "ecx", "edx" ); |
| 65 | |
| 66 | flags = edx; |
| 67 | } |
| 68 | |
| 69 | return( flags & feature ); |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * PadLock AES-ECB block en(de)cryption |
| 74 | */ |
| 75 | int padlock_xcryptecb( aes_context *ctx, |
| 76 | int mode, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 77 | const unsigned char input[16], |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 78 | unsigned char output[16] ) |
| 79 | { |
Paul Bakker | 53e1513 | 2013-12-30 20:43:40 +0100 | [diff] [blame] | 80 | int ebx = 0; |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 81 | uint32_t *rk; |
| 82 | uint32_t *blk; |
| 83 | uint32_t *ctrl; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 84 | unsigned char buf[256]; |
| 85 | |
| 86 | rk = ctx->rk; |
| 87 | blk = PADLOCK_ALIGN16( buf ); |
| 88 | memcpy( blk, input, 16 ); |
| 89 | |
| 90 | ctrl = blk + 4; |
| 91 | *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 ); |
| 92 | |
Manuel Pégourié-Gonnard | 87a8ffe | 2014-06-23 12:40:01 +0200 | [diff] [blame] | 93 | asm( "pushfl \n\t" |
| 94 | "popfl \n\t" |
| 95 | "movl %%ebx, %0 \n\t" |
| 96 | "movl $1, %%ecx \n\t" |
| 97 | "movl %2, %%edx \n\t" |
| 98 | "movl %3, %%ebx \n\t" |
| 99 | "movl %4, %%esi \n\t" |
| 100 | "movl %4, %%edi \n\t" |
| 101 | ".byte 0xf3,0x0f,0xa7,0xc8 \n\t" |
| 102 | "movl %1, %%ebx \n\t" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 103 | : "=m" (ebx) |
| 104 | : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk) |
| 105 | : "ecx", "edx", "esi", "edi" ); |
| 106 | |
| 107 | memcpy( output, blk, 16 ); |
| 108 | |
| 109 | return( 0 ); |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * PadLock AES-CBC buffer en(de)cryption |
| 114 | */ |
| 115 | int padlock_xcryptcbc( aes_context *ctx, |
| 116 | int mode, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 117 | size_t length, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 118 | unsigned char iv[16], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 119 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 120 | unsigned char *output ) |
| 121 | { |
Paul Bakker | 53e1513 | 2013-12-30 20:43:40 +0100 | [diff] [blame] | 122 | int ebx = 0; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 123 | size_t count; |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 124 | uint32_t *rk; |
| 125 | uint32_t *iw; |
| 126 | uint32_t *ctrl; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 127 | unsigned char buf[256]; |
| 128 | |
| 129 | if( ( (long) input & 15 ) != 0 || |
| 130 | ( (long) output & 15 ) != 0 ) |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 131 | return( POLARSSL_ERR_PADLOCK_DATA_MISALIGNED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 132 | |
| 133 | rk = ctx->rk; |
| 134 | iw = PADLOCK_ALIGN16( buf ); |
| 135 | memcpy( iw, iv, 16 ); |
| 136 | |
| 137 | ctrl = iw + 4; |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 138 | *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode ^ 1 ) - 10 ) << 9 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 139 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 140 | count = ( length + 15 ) >> 4; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 141 | |
Manuel Pégourié-Gonnard | 87a8ffe | 2014-06-23 12:40:01 +0200 | [diff] [blame] | 142 | asm( "pushfl \n\t" |
| 143 | "popfl \n\t" |
| 144 | "movl %%ebx, %0 \n\t" |
| 145 | "movl %2, %%ecx \n\t" |
| 146 | "movl %3, %%edx \n\t" |
| 147 | "movl %4, %%ebx \n\t" |
| 148 | "movl %5, %%esi \n\t" |
| 149 | "movl %6, %%edi \n\t" |
| 150 | "movl %7, %%eax \n\t" |
| 151 | ".byte 0xf3,0x0f,0xa7,0xd0 \n\t" |
| 152 | "movl %1, %%ebx \n\t" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 153 | : "=m" (ebx) |
| 154 | : "m" (ebx), "m" (count), "m" (ctrl), |
| 155 | "m" (rk), "m" (input), "m" (output), "m" (iw) |
| 156 | : "eax", "ecx", "edx", "esi", "edi" ); |
| 157 | |
| 158 | memcpy( iv, iw, 16 ); |
| 159 | |
| 160 | return( 0 ); |
| 161 | } |
| 162 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 163 | #endif /* POLARSSL_HAVE_X86 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 164 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 165 | #endif /* POLARSSL_PADLOCK_C */ |