pk_wrap.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  158. NULL,
  159. NULL,
  160. #endif
  161. rsa_decrypt_wrap,
  162. rsa_encrypt_wrap,
  163. rsa_check_pair_wrap,
  164. rsa_alloc_wrap,
  165. rsa_free_wrap,
  166. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  167. NULL,
  168. NULL,
  169. #endif
  170. rsa_debug,
  171. };
  172. #endif /* MBEDTLS_RSA_C */
  173. #if defined(MBEDTLS_ECP_C)
  174. /*
  175. * Generic EC key
  176. */
  177. static int eckey_can_do( mbedtls_pk_type_t type )
  178. {
  179. return( type == MBEDTLS_PK_ECKEY ||
  180. type == MBEDTLS_PK_ECKEY_DH ||
  181. type == MBEDTLS_PK_ECDSA );
  182. }
  183. static size_t eckey_get_bitlen( const void *ctx )
  184. {
  185. return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
  186. }
  187. #if defined(MBEDTLS_ECDSA_C)
  188. /* Forward declarations */
  189. static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
  190. const unsigned char *hash, size_t hash_len,
  191. const unsigned char *sig, size_t sig_len );
  192. static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  193. const unsigned char *hash, size_t hash_len,
  194. unsigned char *sig, size_t *sig_len,
  195. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  196. static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
  197. const unsigned char *hash, size_t hash_len,
  198. const unsigned char *sig, size_t sig_len )
  199. {
  200. int ret;
  201. mbedtls_ecdsa_context ecdsa;
  202. mbedtls_ecdsa_init( &ecdsa );
  203. if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
  204. ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
  205. mbedtls_ecdsa_free( &ecdsa );
  206. return( ret );
  207. }
  208. static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  209. const unsigned char *hash, size_t hash_len,
  210. unsigned char *sig, size_t *sig_len,
  211. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  212. {
  213. int ret;
  214. mbedtls_ecdsa_context ecdsa;
  215. mbedtls_ecdsa_init( &ecdsa );
  216. if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
  217. ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
  218. f_rng, p_rng );
  219. mbedtls_ecdsa_free( &ecdsa );
  220. return( ret );
  221. }
  222. #if defined(MBEDTLS_ECP_RESTARTABLE)
  223. /* Forward declarations */
  224. static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
  225. const unsigned char *hash, size_t hash_len,
  226. const unsigned char *sig, size_t sig_len,
  227. void *rs_ctx );
  228. static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
  229. const unsigned char *hash, size_t hash_len,
  230. unsigned char *sig, size_t *sig_len,
  231. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  232. void *rs_ctx );
  233. /*
  234. * Restart context for ECDSA operations with ECKEY context
  235. *
  236. * We need to store an actual ECDSA context, as we need to pass the same to
  237. * the underlying ecdsa function, so we can't create it on the fly every time.
  238. */
  239. typedef struct
  240. {
  241. mbedtls_ecdsa_restart_ctx ecdsa_rs;
  242. mbedtls_ecdsa_context ecdsa_ctx;
  243. } eckey_restart_ctx;
  244. static void *eckey_rs_alloc( void )
  245. {
  246. eckey_restart_ctx *rs_ctx;
  247. void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
  248. if( ctx != NULL )
  249. {
  250. rs_ctx = ctx;
  251. mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
  252. mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
  253. }
  254. return( ctx );
  255. }
  256. static void eckey_rs_free( void *ctx )
  257. {
  258. eckey_restart_ctx *rs_ctx;
  259. if( ctx == NULL)
  260. return;
  261. rs_ctx = ctx;
  262. mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
  263. mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
  264. mbedtls_free( ctx );
  265. }
  266. static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
  267. const unsigned char *hash, size_t hash_len,
  268. const unsigned char *sig, size_t sig_len,
  269. void *rs_ctx )
  270. {
  271. int ret;
  272. eckey_restart_ctx *rs = rs_ctx;
  273. /* Should never happen */
  274. if( rs == NULL )
  275. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  276. /* set up our own sub-context if needed (that is, on first run) */
  277. if( rs->ecdsa_ctx.grp.pbits == 0 )
  278. MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
  279. MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
  280. md_alg, hash, hash_len,
  281. sig, sig_len, &rs->ecdsa_rs ) );
  282. cleanup:
  283. return( ret );
  284. }
  285. static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
  286. const unsigned char *hash, size_t hash_len,
  287. unsigned char *sig, size_t *sig_len,
  288. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  289. void *rs_ctx )
  290. {
  291. int ret;
  292. eckey_restart_ctx *rs = rs_ctx;
  293. /* Should never happen */
  294. if( rs == NULL )
  295. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  296. /* set up our own sub-context if needed (that is, on first run) */
  297. if( rs->ecdsa_ctx.grp.pbits == 0 )
  298. MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
  299. MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
  300. hash, hash_len, sig, sig_len,
  301. f_rng, p_rng, &rs->ecdsa_rs ) );
  302. cleanup:
  303. return( ret );
  304. }
  305. #endif /* MBEDTLS_ECP_RESTARTABLE */
  306. #endif /* MBEDTLS_ECDSA_C */
  307. static int eckey_check_pair( const void *pub, const void *prv )
  308. {
  309. return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
  310. (const mbedtls_ecp_keypair *) prv ) );
  311. }
  312. static void *eckey_alloc_wrap( void )
  313. {
  314. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
  315. if( ctx != NULL )
  316. mbedtls_ecp_keypair_init( ctx );
  317. return( ctx );
  318. }
  319. static void eckey_free_wrap( void *ctx )
  320. {
  321. mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
  322. mbedtls_free( ctx );
  323. }
  324. static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
  325. {
  326. items->type = MBEDTLS_PK_DEBUG_ECP;
  327. items->name = "eckey.Q";
  328. items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
  329. }
  330. const mbedtls_pk_info_t mbedtls_eckey_info = {
  331. MBEDTLS_PK_ECKEY,
  332. "EC",
  333. eckey_get_bitlen,
  334. eckey_can_do,
  335. #if defined(MBEDTLS_ECDSA_C)
  336. eckey_verify_wrap,
  337. eckey_sign_wrap,
  338. #if defined(MBEDTLS_ECP_RESTARTABLE)
  339. eckey_verify_rs_wrap,
  340. eckey_sign_rs_wrap,
  341. #endif
  342. #else /* MBEDTLS_ECDSA_C */
  343. NULL,
  344. NULL,
  345. #endif /* MBEDTLS_ECDSA_C */
  346. NULL,
  347. NULL,
  348. eckey_check_pair,
  349. eckey_alloc_wrap,
  350. eckey_free_wrap,
  351. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  352. eckey_rs_alloc,
  353. eckey_rs_free,
  354. #endif
  355. eckey_debug,
  356. };
  357. /*
  358. * EC key restricted to ECDH
  359. */
  360. static int eckeydh_can_do( mbedtls_pk_type_t type )
  361. {
  362. return( type == MBEDTLS_PK_ECKEY ||
  363. type == MBEDTLS_PK_ECKEY_DH );
  364. }
  365. const mbedtls_pk_info_t mbedtls_eckeydh_info = {
  366. MBEDTLS_PK_ECKEY_DH,
  367. "EC_DH",
  368. eckey_get_bitlen, /* Same underlying key structure */
  369. eckeydh_can_do,
  370. NULL,
  371. NULL,
  372. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  373. NULL,
  374. NULL,
  375. #endif
  376. NULL,
  377. NULL,
  378. eckey_check_pair,
  379. eckey_alloc_wrap, /* Same underlying key structure */
  380. eckey_free_wrap, /* Same underlying key structure */
  381. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  382. NULL,
  383. NULL,
  384. #endif
  385. eckey_debug, /* Same underlying key structure */
  386. };
  387. #endif /* MBEDTLS_ECP_C */
  388. #if defined(MBEDTLS_ECDSA_C)
  389. static int ecdsa_can_do( mbedtls_pk_type_t type )
  390. {
  391. return( type == MBEDTLS_PK_ECDSA );
  392. }
  393. static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
  394. const unsigned char *hash, size_t hash_len,
  395. const unsigned char *sig, size_t sig_len )
  396. {
  397. int ret;
  398. ((void) md_alg);
  399. ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
  400. hash, hash_len, sig, sig_len );
  401. if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
  402. return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
  403. return( ret );
  404. }
  405. static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  406. const unsigned char *hash, size_t hash_len,
  407. unsigned char *sig, size_t *sig_len,
  408. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  409. {
  410. return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
  411. md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
  412. }
  413. #if defined(MBEDTLS_ECP_RESTARTABLE)
  414. static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
  415. const unsigned char *hash, size_t hash_len,
  416. const unsigned char *sig, size_t sig_len,
  417. void *rs_ctx )
  418. {
  419. int ret;
  420. ((void) md_alg);
  421. ret = mbedtls_ecdsa_read_signature_restartable(
  422. (mbedtls_ecdsa_context *) ctx,
  423. hash, hash_len, sig, sig_len,
  424. (mbedtls_ecdsa_restart_ctx *) rs_ctx );
  425. if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
  426. return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
  427. return( ret );
  428. }
  429. static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
  430. const unsigned char *hash, size_t hash_len,
  431. unsigned char *sig, size_t *sig_len,
  432. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  433. void *rs_ctx )
  434. {
  435. return( mbedtls_ecdsa_write_signature_restartable(
  436. (mbedtls_ecdsa_context *) ctx,
  437. md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng,
  438. (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
  439. }
  440. #endif /* MBEDTLS_ECP_RESTARTABLE */
  441. static void *ecdsa_alloc_wrap( void )
  442. {
  443. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
  444. if( ctx != NULL )
  445. mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
  446. return( ctx );
  447. }
  448. static void ecdsa_free_wrap( void *ctx )
  449. {
  450. mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
  451. mbedtls_free( ctx );
  452. }
  453. #if defined(MBEDTLS_ECP_RESTARTABLE)
  454. static void *ecdsa_rs_alloc( void )
  455. {
  456. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
  457. if( ctx != NULL )
  458. mbedtls_ecdsa_restart_init( ctx );
  459. return( ctx );
  460. }
  461. static void ecdsa_rs_free( void *ctx )
  462. {
  463. mbedtls_ecdsa_restart_free( ctx );
  464. mbedtls_free( ctx );
  465. }
  466. #endif /* MBEDTLS_ECP_RESTARTABLE */
  467. const mbedtls_pk_info_t mbedtls_ecdsa_info = {
  468. MBEDTLS_PK_ECDSA,
  469. "ECDSA",
  470. eckey_get_bitlen, /* Compatible key structures */
  471. ecdsa_can_do,
  472. ecdsa_verify_wrap,
  473. ecdsa_sign_wrap,
  474. #if defined(MBEDTLS_ECP_RESTARTABLE)
  475. ecdsa_verify_rs_wrap,
  476. ecdsa_sign_rs_wrap,
  477. #endif
  478. NULL,
  479. NULL,
  480. eckey_check_pair, /* Compatible key structures */
  481. ecdsa_alloc_wrap,
  482. ecdsa_free_wrap,
  483. #if defined(MBEDTLS_ECP_RESTARTABLE)
  484. ecdsa_rs_alloc,
  485. ecdsa_rs_free,
  486. #endif
  487. eckey_debug, /* Compatible key structures */
  488. };
  489. #endif /* MBEDTLS_ECDSA_C */
  490. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  491. /*
  492. * Support for alternative RSA-private implementations
  493. */
  494. static int rsa_alt_can_do( mbedtls_pk_type_t type )
  495. {
  496. return( type == MBEDTLS_PK_RSA );
  497. }
  498. static size_t rsa_alt_get_bitlen( const void *ctx )
  499. {
  500. const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
  501. return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
  502. }
  503. static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  504. const unsigned char *hash, size_t hash_len,
  505. unsigned char *sig, size_t *sig_len,
  506. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  507. {
  508. mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
  509. #if SIZE_MAX > UINT_MAX
  510. if( UINT_MAX < hash_len )
  511. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  512. #endif /* SIZE_MAX > UINT_MAX */
  513. *sig_len = rsa_alt->key_len_func( rsa_alt->key );
  514. return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
  515. md_alg, (unsigned int) hash_len, hash, sig ) );
  516. }
  517. static int rsa_alt_decrypt_wrap( void *ctx,
  518. const unsigned char *input, size_t ilen,
  519. unsigned char *output, size_t *olen, size_t osize,
  520. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  521. {
  522. mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
  523. ((void) f_rng);
  524. ((void) p_rng);
  525. if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
  526. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  527. return( rsa_alt->decrypt_func( rsa_alt->key,
  528. MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
  529. }
  530. #if defined(MBEDTLS_RSA_C)
  531. static int rsa_alt_check_pair( const void *pub, const void *prv )
  532. {
  533. unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
  534. unsigned char hash[32];
  535. size_t sig_len = 0;
  536. int ret;
  537. if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
  538. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  539. memset( hash, 0x2a, sizeof( hash ) );
  540. if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
  541. hash, sizeof( hash ),
  542. sig, &sig_len, NULL, NULL ) ) != 0 )
  543. {
  544. return( ret );
  545. }
  546. if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
  547. hash, sizeof( hash ), sig, sig_len ) != 0 )
  548. {
  549. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  550. }
  551. return( 0 );
  552. }
  553. #endif /* MBEDTLS_RSA_C */
  554. static void *rsa_alt_alloc_wrap( void )
  555. {
  556. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
  557. if( ctx != NULL )
  558. memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
  559. return( ctx );
  560. }
  561. static void rsa_alt_free_wrap( void *ctx )
  562. {
  563. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
  564. mbedtls_free( ctx );
  565. }
  566. const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
  567. MBEDTLS_PK_RSA_ALT,
  568. "RSA-alt",
  569. rsa_alt_get_bitlen,
  570. rsa_alt_can_do,
  571. NULL,
  572. rsa_alt_sign_wrap,
  573. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  574. NULL,
  575. NULL,
  576. #endif
  577. rsa_alt_decrypt_wrap,
  578. NULL,
  579. #if defined(MBEDTLS_RSA_C)
  580. rsa_alt_check_pair,
  581. #else
  582. NULL,
  583. #endif
  584. rsa_alt_alloc_wrap,
  585. rsa_alt_free_wrap,
  586. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  587. NULL,
  588. NULL,
  589. #endif
  590. NULL,
  591. };
  592. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  593. #endif /* MBEDTLS_PK_C */