blob: afb7e0ad42b5f1f28af282677a6a7fc3e2b46923 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * VIA PadLock support functions
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * 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é-Gonnard37ff1402015-09-04 14:21:07 +020012 *
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 Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
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 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
46/*
47 * This implementation is based on the VIA PadLock Programming Guide:
48 *
49 * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
50 * programming_guide.pdf
51 */
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020055#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020057#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_PADLOCK_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/padlock.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Manuel Pégourié-Gonnard45ec8da2015-02-10 13:50:47 +000063#include <string.h>
64
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020065#ifndef asm
66#define asm __asm
67#endif
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_HAVE_X86)
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Paul Bakker5121ce52009-01-03 21:22:43 +000071/*
72 * PadLock detection routine
73 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +010074int mbedtls_padlock_has_support( int feature )
Paul Bakker5121ce52009-01-03 21:22:43 +000075{
76 static int flags = -1;
Paul Bakker53e15132013-12-30 20:43:40 +010077 int ebx = 0, edx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +000078
79 if( flags == -1 )
80 {
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020081 asm( "movl %%ebx, %0 \n\t"
82 "movl $0xC0000000, %%eax \n\t"
83 "cpuid \n\t"
84 "cmpl $0xC0000001, %%eax \n\t"
85 "movl $0, %%edx \n\t"
86 "jb unsupported \n\t"
87 "movl $0xC0000001, %%eax \n\t"
88 "cpuid \n\t"
89 "unsupported: \n\t"
90 "movl %%edx, %1 \n\t"
91 "movl %2, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +000092 : "=m" (ebx), "=m" (edx)
93 : "m" (ebx)
94 : "eax", "ecx", "edx" );
95
96 flags = edx;
97 }
98
99 return( flags & feature );
100}
101
102/*
103 * PadLock AES-ECB block en(de)cryption
104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105int mbedtls_padlock_xcryptecb( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000107 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 unsigned char output[16] )
109{
Paul Bakker53e15132013-12-30 20:43:40 +0100110 int ebx = 0;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000111 uint32_t *rk;
112 uint32_t *blk;
113 uint32_t *ctrl;
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 unsigned char buf[256];
115
116 rk = ctx->rk;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 blk = MBEDTLS_PADLOCK_ALIGN16( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 memcpy( blk, input, 16 );
119
120 ctrl = blk + 4;
121 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 );
122
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +0200123 asm( "pushfl \n\t"
124 "popfl \n\t"
125 "movl %%ebx, %0 \n\t"
126 "movl $1, %%ecx \n\t"
127 "movl %2, %%edx \n\t"
128 "movl %3, %%ebx \n\t"
129 "movl %4, %%esi \n\t"
130 "movl %4, %%edi \n\t"
131 ".byte 0xf3,0x0f,0xa7,0xc8 \n\t"
132 "movl %1, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 : "=m" (ebx)
134 : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
Manuel Pégourié-Gonnardcf201202015-04-02 10:46:55 +0100135 : "memory", "ecx", "edx", "esi", "edi" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
137 memcpy( output, blk, 16 );
138
139 return( 0 );
140}
141
142/*
143 * PadLock AES-CBC buffer en(de)cryption
144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145int mbedtls_padlock_xcryptcbc( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000147 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000149 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 unsigned char *output )
151{
Paul Bakker53e15132013-12-30 20:43:40 +0100152 int ebx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000153 size_t count;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000154 uint32_t *rk;
155 uint32_t *iw;
156 uint32_t *ctrl;
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 unsigned char buf[256];
158
159 if( ( (long) input & 15 ) != 0 ||
160 ( (long) output & 15 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 return( MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
163 rk = ctx->rk;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 iw = MBEDTLS_PADLOCK_ALIGN16( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 memcpy( iw, iv, 16 );
166
167 ctrl = iw + 4;
Paul Bakker66d5d072014-06-17 16:39:18 +0200168 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode ^ 1 ) - 10 ) << 9 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
Paul Bakker66d5d072014-06-17 16:39:18 +0200170 count = ( length + 15 ) >> 4;
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +0200172 asm( "pushfl \n\t"
173 "popfl \n\t"
174 "movl %%ebx, %0 \n\t"
175 "movl %2, %%ecx \n\t"
176 "movl %3, %%edx \n\t"
177 "movl %4, %%ebx \n\t"
178 "movl %5, %%esi \n\t"
179 "movl %6, %%edi \n\t"
180 "movl %7, %%eax \n\t"
181 ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
182 "movl %1, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 : "=m" (ebx)
184 : "m" (ebx), "m" (count), "m" (ctrl),
185 "m" (rk), "m" (input), "m" (output), "m" (iw)
Manuel Pégourié-Gonnardcf201202015-04-02 10:46:55 +0100186 : "memory", "eax", "ecx", "edx", "esi", "edi" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
188 memcpy( iv, iw, 16 );
189
190 return( 0 );
191}
192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#endif /* MBEDTLS_HAVE_X86 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#endif /* MBEDTLS_PADLOCK_C */