mbed TLS library
Dependents: HTTPClient-SSL WS_SERVER
polarssl/padlock.c@0:137634ff4186, 2015-06-11 (annotated)
- Committer:
- ansond
- Date:
- Thu Jun 11 03:27:03 2015 +0000
- Revision:
- 0:137634ff4186
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:137634ff4186 | 1 | /* |
ansond | 0:137634ff4186 | 2 | * VIA PadLock support functions |
ansond | 0:137634ff4186 | 3 | * |
ansond | 0:137634ff4186 | 4 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
ansond | 0:137634ff4186 | 5 | * |
ansond | 0:137634ff4186 | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
ansond | 0:137634ff4186 | 7 | * |
ansond | 0:137634ff4186 | 8 | * This program is free software; you can redistribute it and/or modify |
ansond | 0:137634ff4186 | 9 | * it under the terms of the GNU General Public License as published by |
ansond | 0:137634ff4186 | 10 | * the Free Software Foundation; either version 2 of the License, or |
ansond | 0:137634ff4186 | 11 | * (at your option) any later version. |
ansond | 0:137634ff4186 | 12 | * |
ansond | 0:137634ff4186 | 13 | * This program is distributed in the hope that it will be useful, |
ansond | 0:137634ff4186 | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ansond | 0:137634ff4186 | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ansond | 0:137634ff4186 | 16 | * GNU General Public License for more details. |
ansond | 0:137634ff4186 | 17 | * |
ansond | 0:137634ff4186 | 18 | * You should have received a copy of the GNU General Public License along |
ansond | 0:137634ff4186 | 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
ansond | 0:137634ff4186 | 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
ansond | 0:137634ff4186 | 21 | */ |
ansond | 0:137634ff4186 | 22 | /* |
ansond | 0:137634ff4186 | 23 | * This implementation is based on the VIA PadLock Programming Guide: |
ansond | 0:137634ff4186 | 24 | * |
ansond | 0:137634ff4186 | 25 | * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/ |
ansond | 0:137634ff4186 | 26 | * programming_guide.pdf |
ansond | 0:137634ff4186 | 27 | */ |
ansond | 0:137634ff4186 | 28 | |
ansond | 0:137634ff4186 | 29 | #if !defined(POLARSSL_CONFIG_FILE) |
ansond | 0:137634ff4186 | 30 | #include "polarssl/config.h" |
ansond | 0:137634ff4186 | 31 | #else |
ansond | 0:137634ff4186 | 32 | #include POLARSSL_CONFIG_FILE |
ansond | 0:137634ff4186 | 33 | #endif |
ansond | 0:137634ff4186 | 34 | |
ansond | 0:137634ff4186 | 35 | #if defined(POLARSSL_PADLOCK_C) |
ansond | 0:137634ff4186 | 36 | |
ansond | 0:137634ff4186 | 37 | #include "polarssl/padlock.h" |
ansond | 0:137634ff4186 | 38 | |
ansond | 0:137634ff4186 | 39 | #include <string.h> |
ansond | 0:137634ff4186 | 40 | |
ansond | 0:137634ff4186 | 41 | #if defined(POLARSSL_HAVE_X86) |
ansond | 0:137634ff4186 | 42 | |
ansond | 0:137634ff4186 | 43 | /* |
ansond | 0:137634ff4186 | 44 | * PadLock detection routine |
ansond | 0:137634ff4186 | 45 | */ |
ansond | 0:137634ff4186 | 46 | int padlock_supports( int feature ) |
ansond | 0:137634ff4186 | 47 | { |
ansond | 0:137634ff4186 | 48 | static int flags = -1; |
ansond | 0:137634ff4186 | 49 | int ebx = 0, edx = 0; |
ansond | 0:137634ff4186 | 50 | |
ansond | 0:137634ff4186 | 51 | if( flags == -1 ) |
ansond | 0:137634ff4186 | 52 | { |
ansond | 0:137634ff4186 | 53 | asm( "movl %%ebx, %0 \n\t" |
ansond | 0:137634ff4186 | 54 | "movl $0xC0000000, %%eax \n\t" |
ansond | 0:137634ff4186 | 55 | "cpuid \n\t" |
ansond | 0:137634ff4186 | 56 | "cmpl $0xC0000001, %%eax \n\t" |
ansond | 0:137634ff4186 | 57 | "movl $0, %%edx \n\t" |
ansond | 0:137634ff4186 | 58 | "jb unsupported \n\t" |
ansond | 0:137634ff4186 | 59 | "movl $0xC0000001, %%eax \n\t" |
ansond | 0:137634ff4186 | 60 | "cpuid \n\t" |
ansond | 0:137634ff4186 | 61 | "unsupported: \n\t" |
ansond | 0:137634ff4186 | 62 | "movl %%edx, %1 \n\t" |
ansond | 0:137634ff4186 | 63 | "movl %2, %%ebx \n\t" |
ansond | 0:137634ff4186 | 64 | : "=m" (ebx), "=m" (edx) |
ansond | 0:137634ff4186 | 65 | : "m" (ebx) |
ansond | 0:137634ff4186 | 66 | : "eax", "ecx", "edx" ); |
ansond | 0:137634ff4186 | 67 | |
ansond | 0:137634ff4186 | 68 | flags = edx; |
ansond | 0:137634ff4186 | 69 | } |
ansond | 0:137634ff4186 | 70 | |
ansond | 0:137634ff4186 | 71 | return( flags & feature ); |
ansond | 0:137634ff4186 | 72 | } |
ansond | 0:137634ff4186 | 73 | |
ansond | 0:137634ff4186 | 74 | /* |
ansond | 0:137634ff4186 | 75 | * PadLock AES-ECB block en(de)cryption |
ansond | 0:137634ff4186 | 76 | */ |
ansond | 0:137634ff4186 | 77 | int padlock_xcryptecb( aes_context *ctx, |
ansond | 0:137634ff4186 | 78 | int mode, |
ansond | 0:137634ff4186 | 79 | const unsigned char input[16], |
ansond | 0:137634ff4186 | 80 | unsigned char output[16] ) |
ansond | 0:137634ff4186 | 81 | { |
ansond | 0:137634ff4186 | 82 | int ebx = 0; |
ansond | 0:137634ff4186 | 83 | uint32_t *rk; |
ansond | 0:137634ff4186 | 84 | uint32_t *blk; |
ansond | 0:137634ff4186 | 85 | uint32_t *ctrl; |
ansond | 0:137634ff4186 | 86 | unsigned char buf[256]; |
ansond | 0:137634ff4186 | 87 | |
ansond | 0:137634ff4186 | 88 | rk = ctx->rk; |
ansond | 0:137634ff4186 | 89 | blk = PADLOCK_ALIGN16( buf ); |
ansond | 0:137634ff4186 | 90 | memcpy( blk, input, 16 ); |
ansond | 0:137634ff4186 | 91 | |
ansond | 0:137634ff4186 | 92 | ctrl = blk + 4; |
ansond | 0:137634ff4186 | 93 | *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 ); |
ansond | 0:137634ff4186 | 94 | |
ansond | 0:137634ff4186 | 95 | asm( "pushfl \n\t" |
ansond | 0:137634ff4186 | 96 | "popfl \n\t" |
ansond | 0:137634ff4186 | 97 | "movl %%ebx, %0 \n\t" |
ansond | 0:137634ff4186 | 98 | "movl $1, %%ecx \n\t" |
ansond | 0:137634ff4186 | 99 | "movl %2, %%edx \n\t" |
ansond | 0:137634ff4186 | 100 | "movl %3, %%ebx \n\t" |
ansond | 0:137634ff4186 | 101 | "movl %4, %%esi \n\t" |
ansond | 0:137634ff4186 | 102 | "movl %4, %%edi \n\t" |
ansond | 0:137634ff4186 | 103 | ".byte 0xf3,0x0f,0xa7,0xc8 \n\t" |
ansond | 0:137634ff4186 | 104 | "movl %1, %%ebx \n\t" |
ansond | 0:137634ff4186 | 105 | : "=m" (ebx) |
ansond | 0:137634ff4186 | 106 | : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk) |
ansond | 0:137634ff4186 | 107 | : "memory", "ecx", "edx", "esi", "edi" ); |
ansond | 0:137634ff4186 | 108 | |
ansond | 0:137634ff4186 | 109 | memcpy( output, blk, 16 ); |
ansond | 0:137634ff4186 | 110 | |
ansond | 0:137634ff4186 | 111 | return( 0 ); |
ansond | 0:137634ff4186 | 112 | } |
ansond | 0:137634ff4186 | 113 | |
ansond | 0:137634ff4186 | 114 | /* |
ansond | 0:137634ff4186 | 115 | * PadLock AES-CBC buffer en(de)cryption |
ansond | 0:137634ff4186 | 116 | */ |
ansond | 0:137634ff4186 | 117 | int padlock_xcryptcbc( aes_context *ctx, |
ansond | 0:137634ff4186 | 118 | int mode, |
ansond | 0:137634ff4186 | 119 | size_t length, |
ansond | 0:137634ff4186 | 120 | unsigned char iv[16], |
ansond | 0:137634ff4186 | 121 | const unsigned char *input, |
ansond | 0:137634ff4186 | 122 | unsigned char *output ) |
ansond | 0:137634ff4186 | 123 | { |
ansond | 0:137634ff4186 | 124 | int ebx = 0; |
ansond | 0:137634ff4186 | 125 | size_t count; |
ansond | 0:137634ff4186 | 126 | uint32_t *rk; |
ansond | 0:137634ff4186 | 127 | uint32_t *iw; |
ansond | 0:137634ff4186 | 128 | uint32_t *ctrl; |
ansond | 0:137634ff4186 | 129 | unsigned char buf[256]; |
ansond | 0:137634ff4186 | 130 | |
ansond | 0:137634ff4186 | 131 | if( ( (long) input & 15 ) != 0 || |
ansond | 0:137634ff4186 | 132 | ( (long) output & 15 ) != 0 ) |
ansond | 0:137634ff4186 | 133 | return( POLARSSL_ERR_PADLOCK_DATA_MISALIGNED ); |
ansond | 0:137634ff4186 | 134 | |
ansond | 0:137634ff4186 | 135 | rk = ctx->rk; |
ansond | 0:137634ff4186 | 136 | iw = PADLOCK_ALIGN16( buf ); |
ansond | 0:137634ff4186 | 137 | memcpy( iw, iv, 16 ); |
ansond | 0:137634ff4186 | 138 | |
ansond | 0:137634ff4186 | 139 | ctrl = iw + 4; |
ansond | 0:137634ff4186 | 140 | *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode ^ 1 ) - 10 ) << 9 ); |
ansond | 0:137634ff4186 | 141 | |
ansond | 0:137634ff4186 | 142 | count = ( length + 15 ) >> 4; |
ansond | 0:137634ff4186 | 143 | |
ansond | 0:137634ff4186 | 144 | asm( "pushfl \n\t" |
ansond | 0:137634ff4186 | 145 | "popfl \n\t" |
ansond | 0:137634ff4186 | 146 | "movl %%ebx, %0 \n\t" |
ansond | 0:137634ff4186 | 147 | "movl %2, %%ecx \n\t" |
ansond | 0:137634ff4186 | 148 | "movl %3, %%edx \n\t" |
ansond | 0:137634ff4186 | 149 | "movl %4, %%ebx \n\t" |
ansond | 0:137634ff4186 | 150 | "movl %5, %%esi \n\t" |
ansond | 0:137634ff4186 | 151 | "movl %6, %%edi \n\t" |
ansond | 0:137634ff4186 | 152 | "movl %7, %%eax \n\t" |
ansond | 0:137634ff4186 | 153 | ".byte 0xf3,0x0f,0xa7,0xd0 \n\t" |
ansond | 0:137634ff4186 | 154 | "movl %1, %%ebx \n\t" |
ansond | 0:137634ff4186 | 155 | : "=m" (ebx) |
ansond | 0:137634ff4186 | 156 | : "m" (ebx), "m" (count), "m" (ctrl), |
ansond | 0:137634ff4186 | 157 | "m" (rk), "m" (input), "m" (output), "m" (iw) |
ansond | 0:137634ff4186 | 158 | : "memory", "eax", "ecx", "edx", "esi", "edi" ); |
ansond | 0:137634ff4186 | 159 | |
ansond | 0:137634ff4186 | 160 | memcpy( iv, iw, 16 ); |
ansond | 0:137634ff4186 | 161 | |
ansond | 0:137634ff4186 | 162 | return( 0 ); |
ansond | 0:137634ff4186 | 163 | } |
ansond | 0:137634ff4186 | 164 | |
ansond | 0:137634ff4186 | 165 | #endif /* POLARSSL_HAVE_X86 */ |
ansond | 0:137634ff4186 | 166 | |
ansond | 0:137634ff4186 | 167 | #endif /* POLARSSL_PADLOCK_C */ |
ansond | 0:137634ff4186 | 168 |