00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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
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
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 uint32_t *rk;
00081 uint32_t *blk;
00082 uint32_t *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
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 uint32_t *rk;
00123 uint32_t *iw;
00124 uint32_t *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