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 * Platform-specific and custom entropy polling 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 #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_ENTROPY_C)
HannesTschofenig 0:796d0f61a05b 33
HannesTschofenig 0:796d0f61a05b 34 #include "polarssl/entropy.h"
HannesTschofenig 0:796d0f61a05b 35 #include "polarssl/entropy_poll.h"
HannesTschofenig 0:796d0f61a05b 36
HannesTschofenig 0:796d0f61a05b 37 #if defined(POLARSSL_TIMING_C)
HannesTschofenig 0:796d0f61a05b 38 #include "polarssl/timing.h"
HannesTschofenig 0:796d0f61a05b 39 #endif
HannesTschofenig 0:796d0f61a05b 40 #if defined(POLARSSL_HAVEGE_C)
HannesTschofenig 0:796d0f61a05b 41 #include "polarssl/havege.h"
HannesTschofenig 0:796d0f61a05b 42 #endif
HannesTschofenig 0:796d0f61a05b 43
HannesTschofenig 0:796d0f61a05b 44 #if !defined(POLARSSL_NO_PLATFORM_ENTROPY)
HannesTschofenig 0:796d0f61a05b 45 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
HannesTschofenig 0:796d0f61a05b 46
HannesTschofenig 0:796d0f61a05b 47 #if !defined(_WIN32_WINNT)
HannesTschofenig 0:796d0f61a05b 48 #define _WIN32_WINNT 0x0400
HannesTschofenig 0:796d0f61a05b 49 #endif
HannesTschofenig 0:796d0f61a05b 50 #include <windows.h>
HannesTschofenig 0:796d0f61a05b 51 #include <wincrypt.h>
HannesTschofenig 0:796d0f61a05b 52
HannesTschofenig 0:796d0f61a05b 53 int platform_entropy_poll( void *data, unsigned char *output, size_t len,
HannesTschofenig 0:796d0f61a05b 54 size_t *olen )
HannesTschofenig 0:796d0f61a05b 55 {
HannesTschofenig 0:796d0f61a05b 56 HCRYPTPROV provider;
HannesTschofenig 0:796d0f61a05b 57 ((void) data);
HannesTschofenig 0:796d0f61a05b 58 *olen = 0;
HannesTschofenig 0:796d0f61a05b 59
HannesTschofenig 0:796d0f61a05b 60 if( CryptAcquireContext( &provider, NULL, NULL,
HannesTschofenig 0:796d0f61a05b 61 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
HannesTschofenig 0:796d0f61a05b 62 {
HannesTschofenig 0:796d0f61a05b 63 return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
HannesTschofenig 0:796d0f61a05b 64 }
HannesTschofenig 0:796d0f61a05b 65
HannesTschofenig 0:796d0f61a05b 66 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
HannesTschofenig 0:796d0f61a05b 67 return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
HannesTschofenig 0:796d0f61a05b 68
HannesTschofenig 0:796d0f61a05b 69 CryptReleaseContext( provider, 0 );
HannesTschofenig 0:796d0f61a05b 70 *olen = len;
HannesTschofenig 0:796d0f61a05b 71
HannesTschofenig 0:796d0f61a05b 72 return( 0 );
HannesTschofenig 0:796d0f61a05b 73 }
HannesTschofenig 0:796d0f61a05b 74 #else /* _WIN32 && !EFIX64 && !EFI32 */
HannesTschofenig 0:796d0f61a05b 75
HannesTschofenig 0:796d0f61a05b 76 #include <stdio.h>
HannesTschofenig 0:796d0f61a05b 77
HannesTschofenig 0:796d0f61a05b 78 int platform_entropy_poll( void *data,
HannesTschofenig 0:796d0f61a05b 79 unsigned char *output, size_t len, size_t *olen )
HannesTschofenig 0:796d0f61a05b 80 {
HannesTschofenig 0:796d0f61a05b 81 FILE *file;
HannesTschofenig 0:796d0f61a05b 82 size_t ret;
HannesTschofenig 0:796d0f61a05b 83 ((void) data);
HannesTschofenig 0:796d0f61a05b 84
HannesTschofenig 0:796d0f61a05b 85 *olen = 0;
HannesTschofenig 0:796d0f61a05b 86
HannesTschofenig 0:796d0f61a05b 87 file = fopen( "/dev/urandom", "rb" );
HannesTschofenig 0:796d0f61a05b 88 if( file == NULL )
HannesTschofenig 0:796d0f61a05b 89 return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
HannesTschofenig 0:796d0f61a05b 90
HannesTschofenig 0:796d0f61a05b 91 ret = fread( output, 1, len, file );
HannesTschofenig 0:796d0f61a05b 92 if( ret != len )
HannesTschofenig 0:796d0f61a05b 93 {
HannesTschofenig 0:796d0f61a05b 94 fclose( file );
HannesTschofenig 0:796d0f61a05b 95 return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
HannesTschofenig 0:796d0f61a05b 96 }
HannesTschofenig 0:796d0f61a05b 97
HannesTschofenig 0:796d0f61a05b 98 fclose( file );
HannesTschofenig 0:796d0f61a05b 99 *olen = len;
HannesTschofenig 0:796d0f61a05b 100
HannesTschofenig 0:796d0f61a05b 101 return( 0 );
HannesTschofenig 0:796d0f61a05b 102 }
HannesTschofenig 0:796d0f61a05b 103 #endif /* _WIN32 && !EFIX64 && !EFI32 */
HannesTschofenig 0:796d0f61a05b 104 #endif /* !POLARSSL_NO_PLATFORM_ENTROPY */
HannesTschofenig 0:796d0f61a05b 105
HannesTschofenig 0:796d0f61a05b 106 #if defined(POLARSSL_TIMING_C)
HannesTschofenig 0:796d0f61a05b 107 int hardclock_poll( void *data,
HannesTschofenig 0:796d0f61a05b 108 unsigned char *output, size_t len, size_t *olen )
HannesTschofenig 0:796d0f61a05b 109 {
HannesTschofenig 0:796d0f61a05b 110 unsigned long timer = hardclock();
HannesTschofenig 0:796d0f61a05b 111 ((void) data);
HannesTschofenig 0:796d0f61a05b 112 *olen = 0;
HannesTschofenig 0:796d0f61a05b 113
HannesTschofenig 0:796d0f61a05b 114 if( len < sizeof(unsigned long) )
HannesTschofenig 0:796d0f61a05b 115 return( 0 );
HannesTschofenig 0:796d0f61a05b 116
HannesTschofenig 0:796d0f61a05b 117 memcpy( output, &timer, sizeof(unsigned long) );
HannesTschofenig 0:796d0f61a05b 118 *olen = sizeof(unsigned long);
HannesTschofenig 0:796d0f61a05b 119
HannesTschofenig 0:796d0f61a05b 120 return( 0 );
HannesTschofenig 0:796d0f61a05b 121 }
HannesTschofenig 0:796d0f61a05b 122 #endif /* POLARSSL_TIMING_C */
HannesTschofenig 0:796d0f61a05b 123
HannesTschofenig 0:796d0f61a05b 124 #if defined(POLARSSL_HAVEGE_C)
HannesTschofenig 0:796d0f61a05b 125 int havege_poll( void *data,
HannesTschofenig 0:796d0f61a05b 126 unsigned char *output, size_t len, size_t *olen )
HannesTschofenig 0:796d0f61a05b 127 {
HannesTschofenig 0:796d0f61a05b 128 havege_state *hs = (havege_state *) data;
HannesTschofenig 0:796d0f61a05b 129 *olen = 0;
HannesTschofenig 0:796d0f61a05b 130
HannesTschofenig 0:796d0f61a05b 131 if( havege_random( hs, output, len ) != 0 )
HannesTschofenig 0:796d0f61a05b 132 return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
HannesTschofenig 0:796d0f61a05b 133
HannesTschofenig 0:796d0f61a05b 134 *olen = len;
HannesTschofenig 0:796d0f61a05b 135
HannesTschofenig 0:796d0f61a05b 136 return( 0 );
HannesTschofenig 0:796d0f61a05b 137 }
HannesTschofenig 0:796d0f61a05b 138 #endif /* POLARSSL_HAVEGE_C */
HannesTschofenig 0:796d0f61a05b 139
HannesTschofenig 0:796d0f61a05b 140 #endif /* POLARSSL_ENTROPY_C */
HannesTschofenig 0:796d0f61a05b 141
HannesTschofenig 0:796d0f61a05b 142