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 * An 32-bit implementation of the XTEA algorithm
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 #if !defined(POLARSSL_CONFIG_FILE)
HannesTschofenig 0:796d0f61a05b 27 #include "polarssl/config.h"
HannesTschofenig 0:796d0f61a05b 28 #else
HannesTschofenig 0:796d0f61a05b 29 #include POLARSSL_CONFIG_FILE
HannesTschofenig 0:796d0f61a05b 30 #endif
HannesTschofenig 0:796d0f61a05b 31
HannesTschofenig 0:796d0f61a05b 32 #if defined(POLARSSL_XTEA_C)
HannesTschofenig 0:796d0f61a05b 33
HannesTschofenig 0:796d0f61a05b 34 #include "polarssl/xtea.h"
HannesTschofenig 0:796d0f61a05b 35
HannesTschofenig 0:796d0f61a05b 36 #if defined(POLARSSL_PLATFORM_C)
HannesTschofenig 0:796d0f61a05b 37 #include "polarssl/platform.h"
HannesTschofenig 0:796d0f61a05b 38 #else
HannesTschofenig 0:796d0f61a05b 39 #define polarssl_printf printf
HannesTschofenig 0:796d0f61a05b 40 #endif
HannesTschofenig 0:796d0f61a05b 41
HannesTschofenig 0:796d0f61a05b 42 #if !defined(POLARSSL_XTEA_ALT)
HannesTschofenig 0:796d0f61a05b 43
HannesTschofenig 0:796d0f61a05b 44 /*
HannesTschofenig 0:796d0f61a05b 45 * 32-bit integer manipulation macros (big endian)
HannesTschofenig 0:796d0f61a05b 46 */
HannesTschofenig 0:796d0f61a05b 47 #ifndef GET_UINT32_BE
HannesTschofenig 0:796d0f61a05b 48 #define GET_UINT32_BE(n,b,i) \
HannesTschofenig 0:796d0f61a05b 49 { \
HannesTschofenig 0:796d0f61a05b 50 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
HannesTschofenig 0:796d0f61a05b 51 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
HannesTschofenig 0:796d0f61a05b 52 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
HannesTschofenig 0:796d0f61a05b 53 | ( (uint32_t) (b)[(i) + 3] ); \
HannesTschofenig 0:796d0f61a05b 54 }
HannesTschofenig 0:796d0f61a05b 55 #endif
HannesTschofenig 0:796d0f61a05b 56
HannesTschofenig 0:796d0f61a05b 57 #ifndef PUT_UINT32_BE
HannesTschofenig 0:796d0f61a05b 58 #define PUT_UINT32_BE(n,b,i) \
HannesTschofenig 0:796d0f61a05b 59 { \
HannesTschofenig 0:796d0f61a05b 60 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
HannesTschofenig 0:796d0f61a05b 61 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
HannesTschofenig 0:796d0f61a05b 62 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
HannesTschofenig 0:796d0f61a05b 63 (b)[(i) + 3] = (unsigned char) ( (n) ); \
HannesTschofenig 0:796d0f61a05b 64 }
HannesTschofenig 0:796d0f61a05b 65 #endif
HannesTschofenig 0:796d0f61a05b 66
HannesTschofenig 0:796d0f61a05b 67 /*
HannesTschofenig 0:796d0f61a05b 68 * XTEA key schedule
HannesTschofenig 0:796d0f61a05b 69 */
HannesTschofenig 0:796d0f61a05b 70 void xtea_setup( xtea_context *ctx, const unsigned char key[16] )
HannesTschofenig 0:796d0f61a05b 71 {
HannesTschofenig 0:796d0f61a05b 72 int i;
HannesTschofenig 0:796d0f61a05b 73
HannesTschofenig 0:796d0f61a05b 74 memset(ctx, 0, sizeof(xtea_context));
HannesTschofenig 0:796d0f61a05b 75
HannesTschofenig 0:796d0f61a05b 76 for( i = 0; i < 4; i++ )
HannesTschofenig 0:796d0f61a05b 77 {
HannesTschofenig 0:796d0f61a05b 78 GET_UINT32_BE( ctx->k[i], key, i << 2 );
HannesTschofenig 0:796d0f61a05b 79 }
HannesTschofenig 0:796d0f61a05b 80 }
HannesTschofenig 0:796d0f61a05b 81
HannesTschofenig 0:796d0f61a05b 82 /*
HannesTschofenig 0:796d0f61a05b 83 * XTEA encrypt function
HannesTschofenig 0:796d0f61a05b 84 */
HannesTschofenig 0:796d0f61a05b 85 int xtea_crypt_ecb( xtea_context *ctx, int mode,
HannesTschofenig 0:796d0f61a05b 86 const unsigned char input[8], unsigned char output[8])
HannesTschofenig 0:796d0f61a05b 87 {
HannesTschofenig 0:796d0f61a05b 88 uint32_t *k, v0, v1, i;
HannesTschofenig 0:796d0f61a05b 89
HannesTschofenig 0:796d0f61a05b 90 k = ctx->k;
HannesTschofenig 0:796d0f61a05b 91
HannesTschofenig 0:796d0f61a05b 92 GET_UINT32_BE( v0, input, 0 );
HannesTschofenig 0:796d0f61a05b 93 GET_UINT32_BE( v1, input, 4 );
HannesTschofenig 0:796d0f61a05b 94
HannesTschofenig 0:796d0f61a05b 95 if( mode == XTEA_ENCRYPT )
HannesTschofenig 0:796d0f61a05b 96 {
HannesTschofenig 0:796d0f61a05b 97 uint32_t sum = 0, delta = 0x9E3779B9;
HannesTschofenig 0:796d0f61a05b 98
HannesTschofenig 0:796d0f61a05b 99 for( i = 0; i < 32; i++ )
HannesTschofenig 0:796d0f61a05b 100 {
HannesTschofenig 0:796d0f61a05b 101 v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
HannesTschofenig 0:796d0f61a05b 102 sum += delta;
HannesTschofenig 0:796d0f61a05b 103 v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
HannesTschofenig 0:796d0f61a05b 104 }
HannesTschofenig 0:796d0f61a05b 105 }
HannesTschofenig 0:796d0f61a05b 106 else /* XTEA_DECRYPT */
HannesTschofenig 0:796d0f61a05b 107 {
HannesTschofenig 0:796d0f61a05b 108 uint32_t delta = 0x9E3779B9, sum = delta * 32;
HannesTschofenig 0:796d0f61a05b 109
HannesTschofenig 0:796d0f61a05b 110 for( i = 0; i < 32; i++ )
HannesTschofenig 0:796d0f61a05b 111 {
HannesTschofenig 0:796d0f61a05b 112 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
HannesTschofenig 0:796d0f61a05b 113 sum -= delta;
HannesTschofenig 0:796d0f61a05b 114 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
HannesTschofenig 0:796d0f61a05b 115 }
HannesTschofenig 0:796d0f61a05b 116 }
HannesTschofenig 0:796d0f61a05b 117
HannesTschofenig 0:796d0f61a05b 118 PUT_UINT32_BE( v0, output, 0 );
HannesTschofenig 0:796d0f61a05b 119 PUT_UINT32_BE( v1, output, 4 );
HannesTschofenig 0:796d0f61a05b 120
HannesTschofenig 0:796d0f61a05b 121 return( 0 );
HannesTschofenig 0:796d0f61a05b 122 }
HannesTschofenig 0:796d0f61a05b 123
HannesTschofenig 0:796d0f61a05b 124 #if defined(POLARSSL_CIPHER_MODE_CBC)
HannesTschofenig 0:796d0f61a05b 125 /*
HannesTschofenig 0:796d0f61a05b 126 * XTEA-CBC buffer encryption/decryption
HannesTschofenig 0:796d0f61a05b 127 */
HannesTschofenig 0:796d0f61a05b 128 int xtea_crypt_cbc( xtea_context *ctx, int mode, size_t length,
HannesTschofenig 0:796d0f61a05b 129 unsigned char iv[8], const unsigned char *input,
HannesTschofenig 0:796d0f61a05b 130 unsigned char *output)
HannesTschofenig 0:796d0f61a05b 131 {
HannesTschofenig 0:796d0f61a05b 132 int i;
HannesTschofenig 0:796d0f61a05b 133 unsigned char temp[8];
HannesTschofenig 0:796d0f61a05b 134
HannesTschofenig 0:796d0f61a05b 135 if( length % 8 )
HannesTschofenig 0:796d0f61a05b 136 return( POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH );
HannesTschofenig 0:796d0f61a05b 137
HannesTschofenig 0:796d0f61a05b 138 if( mode == XTEA_DECRYPT )
HannesTschofenig 0:796d0f61a05b 139 {
HannesTschofenig 0:796d0f61a05b 140 while( length > 0 )
HannesTschofenig 0:796d0f61a05b 141 {
HannesTschofenig 0:796d0f61a05b 142 memcpy( temp, input, 8 );
HannesTschofenig 0:796d0f61a05b 143 xtea_crypt_ecb( ctx, mode, input, output );
HannesTschofenig 0:796d0f61a05b 144
HannesTschofenig 0:796d0f61a05b 145 for(i = 0; i < 8; i++)
HannesTschofenig 0:796d0f61a05b 146 output[i] = (unsigned char)( output[i] ^ iv[i] );
HannesTschofenig 0:796d0f61a05b 147
HannesTschofenig 0:796d0f61a05b 148 memcpy( iv, temp, 8 );
HannesTschofenig 0:796d0f61a05b 149
HannesTschofenig 0:796d0f61a05b 150 input += 8;
HannesTschofenig 0:796d0f61a05b 151 output += 8;
HannesTschofenig 0:796d0f61a05b 152 length -= 8;
HannesTschofenig 0:796d0f61a05b 153 }
HannesTschofenig 0:796d0f61a05b 154 }
HannesTschofenig 0:796d0f61a05b 155 else
HannesTschofenig 0:796d0f61a05b 156 {
HannesTschofenig 0:796d0f61a05b 157 while( length > 0 )
HannesTschofenig 0:796d0f61a05b 158 {
HannesTschofenig 0:796d0f61a05b 159 for( i = 0; i < 8; i++ )
HannesTschofenig 0:796d0f61a05b 160 output[i] = (unsigned char)( input[i] ^ iv[i] );
HannesTschofenig 0:796d0f61a05b 161
HannesTschofenig 0:796d0f61a05b 162 xtea_crypt_ecb( ctx, mode, output, output );
HannesTschofenig 0:796d0f61a05b 163 memcpy( iv, output, 8 );
HannesTschofenig 0:796d0f61a05b 164
HannesTschofenig 0:796d0f61a05b 165 input += 8;
HannesTschofenig 0:796d0f61a05b 166 output += 8;
HannesTschofenig 0:796d0f61a05b 167 length -= 8;
HannesTschofenig 0:796d0f61a05b 168 }
HannesTschofenig 0:796d0f61a05b 169 }
HannesTschofenig 0:796d0f61a05b 170
HannesTschofenig 0:796d0f61a05b 171 return( 0 );
HannesTschofenig 0:796d0f61a05b 172 }
HannesTschofenig 0:796d0f61a05b 173 #endif /* POLARSSL_CIPHER_MODE_CBC */
HannesTschofenig 0:796d0f61a05b 174 #endif /* !POLARSSL_XTEA_ALT */
HannesTschofenig 0:796d0f61a05b 175
HannesTschofenig 0:796d0f61a05b 176 #if defined(POLARSSL_SELF_TEST)
HannesTschofenig 0:796d0f61a05b 177
HannesTschofenig 0:796d0f61a05b 178 #include <string.h>
HannesTschofenig 0:796d0f61a05b 179 #include <stdio.h>
HannesTschofenig 0:796d0f61a05b 180
HannesTschofenig 0:796d0f61a05b 181 /*
HannesTschofenig 0:796d0f61a05b 182 * XTEA tests vectors (non-official)
HannesTschofenig 0:796d0f61a05b 183 */
HannesTschofenig 0:796d0f61a05b 184
HannesTschofenig 0:796d0f61a05b 185 static const unsigned char xtea_test_key[6][16] =
HannesTschofenig 0:796d0f61a05b 186 {
HannesTschofenig 0:796d0f61a05b 187 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
HannesTschofenig 0:796d0f61a05b 188 0x0c, 0x0d, 0x0e, 0x0f },
HannesTschofenig 0:796d0f61a05b 189 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
HannesTschofenig 0:796d0f61a05b 190 0x0c, 0x0d, 0x0e, 0x0f },
HannesTschofenig 0:796d0f61a05b 191 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
HannesTschofenig 0:796d0f61a05b 192 0x0c, 0x0d, 0x0e, 0x0f },
HannesTschofenig 0:796d0f61a05b 193 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
HannesTschofenig 0:796d0f61a05b 194 0x00, 0x00, 0x00, 0x00 },
HannesTschofenig 0:796d0f61a05b 195 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
HannesTschofenig 0:796d0f61a05b 196 0x00, 0x00, 0x00, 0x00 },
HannesTschofenig 0:796d0f61a05b 197 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
HannesTschofenig 0:796d0f61a05b 198 0x00, 0x00, 0x00, 0x00 }
HannesTschofenig 0:796d0f61a05b 199 };
HannesTschofenig 0:796d0f61a05b 200
HannesTschofenig 0:796d0f61a05b 201 static const unsigned char xtea_test_pt[6][8] =
HannesTschofenig 0:796d0f61a05b 202 {
HannesTschofenig 0:796d0f61a05b 203 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
HannesTschofenig 0:796d0f61a05b 204 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
HannesTschofenig 0:796d0f61a05b 205 { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
HannesTschofenig 0:796d0f61a05b 206 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
HannesTschofenig 0:796d0f61a05b 207 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
HannesTschofenig 0:796d0f61a05b 208 { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
HannesTschofenig 0:796d0f61a05b 209 };
HannesTschofenig 0:796d0f61a05b 210
HannesTschofenig 0:796d0f61a05b 211 static const unsigned char xtea_test_ct[6][8] =
HannesTschofenig 0:796d0f61a05b 212 {
HannesTschofenig 0:796d0f61a05b 213 { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
HannesTschofenig 0:796d0f61a05b 214 { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
HannesTschofenig 0:796d0f61a05b 215 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
HannesTschofenig 0:796d0f61a05b 216 { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
HannesTschofenig 0:796d0f61a05b 217 { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
HannesTschofenig 0:796d0f61a05b 218 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
HannesTschofenig 0:796d0f61a05b 219 };
HannesTschofenig 0:796d0f61a05b 220
HannesTschofenig 0:796d0f61a05b 221 /*
HannesTschofenig 0:796d0f61a05b 222 * Checkup routine
HannesTschofenig 0:796d0f61a05b 223 */
HannesTschofenig 0:796d0f61a05b 224 int xtea_self_test( int verbose )
HannesTschofenig 0:796d0f61a05b 225 {
HannesTschofenig 0:796d0f61a05b 226 int i;
HannesTschofenig 0:796d0f61a05b 227 unsigned char buf[8];
HannesTschofenig 0:796d0f61a05b 228 xtea_context ctx;
HannesTschofenig 0:796d0f61a05b 229
HannesTschofenig 0:796d0f61a05b 230 for( i = 0; i < 6; i++ )
HannesTschofenig 0:796d0f61a05b 231 {
HannesTschofenig 0:796d0f61a05b 232 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 233 polarssl_printf( " XTEA test #%d: ", i + 1 );
HannesTschofenig 0:796d0f61a05b 234
HannesTschofenig 0:796d0f61a05b 235 memcpy( buf, xtea_test_pt[i], 8 );
HannesTschofenig 0:796d0f61a05b 236
HannesTschofenig 0:796d0f61a05b 237 xtea_setup( &ctx, xtea_test_key[i] );
HannesTschofenig 0:796d0f61a05b 238 xtea_crypt_ecb( &ctx, XTEA_ENCRYPT, buf, buf );
HannesTschofenig 0:796d0f61a05b 239
HannesTschofenig 0:796d0f61a05b 240 if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
HannesTschofenig 0:796d0f61a05b 241 {
HannesTschofenig 0:796d0f61a05b 242 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 243 polarssl_printf( "failed\n" );
HannesTschofenig 0:796d0f61a05b 244
HannesTschofenig 0:796d0f61a05b 245 return( 1 );
HannesTschofenig 0:796d0f61a05b 246 }
HannesTschofenig 0:796d0f61a05b 247
HannesTschofenig 0:796d0f61a05b 248 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 249 polarssl_printf( "passed\n" );
HannesTschofenig 0:796d0f61a05b 250 }
HannesTschofenig 0:796d0f61a05b 251
HannesTschofenig 0:796d0f61a05b 252 if( verbose != 0 )
HannesTschofenig 0:796d0f61a05b 253 polarssl_printf( "\n" );
HannesTschofenig 0:796d0f61a05b 254
HannesTschofenig 0:796d0f61a05b 255 return( 0 );
HannesTschofenig 0:796d0f61a05b 256 }
HannesTschofenig 0:796d0f61a05b 257
HannesTschofenig 0:796d0f61a05b 258 #endif /* POLARSSL_SELF_TEST */
HannesTschofenig 0:796d0f61a05b 259
HannesTschofenig 0:796d0f61a05b 260 #endif /* POLARSSL_XTEA_C */
HannesTschofenig 0:796d0f61a05b 261
HannesTschofenig 0:796d0f61a05b 262