Hannes Tschofenig
/
aes-gcm-test-program
Example program to test AES-GCM functionality. Used for a workshop
SSL/include/polarssl/openssl.h@0:796d0f61a05b, 2018-09-27 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
HannesTschofenig | 0:796d0f61a05b | 1 | /** |
HannesTschofenig | 0:796d0f61a05b | 2 | * \file openssl.h |
HannesTschofenig | 0:796d0f61a05b | 3 | * |
HannesTschofenig | 0:796d0f61a05b | 4 | * \brief OpenSSL wrapper (definitions, inline functions). |
HannesTschofenig | 0:796d0f61a05b | 5 | * |
HannesTschofenig | 0:796d0f61a05b | 6 | * Copyright (C) 2006-2010, Brainspark B.V. |
HannesTschofenig | 0:796d0f61a05b | 7 | * |
HannesTschofenig | 0:796d0f61a05b | 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
HannesTschofenig | 0:796d0f61a05b | 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
HannesTschofenig | 0:796d0f61a05b | 10 | * |
HannesTschofenig | 0:796d0f61a05b | 11 | * All rights reserved. |
HannesTschofenig | 0:796d0f61a05b | 12 | * |
HannesTschofenig | 0:796d0f61a05b | 13 | * This program is free software; you can redistribute it and/or modify |
HannesTschofenig | 0:796d0f61a05b | 14 | * it under the terms of the GNU General Public License as published by |
HannesTschofenig | 0:796d0f61a05b | 15 | * the Free Software Foundation; either version 2 of the License, or |
HannesTschofenig | 0:796d0f61a05b | 16 | * (at your option) any later version. |
HannesTschofenig | 0:796d0f61a05b | 17 | * |
HannesTschofenig | 0:796d0f61a05b | 18 | * This program is distributed in the hope that it will be useful, |
HannesTschofenig | 0:796d0f61a05b | 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
HannesTschofenig | 0:796d0f61a05b | 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
HannesTschofenig | 0:796d0f61a05b | 21 | * GNU General Public License for more details. |
HannesTschofenig | 0:796d0f61a05b | 22 | * |
HannesTschofenig | 0:796d0f61a05b | 23 | * You should have received a copy of the GNU General Public License along |
HannesTschofenig | 0:796d0f61a05b | 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
HannesTschofenig | 0:796d0f61a05b | 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
HannesTschofenig | 0:796d0f61a05b | 26 | */ |
HannesTschofenig | 0:796d0f61a05b | 27 | /* |
HannesTschofenig | 0:796d0f61a05b | 28 | * OpenSSL wrapper contributed by David Barett |
HannesTschofenig | 0:796d0f61a05b | 29 | */ |
HannesTschofenig | 0:796d0f61a05b | 30 | #ifndef POLARSSL_OPENSSL_H |
HannesTschofenig | 0:796d0f61a05b | 31 | #define POLARSSL_OPENSSL_H |
HannesTschofenig | 0:796d0f61a05b | 32 | |
HannesTschofenig | 0:796d0f61a05b | 33 | #include "aes.h" |
HannesTschofenig | 0:796d0f61a05b | 34 | #include "md5.h" |
HannesTschofenig | 0:796d0f61a05b | 35 | #include "rsa.h" |
HannesTschofenig | 0:796d0f61a05b | 36 | #include "sha1.h" |
HannesTschofenig | 0:796d0f61a05b | 37 | |
HannesTschofenig | 0:796d0f61a05b | 38 | #define AES_SIZE 16 |
HannesTschofenig | 0:796d0f61a05b | 39 | #define AES_BLOCK_SIZE 16 |
HannesTschofenig | 0:796d0f61a05b | 40 | #define AES_KEY aes_context |
HannesTschofenig | 0:796d0f61a05b | 41 | #define MD5_CTX md5_context |
HannesTschofenig | 0:796d0f61a05b | 42 | #define SHA_CTX sha1_context |
HannesTschofenig | 0:796d0f61a05b | 43 | |
HannesTschofenig | 0:796d0f61a05b | 44 | #define SHA1_Init( CTX ) \ |
HannesTschofenig | 0:796d0f61a05b | 45 | sha1_starts( (CTX) ) |
HannesTschofenig | 0:796d0f61a05b | 46 | #define SHA1_Update( CTX, BUF, LEN ) \ |
HannesTschofenig | 0:796d0f61a05b | 47 | sha1_update( (CTX), (unsigned char *)(BUF), (LEN) ) |
HannesTschofenig | 0:796d0f61a05b | 48 | #define SHA1_Final( OUT, CTX ) \ |
HannesTschofenig | 0:796d0f61a05b | 49 | sha1_finish( (CTX), (OUT) ) |
HannesTschofenig | 0:796d0f61a05b | 50 | |
HannesTschofenig | 0:796d0f61a05b | 51 | #define MD5_Init( CTX ) \ |
HannesTschofenig | 0:796d0f61a05b | 52 | md5_starts( (CTX) ) |
HannesTschofenig | 0:796d0f61a05b | 53 | #define MD5_Update( CTX, BUF, LEN ) \ |
HannesTschofenig | 0:796d0f61a05b | 54 | md5_update( (CTX), (unsigned char *)(BUF), (LEN) ) |
HannesTschofenig | 0:796d0f61a05b | 55 | #define MD5_Final( OUT, CTX ) \ |
HannesTschofenig | 0:796d0f61a05b | 56 | md5_finish( (CTX), (OUT) ) |
HannesTschofenig | 0:796d0f61a05b | 57 | |
HannesTschofenig | 0:796d0f61a05b | 58 | #define AES_set_encrypt_key( KEY, KEYSIZE, CTX ) \ |
HannesTschofenig | 0:796d0f61a05b | 59 | aes_setkey_enc( (CTX), (KEY), (KEYSIZE) ) |
HannesTschofenig | 0:796d0f61a05b | 60 | #define AES_set_decrypt_key( KEY, KEYSIZE, CTX ) \ |
HannesTschofenig | 0:796d0f61a05b | 61 | aes_setkey_dec( (CTX), (KEY), (KEYSIZE) ) |
HannesTschofenig | 0:796d0f61a05b | 62 | #define AES_cbc_encrypt( INPUT, OUTPUT, LEN, CTX, IV, MODE ) \ |
HannesTschofenig | 0:796d0f61a05b | 63 | aes_crypt_cbc( (CTX), (MODE), (LEN), (IV), (INPUT), (OUTPUT) ) |
HannesTschofenig | 0:796d0f61a05b | 64 | |
HannesTschofenig | 0:796d0f61a05b | 65 | #ifdef __cplusplus |
HannesTschofenig | 0:796d0f61a05b | 66 | extern "C" { |
HannesTschofenig | 0:796d0f61a05b | 67 | #endif |
HannesTschofenig | 0:796d0f61a05b | 68 | |
HannesTschofenig | 0:796d0f61a05b | 69 | /* |
HannesTschofenig | 0:796d0f61a05b | 70 | * RSA stuff follows. TODO: needs cleanup |
HannesTschofenig | 0:796d0f61a05b | 71 | */ |
HannesTschofenig | 0:796d0f61a05b | 72 | inline int __RSA_Passthrough( void *output, void *input, int size ) |
HannesTschofenig | 0:796d0f61a05b | 73 | { |
HannesTschofenig | 0:796d0f61a05b | 74 | memcpy( output, input, size ); |
HannesTschofenig | 0:796d0f61a05b | 75 | return size; |
HannesTschofenig | 0:796d0f61a05b | 76 | } |
HannesTschofenig | 0:796d0f61a05b | 77 | |
HannesTschofenig | 0:796d0f61a05b | 78 | inline rsa_context* d2i_RSA_PUBKEY( void *ignore, unsigned char **bufptr, |
HannesTschofenig | 0:796d0f61a05b | 79 | int len ) |
HannesTschofenig | 0:796d0f61a05b | 80 | { |
HannesTschofenig | 0:796d0f61a05b | 81 | unsigned char *buffer = *(unsigned char **) bufptr; |
HannesTschofenig | 0:796d0f61a05b | 82 | rsa_context *rsa; |
HannesTschofenig | 0:796d0f61a05b | 83 | |
HannesTschofenig | 0:796d0f61a05b | 84 | /* |
HannesTschofenig | 0:796d0f61a05b | 85 | * Not a general-purpose parser: only parses public key from *exactly* |
HannesTschofenig | 0:796d0f61a05b | 86 | * openssl genrsa -out privkey.pem 512 (or 1024) |
HannesTschofenig | 0:796d0f61a05b | 87 | * openssl rsa -in privkey.pem -out privatekey.der -outform der |
HannesTschofenig | 0:796d0f61a05b | 88 | * openssl rsa -in privkey.pem -out pubkey.der -outform der -pubout |
HannesTschofenig | 0:796d0f61a05b | 89 | * |
HannesTschofenig | 0:796d0f61a05b | 90 | * TODO: make a general-purpose parse |
HannesTschofenig | 0:796d0f61a05b | 91 | */ |
HannesTschofenig | 0:796d0f61a05b | 92 | if( ignore != 0 || ( len != 94 && len != 162 ) ) |
HannesTschofenig | 0:796d0f61a05b | 93 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 94 | |
HannesTschofenig | 0:796d0f61a05b | 95 | rsa = (rsa_context *) malloc( sizeof( rsa_rsa ) ); |
HannesTschofenig | 0:796d0f61a05b | 96 | if( rsa == NULL ) |
HannesTschofenig | 0:796d0f61a05b | 97 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 98 | |
HannesTschofenig | 0:796d0f61a05b | 99 | memset( rsa, 0, sizeof( rsa_context ) ); |
HannesTschofenig | 0:796d0f61a05b | 100 | |
HannesTschofenig | 0:796d0f61a05b | 101 | if( ( len == 94 && |
HannesTschofenig | 0:796d0f61a05b | 102 | mpi_read_binary( &rsa->N, &buffer[ 25], 64 ) == 0 && |
HannesTschofenig | 0:796d0f61a05b | 103 | mpi_read_binary( &rsa->E, &buffer[ 91], 3 ) == 0 ) || |
HannesTschofenig | 0:796d0f61a05b | 104 | ( len == 162 && |
HannesTschofenig | 0:796d0f61a05b | 105 | mpi_read_binary( &rsa->N, &buffer[ 29], 128 ) == 0 ) && |
HannesTschofenig | 0:796d0f61a05b | 106 | mpi_read_binary( &rsa->E, &buffer[159], 3 ) == 0 ) |
HannesTschofenig | 0:796d0f61a05b | 107 | { |
HannesTschofenig | 0:796d0f61a05b | 108 | /* |
HannesTschofenig | 0:796d0f61a05b | 109 | * key read successfully |
HannesTschofenig | 0:796d0f61a05b | 110 | */ |
HannesTschofenig | 0:796d0f61a05b | 111 | rsa->len = ( mpi_msb( &rsa->N ) + 7 ) >> 3; |
HannesTschofenig | 0:796d0f61a05b | 112 | return( rsa ); |
HannesTschofenig | 0:796d0f61a05b | 113 | } |
HannesTschofenig | 0:796d0f61a05b | 114 | else |
HannesTschofenig | 0:796d0f61a05b | 115 | { |
HannesTschofenig | 0:796d0f61a05b | 116 | memset( rsa, 0, sizeof( rsa_context ) ); |
HannesTschofenig | 0:796d0f61a05b | 117 | free( rsa ); |
HannesTschofenig | 0:796d0f61a05b | 118 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 119 | } |
HannesTschofenig | 0:796d0f61a05b | 120 | } |
HannesTschofenig | 0:796d0f61a05b | 121 | |
HannesTschofenig | 0:796d0f61a05b | 122 | #define RSA rsa_context |
HannesTschofenig | 0:796d0f61a05b | 123 | #define RSA_PKCS1_PADDING 1 /* ignored; always encrypt with this */ |
HannesTschofenig | 0:796d0f61a05b | 124 | #define RSA_size( CTX ) (CTX)->len |
HannesTschofenig | 0:796d0f61a05b | 125 | #define RSA_free( CTX ) rsa_free( CTX ) |
HannesTschofenig | 0:796d0f61a05b | 126 | #define ERR_get_error( ) "ERR_get_error() not supported" |
HannesTschofenig | 0:796d0f61a05b | 127 | #define RSA_blinding_off( IGNORE ) |
HannesTschofenig | 0:796d0f61a05b | 128 | |
HannesTschofenig | 0:796d0f61a05b | 129 | #define d2i_RSAPrivateKey( a, b, c ) new rsa_context /* TODO: C++ bleh */ |
HannesTschofenig | 0:796d0f61a05b | 130 | |
HannesTschofenig | 0:796d0f61a05b | 131 | inline int RSA_public_decrypt ( int size, unsigned char* input, unsigned char* output, RSA* key, int ignore ) { int outsize=size; if( !rsa_pkcs1_decrypt( key, RSA_PUBLIC, &outsize, input, output ) ) return outsize; else return -1; } |
HannesTschofenig | 0:796d0f61a05b | 132 | inline int RSA_private_decrypt( int size, unsigned char* input, unsigned char* output, RSA* key, int ignore ) { int outsize=size; if( !rsa_pkcs1_decrypt( key, RSA_PRIVATE, &outsize, input, output ) ) return outsize; else return -1; } |
HannesTschofenig | 0:796d0f61a05b | 133 | inline int RSA_public_encrypt ( int size, unsigned char* input, unsigned char* output, RSA* key, int ignore ) { if( !rsa_pkcs1_encrypt( key, RSA_PUBLIC, size, input, output ) ) return RSA_size(key); else return -1; } |
HannesTschofenig | 0:796d0f61a05b | 134 | inline int RSA_private_encrypt( int size, unsigned char* input, unsigned char* output, RSA* key, int ignore ) { if( !rsa_pkcs1_encrypt( key, RSA_PRIVATE, size, input, output ) ) return RSA_size(key); else return -1; } |
HannesTschofenig | 0:796d0f61a05b | 135 | |
HannesTschofenig | 0:796d0f61a05b | 136 | #ifdef __cplusplus |
HannesTschofenig | 0:796d0f61a05b | 137 | } |
HannesTschofenig | 0:796d0f61a05b | 138 | #endif |
HannesTschofenig | 0:796d0f61a05b | 139 | |
HannesTschofenig | 0:796d0f61a05b | 140 | #endif /* openssl.h */ |
HannesTschofenig | 0:796d0f61a05b | 141 | |
HannesTschofenig | 0:796d0f61a05b | 142 |