pk.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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/bignum.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. /* Implementation that should never be optimized out by the compiler */
  41. static void mbedtls_zeroize( void *v, size_t n ) {
  42. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  43. }
  44. /*
  45. * Initialise a mbedtls_pk_context
  46. */
  47. void mbedtls_pk_init( mbedtls_pk_context *ctx )
  48. {
  49. if( ctx == NULL )
  50. return;
  51. ctx->pk_info = NULL;
  52. ctx->pk_ctx = NULL;
  53. }
  54. /*
  55. * Free (the components of) a mbedtls_pk_context
  56. */
  57. void mbedtls_pk_free( mbedtls_pk_context *ctx )
  58. {
  59. if( ctx == NULL || ctx->pk_info == NULL )
  60. return;
  61. ctx->pk_info->ctx_free_func( ctx->pk_ctx );
  62. mbedtls_zeroize( ctx, sizeof( mbedtls_pk_context ) );
  63. }
  64. /*
  65. * Get pk_info structure from type
  66. */
  67. const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type )
  68. {
  69. switch( pk_type ) {
  70. #if defined(MBEDTLS_RSA_C)
  71. case MBEDTLS_PK_RSA:
  72. return( &mbedtls_rsa_info );
  73. #endif
  74. #if defined(MBEDTLS_ECP_C)
  75. case MBEDTLS_PK_ECKEY:
  76. return( &mbedtls_eckey_info );
  77. case MBEDTLS_PK_ECKEY_DH:
  78. return( &mbedtls_eckeydh_info );
  79. #endif
  80. #if defined(MBEDTLS_ECDSA_C)
  81. case MBEDTLS_PK_ECDSA:
  82. return( &mbedtls_ecdsa_info );
  83. #endif
  84. /* MBEDTLS_PK_RSA_ALT omitted on purpose */
  85. default:
  86. return( NULL );
  87. }
  88. }
  89. /*
  90. * Initialise context
  91. */
  92. int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info )
  93. {
  94. if( ctx == NULL || info == NULL || ctx->pk_info != NULL )
  95. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  96. if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
  97. return( MBEDTLS_ERR_PK_ALLOC_FAILED );
  98. ctx->pk_info = info;
  99. return( 0 );
  100. }
  101. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  102. /*
  103. * Initialize an RSA-alt context
  104. */
  105. int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
  106. mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
  107. mbedtls_pk_rsa_alt_sign_func sign_func,
  108. mbedtls_pk_rsa_alt_key_len_func key_len_func )
  109. {
  110. mbedtls_rsa_alt_context *rsa_alt;
  111. const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
  112. if( ctx == NULL || ctx->pk_info != NULL )
  113. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  114. if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL )
  115. return( MBEDTLS_ERR_PK_ALLOC_FAILED );
  116. ctx->pk_info = info;
  117. rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
  118. rsa_alt->key = key;
  119. rsa_alt->decrypt_func = decrypt_func;
  120. rsa_alt->sign_func = sign_func;
  121. rsa_alt->key_len_func = key_len_func;
  122. return( 0 );
  123. }
  124. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  125. /*
  126. * Tell if a PK can do the operations of the given type
  127. */
  128. int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type )
  129. {
  130. /* null or NONE context can't do anything */
  131. if( ctx == NULL || ctx->pk_info == NULL )
  132. return( 0 );
  133. return( ctx->pk_info->can_do( type ) );
  134. }
  135. /*
  136. * Helper for mbedtls_pk_sign and mbedtls_pk_verify
  137. */
  138. static inline int pk_hashlen_helper( mbedtls_md_type_t md_alg, size_t *hash_len )
  139. {
  140. const mbedtls_md_info_t *md_info;
  141. if( *hash_len != 0 )
  142. return( 0 );
  143. if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
  144. return( -1 );
  145. *hash_len = mbedtls_md_get_size( md_info );
  146. return( 0 );
  147. }
  148. /*
  149. * Verify a signature
  150. */
  151. int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  152. const unsigned char *hash, size_t hash_len,
  153. const unsigned char *sig, size_t sig_len )
  154. {
  155. if( ctx == NULL || ctx->pk_info == NULL ||
  156. pk_hashlen_helper( md_alg, &hash_len ) != 0 )
  157. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  158. if( ctx->pk_info->verify_func == NULL )
  159. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  160. return( ctx->pk_info->verify_func( ctx->pk_ctx, md_alg, hash, hash_len,
  161. sig, sig_len ) );
  162. }
  163. /*
  164. * Verify a signature with options
  165. */
  166. int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
  167. mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  168. const unsigned char *hash, size_t hash_len,
  169. const unsigned char *sig, size_t sig_len )
  170. {
  171. if( ctx == NULL || ctx->pk_info == NULL )
  172. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  173. if( ! mbedtls_pk_can_do( ctx, type ) )
  174. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  175. if( type == MBEDTLS_PK_RSASSA_PSS )
  176. {
  177. #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
  178. int ret;
  179. const mbedtls_pk_rsassa_pss_options *pss_opts;
  180. #if defined(MBEDTLS_HAVE_INT64)
  181. if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
  182. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  183. #endif /* MBEDTLS_HAVE_INT64 */
  184. if( options == NULL )
  185. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  186. pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
  187. if( sig_len < mbedtls_pk_get_len( ctx ) )
  188. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  189. ret = mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_pk_rsa( *ctx ),
  190. NULL, NULL, MBEDTLS_RSA_PUBLIC,
  191. md_alg, (unsigned int) hash_len, hash,
  192. pss_opts->mgf1_hash_id,
  193. pss_opts->expected_salt_len,
  194. sig );
  195. if( ret != 0 )
  196. return( ret );
  197. if( sig_len > mbedtls_pk_get_len( ctx ) )
  198. return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
  199. return( 0 );
  200. #else
  201. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  202. #endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
  203. }
  204. /* General case: no options */
  205. if( options != NULL )
  206. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  207. return( mbedtls_pk_verify( ctx, md_alg, hash, hash_len, sig, sig_len ) );
  208. }
  209. /*
  210. * Make a signature
  211. */
  212. int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  213. const unsigned char *hash, size_t hash_len,
  214. unsigned char *sig, size_t *sig_len,
  215. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  216. {
  217. if( ctx == NULL || ctx->pk_info == NULL ||
  218. pk_hashlen_helper( md_alg, &hash_len ) != 0 )
  219. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  220. if( ctx->pk_info->sign_func == NULL )
  221. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  222. return( ctx->pk_info->sign_func( ctx->pk_ctx, md_alg, hash, hash_len,
  223. sig, sig_len, f_rng, p_rng ) );
  224. }
  225. /*
  226. * Decrypt message
  227. */
  228. int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
  229. const unsigned char *input, size_t ilen,
  230. unsigned char *output, size_t *olen, size_t osize,
  231. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  232. {
  233. if( ctx == NULL || ctx->pk_info == NULL )
  234. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  235. if( ctx->pk_info->decrypt_func == NULL )
  236. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  237. return( ctx->pk_info->decrypt_func( ctx->pk_ctx, input, ilen,
  238. output, olen, osize, f_rng, p_rng ) );
  239. }
  240. /*
  241. * Encrypt message
  242. */
  243. int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
  244. const unsigned char *input, size_t ilen,
  245. unsigned char *output, size_t *olen, size_t osize,
  246. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  247. {
  248. if( ctx == NULL || ctx->pk_info == NULL )
  249. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  250. if( ctx->pk_info->encrypt_func == NULL )
  251. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  252. return( ctx->pk_info->encrypt_func( ctx->pk_ctx, input, ilen,
  253. output, olen, osize, f_rng, p_rng ) );
  254. }
  255. /*
  256. * Check public-private key pair
  257. */
  258. int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv )
  259. {
  260. if( pub == NULL || pub->pk_info == NULL ||
  261. prv == NULL || prv->pk_info == NULL ||
  262. prv->pk_info->check_pair_func == NULL )
  263. {
  264. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  265. }
  266. if( prv->pk_info->type == MBEDTLS_PK_RSA_ALT )
  267. {
  268. if( pub->pk_info->type != MBEDTLS_PK_RSA )
  269. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  270. }
  271. else
  272. {
  273. if( pub->pk_info != prv->pk_info )
  274. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  275. }
  276. return( prv->pk_info->check_pair_func( pub->pk_ctx, prv->pk_ctx ) );
  277. }
  278. /*
  279. * Get key size in bits
  280. */
  281. size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx )
  282. {
  283. if( ctx == NULL || ctx->pk_info == NULL )
  284. return( 0 );
  285. return( ctx->pk_info->get_bitlen( ctx->pk_ctx ) );
  286. }
  287. /*
  288. * Export debug information
  289. */
  290. int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items )
  291. {
  292. if( ctx == NULL || ctx->pk_info == NULL )
  293. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  294. if( ctx->pk_info->debug_func == NULL )
  295. return( MBEDTLS_ERR_PK_TYPE_MISMATCH );
  296. ctx->pk_info->debug_func( ctx->pk_ctx, items );
  297. return( 0 );
  298. }
  299. /*
  300. * Access the PK type name
  301. */
  302. const char *mbedtls_pk_get_name( const mbedtls_pk_context *ctx )
  303. {
  304. if( ctx == NULL || ctx->pk_info == NULL )
  305. return( "invalid PK" );
  306. return( ctx->pk_info->name );
  307. }
  308. /*
  309. * Access the PK type
  310. */
  311. mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx )
  312. {
  313. if( ctx == NULL || ctx->pk_info == NULL )
  314. return( MBEDTLS_PK_NONE );
  315. return( ctx->pk_info->type );
  316. }
  317. #endif /* MBEDTLS_PK_C */