test_suite_pk.function 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/pk.h"
  3. /* For error codes */
  4. #include "mbedtls/asn1.h"
  5. #include "mbedtls/base64.h"
  6. #include "mbedtls/ecp.h"
  7. #include "mbedtls/rsa.h"
  8. #include <limits.h>
  9. #include <stdint.h>
  10. /* Needed only for test case data under #if defined(MBEDTLS_USE_PSA_CRYPTO),
  11. * but the test code generator requires test case data to be valid C code
  12. * unconditionally (https://github.com/ARMmbed/mbedtls/issues/2023). */
  13. #include "psa/crypto.h"
  14. #define RSA_KEY_SIZE 512
  15. #define RSA_KEY_LEN 64
  16. /** Generate a key of the desired type.
  17. *
  18. * \param pk The PK object to fill. It must have been initialized
  19. * with mbedtls_pk_setup().
  20. * \param parameter - For RSA keys, the key size in bits.
  21. * - For EC keys, the curve (\c MBEDTLS_ECP_DP_xxx).
  22. *
  23. * \return The status from the underlying type-specific key
  24. * generation function.
  25. * \return -1 if the key type is not recognized.
  26. */
  27. static int pk_genkey( mbedtls_pk_context *pk, int parameter )
  28. {
  29. ((void) pk);
  30. (void) parameter;
  31. #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
  32. if( mbedtls_pk_get_type( pk ) == MBEDTLS_PK_RSA )
  33. return mbedtls_rsa_gen_key( mbedtls_pk_rsa( *pk ),
  34. mbedtls_test_rnd_std_rand, NULL,
  35. parameter, 3 );
  36. #endif
  37. #if defined(MBEDTLS_ECP_C)
  38. if( mbedtls_pk_get_type( pk ) == MBEDTLS_PK_ECKEY ||
  39. mbedtls_pk_get_type( pk ) == MBEDTLS_PK_ECKEY_DH ||
  40. mbedtls_pk_get_type( pk ) == MBEDTLS_PK_ECDSA )
  41. {
  42. int ret;
  43. if( ( ret = mbedtls_ecp_group_load( &mbedtls_pk_ec( *pk )->grp,
  44. parameter ) ) != 0 )
  45. return( ret );
  46. return mbedtls_ecp_gen_keypair( &mbedtls_pk_ec( *pk )->grp,
  47. &mbedtls_pk_ec( *pk )->d,
  48. &mbedtls_pk_ec( *pk )->Q,
  49. mbedtls_test_rnd_std_rand, NULL );
  50. }
  51. #endif
  52. return( -1 );
  53. }
  54. #if defined(MBEDTLS_RSA_C)
  55. int mbedtls_rsa_decrypt_func( void *ctx, int mode, size_t *olen,
  56. const unsigned char *input, unsigned char *output,
  57. size_t output_max_len )
  58. {
  59. return( mbedtls_rsa_pkcs1_decrypt( (mbedtls_rsa_context *) ctx,
  60. mbedtls_test_rnd_std_rand, NULL, mode,
  61. olen, input, output, output_max_len ) );
  62. }
  63. int mbedtls_rsa_sign_func( void *ctx,
  64. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  65. int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
  66. const unsigned char *hash, unsigned char *sig )
  67. {
  68. ((void) f_rng);
  69. ((void) p_rng);
  70. return( mbedtls_rsa_pkcs1_sign( (mbedtls_rsa_context *) ctx,
  71. mbedtls_test_rnd_std_rand, NULL, mode,
  72. md_alg, hashlen, hash, sig ) );
  73. }
  74. size_t mbedtls_rsa_key_len_func( void *ctx )
  75. {
  76. return( ((const mbedtls_rsa_context *) ctx)->len );
  77. }
  78. #endif /* MBEDTLS_RSA_C */
  79. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  80. /*
  81. * Generate a key using PSA and return the key identifier of that key,
  82. * or 0 if the key generation failed.
  83. * The key uses NIST P-256 and is usable for signing with SHA-256.
  84. */
  85. mbedtls_svc_key_id_t pk_psa_genkey( void )
  86. {
  87. mbedtls_svc_key_id_t key;
  88. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  89. const psa_key_type_t type =
  90. PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_FAMILY_SECP_R1 );
  91. const size_t bits = 256;
  92. psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
  93. psa_set_key_algorithm( &attributes, PSA_ALG_ECDSA(PSA_ALG_SHA_256) );
  94. psa_set_key_type( &attributes, type );
  95. psa_set_key_bits( &attributes, bits );
  96. PSA_ASSERT( psa_generate_key( &attributes, &key ) );
  97. exit:
  98. return( key );
  99. }
  100. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  101. /* END_HEADER */
  102. /* BEGIN_DEPENDENCIES
  103. * depends_on:MBEDTLS_PK_C
  104. * END_DEPENDENCIES
  105. */
  106. /* BEGIN_CASE depends_on:MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_ECDSA_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED */
  107. void pk_psa_utils( )
  108. {
  109. mbedtls_pk_context pk, pk2;
  110. mbedtls_svc_key_id_t key;
  111. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  112. const char * const name = "Opaque";
  113. const size_t bitlen = 256; /* harcoded in genkey() */
  114. mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
  115. unsigned char b1[1], b2[1];
  116. size_t len;
  117. mbedtls_pk_debug_item dbg;
  118. PSA_ASSERT( psa_crypto_init( ) );
  119. mbedtls_pk_init( &pk );
  120. mbedtls_pk_init( &pk2 );
  121. TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
  122. TEST_ASSERT( mbedtls_pk_setup_opaque( &pk, MBEDTLS_SVC_KEY_ID_INIT ) ==
  123. MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  124. mbedtls_pk_free( &pk );
  125. mbedtls_pk_init( &pk );
  126. key = pk_psa_genkey();
  127. if( mbedtls_svc_key_id_is_null( key ) )
  128. goto exit;
  129. TEST_ASSERT( mbedtls_pk_setup_opaque( &pk, key ) == 0 );
  130. TEST_ASSERT( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_OPAQUE );
  131. TEST_ASSERT( strcmp( mbedtls_pk_get_name( &pk), name ) == 0 );
  132. TEST_ASSERT( mbedtls_pk_get_bitlen( &pk ) == bitlen );
  133. TEST_ASSERT( mbedtls_pk_get_len( &pk ) == bitlen / 8 );
  134. TEST_ASSERT( mbedtls_pk_can_do( &pk, MBEDTLS_PK_ECKEY ) == 1 );
  135. TEST_ASSERT( mbedtls_pk_can_do( &pk, MBEDTLS_PK_ECDSA ) == 1 );
  136. TEST_ASSERT( mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) == 0 );
  137. /* unsupported operations: verify, decrypt, encrypt */
  138. TEST_ASSERT( mbedtls_pk_verify( &pk, md_alg,
  139. b1, sizeof( b1), b2, sizeof( b2 ) )
  140. == MBEDTLS_ERR_PK_TYPE_MISMATCH );
  141. TEST_ASSERT( mbedtls_pk_decrypt( &pk, b1, sizeof( b1 ),
  142. b2, &len, sizeof( b2 ),
  143. NULL, NULL )
  144. == MBEDTLS_ERR_PK_TYPE_MISMATCH );
  145. TEST_ASSERT( mbedtls_pk_encrypt( &pk, b1, sizeof( b1 ),
  146. b2, &len, sizeof( b2 ),
  147. NULL, NULL )
  148. == MBEDTLS_ERR_PK_TYPE_MISMATCH );
  149. /* unsupported functions: check_pair, debug */
  150. TEST_ASSERT( mbedtls_pk_setup( &pk2,
  151. mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY ) ) == 0 );
  152. TEST_ASSERT( mbedtls_pk_check_pair( &pk, &pk2 )
  153. == MBEDTLS_ERR_PK_TYPE_MISMATCH );
  154. TEST_ASSERT( mbedtls_pk_debug( &pk, &dbg )
  155. == MBEDTLS_ERR_PK_TYPE_MISMATCH );
  156. /* test that freeing the context does not destroy the key */
  157. mbedtls_pk_free( &pk );
  158. TEST_ASSERT( PSA_SUCCESS == psa_get_key_attributes( key, &attributes ) );
  159. TEST_ASSERT( PSA_SUCCESS == psa_destroy_key( key ) );
  160. exit:
  161. /*
  162. * Key attributes may have been returned by psa_get_key_attributes()
  163. * thus reset them as required.
  164. */
  165. psa_reset_key_attributes( &attributes );
  166. mbedtls_pk_free( &pk ); /* redundant except upon error */
  167. mbedtls_pk_free( &pk2 );
  168. USE_PSA_DONE( );
  169. }
  170. /* END_CASE */
  171. /* BEGIN_CASE */
  172. void valid_parameters( )
  173. {
  174. mbedtls_pk_context pk;
  175. unsigned char buf[1];
  176. size_t len;
  177. void *options = NULL;
  178. mbedtls_pk_init( &pk );
  179. TEST_VALID_PARAM( mbedtls_pk_free( NULL ) );
  180. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  181. TEST_VALID_PARAM( mbedtls_pk_restart_free( NULL ) );
  182. #endif
  183. TEST_ASSERT( mbedtls_pk_setup( &pk, NULL ) ==
  184. MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  185. /* In informational functions, we accept NULL where a context pointer
  186. * is expected because that's what the library has done forever.
  187. * We do not document that NULL is accepted, so we may wish to change
  188. * the behavior in a future version. */
  189. TEST_ASSERT( mbedtls_pk_get_bitlen( NULL ) == 0 );
  190. TEST_ASSERT( mbedtls_pk_get_len( NULL ) == 0 );
  191. TEST_ASSERT( mbedtls_pk_can_do( NULL, MBEDTLS_PK_NONE ) == 0 );
  192. TEST_ASSERT( mbedtls_pk_sign_restartable( &pk,
  193. MBEDTLS_MD_NONE,
  194. NULL, 0,
  195. buf, &len,
  196. mbedtls_test_rnd_std_rand, NULL,
  197. NULL ) ==
  198. MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  199. TEST_ASSERT( mbedtls_pk_sign_restartable( &pk,
  200. MBEDTLS_MD_NONE,
  201. NULL, 0,
  202. buf, &len,
  203. mbedtls_test_rnd_std_rand, NULL,
  204. NULL ) ==
  205. MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  206. TEST_ASSERT( mbedtls_pk_sign( &pk,
  207. MBEDTLS_MD_NONE,
  208. NULL, 0,
  209. buf, &len,
  210. mbedtls_test_rnd_std_rand, NULL ) ==
  211. MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  212. TEST_ASSERT( mbedtls_pk_verify_restartable( &pk,
  213. MBEDTLS_MD_NONE,
  214. NULL, 0,
  215. buf, sizeof( buf ),
  216. NULL ) ==
  217. MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  218. TEST_ASSERT( mbedtls_pk_verify( &pk,
  219. MBEDTLS_MD_NONE,
  220. NULL, 0,
  221. buf, sizeof( buf ) ) ==
  222. MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  223. TEST_ASSERT( mbedtls_pk_verify_ext( MBEDTLS_PK_NONE, options,
  224. &pk,
  225. MBEDTLS_MD_NONE,
  226. NULL, 0,
  227. buf, sizeof( buf ) ) ==
  228. MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  229. TEST_ASSERT( mbedtls_pk_encrypt( &pk,
  230. NULL, 0,
  231. NULL, &len, 0,
  232. mbedtls_test_rnd_std_rand, NULL ) ==
  233. MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  234. TEST_ASSERT( mbedtls_pk_decrypt( &pk,
  235. NULL, 0,
  236. NULL, &len, 0,
  237. mbedtls_test_rnd_std_rand, NULL ) ==
  238. MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  239. #if defined(MBEDTLS_PK_PARSE_C)
  240. TEST_ASSERT( mbedtls_pk_parse_key( &pk, NULL, 0, NULL, 1 ) ==
  241. MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
  242. TEST_ASSERT( mbedtls_pk_parse_public_key( &pk, NULL, 0 ) ==
  243. MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
  244. #endif /* MBEDTLS_PK_PARSE_C */
  245. }
  246. /* END_CASE */
  247. /* BEGIN_CASE depends_on:MBEDTLS_PK_WRITE_C */
  248. void valid_parameters_pkwrite( data_t *key_data )
  249. {
  250. mbedtls_pk_context pk;
  251. /* For the write tests to be effective, we need a valid key pair. */
  252. mbedtls_pk_init( &pk );
  253. TEST_ASSERT( mbedtls_pk_parse_key( &pk,
  254. key_data->x, key_data->len,
  255. NULL, 0 ) == 0 );
  256. TEST_ASSERT( mbedtls_pk_write_key_der( &pk, NULL, 0 ) ==
  257. MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  258. TEST_ASSERT( mbedtls_pk_write_pubkey_der( &pk, NULL, 0 ) ==
  259. MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  260. #if defined(MBEDTLS_PEM_WRITE_C)
  261. TEST_ASSERT( mbedtls_pk_write_key_pem( &pk, NULL, 0 ) ==
  262. MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
  263. TEST_ASSERT( mbedtls_pk_write_pubkey_pem( &pk, NULL, 0 ) ==
  264. MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
  265. #endif /* MBEDTLS_PEM_WRITE_C */
  266. exit:
  267. mbedtls_pk_free( &pk );
  268. }
  269. /* END_CASE */
  270. /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
  271. void invalid_parameters( )
  272. {
  273. size_t len;
  274. unsigned char *null_buf = NULL;
  275. unsigned char buf[1];
  276. unsigned char *p = buf;
  277. char str[1] = {0};
  278. mbedtls_pk_context pk;
  279. mbedtls_md_type_t valid_md = MBEDTLS_MD_SHA256;
  280. void *options = buf;
  281. (void) null_buf;
  282. (void) p;
  283. (void) str;
  284. mbedtls_pk_init( &pk );
  285. TEST_INVALID_PARAM( mbedtls_pk_init( NULL ) );
  286. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  287. TEST_INVALID_PARAM( mbedtls_pk_restart_init( NULL ) );
  288. #endif
  289. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  290. mbedtls_pk_setup( NULL, NULL ) );
  291. #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  292. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  293. mbedtls_pk_setup_rsa_alt( NULL, buf,
  294. NULL, NULL, NULL ) );
  295. #endif
  296. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  297. mbedtls_pk_verify_restartable( NULL,
  298. MBEDTLS_MD_NONE,
  299. buf, sizeof( buf ),
  300. buf, sizeof( buf ),
  301. NULL ) );
  302. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  303. mbedtls_pk_verify_restartable( &pk,
  304. MBEDTLS_MD_NONE,
  305. NULL, sizeof( buf ),
  306. buf, sizeof( buf ),
  307. NULL ) );
  308. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  309. mbedtls_pk_verify_restartable( &pk,
  310. valid_md,
  311. NULL, 0,
  312. buf, sizeof( buf ),
  313. NULL ) );
  314. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  315. mbedtls_pk_verify_restartable( &pk,
  316. MBEDTLS_MD_NONE,
  317. buf, sizeof( buf ),
  318. NULL, sizeof( buf ),
  319. NULL ) );
  320. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  321. mbedtls_pk_verify( NULL,
  322. MBEDTLS_MD_NONE,
  323. buf, sizeof( buf ),
  324. buf, sizeof( buf ) ) );
  325. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  326. mbedtls_pk_verify( &pk,
  327. MBEDTLS_MD_NONE,
  328. NULL, sizeof( buf ),
  329. buf, sizeof( buf ) ) );
  330. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  331. mbedtls_pk_verify( &pk,
  332. valid_md,
  333. NULL, 0,
  334. buf, sizeof( buf ) ) );
  335. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  336. mbedtls_pk_verify( &pk,
  337. MBEDTLS_MD_NONE,
  338. buf, sizeof( buf ),
  339. NULL, sizeof( buf ) ) );
  340. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  341. mbedtls_pk_verify_ext( MBEDTLS_PK_NONE, options,
  342. NULL,
  343. MBEDTLS_MD_NONE,
  344. buf, sizeof( buf ),
  345. buf, sizeof( buf ) ) );
  346. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  347. mbedtls_pk_verify_ext( MBEDTLS_PK_NONE, options,
  348. &pk,
  349. MBEDTLS_MD_NONE,
  350. NULL, sizeof( buf ),
  351. buf, sizeof( buf ) ) );
  352. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  353. mbedtls_pk_verify_ext( MBEDTLS_PK_NONE, options,
  354. &pk,
  355. valid_md,
  356. NULL, 0,
  357. buf, sizeof( buf ) ) );
  358. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  359. mbedtls_pk_verify_ext( MBEDTLS_PK_NONE, options,
  360. &pk,
  361. MBEDTLS_MD_NONE,
  362. buf, sizeof( buf ),
  363. NULL, sizeof( buf ) ) );
  364. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  365. mbedtls_pk_sign_restartable( NULL, MBEDTLS_MD_NONE, buf, sizeof( buf ),
  366. buf, &len, mbedtls_test_rnd_std_rand,
  367. NULL, NULL ) );
  368. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  369. mbedtls_pk_sign_restartable( &pk, MBEDTLS_MD_NONE, NULL, sizeof( buf ),
  370. buf, &len, mbedtls_test_rnd_std_rand,
  371. NULL, NULL ) );
  372. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  373. mbedtls_pk_sign_restartable( &pk, valid_md, NULL, 0, buf, &len,
  374. mbedtls_test_rnd_std_rand, NULL, NULL ) );
  375. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  376. mbedtls_pk_sign_restartable( &pk, MBEDTLS_MD_NONE, buf, sizeof( buf ),
  377. NULL, &len, mbedtls_test_rnd_std_rand,
  378. NULL, NULL ) );
  379. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  380. mbedtls_pk_sign( NULL, MBEDTLS_MD_NONE, buf, sizeof( buf ),
  381. buf, &len, mbedtls_test_rnd_std_rand, NULL ) );
  382. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  383. mbedtls_pk_sign( &pk, MBEDTLS_MD_NONE, NULL, sizeof( buf ),
  384. buf, &len, mbedtls_test_rnd_std_rand, NULL ) );
  385. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  386. mbedtls_pk_sign( &pk, valid_md, NULL, 0, buf, &len,
  387. mbedtls_test_rnd_std_rand, NULL ) );
  388. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  389. mbedtls_pk_sign( &pk, MBEDTLS_MD_NONE, buf, sizeof( buf ), NULL, &len,
  390. mbedtls_test_rnd_std_rand, NULL ) );
  391. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  392. mbedtls_pk_decrypt( NULL, buf, sizeof( buf ), buf, &len, sizeof( buf ),
  393. mbedtls_test_rnd_std_rand, NULL ) );
  394. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  395. mbedtls_pk_decrypt( &pk, NULL, sizeof( buf ), buf, &len, sizeof( buf ),
  396. mbedtls_test_rnd_std_rand, NULL ) );
  397. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  398. mbedtls_pk_decrypt( &pk, buf, sizeof( buf ), NULL, &len, sizeof( buf ),
  399. mbedtls_test_rnd_std_rand, NULL ) );
  400. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  401. mbedtls_pk_decrypt( &pk, buf, sizeof( buf ), buf, NULL, sizeof( buf ),
  402. mbedtls_test_rnd_std_rand, NULL ) );
  403. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  404. mbedtls_pk_encrypt( NULL, buf, sizeof( buf ), buf, &len, sizeof( buf ),
  405. mbedtls_test_rnd_std_rand, NULL ) );
  406. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  407. mbedtls_pk_encrypt( &pk, NULL, sizeof( buf ), buf, &len, sizeof( buf ),
  408. mbedtls_test_rnd_std_rand, NULL ) );
  409. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  410. mbedtls_pk_encrypt( &pk, buf, sizeof( buf ), NULL, &len, sizeof( buf ),
  411. mbedtls_test_rnd_std_rand, NULL ) );
  412. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  413. mbedtls_pk_encrypt( &pk, buf, sizeof( buf ), buf, NULL, sizeof( buf ),
  414. mbedtls_test_rnd_std_rand, NULL ) );
  415. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  416. mbedtls_pk_check_pair( NULL, &pk ) );
  417. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  418. mbedtls_pk_check_pair( &pk, NULL ) );
  419. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  420. mbedtls_pk_debug( NULL, NULL ) );
  421. #if defined(MBEDTLS_PK_PARSE_C)
  422. #if defined(MBEDTLS_FS_IO)
  423. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  424. mbedtls_pk_load_file( NULL, &p, &len ) );
  425. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  426. mbedtls_pk_load_file( str, NULL, &len ) );
  427. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  428. mbedtls_pk_load_file( str, &p, NULL ) );
  429. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  430. mbedtls_pk_parse_keyfile( NULL, str, NULL ) );
  431. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  432. mbedtls_pk_parse_keyfile( &pk, NULL, NULL ) );
  433. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  434. mbedtls_pk_parse_public_keyfile( NULL, str ) );
  435. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  436. mbedtls_pk_parse_public_keyfile( &pk, NULL ) );
  437. #endif
  438. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  439. mbedtls_pk_parse_subpubkey( NULL, buf, &pk ) );
  440. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  441. mbedtls_pk_parse_subpubkey( &null_buf, buf, &pk ) );
  442. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  443. mbedtls_pk_parse_subpubkey( &p, NULL, &pk ) );
  444. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  445. mbedtls_pk_parse_subpubkey( &p, buf, NULL ) );
  446. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  447. mbedtls_pk_parse_key( NULL,
  448. buf, sizeof( buf ),
  449. buf, sizeof( buf ) ) );
  450. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  451. mbedtls_pk_parse_key( &pk,
  452. NULL, sizeof( buf ),
  453. buf, sizeof( buf ) ) );
  454. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  455. mbedtls_pk_parse_public_key( NULL,
  456. buf, sizeof( buf ) ) );
  457. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  458. mbedtls_pk_parse_public_key( &pk,
  459. NULL, sizeof( buf ) ) );
  460. #endif /* MBEDTLS_PK_PARSE_C */
  461. #if defined(MBEDTLS_PK_WRITE_C)
  462. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  463. mbedtls_pk_write_pubkey( NULL, p, &pk ) );
  464. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  465. mbedtls_pk_write_pubkey( &null_buf, p, &pk ) );
  466. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  467. mbedtls_pk_write_pubkey( &p, NULL, &pk ) );
  468. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  469. mbedtls_pk_write_pubkey( &p, p, NULL ) );
  470. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  471. mbedtls_pk_write_pubkey_der( NULL,
  472. buf, sizeof( buf ) ) );
  473. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  474. mbedtls_pk_write_pubkey_der( &pk,
  475. NULL, sizeof( buf ) ) );
  476. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  477. mbedtls_pk_write_key_der( NULL,
  478. buf, sizeof( buf ) ) );
  479. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  480. mbedtls_pk_write_key_der( &pk,
  481. NULL, sizeof( buf ) ) );
  482. #if defined(MBEDTLS_PEM_WRITE_C)
  483. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  484. mbedtls_pk_write_pubkey_pem( NULL,
  485. buf, sizeof( buf ) ) );
  486. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  487. mbedtls_pk_write_pubkey_pem( &pk,
  488. NULL, sizeof( buf ) ) );
  489. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  490. mbedtls_pk_write_key_pem( NULL,
  491. buf, sizeof( buf ) ) );
  492. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
  493. mbedtls_pk_write_key_pem( &pk,
  494. NULL, sizeof( buf ) ) );
  495. #endif /* MBEDTLS_PEM_WRITE_C */
  496. #endif /* MBEDTLS_PK_WRITE_C */
  497. }
  498. /* END_CASE */
  499. /* BEGIN_CASE */
  500. void pk_utils( int type, int parameter, int bitlen, int len, char * name )
  501. {
  502. mbedtls_pk_context pk;
  503. mbedtls_pk_init( &pk );
  504. TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( type ) ) == 0 );
  505. TEST_ASSERT( pk_genkey( &pk, parameter ) == 0 );
  506. TEST_ASSERT( (int) mbedtls_pk_get_type( &pk ) == type );
  507. TEST_ASSERT( mbedtls_pk_can_do( &pk, type ) );
  508. TEST_ASSERT( mbedtls_pk_get_bitlen( &pk ) == (unsigned) bitlen );
  509. TEST_ASSERT( mbedtls_pk_get_len( &pk ) == (unsigned) len );
  510. TEST_ASSERT( strcmp( mbedtls_pk_get_name( &pk), name ) == 0 );
  511. exit:
  512. mbedtls_pk_free( &pk );
  513. }
  514. /* END_CASE */
  515. /* BEGIN_CASE depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_FS_IO */
  516. void mbedtls_pk_check_pair( char * pub_file, char * prv_file, int ret )
  517. {
  518. mbedtls_pk_context pub, prv, alt;
  519. mbedtls_pk_init( &pub );
  520. mbedtls_pk_init( &prv );
  521. mbedtls_pk_init( &alt );
  522. TEST_ASSERT( mbedtls_pk_parse_public_keyfile( &pub, pub_file ) == 0 );
  523. TEST_ASSERT( mbedtls_pk_parse_keyfile( &prv, prv_file, NULL ) == 0 );
  524. TEST_ASSERT( mbedtls_pk_check_pair( &pub, &prv ) == ret );
  525. #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
  526. if( mbedtls_pk_get_type( &prv ) == MBEDTLS_PK_RSA )
  527. {
  528. TEST_ASSERT( mbedtls_pk_setup_rsa_alt( &alt, mbedtls_pk_rsa( prv ),
  529. mbedtls_rsa_decrypt_func, mbedtls_rsa_sign_func,
  530. mbedtls_rsa_key_len_func ) == 0 );
  531. TEST_ASSERT( mbedtls_pk_check_pair( &pub, &alt ) == ret );
  532. }
  533. #endif
  534. mbedtls_pk_free( &pub );
  535. mbedtls_pk_free( &prv );
  536. mbedtls_pk_free( &alt );
  537. }
  538. /* END_CASE */
  539. /* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
  540. void pk_rsa_verify_test_vec( data_t * message_str, int digest, int mod,
  541. int radix_N, char * input_N, int radix_E,
  542. char * input_E, data_t * result_str,
  543. int result )
  544. {
  545. unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
  546. mbedtls_rsa_context *rsa;
  547. mbedtls_pk_context pk;
  548. mbedtls_pk_restart_ctx *rs_ctx = NULL;
  549. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  550. mbedtls_pk_restart_ctx ctx;
  551. rs_ctx = &ctx;
  552. mbedtls_pk_restart_init( rs_ctx );
  553. // this setting would ensure restart would happen if ECC was used
  554. mbedtls_ecp_set_max_ops( 1 );
  555. #endif
  556. mbedtls_pk_init( &pk );
  557. memset( hash_result, 0x00, MBEDTLS_MD_MAX_SIZE );
  558. TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 );
  559. rsa = mbedtls_pk_rsa( pk );
  560. rsa->len = mod / 8;
  561. TEST_ASSERT( mbedtls_test_read_mpi( &rsa->N, radix_N, input_N ) == 0 );
  562. TEST_ASSERT( mbedtls_test_read_mpi( &rsa->E, radix_E, input_E ) == 0 );
  563. if( mbedtls_md_info_from_type( digest ) != NULL )
  564. TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str->x, message_str->len, hash_result ) == 0 );
  565. TEST_ASSERT( mbedtls_pk_verify( &pk, digest, hash_result, 0,
  566. result_str->x, mbedtls_pk_get_len( &pk ) ) == result );
  567. TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, digest, hash_result, 0,
  568. result_str->x, mbedtls_pk_get_len( &pk ), rs_ctx ) == result );
  569. exit:
  570. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  571. mbedtls_pk_restart_free( rs_ctx );
  572. #endif
  573. mbedtls_pk_free( &pk );
  574. }
  575. /* END_CASE */
  576. /* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
  577. void pk_rsa_verify_ext_test_vec( data_t * message_str, int digest,
  578. int mod, int radix_N, char * input_N,
  579. int radix_E, char * input_E,
  580. data_t * result_str, int pk_type,
  581. int mgf1_hash_id, int salt_len, int result )
  582. {
  583. unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
  584. mbedtls_rsa_context *rsa;
  585. mbedtls_pk_context pk;
  586. mbedtls_pk_rsassa_pss_options pss_opts;
  587. void *options;
  588. size_t hash_len;
  589. mbedtls_pk_init( &pk );
  590. memset( hash_result, 0x00, sizeof( hash_result ) );
  591. TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 );
  592. rsa = mbedtls_pk_rsa( pk );
  593. rsa->len = mod / 8;
  594. TEST_ASSERT( mbedtls_test_read_mpi( &rsa->N, radix_N, input_N ) == 0 );
  595. TEST_ASSERT( mbedtls_test_read_mpi( &rsa->E, radix_E, input_E ) == 0 );
  596. if( digest != MBEDTLS_MD_NONE )
  597. {
  598. TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ),
  599. message_str->x, message_str->len, hash_result ) == 0 );
  600. hash_len = 0;
  601. }
  602. else
  603. {
  604. memcpy( hash_result, message_str->x, message_str->len );
  605. hash_len = message_str->len;
  606. }
  607. if( mgf1_hash_id < 0 )
  608. {
  609. options = NULL;
  610. }
  611. else
  612. {
  613. options = &pss_opts;
  614. pss_opts.mgf1_hash_id = mgf1_hash_id;
  615. pss_opts.expected_salt_len = salt_len;
  616. }
  617. TEST_ASSERT( mbedtls_pk_verify_ext( pk_type, options, &pk,
  618. digest, hash_result, hash_len,
  619. result_str->x, mbedtls_pk_get_len( &pk ) ) == result );
  620. exit:
  621. mbedtls_pk_free( &pk );
  622. }
  623. /* END_CASE */
  624. /* BEGIN_CASE depends_on:MBEDTLS_ECDSA_C */
  625. void pk_ec_test_vec( int type, int id, data_t * key, data_t * hash,
  626. data_t * sig, int ret )
  627. {
  628. mbedtls_pk_context pk;
  629. mbedtls_ecp_keypair *eckey;
  630. mbedtls_pk_init( &pk );
  631. USE_PSA_INIT( );
  632. TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( type ) ) == 0 );
  633. TEST_ASSERT( mbedtls_pk_can_do( &pk, MBEDTLS_PK_ECDSA ) );
  634. eckey = mbedtls_pk_ec( pk );
  635. TEST_ASSERT( mbedtls_ecp_group_load( &eckey->grp, id ) == 0 );
  636. TEST_ASSERT( mbedtls_ecp_point_read_binary( &eckey->grp, &eckey->Q,
  637. key->x, key->len ) == 0 );
  638. // MBEDTLS_MD_NONE is used since it will be ignored.
  639. TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_NONE,
  640. hash->x, hash->len, sig->x, sig->len ) == ret );
  641. exit:
  642. mbedtls_pk_free( &pk );
  643. USE_PSA_DONE( );
  644. }
  645. /* END_CASE */
  646. /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_C:MBEDTLS_ECDSA_DETERMINISTIC */
  647. void pk_sign_verify_restart( int pk_type, int grp_id, char *d_str,
  648. char *QX_str, char *QY_str,
  649. int md_alg, char *msg, data_t *sig_check,
  650. int max_ops, int min_restart, int max_restart )
  651. {
  652. int ret, cnt_restart;
  653. mbedtls_pk_restart_ctx rs_ctx;
  654. mbedtls_pk_context prv, pub;
  655. unsigned char hash[MBEDTLS_MD_MAX_SIZE];
  656. unsigned char sig[MBEDTLS_ECDSA_MAX_LEN];
  657. size_t hlen, slen;
  658. const mbedtls_md_info_t *md_info;
  659. mbedtls_pk_restart_init( &rs_ctx );
  660. mbedtls_pk_init( &prv );
  661. mbedtls_pk_init( &pub );
  662. memset( hash, 0, sizeof( hash ) );
  663. memset( sig, 0, sizeof( sig ) );
  664. TEST_ASSERT( mbedtls_pk_setup( &prv, mbedtls_pk_info_from_type( pk_type ) ) == 0 );
  665. TEST_ASSERT( mbedtls_ecp_group_load( &mbedtls_pk_ec( prv )->grp, grp_id ) == 0 );
  666. TEST_ASSERT( mbedtls_test_read_mpi( &mbedtls_pk_ec( prv )->d, 16, d_str ) == 0 );
  667. TEST_ASSERT( mbedtls_pk_setup( &pub, mbedtls_pk_info_from_type( pk_type ) ) == 0 );
  668. TEST_ASSERT( mbedtls_ecp_group_load( &mbedtls_pk_ec( pub )->grp, grp_id ) == 0 );
  669. TEST_ASSERT( mbedtls_ecp_point_read_string( &mbedtls_pk_ec( pub )->Q, 16, QX_str, QY_str ) == 0 );
  670. md_info = mbedtls_md_info_from_type( md_alg );
  671. TEST_ASSERT( md_info != NULL );
  672. hlen = mbedtls_md_get_size( md_info );
  673. TEST_ASSERT( mbedtls_md( md_info,
  674. (const unsigned char *) msg, strlen( msg ),
  675. hash ) == 0 );
  676. mbedtls_ecp_set_max_ops( max_ops );
  677. slen = sizeof( sig );
  678. cnt_restart = 0;
  679. do {
  680. ret = mbedtls_pk_sign_restartable( &prv, md_alg, hash, hlen,
  681. sig, &slen, NULL, NULL, &rs_ctx );
  682. } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
  683. TEST_ASSERT( ret == 0 );
  684. TEST_ASSERT( slen == sig_check->len );
  685. TEST_ASSERT( memcmp( sig, sig_check->x, slen ) == 0 );
  686. TEST_ASSERT( cnt_restart >= min_restart );
  687. TEST_ASSERT( cnt_restart <= max_restart );
  688. cnt_restart = 0;
  689. do {
  690. ret = mbedtls_pk_verify_restartable( &pub, md_alg,
  691. hash, hlen, sig, slen, &rs_ctx );
  692. } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
  693. TEST_ASSERT( ret == 0 );
  694. TEST_ASSERT( cnt_restart >= min_restart );
  695. TEST_ASSERT( cnt_restart <= max_restart );
  696. hash[0]++;
  697. do {
  698. ret = mbedtls_pk_verify_restartable( &pub, md_alg,
  699. hash, hlen, sig, slen, &rs_ctx );
  700. } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
  701. TEST_ASSERT( ret != 0 );
  702. hash[0]--;
  703. sig[0]++;
  704. do {
  705. ret = mbedtls_pk_verify_restartable( &pub, md_alg,
  706. hash, hlen, sig, slen, &rs_ctx );
  707. } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
  708. TEST_ASSERT( ret != 0 );
  709. sig[0]--;
  710. /* Do we leak memory when aborting? try verify then sign
  711. * This test only makes sense when we actually restart */
  712. if( min_restart > 0 )
  713. {
  714. ret = mbedtls_pk_verify_restartable( &pub, md_alg,
  715. hash, hlen, sig, slen, &rs_ctx );
  716. TEST_ASSERT( ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
  717. mbedtls_pk_restart_free( &rs_ctx );
  718. slen = sizeof( sig );
  719. ret = mbedtls_pk_sign_restartable( &prv, md_alg, hash, hlen,
  720. sig, &slen, NULL, NULL, &rs_ctx );
  721. TEST_ASSERT( ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
  722. }
  723. exit:
  724. mbedtls_pk_restart_free( &rs_ctx );
  725. mbedtls_pk_free( &prv );
  726. mbedtls_pk_free( &pub );
  727. }
  728. /* END_CASE */
  729. /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
  730. void pk_sign_verify( int type, int parameter, int sign_ret, int verify_ret )
  731. {
  732. mbedtls_pk_context pk;
  733. size_t sig_len, hash_len;
  734. mbedtls_md_type_t md = MBEDTLS_MD_SHA256;
  735. unsigned char *hash = NULL;
  736. unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
  737. void *rs_ctx = NULL;
  738. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  739. mbedtls_pk_restart_ctx ctx;
  740. rs_ctx = &ctx;
  741. mbedtls_pk_restart_init( rs_ctx );
  742. /* This value is large enough that the operation will complete in one run.
  743. * See comments at the top of ecp_test_vect_restart in
  744. * test_suite_ecp.function for estimates of operation counts. */
  745. mbedtls_ecp_set_max_ops( 42000 );
  746. #endif
  747. hash_len = mbedtls_md_get_size( mbedtls_md_info_from_type( md ) );
  748. ASSERT_ALLOC( hash, hash_len );
  749. mbedtls_pk_init( &pk );
  750. USE_PSA_INIT( );
  751. memset( hash, 0x2a, hash_len );
  752. memset( sig, 0, sizeof sig );
  753. TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( type ) ) == 0 );
  754. TEST_ASSERT( pk_genkey( &pk, parameter ) == 0 );
  755. TEST_ASSERT( mbedtls_pk_sign_restartable( &pk, md,
  756. hash, hash_len, sig, &sig_len,
  757. mbedtls_test_rnd_std_rand, NULL, rs_ctx ) == sign_ret );
  758. if( sign_ret == 0 )
  759. TEST_ASSERT( sig_len <= MBEDTLS_PK_SIGNATURE_MAX_SIZE );
  760. else
  761. sig_len = MBEDTLS_PK_SIGNATURE_MAX_SIZE;
  762. TEST_ASSERT( mbedtls_pk_verify( &pk, md,
  763. hash, hash_len, sig, sig_len ) == verify_ret );
  764. if( verify_ret == 0 )
  765. {
  766. hash[0]++;
  767. TEST_ASSERT( mbedtls_pk_verify( &pk, md,
  768. hash, hash_len, sig, sig_len ) != 0 );
  769. hash[0]--;
  770. sig[0]++;
  771. TEST_ASSERT( mbedtls_pk_verify( &pk, md,
  772. hash, hash_len, sig, sig_len ) != 0 );
  773. sig[0]--;
  774. }
  775. TEST_ASSERT( mbedtls_pk_sign( &pk, md, hash, hash_len,
  776. sig, &sig_len,
  777. mbedtls_test_rnd_std_rand,
  778. NULL ) == sign_ret );
  779. if( sign_ret == 0 )
  780. TEST_ASSERT( sig_len <= MBEDTLS_PK_SIGNATURE_MAX_SIZE );
  781. else
  782. sig_len = MBEDTLS_PK_SIGNATURE_MAX_SIZE;
  783. TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, md,
  784. hash, hash_len, sig, sig_len, rs_ctx ) == verify_ret );
  785. if( verify_ret == 0 )
  786. {
  787. hash[0]++;
  788. TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, md,
  789. hash, hash_len, sig, sig_len, rs_ctx ) != 0 );
  790. hash[0]--;
  791. sig[0]++;
  792. TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, md,
  793. hash, hash_len, sig, sig_len, rs_ctx ) != 0 );
  794. sig[0]--;
  795. }
  796. exit:
  797. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  798. mbedtls_pk_restart_free( rs_ctx );
  799. #endif
  800. mbedtls_pk_free( &pk );
  801. mbedtls_free( hash );
  802. USE_PSA_DONE( );
  803. }
  804. /* END_CASE */
  805. /* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
  806. void pk_rsa_encrypt_test_vec( data_t * message, int mod, int radix_N,
  807. char * input_N, int radix_E, char * input_E,
  808. data_t * result, int ret )
  809. {
  810. unsigned char output[300];
  811. mbedtls_test_rnd_pseudo_info rnd_info;
  812. mbedtls_rsa_context *rsa;
  813. mbedtls_pk_context pk;
  814. size_t olen;
  815. memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
  816. memset( output, 0, sizeof( output ) );
  817. mbedtls_pk_init( &pk );
  818. TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 );
  819. rsa = mbedtls_pk_rsa( pk );
  820. rsa->len = mod / 8;
  821. TEST_ASSERT( mbedtls_test_read_mpi( &rsa->N, radix_N, input_N ) == 0 );
  822. TEST_ASSERT( mbedtls_test_read_mpi( &rsa->E, radix_E, input_E ) == 0 );
  823. TEST_ASSERT( mbedtls_pk_encrypt( &pk, message->x, message->len,
  824. output, &olen, sizeof( output ),
  825. mbedtls_test_rnd_pseudo_rand, &rnd_info ) == ret );
  826. TEST_ASSERT( olen == result->len );
  827. TEST_ASSERT( memcmp( output, result->x, olen ) == 0 );
  828. exit:
  829. mbedtls_pk_free( &pk );
  830. }
  831. /* END_CASE */
  832. /* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
  833. void pk_rsa_decrypt_test_vec( data_t * cipher, int mod, int radix_P,
  834. char * input_P, int radix_Q, char * input_Q,
  835. int radix_N, char * input_N, int radix_E,
  836. char * input_E, data_t * clear, int ret )
  837. {
  838. unsigned char output[256];
  839. mbedtls_test_rnd_pseudo_info rnd_info;
  840. mbedtls_mpi N, P, Q, E;
  841. mbedtls_rsa_context *rsa;
  842. mbedtls_pk_context pk;
  843. size_t olen;
  844. mbedtls_pk_init( &pk );
  845. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
  846. mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
  847. memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
  848. /* init pk-rsa context */
  849. TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 );
  850. rsa = mbedtls_pk_rsa( pk );
  851. /* load public key */
  852. TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
  853. TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
  854. /* load private key */
  855. TEST_ASSERT( mbedtls_test_read_mpi( &P, radix_P, input_P ) == 0 );
  856. TEST_ASSERT( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ) == 0 );
  857. TEST_ASSERT( mbedtls_rsa_import( rsa, &N, &P, &Q, NULL, &E ) == 0 );
  858. TEST_ASSERT( mbedtls_rsa_get_len( rsa ) == (size_t) ( mod / 8 ) );
  859. TEST_ASSERT( mbedtls_rsa_complete( rsa ) == 0 );
  860. /* decryption test */
  861. memset( output, 0, sizeof( output ) );
  862. olen = 0;
  863. TEST_ASSERT( mbedtls_pk_decrypt( &pk, cipher->x, cipher->len,
  864. output, &olen, sizeof( output ),
  865. mbedtls_test_rnd_pseudo_rand, &rnd_info ) == ret );
  866. if( ret == 0 )
  867. {
  868. TEST_ASSERT( olen == clear->len );
  869. TEST_ASSERT( memcmp( output, clear->x, olen ) == 0 );
  870. }
  871. exit:
  872. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
  873. mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
  874. mbedtls_pk_free( &pk );
  875. }
  876. /* END_CASE */
  877. /* BEGIN_CASE */
  878. void pk_ec_nocrypt( int type )
  879. {
  880. mbedtls_pk_context pk;
  881. unsigned char output[100];
  882. unsigned char input[100];
  883. mbedtls_test_rnd_pseudo_info rnd_info;
  884. size_t olen = 0;
  885. int ret = MBEDTLS_ERR_PK_TYPE_MISMATCH;
  886. mbedtls_pk_init( &pk );
  887. memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
  888. memset( output, 0, sizeof( output ) );
  889. memset( input, 0, sizeof( input ) );
  890. TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( type ) ) == 0 );
  891. TEST_ASSERT( mbedtls_pk_encrypt( &pk, input, sizeof( input ),
  892. output, &olen, sizeof( output ),
  893. mbedtls_test_rnd_pseudo_rand, &rnd_info ) == ret );
  894. TEST_ASSERT( mbedtls_pk_decrypt( &pk, input, sizeof( input ),
  895. output, &olen, sizeof( output ),
  896. mbedtls_test_rnd_pseudo_rand, &rnd_info ) == ret );
  897. exit:
  898. mbedtls_pk_free( &pk );
  899. }
  900. /* END_CASE */
  901. /* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
  902. void pk_rsa_overflow( )
  903. {
  904. mbedtls_pk_context pk;
  905. size_t hash_len = SIZE_MAX, sig_len = SIZE_MAX;
  906. unsigned char hash[50], sig[100];
  907. if( SIZE_MAX <= UINT_MAX )
  908. return;
  909. memset( hash, 0x2a, sizeof hash );
  910. memset( sig, 0, sizeof sig );
  911. mbedtls_pk_init( &pk );
  912. TEST_ASSERT( mbedtls_pk_setup( &pk,
  913. mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 );
  914. #if defined(MBEDTLS_PKCS1_V21)
  915. TEST_ASSERT( mbedtls_pk_verify_ext( MBEDTLS_PK_RSASSA_PSS, NULL, &pk,
  916. MBEDTLS_MD_NONE, hash, hash_len, sig, sig_len ) ==
  917. MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  918. #endif /* MBEDTLS_PKCS1_V21 */
  919. TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_NONE, hash, hash_len,
  920. sig, sig_len ) == MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  921. TEST_ASSERT( mbedtls_pk_sign( &pk, MBEDTLS_MD_NONE, hash, hash_len, sig,
  922. &sig_len, mbedtls_test_rnd_std_rand, NULL )
  923. == MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  924. exit:
  925. mbedtls_pk_free( &pk );
  926. }
  927. /* END_CASE */
  928. /* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_PK_RSA_ALT_SUPPORT */
  929. void pk_rsa_alt( )
  930. {
  931. /*
  932. * An rsa_alt context can only do private operations (decrypt, sign).
  933. * Test it against the public operations (encrypt, verify) of a
  934. * corresponding rsa context.
  935. */
  936. mbedtls_rsa_context raw;
  937. mbedtls_pk_context rsa, alt;
  938. mbedtls_pk_debug_item dbg_items[10];
  939. unsigned char hash[50], sig[64];
  940. unsigned char msg[50], ciph[64], test[50];
  941. size_t sig_len, ciph_len, test_len;
  942. int ret = MBEDTLS_ERR_PK_TYPE_MISMATCH;
  943. mbedtls_rsa_init( &raw, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
  944. mbedtls_pk_init( &rsa ); mbedtls_pk_init( &alt );
  945. memset( hash, 0x2a, sizeof hash );
  946. memset( sig, 0, sizeof sig );
  947. memset( msg, 0x2a, sizeof msg );
  948. memset( ciph, 0, sizeof ciph );
  949. memset( test, 0, sizeof test );
  950. /* Initiliaze PK RSA context with random key */
  951. TEST_ASSERT( mbedtls_pk_setup( &rsa,
  952. mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 );
  953. TEST_ASSERT( pk_genkey( &rsa, RSA_KEY_SIZE ) == 0 );
  954. /* Extract key to the raw rsa context */
  955. TEST_ASSERT( mbedtls_rsa_copy( &raw, mbedtls_pk_rsa( rsa ) ) == 0 );
  956. /* Initialize PK RSA_ALT context */
  957. TEST_ASSERT( mbedtls_pk_setup_rsa_alt( &alt, (void *) &raw,
  958. mbedtls_rsa_decrypt_func, mbedtls_rsa_sign_func, mbedtls_rsa_key_len_func ) == 0 );
  959. /* Test administrative functions */
  960. TEST_ASSERT( mbedtls_pk_can_do( &alt, MBEDTLS_PK_RSA ) );
  961. TEST_ASSERT( mbedtls_pk_get_bitlen( &alt ) == RSA_KEY_SIZE );
  962. TEST_ASSERT( mbedtls_pk_get_len( &alt ) == RSA_KEY_LEN );
  963. TEST_ASSERT( mbedtls_pk_get_type( &alt ) == MBEDTLS_PK_RSA_ALT );
  964. TEST_ASSERT( strcmp( mbedtls_pk_get_name( &alt ), "RSA-alt" ) == 0 );
  965. /* Test signature */
  966. #if SIZE_MAX > UINT_MAX
  967. TEST_ASSERT( mbedtls_pk_sign( &alt, MBEDTLS_MD_NONE, hash, SIZE_MAX, sig,
  968. &sig_len, mbedtls_test_rnd_std_rand, NULL )
  969. == MBEDTLS_ERR_PK_BAD_INPUT_DATA );
  970. #endif /* SIZE_MAX > UINT_MAX */
  971. TEST_ASSERT( mbedtls_pk_sign( &alt, MBEDTLS_MD_NONE, hash, sizeof hash, sig,
  972. &sig_len, mbedtls_test_rnd_std_rand, NULL )
  973. == 0 );
  974. TEST_ASSERT( sig_len == RSA_KEY_LEN );
  975. TEST_ASSERT( mbedtls_pk_verify( &rsa, MBEDTLS_MD_NONE,
  976. hash, sizeof hash, sig, sig_len ) == 0 );
  977. /* Test decrypt */
  978. TEST_ASSERT( mbedtls_pk_encrypt( &rsa, msg, sizeof msg,
  979. ciph, &ciph_len, sizeof ciph,
  980. mbedtls_test_rnd_std_rand, NULL ) == 0 );
  981. TEST_ASSERT( mbedtls_pk_decrypt( &alt, ciph, ciph_len,
  982. test, &test_len, sizeof test,
  983. mbedtls_test_rnd_std_rand, NULL ) == 0 );
  984. TEST_ASSERT( test_len == sizeof msg );
  985. TEST_ASSERT( memcmp( test, msg, test_len ) == 0 );
  986. /* Test forbidden operations */
  987. TEST_ASSERT( mbedtls_pk_encrypt( &alt, msg, sizeof msg,
  988. ciph, &ciph_len, sizeof ciph,
  989. mbedtls_test_rnd_std_rand, NULL ) == ret );
  990. TEST_ASSERT( mbedtls_pk_verify( &alt, MBEDTLS_MD_NONE,
  991. hash, sizeof hash, sig, sig_len ) == ret );
  992. TEST_ASSERT( mbedtls_pk_debug( &alt, dbg_items ) == ret );
  993. exit:
  994. mbedtls_rsa_free( &raw );
  995. mbedtls_pk_free( &rsa ); mbedtls_pk_free( &alt );
  996. }
  997. /* END_CASE */
  998. /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C:MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_ECDSA_C */
  999. void pk_psa_sign( int grpid_arg,
  1000. int psa_curve_arg, int expected_bits_arg )
  1001. {
  1002. mbedtls_ecp_group_id grpid = grpid_arg;
  1003. mbedtls_pk_context pk;
  1004. unsigned char hash[32];
  1005. unsigned char sig[MBEDTLS_ECDSA_MAX_LEN];
  1006. unsigned char pkey_legacy[200];
  1007. unsigned char pkey_psa[200];
  1008. unsigned char *pkey_legacy_start, *pkey_psa_start;
  1009. size_t sig_len, klen_legacy, klen_psa;
  1010. int ret;
  1011. mbedtls_svc_key_id_t key_id;
  1012. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  1013. psa_key_type_t expected_type = PSA_KEY_TYPE_ECC_KEY_PAIR( psa_curve_arg );
  1014. size_t expected_bits = expected_bits_arg;
  1015. /*
  1016. * This tests making signatures with a wrapped PSA key:
  1017. * - generate a fresh ECP legacy PK context
  1018. * - wrap it in a PK context and make a signature this way
  1019. * - extract the public key
  1020. * - parse it to a PK context and verify the signature this way
  1021. */
  1022. PSA_ASSERT( psa_crypto_init( ) );
  1023. /* Create legacy EC public/private key in PK context. */
  1024. mbedtls_pk_init( &pk );
  1025. TEST_ASSERT( mbedtls_pk_setup( &pk,
  1026. mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY ) ) == 0 );
  1027. TEST_ASSERT( mbedtls_ecp_gen_key( grpid,
  1028. (mbedtls_ecp_keypair*) pk.pk_ctx,
  1029. mbedtls_test_rnd_std_rand, NULL ) == 0 );
  1030. /* Export underlying public key for re-importing in a legacy context. */
  1031. ret = mbedtls_pk_write_pubkey_der( &pk, pkey_legacy,
  1032. sizeof( pkey_legacy ) );
  1033. TEST_ASSERT( ret >= 0 );
  1034. klen_legacy = (size_t) ret;
  1035. /* mbedtls_pk_write_pubkey_der() writes backwards in the data buffer. */
  1036. pkey_legacy_start = pkey_legacy + sizeof( pkey_legacy ) - klen_legacy;
  1037. /* Turn PK context into an opaque one. */
  1038. TEST_ASSERT( mbedtls_pk_wrap_as_opaque( &pk, &key_id,
  1039. PSA_ALG_SHA_256 ) == 0 );
  1040. PSA_ASSERT( psa_get_key_attributes( key_id, &attributes ) );
  1041. TEST_EQUAL( psa_get_key_type( &attributes ), expected_type );
  1042. TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
  1043. TEST_EQUAL( psa_get_key_lifetime( &attributes ),
  1044. PSA_KEY_LIFETIME_VOLATILE );
  1045. memset( hash, 0x2a, sizeof hash );
  1046. memset( sig, 0, sizeof sig );
  1047. TEST_ASSERT( mbedtls_pk_sign( &pk, MBEDTLS_MD_SHA256,
  1048. hash, sizeof hash, sig, &sig_len,
  1049. NULL, NULL ) == 0 );
  1050. /* Export underlying public key for re-importing in a psa context. */
  1051. ret = mbedtls_pk_write_pubkey_der( &pk, pkey_psa,
  1052. sizeof( pkey_psa ) );
  1053. TEST_ASSERT( ret >= 0 );
  1054. klen_psa = (size_t) ret;
  1055. /* mbedtls_pk_write_pubkey_der() writes backwards in the data buffer. */
  1056. pkey_psa_start = pkey_psa + sizeof( pkey_psa ) - klen_psa;
  1057. TEST_ASSERT( klen_psa == klen_legacy );
  1058. TEST_ASSERT( memcmp( pkey_psa_start, pkey_legacy_start, klen_psa ) == 0 );
  1059. mbedtls_pk_free( &pk );
  1060. TEST_ASSERT( PSA_SUCCESS == psa_destroy_key( key_id ) );
  1061. mbedtls_pk_init( &pk );
  1062. TEST_ASSERT( mbedtls_pk_parse_public_key( &pk, pkey_legacy_start,
  1063. klen_legacy ) == 0 );
  1064. TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256,
  1065. hash, sizeof hash, sig, sig_len ) == 0 );
  1066. exit:
  1067. /*
  1068. * Key attributes may have been returned by psa_get_key_attributes()
  1069. * thus reset them as required.
  1070. */
  1071. psa_reset_key_attributes( &attributes );
  1072. mbedtls_pk_free( &pk );
  1073. USE_PSA_DONE( );
  1074. }
  1075. /* END_CASE */