Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Public Key abstraction layer |
| 3 | * |
| 4 | * Copyright (C) 2006-2013, Brainspark B.V. |
| 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | */ |
| 25 | |
| 26 | #include "polarssl/config.h" |
| 27 | |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 28 | #include "polarssl/pk.h" |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 29 | #include "polarssl/pk_wrap.h" |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 30 | |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 31 | #if defined(POLARSSL_RSA_C) |
| 32 | #include "polarssl/rsa.h" |
| 33 | #endif |
| 34 | #if defined(POLARSSL_ECP_C) |
| 35 | #include "polarssl/ecp.h" |
| 36 | #endif |
Manuel Pégourié-Gonnard | 7c5819e | 2013-07-10 12:29:57 +0200 | [diff] [blame] | 37 | #if defined(POLARSSL_ECDSA_C) |
| 38 | #include "polarssl/ecdsa.h" |
| 39 | #endif |
Manuel Pégourié-Gonnard | 81c313c | 2013-07-09 10:35:54 +0200 | [diff] [blame] | 40 | |
Paul Bakker | d9ca94a | 2013-07-25 11:25:09 +0200 | [diff] [blame] | 41 | #if defined(POLARSSL_MEMORY_C) |
| 42 | #include "polarssl/memory.h" |
| 43 | #else |
| 44 | #define polarssl_malloc malloc |
| 45 | #define polarssl_free free |
| 46 | #endif |
| 47 | |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 48 | #include <stdlib.h> |
| 49 | |
| 50 | /* |
| 51 | * Initialise a pk_context |
| 52 | */ |
| 53 | void pk_init( pk_context *ctx ) |
| 54 | { |
| 55 | if( ctx == NULL ) |
| 56 | return; |
| 57 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 58 | ctx->info = NULL; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 59 | ctx->type = POLARSSL_PK_NONE; |
| 60 | ctx->data = NULL; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Free (the components of) a pk_context |
| 65 | */ |
| 66 | void pk_free( pk_context *ctx ) |
| 67 | { |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 68 | if( ctx == NULL || ctx->info == NULL) |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 69 | return; |
| 70 | |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 71 | ctx->info->ctx_free_func( ctx->data ); |
| 72 | ctx->data = NULL; |
Manuel Pégourié-Gonnard | 1f73a65 | 2013-07-09 10:26:41 +0200 | [diff] [blame] | 73 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 74 | ctx->info = NULL; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 75 | ctx->type = POLARSSL_PK_NONE; |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Get pk_info structure from type |
| 80 | */ |
| 81 | static const pk_info_t * pk_info_from_type( pk_type_t pk_type ) |
| 82 | { |
| 83 | switch( pk_type ) { |
| 84 | #if defined(POLARSSL_RSA_C) |
| 85 | case POLARSSL_PK_RSA: |
| 86 | return &rsa_info; |
| 87 | #endif |
| 88 | #if defined(POLARSSL_ECP_C) |
| 89 | case POLARSSL_PK_ECKEY: |
| 90 | return &eckey_info; |
| 91 | case POLARSSL_PK_ECKEY_DH: |
| 92 | return &eckeydh_info; |
| 93 | #endif |
| 94 | #if defined(POLARSSL_ECDSA_C) |
| 95 | case POLARSSL_PK_ECDSA: |
| 96 | return &ecdsa_info; |
| 97 | #endif |
| 98 | default: |
| 99 | return NULL; |
| 100 | } |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Set a pk_context to a given type |
| 105 | */ |
| 106 | int pk_set_type( pk_context *ctx, pk_type_t type ) |
| 107 | { |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 108 | const pk_info_t *info; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 109 | |
Manuel Pégourié-Gonnard | 374e4b8 | 2013-07-09 10:21:34 +0200 | [diff] [blame] | 110 | if( ctx->type == type ) |
| 111 | return( 0 ); |
| 112 | |
| 113 | if( ctx->type != POLARSSL_PK_NONE ) |
| 114 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
| 115 | |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 116 | if( ( info = pk_info_from_type( type ) ) == NULL ) |
Manuel Pégourié-Gonnard | c2c9003 | 2013-07-15 11:04:58 +0200 | [diff] [blame] | 117 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 118 | |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 119 | if( ( ctx->data = info->ctx_alloc_func() ) == NULL ) |
Manuel Pégourié-Gonnard | 7a6c946 | 2013-07-09 10:04:07 +0200 | [diff] [blame] | 120 | return( POLARSSL_ERR_PK_MALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 121 | |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 122 | ctx->type = type; |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 123 | ctx->info = info; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 124 | |
| 125 | return( 0 ); |
| 126 | } |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame^] | 127 | |
| 128 | /* |
| 129 | * Tell if a PK can do the operations of the given type |
| 130 | */ |
| 131 | int pk_can_do( pk_context *ctx, pk_type_t type ) |
| 132 | { |
| 133 | /* null of NONE context can't do anything */ |
| 134 | if( ctx == NULL || ctx->info == NULL ) |
| 135 | return( 0 ); |
| 136 | |
| 137 | return( ctx->info->can_do( type ) ); |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Verify a signature |
| 142 | */ |
| 143 | int pk_verify( pk_context *ctx, |
| 144 | const unsigned char *hash, const md_info_t *md_info, |
| 145 | const unsigned char *sig, size_t sig_len ) |
| 146 | { |
| 147 | if( ctx == NULL || ctx->info == NULL ) |
| 148 | return( POLARSSL_ERR_PK_TYPE_MISMATCH ); // TODO |
| 149 | |
| 150 | return( ctx->info->verify_func( ctx->data, hash, md_info, sig, sig_len ) ); |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Get key size in bits |
| 155 | */ |
| 156 | size_t pk_get_size( const pk_context *ctx ) |
| 157 | { |
| 158 | if( ctx == NULL || ctx->info == NULL ) |
| 159 | return( 0 ); |
| 160 | |
| 161 | return( ctx->info->get_size( ctx->data ) ); |
| 162 | } |