Example program to test AES-GCM functionality. Used for a workshop

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers padlock.c Source File

padlock.c

00001 /*
00002  *  VIA PadLock support functions
00003  *
00004  *  Copyright (C) 2006-2014, 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 #if !defined(POLARSSL_CONFIG_FILE)
00033 #include "polarssl/config.h"
00034 #else
00035 #include POLARSSL_CONFIG_FILE
00036 #endif
00037 
00038 #if defined(POLARSSL_PADLOCK_C)
00039 
00040 #include "polarssl/padlock.h"
00041 
00042 #if defined(POLARSSL_HAVE_X86)
00043 
00044 /*
00045  * PadLock detection routine
00046  */
00047 int padlock_supports( int feature )
00048 {
00049     static int flags = -1;
00050     int ebx = 0, edx = 0;
00051 
00052     if( flags == -1 )
00053     {
00054         asm( "movl  %%ebx, %0           \n"     \
00055              "movl  $0xC0000000, %%eax  \n"     \
00056              "cpuid                     \n"     \
00057              "cmpl  $0xC0000001, %%eax  \n"     \
00058              "movl  $0, %%edx           \n"     \
00059              "jb    unsupported         \n"     \
00060              "movl  $0xC0000001, %%eax  \n"     \
00061              "cpuid                     \n"     \
00062              "unsupported:              \n"     \
00063              "movl  %%edx, %1           \n"     \
00064              "movl  %2, %%ebx           \n"
00065              : "=m" (ebx), "=m" (edx)
00066              :  "m" (ebx)
00067              : "eax", "ecx", "edx" );
00068 
00069         flags = edx;
00070     }
00071 
00072     return( flags & feature );
00073 }
00074 
00075 /*
00076  * PadLock AES-ECB block en(de)cryption
00077  */
00078 int padlock_xcryptecb( aes_context *ctx,
00079                        int mode,
00080                        const unsigned char input[16],
00081                        unsigned char output[16] )
00082 {
00083     int ebx = 0;
00084     uint32_t *rk;
00085     uint32_t *blk;
00086     uint32_t *ctrl;
00087     unsigned char buf[256];
00088 
00089     rk  = ctx->rk ;
00090     blk = PADLOCK_ALIGN16( buf );
00091     memcpy( blk, input, 16 );
00092 
00093      ctrl = blk + 4;
00094     *ctrl = 0x80 | ctx->nr  | ( ( ctx->nr  + ( mode^1 ) - 10 ) << 9 );
00095 
00096     asm( "pushfl; popfl         \n"     \
00097          "movl    %%ebx, %0     \n"     \
00098          "movl    $1, %%ecx     \n"     \
00099          "movl    %2, %%edx     \n"     \
00100          "movl    %3, %%ebx     \n"     \
00101          "movl    %4, %%esi     \n"     \
00102          "movl    %4, %%edi     \n"     \
00103          ".byte  0xf3,0x0f,0xa7,0xc8\n" \
00104          "movl    %1, %%ebx     \n"
00105          : "=m" (ebx)
00106          :  "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
00107          : "ecx", "edx", "esi", "edi" );
00108 
00109     memcpy( output, blk, 16 );
00110 
00111     return( 0 );
00112 }
00113 
00114 /*
00115  * PadLock AES-CBC buffer en(de)cryption
00116  */
00117 int padlock_xcryptcbc( aes_context *ctx,
00118                        int mode,
00119                        size_t length,
00120                        unsigned char iv[16],
00121                        const unsigned char *input,
00122                        unsigned char *output )
00123 {
00124     int ebx = 0;
00125     size_t count;
00126     uint32_t *rk;
00127     uint32_t *iw;
00128     uint32_t *ctrl;
00129     unsigned char buf[256];
00130 
00131     if( ( (long) input  & 15 ) != 0 ||
00132         ( (long) output & 15 ) != 0 )
00133         return( POLARSSL_ERR_PADLOCK_DATA_MISALIGNED );
00134 
00135     rk = ctx->rk ;
00136     iw = PADLOCK_ALIGN16( buf );
00137     memcpy( iw, iv, 16 );
00138 
00139      ctrl = iw + 4;
00140     *ctrl = 0x80 | ctx->nr  | ( ( ctx->nr  + (mode^1) - 10 ) << 9 );
00141 
00142     count = (length + 15) >> 4;
00143 
00144     asm( "pushfl; popfl         \n"     \
00145          "movl    %%ebx, %0     \n"     \
00146          "movl    %2, %%ecx     \n"     \
00147          "movl    %3, %%edx     \n"     \
00148          "movl    %4, %%ebx     \n"     \
00149          "movl    %5, %%esi     \n"     \
00150          "movl    %6, %%edi     \n"     \
00151          "movl    %7, %%eax     \n"     \
00152          ".byte  0xf3,0x0f,0xa7,0xd0\n" \
00153          "movl    %1, %%ebx     \n"
00154          : "=m" (ebx)
00155          :  "m" (ebx), "m" (count), "m" (ctrl),
00156             "m"  (rk), "m" (input), "m" (output), "m" (iw)
00157          : "eax", "ecx", "edx", "esi", "edi" );
00158 
00159     memcpy( iv, iw, 16 );
00160 
00161     return( 0 );
00162 }
00163 
00164 #endif /* POLARSSL_HAVE_X86 */
00165 
00166 #endif /* POLARSSL_PADLOCK_C */
00167 
00168