pk_wrap.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /*
  2. * Public Key abstraction layer: wrapper functions
  3. *
  4. * Copyright The Mbed TLS Contributors
  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. #include "common.h"
  20. #if defined(MBEDTLS_PK_C)
  21. #include "mbedtls/pk_internal.h"
  22. #include "mbedtls/error.h"
  23. /* Even if RSA not activated, for the sake of RSA-alt */
  24. #include "mbedtls/rsa.h"
  25. #include <string.h>
  26. #if defined(MBEDTLS_ECP_C)
  27. #include "mbedtls/ecp.h"
  28. #endif
  29. #if defined(MBEDTLS_ECDSA_C)
  30. #include "mbedtls/ecdsa.h"
  31. #endif
  32. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  33. #include "mbedtls/asn1write.h"
  34. #endif
  35. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  36. #include "mbedtls/platform_util.h"
  37. #endif
  38. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  39. #include "psa/crypto.h"
  40. #include "mbedtls/psa_util.h"
  41. #include "mbedtls/asn1.h"
  42. #endif
  43. #if defined(MBEDTLS_PLATFORM_C)
  44. #include "mbedtls/platform.h"
  45. #else
  46. #include <stdlib.h>
  47. #define mbedtls_calloc calloc
  48. #define mbedtls_free free
  49. #endif
  50. #include <limits.h>
  51. #include <stdint.h>
  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 = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  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. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  161. NULL,
  162. NULL,
  163. #endif
  164. rsa_decrypt_wrap,
  165. rsa_encrypt_wrap,
  166. rsa_check_pair_wrap,
  167. rsa_alloc_wrap,
  168. rsa_free_wrap,
  169. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  170. NULL,
  171. NULL,
  172. #endif
  173. rsa_debug,
  174. };
  175. #endif /* MBEDTLS_RSA_C */
  176. #if defined(MBEDTLS_ECP_C)
  177. /*
  178. * Generic EC key
  179. */
  180. static int eckey_can_do( mbedtls_pk_type_t type )
  181. {
  182. return( type == MBEDTLS_PK_ECKEY ||
  183. type == MBEDTLS_PK_ECKEY_DH ||
  184. type == MBEDTLS_PK_ECDSA );
  185. }
  186. static size_t eckey_get_bitlen( const void *ctx )
  187. {
  188. return( ((mbedtls_ecp_keypair *) ctx)->grp.pbits );
  189. }
  190. #if defined(MBEDTLS_ECDSA_C)
  191. /* Forward declarations */
  192. static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
  193. const unsigned char *hash, size_t hash_len,
  194. const unsigned char *sig, size_t sig_len );
  195. static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  196. const unsigned char *hash, size_t hash_len,
  197. unsigned char *sig, size_t *sig_len,
  198. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  199. static int eckey_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
  200. const unsigned char *hash, size_t hash_len,
  201. const unsigned char *sig, size_t sig_len )
  202. {
  203. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  204. mbedtls_ecdsa_context ecdsa;
  205. mbedtls_ecdsa_init( &ecdsa );
  206. if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
  207. ret = ecdsa_verify_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len );
  208. mbedtls_ecdsa_free( &ecdsa );
  209. return( ret );
  210. }
  211. static int eckey_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  212. const unsigned char *hash, size_t hash_len,
  213. unsigned char *sig, size_t *sig_len,
  214. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  215. {
  216. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  217. mbedtls_ecdsa_context ecdsa;
  218. mbedtls_ecdsa_init( &ecdsa );
  219. if( ( ret = mbedtls_ecdsa_from_keypair( &ecdsa, ctx ) ) == 0 )
  220. ret = ecdsa_sign_wrap( &ecdsa, md_alg, hash, hash_len, sig, sig_len,
  221. f_rng, p_rng );
  222. mbedtls_ecdsa_free( &ecdsa );
  223. return( ret );
  224. }
  225. #if defined(MBEDTLS_ECP_RESTARTABLE)
  226. /* Forward declarations */
  227. static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
  228. const unsigned char *hash, size_t hash_len,
  229. const unsigned char *sig, size_t sig_len,
  230. void *rs_ctx );
  231. static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
  232. const unsigned char *hash, size_t hash_len,
  233. unsigned char *sig, size_t *sig_len,
  234. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  235. void *rs_ctx );
  236. /*
  237. * Restart context for ECDSA operations with ECKEY context
  238. *
  239. * We need to store an actual ECDSA context, as we need to pass the same to
  240. * the underlying ecdsa function, so we can't create it on the fly every time.
  241. */
  242. typedef struct
  243. {
  244. mbedtls_ecdsa_restart_ctx ecdsa_rs;
  245. mbedtls_ecdsa_context ecdsa_ctx;
  246. } eckey_restart_ctx;
  247. static void *eckey_rs_alloc( void )
  248. {
  249. eckey_restart_ctx *rs_ctx;
  250. void *ctx = mbedtls_calloc( 1, sizeof( eckey_restart_ctx ) );
  251. if( ctx != NULL )
  252. {
  253. rs_ctx = ctx;
  254. mbedtls_ecdsa_restart_init( &rs_ctx->ecdsa_rs );
  255. mbedtls_ecdsa_init( &rs_ctx->ecdsa_ctx );
  256. }
  257. return( ctx );
  258. }
  259. static void eckey_rs_free( void *ctx )
  260. {
  261. eckey_restart_ctx *rs_ctx;
  262. if( ctx == NULL)
  263. return;
  264. rs_ctx = ctx;
  265. mbedtls_ecdsa_restart_free( &rs_ctx->ecdsa_rs );
  266. mbedtls_ecdsa_free( &rs_ctx->ecdsa_ctx );
  267. mbedtls_free( ctx );
  268. }
  269. static int eckey_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
  270. const unsigned char *hash, size_t hash_len,
  271. const unsigned char *sig, size_t sig_len,
  272. void *rs_ctx )
  273. {
  274. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  275. eckey_restart_ctx *rs = rs_ctx;
  276. /* Should never happen */
  277. if( rs == NULL )
  278. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  279. /* set up our own sub-context if needed (that is, on first run) */
  280. if( rs->ecdsa_ctx.grp.pbits == 0 )
  281. MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
  282. MBEDTLS_MPI_CHK( ecdsa_verify_rs_wrap( &rs->ecdsa_ctx,
  283. md_alg, hash, hash_len,
  284. sig, sig_len, &rs->ecdsa_rs ) );
  285. cleanup:
  286. return( ret );
  287. }
  288. static int eckey_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
  289. const unsigned char *hash, size_t hash_len,
  290. unsigned char *sig, size_t *sig_len,
  291. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  292. void *rs_ctx )
  293. {
  294. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  295. eckey_restart_ctx *rs = rs_ctx;
  296. /* Should never happen */
  297. if( rs == NULL )
  298. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  299. /* set up our own sub-context if needed (that is, on first run) */
  300. if( rs->ecdsa_ctx.grp.pbits == 0 )
  301. MBEDTLS_MPI_CHK( mbedtls_ecdsa_from_keypair( &rs->ecdsa_ctx, ctx ) );
  302. MBEDTLS_MPI_CHK( ecdsa_sign_rs_wrap( &rs->ecdsa_ctx, md_alg,
  303. hash, hash_len, sig, sig_len,
  304. f_rng, p_rng, &rs->ecdsa_rs ) );
  305. cleanup:
  306. return( ret );
  307. }
  308. #endif /* MBEDTLS_ECP_RESTARTABLE */
  309. #endif /* MBEDTLS_ECDSA_C */
  310. static int eckey_check_pair( const void *pub, const void *prv )
  311. {
  312. return( mbedtls_ecp_check_pub_priv( (const mbedtls_ecp_keypair *) pub,
  313. (const mbedtls_ecp_keypair *) prv ) );
  314. }
  315. static void *eckey_alloc_wrap( void )
  316. {
  317. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
  318. if( ctx != NULL )
  319. mbedtls_ecp_keypair_init( ctx );
  320. return( ctx );
  321. }
  322. static void eckey_free_wrap( void *ctx )
  323. {
  324. mbedtls_ecp_keypair_free( (mbedtls_ecp_keypair *) ctx );
  325. mbedtls_free( ctx );
  326. }
  327. static void eckey_debug( const void *ctx, mbedtls_pk_debug_item *items )
  328. {
  329. items->type = MBEDTLS_PK_DEBUG_ECP;
  330. items->name = "eckey.Q";
  331. items->value = &( ((mbedtls_ecp_keypair *) ctx)->Q );
  332. }
  333. const mbedtls_pk_info_t mbedtls_eckey_info = {
  334. MBEDTLS_PK_ECKEY,
  335. "EC",
  336. eckey_get_bitlen,
  337. eckey_can_do,
  338. #if defined(MBEDTLS_ECDSA_C)
  339. eckey_verify_wrap,
  340. eckey_sign_wrap,
  341. #if defined(MBEDTLS_ECP_RESTARTABLE)
  342. eckey_verify_rs_wrap,
  343. eckey_sign_rs_wrap,
  344. #endif
  345. #else /* MBEDTLS_ECDSA_C */
  346. NULL,
  347. NULL,
  348. #endif /* MBEDTLS_ECDSA_C */
  349. NULL,
  350. NULL,
  351. eckey_check_pair,
  352. eckey_alloc_wrap,
  353. eckey_free_wrap,
  354. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  355. eckey_rs_alloc,
  356. eckey_rs_free,
  357. #endif
  358. eckey_debug,
  359. };
  360. /*
  361. * EC key restricted to ECDH
  362. */
  363. static int eckeydh_can_do( mbedtls_pk_type_t type )
  364. {
  365. return( type == MBEDTLS_PK_ECKEY ||
  366. type == MBEDTLS_PK_ECKEY_DH );
  367. }
  368. const mbedtls_pk_info_t mbedtls_eckeydh_info = {
  369. MBEDTLS_PK_ECKEY_DH,
  370. "EC_DH",
  371. eckey_get_bitlen, /* Same underlying key structure */
  372. eckeydh_can_do,
  373. NULL,
  374. NULL,
  375. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  376. NULL,
  377. NULL,
  378. #endif
  379. NULL,
  380. NULL,
  381. eckey_check_pair,
  382. eckey_alloc_wrap, /* Same underlying key structure */
  383. eckey_free_wrap, /* Same underlying key structure */
  384. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  385. NULL,
  386. NULL,
  387. #endif
  388. eckey_debug, /* Same underlying key structure */
  389. };
  390. #endif /* MBEDTLS_ECP_C */
  391. #if defined(MBEDTLS_ECDSA_C)
  392. static int ecdsa_can_do( mbedtls_pk_type_t type )
  393. {
  394. return( type == MBEDTLS_PK_ECDSA );
  395. }
  396. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  397. /*
  398. * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
  399. * those integers and convert it to the fixed-length encoding expected by PSA.
  400. */
  401. static int extract_ecdsa_sig_int( unsigned char **from, const unsigned char *end,
  402. unsigned char *to, size_t to_len )
  403. {
  404. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  405. size_t unpadded_len, padding_len;
  406. if( ( ret = mbedtls_asn1_get_tag( from, end, &unpadded_len,
  407. MBEDTLS_ASN1_INTEGER ) ) != 0 )
  408. {
  409. return( ret );
  410. }
  411. while( unpadded_len > 0 && **from == 0x00 )
  412. {
  413. ( *from )++;
  414. unpadded_len--;
  415. }
  416. if( unpadded_len > to_len || unpadded_len == 0 )
  417. return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  418. padding_len = to_len - unpadded_len;
  419. memset( to, 0x00, padding_len );
  420. memcpy( to + padding_len, *from, unpadded_len );
  421. ( *from ) += unpadded_len;
  422. return( 0 );
  423. }
  424. /*
  425. * Convert a signature from an ASN.1 sequence of two integers
  426. * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
  427. * twice as big as int_size.
  428. */
  429. static int extract_ecdsa_sig( unsigned char **p, const unsigned char *end,
  430. unsigned char *sig, size_t int_size )
  431. {
  432. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  433. size_t tmp_size;
  434. if( ( ret = mbedtls_asn1_get_tag( p, end, &tmp_size,
  435. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  436. return( ret );
  437. /* Extract r */
  438. if( ( ret = extract_ecdsa_sig_int( p, end, sig, int_size ) ) != 0 )
  439. return( ret );
  440. /* Extract s */
  441. if( ( ret = extract_ecdsa_sig_int( p, end, sig + int_size, int_size ) ) != 0 )
  442. return( ret );
  443. return( 0 );
  444. }
  445. static int ecdsa_verify_wrap( void *ctx_arg, mbedtls_md_type_t md_alg,
  446. const unsigned char *hash, size_t hash_len,
  447. const unsigned char *sig, size_t sig_len )
  448. {
  449. mbedtls_ecdsa_context *ctx = ctx_arg;
  450. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  451. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  452. psa_key_id_t key_id = 0;
  453. psa_status_t status;
  454. mbedtls_pk_context key;
  455. int key_len;
  456. /* see ECP_PUB_DER_MAX_BYTES in pkwrite.c */
  457. unsigned char buf[30 + 2 * MBEDTLS_ECP_MAX_BYTES];
  458. unsigned char *p;
  459. mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
  460. psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
  461. size_t curve_bits;
  462. psa_ecc_family_t curve =
  463. mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
  464. const size_t signature_part_size = ( ctx->grp.nbits + 7 ) / 8;
  465. ((void) md_alg);
  466. if( curve == 0 )
  467. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  468. /* mbedtls_pk_write_pubkey() expects a full PK context;
  469. * re-construct one to make it happy */
  470. key.pk_info = &pk_info;
  471. key.pk_ctx = ctx;
  472. p = buf + sizeof( buf );
  473. key_len = mbedtls_pk_write_pubkey( &p, buf, &key );
  474. if( key_len <= 0 )
  475. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  476. psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve ) );
  477. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
  478. psa_set_key_algorithm( &attributes, psa_sig_md );
  479. status = psa_import_key( &attributes,
  480. buf + sizeof( buf ) - key_len, key_len,
  481. &key_id );
  482. if( status != PSA_SUCCESS )
  483. {
  484. ret = mbedtls_psa_err_translate_pk( status );
  485. goto cleanup;
  486. }
  487. /* We don't need the exported key anymore and can
  488. * reuse its buffer for signature extraction. */
  489. if( 2 * signature_part_size > sizeof( buf ) )
  490. {
  491. ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  492. goto cleanup;
  493. }
  494. p = (unsigned char*) sig;
  495. if( ( ret = extract_ecdsa_sig( &p, sig + sig_len, buf,
  496. signature_part_size ) ) != 0 )
  497. {
  498. goto cleanup;
  499. }
  500. if( psa_verify_hash( key_id, psa_sig_md,
  501. hash, hash_len,
  502. buf, 2 * signature_part_size )
  503. != PSA_SUCCESS )
  504. {
  505. ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
  506. goto cleanup;
  507. }
  508. if( p != sig + sig_len )
  509. {
  510. ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
  511. goto cleanup;
  512. }
  513. ret = 0;
  514. cleanup:
  515. psa_destroy_key( key_id );
  516. return( ret );
  517. }
  518. #else /* MBEDTLS_USE_PSA_CRYPTO */
  519. static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg,
  520. const unsigned char *hash, size_t hash_len,
  521. const unsigned char *sig, size_t sig_len )
  522. {
  523. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  524. ((void) md_alg);
  525. ret = mbedtls_ecdsa_read_signature( (mbedtls_ecdsa_context *) ctx,
  526. hash, hash_len, sig, sig_len );
  527. if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
  528. return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
  529. return( ret );
  530. }
  531. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  532. static int ecdsa_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  533. const unsigned char *hash, size_t hash_len,
  534. unsigned char *sig, size_t *sig_len,
  535. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  536. {
  537. return( mbedtls_ecdsa_write_signature( (mbedtls_ecdsa_context *) ctx,
  538. md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng ) );
  539. }
  540. #if defined(MBEDTLS_ECP_RESTARTABLE)
  541. static int ecdsa_verify_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
  542. const unsigned char *hash, size_t hash_len,
  543. const unsigned char *sig, size_t sig_len,
  544. void *rs_ctx )
  545. {
  546. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  547. ((void) md_alg);
  548. ret = mbedtls_ecdsa_read_signature_restartable(
  549. (mbedtls_ecdsa_context *) ctx,
  550. hash, hash_len, sig, sig_len,
  551. (mbedtls_ecdsa_restart_ctx *) rs_ctx );
  552. if( ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH )
  553. return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
  554. return( ret );
  555. }
  556. static int ecdsa_sign_rs_wrap( void *ctx, mbedtls_md_type_t md_alg,
  557. const unsigned char *hash, size_t hash_len,
  558. unsigned char *sig, size_t *sig_len,
  559. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  560. void *rs_ctx )
  561. {
  562. return( mbedtls_ecdsa_write_signature_restartable(
  563. (mbedtls_ecdsa_context *) ctx,
  564. md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng,
  565. (mbedtls_ecdsa_restart_ctx *) rs_ctx ) );
  566. }
  567. #endif /* MBEDTLS_ECP_RESTARTABLE */
  568. static void *ecdsa_alloc_wrap( void )
  569. {
  570. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_context ) );
  571. if( ctx != NULL )
  572. mbedtls_ecdsa_init( (mbedtls_ecdsa_context *) ctx );
  573. return( ctx );
  574. }
  575. static void ecdsa_free_wrap( void *ctx )
  576. {
  577. mbedtls_ecdsa_free( (mbedtls_ecdsa_context *) ctx );
  578. mbedtls_free( ctx );
  579. }
  580. #if defined(MBEDTLS_ECP_RESTARTABLE)
  581. static void *ecdsa_rs_alloc( void )
  582. {
  583. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ecdsa_restart_ctx ) );
  584. if( ctx != NULL )
  585. mbedtls_ecdsa_restart_init( ctx );
  586. return( ctx );
  587. }
  588. static void ecdsa_rs_free( void *ctx )
  589. {
  590. mbedtls_ecdsa_restart_free( ctx );
  591. mbedtls_free( ctx );
  592. }
  593. #endif /* MBEDTLS_ECP_RESTARTABLE */
  594. const mbedtls_pk_info_t mbedtls_ecdsa_info = {
  595. MBEDTLS_PK_ECDSA,
  596. "ECDSA",
  597. eckey_get_bitlen, /* Compatible key structures */
  598. ecdsa_can_do,
  599. ecdsa_verify_wrap,
  600. ecdsa_sign_wrap,
  601. #if defined(MBEDTLS_ECP_RESTARTABLE)
  602. ecdsa_verify_rs_wrap,
  603. ecdsa_sign_rs_wrap,
  604. #endif
  605. NULL,
  606. NULL,
  607. eckey_check_pair, /* Compatible key structures */
  608. ecdsa_alloc_wrap,
  609. ecdsa_free_wrap,
  610. #if defined(MBEDTLS_ECP_RESTARTABLE)
  611. ecdsa_rs_alloc,
  612. ecdsa_rs_free,
  613. #endif
  614. eckey_debug, /* Compatible key structures */
  615. };
  616. #endif /* MBEDTLS_ECDSA_C */
  617. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  618. /*
  619. * Support for alternative RSA-private implementations
  620. */
  621. static int rsa_alt_can_do( mbedtls_pk_type_t type )
  622. {
  623. return( type == MBEDTLS_PK_RSA );
  624. }
  625. static size_t rsa_alt_get_bitlen( const void *ctx )
  626. {
  627. const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
  628. return( 8 * rsa_alt->key_len_func( rsa_alt->key ) );
  629. }
  630. static int rsa_alt_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  631. const unsigned char *hash, size_t hash_len,
  632. unsigned char *sig, size_t *sig_len,
  633. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  634. {
  635. mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
  636. #if SIZE_MAX > UINT_MAX
  637. if( UINT_MAX < hash_len )
  638. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  639. #endif /* SIZE_MAX > UINT_MAX */
  640. *sig_len = rsa_alt->key_len_func( rsa_alt->key );
  641. if( *sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
  642. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  643. return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE,
  644. md_alg, (unsigned int) hash_len, hash, sig ) );
  645. }
  646. static int rsa_alt_decrypt_wrap( void *ctx,
  647. const unsigned char *input, size_t ilen,
  648. unsigned char *output, size_t *olen, size_t osize,
  649. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  650. {
  651. mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
  652. ((void) f_rng);
  653. ((void) p_rng);
  654. if( ilen != rsa_alt->key_len_func( rsa_alt->key ) )
  655. return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
  656. return( rsa_alt->decrypt_func( rsa_alt->key,
  657. MBEDTLS_RSA_PRIVATE, olen, input, output, osize ) );
  658. }
  659. #if defined(MBEDTLS_RSA_C)
  660. static int rsa_alt_check_pair( const void *pub, const void *prv )
  661. {
  662. unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
  663. unsigned char hash[32];
  664. size_t sig_len = 0;
  665. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  666. if( rsa_alt_get_bitlen( prv ) != rsa_get_bitlen( pub ) )
  667. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  668. memset( hash, 0x2a, sizeof( hash ) );
  669. if( ( ret = rsa_alt_sign_wrap( (void *) prv, MBEDTLS_MD_NONE,
  670. hash, sizeof( hash ),
  671. sig, &sig_len, NULL, NULL ) ) != 0 )
  672. {
  673. return( ret );
  674. }
  675. if( rsa_verify_wrap( (void *) pub, MBEDTLS_MD_NONE,
  676. hash, sizeof( hash ), sig, sig_len ) != 0 )
  677. {
  678. return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  679. }
  680. return( 0 );
  681. }
  682. #endif /* MBEDTLS_RSA_C */
  683. static void *rsa_alt_alloc_wrap( void )
  684. {
  685. void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_alt_context ) );
  686. if( ctx != NULL )
  687. memset( ctx, 0, sizeof( mbedtls_rsa_alt_context ) );
  688. return( ctx );
  689. }
  690. static void rsa_alt_free_wrap( void *ctx )
  691. {
  692. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_rsa_alt_context ) );
  693. mbedtls_free( ctx );
  694. }
  695. const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
  696. MBEDTLS_PK_RSA_ALT,
  697. "RSA-alt",
  698. rsa_alt_get_bitlen,
  699. rsa_alt_can_do,
  700. NULL,
  701. rsa_alt_sign_wrap,
  702. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  703. NULL,
  704. NULL,
  705. #endif
  706. rsa_alt_decrypt_wrap,
  707. NULL,
  708. #if defined(MBEDTLS_RSA_C)
  709. rsa_alt_check_pair,
  710. #else
  711. NULL,
  712. #endif
  713. rsa_alt_alloc_wrap,
  714. rsa_alt_free_wrap,
  715. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  716. NULL,
  717. NULL,
  718. #endif
  719. NULL,
  720. };
  721. #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
  722. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  723. static void *pk_opaque_alloc_wrap( void )
  724. {
  725. void *ctx = mbedtls_calloc( 1, sizeof( psa_key_id_t ) );
  726. /* no _init() function to call, an calloc() already zeroized */
  727. return( ctx );
  728. }
  729. static void pk_opaque_free_wrap( void *ctx )
  730. {
  731. mbedtls_platform_zeroize( ctx, sizeof( psa_key_id_t ) );
  732. mbedtls_free( ctx );
  733. }
  734. static size_t pk_opaque_get_bitlen( const void *ctx )
  735. {
  736. const psa_key_id_t *key = (const psa_key_id_t *) ctx;
  737. size_t bits;
  738. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  739. if( PSA_SUCCESS != psa_get_key_attributes( *key, &attributes ) )
  740. return( 0 );
  741. bits = psa_get_key_bits( &attributes );
  742. psa_reset_key_attributes( &attributes );
  743. return( bits );
  744. }
  745. static int pk_opaque_can_do( mbedtls_pk_type_t type )
  746. {
  747. /* For now opaque PSA keys can only wrap ECC keypairs,
  748. * as checked by setup_psa().
  749. * Also, ECKEY_DH does not really make sense with the current API. */
  750. return( type == MBEDTLS_PK_ECKEY ||
  751. type == MBEDTLS_PK_ECDSA );
  752. }
  753. #if defined(MBEDTLS_ECDSA_C)
  754. /*
  755. * Simultaneously convert and move raw MPI from the beginning of a buffer
  756. * to an ASN.1 MPI at the end of the buffer.
  757. * See also mbedtls_asn1_write_mpi().
  758. *
  759. * p: pointer to the end of the output buffer
  760. * start: start of the output buffer, and also of the mpi to write at the end
  761. * n_len: length of the mpi to read from start
  762. */
  763. static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
  764. size_t n_len )
  765. {
  766. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  767. size_t len = 0;
  768. if( (size_t)( *p - start ) < n_len )
  769. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  770. len = n_len;
  771. *p -= len;
  772. memmove( *p, start, len );
  773. /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
  774. * Neither r nor s should be 0, but as a failsafe measure, still detect
  775. * that rather than overflowing the buffer in case of a PSA error. */
  776. while( len > 0 && **p == 0x00 )
  777. {
  778. ++(*p);
  779. --len;
  780. }
  781. /* this is only reached if the signature was invalid */
  782. if( len == 0 )
  783. return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
  784. /* if the msb is 1, ASN.1 requires that we prepend a 0.
  785. * Neither r nor s can be 0, so we can assume len > 0 at all times. */
  786. if( **p & 0x80 )
  787. {
  788. if( *p - start < 1 )
  789. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  790. *--(*p) = 0x00;
  791. len += 1;
  792. }
  793. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  794. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
  795. MBEDTLS_ASN1_INTEGER ) );
  796. return( (int) len );
  797. }
  798. /* Transcode signature from PSA format to ASN.1 sequence.
  799. * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
  800. * MPIs, and in-place.
  801. *
  802. * [in/out] sig: the signature pre- and post-transcoding
  803. * [in/out] sig_len: signature length pre- and post-transcoding
  804. * [int] buf_len: the available size the in/out buffer
  805. */
  806. static int pk_ecdsa_sig_asn1_from_psa( unsigned char *sig, size_t *sig_len,
  807. size_t buf_len )
  808. {
  809. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  810. size_t len = 0;
  811. const size_t rs_len = *sig_len / 2;
  812. unsigned char *p = sig + buf_len;
  813. MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig + rs_len, rs_len ) );
  814. MBEDTLS_ASN1_CHK_ADD( len, asn1_write_mpibuf( &p, sig, rs_len ) );
  815. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, sig, len ) );
  816. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, sig,
  817. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
  818. memmove( sig, p, len );
  819. *sig_len = len;
  820. return( 0 );
  821. }
  822. #endif /* MBEDTLS_ECDSA_C */
  823. static int pk_opaque_sign_wrap( void *ctx, mbedtls_md_type_t md_alg,
  824. const unsigned char *hash, size_t hash_len,
  825. unsigned char *sig, size_t *sig_len,
  826. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  827. {
  828. #if !defined(MBEDTLS_ECDSA_C)
  829. ((void) ctx);
  830. ((void) md_alg);
  831. ((void) hash);
  832. ((void) hash_len);
  833. ((void) sig);
  834. ((void) sig_len);
  835. ((void) f_rng);
  836. ((void) p_rng);
  837. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  838. #else /* !MBEDTLS_ECDSA_C */
  839. const psa_key_id_t *key = (const psa_key_id_t *) ctx;
  840. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  841. psa_algorithm_t alg = PSA_ALG_ECDSA( mbedtls_psa_translate_md( md_alg ) );
  842. size_t buf_len;
  843. psa_status_t status;
  844. /* PSA has its own RNG */
  845. (void) f_rng;
  846. (void) p_rng;
  847. /* PSA needs an output buffer of known size, but our API doesn't provide
  848. * that information. Assume that the buffer is large enough for a
  849. * maximal-length signature with that key (otherwise the application is
  850. * buggy anyway). */
  851. status = psa_get_key_attributes( *key, &attributes );
  852. if( status != PSA_SUCCESS )
  853. return( mbedtls_psa_err_translate_pk( status ) );
  854. buf_len = MBEDTLS_ECDSA_MAX_SIG_LEN( psa_get_key_bits( &attributes ) );
  855. psa_reset_key_attributes( &attributes );
  856. if( buf_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE )
  857. return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  858. /* make the signature */
  859. status = psa_sign_hash( *key, alg, hash, hash_len,
  860. sig, buf_len, sig_len );
  861. if( status != PSA_SUCCESS )
  862. return( mbedtls_psa_err_translate_pk( status ) );
  863. /* transcode it to ASN.1 sequence */
  864. return( pk_ecdsa_sig_asn1_from_psa( sig, sig_len, buf_len ) );
  865. #endif /* !MBEDTLS_ECDSA_C */
  866. }
  867. const mbedtls_pk_info_t mbedtls_pk_opaque_info = {
  868. MBEDTLS_PK_OPAQUE,
  869. "Opaque",
  870. pk_opaque_get_bitlen,
  871. pk_opaque_can_do,
  872. NULL, /* verify - will be done later */
  873. pk_opaque_sign_wrap,
  874. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  875. NULL, /* restartable verify - not relevant */
  876. NULL, /* restartable sign - not relevant */
  877. #endif
  878. NULL, /* decrypt - will be done later */
  879. NULL, /* encrypt - will be done later */
  880. NULL, /* check_pair - could be done later or left NULL */
  881. pk_opaque_alloc_wrap,
  882. pk_opaque_free_wrap,
  883. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  884. NULL, /* restart alloc - not relevant */
  885. NULL, /* restart free - not relevant */
  886. #endif
  887. NULL, /* debug - could be done later, or even left NULL */
  888. };
  889. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  890. #endif /* MBEDTLS_PK_C */