blob: 29aec309fffa65995d7e32295eb46e515efbe248 [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é-Gonnard7a6c9462013-07-09 10:04:07 +020036#define POLARSSL_ERR_PK_MALLOC_FAILED -0x2F80 /**< Memory alloation failed. */
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +020037#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 +020038
Paul Bakkered27a042013-04-18 22:46:23 +020039#ifdef __cplusplus
40extern "C" {
41#endif
42
43/**
44 * \brief Public key types
45 */
46typedef enum {
47 POLARSSL_PK_NONE=0,
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020048#if defined(POLARSSL_RSA_C)
Paul Bakkered27a042013-04-18 22:46:23 +020049 POLARSSL_PK_RSA,
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020050#endif
51#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard5a9b82e2013-07-01 16:57:44 +020052 POLARSSL_PK_ECKEY,
53 POLARSSL_PK_ECKEY_DH,
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020054#endif
Manuel Pégourié-Gonnard1e60cd02013-07-10 10:28:53 +020055#if defined(POLARSSL_ECDSA_C)
56 POLARSSL_PK_ECDSA,
57#endif
Paul Bakkered27a042013-04-18 22:46:23 +020058} pk_type_t;
59
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020060/**
61 * \brief Public key container
62 */
63typedef struct
64{
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020065 pk_type_t type; /**< Public key type */
66 void * data; /**< Public key data */
67 int dont_free; /**< True if data must not be freed */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020068} pk_context;
69
70/**
71 * \brief Initialize a pk_context (as NONE)
72 */
73void pk_init( pk_context *ctx );
74
75/**
76 * \brief Free a pk_context
77 */
78void pk_free( pk_context *ctx );
79
80/**
81 * \brief Set a pk_context to a given type
82 *
83 * \param ctx Context to initialize
84 * \param type Type of key
85 *
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +020086 * \note Once the type of a key has been set, it cannot be reset.
87 * If you want to do so, you need to use pk_free() first.
88 *
89 * \return O on success,
90 * POLARSSL_ERR_PK_MALLOC_FAILED on memory allocation fail,
91 * POLARSSL_ERR_PK_TYPE_MISMATCH on attempts to reset type.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020092 */
93int pk_set_type( pk_context *ctx, pk_type_t type );
94
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020095#if defined(POLARSSL_RSA_C)
96/**
97 * \brief Wrap a RSA context in a PK context
98 *
99 * \param ctx PK context to initiliaze
100 * \param rsa RSA context to use
101 *
102 * \note The PK context must be freshly initialized.
103 *
104 * \return O on success,
105 * POLARSSL_ERR_PK_TYPE_MISMATCH if ctx was not empty.
106 */
107int pk_wrap_rsa( pk_context *ctx, const rsa_context *rsa);
108#endif
109
Paul Bakkered27a042013-04-18 22:46:23 +0200110#ifdef __cplusplus
111}
112#endif
113
114#endif /* pk.h */