pk_wrap.c 15 KB

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