PolarSSL v1.1.4
|
00001 /* 00002 * VIA PadLock support functions 00003 * 00004 * Copyright (C) 2006-2010, Brainspark B.V. 00005 * 00006 * This file is part of PolarSSL (http://www.polarssl.org) 00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> 00008 * 00009 * All rights reserved. 00010 * 00011 * This program is free software; you can redistribute it and/or modify 00012 * it under the terms of the GNU General Public License as published by 00013 * the Free Software Foundation; either version 2 of the License, or 00014 * (at your option) any later version. 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU General Public License along 00022 * with this program; if not, write to the Free Software Foundation, Inc., 00023 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00024 */ 00025 /* 00026 * This implementation is based on the VIA PadLock Programming Guide: 00027 * 00028 * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/ 00029 * programming_guide.pdf 00030 */ 00031 00032 #include "polarssl/config.h" 00033 00034 #if defined(POLARSSL_PADLOCK_C) 00035 00036 #include "polarssl/padlock.h" 00037 00038 #if defined(POLARSSL_HAVE_X86) 00039 00040 /* 00041 * PadLock detection routine 00042 */ 00043 int padlock_supports( int feature ) 00044 { 00045 static int flags = -1; 00046 int ebx, edx; 00047 00048 if( flags == -1 ) 00049 { 00050 asm( "movl %%ebx, %0 \n" \ 00051 "movl $0xC0000000, %%eax \n" \ 00052 "cpuid \n" \ 00053 "cmpl $0xC0000001, %%eax \n" \ 00054 "movl $0, %%edx \n" \ 00055 "jb unsupported \n" \ 00056 "movl $0xC0000001, %%eax \n" \ 00057 "cpuid \n" \ 00058 "unsupported: \n" \ 00059 "movl %%edx, %1 \n" \ 00060 "movl %2, %%ebx \n" 00061 : "=m" (ebx), "=m" (edx) 00062 : "m" (ebx) 00063 : "eax", "ecx", "edx" ); 00064 00065 flags = edx; 00066 } 00067 00068 return( flags & feature ); 00069 } 00070 00071 /* 00072 * PadLock AES-ECB block en(de)cryption 00073 */ 00074 int padlock_xcryptecb( aes_context *ctx, 00075 int mode, 00076 const unsigned char input[16], 00077 unsigned char output[16] ) 00078 { 00079 int ebx; 00080 unsigned long *rk; 00081 unsigned long *blk; 00082 unsigned long *ctrl; 00083 unsigned char buf[256]; 00084 00085 rk = ctx->rk; 00086 blk = PADLOCK_ALIGN16( buf ); 00087 memcpy( blk, input, 16 ); 00088 00089 ctrl = blk + 4; 00090 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 ); 00091 00092 asm( "pushfl; popfl \n" \ 00093 "movl %%ebx, %0 \n" \ 00094 "movl $1, %%ecx \n" \ 00095 "movl %2, %%edx \n" \ 00096 "movl %3, %%ebx \n" \ 00097 "movl %4, %%esi \n" \ 00098 "movl %4, %%edi \n" \ 00099 ".byte 0xf3,0x0f,0xa7,0xc8\n" \ 00100 "movl %1, %%ebx \n" 00101 : "=m" (ebx) 00102 : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk) 00103 : "ecx", "edx", "esi", "edi" ); 00104 00105 memcpy( output, blk, 16 ); 00106 00107 return( 0 ); 00108 } 00109 00110 /* 00111 * PadLock AES-CBC buffer en(de)cryption 00112 */ 00113 int padlock_xcryptcbc( aes_context *ctx, 00114 int mode, 00115 size_t length, 00116 unsigned char iv[16], 00117 const unsigned char *input, 00118 unsigned char *output ) 00119 { 00120 int ebx; 00121 size_t count; 00122 unsigned long *rk; 00123 unsigned long *iw; 00124 unsigned long *ctrl; 00125 unsigned char buf[256]; 00126 00127 if( ( (long) input & 15 ) != 0 || 00128 ( (long) output & 15 ) != 0 ) 00129 return( POLARSSL_ERR_PADLOCK_DATA_MISALIGNED ); 00130 00131 rk = ctx->rk; 00132 iw = PADLOCK_ALIGN16( buf ); 00133 memcpy( iw, iv, 16 ); 00134 00135 ctrl = iw + 4; 00136 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + (mode^1) - 10 ) << 9 ); 00137 00138 count = (length + 15) >> 4; 00139 00140 asm( "pushfl; popfl \n" \ 00141 "movl %%ebx, %0 \n" \ 00142 "movl %2, %%ecx \n" \ 00143 "movl %3, %%edx \n" \ 00144 "movl %4, %%ebx \n" \ 00145 "movl %5, %%esi \n" \ 00146 "movl %6, %%edi \n" \ 00147 "movl %7, %%eax \n" \ 00148 ".byte 0xf3,0x0f,0xa7,0xd0\n" \ 00149 "movl %1, %%ebx \n" 00150 : "=m" (ebx) 00151 : "m" (ebx), "m" (count), "m" (ctrl), 00152 "m" (rk), "m" (input), "m" (output), "m" (iw) 00153 : "eax", "ecx", "edx", "esi", "edi" ); 00154 00155 memcpy( iv, iw, 16 ); 00156 00157 return( 0 ); 00158 } 00159 00160 #endif 00161 00162 #endif