pk_wrap.c 14 KB

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