blob: 887a386e8cbfb4f7bd997835463ef967341e8c85 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * VIA PadLock support functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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 Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * This implementation is based on the VIA PadLock Programming Guide:
23 *
24 * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
25 * programming_guide.pdf
26 */
27
Gilles Peskinedb09ef62020-06-03 01:43:33 +020028#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_PADLOCK_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/padlock.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Manuel Pégourié-Gonnard45ec8da2015-02-10 13:50:47 +000034#include <string.h>
35
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020036#ifndef asm
37#define asm __asm
38#endif
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_HAVE_X86)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker5121ce52009-01-03 21:22:43 +000042/*
43 * PadLock detection routine
44 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +010045int mbedtls_padlock_has_support( int feature )
Paul Bakker5121ce52009-01-03 21:22:43 +000046{
47 static int flags = -1;
Paul Bakker53e15132013-12-30 20:43:40 +010048 int ebx = 0, edx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +000049
50 if( flags == -1 )
51 {
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020052 asm( "movl %%ebx, %0 \n\t"
53 "movl $0xC0000000, %%eax \n\t"
54 "cpuid \n\t"
55 "cmpl $0xC0000001, %%eax \n\t"
56 "movl $0, %%edx \n\t"
57 "jb unsupported \n\t"
58 "movl $0xC0000001, %%eax \n\t"
59 "cpuid \n\t"
60 "unsupported: \n\t"
61 "movl %%edx, %1 \n\t"
62 "movl %2, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +000063 : "=m" (ebx), "=m" (edx)
64 : "m" (ebx)
65 : "eax", "ecx", "edx" );
66
67 flags = edx;
68 }
69
70 return( flags & feature );
71}
72
73/*
74 * PadLock AES-ECB block en(de)cryption
75 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076int mbedtls_padlock_xcryptecb( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +000077 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +000078 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +000079 unsigned char output[16] )
80{
Paul Bakker53e15132013-12-30 20:43:40 +010081 int ebx = 0;
Paul Bakker5c2364c2012-10-01 14:41:15 +000082 uint32_t *rk;
83 uint32_t *blk;
84 uint32_t *ctrl;
Paul Bakker5121ce52009-01-03 21:22:43 +000085 unsigned char buf[256];
86
87 rk = ctx->rk;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 blk = MBEDTLS_PADLOCK_ALIGN16( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000089 memcpy( blk, input, 16 );
90
91 ctrl = blk + 4;
92 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 );
93
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020094 asm( "pushfl \n\t"
95 "popfl \n\t"
96 "movl %%ebx, %0 \n\t"
97 "movl $1, %%ecx \n\t"
98 "movl %2, %%edx \n\t"
99 "movl %3, %%ebx \n\t"
100 "movl %4, %%esi \n\t"
101 "movl %4, %%edi \n\t"
102 ".byte 0xf3,0x0f,0xa7,0xc8 \n\t"
103 "movl %1, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +0000104 : "=m" (ebx)
105 : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
Manuel Pégourié-Gonnardcf201202015-04-02 10:46:55 +0100106 : "memory", "ecx", "edx", "esi", "edi" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108 memcpy( output, blk, 16 );
109
110 return( 0 );
111}
112
113/*
114 * PadLock AES-CBC buffer en(de)cryption
115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116int mbedtls_padlock_xcryptcbc( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000118 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000120 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 unsigned char *output )
122{
Paul Bakker53e15132013-12-30 20:43:40 +0100123 int ebx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000124 size_t count;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000125 uint32_t *rk;
126 uint32_t *iw;
127 uint32_t *ctrl;
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 unsigned char buf[256];
129
130 if( ( (long) input & 15 ) != 0 ||
131 ( (long) output & 15 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 return( MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
134 rk = ctx->rk;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 iw = MBEDTLS_PADLOCK_ALIGN16( buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 memcpy( iw, iv, 16 );
137
138 ctrl = iw + 4;
Paul Bakker66d5d072014-06-17 16:39:18 +0200139 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode ^ 1 ) - 10 ) << 9 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
Paul Bakker66d5d072014-06-17 16:39:18 +0200141 count = ( length + 15 ) >> 4;
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +0200143 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"
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 : "=m" (ebx)
155 : "m" (ebx), "m" (count), "m" (ctrl),
156 "m" (rk), "m" (input), "m" (output), "m" (iw)
Manuel Pégourié-Gonnardcf201202015-04-02 10:46:55 +0100157 : "memory", "eax", "ecx", "edx", "esi", "edi" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
159 memcpy( iv, iw, 16 );
160
161 return( 0 );
162}
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#endif /* MBEDTLS_HAVE_X86 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#endif /* MBEDTLS_PADLOCK_C */