blob: ad511b42bf06b2de410044063939535f18b91817 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_PADLOCK_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/padlock.h"
Teppo Järvelin3137fb22019-10-04 13:29:55 +030037#include "mbedtls/platform_util.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Manuel Pégourié-Gonnard45ec8da2015-02-10 13:50:47 +000039#include <string.h>
40
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020041#ifndef asm
42#define asm __asm
43#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_HAVE_X86)
Paul Bakker5121ce52009-01-03 21:22:43 +000046
Paul Bakker5121ce52009-01-03 21:22:43 +000047/*
48 * PadLock detection routine
49 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +010050int mbedtls_padlock_has_support( int feature )
Paul Bakker5121ce52009-01-03 21:22:43 +000051{
52 static int flags = -1;
Paul Bakker53e15132013-12-30 20:43:40 +010053 int ebx = 0, edx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +000054
55 if( flags == -1 )
56 {
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020057 asm( "movl %%ebx, %0 \n\t"
58 "movl $0xC0000000, %%eax \n\t"
59 "cpuid \n\t"
60 "cmpl $0xC0000001, %%eax \n\t"
61 "movl $0, %%edx \n\t"
62 "jb unsupported \n\t"
63 "movl $0xC0000001, %%eax \n\t"
64 "cpuid \n\t"
65 "unsupported: \n\t"
66 "movl %%edx, %1 \n\t"
67 "movl %2, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +000068 : "=m" (ebx), "=m" (edx)
69 : "m" (ebx)
70 : "eax", "ecx", "edx" );
71
72 flags = edx;
73 }
74
75 return( flags & feature );
76}
77
78/*
79 * PadLock AES-ECB block en(de)cryption
80 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081int mbedtls_padlock_xcryptecb( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +000082 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +000083 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +000084 unsigned char output[16] )
85{
Paul Bakker53e15132013-12-30 20:43:40 +010086 int ebx = 0;
Paul Bakker5c2364c2012-10-01 14:41:15 +000087 uint32_t *rk;
88 uint32_t *blk;
89 uint32_t *ctrl;
Paul Bakker5121ce52009-01-03 21:22:43 +000090 unsigned char buf[256];
91
92 rk = ctx->rk;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 blk = MBEDTLS_PADLOCK_ALIGN16( buf );
Teppo Järvelin91d79382019-10-02 09:09:31 +030094 mbedtls_platform_memcpy( blk, input, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96 ctrl = blk + 4;
97 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 );
98
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +020099 asm( "pushfl \n\t"
100 "popfl \n\t"
101 "movl %%ebx, %0 \n\t"
102 "movl $1, %%ecx \n\t"
103 "movl %2, %%edx \n\t"
104 "movl %3, %%ebx \n\t"
105 "movl %4, %%esi \n\t"
106 "movl %4, %%edi \n\t"
107 ".byte 0xf3,0x0f,0xa7,0xc8 \n\t"
108 "movl %1, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 : "=m" (ebx)
110 : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
Manuel Pégourié-Gonnardcf201202015-04-02 10:46:55 +0100111 : "memory", "ecx", "edx", "esi", "edi" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
Teppo Järvelin91d79382019-10-02 09:09:31 +0300113 mbedtls_platform_memcpy( output, blk, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
115 return( 0 );
116}
117
118/*
119 * PadLock AES-CBC buffer en(de)cryption
120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121int mbedtls_padlock_xcryptcbc( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000123 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000125 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 unsigned char *output )
127{
Paul Bakker53e15132013-12-30 20:43:40 +0100128 int ebx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000129 size_t count;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000130 uint32_t *rk;
131 uint32_t *iw;
132 uint32_t *ctrl;
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 unsigned char buf[256];
134
135 if( ( (long) input & 15 ) != 0 ||
136 ( (long) output & 15 ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 return( MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
139 rk = ctx->rk;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 iw = MBEDTLS_PADLOCK_ALIGN16( buf );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300141 mbedtls_platform_memcpy( iw, iv, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
143 ctrl = iw + 4;
Paul Bakker66d5d072014-06-17 16:39:18 +0200144 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode ^ 1 ) - 10 ) << 9 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
Paul Bakker66d5d072014-06-17 16:39:18 +0200146 count = ( length + 15 ) >> 4;
Paul Bakker5121ce52009-01-03 21:22:43 +0000147
Manuel Pégourié-Gonnard87a8ffe2014-06-23 12:40:01 +0200148 asm( "pushfl \n\t"
149 "popfl \n\t"
150 "movl %%ebx, %0 \n\t"
151 "movl %2, %%ecx \n\t"
152 "movl %3, %%edx \n\t"
153 "movl %4, %%ebx \n\t"
154 "movl %5, %%esi \n\t"
155 "movl %6, %%edi \n\t"
156 "movl %7, %%eax \n\t"
157 ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
158 "movl %1, %%ebx \n\t"
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 : "=m" (ebx)
160 : "m" (ebx), "m" (count), "m" (ctrl),
161 "m" (rk), "m" (input), "m" (output), "m" (iw)
Manuel Pégourié-Gonnardcf201202015-04-02 10:46:55 +0100162 : "memory", "eax", "ecx", "edx", "esi", "edi" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
Teppo Järvelin91d79382019-10-02 09:09:31 +0300164 mbedtls_platform_memcpy( iv, iw, 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
166 return( 0 );
167}
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#endif /* MBEDTLS_HAVE_X86 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#endif /* MBEDTLS_PADLOCK_C */