pk.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Public Key abstraction layer
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_PK_C)
  27. #include "mbedtls/pk.h"
  28. #include "mbedtls/pk_internal.h"
  29. #include "mbedtls/platform_util.h"
  30. #if defined(MBEDTLS_RSA_C)
  31. #include "mbedtls/rsa.h"
  32. #endif
  33. #if defined(MBEDTLS_ECP_C)
  34. #include "mbedtls/ecp.h"
  35. #endif
  36. #if defined(MBEDTLS_ECDSA_C)
  37. #include "mbedtls/ecdsa.h"
  38. #endif
  39. #include <limits.h>
  40. #include <stdint.h>
  41. /*
  42. * Initialise a mbedtls_pk_context
  43. */
  44. void mbedtls_pk_init( mbedtls_pk_context *ctx )
  45. {
  46. if( ctx == NULL )
  47. return;
  48. ctx->pk_info = NULL;
  49. ctx->pk_ctx = NULL;
  50. }
  51. /*
  52. * Free (the components of) a mbedtls_pk_context
  53. */
  54. void mbedtls_pk_free( mbedtls_pk_context *ctx )
  55. {
  56. if( ctx == NULL || ctx->pk_info == NULL )
  57. return;
  58. ctx->pk_info->ctx_free_func( ctx->pk_ctx );
  59. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) );
  60. }
  61. /*
  62. * Get pk_info structure from type
  63. */
  64. const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
  65. {
  66. switch( pk_type ) {
  67. #if defined(MBEDTLS_RSA_C)
  68. case MBEDTLS_PK_RSA:
  69. return( &mbedtls_rsa_info );
  70. #endif
  71. #if defined(MBEDTLS_ECP_C)
  72. case MBEDTLS_PK_ECKEY:
  73. return( &mbedtls_eckey_info );
  74. case MBEDTLS_PK_ECKEY_DH:
  75. return( &mbedtls_eckeydh_info );
  76. #endif
  77. #if defined(MBEDTLS_ECDSA_C)
  78. case MBEDTLS_PK_ECDSA:
  79. return( &mbedtls_ecdsa_info );
  80. #endif
  81. /* MBEDTLS_PK_RSA_ALT omitted on purpose */
  82. default:
  83. return( NULL );
  84. }
  85. }
  86. /*
  87. * Initialise context
  88. */
  89. int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
  90. {
  91. if( ctx == NULL || info == NULL || ctx->pk_info != NULL )
  92. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  93. if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
  94. return( MBEDTLS_ERR_PK_ALLOC_FAILED );
  95. ctx->pk_info = info;
  96. return( 0 );
  97. }
  98. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  99. /*
  100. * Initialize an RSA-alt context
  101. */
  102. int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
  103. mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
  104. mbedtls_pk_rsa_alt_sign_func sign_func,
  105. mbedtls_pk_rsa_alt_key_len_func key_len_func )
  106. {
  107. mbedtls_rsa_alt_context *rsa_alt;
  108. const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
  109. if( ctx == NULL || ctx->pk_info != NULL )
  110. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  111. if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
  112. return( MBEDTLS_ERR_PK_ALLOC_FAILED );
  113. ctx->pk_info = info;
  114. rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
  115. rsa_alt->key = key;
  116. rsa_alt->decrypt_func = decrypt_func;
  117. rsa_alt->sign_func = sign_func;
  118. rsa_alt->key_len_func = key_len_func;
  119. return( 0 );
  120. }
  121. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  122. /*
  123. * Tell if a PK can do the operations of the given type
  124. */
  125. int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
  126. {
  127. /* null or NONE context can't do anything */
  128. if( ctx == NULL || ctx->pk_info == NULL )
  129. return( 0 );
  130. return( ctx->pk_info->can_do( type ) );
  131. }
  132. /*
  133. * Helper for mbedtls_pk_sign and mbedtls_pk_verify
  134. */
  135. static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
  136. {
  137. const mbedtls_md_info_t *md_info;
  138. if( *hash_len != 0 )
  139. return( 0 );
  140. if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
  141. return( -1 );
  142. *hash_len = mbedtls_md_get_size( md_info );
  143. return( 0 );
  144. }
  145. /*
  146. * Verify a signature
  147. */
  148. int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  149. const unsigned char *hash, size_t hash_len,
  150. const unsigned char *sig, size_t sig_len )
  151. {
  152. if( ctx == NULL || ctx->pk_info == NULL ||
  153. pk_hashlen_helper( md_alg, &hash_len ) != 0 )
  154. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  155. if( ctx->pk_info->verify_func == NULL )
  156. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  157. return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
  158. sig, sig_len ) );
  159. }
  160. /*
  161. * Verify a signature with options
  162. */
  163. int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
  164. mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  165. const unsigned char *hash, size_t hash_len,
  166. const unsigned char *sig, size_t sig_len )
  167. {
  168. if( ctx == NULL || ctx->pk_info == NULL )
  169. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  170. if( ! mbedtls_pk_can_do( ctx, type ) )
  171. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  172. if( type == MBEDTLS_PK_RSASSA_PSS )
  173. {
  174. #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
  175. int ret;
  176. const mbedtls_pk_rsassa_pss_options *pss_opts;
  177. #if SIZE_MAX > UINT_MAX
  178. if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
  179. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  180. #endif /* SIZE_MAX > UINT_MAX */
  181. if( options == NULL )
  182. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  183. pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
  184. if( sig_len < mbedtls_pk_get_len( ctx ) )
  185. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  186. ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
  187. NULL, NULL, MBEDTLS_RSA_PUBLIC,
  188. md_alg, (unsigned int) hash_len, hash,
  189. pss_opts->mgf1_hash_id,
  190. pss_opts->expected_salt_len,
  191. sig );
  192. if( ret != 0 )
  193. return( ret );
  194. if( sig_len > mbedtls_pk_get_len( ctx ) )
  195. return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
  196. return( 0 );
  197. #else
  198. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  199. #endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
  200. }
  201. /* General case: no options */
  202. if( options != NULL )
  203. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  204. return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
  205. }
  206. /*
  207. * Make a signature
  208. */
  209. int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  210. const unsigned char *hash, size_t hash_len,
  211. unsigned char *sig, size_t *sig_len,
  212. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  213. {
  214. if( ctx == NULL || ctx->pk_info == NULL ||
  215. pk_hashlen_helper( md_alg, &hash_len ) != 0 )
  216. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  217. if( ctx->pk_info->sign_func == NULL )
  218. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  219. return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
  220. sig, sig_len, f_rng, p_rng ) );
  221. }
  222. /*
  223. * Decrypt message
  224. */
  225. int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
  226. const unsigned char *input, size_t ilen,
  227. unsigned char *output, size_t *olen, size_t osize,
  228. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  229. {
  230. if( ctx == NULL || ctx->pk_info == NULL )
  231. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  232. if( ctx->pk_info->decrypt_func == NULL )
  233. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  234. return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
  235. output, olen, osize, f_rng, p_rng ) );
  236. }
  237. /*
  238. * Encrypt message
  239. */
  240. int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
  241. const unsigned char *input, size_t ilen,
  242. unsigned char *output, size_t *olen, size_t osize,
  243. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  244. {
  245. if( ctx == NULL || ctx->pk_info == NULL )
  246. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  247. if( ctx->pk_info->encrypt_func == NULL )
  248. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  249. return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
  250. output, olen, osize, f_rng, p_rng ) );
  251. }
  252. /*
  253. * Check public-private key pair
  254. */
  255. int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
  256. {
  257. if( pub == NULL || pub->pk_info == NULL ||
  258. prv == NULL || prv->pk_info == NULL ||
  259. prv->pk_info->check_pair_func == NULL )
  260. {
  261. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  262. }
  263. if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
  264. {
  265. if( pub->pk_info->type != MBEDTLS_PK_RSA )
  266. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  267. }
  268. else
  269. {
  270. if( pub->pk_info != prv->pk_info )
  271. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  272. }
  273. return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
  274. }
  275. /*
  276. * Get key size in bits
  277. */
  278. size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
  279. {
  280. if( ctx == NULL || ctx->pk_info == NULL )
  281. return( 0 );
  282. return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
  283. }
  284. /*
  285. * Export debug information
  286. */
  287. int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
  288. {
  289. if( ctx == NULL || ctx->pk_info == NULL )
  290. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  291. if( ctx->pk_info->debug_func == NULL )
  292. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  293. ctx->pk_info->debug_func( ctx->pk_ctx, items );
  294. return( 0 );
  295. }
  296. /*
  297. * Access the PK type name
  298. */
  299. const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
  300. {
  301. if( ctx == NULL || ctx->pk_info == NULL )
  302. return( "invalid PK" );
  303. return( ctx->pk_info->name );
  304. }
  305. /*
  306. * Access the PK type
  307. */
  308. mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
  309. {
  310. if( ctx == NULL || ctx->pk_info == NULL )
  311. return( MBEDTLS_PK_NONE );
  312. return( ctx->pk_info->type );
  313. }
  314. #endif /* MBEDTLS_PK_C */