Example program to test AES-GCM functionality. Used for a workshop

Dependencies:   mbed

Committer:
HannesTschofenig
Date:
Thu Sep 27 06:34:22 2018 +0000
Revision:
0:796d0f61a05b
Example AES-GCM test program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HannesTschofenig 0:796d0f61a05b 1 /**
HannesTschofenig 0:796d0f61a05b 2 * \file pk.h
HannesTschofenig 0:796d0f61a05b 3 *
HannesTschofenig 0:796d0f61a05b 4 * \brief Public Key abstraction layer
HannesTschofenig 0:796d0f61a05b 5 *
HannesTschofenig 0:796d0f61a05b 6 * Copyright (C) 2006-2013, Brainspark B.V.
HannesTschofenig 0:796d0f61a05b 7 *
HannesTschofenig 0:796d0f61a05b 8 * This file is part of PolarSSL (http://www.polarssl.org)
HannesTschofenig 0:796d0f61a05b 9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
HannesTschofenig 0:796d0f61a05b 10 *
HannesTschofenig 0:796d0f61a05b 11 * All rights reserved.
HannesTschofenig 0:796d0f61a05b 12 *
HannesTschofenig 0:796d0f61a05b 13 * This program is free software; you can redistribute it and/or modify
HannesTschofenig 0:796d0f61a05b 14 * it under the terms of the GNU General Public License as published by
HannesTschofenig 0:796d0f61a05b 15 * the Free Software Foundation; either version 2 of the License, or
HannesTschofenig 0:796d0f61a05b 16 * (at your option) any later version.
HannesTschofenig 0:796d0f61a05b 17 *
HannesTschofenig 0:796d0f61a05b 18 * This program is distributed in the hope that it will be useful,
HannesTschofenig 0:796d0f61a05b 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
HannesTschofenig 0:796d0f61a05b 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
HannesTschofenig 0:796d0f61a05b 21 * GNU General Public License for more details.
HannesTschofenig 0:796d0f61a05b 22 *
HannesTschofenig 0:796d0f61a05b 23 * You should have received a copy of the GNU General Public License along
HannesTschofenig 0:796d0f61a05b 24 * with this program; if not, write to the Free Software Foundation, Inc.,
HannesTschofenig 0:796d0f61a05b 25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
HannesTschofenig 0:796d0f61a05b 26 */
HannesTschofenig 0:796d0f61a05b 27
HannesTschofenig 0:796d0f61a05b 28 #ifndef POLARSSL_PK_H
HannesTschofenig 0:796d0f61a05b 29 #define POLARSSL_PK_H
HannesTschofenig 0:796d0f61a05b 30
HannesTschofenig 0:796d0f61a05b 31 #if !defined(POLARSSL_CONFIG_FILE)
HannesTschofenig 0:796d0f61a05b 32 #include "config.h"
HannesTschofenig 0:796d0f61a05b 33 #else
HannesTschofenig 0:796d0f61a05b 34 #include POLARSSL_CONFIG_FILE
HannesTschofenig 0:796d0f61a05b 35 #endif
HannesTschofenig 0:796d0f61a05b 36
HannesTschofenig 0:796d0f61a05b 37 #include "md.h"
HannesTschofenig 0:796d0f61a05b 38
HannesTschofenig 0:796d0f61a05b 39 #if defined(POLARSSL_RSA_C)
HannesTschofenig 0:796d0f61a05b 40 #include "rsa.h"
HannesTschofenig 0:796d0f61a05b 41 #endif
HannesTschofenig 0:796d0f61a05b 42
HannesTschofenig 0:796d0f61a05b 43 #if defined(POLARSSL_ECP_C)
HannesTschofenig 0:796d0f61a05b 44 #include "ecp.h"
HannesTschofenig 0:796d0f61a05b 45 #endif
HannesTschofenig 0:796d0f61a05b 46
HannesTschofenig 0:796d0f61a05b 47 #if defined(POLARSSL_ECDSA_C)
HannesTschofenig 0:796d0f61a05b 48 #include "ecdsa.h"
HannesTschofenig 0:796d0f61a05b 49 #endif
HannesTschofenig 0:796d0f61a05b 50
HannesTschofenig 0:796d0f61a05b 51 #define POLARSSL_ERR_PK_MALLOC_FAILED -0x2F80 /**< Memory alloation failed. */
HannesTschofenig 0:796d0f61a05b 52 #define POLARSSL_ERR_PK_TYPE_MISMATCH -0x2F00 /**< Type mismatch, eg attempt to encrypt with an ECDSA key */
HannesTschofenig 0:796d0f61a05b 53 #define POLARSSL_ERR_PK_BAD_INPUT_DATA -0x2E80 /**< Bad input parameters to function. */
HannesTschofenig 0:796d0f61a05b 54 #define POLARSSL_ERR_PK_FILE_IO_ERROR -0x2E00 /**< Read/write of file failed. */
HannesTschofenig 0:796d0f61a05b 55 #define POLARSSL_ERR_PK_KEY_INVALID_VERSION -0x2D80 /**< Unsupported key version */
HannesTschofenig 0:796d0f61a05b 56 #define POLARSSL_ERR_PK_KEY_INVALID_FORMAT -0x2D00 /**< Invalid key tag or value. */
HannesTschofenig 0:796d0f61a05b 57 #define POLARSSL_ERR_PK_UNKNOWN_PK_ALG -0x2C80 /**< Key algorithm is unsupported (only RSA and EC are supported). */
HannesTschofenig 0:796d0f61a05b 58 #define POLARSSL_ERR_PK_PASSWORD_REQUIRED -0x2C00 /**< Private key password can't be empty. */
HannesTschofenig 0:796d0f61a05b 59 #define POLARSSL_ERR_PK_PASSWORD_MISMATCH -0x2B80 /**< Given private key password does not allow for correct decryption. */
HannesTschofenig 0:796d0f61a05b 60 #define POLARSSL_ERR_PK_INVALID_PUBKEY -0x2B00 /**< The pubkey tag or value is invalid (only RSA and EC are supported). */
HannesTschofenig 0:796d0f61a05b 61 #define POLARSSL_ERR_PK_INVALID_ALG -0x2A80 /**< The algorithm tag or value is invalid. */
HannesTschofenig 0:796d0f61a05b 62 #define POLARSSL_ERR_PK_UNKNOWN_NAMED_CURVE -0x2A00 /**< Elliptic curve is unsupported (only NIST curves are supported). */
HannesTschofenig 0:796d0f61a05b 63 #define POLARSSL_ERR_PK_FEATURE_UNAVAILABLE -0x2980 /**< Unavailable feature, e.g. RSA disabled for RSA key. */
HannesTschofenig 0:796d0f61a05b 64 #define POLARSSL_ERR_PK_SIG_LEN_MISMATCH -0x2000 /**< The signature is valid but its length is less than expected. */
HannesTschofenig 0:796d0f61a05b 65
HannesTschofenig 0:796d0f61a05b 66
HannesTschofenig 0:796d0f61a05b 67 #if defined(POLARSSL_RSA_C)
HannesTschofenig 0:796d0f61a05b 68 /**
HannesTschofenig 0:796d0f61a05b 69 * Quick access to an RSA context inside a PK context.
HannesTschofenig 0:796d0f61a05b 70 *
HannesTschofenig 0:796d0f61a05b 71 * \warning You must make sure the PK context actually holds an RSA context
HannesTschofenig 0:796d0f61a05b 72 * before using this macro!
HannesTschofenig 0:796d0f61a05b 73 */
HannesTschofenig 0:796d0f61a05b 74 #define pk_rsa( pk ) ( (rsa_context *) (pk).pk_ctx )
HannesTschofenig 0:796d0f61a05b 75 #endif /* POLARSSL_RSA_C */
HannesTschofenig 0:796d0f61a05b 76
HannesTschofenig 0:796d0f61a05b 77 #if defined(POLARSSL_ECP_C)
HannesTschofenig 0:796d0f61a05b 78 /**
HannesTschofenig 0:796d0f61a05b 79 * Quick access to an EC context inside a PK context.
HannesTschofenig 0:796d0f61a05b 80 *
HannesTschofenig 0:796d0f61a05b 81 * \warning You must make sure the PK context actually holds an EC context
HannesTschofenig 0:796d0f61a05b 82 * before using this macro!
HannesTschofenig 0:796d0f61a05b 83 */
HannesTschofenig 0:796d0f61a05b 84 #define pk_ec( pk ) ( (ecp_keypair *) (pk).pk_ctx )
HannesTschofenig 0:796d0f61a05b 85 #endif /* POLARSSL_ECP_C */
HannesTschofenig 0:796d0f61a05b 86
HannesTschofenig 0:796d0f61a05b 87
HannesTschofenig 0:796d0f61a05b 88 #ifdef __cplusplus
HannesTschofenig 0:796d0f61a05b 89 extern "C" {
HannesTschofenig 0:796d0f61a05b 90 #endif
HannesTschofenig 0:796d0f61a05b 91
HannesTschofenig 0:796d0f61a05b 92 /**
HannesTschofenig 0:796d0f61a05b 93 * \brief Public key types
HannesTschofenig 0:796d0f61a05b 94 */
HannesTschofenig 0:796d0f61a05b 95 typedef enum {
HannesTschofenig 0:796d0f61a05b 96 POLARSSL_PK_NONE=0,
HannesTschofenig 0:796d0f61a05b 97 POLARSSL_PK_RSA,
HannesTschofenig 0:796d0f61a05b 98 POLARSSL_PK_ECKEY,
HannesTschofenig 0:796d0f61a05b 99 POLARSSL_PK_ECKEY_DH,
HannesTschofenig 0:796d0f61a05b 100 POLARSSL_PK_ECDSA,
HannesTschofenig 0:796d0f61a05b 101 POLARSSL_PK_RSA_ALT,
HannesTschofenig 0:796d0f61a05b 102 } pk_type_t;
HannesTschofenig 0:796d0f61a05b 103
HannesTschofenig 0:796d0f61a05b 104 /**
HannesTschofenig 0:796d0f61a05b 105 * \brief Types for interfacing with the debug module
HannesTschofenig 0:796d0f61a05b 106 */
HannesTschofenig 0:796d0f61a05b 107 typedef enum
HannesTschofenig 0:796d0f61a05b 108 {
HannesTschofenig 0:796d0f61a05b 109 POLARSSL_PK_DEBUG_NONE = 0,
HannesTschofenig 0:796d0f61a05b 110 POLARSSL_PK_DEBUG_MPI,
HannesTschofenig 0:796d0f61a05b 111 POLARSSL_PK_DEBUG_ECP,
HannesTschofenig 0:796d0f61a05b 112 } pk_debug_type;
HannesTschofenig 0:796d0f61a05b 113
HannesTschofenig 0:796d0f61a05b 114 /**
HannesTschofenig 0:796d0f61a05b 115 * \brief Item to send to the debug module
HannesTschofenig 0:796d0f61a05b 116 */
HannesTschofenig 0:796d0f61a05b 117 typedef struct
HannesTschofenig 0:796d0f61a05b 118 {
HannesTschofenig 0:796d0f61a05b 119 pk_debug_type type;
HannesTschofenig 0:796d0f61a05b 120 const char *name;
HannesTschofenig 0:796d0f61a05b 121 void *value;
HannesTschofenig 0:796d0f61a05b 122 } pk_debug_item;
HannesTschofenig 0:796d0f61a05b 123
HannesTschofenig 0:796d0f61a05b 124 /** Maximum number of item send for debugging, plus 1 */
HannesTschofenig 0:796d0f61a05b 125 #define POLARSSL_PK_DEBUG_MAX_ITEMS 3
HannesTschofenig 0:796d0f61a05b 126
HannesTschofenig 0:796d0f61a05b 127 /**
HannesTschofenig 0:796d0f61a05b 128 * \brief Public key information and operations
HannesTschofenig 0:796d0f61a05b 129 */
HannesTschofenig 0:796d0f61a05b 130 typedef struct
HannesTschofenig 0:796d0f61a05b 131 {
HannesTschofenig 0:796d0f61a05b 132 /** Public key type */
HannesTschofenig 0:796d0f61a05b 133 pk_type_t type;
HannesTschofenig 0:796d0f61a05b 134
HannesTschofenig 0:796d0f61a05b 135 /** Type name */
HannesTschofenig 0:796d0f61a05b 136 const char *name;
HannesTschofenig 0:796d0f61a05b 137
HannesTschofenig 0:796d0f61a05b 138 /** Get key size in bits */
HannesTschofenig 0:796d0f61a05b 139 size_t (*get_size)( const void * );
HannesTschofenig 0:796d0f61a05b 140
HannesTschofenig 0:796d0f61a05b 141 /** Tell if the context implements this type (e.g. ECKEY can do ECDSA) */
HannesTschofenig 0:796d0f61a05b 142 int (*can_do)( pk_type_t type );
HannesTschofenig 0:796d0f61a05b 143
HannesTschofenig 0:796d0f61a05b 144 /** Verify signature */
HannesTschofenig 0:796d0f61a05b 145 int (*verify_func)( void *ctx, md_type_t md_alg,
HannesTschofenig 0:796d0f61a05b 146 const unsigned char *hash, size_t hash_len,
HannesTschofenig 0:796d0f61a05b 147 const unsigned char *sig, size_t sig_len );
HannesTschofenig 0:796d0f61a05b 148
HannesTschofenig 0:796d0f61a05b 149 /** Make signature */
HannesTschofenig 0:796d0f61a05b 150 int (*sign_func)( void *ctx, md_type_t md_alg,
HannesTschofenig 0:796d0f61a05b 151 const unsigned char *hash, size_t hash_len,
HannesTschofenig 0:796d0f61a05b 152 unsigned char *sig, size_t *sig_len,
HannesTschofenig 0:796d0f61a05b 153 int (*f_rng)(void *, unsigned char *, size_t),
HannesTschofenig 0:796d0f61a05b 154 void *p_rng );
HannesTschofenig 0:796d0f61a05b 155
HannesTschofenig 0:796d0f61a05b 156 /** Decrypt message */
HannesTschofenig 0:796d0f61a05b 157 int (*decrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
HannesTschofenig 0:796d0f61a05b 158 unsigned char *output, size_t *olen, size_t osize,
HannesTschofenig 0:796d0f61a05b 159 int (*f_rng)(void *, unsigned char *, size_t),
HannesTschofenig 0:796d0f61a05b 160 void *p_rng );
HannesTschofenig 0:796d0f61a05b 161
HannesTschofenig 0:796d0f61a05b 162 /** Encrypt message */
HannesTschofenig 0:796d0f61a05b 163 int (*encrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
HannesTschofenig 0:796d0f61a05b 164 unsigned char *output, size_t *olen, size_t osize,
HannesTschofenig 0:796d0f61a05b 165 int (*f_rng)(void *, unsigned char *, size_t),
HannesTschofenig 0:796d0f61a05b 166 void *p_rng );
HannesTschofenig 0:796d0f61a05b 167
HannesTschofenig 0:796d0f61a05b 168 /** Allocate a new context */
HannesTschofenig 0:796d0f61a05b 169 void * (*ctx_alloc_func)( void );
HannesTschofenig 0:796d0f61a05b 170
HannesTschofenig 0:796d0f61a05b 171 /** Free the given context */
HannesTschofenig 0:796d0f61a05b 172 void (*ctx_free_func)( void *ctx );
HannesTschofenig 0:796d0f61a05b 173
HannesTschofenig 0:796d0f61a05b 174 /** Interface with the debug module */
HannesTschofenig 0:796d0f61a05b 175 void (*debug_func)( const void *ctx, pk_debug_item *items );
HannesTschofenig 0:796d0f61a05b 176
HannesTschofenig 0:796d0f61a05b 177 } pk_info_t;
HannesTschofenig 0:796d0f61a05b 178
HannesTschofenig 0:796d0f61a05b 179 /**
HannesTschofenig 0:796d0f61a05b 180 * \brief Public key container
HannesTschofenig 0:796d0f61a05b 181 */
HannesTschofenig 0:796d0f61a05b 182 typedef struct
HannesTschofenig 0:796d0f61a05b 183 {
HannesTschofenig 0:796d0f61a05b 184 const pk_info_t * pk_info; /**< Public key informations */
HannesTschofenig 0:796d0f61a05b 185 void * pk_ctx; /**< Underlying public key context */
HannesTschofenig 0:796d0f61a05b 186 } pk_context;
HannesTschofenig 0:796d0f61a05b 187
HannesTschofenig 0:796d0f61a05b 188 /**
HannesTschofenig 0:796d0f61a05b 189 * \brief Types for RSA-alt abstraction
HannesTschofenig 0:796d0f61a05b 190 */
HannesTschofenig 0:796d0f61a05b 191 typedef int (*pk_rsa_alt_decrypt_func)( void *ctx, int mode, size_t *olen,
HannesTschofenig 0:796d0f61a05b 192 const unsigned char *input, unsigned char *output,
HannesTschofenig 0:796d0f61a05b 193 size_t output_max_len );
HannesTschofenig 0:796d0f61a05b 194 typedef int (*pk_rsa_alt_sign_func)( void *ctx,
HannesTschofenig 0:796d0f61a05b 195 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
HannesTschofenig 0:796d0f61a05b 196 int mode, md_type_t md_alg, unsigned int hashlen,
HannesTschofenig 0:796d0f61a05b 197 const unsigned char *hash, unsigned char *sig );
HannesTschofenig 0:796d0f61a05b 198 typedef size_t (*pk_rsa_alt_key_len_func)( void *ctx );
HannesTschofenig 0:796d0f61a05b 199
HannesTschofenig 0:796d0f61a05b 200 /**
HannesTschofenig 0:796d0f61a05b 201 * \brief Return information associated with the given PK type
HannesTschofenig 0:796d0f61a05b 202 *
HannesTschofenig 0:796d0f61a05b 203 * \param pk_type PK type to search for.
HannesTschofenig 0:796d0f61a05b 204 *
HannesTschofenig 0:796d0f61a05b 205 * \return The PK info associated with the type or NULL if not found.
HannesTschofenig 0:796d0f61a05b 206 */
HannesTschofenig 0:796d0f61a05b 207 const pk_info_t *pk_info_from_type( pk_type_t pk_type );
HannesTschofenig 0:796d0f61a05b 208
HannesTschofenig 0:796d0f61a05b 209 /**
HannesTschofenig 0:796d0f61a05b 210 * \brief Initialize a pk_context (as NONE)
HannesTschofenig 0:796d0f61a05b 211 */
HannesTschofenig 0:796d0f61a05b 212 void pk_init( pk_context *ctx );
HannesTschofenig 0:796d0f61a05b 213
HannesTschofenig 0:796d0f61a05b 214 /**
HannesTschofenig 0:796d0f61a05b 215 * \brief Free a pk_context
HannesTschofenig 0:796d0f61a05b 216 */
HannesTschofenig 0:796d0f61a05b 217 void pk_free( pk_context *ctx );
HannesTschofenig 0:796d0f61a05b 218
HannesTschofenig 0:796d0f61a05b 219 /**
HannesTschofenig 0:796d0f61a05b 220 * \brief Initialize a PK context with the information given
HannesTschofenig 0:796d0f61a05b 221 * and allocates the type-specific PK subcontext.
HannesTschofenig 0:796d0f61a05b 222 *
HannesTschofenig 0:796d0f61a05b 223 * \param ctx Context to initialize. Must be empty (type NONE).
HannesTschofenig 0:796d0f61a05b 224 * \param info Information to use
HannesTschofenig 0:796d0f61a05b 225 *
HannesTschofenig 0:796d0f61a05b 226 * \return 0 on success,
HannesTschofenig 0:796d0f61a05b 227 * POLARSSL_ERR_PK_BAD_INPUT_DATA on invalid input,
HannesTschofenig 0:796d0f61a05b 228 * POLARSSL_ERR_PK_MALLOC_FAILED on allocation failure.
HannesTschofenig 0:796d0f61a05b 229 *
HannesTschofenig 0:796d0f61a05b 230 * \note For contexts holding an RSA-alt key, use
HannesTschofenig 0:796d0f61a05b 231 * \c pk_init_ctx_rsa_alt() instead.
HannesTschofenig 0:796d0f61a05b 232 */
HannesTschofenig 0:796d0f61a05b 233 int pk_init_ctx( pk_context *ctx, const pk_info_t *info );
HannesTschofenig 0:796d0f61a05b 234
HannesTschofenig 0:796d0f61a05b 235 /**
HannesTschofenig 0:796d0f61a05b 236 * \brief Initialize an RSA-alt context
HannesTschofenig 0:796d0f61a05b 237 *
HannesTschofenig 0:796d0f61a05b 238 * \param ctx Context to initialize. Must be empty (type NONE).
HannesTschofenig 0:796d0f61a05b 239 * \param key RSA key pointer
HannesTschofenig 0:796d0f61a05b 240 * \param decrypt_func Decryption function
HannesTschofenig 0:796d0f61a05b 241 * \param sign_func Signing function
HannesTschofenig 0:796d0f61a05b 242 * \param key_len_func Function returning key length in bytes
HannesTschofenig 0:796d0f61a05b 243 *
HannesTschofenig 0:796d0f61a05b 244 * \return 0 on success, or POLARSSL_ERR_PK_BAD_INPUT_DATA if the
HannesTschofenig 0:796d0f61a05b 245 * context wasn't already initialized as RSA_ALT.
HannesTschofenig 0:796d0f61a05b 246 *
HannesTschofenig 0:796d0f61a05b 247 * \note This function replaces \c pk_init_ctx() for RSA-alt.
HannesTschofenig 0:796d0f61a05b 248 */
HannesTschofenig 0:796d0f61a05b 249 int pk_init_ctx_rsa_alt( pk_context *ctx, void * key,
HannesTschofenig 0:796d0f61a05b 250 pk_rsa_alt_decrypt_func decrypt_func,
HannesTschofenig 0:796d0f61a05b 251 pk_rsa_alt_sign_func sign_func,
HannesTschofenig 0:796d0f61a05b 252 pk_rsa_alt_key_len_func key_len_func );
HannesTschofenig 0:796d0f61a05b 253
HannesTschofenig 0:796d0f61a05b 254 /**
HannesTschofenig 0:796d0f61a05b 255 * \brief Get the size in bits of the underlying key
HannesTschofenig 0:796d0f61a05b 256 *
HannesTschofenig 0:796d0f61a05b 257 * \param ctx Context to use
HannesTschofenig 0:796d0f61a05b 258 *
HannesTschofenig 0:796d0f61a05b 259 * \return Key size in bits, or 0 on error
HannesTschofenig 0:796d0f61a05b 260 */
HannesTschofenig 0:796d0f61a05b 261 size_t pk_get_size( const pk_context *ctx );
HannesTschofenig 0:796d0f61a05b 262
HannesTschofenig 0:796d0f61a05b 263 /**
HannesTschofenig 0:796d0f61a05b 264 * \brief Get the length in bytes of the underlying key
HannesTschofenig 0:796d0f61a05b 265 * \param ctx Context to use
HannesTschofenig 0:796d0f61a05b 266 *
HannesTschofenig 0:796d0f61a05b 267 * \return Key length in bytes, or 0 on error
HannesTschofenig 0:796d0f61a05b 268 */
HannesTschofenig 0:796d0f61a05b 269 static inline size_t pk_get_len( const pk_context *ctx )
HannesTschofenig 0:796d0f61a05b 270 {
HannesTschofenig 0:796d0f61a05b 271 return( ( pk_get_size( ctx ) + 7 ) / 8 );
HannesTschofenig 0:796d0f61a05b 272 }
HannesTschofenig 0:796d0f61a05b 273
HannesTschofenig 0:796d0f61a05b 274 /**
HannesTschofenig 0:796d0f61a05b 275 * \brief Tell if a context can do the operation given by type
HannesTschofenig 0:796d0f61a05b 276 *
HannesTschofenig 0:796d0f61a05b 277 * \param ctx Context to test
HannesTschofenig 0:796d0f61a05b 278 * \param type Target type
HannesTschofenig 0:796d0f61a05b 279 *
HannesTschofenig 0:796d0f61a05b 280 * \return 0 if context can't do the operations,
HannesTschofenig 0:796d0f61a05b 281 * 1 otherwise.
HannesTschofenig 0:796d0f61a05b 282 */
HannesTschofenig 0:796d0f61a05b 283 int pk_can_do( pk_context *ctx, pk_type_t type );
HannesTschofenig 0:796d0f61a05b 284
HannesTschofenig 0:796d0f61a05b 285 /**
HannesTschofenig 0:796d0f61a05b 286 * \brief Verify signature
HannesTschofenig 0:796d0f61a05b 287 *
HannesTschofenig 0:796d0f61a05b 288 * \param ctx PK context to use
HannesTschofenig 0:796d0f61a05b 289 * \param md_alg Hash algorithm used (see notes)
HannesTschofenig 0:796d0f61a05b 290 * \param hash Hash of the message to sign
HannesTschofenig 0:796d0f61a05b 291 * \param hash_len Hash length or 0 (see notes)
HannesTschofenig 0:796d0f61a05b 292 * \param sig Signature to verify
HannesTschofenig 0:796d0f61a05b 293 * \param sig_len Signature length
HannesTschofenig 0:796d0f61a05b 294 *
HannesTschofenig 0:796d0f61a05b 295 * \return 0 on success (signature is valid),
HannesTschofenig 0:796d0f61a05b 296 * POLARSSL_ERR_PK_SIG_LEN_MISMATCH if the signature is
HannesTschofenig 0:796d0f61a05b 297 * valid but its actual length is less than sig_len,
HannesTschofenig 0:796d0f61a05b 298 * or a specific error code.
HannesTschofenig 0:796d0f61a05b 299 *
HannesTschofenig 0:796d0f61a05b 300 * \note If hash_len is 0, then the length associated with md_alg
HannesTschofenig 0:796d0f61a05b 301 * is used instead, or an error returned if it is invalid.
HannesTschofenig 0:796d0f61a05b 302 *
HannesTschofenig 0:796d0f61a05b 303 * \note md_alg may be POLARSSL_MD_NONE, only if hash_len != 0
HannesTschofenig 0:796d0f61a05b 304 */
HannesTschofenig 0:796d0f61a05b 305 int pk_verify( pk_context *ctx, md_type_t md_alg,
HannesTschofenig 0:796d0f61a05b 306 const unsigned char *hash, size_t hash_len,
HannesTschofenig 0:796d0f61a05b 307 const unsigned char *sig, size_t sig_len );
HannesTschofenig 0:796d0f61a05b 308
HannesTschofenig 0:796d0f61a05b 309 /**
HannesTschofenig 0:796d0f61a05b 310 * \brief Make signature
HannesTschofenig 0:796d0f61a05b 311 *
HannesTschofenig 0:796d0f61a05b 312 * \param ctx PK context to use
HannesTschofenig 0:796d0f61a05b 313 * \param md_alg Hash algorithm used (see notes)
HannesTschofenig 0:796d0f61a05b 314 * \param hash Hash of the message to sign
HannesTschofenig 0:796d0f61a05b 315 * \param hash_len Hash length or 0 (see notes)
HannesTschofenig 0:796d0f61a05b 316 * \param sig Place to write the signature
HannesTschofenig 0:796d0f61a05b 317 * \param sig_len Number of bytes written
HannesTschofenig 0:796d0f61a05b 318 * \param f_rng RNG function
HannesTschofenig 0:796d0f61a05b 319 * \param p_rng RNG parameter
HannesTschofenig 0:796d0f61a05b 320 *
HannesTschofenig 0:796d0f61a05b 321 * \return 0 on success, or a specific error code.
HannesTschofenig 0:796d0f61a05b 322 *
HannesTschofenig 0:796d0f61a05b 323 * \note If hash_len is 0, then the length associated with md_alg
HannesTschofenig 0:796d0f61a05b 324 * is used instead, or an error returned if it is invalid.
HannesTschofenig 0:796d0f61a05b 325 *
HannesTschofenig 0:796d0f61a05b 326 * \note md_alg may be POLARSSL_MD_NONE, only if hash_len != 0
HannesTschofenig 0:796d0f61a05b 327 */
HannesTschofenig 0:796d0f61a05b 328 int pk_sign( pk_context *ctx, md_type_t md_alg,
HannesTschofenig 0:796d0f61a05b 329 const unsigned char *hash, size_t hash_len,
HannesTschofenig 0:796d0f61a05b 330 unsigned char *sig, size_t *sig_len,
HannesTschofenig 0:796d0f61a05b 331 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
HannesTschofenig 0:796d0f61a05b 332
HannesTschofenig 0:796d0f61a05b 333 /**
HannesTschofenig 0:796d0f61a05b 334 * \brief Decrypt message
HannesTschofenig 0:796d0f61a05b 335 *
HannesTschofenig 0:796d0f61a05b 336 * \param ctx PK context to use
HannesTschofenig 0:796d0f61a05b 337 * \param input Input to decrypt
HannesTschofenig 0:796d0f61a05b 338 * \param ilen Input size
HannesTschofenig 0:796d0f61a05b 339 * \param output Decrypted output
HannesTschofenig 0:796d0f61a05b 340 * \param olen Decrypted message length
HannesTschofenig 0:796d0f61a05b 341 * \param osize Size of the output buffer
HannesTschofenig 0:796d0f61a05b 342 * \param f_rng RNG function
HannesTschofenig 0:796d0f61a05b 343 * \param p_rng RNG parameter
HannesTschofenig 0:796d0f61a05b 344 *
HannesTschofenig 0:796d0f61a05b 345 * \return 0 on success, or a specific error code.
HannesTschofenig 0:796d0f61a05b 346 */
HannesTschofenig 0:796d0f61a05b 347 int pk_decrypt( pk_context *ctx,
HannesTschofenig 0:796d0f61a05b 348 const unsigned char *input, size_t ilen,
HannesTschofenig 0:796d0f61a05b 349 unsigned char *output, size_t *olen, size_t osize,
HannesTschofenig 0:796d0f61a05b 350 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
HannesTschofenig 0:796d0f61a05b 351
HannesTschofenig 0:796d0f61a05b 352 /**
HannesTschofenig 0:796d0f61a05b 353 * \brief Encrypt message
HannesTschofenig 0:796d0f61a05b 354 *
HannesTschofenig 0:796d0f61a05b 355 * \param ctx PK context to use
HannesTschofenig 0:796d0f61a05b 356 * \param input Message to encrypt
HannesTschofenig 0:796d0f61a05b 357 * \param ilen Message size
HannesTschofenig 0:796d0f61a05b 358 * \param output Encrypted output
HannesTschofenig 0:796d0f61a05b 359 * \param olen Encrypted output length
HannesTschofenig 0:796d0f61a05b 360 * \param osize Size of the output buffer
HannesTschofenig 0:796d0f61a05b 361 * \param f_rng RNG function
HannesTschofenig 0:796d0f61a05b 362 * \param p_rng RNG parameter
HannesTschofenig 0:796d0f61a05b 363 *
HannesTschofenig 0:796d0f61a05b 364 * \return 0 on success, or a specific error code.
HannesTschofenig 0:796d0f61a05b 365 */
HannesTschofenig 0:796d0f61a05b 366 int pk_encrypt( pk_context *ctx,
HannesTschofenig 0:796d0f61a05b 367 const unsigned char *input, size_t ilen,
HannesTschofenig 0:796d0f61a05b 368 unsigned char *output, size_t *olen, size_t osize,
HannesTschofenig 0:796d0f61a05b 369 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
HannesTschofenig 0:796d0f61a05b 370
HannesTschofenig 0:796d0f61a05b 371 /**
HannesTschofenig 0:796d0f61a05b 372 * \brief Export debug information
HannesTschofenig 0:796d0f61a05b 373 *
HannesTschofenig 0:796d0f61a05b 374 * \param ctx Context to use
HannesTschofenig 0:796d0f61a05b 375 * \param items Place to write debug items
HannesTschofenig 0:796d0f61a05b 376 *
HannesTschofenig 0:796d0f61a05b 377 * \return 0 on success or POLARSSL_ERR_PK_BAD_INPUT_DATA
HannesTschofenig 0:796d0f61a05b 378 */
HannesTschofenig 0:796d0f61a05b 379 int pk_debug( const pk_context *ctx, pk_debug_item *items );
HannesTschofenig 0:796d0f61a05b 380
HannesTschofenig 0:796d0f61a05b 381 /**
HannesTschofenig 0:796d0f61a05b 382 * \brief Access the type name
HannesTschofenig 0:796d0f61a05b 383 *
HannesTschofenig 0:796d0f61a05b 384 * \param ctx Context to use
HannesTschofenig 0:796d0f61a05b 385 *
HannesTschofenig 0:796d0f61a05b 386 * \return Type name on success, or "invalid PK"
HannesTschofenig 0:796d0f61a05b 387 */
HannesTschofenig 0:796d0f61a05b 388 const char * pk_get_name( const pk_context *ctx );
HannesTschofenig 0:796d0f61a05b 389
HannesTschofenig 0:796d0f61a05b 390 /**
HannesTschofenig 0:796d0f61a05b 391 * \brief Get the key type
HannesTschofenig 0:796d0f61a05b 392 *
HannesTschofenig 0:796d0f61a05b 393 * \param ctx Context to use
HannesTschofenig 0:796d0f61a05b 394 *
HannesTschofenig 0:796d0f61a05b 395 * \return Type on success, or POLARSSL_PK_NONE
HannesTschofenig 0:796d0f61a05b 396 */
HannesTschofenig 0:796d0f61a05b 397 pk_type_t pk_get_type( const pk_context *ctx );
HannesTschofenig 0:796d0f61a05b 398
HannesTschofenig 0:796d0f61a05b 399 #if defined(POLARSSL_PK_PARSE_C)
HannesTschofenig 0:796d0f61a05b 400 /** \ingroup pk_module */
HannesTschofenig 0:796d0f61a05b 401 /**
HannesTschofenig 0:796d0f61a05b 402 * \brief Parse a private key
HannesTschofenig 0:796d0f61a05b 403 *
HannesTschofenig 0:796d0f61a05b 404 * \param ctx key to be initialized
HannesTschofenig 0:796d0f61a05b 405 * \param key input buffer
HannesTschofenig 0:796d0f61a05b 406 * \param keylen size of the buffer
HannesTschofenig 0:796d0f61a05b 407 * \param pwd password for decryption (optional)
HannesTschofenig 0:796d0f61a05b 408 * \param pwdlen size of the password
HannesTschofenig 0:796d0f61a05b 409 *
HannesTschofenig 0:796d0f61a05b 410 * \note On entry, ctx must be empty, either freshly initialised
HannesTschofenig 0:796d0f61a05b 411 * with pk_init() or reset with pk_free(). If you need a
HannesTschofenig 0:796d0f61a05b 412 * specific key type, check the result with pk_can_do().
HannesTschofenig 0:796d0f61a05b 413 *
HannesTschofenig 0:796d0f61a05b 414 * \note The key is also checked for correctness.
HannesTschofenig 0:796d0f61a05b 415 *
HannesTschofenig 0:796d0f61a05b 416 * \return 0 if successful, or a specific PK or PEM error code
HannesTschofenig 0:796d0f61a05b 417 */
HannesTschofenig 0:796d0f61a05b 418 int pk_parse_key( pk_context *ctx,
HannesTschofenig 0:796d0f61a05b 419 const unsigned char *key, size_t keylen,
HannesTschofenig 0:796d0f61a05b 420 const unsigned char *pwd, size_t pwdlen );
HannesTschofenig 0:796d0f61a05b 421
HannesTschofenig 0:796d0f61a05b 422 /** \ingroup pk_module */
HannesTschofenig 0:796d0f61a05b 423 /**
HannesTschofenig 0:796d0f61a05b 424 * \brief Parse a public key
HannesTschofenig 0:796d0f61a05b 425 *
HannesTschofenig 0:796d0f61a05b 426 * \param ctx key to be initialized
HannesTschofenig 0:796d0f61a05b 427 * \param key input buffer
HannesTschofenig 0:796d0f61a05b 428 * \param keylen size of the buffer
HannesTschofenig 0:796d0f61a05b 429 *
HannesTschofenig 0:796d0f61a05b 430 * \note On entry, ctx must be empty, either freshly initialised
HannesTschofenig 0:796d0f61a05b 431 * with pk_init() or reset with pk_free(). If you need a
HannesTschofenig 0:796d0f61a05b 432 * specific key type, check the result with pk_can_do().
HannesTschofenig 0:796d0f61a05b 433 *
HannesTschofenig 0:796d0f61a05b 434 * \note The key is also checked for correctness.
HannesTschofenig 0:796d0f61a05b 435 *
HannesTschofenig 0:796d0f61a05b 436 * \return 0 if successful, or a specific PK or PEM error code
HannesTschofenig 0:796d0f61a05b 437 */
HannesTschofenig 0:796d0f61a05b 438 int pk_parse_public_key( pk_context *ctx,
HannesTschofenig 0:796d0f61a05b 439 const unsigned char *key, size_t keylen );
HannesTschofenig 0:796d0f61a05b 440
HannesTschofenig 0:796d0f61a05b 441 #if defined(POLARSSL_FS_IO)
HannesTschofenig 0:796d0f61a05b 442 /** \ingroup pk_module */
HannesTschofenig 0:796d0f61a05b 443 /**
HannesTschofenig 0:796d0f61a05b 444 * \brief Load and parse a private key
HannesTschofenig 0:796d0f61a05b 445 *
HannesTschofenig 0:796d0f61a05b 446 * \param ctx key to be initialized
HannesTschofenig 0:796d0f61a05b 447 * \param path filename to read the private key from
HannesTschofenig 0:796d0f61a05b 448 * \param password password to decrypt the file (can be NULL)
HannesTschofenig 0:796d0f61a05b 449 *
HannesTschofenig 0:796d0f61a05b 450 * \note On entry, ctx must be empty, either freshly initialised
HannesTschofenig 0:796d0f61a05b 451 * with pk_init() or reset with pk_free(). If you need a
HannesTschofenig 0:796d0f61a05b 452 * specific key type, check the result with pk_can_do().
HannesTschofenig 0:796d0f61a05b 453 *
HannesTschofenig 0:796d0f61a05b 454 * \note The key is also checked for correctness.
HannesTschofenig 0:796d0f61a05b 455 *
HannesTschofenig 0:796d0f61a05b 456 * \return 0 if successful, or a specific PK or PEM error code
HannesTschofenig 0:796d0f61a05b 457 */
HannesTschofenig 0:796d0f61a05b 458 int pk_parse_keyfile( pk_context *ctx,
HannesTschofenig 0:796d0f61a05b 459 const char *path, const char *password );
HannesTschofenig 0:796d0f61a05b 460
HannesTschofenig 0:796d0f61a05b 461 /** \ingroup pk_module */
HannesTschofenig 0:796d0f61a05b 462 /**
HannesTschofenig 0:796d0f61a05b 463 * \brief Load and parse a public key
HannesTschofenig 0:796d0f61a05b 464 *
HannesTschofenig 0:796d0f61a05b 465 * \param ctx key to be initialized
HannesTschofenig 0:796d0f61a05b 466 * \param path filename to read the private key from
HannesTschofenig 0:796d0f61a05b 467 *
HannesTschofenig 0:796d0f61a05b 468 * \note On entry, ctx must be empty, either freshly initialised
HannesTschofenig 0:796d0f61a05b 469 * with pk_init() or reset with pk_free(). If you need a
HannesTschofenig 0:796d0f61a05b 470 * specific key type, check the result with pk_can_do().
HannesTschofenig 0:796d0f61a05b 471 *
HannesTschofenig 0:796d0f61a05b 472 * \note The key is also checked for correctness.
HannesTschofenig 0:796d0f61a05b 473 *
HannesTschofenig 0:796d0f61a05b 474 * \return 0 if successful, or a specific PK or PEM error code
HannesTschofenig 0:796d0f61a05b 475 */
HannesTschofenig 0:796d0f61a05b 476 int pk_parse_public_keyfile( pk_context *ctx, const char *path );
HannesTschofenig 0:796d0f61a05b 477 #endif /* POLARSSL_FS_IO */
HannesTschofenig 0:796d0f61a05b 478 #endif /* POLARSSL_PK_PARSE_C */
HannesTschofenig 0:796d0f61a05b 479
HannesTschofenig 0:796d0f61a05b 480 #if defined(POLARSSL_PK_WRITE_C)
HannesTschofenig 0:796d0f61a05b 481 /**
HannesTschofenig 0:796d0f61a05b 482 * \brief Write a private key to a PKCS#1 or SEC1 DER structure
HannesTschofenig 0:796d0f61a05b 483 * Note: data is written at the end of the buffer! Use the
HannesTschofenig 0:796d0f61a05b 484 * return value to determine where you should start
HannesTschofenig 0:796d0f61a05b 485 * using the buffer
HannesTschofenig 0:796d0f61a05b 486 *
HannesTschofenig 0:796d0f61a05b 487 * \param ctx private to write away
HannesTschofenig 0:796d0f61a05b 488 * \param buf buffer to write to
HannesTschofenig 0:796d0f61a05b 489 * \param size size of the buffer
HannesTschofenig 0:796d0f61a05b 490 *
HannesTschofenig 0:796d0f61a05b 491 * \return length of data written if successful, or a specific
HannesTschofenig 0:796d0f61a05b 492 * error code
HannesTschofenig 0:796d0f61a05b 493 */
HannesTschofenig 0:796d0f61a05b 494 int pk_write_key_der( pk_context *ctx, unsigned char *buf, size_t size );
HannesTschofenig 0:796d0f61a05b 495
HannesTschofenig 0:796d0f61a05b 496 /**
HannesTschofenig 0:796d0f61a05b 497 * \brief Write a public key to a SubjectPublicKeyInfo DER structure
HannesTschofenig 0:796d0f61a05b 498 * Note: data is written at the end of the buffer! Use the
HannesTschofenig 0:796d0f61a05b 499 * return value to determine where you should start
HannesTschofenig 0:796d0f61a05b 500 * using the buffer
HannesTschofenig 0:796d0f61a05b 501 *
HannesTschofenig 0:796d0f61a05b 502 * \param ctx public key to write away
HannesTschofenig 0:796d0f61a05b 503 * \param buf buffer to write to
HannesTschofenig 0:796d0f61a05b 504 * \param size size of the buffer
HannesTschofenig 0:796d0f61a05b 505 *
HannesTschofenig 0:796d0f61a05b 506 * \return length of data written if successful, or a specific
HannesTschofenig 0:796d0f61a05b 507 * error code
HannesTschofenig 0:796d0f61a05b 508 */
HannesTschofenig 0:796d0f61a05b 509 int pk_write_pubkey_der( pk_context *ctx, unsigned char *buf, size_t size );
HannesTschofenig 0:796d0f61a05b 510
HannesTschofenig 0:796d0f61a05b 511 #if defined(POLARSSL_PEM_WRITE_C)
HannesTschofenig 0:796d0f61a05b 512 /**
HannesTschofenig 0:796d0f61a05b 513 * \brief Write a public key to a PEM string
HannesTschofenig 0:796d0f61a05b 514 *
HannesTschofenig 0:796d0f61a05b 515 * \param ctx public key to write away
HannesTschofenig 0:796d0f61a05b 516 * \param buf buffer to write to
HannesTschofenig 0:796d0f61a05b 517 * \param size size of the buffer
HannesTschofenig 0:796d0f61a05b 518 *
HannesTschofenig 0:796d0f61a05b 519 * \return 0 successful, or a specific error code
HannesTschofenig 0:796d0f61a05b 520 */
HannesTschofenig 0:796d0f61a05b 521 int pk_write_pubkey_pem( pk_context *ctx, unsigned char *buf, size_t size );
HannesTschofenig 0:796d0f61a05b 522
HannesTschofenig 0:796d0f61a05b 523 /**
HannesTschofenig 0:796d0f61a05b 524 * \brief Write a private key to a PKCS#1 or SEC1 PEM string
HannesTschofenig 0:796d0f61a05b 525 *
HannesTschofenig 0:796d0f61a05b 526 * \param ctx private to write away
HannesTschofenig 0:796d0f61a05b 527 * \param buf buffer to write to
HannesTschofenig 0:796d0f61a05b 528 * \param size size of the buffer
HannesTschofenig 0:796d0f61a05b 529 *
HannesTschofenig 0:796d0f61a05b 530 * \return 0 successful, or a specific error code
HannesTschofenig 0:796d0f61a05b 531 */
HannesTschofenig 0:796d0f61a05b 532 int pk_write_key_pem( pk_context *ctx, unsigned char *buf, size_t size );
HannesTschofenig 0:796d0f61a05b 533 #endif /* POLARSSL_PEM_WRITE_C */
HannesTschofenig 0:796d0f61a05b 534 #endif /* POLARSSL_PK_WRITE_C */
HannesTschofenig 0:796d0f61a05b 535
HannesTschofenig 0:796d0f61a05b 536 /*
HannesTschofenig 0:796d0f61a05b 537 * WARNING: Low-level functions. You probably do not want to use these unless
HannesTschofenig 0:796d0f61a05b 538 * you are certain you do ;)
HannesTschofenig 0:796d0f61a05b 539 */
HannesTschofenig 0:796d0f61a05b 540
HannesTschofenig 0:796d0f61a05b 541 #if defined(POLARSSL_PK_PARSE_C)
HannesTschofenig 0:796d0f61a05b 542 /**
HannesTschofenig 0:796d0f61a05b 543 * \brief Parse a SubjectPublicKeyInfo DER structure
HannesTschofenig 0:796d0f61a05b 544 *
HannesTschofenig 0:796d0f61a05b 545 * \param p the position in the ASN.1 data
HannesTschofenig 0:796d0f61a05b 546 * \param end end of the buffer
HannesTschofenig 0:796d0f61a05b 547 * \param pk the key to fill
HannesTschofenig 0:796d0f61a05b 548 *
HannesTschofenig 0:796d0f61a05b 549 * \return 0 if successful, or a specific PK error code
HannesTschofenig 0:796d0f61a05b 550 */
HannesTschofenig 0:796d0f61a05b 551 int pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
HannesTschofenig 0:796d0f61a05b 552 pk_context *pk );
HannesTschofenig 0:796d0f61a05b 553 #endif /* POLARSSL_PK_PARSE_C */
HannesTschofenig 0:796d0f61a05b 554
HannesTschofenig 0:796d0f61a05b 555 #if defined(POLARSSL_PK_WRITE_C)
HannesTschofenig 0:796d0f61a05b 556 /**
HannesTschofenig 0:796d0f61a05b 557 * \brief Write a subjectPublicKey to ASN.1 data
HannesTschofenig 0:796d0f61a05b 558 * Note: function works backwards in data buffer
HannesTschofenig 0:796d0f61a05b 559 *
HannesTschofenig 0:796d0f61a05b 560 * \param p reference to current position pointer
HannesTschofenig 0:796d0f61a05b 561 * \param start start of the buffer (for bounds-checking)
HannesTschofenig 0:796d0f61a05b 562 * \param key public key to write away
HannesTschofenig 0:796d0f61a05b 563 *
HannesTschofenig 0:796d0f61a05b 564 * \return the length written or a negative error code
HannesTschofenig 0:796d0f61a05b 565 */
HannesTschofenig 0:796d0f61a05b 566 int pk_write_pubkey( unsigned char **p, unsigned char *start,
HannesTschofenig 0:796d0f61a05b 567 const pk_context *key );
HannesTschofenig 0:796d0f61a05b 568 #endif /* POLARSSL_PK_WRITE_C */
HannesTschofenig 0:796d0f61a05b 569
HannesTschofenig 0:796d0f61a05b 570 #ifdef __cplusplus
HannesTschofenig 0:796d0f61a05b 571 }
HannesTschofenig 0:796d0f61a05b 572 #endif
HannesTschofenig 0:796d0f61a05b 573
HannesTschofenig 0:796d0f61a05b 574 #endif /* POLARSSL_PK_H */
HannesTschofenig 0:796d0f61a05b 575
HannesTschofenig 0:796d0f61a05b 576