| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  *  VIA PadLock support functions | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2020-09-05 12:53:20 +02:00
										 |  |  |  *  Copyright The Mbed TLS Contributors | 
					
						
							| 
									
										
										
										
											2024-01-30 14:09:13 +01:00
										 |  |  |  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  *  This implementation is based on the VIA PadLock Programming Guide: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/
 | 
					
						
							|  |  |  |  *  programming_guide.pdf | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 12:54:05 +01:00
										 |  |  | #include "common.h"
 | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_PADLOCK_C)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  | #include "padlock.h"
 | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-21 14:06:23 +02:00
										 |  |  | #if defined(MBEDTLS_VIA_PADLOCK_HAVE_CODE)
 | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * PadLock detection routine | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | int mbedtls_padlock_has_support(int feature) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     static int flags = -1; | 
					
						
							|  |  |  |     int ebx = 0, edx = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (flags == -1) { | 
					
						
							|  |  |  |         asm ("movl  %%ebx, %0           \n\t" | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |              "movl  $0xC0000000, %%eax  \n\t" | 
					
						
							|  |  |  |              "cpuid                     \n\t" | 
					
						
							|  |  |  |              "cmpl  $0xC0000001, %%eax  \n\t" | 
					
						
							|  |  |  |              "movl  $0, %%edx           \n\t" | 
					
						
							| 
									
										
										
										
											2021-12-21 12:54:05 +01:00
										 |  |  |              "jb    1f                  \n\t" | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |              "movl  $0xC0000001, %%eax  \n\t" | 
					
						
							|  |  |  |              "cpuid                     \n\t" | 
					
						
							| 
									
										
										
										
											2021-12-21 12:54:05 +01:00
										 |  |  |              "1:                        \n\t" | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |              "movl  %%edx, %1           \n\t" | 
					
						
							|  |  |  |              "movl  %2, %%ebx           \n\t" | 
					
						
							|  |  |  |              : "=m" (ebx), "=m" (edx) | 
					
						
							|  |  |  |              :  "m" (ebx) | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |              : "eax", "ecx", "edx"); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         flags = edx; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     return flags & feature; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * PadLock AES-ECB block en(de)cryption | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | int mbedtls_padlock_xcryptecb(mbedtls_aes_context *ctx, | 
					
						
							|  |  |  |                               int mode, | 
					
						
							|  |  |  |                               const unsigned char input[16], | 
					
						
							|  |  |  |                               unsigned char output[16]) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     int ebx = 0; | 
					
						
							|  |  |  |     uint32_t *rk; | 
					
						
							|  |  |  |     uint32_t *blk; | 
					
						
							|  |  |  |     uint32_t *ctrl; | 
					
						
							|  |  |  |     unsigned char buf[256]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |     rk = ctx->buf + ctx->rk_offset; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (((long) rk & 15) != 0) { | 
					
						
							|  |  |  |         return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     blk = MBEDTLS_PADLOCK_ALIGN16(buf); | 
					
						
							|  |  |  |     memcpy(blk, input, 16); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     ctrl = blk + 4; | 
					
						
							|  |  |  |     *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode^1) - 10) << 9); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     asm ("pushfl                        \n\t" | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |          "popfl                         \n\t" | 
					
						
							|  |  |  |          "movl    %%ebx, %0             \n\t" | 
					
						
							|  |  |  |          "movl    $1, %%ecx             \n\t" | 
					
						
							|  |  |  |          "movl    %2, %%edx             \n\t" | 
					
						
							|  |  |  |          "movl    %3, %%ebx             \n\t" | 
					
						
							|  |  |  |          "movl    %4, %%esi             \n\t" | 
					
						
							|  |  |  |          "movl    %4, %%edi             \n\t" | 
					
						
							|  |  |  |          ".byte  0xf3,0x0f,0xa7,0xc8    \n\t" | 
					
						
							|  |  |  |          "movl    %1, %%ebx             \n\t" | 
					
						
							|  |  |  |          : "=m" (ebx) | 
					
						
							|  |  |  |          :  "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk) | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |          : "memory", "ecx", "edx", "esi", "edi"); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     memcpy(output, blk, 16); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  | #if defined(MBEDTLS_CIPHER_MODE_CBC)
 | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * PadLock AES-CBC buffer en(de)cryption | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | int mbedtls_padlock_xcryptcbc(mbedtls_aes_context *ctx, | 
					
						
							|  |  |  |                               int mode, | 
					
						
							|  |  |  |                               size_t length, | 
					
						
							|  |  |  |                               unsigned char iv[16], | 
					
						
							|  |  |  |                               const unsigned char *input, | 
					
						
							|  |  |  |                               unsigned char *output) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     int ebx = 0; | 
					
						
							|  |  |  |     size_t count; | 
					
						
							|  |  |  |     uint32_t *rk; | 
					
						
							|  |  |  |     uint32_t *iw; | 
					
						
							|  |  |  |     uint32_t *ctrl; | 
					
						
							|  |  |  |     unsigned char buf[256]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |     rk = ctx->buf + ctx->rk_offset; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (((long) input  & 15) != 0 || | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |         ((long) output & 15) != 0 || | 
					
						
							|  |  |  |         ((long) rk & 15) != 0) { | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         return MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     iw = MBEDTLS_PADLOCK_ALIGN16(buf); | 
					
						
							|  |  |  |     memcpy(iw, iv, 16); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     ctrl = iw + 4; | 
					
						
							|  |  |  |     *ctrl = 0x80 | ctx->nr | ((ctx->nr + (mode ^ 1) - 10) << 9); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     count = (length + 15) >> 4; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     asm ("pushfl                        \n\t" | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |          "popfl                         \n\t" | 
					
						
							|  |  |  |          "movl    %%ebx, %0             \n\t" | 
					
						
							|  |  |  |          "movl    %2, %%ecx             \n\t" | 
					
						
							|  |  |  |          "movl    %3, %%edx             \n\t" | 
					
						
							|  |  |  |          "movl    %4, %%ebx             \n\t" | 
					
						
							|  |  |  |          "movl    %5, %%esi             \n\t" | 
					
						
							|  |  |  |          "movl    %6, %%edi             \n\t" | 
					
						
							|  |  |  |          "movl    %7, %%eax             \n\t" | 
					
						
							|  |  |  |          ".byte  0xf3,0x0f,0xa7,0xd0    \n\t" | 
					
						
							|  |  |  |          "movl    %1, %%ebx             \n\t" | 
					
						
							|  |  |  |          : "=m" (ebx) | 
					
						
							|  |  |  |          :  "m" (ebx), "m" (count), "m" (ctrl), | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |          "m"  (rk), "m" (input), "m" (output), "m" (iw) | 
					
						
							|  |  |  |          : "memory", "eax", "ecx", "edx", "esi", "edi"); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     memcpy(iv, iw, 16); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  | #endif /* MBEDTLS_CIPHER_MODE_CBC */
 | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-21 14:06:23 +02:00
										 |  |  | #endif /* MBEDTLS_VIA_PADLOCK_HAVE_CODE */
 | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #endif /* MBEDTLS_PADLOCK_C */
 |