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