pk.c 11 KB

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