Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * VIA PadLock support functions |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 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. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 26 | #include "common.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PADLOCK_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 30 | # include "padlock.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 31 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 32 | # include <string.h> |
Manuel Pégourié-Gonnard | 45ec8da | 2015-02-10 13:50:47 +0000 | [diff] [blame] | 33 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 34 | # ifndef asm |
| 35 | # define asm __asm |
| 36 | # endif |
Manuel Pégourié-Gonnard | ba19432 | 2015-05-29 09:47:57 +0200 | [diff] [blame] | 37 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 38 | # if defined(MBEDTLS_HAVE_X86) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 39 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 40 | /* |
| 41 | * PadLock detection routine |
| 42 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 43 | int mbedtls_padlock_has_support(int feature) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 44 | { |
| 45 | static int flags = -1; |
Paul Bakker | 53e1513 | 2013-12-30 20:43:40 +0100 | [diff] [blame] | 46 | int ebx = 0, edx = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 47 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 48 | if (flags == -1) { |
| 49 | asm("movl %%ebx, %0 \n\t" |
| 50 | "movl $0xC0000000, %%eax \n\t" |
| 51 | "cpuid \n\t" |
| 52 | "cmpl $0xC0000001, %%eax \n\t" |
| 53 | "movl $0, %%edx \n\t" |
| 54 | "jb 1f \n\t" |
| 55 | "movl $0xC0000001, %%eax \n\t" |
| 56 | "cpuid \n\t" |
| 57 | "1: \n\t" |
| 58 | "movl %%edx, %1 \n\t" |
| 59 | "movl %2, %%ebx \n\t" |
| 60 | : "=m"(ebx), "=m"(edx) |
| 61 | : "m"(ebx) |
| 62 | : "eax", "ecx", "edx"); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 63 | |
| 64 | flags = edx; |
| 65 | } |
| 66 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 67 | return flags & feature; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /* |
| 71 | * PadLock AES-ECB block en(de)cryption |
| 72 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 73 | int mbedtls_padlock_xcryptecb(mbedtls_aes_context *ctx, |
| 74 | int mode, |
| 75 | const unsigned char input[16], |
| 76 | unsigned char output[16]) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 77 | { |
Paul Bakker | 53e1513 | 2013-12-30 20:43:40 +0100 | [diff] [blame] | 78 | int ebx = 0; |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 79 | uint32_t *rk; |
| 80 | uint32_t *blk; |
| 81 | uint32_t *ctrl; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 82 | unsigned char buf[256]; |
| 83 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 84 | rk = ctx->rk; |
| 85 | blk = MBEDTLS_PADLOCK_ALIGN16(buf); |
| 86 | memcpy(blk, input, 16); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 87 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 88 | ctrl = blk + 4; |
| 89 | *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode ^ 1) - 10) << 9); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 90 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 91 | asm("pushfl \n\t" |
| 92 | "popfl \n\t" |
| 93 | "movl %%ebx, %0 \n\t" |
| 94 | "movl $1, %%ecx \n\t" |
| 95 | "movl %2, %%edx \n\t" |
| 96 | "movl %3, %%ebx \n\t" |
| 97 | "movl %4, %%esi \n\t" |
| 98 | "movl %4, %%edi \n\t" |
| 99 | ".byte 0xf3,0x0f,0xa7,0xc8 \n\t" |
| 100 | "movl %1, %%ebx \n\t" |
| 101 | : "=m"(ebx) |
| 102 | : "m"(ebx), "m"(ctrl), "m"(rk), "m"(blk) |
| 103 | : "memory", "ecx", "edx", "esi", "edi"); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 104 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 105 | memcpy(output, blk, 16); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 106 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 107 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* |
| 111 | * PadLock AES-CBC buffer en(de)cryption |
| 112 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 113 | int mbedtls_padlock_xcryptcbc(mbedtls_aes_context *ctx, |
| 114 | int mode, |
| 115 | size_t length, |
| 116 | unsigned char iv[16], |
| 117 | const unsigned char *input, |
| 118 | unsigned char *output) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 119 | { |
Paul Bakker | 53e1513 | 2013-12-30 20:43:40 +0100 | [diff] [blame] | 120 | int ebx = 0; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 121 | size_t count; |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 122 | uint32_t *rk; |
| 123 | uint32_t *iw; |
| 124 | uint32_t *ctrl; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 125 | unsigned char buf[256]; |
| 126 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 127 | if (((long)input & 15) != 0 || ((long)output & 15) != 0) |
| 128 | return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 129 | |
| 130 | rk = ctx->rk; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 131 | iw = MBEDTLS_PADLOCK_ALIGN16(buf); |
| 132 | memcpy(iw, iv, 16); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 133 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 134 | ctrl = iw + 4; |
| 135 | *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode ^ 1) - 10) << 9); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 136 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 137 | count = (length + 15) >> 4; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 138 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 139 | asm("pushfl \n\t" |
| 140 | "popfl \n\t" |
| 141 | "movl %%ebx, %0 \n\t" |
| 142 | "movl %2, %%ecx \n\t" |
| 143 | "movl %3, %%edx \n\t" |
| 144 | "movl %4, %%ebx \n\t" |
| 145 | "movl %5, %%esi \n\t" |
| 146 | "movl %6, %%edi \n\t" |
| 147 | "movl %7, %%eax \n\t" |
| 148 | ".byte 0xf3,0x0f,0xa7,0xd0 \n\t" |
| 149 | "movl %1, %%ebx \n\t" |
| 150 | : "=m"(ebx) |
| 151 | : "m"(ebx), "m"(count), "m"(ctrl), "m"(rk), "m"(input), "m"(output), |
| 152 | "m"(iw) |
| 153 | : "memory", "eax", "ecx", "edx", "esi", "edi"); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 155 | memcpy(iv, iw, 16); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 156 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 157 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 160 | # endif /* MBEDTLS_HAVE_X86 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 161 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 162 | #endif /* MBEDTLS_PADLOCK_C */ |