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

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pk.c Source File

pk.c

00001 /*
00002  *  Public Key abstraction layer
00003  *
00004  *  Copyright (C) 2006-2013, Brainspark B.V.
00005  *
00006  *  This file is part of PolarSSL (http://www.polarssl.org)
00007  *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
00008  *
00009  *  All rights reserved.
00010  *
00011  *  This program is free software; you can redistribute it and/or modify
00012  *  it under the terms of the GNU General Public License as published by
00013  *  the Free Software Foundation; either version 2 of the License, or
00014  *  (at your option) any later version.
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU General Public License for more details.
00020  *
00021  *  You should have received a copy of the GNU General Public License along
00022  *  with this program; if not, write to the Free Software Foundation, Inc.,
00023  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00024  */
00025 
00026 #if !defined(POLARSSL_CONFIG_FILE)
00027 #include "polarssl/config.h"
00028 #else
00029 #include POLARSSL_CONFIG_FILE
00030 #endif
00031 
00032 #if defined(POLARSSL_PK_C)
00033 
00034 #include "polarssl/pk.h"
00035 #include "polarssl/pk_wrap.h"
00036 
00037 #if defined(POLARSSL_RSA_C)
00038 #include "polarssl/rsa.h"
00039 #endif
00040 #if defined(POLARSSL_ECP_C)
00041 #include "polarssl/ecp.h"
00042 #endif
00043 #if defined(POLARSSL_ECDSA_C)
00044 #include "polarssl/ecdsa.h"
00045 #endif
00046 
00047 /*
00048  * Initialise a pk_context
00049  */
00050 void pk_init( pk_context *ctx )
00051 {
00052     if( ctx == NULL )
00053         return;
00054 
00055     ctx->pk_info = NULL;
00056     ctx->pk_ctx = NULL;
00057 }
00058 
00059 /*
00060  * Free (the components of) a pk_context
00061  */
00062 void pk_free( pk_context *ctx )
00063 {
00064     if( ctx == NULL || ctx->pk_info == NULL)
00065         return;
00066 
00067     ctx->pk_info->ctx_free_func( ctx->pk_ctx );
00068     ctx->pk_ctx = NULL;
00069 
00070     ctx->pk_info = NULL;
00071 }
00072 
00073 /*
00074  * Get pk_info structure from type
00075  */
00076 const pk_info_t * pk_info_from_type( pk_type_t pk_type )
00077 {
00078     switch( pk_type ) {
00079 #if defined(POLARSSL_RSA_C)
00080         case POLARSSL_PK_RSA:
00081             return &rsa_info;
00082 #endif
00083 #if defined(POLARSSL_ECP_C)
00084         case POLARSSL_PK_ECKEY:
00085             return &eckey_info;
00086         case POLARSSL_PK_ECKEY_DH:
00087             return &eckeydh_info;
00088 #endif
00089 #if defined(POLARSSL_ECDSA_C)
00090         case POLARSSL_PK_ECDSA:
00091             return &ecdsa_info;
00092 #endif
00093         /* POLARSSL_PK_RSA_ALT omitted on purpose */
00094         default:
00095             return NULL;
00096     }
00097 }
00098 
00099 /*
00100  * Initialise context
00101  */
00102 int pk_init_ctx( pk_context *ctx, const pk_info_t *info )
00103 {
00104     if( ctx == NULL || info == NULL || ctx->pk_info != NULL )
00105         return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
00106 
00107     if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
00108         return( POLARSSL_ERR_PK_MALLOC_FAILED );
00109 
00110     ctx->pk_info = info;
00111 
00112     return( 0 );
00113 }
00114 
00115 /*
00116  * Initialize an RSA-alt context
00117  */
00118 int pk_init_ctx_rsa_alt( pk_context *ctx, void * key,
00119                          pk_rsa_alt_decrypt_func decrypt_func,
00120                          pk_rsa_alt_sign_func sign_func,
00121                          pk_rsa_alt_key_len_func key_len_func )
00122 {
00123     rsa_alt_context *rsa_alt;
00124     const pk_info_t *info = &rsa_alt_info;
00125 
00126     if( ctx == NULL || ctx->pk_info != NULL )
00127         return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
00128 
00129     if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
00130         return( POLARSSL_ERR_PK_MALLOC_FAILED );
00131 
00132     ctx->pk_info = info;
00133 
00134     rsa_alt = (rsa_alt_context *) ctx->pk_ctx;
00135 
00136     rsa_alt->key = key;
00137     rsa_alt->decrypt_func = decrypt_func;
00138     rsa_alt->sign_func = sign_func;
00139     rsa_alt->key_len_func = key_len_func;
00140 
00141     return( 0 );
00142 }
00143 
00144 /*
00145  * Tell if a PK can do the operations of the given type
00146  */
00147 int pk_can_do( pk_context *ctx, pk_type_t type )
00148 {
00149     /* null or NONE context can't do anything */
00150     if( ctx == NULL || ctx->pk_info == NULL )
00151         return( 0 );
00152 
00153     return( ctx->pk_info->can_do( type ) );
00154 }
00155 
00156 /*
00157  * Helper for pk_sign and pk_verify
00158  */
00159 static inline int pk_hashlen_helper( md_type_t md_alg, size_t *hash_len )
00160 {
00161     const md_info_t *md_info;
00162 
00163     if( *hash_len != 0 )
00164         return( 0 );
00165 
00166     if( ( md_info = md_info_from_type( md_alg ) ) == NULL )
00167         return( -1 );
00168 
00169     *hash_len = md_info->size;
00170     return( 0 );
00171 }
00172 
00173 /*
00174  * Verify a signature
00175  */
00176 int pk_verify( pk_context *ctx, md_type_t md_alg,
00177                const unsigned char *hash, size_t hash_len,
00178                const unsigned char *sig, size_t sig_len )
00179 {
00180     if( ctx == NULL || ctx->pk_info == NULL ||
00181         pk_hashlen_helper( md_alg, &hash_len ) != 0 )
00182         return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
00183 
00184     if( ctx->pk_info->verify_func == NULL )
00185         return( POLARSSL_ERR_PK_TYPE_MISMATCH );
00186 
00187     return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
00188                                        sig, sig_len ) );
00189 }
00190 
00191 /*
00192  * Make a signature
00193  */
00194 int pk_sign( pk_context *ctx, md_type_t md_alg,
00195              const unsigned char *hash, size_t hash_len,
00196              unsigned char *sig, size_t *sig_len,
00197              int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
00198 {
00199     if( ctx == NULL || ctx->pk_info == NULL ||
00200         pk_hashlen_helper( md_alg, &hash_len ) != 0 )
00201         return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
00202 
00203     if( ctx->pk_info->sign_func == NULL )
00204         return( POLARSSL_ERR_PK_TYPE_MISMATCH );
00205 
00206     return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
00207                                      sig, sig_len, f_rng, p_rng ) );
00208 }
00209 
00210 /*
00211  * Decrypt message
00212  */
00213 int pk_decrypt( pk_context *ctx,
00214                 const unsigned char *input, size_t ilen,
00215                 unsigned char *output, size_t *olen, size_t osize,
00216                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
00217 {
00218     if( ctx == NULL || ctx->pk_info == NULL )
00219         return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
00220 
00221     if( ctx->pk_info->decrypt_func == NULL )
00222         return( POLARSSL_ERR_PK_TYPE_MISMATCH );
00223 
00224     return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
00225                 output, olen, osize, f_rng, p_rng ) );
00226 }
00227 
00228 /*
00229  * Encrypt message
00230  */
00231 int pk_encrypt( pk_context *ctx,
00232                 const unsigned char *input, size_t ilen,
00233                 unsigned char *output, size_t *olen, size_t osize,
00234                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
00235 {
00236     if( ctx == NULL || ctx->pk_info == NULL )
00237         return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
00238 
00239     if( ctx->pk_info->encrypt_func == NULL )
00240         return( POLARSSL_ERR_PK_TYPE_MISMATCH );
00241 
00242     return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
00243                 output, olen, osize, f_rng, p_rng ) );
00244 }
00245 
00246 /*
00247  * Get key size in bits
00248  */
00249 size_t pk_get_size( const pk_context *ctx )
00250 {
00251     if( ctx == NULL || ctx->pk_info == NULL )
00252         return( 0 );
00253 
00254     return( ctx->pk_info->get_size( ctx->pk_ctx ) );
00255 }
00256 
00257 /*
00258  * Export debug information
00259  */
00260 int pk_debug( const pk_context *ctx, pk_debug_item *items )
00261 {
00262     if( ctx == NULL || ctx->pk_info == NULL )
00263         return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
00264 
00265     if( ctx->pk_info->debug_func == NULL )
00266         return( POLARSSL_ERR_PK_TYPE_MISMATCH );
00267 
00268     ctx->pk_info->debug_func( ctx->pk_ctx, items );
00269     return( 0 );
00270 }
00271 
00272 /*
00273  * Access the PK type name
00274  */
00275 const char * pk_get_name( const pk_context *ctx )
00276 {
00277     if( ctx == NULL || ctx->pk_info == NULL )
00278         return( "invalid PK" );
00279 
00280     return( ctx->pk_info->name );
00281 }
00282 
00283 /*
00284  * Access the PK type
00285  */
00286 pk_type_t pk_get_type( const pk_context *ctx )
00287 {
00288     if( ctx == NULL || ctx->pk_info == NULL )
00289         return( POLARSSL_PK_NONE );
00290 
00291     return( ctx->pk_info->type );
00292 }
00293 
00294 #endif /* POLARSSL_PK_C */
00295 
00296