| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 1 | /** | 
| Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 2 | * \file pk_internal.h | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 3 | * | 
|  | 4 | * \brief Public Key abstraction layer: wrapper functions | 
| Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 5 | */ | 
|  | 6 | /* | 
| Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 7 | *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 8 | *  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é-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 21 | * | 
| Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 22 | *  This file is part of mbed TLS (https://tls.mbed.org) | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 23 | */ | 
|  | 24 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | #ifndef MBEDTLS_PK_WRAP_H | 
|  | 26 | #define MBEDTLS_PK_WRAP_H | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 27 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if !defined(MBEDTLS_CONFIG_FILE) | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 29 | #include "config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 30 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 31 | #include MBEDTLS_CONFIG_FILE | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #endif | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 33 |  | 
|  | 34 | #include "pk.h" | 
|  | 35 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | struct mbedtls_pk_info_t | 
| Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 37 | { | 
|  | 38 | /** Public key type */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 39 | mbedtls_pk_type_t type; | 
| Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 40 |  | 
|  | 41 | /** Type name */ | 
|  | 42 | const char *name; | 
|  | 43 |  | 
|  | 44 | /** Get key size in bits */ | 
| Manuel Pégourié-Gonnard | 39a48f4 | 2015-06-18 16:06:55 +0200 | [diff] [blame] | 45 | size_t (*get_bitlen)( const void * ); | 
| Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 46 |  | 
|  | 47 | /** Tell if the context implements this type (e.g. ECKEY can do ECDSA) */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 48 | int (*can_do)( mbedtls_pk_type_t type ); | 
| Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 49 |  | 
|  | 50 | /** Verify signature */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 51 | int (*verify_func)( void *ctx, mbedtls_md_type_t md_alg, | 
| Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 52 | const unsigned char *hash, size_t hash_len, | 
|  | 53 | const unsigned char *sig, size_t sig_len ); | 
|  | 54 |  | 
|  | 55 | /** Make signature */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | int (*sign_func)( void *ctx, mbedtls_md_type_t md_alg, | 
| Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 57 | const unsigned char *hash, size_t hash_len, | 
|  | 58 | unsigned char *sig, size_t *sig_len, | 
|  | 59 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 60 | void *p_rng ); | 
|  | 61 |  | 
| Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 62 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) | 
| Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 63 | /** Verify signature (restartable) */ | 
|  | 64 | int (*verify_rs_func)( void *ctx, mbedtls_md_type_t md_alg, | 
|  | 65 | const unsigned char *hash, size_t hash_len, | 
|  | 66 | const unsigned char *sig, size_t sig_len, | 
|  | 67 | void *rs_ctx ); | 
|  | 68 |  | 
|  | 69 | /** Make signature (restartable) */ | 
|  | 70 | int (*sign_rs_func)( void *ctx, mbedtls_md_type_t md_alg, | 
|  | 71 | const unsigned char *hash, size_t hash_len, | 
|  | 72 | unsigned char *sig, size_t *sig_len, | 
|  | 73 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 74 | void *p_rng, void *rs_ctx ); | 
| Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 75 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ | 
| Manuel Pégourié-Gonnard | 1f59606 | 2017-05-09 10:42:40 +0200 | [diff] [blame] | 76 |  | 
| Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 77 | /** Decrypt message */ | 
|  | 78 | int (*decrypt_func)( void *ctx, const unsigned char *input, size_t ilen, | 
|  | 79 | unsigned char *output, size_t *olen, size_t osize, | 
|  | 80 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 81 | void *p_rng ); | 
|  | 82 |  | 
|  | 83 | /** Encrypt message */ | 
|  | 84 | int (*encrypt_func)( void *ctx, const unsigned char *input, size_t ilen, | 
|  | 85 | unsigned char *output, size_t *olen, size_t osize, | 
|  | 86 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 87 | void *p_rng ); | 
|  | 88 |  | 
|  | 89 | /** Check public-private key pair */ | 
|  | 90 | int (*check_pair_func)( const void *pub, const void *prv ); | 
|  | 91 |  | 
|  | 92 | /** Allocate a new context */ | 
|  | 93 | void * (*ctx_alloc_func)( void ); | 
|  | 94 |  | 
|  | 95 | /** Free the given context */ | 
|  | 96 | void (*ctx_free_func)( void *ctx ); | 
|  | 97 |  | 
| Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 98 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) | 
| Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 99 | /** Allocate the restart context */ | 
|  | 100 | void * (*rs_alloc_func)( void ); | 
|  | 101 |  | 
|  | 102 | /** Free the restart context */ | 
|  | 103 | void (*rs_free_func)( void *rs_ctx ); | 
| Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 104 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ | 
| Manuel Pégourié-Gonnard | 0bbc66c | 2017-08-18 16:22:06 +0200 | [diff] [blame] | 105 |  | 
| Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 106 | /** Interface with the debug module */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 107 | void (*debug_func)( const void *ctx, mbedtls_pk_debug_item *items ); | 
| Manuel Pégourié-Gonnard | c89d6cf | 2015-03-31 14:43:19 +0200 | [diff] [blame] | 108 |  | 
|  | 109 | }; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 110 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 111 | /* Container for RSA-alt */ | 
|  | 112 | typedef struct | 
|  | 113 | { | 
|  | 114 | void *key; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 115 | mbedtls_pk_rsa_alt_decrypt_func decrypt_func; | 
|  | 116 | mbedtls_pk_rsa_alt_sign_func sign_func; | 
|  | 117 | mbedtls_pk_rsa_alt_key_len_func key_len_func; | 
|  | 118 | } mbedtls_rsa_alt_context; | 
| Manuel Pégourié-Gonnard | 348bcb3 | 2015-03-31 14:01:33 +0200 | [diff] [blame] | 119 | #endif | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 120 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 121 | #if defined(MBEDTLS_RSA_C) | 
|  | 122 | extern const mbedtls_pk_info_t mbedtls_rsa_info; | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 123 | #endif | 
|  | 124 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 125 | #if defined(MBEDTLS_ECP_C) | 
|  | 126 | extern const mbedtls_pk_info_t mbedtls_eckey_info; | 
|  | 127 | extern const mbedtls_pk_info_t mbedtls_eckeydh_info; | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 128 | #endif | 
|  | 129 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | #if defined(MBEDTLS_ECDSA_C) | 
|  | 131 | extern const mbedtls_pk_info_t mbedtls_ecdsa_info; | 
| Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 132 | #endif | 
|  | 133 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 134 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) | 
|  | 135 | extern const mbedtls_pk_info_t mbedtls_rsa_alt_info; | 
| Manuel Pégourié-Gonnard | 348bcb3 | 2015-03-31 14:01:33 +0200 | [diff] [blame] | 136 | #endif | 
| Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 137 |  | 
| Manuel Pégourié-Gonnard | 1ecf92c3 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 138 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Manuel Pégourié-Gonnard | 276cb64 | 2018-11-06 09:34:30 +0100 | [diff] [blame] | 139 | extern const mbedtls_pk_info_t mbedtls_pk_opaque_info; | 
| Manuel Pégourié-Gonnard | 1ecf92c3 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 140 | #endif | 
|  | 141 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 142 | #endif /* MBEDTLS_PK_WRAP_H */ |