mbed TLS v2.7.16
pk.h
Go to the documentation of this file.
1 
6 /*
7  * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
8  * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9  *
10  * This file is provided under the Apache License 2.0, or the
11  * GNU General Public License v2.0 or later.
12  *
13  * **********
14  * Apache License 2.0:
15  *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may
17  * not use this file except in compliance with the License.
18  * You may obtain a copy of the License at
19  *
20  * http://www.apache.org/licenses/LICENSE-2.0
21  *
22  * Unless required by applicable law or agreed to in writing, software
23  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25  * See the License for the specific language governing permissions and
26  * limitations under the License.
27  *
28  * **********
29  *
30  * **********
31  * GNU General Public License v2.0 or later:
32  *
33  * This program is free software; you can redistribute it and/or modify
34  * it under the terms of the GNU General Public License as published by
35  * the Free Software Foundation; either version 2 of the License, or
36  * (at your option) any later version.
37  *
38  * This program is distributed in the hope that it will be useful,
39  * but WITHOUT ANY WARRANTY; without even the implied warranty of
40  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41  * GNU General Public License for more details.
42  *
43  * You should have received a copy of the GNU General Public License along
44  * with this program; if not, write to the Free Software Foundation, Inc.,
45  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46  *
47  * **********
48  *
49  * This file is part of mbed TLS (https://tls.mbed.org)
50  */
51 
52 #ifndef MBEDTLS_PK_H
53 #define MBEDTLS_PK_H
54 
55 #if !defined(MBEDTLS_CONFIG_FILE)
56 #include "config.h"
57 #else
58 #include MBEDTLS_CONFIG_FILE
59 #endif
60 
61 #include "md.h"
62 
63 #if defined(MBEDTLS_RSA_C)
64 #include "rsa.h"
65 #endif
66 
67 #if defined(MBEDTLS_ECP_C)
68 #include "ecp.h"
69 #endif
70 
71 #if defined(MBEDTLS_ECDSA_C)
72 #include "ecdsa.h"
73 #endif
74 
75 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
76  !defined(inline) && !defined(__cplusplus)
77 #define inline __inline
78 #endif
79 
80 #define MBEDTLS_ERR_PK_ALLOC_FAILED -0x3F80
81 #define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00
82 #define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x3E80
83 #define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00
84 #define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80
85 #define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00
86 #define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80
87 #define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00
88 #define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80
89 #define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00
90 #define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80
91 #define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00
92 #define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980
93 #define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x3900
94 #define MBEDTLS_ERR_PK_HW_ACCEL_FAILED -0x3880
96 #ifdef __cplusplus
97 extern "C" {
98 #endif
99 
103 typedef enum {
112 
117 typedef struct
118 {
121 
123 
127 typedef enum
128 {
133 
137 typedef struct
138 {
140  const char *name;
141  void *value;
143 
145 #define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
146 
151 
155 typedef struct
156 {
158  void * pk_ctx;
160 
161 #if defined(MBEDTLS_RSA_C)
162 
169 {
170  return( (mbedtls_rsa_context *) (pk).pk_ctx );
171 }
172 #endif /* MBEDTLS_RSA_C */
173 
174 #if defined(MBEDTLS_ECP_C)
175 
182 {
183  return( (mbedtls_ecp_keypair *) (pk).pk_ctx );
184 }
185 #endif /* MBEDTLS_ECP_C */
186 
187 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
188 
191 typedef int (*mbedtls_pk_rsa_alt_decrypt_func)( void *ctx, int mode, size_t *olen,
192  const unsigned char *input, unsigned char *output,
193  size_t output_max_len );
194 typedef int (*mbedtls_pk_rsa_alt_sign_func)( void *ctx,
195  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
196  int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
197  const unsigned char *hash, unsigned char *sig );
198 typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)( void *ctx );
199 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
200 
209 
214 
219 
234 int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info );
235 
236 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
237 
251 int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
252  mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
254  mbedtls_pk_rsa_alt_key_len_func key_len_func );
255 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
256 
264 size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx );
265 
272 static inline size_t mbedtls_pk_get_len( const mbedtls_pk_context *ctx )
273 {
274  return( ( mbedtls_pk_get_bitlen( ctx ) + 7 ) / 8 );
275 }
276 
287 
313  const unsigned char *hash, size_t hash_len,
314  const unsigned char *sig, size_t sig_len );
315 
345 int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
347  const unsigned char *hash, size_t hash_len,
348  const unsigned char *sig, size_t sig_len );
349 
379  const unsigned char *hash, size_t hash_len,
380  unsigned char *sig, size_t *sig_len,
381  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
382 
400  const unsigned char *input, size_t ilen,
401  unsigned char *output, size_t *olen, size_t osize,
402  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
403 
421  const unsigned char *input, size_t ilen,
422  unsigned char *output, size_t *olen, size_t osize,
423  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
424 
433 int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv );
434 
444 
452 const char * mbedtls_pk_get_name( const mbedtls_pk_context *ctx );
453 
462 
463 #if defined(MBEDTLS_PK_PARSE_C)
464 
484  const unsigned char *key, size_t keylen,
485  const unsigned char *pwd, size_t pwdlen );
486 
505  const unsigned char *key, size_t keylen );
506 
507 #if defined(MBEDTLS_FS_IO)
508 
525  const char *path, const char *password );
526 
543 int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path );
544 #endif /* MBEDTLS_FS_IO */
545 #endif /* MBEDTLS_PK_PARSE_C */
546 
547 #if defined(MBEDTLS_PK_WRITE_C)
548 
561 int mbedtls_pk_write_key_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
562 
576 int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
577 
578 #if defined(MBEDTLS_PEM_WRITE_C)
579 
588 int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
589 
599 int mbedtls_pk_write_key_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
600 #endif /* MBEDTLS_PEM_WRITE_C */
601 #endif /* MBEDTLS_PK_WRITE_C */
602 
603 /*
604  * WARNING: Low-level functions. You probably do not want to use these unless
605  * you are certain you do ;)
606  */
607 
608 #if defined(MBEDTLS_PK_PARSE_C)
609 
618 int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
619  mbedtls_pk_context *pk );
620 #endif /* MBEDTLS_PK_PARSE_C */
621 
622 #if defined(MBEDTLS_PK_WRITE_C)
623 
633 int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
634  const mbedtls_pk_context *key );
635 #endif /* MBEDTLS_PK_WRITE_C */
636 
637 /*
638  * Internal module functions. You probably do not want to use these unless you
639  * know you do.
640  */
641 #if defined(MBEDTLS_FS_IO)
642 int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n );
643 #endif
644 
645 #ifdef __cplusplus
646 }
647 #endif
648 
649 #endif /* MBEDTLS_PK_H */
int mbedtls_pk_write_pubkey_pem(mbedtls_pk_context *ctx, unsigned char *buf, size_t size)
Write a public key to a PEM string.
Options for RSASSA-PSS signature verification. See mbedtls_rsa_rsassa_pss_verify_ext() ...
Definition: pk.h:117
Public key container.
Definition: pk.h:155
int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key, mbedtls_pk_rsa_alt_decrypt_func decrypt_func, mbedtls_pk_rsa_alt_sign_func sign_func, mbedtls_pk_rsa_alt_key_len_func key_len_func)
Initialize an RSA-alt context.
int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end, mbedtls_pk_context *pk)
Parse a SubjectPublicKeyInfo DER structure.
void mbedtls_pk_init(mbedtls_pk_context *ctx)
Initialize a mbedtls_pk_context (as NONE)
static mbedtls_rsa_context * mbedtls_pk_rsa(const mbedtls_pk_context pk)
Definition: pk.h:168
mbedtls_pk_debug_type
Types for interfacing with the debug module.
Definition: pk.h:127
int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
Tell if a context can do the operation given by type.
int mbedtls_pk_write_pubkey_der(mbedtls_pk_context *ctx, unsigned char *buf, size_t size)
Write a public key to a SubjectPublicKeyInfo DER structure Note: data is written at the end of the bu...
int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start, const mbedtls_pk_context *key)
Write a subjectPublicKey to ASN.1 data Note: function works backwards in data buffer.
Elliptic curves over GF(p)
int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options, mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hash_len, const unsigned char *sig, size_t sig_len)
Verify signature, with options. (Includes verification of the padding depending on type...
ECP key pair structure.
Definition: ecp.h:200
The Elliptic Curve Digital Signature Algorithm (ECDSA).
const mbedtls_pk_info_t * pk_info
Definition: pk.h:157
int mbedtls_pk_encrypt(mbedtls_pk_context *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, size_t osize, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Encrypt message (including padding if relevant).
size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
Get the size in bits of the underlying key.
mbedtls_pk_type_t
Public key types.
Definition: pk.h:103
Configuration options (set of defines)
void * value
Definition: pk.h:141
static size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx)
Get the length in bytes of the underlying key.
Definition: pk.h:272
int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hash_len, unsigned char *sig, size_t *sig_len, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Make signature, including padding if relevant.
const char * name
Definition: pk.h:140
int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
Export debug information.
void * pk_ctx
Definition: pk.h:158
int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx, const unsigned char *key, size_t keylen)
Parse a public key in PEM or DER format.
int mbedtls_pk_parse_key(mbedtls_pk_context *ctx, const unsigned char *key, size_t keylen, const unsigned char *pwd, size_t pwdlen)
Parse a private key in PEM or DER format.
void mbedtls_pk_free(mbedtls_pk_context *ctx)
Free a mbedtls_pk_context.
mbedtls_md_type_t mgf1_hash_id
Definition: pk.h:119
size_t(* mbedtls_pk_rsa_alt_key_len_func)(void *ctx)
Definition: pk.h:198
int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx, const char *path, const char *password)
Load and parse a private key.
struct mbedtls_pk_info_t mbedtls_pk_info_t
Public key information and operations.
Definition: pk.h:150
The generic message-digest wrapper.
The RSA public-key cryptosystem.
int(* mbedtls_pk_rsa_alt_decrypt_func)(void *ctx, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
Types for RSA-alt abstraction.
Definition: pk.h:191
int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hash_len, const unsigned char *sig, size_t sig_len)
Verify signature (including padding if relevant).
mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Get the key type.
int mbedtls_pk_write_key_pem(mbedtls_pk_context *ctx, unsigned char *buf, size_t size)
Write a private key to a PKCS#1 or SEC1 PEM string.
int(* mbedtls_pk_rsa_alt_sign_func)(void *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Definition: pk.h:194
int mbedtls_pk_write_key_der(mbedtls_pk_context *ctx, unsigned char *buf, size_t size)
Write a private key to a PKCS#1 or SEC1 DER structure Note: data is written at the end of the buffer!...
const char * mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Access the type name.
int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
static mbedtls_ecp_keypair * mbedtls_pk_ec(const mbedtls_pk_context pk)
Definition: pk.h:181
int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
Initialize a PK context with the information given and allocates the type-specific PK subcontext...
mbedtls_pk_debug_type type
Definition: pk.h:139
int mbedtls_pk_check_pair(const mbedtls_pk_context *pub, const mbedtls_pk_context *prv)
Check if a public-private pair of keys matches.
mbedtls_md_type_t
Enumeration of supported message digests.
Definition: md.h:83
The RSA context structure.
Definition: rsa.h:121
Item to send to the debug module.
Definition: pk.h:137
int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Load and parse a public key.
const mbedtls_pk_info_t * mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
Return information associated with the given PK type.
int mbedtls_pk_decrypt(mbedtls_pk_context *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, size_t osize, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Decrypt message (including padding if relevant).