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