blob: f06ec68a4cc19759b7a0a132746794c7b1805406 [file] [log] [blame]
Paul Bakkered27a042013-04-18 22:46:23 +02001/**
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é-Gonnardd73b3c12013-08-12 17:06:05 +020027
Paul Bakkered27a042013-04-18 22:46:23 +020028#ifndef POLARSSL_PK_H
29#define POLARSSL_PK_H
30
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020031#include "config.h"
32
33#if defined(POLARSSL_RSA_C)
34#include "rsa.h"
35#endif
36
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020037#if defined(POLARSSL_ECP_C)
38#include "ecp.h"
39#endif
40
Manuel Pégourié-Gonnard211a64c2013-08-09 15:04:26 +020041#if defined(POLARSSL_ECDSA_C)
42#include "ecdsa.h"
43#endif
44
Manuel Pégourié-Gonnard7a6c9462013-07-09 10:04:07 +020045#define POLARSSL_ERR_PK_MALLOC_FAILED -0x2F80 /**< Memory alloation failed. */
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +020046#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é-Gonnard7a6c9462013-07-09 10:04:07 +020047
Manuel Pégourié-Gonnard360a5832013-07-10 14:56:36 +020048#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é-Gonnarda2d4e642013-07-11 13:59:02 +020055#define pk_rsa( pk ) ( (rsa_context *) (pk).data )
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020056#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard360a5832013-07-10 14:56:36 +020057
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é-Gonnarda2d4e642013-07-11 13:59:02 +020065#define pk_ec( pk ) ( (ecp_keypair *) (pk).data )
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020066#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard360a5832013-07-10 14:56:36 +020067
68
Paul Bakkered27a042013-04-18 22:46:23 +020069#ifdef __cplusplus
70extern "C" {
71#endif
72
73/**
74 * \brief Public key types
75 */
76typedef enum {
77 POLARSSL_PK_NONE=0,
78 POLARSSL_PK_RSA,
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +020079 POLARSSL_PK_ECKEY,
80 POLARSSL_PK_ECKEY_DH,
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +020081 POLARSSL_PK_ECDSA,
Paul Bakkered27a042013-04-18 22:46:23 +020082} pk_type_t;
83
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020084/**
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020085 * \brief Public key info
86 */
87typedef struct
88{
89 /** Public key type */
90 pk_type_t type;
91
92 /** Verify signature */
93 int (*verify_func)( void *ctx,
94 const unsigned char *hash, const md_info_t *md_info,
95 const unsigned char *sig, size_t sig_len );
96} pk_info_t;
97
98/**
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020099 * \brief Public key container
100 */
101typedef struct
102{
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200103 const pk_info_t * info; /**< Public key informations */
104 pk_type_t type; /**< Public key type (temporary) */
105 void * data; /**< Public key data */
106 int dont_free; /**< True if data must not be freed */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200107} pk_context;
108
109/**
110 * \brief Initialize a pk_context (as NONE)
111 */
112void pk_init( pk_context *ctx );
113
114/**
115 * \brief Free a pk_context
116 */
117void pk_free( pk_context *ctx );
118
119/**
120 * \brief Set a pk_context to a given type
121 *
122 * \param ctx Context to initialize
123 * \param type Type of key
124 *
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +0200125 * \note Once the type of a key has been set, it cannot be reset.
126 * If you want to do so, you need to use pk_free() first.
127 *
128 * \return O on success,
129 * POLARSSL_ERR_PK_MALLOC_FAILED on memory allocation fail,
130 * POLARSSL_ERR_PK_TYPE_MISMATCH on attempts to reset type.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200131 */
132int pk_set_type( pk_context *ctx, pk_type_t type );
133
Manuel Pégourié-Gonnard211a64c2013-08-09 15:04:26 +0200134#if defined(POLARSSL_ECDSA_C)
135/**
136 * \brief Convert a generic EC key into an ECDSA context
137 *
138 * \param ctx Context to convert
139 *
140 * \return 0 on success, or
141 * POLARSSL_ERR_PK_MALLOC_FAILED or
142 * POLARSSL_ERR_PK_TYPE_MISMATCH.
143 */
144int pk_ec_to_ecdsa( pk_context *ctx );
145
146/**
147 * \brief Tell if a PK context can be used for ECDSA
148 *
149 * \param ctx Context to check
150 *
151 * \return 0 if context cannot be used for ECDSA,
152 * 1 otherwise
153 */
154static inline int pk_can_ecdsa( pk_context ctx )
155{
156 return( ctx.type == POLARSSL_PK_ECKEY ||
157 ctx.type == POLARSSL_PK_ECDSA );
158}
159#endif /* POLARSSL_ECDSA_C */
160
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +0200161#if defined(POLARSSL_RSA_C)
162/**
163 * \brief Wrap a RSA context in a PK context
164 *
165 * \param ctx PK context to initiliaze
166 * \param rsa RSA context to use
167 *
168 * \note The PK context must be freshly initialized.
169 *
170 * \return O on success,
171 * POLARSSL_ERR_PK_TYPE_MISMATCH if ctx was not empty.
172 */
173int pk_wrap_rsa( pk_context *ctx, const rsa_context *rsa);
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200174#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +0200175
Paul Bakkered27a042013-04-18 22:46:23 +0200176#ifdef __cplusplus
177}
178#endif
179
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200180#endif /* POLARSSL_PK_H */