blob: 032d59524887040ede6af9be3df3958782c61aca [file] [log] [blame]
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02001/**
Darryl Greena40a1012018-01-05 15:33:17 +00002 * \file pk_internal.h
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02003 *
4 * \brief Public Key abstraction layer: wrapper functions
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020021 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020023 */
24
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#ifndef MBEDTLS_PK_WRAP_H
26#define MBEDTLS_PK_WRAP_H
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020029#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#endif
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020033
34#include "pk.h"
35
Manuel Pégourié-Gonnard1c1cc0d2019-09-19 10:45:14 +020036/* Dummy definition to keep check-names.sh happy - don't uncomment */
37//#define MBEDTLS_PK_INFO_ECKEY
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039struct mbedtls_pk_info_t
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020040{
41 /** Public key type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042 mbedtls_pk_type_t type;
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020043
44 /** Type name */
45 const char *name;
46
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +020047 /** Get key size in bits (must be valid)*/
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +020048 size_t (*get_bitlen)( const void * );
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020049
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +020050 /** Tell if the context implements this type (e.g. ECKEY can do ECDSA)
51 * (must be valid) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 int (*can_do)( mbedtls_pk_type_t type );
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020053
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +020054 /** Verify signature (may be NULL) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055 int (*verify_func)( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020056 const unsigned char *hash, size_t hash_len,
57 const unsigned char *sig, size_t sig_len );
58
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +020059 /** Make signature (may be NULL)*/
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 int (*sign_func)( void *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020061 const unsigned char *hash, size_t hash_len,
62 unsigned char *sig, size_t *sig_len,
63 int (*f_rng)(void *, unsigned char *, size_t),
64 void *p_rng );
65
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020066#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +020067 /** Verify signature (restartable) (may be NULL) */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +020068 int (*verify_rs_func)( void *ctx, mbedtls_md_type_t md_alg,
69 const unsigned char *hash, size_t hash_len,
70 const unsigned char *sig, size_t sig_len,
71 void *rs_ctx );
72
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +020073 /** Make signature (restartable) (may be NULL) */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +020074 int (*sign_rs_func)( void *ctx, mbedtls_md_type_t md_alg,
75 const unsigned char *hash, size_t hash_len,
76 unsigned char *sig, size_t *sig_len,
77 int (*f_rng)(void *, unsigned char *, size_t),
78 void *p_rng, void *rs_ctx );
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020079#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +020080
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +020081 /** Decrypt message (may be NULL) */
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020082 int (*decrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
83 unsigned char *output, size_t *olen, size_t osize,
84 int (*f_rng)(void *, unsigned char *, size_t),
85 void *p_rng );
86
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +020087 /** Encrypt message (may be NULL ) */
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020088 int (*encrypt_func)( void *ctx, const unsigned char *input, size_t ilen,
89 unsigned char *output, size_t *olen, size_t osize,
90 int (*f_rng)(void *, unsigned char *, size_t),
91 void *p_rng );
92
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +020093 /** Check public-private key pair (may be NULL) */
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020094 int (*check_pair_func)( const void *pub, const void *prv );
95
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +020096 /** Allocate a new context (must be valid) */
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +020097 void * (*ctx_alloc_func)( void );
98
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +020099 /** Free the given context (must be valid) */
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +0200100 void (*ctx_free_func)( void *ctx );
101
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200102#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +0200103 /** Allocate the restart context (may be NULL)*/
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200104 void * (*rs_alloc_func)( void );
105
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +0200106 /** Free the restart context (may be NULL) */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200107 void (*rs_free_func)( void *rs_ctx );
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200108#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200109
Manuel Pégourié-Gonnard57d96cd2019-09-19 10:45:14 +0200110 /** Interface with the debug module (may be NULL) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 void (*debug_func)( const void *ctx, mbedtls_pk_debug_item *items );
Manuel Pégourié-Gonnardc89d6cf2015-03-31 14:43:19 +0200112
113};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200115/* Container for RSA-alt */
116typedef struct
117{
118 void *key;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 mbedtls_pk_rsa_alt_decrypt_func decrypt_func;
120 mbedtls_pk_rsa_alt_sign_func sign_func;
121 mbedtls_pk_rsa_alt_key_len_func key_len_func;
122} mbedtls_rsa_alt_context;
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +0200123#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#if defined(MBEDTLS_RSA_C)
126extern const mbedtls_pk_info_t mbedtls_rsa_info;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200127#endif
128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129#if defined(MBEDTLS_ECP_C)
130extern const mbedtls_pk_info_t mbedtls_eckey_info;
131extern const mbedtls_pk_info_t mbedtls_eckeydh_info;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200132#endif
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#if defined(MBEDTLS_ECDSA_C)
135extern const mbedtls_pk_info_t mbedtls_ecdsa_info;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200136#endif
137
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300138#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeradf11e12019-08-21 13:03:44 +0100139extern const mbedtls_pk_info_t mbedtls_uecc_eckey_info;
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300140#endif
141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
143extern const mbedtls_pk_info_t mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +0200144#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#endif /* MBEDTLS_PK_WRAP_H */