quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

padlock.c (4313B)


      1 /*
      2  *  VIA PadLock support functions
      3  *
      4  *  Copyright The Mbed TLS Contributors
      5  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
      6  */
      7 /*
      8  *  This implementation is based on the VIA PadLock Programming Guide:
      9  *
     10  *  http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
     11  *  programming_guide.pdf
     12  */
     13 
     14 #include "common.h"
     15 
     16 #if defined(MBEDTLS_PADLOCK_C)
     17 
     18 #include "padlock.h"
     19 
     20 #include <string.h>
     21 
     22 #if defined(MBEDTLS_VIA_PADLOCK_HAVE_CODE)
     23 
     24 /*
     25  * PadLock detection routine
     26  */
     27 int mbedtls_padlock_has_support(int feature)
     28 {
     29     static int flags = -1;
     30     int ebx = 0, edx = 0;
     31 
     32     if (flags == -1) {
     33         asm ("movl  %%ebx, %0           \n\t"
     34              "movl  $0xC0000000, %%eax  \n\t"
     35              "cpuid                     \n\t"
     36              "cmpl  $0xC0000001, %%eax  \n\t"
     37              "movl  $0, %%edx           \n\t"
     38              "jb    1f                  \n\t"
     39              "movl  $0xC0000001, %%eax  \n\t"
     40              "cpuid                     \n\t"
     41              "1:                        \n\t"
     42              "movl  %%edx, %1           \n\t"
     43              "movl  %2, %%ebx           \n\t"
     44              : "=m" (ebx), "=m" (edx)
     45              :  "m" (ebx)
     46              : "eax", "ecx", "edx");
     47 
     48         flags = edx;
     49     }
     50 
     51     return flags & feature;
     52 }
     53 
     54 /*
     55  * PadLock AES-ECB block en(de)cryption
     56  */
     57 int mbedtls_padlock_xcryptecb(mbedtls_aes_context *ctx,
     58                               int mode,
     59                               const unsigned char input[16],
     60                               unsigned char output[16])
     61 {
     62     int ebx = 0;
     63     uint32_t *rk;
     64     uint32_t *blk;
     65     uint32_t *ctrl;
     66     unsigned char buf[256];
     67 
     68     rk = ctx->buf + ctx->rk_offset;
     69 
     70     if (((long) rk & 15) != 0) {
     71         return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
     72     }
     73 
     74     blk = MBEDTLS_PADLOCK_ALIGN16(buf);
     75     memcpy(blk, input, 16);
     76 
     77     ctrl = blk + 4;
     78     *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode^1) - 10) << 9);
     79 
     80     asm ("pushfl                        \n\t"
     81          "popfl                         \n\t"
     82          "movl    %%ebx, %0             \n\t"
     83          "movl    $1, %%ecx             \n\t"
     84          "movl    %2, %%edx             \n\t"
     85          "movl    %3, %%ebx             \n\t"
     86          "movl    %4, %%esi             \n\t"
     87          "movl    %4, %%edi             \n\t"
     88          ".byte  0xf3,0x0f,0xa7,0xc8    \n\t"
     89          "movl    %1, %%ebx             \n\t"
     90          : "=m" (ebx)
     91          :  "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
     92          : "memory", "ecx", "edx", "esi", "edi");
     93 
     94     memcpy(output, blk, 16);
     95 
     96     return 0;
     97 }
     98 
     99 #if defined(MBEDTLS_CIPHER_MODE_CBC)
    100 /*
    101  * PadLock AES-CBC buffer en(de)cryption
    102  */
    103 int mbedtls_padlock_xcryptcbc(mbedtls_aes_context *ctx,
    104                               int mode,
    105                               size_t length,
    106                               unsigned char iv[16],
    107                               const unsigned char *input,
    108                               unsigned char *output)
    109 {
    110     int ebx = 0;
    111     size_t count;
    112     uint32_t *rk;
    113     uint32_t *iw;
    114     uint32_t *ctrl;
    115     unsigned char buf[256];
    116 
    117     rk = ctx->buf + ctx->rk_offset;
    118 
    119     if (((long) input  & 15) != 0 ||
    120         ((long) output & 15) != 0 ||
    121         ((long) rk & 15) != 0) {
    122         return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED;
    123     }
    124 
    125     iw = MBEDTLS_PADLOCK_ALIGN16(buf);
    126     memcpy(iw, iv, 16);
    127 
    128     ctrl = iw + 4;
    129     *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode ^ 1) - 10) << 9);
    130 
    131     count = (length + 15) >> 4;
    132 
    133     asm ("pushfl                        \n\t"
    134          "popfl                         \n\t"
    135          "movl    %%ebx, %0             \n\t"
    136          "movl    %2, %%ecx             \n\t"
    137          "movl    %3, %%edx             \n\t"
    138          "movl    %4, %%ebx             \n\t"
    139          "movl    %5, %%esi             \n\t"
    140          "movl    %6, %%edi             \n\t"
    141          "movl    %7, %%eax             \n\t"
    142          ".byte  0xf3,0x0f,0xa7,0xd0    \n\t"
    143          "movl    %1, %%ebx             \n\t"
    144          : "=m" (ebx)
    145          :  "m" (ebx), "m" (count), "m" (ctrl),
    146          "m"  (rk), "m" (input), "m" (output), "m" (iw)
    147          : "memory", "eax", "ecx", "edx", "esi", "edi");
    148 
    149     memcpy(iv, iw, 16);
    150 
    151     return 0;
    152 }
    153 #endif /* MBEDTLS_CIPHER_MODE_CBC */
    154 
    155 #endif /* MBEDTLS_VIA_PADLOCK_HAVE_CODE */
    156 
    157 #endif /* MBEDTLS_PADLOCK_C */