Example program to test AES-GCM functionality. Used for a workshop

Dependencies:   mbed

Committer:
HannesTschofenig
Date:
Thu Sep 27 06:34:22 2018 +0000
Revision:
0:796d0f61a05b
Example AES-GCM test program

Who changed what in which revision?

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