pk_wrap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * Public Key abstraction layer: wrapper functions
  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_internal.h"
  28. /* Even if RSA not activated, for the sake of RSA-alt */
  29. #include "mbedtls/rsa.h"
  30. #include <string.h>
  31. #if defined(MBEDTLS_ECP_C)
  32. #include "mbedtls/ecp.h"
  33. #endif
  34. #if defined(MBEDTLS_ECDSA_C)
  35. #include "mbedtls/ecdsa.h"
  36. #endif
  37. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  38. #include "mbedtls/platform_util.h"
  39. #endif
  40. #if defined(MBEDTLS_PLATFORM_C)
  41. #include "mbedtls/platform.h"
  42. #else
  43. #include <stdlib.h>
  44. #define mbedtls_calloc calloc
  45. #define mbedtls_free free
  46. #endif
  47. #include <limits.h>
  48. #include <stdint.h>
  49. #if defined(MBEDTLS_RSA_C)
  50. static int rsa_can_do( mbedtls_pk_type_t type )
  51. {
  52. return( type == MBEDTLS_PK_RSA ||
  53. type == MBEDTLS_PK_RSASSA_PSS );
  54. }
  55. static size_t rsa_get_bitlen( const void *ctx )
  56. {
  57. const mbedtls_rsa_context * rsa = (const mbedtls_rsa_context *) ctx;
  58. return( 8 * mbedtls_rsa_get_len( rsa ) );
  59. }
  60. static int rsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
  61. const unsigned char *hash, size_t hash_len,
  62. const unsigned char *sig, size_t sig_len )
  63. {
  64. int ret;
  65. mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
  66. size_t rsa_len = mbedtls_rsa_get_len( rsa );
  67. #if SIZE_MAX > UINT_MAX
  68. if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
  69. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  70. #endif /* SIZE_MAX > UINT_MAX */
  71. if( sig_len < rsa_len )
  72. return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
  73. if( ( ret = mbedtls_rsa_pkcs1_verify( rsa, NULL, NULL,
  74. MBEDTLS_RSA_PUBLIC, md_alg,
  75. (unsigned int) hash_len, hash, sig ) ) != 0 )
  76. return( ret );
  77. /* The buffer contains a valid signature followed by extra data.
  78. * We have a special error code for that so that so that callers can
  79. * use mbedtls_pk_verify() to check "Does the buffer start with a
  80. * valid signature?" and not just "Does the buffer contain a valid
  81. * signature?". */
  82. if( sig_len > rsa_len )
  83. return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
  84. return( 0 );
  85. }
  86. static int rsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  87. const unsigned char *hash, size_t hash_len,
  88. unsigned char *sig, size_t *sig_len,
  89. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  90. {
  91. mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
  92. #if SIZE_MAX > UINT_MAX
  93. if( md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len )
  94. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  95. #endif /* SIZE_MAX > UINT_MAX */
  96. *sig_len = mbedtls_rsa_get_len( rsa );
  97. return( mbedtls_rsa_pkcs1_sign( rsa, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
  98. md_alg, (unsigned int) hash_len, hash, sig ) );
  99. }
  100. static int rsa_decrypt_wrap( void *ctx,
  101. const unsigned char *input, size_t ilen,
  102. unsigned char *output, size_t *olen, size_t osize,
  103. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  104. {
  105. mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
  106. if( ilen != mbedtls_rsa_get_len( rsa ) )
  107. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  108. return( mbedtls_rsa_pkcs1_decrypt( rsa, f_rng, p_rng,
  109. MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
  110. }
  111. static int rsa_encrypt_wrap( void *ctx,
  112. const unsigned char *input, size_t ilen,
  113. unsigned char *output, size_t *olen, size_t osize,
  114. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  115. {
  116. mbedtls_rsa_context * rsa = (mbedtls_rsa_context *) ctx;
  117. *olen = mbedtls_rsa_get_len( rsa );
  118. if( *olen > osize )
  119. return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
  120. return( mbedtls_rsa_pkcs1_encrypt( rsa, f_rng, p_rng, MBEDTLS_RSA_PUBLIC,
  121. ilen, input, output ) );
  122. }
  123. static int rsa_check_pair_wrap( const void *pub, const void *prv )
  124. {
  125. return( mbedtls_rsa_check_pub_priv( (const mbedtls_rsa_context *) pub,
  126. (const mbedtls_rsa_context *) prv ) );
  127. }
  128. static void *rsa_alloc_wrap( void )
  129. {
  130. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
  131. if( ctx != NULL )
  132. mbedtls_rsa_init( (mbedtls_rsa_context *) ctx, 0, 0 );
  133. return( ctx );
  134. }
  135. static void rsa_free_wrap( void *ctx )
  136. {
  137. mbedtls_rsa_free( (mbedtls_rsa_context *) ctx );
  138. mbedtls_free( ctx );
  139. }
  140. static void rsa_debug( const void *ctx, mbedtls_pk_debug_item *items )
  141. {
  142. items->type = MBEDTLS_PK_DEBUG_MPI;
  143. items->name = "rsa.N";
  144. items->value = &( ((mbedtls_rsa_context *) ctx)->N );
  145. items++;
  146. items->type = MBEDTLS_PK_DEBUG_MPI;
  147. items->name = "rsa.E";
  148. items->value = &( ((mbedtls_rsa_context *) ctx)->E );
  149. }
  150. const mbedtls_pk_info_t mbedtls_rsa_info = {
  151. MBEDTLS_PK_RSA,
  152. "RSA",
  153. rsa_get_bitlen,
  154. rsa_can_do,
  155. rsa_verify_wrap,
  156. rsa_sign_wrap,
  157. rsa_decrypt_wrap,
  158. rsa_encrypt_wrap,
  159. rsa_check_pair_wrap,
  160. rsa_alloc_wrap,
  161. rsa_free_wrap,
  162. rsa_debug,
  163. };
  164. #endif /* MBEDTLS_RSA_C */
  165. #if defined(MBEDTLS_ECP_C)
  166. /*
  167. * Generic EC key
  168. */
  169. static int eckey_can_do( mbedtls_pk_type_t type )
  170. {
  171. return( type == MBEDTLS_PK_ECKEY ||
  172. type == MBEDTLS_PK_ECKEY_DH ||
  173. type == MBEDTLS_PK_ECDSA );
  174. }
  175. static size_t eckey_get_bitlen( const void *ctx )
  176. {
  177. return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
  178. }
  179. #if defined(MBEDTLS_ECDSA_C)
  180. /* Forward declarations */
  181. static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
  182. const unsigned char *hash, size_t hash_len,
  183. const unsigned char *sig, size_t sig_len );
  184. static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  185. const unsigned char *hash, size_t hash_len,
  186. unsigned char *sig, size_t *sig_len,
  187. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  188. static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
  189. const unsigned char *hash, size_t hash_len,
  190. const unsigned char *sig, size_t sig_len )
  191. {
  192. int ret;
  193. mbedtls_ecdsa_context ecdsa;
  194. mbedtls_ecdsa_init( &ecdsa );
  195. if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
  196. ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
  197. mbedtls_ecdsa_free( &ecdsa );
  198. return( ret );
  199. }
  200. static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  201. const unsigned char *hash, size_t hash_len,
  202. unsigned char *sig, size_t *sig_len,
  203. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  204. {
  205. int ret;
  206. mbedtls_ecdsa_context ecdsa;
  207. mbedtls_ecdsa_init( &ecdsa );
  208. if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
  209. ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
  210. f_rng, p_rng );
  211. mbedtls_ecdsa_free( &ecdsa );
  212. return( ret );
  213. }
  214. #endif /* MBEDTLS_ECDSA_C */
  215. static int eckey_check_pair( const void *pub, const void *prv )
  216. {
  217. return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
  218. (const mbedtls_ecp_keypair *) prv ) );
  219. }
  220. static void *eckey_alloc_wrap( void )
  221. {
  222. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
  223. if( ctx != NULL )
  224. mbedtls_ecp_keypair_init( ctx );
  225. return( ctx );
  226. }
  227. static void eckey_free_wrap( void *ctx )
  228. {
  229. mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
  230. mbedtls_free( ctx );
  231. }
  232. static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
  233. {
  234. items->type = MBEDTLS_PK_DEBUG_ECP;
  235. items->name = "eckey.Q";
  236. items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
  237. }
  238. const mbedtls_pk_info_t mbedtls_eckey_info = {
  239. MBEDTLS_PK_ECKEY,
  240. "EC",
  241. eckey_get_bitlen,
  242. eckey_can_do,
  243. #if defined(MBEDTLS_ECDSA_C)
  244. eckey_verify_wrap,
  245. eckey_sign_wrap,
  246. #else
  247. NULL,
  248. NULL,
  249. #endif
  250. NULL,
  251. NULL,
  252. eckey_check_pair,
  253. eckey_alloc_wrap,
  254. eckey_free_wrap,
  255. eckey_debug,
  256. };
  257. /*
  258. * EC key restricted to ECDH
  259. */
  260. static int eckeydh_can_do( mbedtls_pk_type_t type )
  261. {
  262. return( type == MBEDTLS_PK_ECKEY ||
  263. type == MBEDTLS_PK_ECKEY_DH );
  264. }
  265. const mbedtls_pk_info_t mbedtls_eckeydh_info = {
  266. MBEDTLS_PK_ECKEY_DH,
  267. "EC_DH",
  268. eckey_get_bitlen, /* Same underlying key structure */
  269. eckeydh_can_do,
  270. NULL,
  271. NULL,
  272. NULL,
  273. NULL,
  274. eckey_check_pair,
  275. eckey_alloc_wrap, /* Same underlying key structure */
  276. eckey_free_wrap, /* Same underlying key structure */
  277. eckey_debug, /* Same underlying key structure */
  278. };
  279. #endif /* MBEDTLS_ECP_C */
  280. #if defined(MBEDTLS_ECDSA_C)
  281. static int ecdsa_can_do( mbedtls_pk_type_t type )
  282. {
  283. return( type == MBEDTLS_PK_ECDSA );
  284. }
  285. static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
  286. const unsigned char *hash, size_t hash_len,
  287. const unsigned char *sig, size_t sig_len )
  288. {
  289. int ret;
  290. ((void) md_alg);
  291. ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
  292. hash, hash_len, sig, sig_len );
  293. if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
  294. return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
  295. return( ret );
  296. }
  297. static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  298. const unsigned char *hash, size_t hash_len,
  299. unsigned char *sig, size_t *sig_len,
  300. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  301. {
  302. return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
  303. md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
  304. }
  305. static void *ecdsa_alloc_wrap( void )
  306. {
  307. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
  308. if( ctx != NULL )
  309. mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
  310. return( ctx );
  311. }
  312. static void ecdsa_free_wrap( void *ctx )
  313. {
  314. mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
  315. mbedtls_free( ctx );
  316. }
  317. const mbedtls_pk_info_t mbedtls_ecdsa_info = {
  318. MBEDTLS_PK_ECDSA,
  319. "ECDSA",
  320. eckey_get_bitlen, /* Compatible key structures */
  321. ecdsa_can_do,
  322. ecdsa_verify_wrap,
  323. ecdsa_sign_wrap,
  324. NULL,
  325. NULL,
  326. eckey_check_pair, /* Compatible key structures */
  327. ecdsa_alloc_wrap,
  328. ecdsa_free_wrap,
  329. eckey_debug, /* Compatible key structures */
  330. };
  331. #endif /* MBEDTLS_ECDSA_C */
  332. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  333. /*
  334. * Support for alternative RSA-private implementations
  335. */
  336. static int rsa_alt_can_do( mbedtls_pk_type_t type )
  337. {
  338. return( type == MBEDTLS_PK_RSA );
  339. }
  340. static size_t rsa_alt_get_bitlen( const void *ctx )
  341. {
  342. const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
  343. return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
  344. }
  345. static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  346. const unsigned char *hash, size_t hash_len,
  347. unsigned char *sig, size_t *sig_len,
  348. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  349. {
  350. mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
  351. #if SIZE_MAX > UINT_MAX
  352. if( UINT_MAX < hash_len )
  353. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  354. #endif /* SIZE_MAX > UINT_MAX */
  355. *sig_len = rsa_alt->key_len_func( rsa_alt->key );
  356. return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
  357. md_alg, (unsigned int) hash_len, hash, sig ) );
  358. }
  359. static int rsa_alt_decrypt_wrap( void *ctx,
  360. const unsigned char *input, size_t ilen,
  361. unsigned char *output, size_t *olen, size_t osize,
  362. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  363. {
  364. mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
  365. ((void) f_rng);
  366. ((void) p_rng);
  367. if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
  368. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  369. return( rsa_alt->decrypt_func( rsa_alt->key,
  370. MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
  371. }
  372. #if defined(MBEDTLS_RSA_C)
  373. static int rsa_alt_check_pair( const void *pub, const void *prv )
  374. {
  375. unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
  376. unsigned char hash[32];
  377. size_t sig_len = 0;
  378. int ret;
  379. if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
  380. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  381. memset( hash, 0x2a, sizeof( hash ) );
  382. if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
  383. hash, sizeof( hash ),
  384. sig, &sig_len, NULL, NULL ) ) != 0 )
  385. {
  386. return( ret );
  387. }
  388. if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
  389. hash, sizeof( hash ), sig, sig_len ) != 0 )
  390. {
  391. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  392. }
  393. return( 0 );
  394. }
  395. #endif /* MBEDTLS_RSA_C */
  396. static void *rsa_alt_alloc_wrap( void )
  397. {
  398. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
  399. if( ctx != NULL )
  400. memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
  401. return( ctx );
  402. }
  403. static void rsa_alt_free_wrap( void *ctx )
  404. {
  405. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
  406. mbedtls_free( ctx );
  407. }
  408. const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
  409. MBEDTLS_PK_RSA_ALT,
  410. "RSA-alt",
  411. rsa_alt_get_bitlen,
  412. rsa_alt_can_do,
  413. NULL,
  414. rsa_alt_sign_wrap,
  415. rsa_alt_decrypt_wrap,
  416. NULL,
  417. #if defined(MBEDTLS_RSA_C)
  418. rsa_alt_check_pair,
  419. #else
  420. NULL,
  421. #endif
  422. rsa_alt_alloc_wrap,
  423. rsa_alt_free_wrap,
  424. NULL,
  425. };
  426. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  427. #endif /* MBEDTLS_PK_C */