2 * VIA PadLock support functions
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 * This implementation is based on the VIA PadLock Programming Guide:
24 * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
25 * programming_guide.pdf
28 #if !defined(MBEDTLS_CONFIG_FILE)
29 #include "mbedtls/config.h"
31 #include MBEDTLS_CONFIG_FILE
34 #if defined(MBEDTLS_PADLOCK_C)
36 #include "mbedtls/padlock.h"
44 #if defined(MBEDTLS_HAVE_X86)
47 * PadLock detection routine
49 int mbedtls_padlock_has_support( int feature )
51 static int flags = -1;
56 asm( "movl %%ebx, %0 \n\t"
57 "movl $0xC0000000, %%eax \n\t"
59 "cmpl $0xC0000001, %%eax \n\t"
62 "movl $0xC0000001, %%eax \n\t"
67 : "=m" (ebx), "=m" (edx)
69 : "eax", "ecx", "edx" );
74 return( flags & feature );
78 * PadLock AES-ECB block en(de)cryption
80 int mbedtls_padlock_xcryptecb( mbedtls_aes_context *ctx,
82 const unsigned char input[16],
83 unsigned char output[16] )
89 unsigned char buf[256];
92 blk = MBEDTLS_PADLOCK_ALIGN16( buf );
93 memcpy( blk, input, 16 );
96 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 );
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"
109 : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk)
110 : "memory", "ecx", "edx", "esi", "edi" );
112 memcpy( output, blk, 16 );
118 * PadLock AES-CBC buffer en(de)cryption
120 int mbedtls_padlock_xcryptcbc( mbedtls_aes_context *ctx,
123 unsigned char iv[16],
124 const unsigned char *input,
125 unsigned char *output )
132 unsigned char buf[256];
134 if( ( (long) input & 15 ) != 0 ||
135 ( (long) output & 15 ) != 0 )
136 return( MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED );
139 iw = MBEDTLS_PADLOCK_ALIGN16( buf );
140 memcpy( iw, iv, 16 );
143 *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode ^ 1 ) - 10 ) << 9 );
145 count = ( length + 15 ) >> 4;
149 "movl %%ebx, %0 \n\t"
150 "movl %2, %%ecx \n\t"
151 "movl %3, %%edx \n\t"
152 "movl %4, %%ebx \n\t"
153 "movl %5, %%esi \n\t"
154 "movl %6, %%edi \n\t"
155 "movl %7, %%eax \n\t"
156 ".byte 0xf3,0x0f,0xa7,0xd0 \n\t"
157 "movl %1, %%ebx \n\t"
159 : "m" (ebx), "m" (count), "m" (ctrl),
160 "m" (rk), "m" (input), "m" (output), "m" (iw)
161 : "memory", "eax", "ecx", "edx", "esi", "edi" );
163 memcpy( iv, iw, 16 );
168 #endif /* MBEDTLS_HAVE_X86 */
170 #endif /* MBEDTLS_PADLOCK_C */