Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * VIA PadLock support functions |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | * |
| 7 | * This file is provided under the Apache License 2.0, or the |
| 8 | * GNU General Public License v2.0 or later. |
| 9 | * |
| 10 | * ********** |
| 11 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 24 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 25 | * ********** |
| 26 | * |
| 27 | * ********** |
| 28 | * GNU General Public License v2.0 or later: |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or modify |
| 31 | * it under the terms of the GNU General Public License as published by |
| 32 | * the Free Software Foundation; either version 2 of the License, or |
| 33 | * (at your option) any later version. |
| 34 | * |
| 35 | * This program is distributed in the hope that it will be useful, |
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | * GNU General Public License for more details. |
| 39 | * |
| 40 | * You should have received a copy of the GNU General Public License along |
| 41 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 42 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 43 | * |
| 44 | * ********** |
| 45 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 46 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 47 | */ |
| 48 | /* |
| 49 | * This implementation is based on the VIA PadLock Programming Guide: |
| 50 | * |
| 51 | * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/ |
| 52 | * programming_guide.pdf |
| 53 | */ |
| 54 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 55 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 56 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 57 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 58 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 59 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 60 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | #if defined(MBEDTLS_PADLOCK_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 62 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 63 | #include "mbedtls/padlock.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 64 | |
Manuel Pégourié-Gonnard | 45ec8da | 2015-02-10 13:50:47 +0000 | [diff] [blame] | 65 | #include <string.h> |
| 66 | |
Manuel Pégourié-Gonnard | ba19432 | 2015-05-29 09:47:57 +0200 | [diff] [blame] | 67 | #ifndef asm |
| 68 | #define asm __asm |
| 69 | #endif |
| 70 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 71 | #if defined(MBEDTLS_HAVE_X86) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 72 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 73 | /* |
| 74 | * PadLock detection routine |
| 75 | */ |
Manuel Pégourié-Gonnard | c730ed3 | 2015-06-02 10:38:50 +0100 | [diff] [blame] | 76 | int mbedtls_padlock_has_support( int feature ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 77 | { |
| 78 | static int flags = -1; |
Paul Bakker | 53e1513 | 2013-12-30 20:43:40 +0100 | [diff] [blame] | 79 | int ebx = 0, edx = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 80 | |
| 81 | if( flags == -1 ) |
| 82 | { |
Manuel Pégourié-Gonnard | 87a8ffe | 2014-06-23 12:40:01 +0200 | [diff] [blame] | 83 | asm( "movl %%ebx, %0 \n\t" |
| 84 | "movl $0xC0000000, %%eax \n\t" |
| 85 | "cpuid \n\t" |
| 86 | "cmpl $0xC0000001, %%eax \n\t" |
| 87 | "movl $0, %%edx \n\t" |
| 88 | "jb unsupported \n\t" |
| 89 | "movl $0xC0000001, %%eax \n\t" |
| 90 | "cpuid \n\t" |
| 91 | "unsupported: \n\t" |
| 92 | "movl %%edx, %1 \n\t" |
| 93 | "movl %2, %%ebx \n\t" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 94 | : "=m" (ebx), "=m" (edx) |
| 95 | : "m" (ebx) |
| 96 | : "eax", "ecx", "edx" ); |
| 97 | |
| 98 | flags = edx; |
| 99 | } |
| 100 | |
| 101 | return( flags & feature ); |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * PadLock AES-ECB block en(de)cryption |
| 106 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 107 | int mbedtls_padlock_xcryptecb( mbedtls_aes_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 108 | int mode, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 109 | const unsigned char input[16], |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 110 | unsigned char output[16] ) |
| 111 | { |
Paul Bakker | 53e1513 | 2013-12-30 20:43:40 +0100 | [diff] [blame] | 112 | int ebx = 0; |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 113 | uint32_t *rk; |
| 114 | uint32_t *blk; |
| 115 | uint32_t *ctrl; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 116 | unsigned char buf[256]; |
| 117 | |
| 118 | rk = ctx->rk; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 119 | blk = MBEDTLS_PADLOCK_ALIGN16( buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 120 | memcpy( blk, input, 16 ); |
| 121 | |
| 122 | ctrl = blk + 4; |
| 123 | *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 ); |
| 124 | |
Manuel Pégourié-Gonnard | 87a8ffe | 2014-06-23 12:40:01 +0200 | [diff] [blame] | 125 | asm( "pushfl \n\t" |
| 126 | "popfl \n\t" |
| 127 | "movl %%ebx, %0 \n\t" |
| 128 | "movl $1, %%ecx \n\t" |
| 129 | "movl %2, %%edx \n\t" |
| 130 | "movl %3, %%ebx \n\t" |
| 131 | "movl %4, %%esi \n\t" |
| 132 | "movl %4, %%edi \n\t" |
| 133 | ".byte 0xf3,0x0f,0xa7,0xc8 \n\t" |
| 134 | "movl %1, %%ebx \n\t" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 135 | : "=m" (ebx) |
| 136 | : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk) |
Manuel Pégourié-Gonnard | cf20120 | 2015-04-02 10:46:55 +0100 | [diff] [blame] | 137 | : "memory", "ecx", "edx", "esi", "edi" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 138 | |
| 139 | memcpy( output, blk, 16 ); |
| 140 | |
| 141 | return( 0 ); |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * PadLock AES-CBC buffer en(de)cryption |
| 146 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | int mbedtls_padlock_xcryptcbc( mbedtls_aes_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 148 | int mode, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 149 | size_t length, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 150 | unsigned char iv[16], |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 151 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 152 | unsigned char *output ) |
| 153 | { |
Paul Bakker | 53e1513 | 2013-12-30 20:43:40 +0100 | [diff] [blame] | 154 | int ebx = 0; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 155 | size_t count; |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 156 | uint32_t *rk; |
| 157 | uint32_t *iw; |
| 158 | uint32_t *ctrl; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 159 | unsigned char buf[256]; |
| 160 | |
| 161 | if( ( (long) input & 15 ) != 0 || |
| 162 | ( (long) output & 15 ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | return( MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 164 | |
| 165 | rk = ctx->rk; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 166 | iw = MBEDTLS_PADLOCK_ALIGN16( buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 167 | memcpy( iw, iv, 16 ); |
| 168 | |
| 169 | ctrl = iw + 4; |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 170 | *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode ^ 1 ) - 10 ) << 9 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 171 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 172 | count = ( length + 15 ) >> 4; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 173 | |
Manuel Pégourié-Gonnard | 87a8ffe | 2014-06-23 12:40:01 +0200 | [diff] [blame] | 174 | asm( "pushfl \n\t" |
| 175 | "popfl \n\t" |
| 176 | "movl %%ebx, %0 \n\t" |
| 177 | "movl %2, %%ecx \n\t" |
| 178 | "movl %3, %%edx \n\t" |
| 179 | "movl %4, %%ebx \n\t" |
| 180 | "movl %5, %%esi \n\t" |
| 181 | "movl %6, %%edi \n\t" |
| 182 | "movl %7, %%eax \n\t" |
| 183 | ".byte 0xf3,0x0f,0xa7,0xd0 \n\t" |
| 184 | "movl %1, %%ebx \n\t" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 185 | : "=m" (ebx) |
| 186 | : "m" (ebx), "m" (count), "m" (ctrl), |
| 187 | "m" (rk), "m" (input), "m" (output), "m" (iw) |
Manuel Pégourié-Gonnard | cf20120 | 2015-04-02 10:46:55 +0100 | [diff] [blame] | 188 | : "memory", "eax", "ecx", "edx", "esi", "edi" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 189 | |
| 190 | memcpy( iv, iw, 16 ); |
| 191 | |
| 192 | return( 0 ); |
| 193 | } |
| 194 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 195 | #endif /* MBEDTLS_HAVE_X86 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 196 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 197 | #endif /* MBEDTLS_PADLOCK_C */ |