Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file pk.h |
| 3 | * |
| 4 | * \brief Public Key abstraction layer |
| 5 | * |
| 6 | * Copyright (C) 2006-2013, Brainspark B.V. |
| 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 10 | * |
| 11 | * All rights reserved. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | */ |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 27 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 28 | #ifndef POLARSSL_PK_H |
| 29 | #define POLARSSL_PK_H |
| 30 | |
Manuel Pégourié-Gonnard | 244569f | 2013-07-10 09:46:30 +0200 | [diff] [blame] | 31 | #include "config.h" |
| 32 | |
| 33 | #if defined(POLARSSL_RSA_C) |
| 34 | #include "rsa.h" |
| 35 | #endif |
| 36 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 37 | #if defined(POLARSSL_ECP_C) |
| 38 | #include "ecp.h" |
| 39 | #endif |
| 40 | |
Manuel Pégourié-Gonnard | 211a64c | 2013-08-09 15:04:26 +0200 | [diff] [blame] | 41 | #if defined(POLARSSL_ECDSA_C) |
| 42 | #include "ecdsa.h" |
| 43 | #endif |
| 44 | |
Manuel Pégourié-Gonnard | 7a6c946 | 2013-07-09 10:04:07 +0200 | [diff] [blame] | 45 | #define POLARSSL_ERR_PK_MALLOC_FAILED -0x2F80 /**< Memory alloation failed. */ |
Manuel Pégourié-Gonnard | 1569938 | 2013-08-14 19:22:48 +0200 | [diff] [blame] | 46 | #define POLARSSL_ERR_PK_TYPE_MISMATCH -0x2F00 /**< Type mismatch, eg attempt to use a RSA key as EC, or to modify key type. */ |
| 47 | #define POLARSSL_ERR_PK_BAD_INPUT_DATA -0x2E80 /**< Bad input parameters to function. */ |
Manuel Pégourié-Gonnard | 7a6c946 | 2013-07-09 10:04:07 +0200 | [diff] [blame] | 48 | |
Manuel Pégourié-Gonnard | 360a583 | 2013-07-10 14:56:36 +0200 | [diff] [blame] | 49 | #if defined(POLARSSL_RSA_C) |
| 50 | /** |
| 51 | * Quick access to an RSA context inside a PK context. |
| 52 | * |
| 53 | * \warning You must make sure the PK context actually holds an RSA context |
| 54 | * before using this macro! |
| 55 | */ |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 56 | #define pk_rsa( pk ) ( (rsa_context *) (pk).pk_ctx ) |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 57 | #endif /* POLARSSL_RSA_C */ |
Manuel Pégourié-Gonnard | 360a583 | 2013-07-10 14:56:36 +0200 | [diff] [blame] | 58 | |
| 59 | #if defined(POLARSSL_ECP_C) |
| 60 | /** |
| 61 | * Quick access to an EC context inside a PK context. |
| 62 | * |
| 63 | * \warning You must make sure the PK context actually holds an EC context |
| 64 | * before using this macro! |
| 65 | */ |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 66 | #define pk_ec( pk ) ( (ecp_keypair *) (pk).pk_ctx ) |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 67 | #endif /* POLARSSL_ECP_C */ |
Manuel Pégourié-Gonnard | 360a583 | 2013-07-10 14:56:36 +0200 | [diff] [blame] | 68 | |
| 69 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 70 | #ifdef __cplusplus |
| 71 | extern "C" { |
| 72 | #endif |
| 73 | |
| 74 | /** |
| 75 | * \brief Public key types |
| 76 | */ |
| 77 | typedef enum { |
| 78 | POLARSSL_PK_NONE=0, |
| 79 | POLARSSL_PK_RSA, |
Manuel Pégourié-Gonnard | 5a9b82e | 2013-07-01 16:57:44 +0200 | [diff] [blame] | 80 | POLARSSL_PK_ECKEY, |
| 81 | POLARSSL_PK_ECKEY_DH, |
Manuel Pégourié-Gonnard | 1e60cd0 | 2013-07-10 10:28:53 +0200 | [diff] [blame] | 82 | POLARSSL_PK_ECDSA, |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 83 | } pk_type_t; |
| 84 | |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 85 | /** |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 86 | * \brief Types for interfacing with the debug module |
| 87 | */ |
| 88 | typedef enum |
| 89 | { |
| 90 | POLARSSL_PK_DEBUG_NONE = 0, |
| 91 | POLARSSL_PK_DEBUG_MPI, |
| 92 | POLARSSL_PK_DEBUG_ECP, |
| 93 | } pk_debug_type; |
| 94 | |
| 95 | /** |
| 96 | * \brief Item to send to the debug module |
| 97 | */ |
| 98 | typedef struct |
| 99 | { |
| 100 | pk_debug_type type; |
| 101 | char *name; |
| 102 | void *value; |
| 103 | } pk_debug_item; |
| 104 | |
| 105 | /** Maximum number of item send for debugging, plus 1 */ |
| 106 | #define POLARSSL_PK_DEBUG_MAX_ITEMS 3 |
| 107 | |
| 108 | /** |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 109 | * \brief Public key information and operations |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 110 | */ |
| 111 | typedef struct |
| 112 | { |
| 113 | /** Public key type */ |
| 114 | pk_type_t type; |
| 115 | |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 116 | /** Type name */ |
| 117 | const char *name; |
| 118 | |
| 119 | /** Get key size in bits */ |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 120 | size_t (*get_size)( const void * ); |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 121 | |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 122 | /** Tell if the context implements this type (eg ECKEY can do ECDSA) */ |
| 123 | int (*can_do)( pk_type_t type ); |
| 124 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 125 | /** Verify signature */ |
| 126 | int (*verify_func)( void *ctx, |
| 127 | const unsigned char *hash, const md_info_t *md_info, |
| 128 | const unsigned char *sig, size_t sig_len ); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 129 | |
| 130 | /** Allocate a new context */ |
| 131 | void * (*ctx_alloc_func)( void ); |
| 132 | |
| 133 | /** Free the given context */ |
| 134 | void (*ctx_free_func)( void *ctx ); |
| 135 | |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 136 | /** Interface with the debug module */ |
| 137 | void (*debug_func)( const void *ctx, pk_debug_item *items ); |
| 138 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 139 | } pk_info_t; |
| 140 | |
| 141 | /** |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 142 | * \brief Public key container |
| 143 | */ |
| 144 | typedef struct |
| 145 | { |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 146 | const pk_info_t * pk_info; /**< Public key informations */ |
| 147 | void * pk_ctx; /**< Underlying public key context */ |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 148 | } pk_context; |
| 149 | |
| 150 | /** |
Manuel Pégourié-Gonnard | ab46694 | 2013-08-15 11:30:27 +0200 | [diff] [blame^] | 151 | * \brief Return information associated with the given PK type |
| 152 | * |
| 153 | * \param type PK type to search for. |
| 154 | * |
| 155 | * \return The PK info associated with the type or NULL if not found. |
| 156 | */ |
| 157 | const pk_info_t *pk_info_from_type( pk_type_t pk_type ); |
| 158 | |
| 159 | /** |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 160 | * \brief Initialize a pk_context (as NONE) |
| 161 | */ |
| 162 | void pk_init( pk_context *ctx ); |
| 163 | |
| 164 | /** |
Manuel Pégourié-Gonnard | ab46694 | 2013-08-15 11:30:27 +0200 | [diff] [blame^] | 165 | * \brief Initialize a PK context with the information given |
| 166 | * and allocates the type-specific PK subcontext. |
| 167 | * |
| 168 | * \param ctx Context to initialize. Must be empty (type NONE). |
| 169 | * \param info Information to use |
| 170 | * |
| 171 | * \return 0 on success, |
| 172 | * POLARSSL_ERR_PK_BAD_INPUT_DATA on invalid input, |
| 173 | * POLARSSL_ERR_PK_MALLOC_FAILED on allocation failure. |
| 174 | */ |
| 175 | int pk_init_ctx( pk_context *ctx, const pk_info_t *info ); |
| 176 | |
| 177 | /** |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 178 | * \brief Free a pk_context |
| 179 | */ |
| 180 | void pk_free( pk_context *ctx ); |
| 181 | |
| 182 | /** |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 183 | * \brief Get the size in bits of the underlying key |
| 184 | * |
| 185 | * \param ctx Context to use |
| 186 | * |
| 187 | * \return Key size in bits, or 0 on error |
| 188 | */ |
| 189 | size_t pk_get_size( const pk_context *ctx ); |
| 190 | |
| 191 | /** |
| 192 | * \brief Tell if a context can do the operation given by type |
| 193 | * |
| 194 | * \param ctx Context to test |
| 195 | * \param type Target type |
| 196 | * |
| 197 | * \return 0 if context can't do the operations, |
| 198 | * 1 otherwise. |
| 199 | */ |
| 200 | int pk_can_do( pk_context *ctx, pk_type_t type ); |
| 201 | |
| 202 | /** |
| 203 | * \brief Verify signature |
| 204 | * |
| 205 | * \param ctx PK context to use |
| 206 | * \param hash Hash of the message to sign |
| 207 | * \param md_info Information about the hash function used |
| 208 | * \param sig Signature to verify |
| 209 | * \param sig_len Signature length |
| 210 | * |
| 211 | * \return 0 on success (signature is valid), |
| 212 | * or a specific error code. |
| 213 | */ |
| 214 | int pk_verify( pk_context *ctx, |
| 215 | const unsigned char *hash, const md_info_t *md_info, |
| 216 | const unsigned char *sig, size_t sig_len ); |
| 217 | |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 218 | /** |
| 219 | * \brief Export debug information |
| 220 | * |
| 221 | * \param ctx Context to use |
| 222 | * \param items Place to write debug items |
| 223 | * |
| 224 | * \return 0 on sucess or POLARSSL_ERR_PK_BAD_INPUT_DATA |
| 225 | */ |
| 226 | int pk_debug( const pk_context *ctx, pk_debug_item *items ); |
| 227 | |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 228 | /** |
| 229 | * \brief Access the type name |
| 230 | * |
| 231 | * \param ctx Context to use |
| 232 | * |
| 233 | * \return Type name on success, or "invalid PK" |
| 234 | */ |
| 235 | const char * pk_get_name( const pk_context *ctx ); |
| 236 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 237 | #ifdef __cplusplus |
| 238 | } |
| 239 | #endif |
| 240 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 241 | #endif /* POLARSSL_PK_H */ |