Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

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