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