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 | 374e4b8 | 2013-07-09 10:21:34 +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 */ |
Manuel Pégourié-Gonnard | 7a6c946 | 2013-07-09 10:04:07 +0200 | [diff] [blame] | 47 | |
Manuel Pégourié-Gonnard | 360a583 | 2013-07-10 14:56:36 +0200 | [diff] [blame] | 48 | #if defined(POLARSSL_RSA_C) |
| 49 | /** |
| 50 | * Quick access to an RSA context inside a PK context. |
| 51 | * |
| 52 | * \warning You must make sure the PK context actually holds an RSA context |
| 53 | * before using this macro! |
| 54 | */ |
Manuel Pégourié-Gonnard | a2d4e64 | 2013-07-11 13:59:02 +0200 | [diff] [blame] | 55 | #define pk_rsa( pk ) ( (rsa_context *) (pk).data ) |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 56 | #endif /* POLARSSL_RSA_C */ |
Manuel Pégourié-Gonnard | 360a583 | 2013-07-10 14:56:36 +0200 | [diff] [blame] | 57 | |
| 58 | #if defined(POLARSSL_ECP_C) |
| 59 | /** |
| 60 | * Quick access to an EC context inside a PK context. |
| 61 | * |
| 62 | * \warning You must make sure the PK context actually holds an EC context |
| 63 | * before using this macro! |
| 64 | */ |
Manuel Pégourié-Gonnard | a2d4e64 | 2013-07-11 13:59:02 +0200 | [diff] [blame] | 65 | #define pk_ec( pk ) ( (ecp_keypair *) (pk).data ) |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 66 | #endif /* POLARSSL_ECP_C */ |
Manuel Pégourié-Gonnard | 360a583 | 2013-07-10 14:56:36 +0200 | [diff] [blame] | 67 | |
| 68 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 69 | #ifdef __cplusplus |
| 70 | extern "C" { |
| 71 | #endif |
| 72 | |
| 73 | /** |
| 74 | * \brief Public key types |
| 75 | */ |
| 76 | typedef enum { |
| 77 | POLARSSL_PK_NONE=0, |
| 78 | POLARSSL_PK_RSA, |
Manuel Pégourié-Gonnard | 5a9b82e | 2013-07-01 16:57:44 +0200 | [diff] [blame] | 79 | POLARSSL_PK_ECKEY, |
| 80 | POLARSSL_PK_ECKEY_DH, |
Manuel Pégourié-Gonnard | 1e60cd0 | 2013-07-10 10:28:53 +0200 | [diff] [blame] | 81 | POLARSSL_PK_ECDSA, |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 82 | } pk_type_t; |
| 83 | |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 84 | /** |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame^] | 85 | * \brief Types for interfacing with the debug module |
| 86 | */ |
| 87 | typedef enum |
| 88 | { |
| 89 | POLARSSL_PK_DEBUG_NONE = 0, |
| 90 | POLARSSL_PK_DEBUG_MPI, |
| 91 | POLARSSL_PK_DEBUG_ECP, |
| 92 | } pk_debug_type; |
| 93 | |
| 94 | /** |
| 95 | * \brief Item to send to the debug module |
| 96 | */ |
| 97 | typedef struct |
| 98 | { |
| 99 | pk_debug_type type; |
| 100 | char *name; |
| 101 | void *value; |
| 102 | } pk_debug_item; |
| 103 | |
| 104 | /** Maximum number of item send for debugging, plus 1 */ |
| 105 | #define POLARSSL_PK_DEBUG_MAX_ITEMS 3 |
| 106 | |
| 107 | /** |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 108 | * \brief Public key info |
| 109 | */ |
| 110 | typedef struct |
| 111 | { |
| 112 | /** Public key type */ |
| 113 | pk_type_t type; |
| 114 | |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 115 | /** Type name */ |
| 116 | const char *name; |
| 117 | |
| 118 | /** Get key size in bits */ |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 119 | size_t (*get_size)( const void * ); |
Manuel Pégourié-Gonnard | f8c948a | 2013-08-12 19:45:32 +0200 | [diff] [blame] | 120 | |
Manuel Pégourié-Gonnard | f18c3e0 | 2013-08-12 18:41:18 +0200 | [diff] [blame] | 121 | /** Tell if the context implements this type (eg ECKEY can do ECDSA) */ |
| 122 | int (*can_do)( pk_type_t type ); |
| 123 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 124 | /** Verify signature */ |
| 125 | int (*verify_func)( void *ctx, |
| 126 | const unsigned char *hash, const md_info_t *md_info, |
| 127 | const unsigned char *sig, size_t sig_len ); |
Manuel Pégourié-Gonnard | 765db07 | 2013-08-14 15:00:27 +0200 | [diff] [blame] | 128 | |
| 129 | /** Allocate a new context */ |
| 130 | void * (*ctx_alloc_func)( void ); |
| 131 | |
| 132 | /** Free the given context */ |
| 133 | void (*ctx_free_func)( void *ctx ); |
| 134 | |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame^] | 135 | /** Interface with the debug module */ |
| 136 | void (*debug_func)( const void *ctx, pk_debug_item *items ); |
| 137 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 138 | } pk_info_t; |
| 139 | |
| 140 | /** |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 141 | * \brief Public key container |
| 142 | */ |
| 143 | typedef struct |
| 144 | { |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 145 | const pk_info_t * info; /**< Public key informations */ |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 146 | void * data; /**< Public key data */ |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 147 | } pk_context; |
| 148 | |
| 149 | /** |
| 150 | * \brief Initialize a pk_context (as NONE) |
| 151 | */ |
| 152 | void pk_init( pk_context *ctx ); |
| 153 | |
| 154 | /** |
| 155 | * \brief Free a pk_context |
| 156 | */ |
| 157 | void pk_free( pk_context *ctx ); |
| 158 | |
| 159 | /** |
| 160 | * \brief Set a pk_context to a given type |
| 161 | * |
| 162 | * \param ctx Context to initialize |
| 163 | * \param type Type of key |
| 164 | * |
Manuel Pégourié-Gonnard | 374e4b8 | 2013-07-09 10:21:34 +0200 | [diff] [blame] | 165 | * \note Once the type of a key has been set, it cannot be reset. |
| 166 | * If you want to do so, you need to use pk_free() first. |
| 167 | * |
| 168 | * \return O on success, |
| 169 | * POLARSSL_ERR_PK_MALLOC_FAILED on memory allocation fail, |
| 170 | * POLARSSL_ERR_PK_TYPE_MISMATCH on attempts to reset type. |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 171 | */ |
| 172 | int pk_set_type( pk_context *ctx, pk_type_t type ); |
| 173 | |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 174 | /** |
| 175 | * \brief Get the size in bits of the underlying key |
| 176 | * |
| 177 | * \param ctx Context to use |
| 178 | * |
| 179 | * \return Key size in bits, or 0 on error |
| 180 | */ |
| 181 | size_t pk_get_size( const pk_context *ctx ); |
| 182 | |
| 183 | /** |
| 184 | * \brief Tell if a context can do the operation given by type |
| 185 | * |
| 186 | * \param ctx Context to test |
| 187 | * \param type Target type |
| 188 | * |
| 189 | * \return 0 if context can't do the operations, |
| 190 | * 1 otherwise. |
| 191 | */ |
| 192 | int pk_can_do( pk_context *ctx, pk_type_t type ); |
| 193 | |
| 194 | /** |
| 195 | * \brief Verify signature |
| 196 | * |
| 197 | * \param ctx PK context to use |
| 198 | * \param hash Hash of the message to sign |
| 199 | * \param md_info Information about the hash function used |
| 200 | * \param sig Signature to verify |
| 201 | * \param sig_len Signature length |
| 202 | * |
| 203 | * \return 0 on success (signature is valid), |
| 204 | * or a specific error code. |
| 205 | */ |
| 206 | int pk_verify( pk_context *ctx, |
| 207 | const unsigned char *hash, const md_info_t *md_info, |
| 208 | const unsigned char *sig, size_t sig_len ); |
| 209 | |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame^] | 210 | /** |
| 211 | * \brief Export debug information |
| 212 | * |
| 213 | * \param ctx Context to use |
| 214 | * \param items Place to write debug items |
| 215 | * |
| 216 | * \return 0 on sucess or POLARSSL_ERR_PK_BAD_INPUT_DATA |
| 217 | */ |
| 218 | int pk_debug( const pk_context *ctx, pk_debug_item *items ); |
| 219 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 220 | #ifdef __cplusplus |
| 221 | } |
| 222 | #endif |
| 223 | |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 224 | #endif /* POLARSSL_PK_H */ |