Pinned to some recent date
features/mbedtls/src/padlock.c@0:fb7af294d5d9, 2016-11-17 (annotated)
- Committer:
- Simon Cooksey
- Date:
- Thu Nov 17 16:43:53 2016 +0000
- Revision:
- 0:fb7af294d5d9
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Simon Cooksey |
0:fb7af294d5d9 | 1 | /* |
Simon Cooksey |
0:fb7af294d5d9 | 2 | * VIA PadLock support functions |
Simon Cooksey |
0:fb7af294d5d9 | 3 | * |
Simon Cooksey |
0:fb7af294d5d9 | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Simon Cooksey |
0:fb7af294d5d9 | 5 | * SPDX-License-Identifier: Apache-2.0 |
Simon Cooksey |
0:fb7af294d5d9 | 6 | * |
Simon Cooksey |
0:fb7af294d5d9 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
Simon Cooksey |
0:fb7af294d5d9 | 8 | * not use this file except in compliance with the License. |
Simon Cooksey |
0:fb7af294d5d9 | 9 | * You may obtain a copy of the License at |
Simon Cooksey |
0:fb7af294d5d9 | 10 | * |
Simon Cooksey |
0:fb7af294d5d9 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
Simon Cooksey |
0:fb7af294d5d9 | 12 | * |
Simon Cooksey |
0:fb7af294d5d9 | 13 | * Unless required by applicable law or agreed to in writing, software |
Simon Cooksey |
0:fb7af294d5d9 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
Simon Cooksey |
0:fb7af294d5d9 | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Simon Cooksey |
0:fb7af294d5d9 | 16 | * See the License for the specific language governing permissions and |
Simon Cooksey |
0:fb7af294d5d9 | 17 | * limitations under the License. |
Simon Cooksey |
0:fb7af294d5d9 | 18 | * |
Simon Cooksey |
0:fb7af294d5d9 | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Simon Cooksey |
0:fb7af294d5d9 | 20 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 21 | /* |
Simon Cooksey |
0:fb7af294d5d9 | 22 | * This implementation is based on the VIA PadLock Programming Guide: |
Simon Cooksey |
0:fb7af294d5d9 | 23 | * |
Simon Cooksey |
0:fb7af294d5d9 | 24 | * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/ |
Simon Cooksey |
0:fb7af294d5d9 | 25 | * programming_guide.pdf |
Simon Cooksey |
0:fb7af294d5d9 | 26 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 27 | |
Simon Cooksey |
0:fb7af294d5d9 | 28 | #if !defined(MBEDTLS_CONFIG_FILE) |
Simon Cooksey |
0:fb7af294d5d9 | 29 | #include "mbedtls/config.h" |
Simon Cooksey |
0:fb7af294d5d9 | 30 | #else |
Simon Cooksey |
0:fb7af294d5d9 | 31 | #include MBEDTLS_CONFIG_FILE |
Simon Cooksey |
0:fb7af294d5d9 | 32 | #endif |
Simon Cooksey |
0:fb7af294d5d9 | 33 | |
Simon Cooksey |
0:fb7af294d5d9 | 34 | #if defined(MBEDTLS_PADLOCK_C) |
Simon Cooksey |
0:fb7af294d5d9 | 35 | |
Simon Cooksey |
0:fb7af294d5d9 | 36 | #include "mbedtls/padlock.h" |
Simon Cooksey |
0:fb7af294d5d9 | 37 | |
Simon Cooksey |
0:fb7af294d5d9 | 38 | #include <string.h> |
Simon Cooksey |
0:fb7af294d5d9 | 39 | |
Simon Cooksey |
0:fb7af294d5d9 | 40 | #ifndef asm |
Simon Cooksey |
0:fb7af294d5d9 | 41 | #define asm __asm |
Simon Cooksey |
0:fb7af294d5d9 | 42 | #endif |
Simon Cooksey |
0:fb7af294d5d9 | 43 | |
Simon Cooksey |
0:fb7af294d5d9 | 44 | #if defined(MBEDTLS_HAVE_X86) |
Simon Cooksey |
0:fb7af294d5d9 | 45 | |
Simon Cooksey |
0:fb7af294d5d9 | 46 | /* |
Simon Cooksey |
0:fb7af294d5d9 | 47 | * PadLock detection routine |
Simon Cooksey |
0:fb7af294d5d9 | 48 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 49 | int mbedtls_padlock_has_support( int feature ) |
Simon Cooksey |
0:fb7af294d5d9 | 50 | { |
Simon Cooksey |
0:fb7af294d5d9 | 51 | static int flags = -1; |
Simon Cooksey |
0:fb7af294d5d9 | 52 | int ebx = 0, edx = 0; |
Simon Cooksey |
0:fb7af294d5d9 | 53 | |
Simon Cooksey |
0:fb7af294d5d9 | 54 | if( flags == -1 ) |
Simon Cooksey |
0:fb7af294d5d9 | 55 | { |
Simon Cooksey |
0:fb7af294d5d9 | 56 | asm( "movl %%ebx, %0 \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 57 | "movl $0xC0000000, %%eax \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 58 | "cpuid \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 59 | "cmpl $0xC0000001, %%eax \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 60 | "movl $0, %%edx \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 61 | "jb unsupported \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 62 | "movl $0xC0000001, %%eax \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 63 | "cpuid \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 64 | "unsupported: \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 65 | "movl %%edx, %1 \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 66 | "movl %2, %%ebx \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 67 | : "=m" (ebx), "=m" (edx) |
Simon Cooksey |
0:fb7af294d5d9 | 68 | : "m" (ebx) |
Simon Cooksey |
0:fb7af294d5d9 | 69 | : "eax", "ecx", "edx" ); |
Simon Cooksey |
0:fb7af294d5d9 | 70 | |
Simon Cooksey |
0:fb7af294d5d9 | 71 | flags = edx; |
Simon Cooksey |
0:fb7af294d5d9 | 72 | } |
Simon Cooksey |
0:fb7af294d5d9 | 73 | |
Simon Cooksey |
0:fb7af294d5d9 | 74 | return( flags & feature ); |
Simon Cooksey |
0:fb7af294d5d9 | 75 | } |
Simon Cooksey |
0:fb7af294d5d9 | 76 | |
Simon Cooksey |
0:fb7af294d5d9 | 77 | /* |
Simon Cooksey |
0:fb7af294d5d9 | 78 | * PadLock AES-ECB block en(de)cryption |
Simon Cooksey |
0:fb7af294d5d9 | 79 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 80 | int mbedtls_padlock_xcryptecb( mbedtls_aes_context *ctx, |
Simon Cooksey |
0:fb7af294d5d9 | 81 | int mode, |
Simon Cooksey |
0:fb7af294d5d9 | 82 | const unsigned char input[16], |
Simon Cooksey |
0:fb7af294d5d9 | 83 | unsigned char output[16] ) |
Simon Cooksey |
0:fb7af294d5d9 | 84 | { |
Simon Cooksey |
0:fb7af294d5d9 | 85 | int ebx = 0; |
Simon Cooksey |
0:fb7af294d5d9 | 86 | uint32_t *rk; |
Simon Cooksey |
0:fb7af294d5d9 | 87 | uint32_t *blk; |
Simon Cooksey |
0:fb7af294d5d9 | 88 | uint32_t *ctrl; |
Simon Cooksey |
0:fb7af294d5d9 | 89 | unsigned char buf[256]; |
Simon Cooksey |
0:fb7af294d5d9 | 90 | |
Simon Cooksey |
0:fb7af294d5d9 | 91 | rk = ctx->rk; |
Simon Cooksey |
0:fb7af294d5d9 | 92 | blk = MBEDTLS_PADLOCK_ALIGN16( buf ); |
Simon Cooksey |
0:fb7af294d5d9 | 93 | memcpy( blk, input, 16 ); |
Simon Cooksey |
0:fb7af294d5d9 | 94 | |
Simon Cooksey |
0:fb7af294d5d9 | 95 | ctrl = blk + 4; |
Simon Cooksey |
0:fb7af294d5d9 | 96 | *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 ); |
Simon Cooksey |
0:fb7af294d5d9 | 97 | |
Simon Cooksey |
0:fb7af294d5d9 | 98 | asm( "pushfl \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 99 | "popfl \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 100 | "movl %%ebx, %0 \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 101 | "movl $1, %%ecx \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 102 | "movl %2, %%edx \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 103 | "movl %3, %%ebx \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 104 | "movl %4, %%esi \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 105 | "movl %4, %%edi \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 106 | ".byte 0xf3,0x0f,0xa7,0xc8 \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 107 | "movl %1, %%ebx \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 108 | : "=m" (ebx) |
Simon Cooksey |
0:fb7af294d5d9 | 109 | : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk) |
Simon Cooksey |
0:fb7af294d5d9 | 110 | : "memory", "ecx", "edx", "esi", "edi" ); |
Simon Cooksey |
0:fb7af294d5d9 | 111 | |
Simon Cooksey |
0:fb7af294d5d9 | 112 | memcpy( output, blk, 16 ); |
Simon Cooksey |
0:fb7af294d5d9 | 113 | |
Simon Cooksey |
0:fb7af294d5d9 | 114 | return( 0 ); |
Simon Cooksey |
0:fb7af294d5d9 | 115 | } |
Simon Cooksey |
0:fb7af294d5d9 | 116 | |
Simon Cooksey |
0:fb7af294d5d9 | 117 | /* |
Simon Cooksey |
0:fb7af294d5d9 | 118 | * PadLock AES-CBC buffer en(de)cryption |
Simon Cooksey |
0:fb7af294d5d9 | 119 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 120 | int mbedtls_padlock_xcryptcbc( mbedtls_aes_context *ctx, |
Simon Cooksey |
0:fb7af294d5d9 | 121 | int mode, |
Simon Cooksey |
0:fb7af294d5d9 | 122 | size_t length, |
Simon Cooksey |
0:fb7af294d5d9 | 123 | unsigned char iv[16], |
Simon Cooksey |
0:fb7af294d5d9 | 124 | const unsigned char *input, |
Simon Cooksey |
0:fb7af294d5d9 | 125 | unsigned char *output ) |
Simon Cooksey |
0:fb7af294d5d9 | 126 | { |
Simon Cooksey |
0:fb7af294d5d9 | 127 | int ebx = 0; |
Simon Cooksey |
0:fb7af294d5d9 | 128 | size_t count; |
Simon Cooksey |
0:fb7af294d5d9 | 129 | uint32_t *rk; |
Simon Cooksey |
0:fb7af294d5d9 | 130 | uint32_t *iw; |
Simon Cooksey |
0:fb7af294d5d9 | 131 | uint32_t *ctrl; |
Simon Cooksey |
0:fb7af294d5d9 | 132 | unsigned char buf[256]; |
Simon Cooksey |
0:fb7af294d5d9 | 133 | |
Simon Cooksey |
0:fb7af294d5d9 | 134 | if( ( (long) input & 15 ) != 0 || |
Simon Cooksey |
0:fb7af294d5d9 | 135 | ( (long) output & 15 ) != 0 ) |
Simon Cooksey |
0:fb7af294d5d9 | 136 | return( MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED ); |
Simon Cooksey |
0:fb7af294d5d9 | 137 | |
Simon Cooksey |
0:fb7af294d5d9 | 138 | rk = ctx->rk; |
Simon Cooksey |
0:fb7af294d5d9 | 139 | iw = MBEDTLS_PADLOCK_ALIGN16( buf ); |
Simon Cooksey |
0:fb7af294d5d9 | 140 | memcpy( iw, iv, 16 ); |
Simon Cooksey |
0:fb7af294d5d9 | 141 | |
Simon Cooksey |
0:fb7af294d5d9 | 142 | ctrl = iw + 4; |
Simon Cooksey |
0:fb7af294d5d9 | 143 | *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode ^ 1 ) - 10 ) << 9 ); |
Simon Cooksey |
0:fb7af294d5d9 | 144 | |
Simon Cooksey |
0:fb7af294d5d9 | 145 | count = ( length + 15 ) >> 4; |
Simon Cooksey |
0:fb7af294d5d9 | 146 | |
Simon Cooksey |
0:fb7af294d5d9 | 147 | asm( "pushfl \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 148 | "popfl \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 149 | "movl %%ebx, %0 \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 150 | "movl %2, %%ecx \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 151 | "movl %3, %%edx \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 152 | "movl %4, %%ebx \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 153 | "movl %5, %%esi \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 154 | "movl %6, %%edi \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 155 | "movl %7, %%eax \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 156 | ".byte 0xf3,0x0f,0xa7,0xd0 \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 157 | "movl %1, %%ebx \n\t" |
Simon Cooksey |
0:fb7af294d5d9 | 158 | : "=m" (ebx) |
Simon Cooksey |
0:fb7af294d5d9 | 159 | : "m" (ebx), "m" (count), "m" (ctrl), |
Simon Cooksey |
0:fb7af294d5d9 | 160 | "m" (rk), "m" (input), "m" (output), "m" (iw) |
Simon Cooksey |
0:fb7af294d5d9 | 161 | : "memory", "eax", "ecx", "edx", "esi", "edi" ); |
Simon Cooksey |
0:fb7af294d5d9 | 162 | |
Simon Cooksey |
0:fb7af294d5d9 | 163 | memcpy( iv, iw, 16 ); |
Simon Cooksey |
0:fb7af294d5d9 | 164 | |
Simon Cooksey |
0:fb7af294d5d9 | 165 | return( 0 ); |
Simon Cooksey |
0:fb7af294d5d9 | 166 | } |
Simon Cooksey |
0:fb7af294d5d9 | 167 | |
Simon Cooksey |
0:fb7af294d5d9 | 168 | #endif /* MBEDTLS_HAVE_X86 */ |
Simon Cooksey |
0:fb7af294d5d9 | 169 | |
Simon Cooksey |
0:fb7af294d5d9 | 170 | #endif /* MBEDTLS_PADLOCK_C */ |