Modified mbed TLS headers for AES functionality only to reduce build size

Dependents:   BLE_Gateway_Linker_fix BLE_Gateway

Fork of mbedtls by sandbox

Committer:
Christopher Haster
Date:
Fri Jan 22 16:44:49 2016 -0600
Revision:
1:24750b9ad5ef
Initial move of mbedtls to mercurial

Who changed what in which revision?

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