- Main Page
- Data Structures
- Files

# padlock.c

Go to the documentation of this file.

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