| 1 | /* |
| 2 | * VIA PadLock support functions |
| 3 | * |
| 4 | * Copyright The Mbed TLS Contributors |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | /* |
| 20 | * This implementation is based on the VIA PadLock Programming Guide: |
| 21 | * |
| 22 | * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/ |
| 23 | * programming_guide.pdf |
| 24 | */ |
| 25 | |
| 26 | #include "common.h" |
| 27 | |
| 28 | #if defined(MBEDTLS_PADLOCK_C) |
| 29 | |
| 30 | #include "mbedtls/padlock.h" |
| 31 | |
| 32 | #include <string.h> |
| 33 | |
| 34 | /* *INDENT-OFF* */ |
| 35 | #ifndef asm |
| 36 | #define asm __asm |
| 37 | #endif |
| 38 | /* *INDENT-ON* */ |
| 39 | |
| 40 | #if defined(MBEDTLS_HAVE_X86) |
| 41 | |
| 42 | /* |
| 43 | * PadLock detection routine |
| 44 | */ |
| 45 | int mbedtls_padlock_has_support(int feature) |
| 46 | { |
| 47 | static int flags = -1; |
| 48 | int ebx = 0, edx = 0; |
| 49 | |
| 50 | if (flags == -1) { |
| 51 | asm ("movl %%ebx, %0 \n\t" |
| 52 | "movl $0xC0000000, %%eax \n\t" |
| 53 | "cpuid \n\t" |
| 54 | "cmpl $0xC0000001, %%eax \n\t" |
| 55 | "movl $0, %%edx \n\t" |
| 56 | "jb 1f \n\t" |
| 57 | "movl $0xC0000001, %%eax \n\t" |
| 58 | "cpuid \n\t" |
| 59 | "1: \n\t" |
| 60 | "movl %%edx, %1 \n\t" |
| 61 | "movl %2, %%ebx \n\t" |
| 62 | : "=m" (ebx), "=m" (edx) |
| 63 | : "m" (ebx) |
| 64 | : "eax" , "ecx" , "edx" ); |
| 65 | |
| 66 | flags = edx; |
| 67 | } |
| 68 | |
| 69 | return flags & feature; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * PadLock AES-ECB block en(de)cryption |
| 74 | */ |
| 75 | int mbedtls_padlock_xcryptecb(mbedtls_aes_context *ctx, |
| 76 | int mode, |
| 77 | const unsigned char input[16], |
| 78 | unsigned char output[16]) |
| 79 | { |
| 80 | int ebx = 0; |
| 81 | uint32_t *rk; |
| 82 | uint32_t *blk; |
| 83 | uint32_t *ctrl; |
| 84 | unsigned char buf[256]; |
| 85 | |
| 86 | rk = ctx->rk; |
| 87 | blk = MBEDTLS_PADLOCK_ALIGN16(buf); |
| 88 | memcpy(blk, input, 16); |
| 89 | |
| 90 | ctrl = blk + 4; |
| 91 | *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode^1) - 10) << 9); |
| 92 | |
| 93 | asm ("pushfl \n\t" |
| 94 | "popfl \n\t" |
| 95 | "movl %%ebx, %0 \n\t" |
| 96 | "movl $1, %%ecx \n\t" |
| 97 | "movl %2, %%edx \n\t" |
| 98 | "movl %3, %%ebx \n\t" |
| 99 | "movl %4, %%esi \n\t" |
| 100 | "movl %4, %%edi \n\t" |
| 101 | ".byte 0xf3,0x0f,0xa7,0xc8 \n\t" |
| 102 | "movl %1, %%ebx \n\t" |
| 103 | : "=m" (ebx) |
| 104 | : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk) |
| 105 | : "memory" , "ecx" , "edx" , "esi" , "edi" ); |
| 106 | |
| 107 | memcpy(output, blk, 16); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * PadLock AES-CBC buffer en(de)cryption |
| 114 | */ |
| 115 | int mbedtls_padlock_xcryptcbc(mbedtls_aes_context *ctx, |
| 116 | int mode, |
| 117 | size_t length, |
| 118 | unsigned char iv[16], |
| 119 | const unsigned char *input, |
| 120 | unsigned char *output) |
| 121 | { |
| 122 | int ebx = 0; |
| 123 | size_t count; |
| 124 | uint32_t *rk; |
| 125 | uint32_t *iw; |
| 126 | uint32_t *ctrl; |
| 127 | unsigned char buf[256]; |
| 128 | |
| 129 | if (((long) input & 15) != 0 || |
| 130 | ((long) output & 15) != 0) { |
| 131 | return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED; |
| 132 | } |
| 133 | |
| 134 | rk = ctx->rk; |
| 135 | iw = MBEDTLS_PADLOCK_ALIGN16(buf); |
| 136 | memcpy(iw, iv, 16); |
| 137 | |
| 138 | ctrl = iw + 4; |
| 139 | *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode ^ 1) - 10) << 9); |
| 140 | |
| 141 | count = (length + 15) >> 4; |
| 142 | |
| 143 | asm ("pushfl \n\t" |
| 144 | "popfl \n\t" |
| 145 | "movl %%ebx, %0 \n\t" |
| 146 | "movl %2, %%ecx \n\t" |
| 147 | "movl %3, %%edx \n\t" |
| 148 | "movl %4, %%ebx \n\t" |
| 149 | "movl %5, %%esi \n\t" |
| 150 | "movl %6, %%edi \n\t" |
| 151 | "movl %7, %%eax \n\t" |
| 152 | ".byte 0xf3,0x0f,0xa7,0xd0 \n\t" |
| 153 | "movl %1, %%ebx \n\t" |
| 154 | : "=m" (ebx) |
| 155 | : "m" (ebx), "m" (count), "m" (ctrl), |
| 156 | "m" (rk), "m" (input), "m" (output), "m" (iw) |
| 157 | : "memory" , "eax" , "ecx" , "edx" , "esi" , "edi" ); |
| 158 | |
| 159 | memcpy(iv, iw, 16); |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | #endif /* MBEDTLS_HAVE_X86 */ |
| 165 | |
| 166 | #endif /* MBEDTLS_PADLOCK_C */ |
| 167 | |