blob: 2f7008514f6aa15cc6fea7c09164e3003103fc79 [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 */
27#ifndef POLARSSL_PK_H
28#define POLARSSL_PK_H
29
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020030#include "config.h"
31
32#if defined(POLARSSL_RSA_C)
33#include "rsa.h"
34#endif
35
Manuel Pégourié-Gonnard211a64c2013-08-09 15:04:26 +020036#if defined(POLARSSL_ECDSA_C)
37#include "ecdsa.h"
38#endif
39
Manuel Pégourié-Gonnard7a6c9462013-07-09 10:04:07 +020040#define POLARSSL_ERR_PK_MALLOC_FAILED -0x2F80 /**< Memory alloation failed. */
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +020041#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 +020042
Manuel Pégourié-Gonnard360a5832013-07-10 14:56:36 +020043#if defined(POLARSSL_RSA_C)
44/**
45 * Quick access to an RSA context inside a PK context.
46 *
47 * \warning You must make sure the PK context actually holds an RSA context
48 * before using this macro!
49 */
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +020050#define pk_rsa( pk ) ( (rsa_context *) (pk).data )
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020051#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard360a5832013-07-10 14:56:36 +020052
53#if defined(POLARSSL_ECP_C)
54/**
55 * Quick access to an EC context inside a PK context.
56 *
57 * \warning You must make sure the PK context actually holds an EC context
58 * before using this macro!
59 */
Manuel Pégourié-Gonnarda2d4e642013-07-11 13:59:02 +020060#define pk_ec( pk ) ( (ecp_keypair *) (pk).data )
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020061#endif /* POLARSSL_ECP_C */
Manuel Pégourié-Gonnard360a5832013-07-10 14:56:36 +020062
63
Paul Bakkered27a042013-04-18 22:46:23 +020064#ifdef __cplusplus
65extern "C" {
66#endif
67
68/**
69 * \brief Public key types
70 */
71typedef enum {
72 POLARSSL_PK_NONE=0,
73 POLARSSL_PK_RSA,
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +020074 POLARSSL_PK_ECKEY,
75 POLARSSL_PK_ECKEY_DH,
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +020076 POLARSSL_PK_ECDSA,
Paul Bakkered27a042013-04-18 22:46:23 +020077} pk_type_t;
78
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020079/**
80 * \brief Public key container
81 */
82typedef struct
83{
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020084 pk_type_t type; /**< Public key type */
85 void * data; /**< Public key data */
86 int dont_free; /**< True if data must not be freed */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020087} pk_context;
88
89/**
90 * \brief Initialize a pk_context (as NONE)
91 */
92void pk_init( pk_context *ctx );
93
94/**
95 * \brief Free a pk_context
96 */
97void pk_free( pk_context *ctx );
98
99/**
100 * \brief Set a pk_context to a given type
101 *
102 * \param ctx Context to initialize
103 * \param type Type of key
104 *
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +0200105 * \note Once the type of a key has been set, it cannot be reset.
106 * If you want to do so, you need to use pk_free() first.
107 *
108 * \return O on success,
109 * POLARSSL_ERR_PK_MALLOC_FAILED on memory allocation fail,
110 * POLARSSL_ERR_PK_TYPE_MISMATCH on attempts to reset type.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200111 */
112int pk_set_type( pk_context *ctx, pk_type_t type );
113
Manuel Pégourié-Gonnard211a64c2013-08-09 15:04:26 +0200114#if defined(POLARSSL_ECDSA_C)
115/**
116 * \brief Convert a generic EC key into an ECDSA context
117 *
118 * \param ctx Context to convert
119 *
120 * \return 0 on success, or
121 * POLARSSL_ERR_PK_MALLOC_FAILED or
122 * POLARSSL_ERR_PK_TYPE_MISMATCH.
123 */
124int pk_ec_to_ecdsa( pk_context *ctx );
125
126/**
127 * \brief Tell if a PK context can be used for ECDSA
128 *
129 * \param ctx Context to check
130 *
131 * \return 0 if context cannot be used for ECDSA,
132 * 1 otherwise
133 */
134static inline int pk_can_ecdsa( pk_context ctx )
135{
136 return( ctx.type == POLARSSL_PK_ECKEY ||
137 ctx.type == POLARSSL_PK_ECDSA );
138}
139#endif /* POLARSSL_ECDSA_C */
140
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +0200141#if defined(POLARSSL_RSA_C)
142/**
143 * \brief Wrap a RSA context in a PK context
144 *
145 * \param ctx PK context to initiliaze
146 * \param rsa RSA context to use
147 *
148 * \note The PK context must be freshly initialized.
149 *
150 * \return O on success,
151 * POLARSSL_ERR_PK_TYPE_MISMATCH if ctx was not empty.
152 */
153int pk_wrap_rsa( pk_context *ctx, const rsa_context *rsa);
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200154#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +0200155
Paul Bakkered27a042013-04-18 22:46:23 +0200156#ifdef __cplusplus
157}
158#endif
159
160#endif /* pk.h */