evp_pkey_provided_test.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781
  1. /*
  2. * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h> /* memset */
  10. #include <openssl/evp.h>
  11. #include <openssl/pem.h>
  12. #include <openssl/encoder.h>
  13. #include <openssl/provider.h>
  14. #include <openssl/param_build.h>
  15. #include <openssl/core_names.h>
  16. #include <openssl/sha.h>
  17. #include "crypto/ecx.h"
  18. #include "crypto/evp.h" /* For the internal API */
  19. #include "crypto/bn_dh.h" /* _bignum_ffdhe2048_p */
  20. #include "internal/nelem.h"
  21. #include "testutil.h"
  22. static char *datadir = NULL;
  23. /*
  24. * Do not change the order of the following defines unless you also
  25. * update the for loop bounds used inside test_print_key_using_encoder() and
  26. * test_print_key_using_encoder_public().
  27. */
  28. #define PRIV_TEXT 0
  29. #define PRIV_PEM 1
  30. #define PRIV_DER 2
  31. #define PUB_TEXT 3
  32. #define PUB_PEM 4
  33. #define PUB_DER 5
  34. static void stripcr(char *buf, size_t *len)
  35. {
  36. size_t i;
  37. char *curr, *writ;
  38. for (i = *len, curr = buf, writ = buf; i > 0; i--, curr++) {
  39. if (*curr == '\r') {
  40. (*len)--;
  41. continue;
  42. }
  43. if (curr != writ)
  44. *writ = *curr;
  45. writ++;
  46. }
  47. }
  48. static int compare_with_file(const char *alg, int type, BIO *membio)
  49. {
  50. char filename[80];
  51. BIO *file = NULL;
  52. char buf[4096];
  53. char *memdata, *fullfile = NULL;
  54. const char *suffix;
  55. size_t readbytes;
  56. int ret = 0;
  57. int len;
  58. size_t slen;
  59. switch (type) {
  60. case PRIV_TEXT:
  61. suffix = "priv.txt";
  62. break;
  63. case PRIV_PEM:
  64. suffix = "priv.pem";
  65. break;
  66. case PRIV_DER:
  67. suffix = "priv.der";
  68. break;
  69. case PUB_TEXT:
  70. suffix = "pub.txt";
  71. break;
  72. case PUB_PEM:
  73. suffix = "pub.pem";
  74. break;
  75. case PUB_DER:
  76. suffix = "pub.der";
  77. break;
  78. default:
  79. TEST_error("Invalid file type");
  80. goto err;
  81. }
  82. BIO_snprintf(filename, sizeof(filename), "%s.%s", alg, suffix);
  83. fullfile = test_mk_file_path(datadir, filename);
  84. if (!TEST_ptr(fullfile))
  85. goto err;
  86. file = BIO_new_file(fullfile, "rb");
  87. if (!TEST_ptr(file))
  88. goto err;
  89. if (!TEST_true(BIO_read_ex(file, buf, sizeof(buf), &readbytes))
  90. || !TEST_true(BIO_eof(file))
  91. || !TEST_size_t_lt(readbytes, sizeof(buf)))
  92. goto err;
  93. len = BIO_get_mem_data(membio, &memdata);
  94. if (!TEST_int_gt(len, 0))
  95. goto err;
  96. slen = len;
  97. if (type != PRIV_DER && type != PUB_DER) {
  98. stripcr(memdata, &slen);
  99. stripcr(buf, &readbytes);
  100. }
  101. if (!TEST_mem_eq(memdata, slen, buf, readbytes))
  102. goto err;
  103. ret = 1;
  104. err:
  105. OPENSSL_free(fullfile);
  106. (void)BIO_reset(membio);
  107. BIO_free(file);
  108. return ret;
  109. }
  110. static int pass_cb(char *buf, int size, int rwflag, void *u)
  111. {
  112. return 0;
  113. }
  114. static int pass_cb_error(char *buf, int size, int rwflag, void *u)
  115. {
  116. return -1;
  117. }
  118. static int test_print_key_using_pem(const char *alg, const EVP_PKEY *pk)
  119. {
  120. BIO *membio = BIO_new(BIO_s_mem());
  121. int ret = 0;
  122. if (!TEST_ptr(membio))
  123. goto err;
  124. if (/* Output Encrypted private key in PEM form */
  125. !TEST_true(PEM_write_bio_PrivateKey(bio_out, pk, EVP_aes_256_cbc(),
  126. (unsigned char *)"pass", 4,
  127. NULL, NULL))
  128. /* Output zero-length passphrase encrypted private key in PEM form */
  129. || !TEST_true(PEM_write_bio_PKCS8PrivateKey(bio_out, pk,
  130. EVP_aes_256_cbc(),
  131. (const char *)~0, 0,
  132. NULL, NULL))
  133. || !TEST_true(PEM_write_bio_PKCS8PrivateKey(bio_out, pk,
  134. EVP_aes_256_cbc(),
  135. NULL, 0, NULL, ""))
  136. || !TEST_true(PEM_write_bio_PKCS8PrivateKey(bio_out, pk,
  137. EVP_aes_256_cbc(),
  138. NULL, 0, pass_cb, NULL))
  139. || !TEST_false(PEM_write_bio_PKCS8PrivateKey(bio_out, pk,
  140. EVP_aes_256_cbc(),
  141. NULL, 0, pass_cb_error,
  142. NULL))
  143. #ifndef OPENSSL_NO_DES
  144. || !TEST_true(PEM_write_bio_PKCS8PrivateKey_nid(
  145. bio_out, pk, NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  146. (const char *)~0, 0, NULL, NULL))
  147. || !TEST_true(PEM_write_bio_PKCS8PrivateKey_nid(
  148. bio_out, pk, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, NULL, 0,
  149. NULL, ""))
  150. || !TEST_true(PEM_write_bio_PKCS8PrivateKey_nid(
  151. bio_out, pk, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, NULL, 0,
  152. pass_cb, NULL))
  153. || !TEST_false(PEM_write_bio_PKCS8PrivateKey_nid(
  154. bio_out, pk, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, NULL, 0,
  155. pass_cb_error, NULL))
  156. #endif
  157. /* Private key in text form */
  158. || !TEST_int_gt(EVP_PKEY_print_private(membio, pk, 0, NULL), 0)
  159. || !TEST_true(compare_with_file(alg, PRIV_TEXT, membio))
  160. /* Public key in PEM form */
  161. || !TEST_true(PEM_write_bio_PUBKEY(membio, pk))
  162. || !TEST_true(compare_with_file(alg, PUB_PEM, membio))
  163. /* Unencrypted private key in PEM form */
  164. || !TEST_true(PEM_write_bio_PrivateKey(membio, pk,
  165. NULL, NULL, 0, NULL, NULL))
  166. || !TEST_true(compare_with_file(alg, PRIV_PEM, membio))
  167. /* NULL key */
  168. || !TEST_false(PEM_write_bio_PrivateKey(membio, NULL,
  169. NULL, NULL, 0, NULL, NULL))
  170. || !TEST_false(PEM_write_bio_PrivateKey_traditional(membio, NULL,
  171. NULL, NULL, 0, NULL, NULL)))
  172. goto err;
  173. ret = 1;
  174. err:
  175. BIO_free(membio);
  176. return ret;
  177. }
  178. static int test_print_key_type_using_encoder(const char *alg, int type,
  179. const EVP_PKEY *pk)
  180. {
  181. const char *output_type, *output_structure;
  182. int selection;
  183. OSSL_ENCODER_CTX *ctx = NULL;
  184. BIO *membio = BIO_new(BIO_s_mem());
  185. int ret = 0;
  186. switch (type) {
  187. case PRIV_TEXT:
  188. output_type = "TEXT";
  189. output_structure = NULL;
  190. selection = OSSL_KEYMGMT_SELECT_KEYPAIR
  191. | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
  192. break;
  193. case PRIV_PEM:
  194. output_type = "PEM";
  195. output_structure = "PrivateKeyInfo";
  196. selection = OSSL_KEYMGMT_SELECT_KEYPAIR
  197. | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
  198. break;
  199. case PRIV_DER:
  200. output_type = "DER";
  201. output_structure = "PrivateKeyInfo";
  202. selection = OSSL_KEYMGMT_SELECT_KEYPAIR
  203. | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
  204. break;
  205. case PUB_TEXT:
  206. output_type = "TEXT";
  207. output_structure = NULL;
  208. selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY
  209. | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
  210. break;
  211. case PUB_PEM:
  212. output_type = "PEM";
  213. output_structure = "SubjectPublicKeyInfo";
  214. selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY
  215. | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
  216. break;
  217. case PUB_DER:
  218. output_type = "DER";
  219. output_structure = "SubjectPublicKeyInfo";
  220. selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY
  221. | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
  222. break;
  223. default:
  224. TEST_error("Invalid encoding type");
  225. goto err;
  226. }
  227. if (!TEST_ptr(membio))
  228. goto err;
  229. /* Make a context, it's valid for several prints */
  230. TEST_note("Setting up a OSSL_ENCODER context with passphrase");
  231. if (!TEST_ptr(ctx = OSSL_ENCODER_CTX_new_for_pkey(pk, selection,
  232. output_type,
  233. output_structure,
  234. NULL))
  235. /* Check that this operation is supported */
  236. || !TEST_int_ne(OSSL_ENCODER_CTX_get_num_encoders(ctx), 0))
  237. goto err;
  238. /* Use no cipher. This should give us an unencrypted PEM */
  239. TEST_note("Testing with no encryption");
  240. if (!TEST_true(OSSL_ENCODER_to_bio(ctx, membio))
  241. || !TEST_true(compare_with_file(alg, type, membio)))
  242. goto err;
  243. if (type == PRIV_PEM) {
  244. /* Set a passphrase to be used later */
  245. if (!TEST_true(OSSL_ENCODER_CTX_set_passphrase(ctx,
  246. (unsigned char *)"pass",
  247. 4)))
  248. goto err;
  249. /* Use a valid cipher name */
  250. TEST_note("Displaying PEM encrypted with AES-256-CBC");
  251. if (!TEST_true(OSSL_ENCODER_CTX_set_cipher(ctx, "AES-256-CBC", NULL))
  252. || !TEST_true(OSSL_ENCODER_to_bio(ctx, bio_out)))
  253. goto err;
  254. /* Use an invalid cipher name, which should generate no output */
  255. TEST_note("NOT Displaying PEM encrypted with (invalid) FOO");
  256. if (!TEST_false(OSSL_ENCODER_CTX_set_cipher(ctx, "FOO", NULL))
  257. || !TEST_false(OSSL_ENCODER_to_bio(ctx, bio_out)))
  258. goto err;
  259. /* Clear the cipher. This should give us an unencrypted PEM again */
  260. TEST_note("Testing with encryption cleared (no encryption)");
  261. if (!TEST_true(OSSL_ENCODER_CTX_set_cipher(ctx, NULL, NULL))
  262. || !TEST_true(OSSL_ENCODER_to_bio(ctx, membio))
  263. || !TEST_true(compare_with_file(alg, type, membio)))
  264. goto err;
  265. }
  266. ret = 1;
  267. err:
  268. BIO_free(membio);
  269. OSSL_ENCODER_CTX_free(ctx);
  270. return ret;
  271. }
  272. static int test_print_key_using_encoder(const char *alg, const EVP_PKEY *pk)
  273. {
  274. int i;
  275. int ret = 1;
  276. for (i = PRIV_TEXT; i <= PUB_DER; i++)
  277. ret = ret && test_print_key_type_using_encoder(alg, i, pk);
  278. return ret;
  279. }
  280. #ifndef OPENSSL_NO_EC
  281. static int test_print_key_using_encoder_public(const char *alg,
  282. const EVP_PKEY *pk)
  283. {
  284. int i;
  285. int ret = 1;
  286. for (i = PUB_TEXT; i <= PUB_DER; i++)
  287. ret = ret && test_print_key_type_using_encoder(alg, i, pk);
  288. return ret;
  289. }
  290. #endif
  291. /* Array indexes used in test_fromdata_rsa */
  292. #define N 0
  293. #define E 1
  294. #define D 2
  295. #define P 3
  296. #define Q 4
  297. #define DP 5
  298. #define DQ 6
  299. #define QINV 7
  300. static int test_fromdata_rsa(void)
  301. {
  302. int ret = 0, i;
  303. EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
  304. EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL;
  305. /*
  306. * 32-bit RSA key, extracted from this command,
  307. * executed with OpenSSL 1.0.2:
  308. *
  309. * openssl genrsa 32 | openssl rsa -text
  310. */
  311. static unsigned long key_numbers[] = {
  312. 0xbc747fc5, /* N */
  313. 0x10001, /* E */
  314. 0x7b133399, /* D */
  315. 0xe963, /* P */
  316. 0xceb7, /* Q */
  317. 0x8599, /* DP */
  318. 0xbd87, /* DQ */
  319. 0xcc3b, /* QINV */
  320. };
  321. OSSL_PARAM fromdata_params[] = {
  322. OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_N, &key_numbers[N]),
  323. OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_E, &key_numbers[E]),
  324. OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_D, &key_numbers[D]),
  325. OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_FACTOR1, &key_numbers[P]),
  326. OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_FACTOR2, &key_numbers[Q]),
  327. OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_EXPONENT1, &key_numbers[DP]),
  328. OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_EXPONENT2, &key_numbers[DQ]),
  329. OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, &key_numbers[QINV]),
  330. OSSL_PARAM_END
  331. };
  332. BIGNUM *bn = BN_new();
  333. BIGNUM *bn_from = BN_new();
  334. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL)))
  335. goto err;
  336. if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
  337. || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR,
  338. fromdata_params), 1))
  339. goto err;
  340. while (dup_pk == NULL) {
  341. ret = 0;
  342. if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 32)
  343. || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 8)
  344. || !TEST_int_eq(EVP_PKEY_get_size(pk), 4)
  345. || !TEST_false(EVP_PKEY_missing_parameters(pk)))
  346. goto err;
  347. EVP_PKEY_CTX_free(key_ctx);
  348. if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, "")))
  349. goto err;
  350. if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0)
  351. || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0)
  352. || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0)
  353. || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0))
  354. goto err;
  355. /* EVP_PKEY_copy_parameters() should fail for RSA */
  356. if (!TEST_ptr(copy_pk = EVP_PKEY_new())
  357. || !TEST_false(EVP_PKEY_copy_parameters(copy_pk, pk)))
  358. goto err;
  359. EVP_PKEY_free(copy_pk);
  360. copy_pk = NULL;
  361. ret = test_print_key_using_pem("RSA", pk)
  362. && test_print_key_using_encoder("RSA", pk);
  363. if (!ret || !TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
  364. goto err;
  365. ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
  366. EVP_PKEY_free(pk);
  367. pk = dup_pk;
  368. if (!ret)
  369. goto err;
  370. }
  371. err:
  372. /* for better diagnostics always compare key params */
  373. for (i = 0; fromdata_params[i].key != NULL; ++i) {
  374. if (!TEST_true(BN_set_word(bn_from, key_numbers[i]))
  375. || !TEST_true(EVP_PKEY_get_bn_param(pk, fromdata_params[i].key, &bn))
  376. || !TEST_BN_eq(bn, bn_from))
  377. ret = 0;
  378. }
  379. BN_free(bn_from);
  380. BN_free(bn);
  381. EVP_PKEY_free(pk);
  382. EVP_PKEY_free(copy_pk);
  383. EVP_PKEY_CTX_free(key_ctx);
  384. EVP_PKEY_CTX_free(ctx);
  385. return ret;
  386. }
  387. static int test_evp_pkey_get_bn_param_large(void)
  388. {
  389. int ret = 0;
  390. EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
  391. EVP_PKEY *pk = NULL;
  392. OSSL_PARAM_BLD *bld = NULL;
  393. OSSL_PARAM *fromdata_params = NULL;
  394. BIGNUM *n = NULL, *e = NULL, *d = NULL, *n_out = NULL;
  395. /*
  396. * The buffer size chosen here for n_data larger than the buffer used
  397. * internally in EVP_PKEY_get_bn_param.
  398. */
  399. static unsigned char n_data[2050];
  400. static const unsigned char e_data[] = {
  401. 0x1, 0x00, 0x01
  402. };
  403. static const unsigned char d_data[]= {
  404. 0x99, 0x33, 0x13, 0x7b
  405. };
  406. /* N is a large buffer */
  407. memset(n_data, 0xCE, sizeof(n_data));
  408. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  409. || !TEST_ptr(n = BN_bin2bn(n_data, sizeof(n_data), NULL))
  410. || !TEST_ptr(e = BN_bin2bn(e_data, sizeof(e_data), NULL))
  411. || !TEST_ptr(d = BN_bin2bn(d_data, sizeof(d_data), NULL))
  412. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n))
  413. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e))
  414. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d))
  415. || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld))
  416. || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL))
  417. || !TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
  418. || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR,
  419. fromdata_params), 1)
  420. || !TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, ""))
  421. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_RSA_N, &n_out))
  422. || !TEST_BN_eq(n, n_out))
  423. goto err;
  424. ret = 1;
  425. err:
  426. BN_free(n_out);
  427. BN_free(n);
  428. BN_free(e);
  429. BN_free(d);
  430. EVP_PKEY_free(pk);
  431. EVP_PKEY_CTX_free(key_ctx);
  432. EVP_PKEY_CTX_free(ctx);
  433. OSSL_PARAM_free(fromdata_params);
  434. OSSL_PARAM_BLD_free(bld);
  435. return ret;
  436. }
  437. #ifndef OPENSSL_NO_DH
  438. static int test_fromdata_dh_named_group(void)
  439. {
  440. int ret = 0;
  441. int gindex = 0, pcounter = 0, hindex = 0;
  442. EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
  443. EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL;
  444. size_t len;
  445. BIGNUM *pub = NULL, *priv = NULL;
  446. BIGNUM *pub_out = NULL, *priv_out = NULL;
  447. BIGNUM *p = NULL, *q = NULL, *g = NULL, *j = NULL;
  448. OSSL_PARAM *fromdata_params = NULL;
  449. OSSL_PARAM_BLD *bld = NULL;
  450. char name_out[80];
  451. unsigned char seed_out[32];
  452. /*
  453. * DH key data was generated using the following:
  454. * openssl genpkey -algorithm DH -pkeyopt group:ffdhe2048
  455. * -pkeyopt priv_len:224 -text
  456. */
  457. static const unsigned char priv_data[] = {
  458. 0x88, 0x85, 0xe7, 0x9f, 0xee, 0x6d, 0xc5, 0x7c, 0x78, 0xaf, 0x63, 0x5d,
  459. 0x38, 0x2a, 0xd0, 0xed, 0x56, 0x4b, 0x47, 0x21, 0x2b, 0xfa, 0x55, 0xfa,
  460. 0x87, 0xe8, 0xa9, 0x7b,
  461. };
  462. static const unsigned char pub_data[] = {
  463. 0x00, 0xd6, 0x2d, 0x77, 0xe0, 0xd3, 0x7d, 0xf8, 0xeb, 0x98, 0x50, 0xa1,
  464. 0x82, 0x22, 0x65, 0xd5, 0xd9, 0xfe, 0xc9, 0x3f, 0xbe, 0x16, 0x83, 0xbd,
  465. 0x33, 0xe9, 0xc6, 0x93, 0xcf, 0x08, 0xaf, 0x83, 0xfa, 0x80, 0x8a, 0x6c,
  466. 0x64, 0xdf, 0x70, 0x64, 0xd5, 0x0a, 0x7c, 0x5a, 0x72, 0xda, 0x66, 0xe6,
  467. 0xf9, 0xf5, 0x31, 0x21, 0x92, 0xb0, 0x60, 0x1a, 0xb5, 0xd3, 0xf0, 0xa5,
  468. 0xfa, 0x48, 0x95, 0x2e, 0x38, 0xd9, 0xc5, 0xe6, 0xda, 0xfb, 0x6c, 0x03,
  469. 0x9d, 0x4b, 0x69, 0xb7, 0x95, 0xe4, 0x5c, 0xc0, 0x93, 0x4f, 0x48, 0xd9,
  470. 0x7e, 0x06, 0x22, 0xb2, 0xde, 0xf3, 0x79, 0x24, 0xed, 0xe1, 0xd1, 0x4a,
  471. 0x57, 0xf1, 0x40, 0x86, 0x70, 0x42, 0x25, 0xc5, 0x27, 0x68, 0xc9, 0xfa,
  472. 0xe5, 0x8e, 0x62, 0x7e, 0xff, 0x49, 0x6c, 0x5b, 0xb5, 0xba, 0xf9, 0xef,
  473. 0x9a, 0x1a, 0x10, 0xd4, 0x81, 0x53, 0xcf, 0x83, 0x04, 0x18, 0x1c, 0xe1,
  474. 0xdb, 0xe1, 0x65, 0xa9, 0x7f, 0xe1, 0x33, 0xeb, 0xc3, 0x4f, 0xe3, 0xb7,
  475. 0x22, 0xf7, 0x1c, 0x09, 0x4f, 0xed, 0xc6, 0x07, 0x8e, 0x78, 0x05, 0x8f,
  476. 0x7c, 0x96, 0xd9, 0x12, 0xe0, 0x81, 0x74, 0x1a, 0xe9, 0x13, 0xc0, 0x20,
  477. 0x82, 0x65, 0xbb, 0x42, 0x3b, 0xed, 0x08, 0x6a, 0x84, 0x4f, 0xea, 0x77,
  478. 0x14, 0x32, 0xf9, 0xed, 0xc2, 0x12, 0xd6, 0xc5, 0xc6, 0xb3, 0xe5, 0xf2,
  479. 0x6e, 0xf6, 0x16, 0x7f, 0x37, 0xde, 0xbc, 0x09, 0xc7, 0x06, 0x6b, 0x12,
  480. 0xbc, 0xad, 0x2d, 0x49, 0x25, 0xd5, 0xdc, 0xf4, 0x18, 0x14, 0xd2, 0xf0,
  481. 0xf1, 0x1d, 0x1f, 0x3a, 0xaa, 0x15, 0x55, 0xbb, 0x0d, 0x7f, 0xbe, 0x67,
  482. 0xa1, 0xa7, 0xf0, 0xaa, 0xb3, 0xfb, 0x41, 0x82, 0x39, 0x49, 0x93, 0xbc,
  483. 0xa8, 0xee, 0x72, 0x13, 0x45, 0x65, 0x15, 0x42, 0x17, 0xaa, 0xd8, 0xab,
  484. 0xcf, 0x33, 0x42, 0x83, 0x42
  485. };
  486. static const char group_name[] = "ffdhe2048";
  487. static const long priv_len = 224;
  488. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  489. || !TEST_ptr(pub = BN_bin2bn(pub_data, sizeof(pub_data), NULL))
  490. || !TEST_ptr(priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL))
  491. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
  492. OSSL_PKEY_PARAM_GROUP_NAME,
  493. group_name, 0))
  494. || !TEST_true(OSSL_PARAM_BLD_push_long(bld, OSSL_PKEY_PARAM_DH_PRIV_LEN,
  495. priv_len))
  496. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, pub))
  497. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv))
  498. || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)))
  499. goto err;
  500. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL)))
  501. goto err;
  502. if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
  503. || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR,
  504. fromdata_params), 1))
  505. goto err;
  506. /*
  507. * A few extra checks of EVP_PKEY_get_utf8_string_param() to see that
  508. * it behaves as expected with regards to string length and terminating
  509. * NUL byte.
  510. */
  511. if (!TEST_true(EVP_PKEY_get_utf8_string_param(pk,
  512. OSSL_PKEY_PARAM_GROUP_NAME,
  513. NULL, sizeof(name_out),
  514. &len))
  515. || !TEST_size_t_eq(len, sizeof(group_name) - 1)
  516. /* Just enough space to hold the group name and a terminating NUL */
  517. || !TEST_true(EVP_PKEY_get_utf8_string_param(pk,
  518. OSSL_PKEY_PARAM_GROUP_NAME,
  519. name_out,
  520. sizeof(group_name),
  521. &len))
  522. || !TEST_size_t_eq(len, sizeof(group_name) - 1)
  523. /* Too small buffer to hold the terminating NUL byte */
  524. || !TEST_false(EVP_PKEY_get_utf8_string_param(pk,
  525. OSSL_PKEY_PARAM_GROUP_NAME,
  526. name_out,
  527. sizeof(group_name) - 1,
  528. &len))
  529. /* Too small buffer to hold the whole group name, even! */
  530. || !TEST_false(EVP_PKEY_get_utf8_string_param(pk,
  531. OSSL_PKEY_PARAM_GROUP_NAME,
  532. name_out,
  533. sizeof(group_name) - 2,
  534. &len)))
  535. goto err;
  536. while (dup_pk == NULL) {
  537. ret = 0;
  538. if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 2048)
  539. || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 112)
  540. || !TEST_int_eq(EVP_PKEY_get_size(pk), 256)
  541. || !TEST_false(EVP_PKEY_missing_parameters(pk)))
  542. goto err;
  543. if (!TEST_true(EVP_PKEY_get_utf8_string_param(pk,
  544. OSSL_PKEY_PARAM_GROUP_NAME,
  545. name_out,
  546. sizeof(name_out),
  547. &len))
  548. || !TEST_str_eq(name_out, group_name)
  549. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PUB_KEY,
  550. &pub_out))
  551. || !TEST_BN_eq(pub, pub_out)
  552. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY,
  553. &priv_out))
  554. || !TEST_BN_eq(priv, priv_out)
  555. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_P, &p))
  556. || !TEST_BN_eq(&ossl_bignum_ffdhe2048_p, p)
  557. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_Q, &q))
  558. || !TEST_ptr(q)
  559. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_G, &g))
  560. || !TEST_BN_eq(&ossl_bignum_const_2, g)
  561. || !TEST_false(EVP_PKEY_get_bn_param(pk,
  562. OSSL_PKEY_PARAM_FFC_COFACTOR,
  563. &j))
  564. || !TEST_ptr_null(j)
  565. || !TEST_false(EVP_PKEY_get_octet_string_param(pk,
  566. OSSL_PKEY_PARAM_FFC_SEED,
  567. seed_out,
  568. sizeof(seed_out),
  569. &len))
  570. || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_GINDEX,
  571. &gindex))
  572. || !TEST_int_eq(gindex, -1)
  573. || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_H,
  574. &hindex))
  575. || !TEST_int_eq(hindex, 0)
  576. || !TEST_true(EVP_PKEY_get_int_param(pk,
  577. OSSL_PKEY_PARAM_FFC_PCOUNTER,
  578. &pcounter))
  579. || !TEST_int_eq(pcounter, -1))
  580. goto err;
  581. BN_free(p);
  582. p = NULL;
  583. BN_free(q);
  584. q = NULL;
  585. BN_free(g);
  586. g = NULL;
  587. BN_free(j);
  588. j = NULL;
  589. BN_free(pub_out);
  590. pub_out = NULL;
  591. BN_free(priv_out);
  592. priv_out = NULL;
  593. if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, "")))
  594. goto err;
  595. if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0)
  596. || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0)
  597. || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0)
  598. || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0))
  599. goto err;
  600. EVP_PKEY_CTX_free(key_ctx);
  601. key_ctx = NULL;
  602. if (!TEST_ptr(copy_pk = EVP_PKEY_new())
  603. || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk)))
  604. goto err;
  605. EVP_PKEY_free(copy_pk);
  606. copy_pk = NULL;
  607. ret = test_print_key_using_pem("DH", pk)
  608. && test_print_key_using_encoder("DH", pk);
  609. if (!ret || !TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
  610. goto err;
  611. ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
  612. EVP_PKEY_free(pk);
  613. pk = dup_pk;
  614. if (!ret)
  615. goto err;
  616. }
  617. err:
  618. BN_free(p);
  619. BN_free(q);
  620. BN_free(g);
  621. BN_free(j);
  622. BN_free(pub);
  623. BN_free(priv);
  624. BN_free(pub_out);
  625. BN_free(priv_out);
  626. EVP_PKEY_free(copy_pk);
  627. EVP_PKEY_free(pk);
  628. EVP_PKEY_CTX_free(ctx);
  629. EVP_PKEY_CTX_free(key_ctx);
  630. OSSL_PARAM_free(fromdata_params);
  631. OSSL_PARAM_BLD_free(bld);
  632. return ret;
  633. }
  634. static int test_fromdata_dh_fips186_4(void)
  635. {
  636. int ret = 0;
  637. int gindex = 0, pcounter = 0, hindex = 0;
  638. EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
  639. EVP_PKEY *pk = NULL, *dup_pk = NULL;
  640. size_t len;
  641. BIGNUM *pub = NULL, *priv = NULL;
  642. BIGNUM *pub_out = NULL, *priv_out = NULL;
  643. BIGNUM *p = NULL, *q = NULL, *g = NULL, *j = NULL;
  644. OSSL_PARAM_BLD *bld = NULL;
  645. OSSL_PARAM *fromdata_params = NULL;
  646. char name_out[80];
  647. unsigned char seed_out[32];
  648. /*
  649. * DH key data was generated using the following:
  650. * openssl genpkey -algorithm DH
  651. * -pkeyopt group:ffdhe2048 -pkeyopt priv_len:224 -text
  652. */
  653. static const unsigned char priv_data[] = {
  654. 0x88, 0x85, 0xe7, 0x9f, 0xee, 0x6d, 0xc5, 0x7c, 0x78, 0xaf, 0x63, 0x5d,
  655. 0x38, 0x2a, 0xd0, 0xed, 0x56, 0x4b, 0x47, 0x21, 0x2b, 0xfa, 0x55, 0xfa,
  656. 0x87, 0xe8, 0xa9, 0x7b,
  657. };
  658. static const unsigned char pub_data[] = {
  659. 0xd6, 0x2d, 0x77, 0xe0, 0xd3, 0x7d, 0xf8, 0xeb, 0x98, 0x50, 0xa1, 0x82,
  660. 0x22, 0x65, 0xd5, 0xd9, 0xfe, 0xc9, 0x3f, 0xbe, 0x16, 0x83, 0xbd, 0x33,
  661. 0xe9, 0xc6, 0x93, 0xcf, 0x08, 0xaf, 0x83, 0xfa, 0x80, 0x8a, 0x6c, 0x64,
  662. 0xdf, 0x70, 0x64, 0xd5, 0x0a, 0x7c, 0x5a, 0x72, 0xda, 0x66, 0xe6, 0xf9,
  663. 0xf5, 0x31, 0x21, 0x92, 0xb0, 0x60, 0x1a, 0xb5, 0xd3, 0xf0, 0xa5, 0xfa,
  664. 0x48, 0x95, 0x2e, 0x38, 0xd9, 0xc5, 0xe6, 0xda, 0xfb, 0x6c, 0x03, 0x9d,
  665. 0x4b, 0x69, 0xb7, 0x95, 0xe4, 0x5c, 0xc0, 0x93, 0x4f, 0x48, 0xd9, 0x7e,
  666. 0x06, 0x22, 0xb2, 0xde, 0xf3, 0x79, 0x24, 0xed, 0xe1, 0xd1, 0x4a, 0x57,
  667. 0xf1, 0x40, 0x86, 0x70, 0x42, 0x25, 0xc5, 0x27, 0x68, 0xc9, 0xfa, 0xe5,
  668. 0x8e, 0x62, 0x7e, 0xff, 0x49, 0x6c, 0x5b, 0xb5, 0xba, 0xf9, 0xef, 0x9a,
  669. 0x1a, 0x10, 0xd4, 0x81, 0x53, 0xcf, 0x83, 0x04, 0x18, 0x1c, 0xe1, 0xdb,
  670. 0xe1, 0x65, 0xa9, 0x7f, 0xe1, 0x33, 0xeb, 0xc3, 0x4f, 0xe3, 0xb7, 0x22,
  671. 0xf7, 0x1c, 0x09, 0x4f, 0xed, 0xc6, 0x07, 0x8e, 0x78, 0x05, 0x8f, 0x7c,
  672. 0x96, 0xd9, 0x12, 0xe0, 0x81, 0x74, 0x1a, 0xe9, 0x13, 0xc0, 0x20, 0x82,
  673. 0x65, 0xbb, 0x42, 0x3b, 0xed, 0x08, 0x6a, 0x84, 0x4f, 0xea, 0x77, 0x14,
  674. 0x32, 0xf9, 0xed, 0xc2, 0x12, 0xd6, 0xc5, 0xc6, 0xb3, 0xe5, 0xf2, 0x6e,
  675. 0xf6, 0x16, 0x7f, 0x37, 0xde, 0xbc, 0x09, 0xc7, 0x06, 0x6b, 0x12, 0xbc,
  676. 0xad, 0x2d, 0x49, 0x25, 0xd5, 0xdc, 0xf4, 0x18, 0x14, 0xd2, 0xf0, 0xf1,
  677. 0x1d, 0x1f, 0x3a, 0xaa, 0x15, 0x55, 0xbb, 0x0d, 0x7f, 0xbe, 0x67, 0xa1,
  678. 0xa7, 0xf0, 0xaa, 0xb3, 0xfb, 0x41, 0x82, 0x39, 0x49, 0x93, 0xbc, 0xa8,
  679. 0xee, 0x72, 0x13, 0x45, 0x65, 0x15, 0x42, 0x17, 0xaa, 0xd8, 0xab, 0xcf,
  680. 0x33, 0x42, 0x83, 0x42
  681. };
  682. static const char group_name[] = "ffdhe2048";
  683. static const long priv_len = 224;
  684. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  685. || !TEST_ptr(pub = BN_bin2bn(pub_data, sizeof(pub_data), NULL))
  686. || !TEST_ptr(priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL))
  687. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
  688. OSSL_PKEY_PARAM_GROUP_NAME,
  689. group_name, 0))
  690. || !TEST_true(OSSL_PARAM_BLD_push_long(bld, OSSL_PKEY_PARAM_DH_PRIV_LEN,
  691. priv_len))
  692. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, pub))
  693. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv))
  694. || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)))
  695. goto err;
  696. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL)))
  697. goto err;
  698. if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
  699. || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR,
  700. fromdata_params), 1))
  701. goto err;
  702. while (dup_pk == NULL) {
  703. ret = 0;
  704. if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 2048)
  705. || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 112)
  706. || !TEST_int_eq(EVP_PKEY_get_size(pk), 256)
  707. || !TEST_false(EVP_PKEY_missing_parameters(pk)))
  708. goto err;
  709. if (!TEST_true(EVP_PKEY_get_utf8_string_param(pk,
  710. OSSL_PKEY_PARAM_GROUP_NAME,
  711. name_out,
  712. sizeof(name_out),
  713. &len))
  714. || !TEST_str_eq(name_out, group_name)
  715. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PUB_KEY,
  716. &pub_out))
  717. || !TEST_BN_eq(pub, pub_out)
  718. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY,
  719. &priv_out))
  720. || !TEST_BN_eq(priv, priv_out)
  721. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_P, &p))
  722. || !TEST_BN_eq(&ossl_bignum_ffdhe2048_p, p)
  723. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_Q, &q))
  724. || !TEST_ptr(q)
  725. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_G, &g))
  726. || !TEST_BN_eq(&ossl_bignum_const_2, g)
  727. || !TEST_false(EVP_PKEY_get_bn_param(pk,
  728. OSSL_PKEY_PARAM_FFC_COFACTOR,
  729. &j))
  730. || !TEST_ptr_null(j)
  731. || !TEST_false(EVP_PKEY_get_octet_string_param(pk,
  732. OSSL_PKEY_PARAM_FFC_SEED,
  733. seed_out,
  734. sizeof(seed_out),
  735. &len))
  736. || !TEST_true(EVP_PKEY_get_int_param(pk,
  737. OSSL_PKEY_PARAM_FFC_GINDEX,
  738. &gindex))
  739. || !TEST_int_eq(gindex, -1)
  740. || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_H,
  741. &hindex))
  742. || !TEST_int_eq(hindex, 0)
  743. || !TEST_true(EVP_PKEY_get_int_param(pk,
  744. OSSL_PKEY_PARAM_FFC_PCOUNTER,
  745. &pcounter))
  746. || !TEST_int_eq(pcounter, -1))
  747. goto err;
  748. BN_free(p);
  749. p = NULL;
  750. BN_free(q);
  751. q = NULL;
  752. BN_free(g);
  753. g = NULL;
  754. BN_free(j);
  755. j = NULL;
  756. BN_free(pub_out);
  757. pub_out = NULL;
  758. BN_free(priv_out);
  759. priv_out = NULL;
  760. if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, "")))
  761. goto err;
  762. if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0)
  763. || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0)
  764. || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0)
  765. || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0))
  766. goto err;
  767. EVP_PKEY_CTX_free(key_ctx);
  768. key_ctx = NULL;
  769. ret = test_print_key_using_pem("DH", pk)
  770. && test_print_key_using_encoder("DH", pk);
  771. if (!ret || !TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
  772. goto err;
  773. ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
  774. EVP_PKEY_free(pk);
  775. pk = dup_pk;
  776. if (!ret)
  777. goto err;
  778. }
  779. err:
  780. BN_free(p);
  781. BN_free(q);
  782. BN_free(g);
  783. BN_free(j);
  784. BN_free(pub);
  785. BN_free(priv);
  786. BN_free(pub_out);
  787. BN_free(priv_out);
  788. EVP_PKEY_free(pk);
  789. EVP_PKEY_CTX_free(ctx);
  790. EVP_PKEY_CTX_free(key_ctx);
  791. OSSL_PARAM_free(fromdata_params);
  792. OSSL_PARAM_BLD_free(bld);
  793. return ret;
  794. }
  795. #endif
  796. #ifndef OPENSSL_NO_EC
  797. /* Array indexes used in test_fromdata_ecx */
  798. # define PRIV_KEY 0
  799. # define PUB_KEY 1
  800. # define X25519_IDX 0
  801. # define X448_IDX 1
  802. # define ED25519_IDX 2
  803. # define ED448_IDX 3
  804. /*
  805. * tst uses indexes 0 ... (3 * 4 - 1)
  806. * For the 4 ECX key types (X25519_IDX..ED448_IDX)
  807. * 0..3 = public + private key.
  808. * 4..7 = private key (This will generate the public key from the private key)
  809. * 8..11 = public key
  810. */
  811. static int test_fromdata_ecx(int tst)
  812. {
  813. int ret = 0;
  814. EVP_PKEY_CTX *ctx = NULL, *ctx2 = NULL;
  815. EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL;
  816. const char *alg = NULL;
  817. size_t len;
  818. unsigned char out_pub[ED448_KEYLEN];
  819. unsigned char out_priv[ED448_KEYLEN];
  820. OSSL_PARAM params[3] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
  821. /* ED448_KEYLEN > X448_KEYLEN > X25519_KEYLEN == ED25519_KEYLEN */
  822. static unsigned char key_numbers[4][2][ED448_KEYLEN] = {
  823. /* X25519: Keys from RFC 7748 6.1 */
  824. {
  825. /* Private Key */
  826. {
  827. 0x77, 0x07, 0x6d, 0x0a, 0x73, 0x18, 0xa5, 0x7d, 0x3c, 0x16,
  828. 0xc1, 0x72, 0x51, 0xb2, 0x66, 0x45, 0xdf, 0x4c, 0x2f, 0x87,
  829. 0xeb, 0xc0, 0x99, 0x2a, 0xb1, 0x77, 0xfb, 0xa5, 0x1d, 0xb9,
  830. 0x2c, 0x2a
  831. },
  832. /* Public Key */
  833. {
  834. 0x85, 0x20, 0xf0, 0x09, 0x89, 0x30, 0xa7, 0x54, 0x74, 0x8b,
  835. 0x7d, 0xdc, 0xb4, 0x3e, 0xf7, 0x5a, 0x0d, 0xbf, 0x3a, 0x0d,
  836. 0x26, 0x38, 0x1a, 0xf4, 0xeb, 0xa4, 0xa9, 0x8e, 0xaa, 0x9b,
  837. 0x4e, 0x6a
  838. }
  839. },
  840. /* X448: Keys from RFC 7748 6.2 */
  841. {
  842. /* Private Key */
  843. {
  844. 0x9a, 0x8f, 0x49, 0x25, 0xd1, 0x51, 0x9f, 0x57, 0x75, 0xcf,
  845. 0x46, 0xb0, 0x4b, 0x58, 0x00, 0xd4, 0xee, 0x9e, 0xe8, 0xba,
  846. 0xe8, 0xbc, 0x55, 0x65, 0xd4, 0x98, 0xc2, 0x8d, 0xd9, 0xc9,
  847. 0xba, 0xf5, 0x74, 0xa9, 0x41, 0x97, 0x44, 0x89, 0x73, 0x91,
  848. 0x00, 0x63, 0x82, 0xa6, 0xf1, 0x27, 0xab, 0x1d, 0x9a, 0xc2,
  849. 0xd8, 0xc0, 0xa5, 0x98, 0x72, 0x6b
  850. },
  851. /* Public Key */
  852. {
  853. 0x9b, 0x08, 0xf7, 0xcc, 0x31, 0xb7, 0xe3, 0xe6, 0x7d, 0x22,
  854. 0xd5, 0xae, 0xa1, 0x21, 0x07, 0x4a, 0x27, 0x3b, 0xd2, 0xb8,
  855. 0x3d, 0xe0, 0x9c, 0x63, 0xfa, 0xa7, 0x3d, 0x2c, 0x22, 0xc5,
  856. 0xd9, 0xbb, 0xc8, 0x36, 0x64, 0x72, 0x41, 0xd9, 0x53, 0xd4,
  857. 0x0c, 0x5b, 0x12, 0xda, 0x88, 0x12, 0x0d, 0x53, 0x17, 0x7f,
  858. 0x80, 0xe5, 0x32, 0xc4, 0x1f, 0xa0
  859. }
  860. },
  861. /* ED25519: Keys from RFC 8032 */
  862. {
  863. /* Private Key */
  864. {
  865. 0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60, 0xba, 0x84,
  866. 0x4a, 0xf4, 0x92, 0xec, 0x2c, 0xc4, 0x44, 0x49, 0xc5, 0x69,
  867. 0x7b, 0x32, 0x69, 0x19, 0x70, 0x3b, 0xac, 0x03, 0x1c, 0xae,
  868. 0x7f, 0x60
  869. },
  870. /* Public Key */
  871. {
  872. 0xd7, 0x5a, 0x98, 0x01, 0x82, 0xb1, 0x0a, 0xb7, 0xd5, 0x4b,
  873. 0xfe, 0xd3, 0xc9, 0x64, 0x07, 0x3a, 0x0e, 0xe1, 0x72, 0xf3,
  874. 0xda, 0xa6, 0x23, 0x25, 0xaf, 0x02, 0x1a, 0x68, 0xf7, 0x07,
  875. 0x51, 0x1a
  876. }
  877. },
  878. /* ED448: Keys from RFC 8032 */
  879. {
  880. /* Private Key */
  881. {
  882. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10, 0xd6, 0x32,
  883. 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf, 0x6c, 0x92, 0x9f, 0x34,
  884. 0xdd, 0xfa, 0x8c, 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3,
  885. 0x48, 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  886. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f, 0x03, 0x2e,
  887. 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9, 0x5b
  888. },
  889. /* Public Key */
  890. {
  891. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7,
  892. 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  893. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9,
  894. 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  895. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa,
  896. 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61, 0x80
  897. }
  898. }
  899. };
  900. OSSL_PARAM x25519_fromdata_params[] = {
  901. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
  902. key_numbers[X25519_IDX][PRIV_KEY],
  903. X25519_KEYLEN),
  904. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
  905. key_numbers[X25519_IDX][PUB_KEY],
  906. X25519_KEYLEN),
  907. OSSL_PARAM_END
  908. };
  909. OSSL_PARAM x448_fromdata_params[] = {
  910. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
  911. key_numbers[X448_IDX][PRIV_KEY],
  912. X448_KEYLEN),
  913. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
  914. key_numbers[X448_IDX][PUB_KEY],
  915. X448_KEYLEN),
  916. OSSL_PARAM_END
  917. };
  918. OSSL_PARAM ed25519_fromdata_params[] = {
  919. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
  920. key_numbers[ED25519_IDX][PRIV_KEY],
  921. ED25519_KEYLEN),
  922. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
  923. key_numbers[ED25519_IDX][PUB_KEY],
  924. ED25519_KEYLEN),
  925. OSSL_PARAM_END
  926. };
  927. OSSL_PARAM ed448_fromdata_params[] = {
  928. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
  929. key_numbers[ED448_IDX][PRIV_KEY],
  930. ED448_KEYLEN),
  931. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
  932. key_numbers[ED448_IDX][PUB_KEY],
  933. ED448_KEYLEN),
  934. OSSL_PARAM_END
  935. };
  936. OSSL_PARAM *fromdata_params = NULL;
  937. int bits = 0, security_bits = 0, size = 0;
  938. OSSL_PARAM *orig_fromdata_params = NULL;
  939. switch (tst & 3) {
  940. case X25519_IDX:
  941. fromdata_params = x25519_fromdata_params;
  942. bits = X25519_BITS;
  943. security_bits = X25519_SECURITY_BITS;
  944. size = X25519_KEYLEN;
  945. alg = "X25519";
  946. break;
  947. case X448_IDX:
  948. fromdata_params = x448_fromdata_params;
  949. bits = X448_BITS;
  950. security_bits = X448_SECURITY_BITS;
  951. size = X448_KEYLEN;
  952. alg = "X448";
  953. break;
  954. case ED25519_IDX:
  955. fromdata_params = ed25519_fromdata_params;
  956. bits = ED25519_BITS;
  957. security_bits = ED25519_SECURITY_BITS;
  958. size = ED25519_SIGSIZE;
  959. alg = "ED25519";
  960. break;
  961. case ED448_IDX:
  962. fromdata_params = ed448_fromdata_params;
  963. bits = ED448_BITS;
  964. security_bits = ED448_SECURITY_BITS;
  965. size = ED448_SIGSIZE;
  966. alg = "ED448";
  967. break;
  968. default:
  969. goto err;
  970. }
  971. ctx = EVP_PKEY_CTX_new_from_name(NULL, alg, NULL);
  972. if (!TEST_ptr(ctx))
  973. goto err;
  974. orig_fromdata_params = fromdata_params;
  975. if (tst > 7) {
  976. /* public key only */
  977. fromdata_params++;
  978. } else if (tst > 3) {
  979. /* private key only */
  980. params[0] = fromdata_params[0];
  981. params[1] = fromdata_params[2];
  982. fromdata_params = params;
  983. }
  984. if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
  985. || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR,
  986. fromdata_params), 1))
  987. goto err;
  988. while (dup_pk == NULL) {
  989. ret = 0;
  990. if (!TEST_int_eq(EVP_PKEY_get_bits(pk), bits)
  991. || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), security_bits)
  992. || !TEST_int_eq(EVP_PKEY_get_size(pk), size)
  993. || !TEST_false(EVP_PKEY_missing_parameters(pk)))
  994. goto err;
  995. if (!TEST_ptr(ctx2 = EVP_PKEY_CTX_new_from_pkey(NULL, pk, NULL)))
  996. goto err;
  997. if (tst <= 7) {
  998. if (!TEST_int_gt(EVP_PKEY_check(ctx2), 0))
  999. goto err;
  1000. if (!TEST_true(EVP_PKEY_get_octet_string_param(
  1001. pk, orig_fromdata_params[PRIV_KEY].key,
  1002. out_priv, sizeof(out_priv), &len))
  1003. || !TEST_mem_eq(out_priv, len,
  1004. orig_fromdata_params[PRIV_KEY].data,
  1005. orig_fromdata_params[PRIV_KEY].data_size)
  1006. || !TEST_true(EVP_PKEY_get_octet_string_param(
  1007. pk, orig_fromdata_params[PUB_KEY].key,
  1008. out_pub, sizeof(out_pub), &len))
  1009. || !TEST_mem_eq(out_pub, len,
  1010. orig_fromdata_params[PUB_KEY].data,
  1011. orig_fromdata_params[PUB_KEY].data_size))
  1012. goto err;
  1013. } else {
  1014. /* The private key check should fail if there is only a public key */
  1015. if (!TEST_int_gt(EVP_PKEY_public_check(ctx2), 0)
  1016. || !TEST_int_le(EVP_PKEY_private_check(ctx2), 0)
  1017. || !TEST_int_le(EVP_PKEY_check(ctx2), 0))
  1018. goto err;
  1019. }
  1020. EVP_PKEY_CTX_free(ctx2);
  1021. ctx2 = NULL;
  1022. if (!TEST_ptr(copy_pk = EVP_PKEY_new())
  1023. /* This should succeed because there are no parameters to copy */
  1024. || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk)))
  1025. goto err;
  1026. EVP_PKEY_free(copy_pk);
  1027. copy_pk = NULL;
  1028. if (tst > 7)
  1029. ret = test_print_key_using_encoder_public(alg, pk);
  1030. else
  1031. ret = test_print_key_using_pem(alg, pk)
  1032. && test_print_key_using_encoder(alg, pk);
  1033. if (!ret || !TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
  1034. goto err;
  1035. ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
  1036. EVP_PKEY_free(pk);
  1037. pk = dup_pk;
  1038. if (!ret)
  1039. goto err;
  1040. }
  1041. err:
  1042. EVP_PKEY_free(pk);
  1043. EVP_PKEY_free(copy_pk);
  1044. EVP_PKEY_CTX_free(ctx);
  1045. EVP_PKEY_CTX_free(ctx2);
  1046. return ret;
  1047. }
  1048. static int test_fromdata_ec(void)
  1049. {
  1050. int ret = 0;
  1051. EVP_PKEY_CTX *ctx = NULL;
  1052. EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL;
  1053. OSSL_PARAM_BLD *bld = NULL;
  1054. BIGNUM *ec_priv_bn = NULL;
  1055. BIGNUM *bn_priv = NULL;
  1056. OSSL_PARAM *fromdata_params = NULL;
  1057. const char *alg = "EC";
  1058. const char *curve = "prime256v1";
  1059. const char bad_curve[] = "nonexistent-curve";
  1060. OSSL_PARAM nokey_params[2] = {
  1061. OSSL_PARAM_END,
  1062. OSSL_PARAM_END
  1063. };
  1064. /* UNCOMPRESSED FORMAT */
  1065. static const unsigned char ec_pub_keydata[] = {
  1066. POINT_CONVERSION_UNCOMPRESSED,
  1067. 0x1b, 0x93, 0x67, 0x55, 0x1c, 0x55, 0x9f, 0x63,
  1068. 0xd1, 0x22, 0xa4, 0xd8, 0xd1, 0x0a, 0x60, 0x6d,
  1069. 0x02, 0xa5, 0x77, 0x57, 0xc8, 0xa3, 0x47, 0x73,
  1070. 0x3a, 0x6a, 0x08, 0x28, 0x39, 0xbd, 0xc9, 0xd2,
  1071. 0x80, 0xec, 0xe9, 0xa7, 0x08, 0x29, 0x71, 0x2f,
  1072. 0xc9, 0x56, 0x82, 0xee, 0x9a, 0x85, 0x0f, 0x6d,
  1073. 0x7f, 0x59, 0x5f, 0x8c, 0xd1, 0x96, 0x0b, 0xdf,
  1074. 0x29, 0x3e, 0x49, 0x07, 0x88, 0x3f, 0x9a, 0x29
  1075. };
  1076. /* SAME BUT COMPRESSED FORMAT */
  1077. static const unsigned char ec_pub_keydata_compressed[] = {
  1078. POINT_CONVERSION_COMPRESSED+1,
  1079. 0x1b, 0x93, 0x67, 0x55, 0x1c, 0x55, 0x9f, 0x63,
  1080. 0xd1, 0x22, 0xa4, 0xd8, 0xd1, 0x0a, 0x60, 0x6d,
  1081. 0x02, 0xa5, 0x77, 0x57, 0xc8, 0xa3, 0x47, 0x73,
  1082. 0x3a, 0x6a, 0x08, 0x28, 0x39, 0xbd, 0xc9, 0xd2
  1083. };
  1084. static const unsigned char ec_priv_keydata[] = {
  1085. 0x33, 0xd0, 0x43, 0x83, 0xa9, 0x89, 0x56, 0x03,
  1086. 0xd2, 0xd7, 0xfe, 0x6b, 0x01, 0x6f, 0xe4, 0x59,
  1087. 0xcc, 0x0d, 0x9a, 0x24, 0x6c, 0x86, 0x1b, 0x2e,
  1088. 0xdc, 0x4b, 0x4d, 0x35, 0x43, 0xe1, 0x1b, 0xad
  1089. };
  1090. unsigned char out_pub[sizeof(ec_pub_keydata)];
  1091. char out_curve_name[80];
  1092. const OSSL_PARAM *gettable = NULL;
  1093. size_t len;
  1094. EC_GROUP *group = NULL;
  1095. BIGNUM *group_a = NULL;
  1096. BIGNUM *group_b = NULL;
  1097. BIGNUM *group_p = NULL;
  1098. BIGNUM *a = NULL;
  1099. BIGNUM *b = NULL;
  1100. BIGNUM *p = NULL;
  1101. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()))
  1102. goto err;
  1103. if (!TEST_ptr(ec_priv_bn = BN_bin2bn(ec_priv_keydata,
  1104. sizeof(ec_priv_keydata), NULL)))
  1105. goto err;
  1106. if (OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME,
  1107. curve, 0) <= 0)
  1108. goto err;
  1109. /*
  1110. * We intentionally provide the input point in compressed format,
  1111. * and avoid setting `OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT`.
  1112. *
  1113. * Later on we check what format is used when exporting the
  1114. * `OSSL_PKEY_PARAM_PUB_KEY` and expect to default to uncompressed
  1115. * format.
  1116. */
  1117. if (OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_PUB_KEY,
  1118. ec_pub_keydata_compressed,
  1119. sizeof(ec_pub_keydata_compressed)) <= 0)
  1120. goto err;
  1121. if (OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, ec_priv_bn) <= 0)
  1122. goto err;
  1123. if (!TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)))
  1124. goto err;
  1125. ctx = EVP_PKEY_CTX_new_from_name(NULL, alg, NULL);
  1126. if (!TEST_ptr(ctx))
  1127. goto err;
  1128. /* try importing parameters with bad curve first */
  1129. nokey_params[0] =
  1130. OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  1131. (char *)bad_curve, sizeof(bad_curve));
  1132. if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
  1133. || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEY_PARAMETERS,
  1134. nokey_params), 0)
  1135. || !TEST_ptr_null(pk))
  1136. goto err;
  1137. if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
  1138. || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR,
  1139. fromdata_params), 1))
  1140. goto err;
  1141. while (dup_pk == NULL) {
  1142. ret = 0;
  1143. if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 256)
  1144. || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 128)
  1145. || !TEST_int_eq(EVP_PKEY_get_size(pk), 2 + 35 * 2)
  1146. || !TEST_false(EVP_PKEY_missing_parameters(pk)))
  1147. goto err;
  1148. if (!TEST_ptr(copy_pk = EVP_PKEY_new())
  1149. || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk)))
  1150. goto err;
  1151. EVP_PKEY_free(copy_pk);
  1152. copy_pk = NULL;
  1153. if (!TEST_ptr(gettable = EVP_PKEY_gettable_params(pk))
  1154. || !TEST_ptr(OSSL_PARAM_locate_const(gettable,
  1155. OSSL_PKEY_PARAM_GROUP_NAME))
  1156. || !TEST_ptr(OSSL_PARAM_locate_const(gettable,
  1157. OSSL_PKEY_PARAM_PUB_KEY))
  1158. || !TEST_ptr(OSSL_PARAM_locate_const(gettable,
  1159. OSSL_PKEY_PARAM_PRIV_KEY)))
  1160. goto err;
  1161. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(OBJ_sn2nid(curve)))
  1162. || !TEST_ptr(group_p = BN_new())
  1163. || !TEST_ptr(group_a = BN_new())
  1164. || !TEST_ptr(group_b = BN_new())
  1165. || !TEST_true(EC_GROUP_get_curve(group, group_p, group_a, group_b, NULL)))
  1166. goto err;
  1167. if (!TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_EC_A, &a))
  1168. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_EC_B, &b))
  1169. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_EC_P, &p)))
  1170. goto err;
  1171. if (!TEST_BN_eq(group_p, p) || !TEST_BN_eq(group_a, a)
  1172. || !TEST_BN_eq(group_b, b))
  1173. goto err;
  1174. if (!EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME,
  1175. out_curve_name,
  1176. sizeof(out_curve_name),
  1177. &len)
  1178. || !TEST_str_eq(out_curve_name, curve)
  1179. || !EVP_PKEY_get_octet_string_param(pk, OSSL_PKEY_PARAM_PUB_KEY,
  1180. out_pub, sizeof(out_pub), &len)
  1181. /*
  1182. * Our providers use uncompressed format by default if
  1183. * `OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT` was not
  1184. * explicitly set, irrespective of the format used for the
  1185. * input point given as a param to create this key.
  1186. */
  1187. || !TEST_true(out_pub[0] == POINT_CONVERSION_UNCOMPRESSED)
  1188. || !TEST_mem_eq(out_pub + 1, len - 1,
  1189. ec_pub_keydata + 1, sizeof(ec_pub_keydata) - 1)
  1190. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY,
  1191. &bn_priv))
  1192. || !TEST_BN_eq(ec_priv_bn, bn_priv))
  1193. goto err;
  1194. BN_free(bn_priv);
  1195. bn_priv = NULL;
  1196. ret = test_print_key_using_pem(alg, pk)
  1197. && test_print_key_using_encoder(alg, pk);
  1198. if (!ret || !TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
  1199. goto err;
  1200. ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
  1201. EVP_PKEY_free(pk);
  1202. pk = dup_pk;
  1203. if (!ret)
  1204. goto err;
  1205. }
  1206. err:
  1207. EC_GROUP_free(group);
  1208. BN_free(group_a);
  1209. BN_free(group_b);
  1210. BN_free(group_p);
  1211. BN_free(a);
  1212. BN_free(b);
  1213. BN_free(p);
  1214. BN_free(bn_priv);
  1215. BN_free(ec_priv_bn);
  1216. OSSL_PARAM_free(fromdata_params);
  1217. OSSL_PARAM_BLD_free(bld);
  1218. EVP_PKEY_free(pk);
  1219. EVP_PKEY_free(copy_pk);
  1220. EVP_PKEY_CTX_free(ctx);
  1221. return ret;
  1222. }
  1223. static int test_ec_dup_no_operation(void)
  1224. {
  1225. int ret = 0;
  1226. EVP_PKEY_CTX *pctx = NULL, *ctx = NULL, *kctx = NULL;
  1227. EVP_PKEY *param = NULL, *pkey = NULL;
  1228. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL))
  1229. || !TEST_int_gt(EVP_PKEY_paramgen_init(pctx), 0)
  1230. || !TEST_int_gt(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
  1231. NID_X9_62_prime256v1), 0)
  1232. || !TEST_int_gt(EVP_PKEY_paramgen(pctx, &param), 0)
  1233. || !TEST_ptr(param))
  1234. goto err;
  1235. EVP_PKEY_CTX_free(pctx);
  1236. pctx = NULL;
  1237. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(NULL, param, NULL))
  1238. || !TEST_ptr(kctx = EVP_PKEY_CTX_dup(ctx))
  1239. || !TEST_int_gt(EVP_PKEY_keygen_init(kctx), 0)
  1240. || !TEST_int_gt(EVP_PKEY_keygen(kctx, &pkey), 0))
  1241. goto err;
  1242. ret = 1;
  1243. err:
  1244. EVP_PKEY_free(pkey);
  1245. EVP_PKEY_free(param);
  1246. EVP_PKEY_CTX_free(ctx);
  1247. EVP_PKEY_CTX_free(kctx);
  1248. EVP_PKEY_CTX_free(pctx);
  1249. return ret;
  1250. }
  1251. /* Test that keygen doesn't support EVP_PKEY_CTX_dup */
  1252. static int test_ec_dup_keygen_operation(void)
  1253. {
  1254. int ret = 0;
  1255. EVP_PKEY_CTX *pctx = NULL, *ctx = NULL, *kctx = NULL;
  1256. EVP_PKEY *param = NULL, *pkey = NULL;
  1257. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL))
  1258. || !TEST_int_gt(EVP_PKEY_paramgen_init(pctx), 0)
  1259. || !TEST_int_gt(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
  1260. NID_X9_62_prime256v1), 0)
  1261. || !TEST_int_gt(EVP_PKEY_paramgen(pctx, &param), 0)
  1262. || !TEST_ptr(param))
  1263. goto err;
  1264. EVP_PKEY_CTX_free(pctx);
  1265. pctx = NULL;
  1266. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(NULL, param, NULL))
  1267. || !TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
  1268. || !TEST_ptr_null(kctx = EVP_PKEY_CTX_dup(ctx)))
  1269. goto err;
  1270. ret = 1;
  1271. err:
  1272. EVP_PKEY_free(pkey);
  1273. EVP_PKEY_free(param);
  1274. EVP_PKEY_CTX_free(ctx);
  1275. EVP_PKEY_CTX_free(kctx);
  1276. EVP_PKEY_CTX_free(pctx);
  1277. return ret;
  1278. }
  1279. #endif /* OPENSSL_NO_EC */
  1280. #ifndef OPENSSL_NO_DSA
  1281. static int test_fromdata_dsa_fips186_4(void)
  1282. {
  1283. int ret = 0;
  1284. EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
  1285. EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL;
  1286. BIGNUM *pub = NULL, *priv = NULL;
  1287. BIGNUM *p = NULL, *q = NULL, *g = NULL;
  1288. BIGNUM *pub_out = NULL, *priv_out = NULL;
  1289. BIGNUM *p_out = NULL, *q_out = NULL, *g_out = NULL, *j_out = NULL;
  1290. int gindex_out = 0, pcounter_out = 0, hindex_out = 0;
  1291. char name_out[80];
  1292. unsigned char seed_out[32];
  1293. size_t len;
  1294. OSSL_PARAM_BLD *bld = NULL;
  1295. OSSL_PARAM *fromdata_params = NULL;
  1296. /*
  1297. * DSA parameter data was generated using the following:
  1298. * openssl genpkey -genparam -algorithm DSA -pkeyopt pbits:2048 \
  1299. * -pkeyopt qbits:256 -pkeyopt type:0 \
  1300. * -pkeyopt gindex:1 -out dsa_params.pem -text
  1301. */
  1302. static const unsigned char p_data[] = {
  1303. 0x00, 0xa0, 0xb7, 0x02, 0xc4, 0xac, 0xa6, 0x42, 0xab, 0xf2, 0x34, 0x0b,
  1304. 0x22, 0x47, 0x1f, 0x33, 0xcf, 0xd5, 0x04, 0xe4, 0x3e, 0xec, 0xa1, 0x21,
  1305. 0xc8, 0x41, 0x2b, 0xef, 0xb8, 0x1f, 0x0b, 0x5b, 0x88, 0x8b, 0x67, 0xf8,
  1306. 0x68, 0x6d, 0x7c, 0x4d, 0x96, 0x5f, 0x3c, 0x66, 0xef, 0x58, 0x34, 0xd7,
  1307. 0xf6, 0xa2, 0x1b, 0xad, 0xc8, 0x12, 0x52, 0xb8, 0xe8, 0x2a, 0x63, 0xcc,
  1308. 0xea, 0xe7, 0x4e, 0xc8, 0x34, 0x4c, 0x58, 0x59, 0x0a, 0xc2, 0x4a, 0xe4,
  1309. 0xb4, 0x64, 0x20, 0xf4, 0xf6, 0x0a, 0xcf, 0x86, 0x01, 0x6c, 0x7f, 0x23,
  1310. 0x4a, 0x51, 0x07, 0x99, 0x42, 0x28, 0x7a, 0xff, 0x18, 0x67, 0x52, 0x64,
  1311. 0xf2, 0x9a, 0x62, 0x30, 0xc3, 0x00, 0xde, 0x23, 0xe9, 0x11, 0x95, 0x7e,
  1312. 0xd1, 0x3d, 0x8d, 0xb4, 0x0e, 0x9f, 0x9e, 0xb1, 0x30, 0x03, 0xf0, 0x73,
  1313. 0xa8, 0x40, 0x48, 0x42, 0x7b, 0x60, 0xa0, 0xc4, 0xf2, 0x3b, 0x2d, 0x0a,
  1314. 0x0c, 0xb8, 0x19, 0xfb, 0xb4, 0xf8, 0xe0, 0x2a, 0xc7, 0xf1, 0xc0, 0xc6,
  1315. 0x86, 0x14, 0x60, 0x12, 0x0f, 0xc0, 0xde, 0x4a, 0x67, 0xec, 0xc7, 0xde,
  1316. 0x76, 0x21, 0x1a, 0x55, 0x7f, 0x86, 0xc3, 0x97, 0x98, 0xce, 0xf5, 0xcd,
  1317. 0xf0, 0xe7, 0x12, 0xd6, 0x93, 0xee, 0x1b, 0x9b, 0x61, 0xef, 0x05, 0x8c,
  1318. 0x45, 0x46, 0xd9, 0x64, 0x6f, 0xbe, 0x27, 0xaa, 0x67, 0x01, 0xcc, 0x71,
  1319. 0xb1, 0x60, 0xce, 0x21, 0xd8, 0x51, 0x17, 0x27, 0x0d, 0x90, 0x3d, 0x18,
  1320. 0x7c, 0x87, 0x15, 0x8e, 0x48, 0x4c, 0x6c, 0xc5, 0x72, 0xeb, 0xb7, 0x56,
  1321. 0xf5, 0x6b, 0x60, 0x8f, 0xc2, 0xfd, 0x3f, 0x46, 0x5c, 0x00, 0x91, 0x85,
  1322. 0x79, 0x45, 0x5b, 0x1c, 0x82, 0xc4, 0x87, 0x50, 0x79, 0xba, 0xcc, 0x1c,
  1323. 0x32, 0x7e, 0x2e, 0xb8, 0x2e, 0xc5, 0x4e, 0xd1, 0x9b, 0xdb, 0x66, 0x79,
  1324. 0x7c, 0xfe, 0xaf, 0x6a, 0x05
  1325. };
  1326. static const unsigned char q_data[] = {
  1327. 0xa8, 0xcd, 0xf4, 0x33, 0x7b, 0x13, 0x0a, 0x24, 0xc1, 0xde, 0x4a, 0x04,
  1328. 0x7b, 0x4b, 0x71, 0x51, 0x32, 0xe9, 0x47, 0x74, 0xbd, 0x0c, 0x21, 0x40,
  1329. 0x84, 0x12, 0x0a, 0x17, 0x73, 0xdb, 0x29, 0xc7
  1330. };
  1331. static const unsigned char g_data[] = {
  1332. 0x6c, 0xc6, 0xa4, 0x3e, 0x61, 0x84, 0xc1, 0xff, 0x6f, 0x4a, 0x1a, 0x6b,
  1333. 0xb0, 0x24, 0x4b, 0xd2, 0x92, 0x5b, 0x29, 0x5c, 0x61, 0xb8, 0xc9, 0x2b,
  1334. 0xd6, 0xf7, 0x59, 0xfd, 0xd8, 0x70, 0x66, 0x77, 0xfc, 0xc1, 0xa4, 0xd4,
  1335. 0xb0, 0x1e, 0xd5, 0xbf, 0x59, 0x98, 0xb3, 0x66, 0x8b, 0xf4, 0x2e, 0xe6,
  1336. 0x12, 0x3e, 0xcc, 0xf8, 0x02, 0xb8, 0xc6, 0xc3, 0x47, 0xd2, 0xf5, 0xaa,
  1337. 0x0c, 0x5f, 0x51, 0xf5, 0xd0, 0x4c, 0x55, 0x3d, 0x07, 0x73, 0xa6, 0x57,
  1338. 0xce, 0x5a, 0xad, 0x42, 0x0c, 0x13, 0x0f, 0xe2, 0x31, 0x25, 0x8e, 0x72,
  1339. 0x12, 0x73, 0x10, 0xdb, 0x7f, 0x79, 0xeb, 0x59, 0xfc, 0xfe, 0xf7, 0x0c,
  1340. 0x1a, 0x81, 0x53, 0x96, 0x22, 0xb8, 0xe7, 0x58, 0xd8, 0x67, 0x80, 0x60,
  1341. 0xad, 0x8b, 0x55, 0x1c, 0x91, 0xf0, 0x72, 0x9a, 0x7e, 0xad, 0x37, 0xf1,
  1342. 0x77, 0x18, 0x96, 0x8a, 0x68, 0x70, 0xfc, 0x71, 0xa9, 0xa2, 0xe8, 0x35,
  1343. 0x27, 0x78, 0xf2, 0xef, 0x59, 0x36, 0x6d, 0x7c, 0xb6, 0x98, 0xd8, 0x1e,
  1344. 0xfa, 0x25, 0x73, 0x97, 0x45, 0x58, 0xe3, 0xae, 0xbd, 0x52, 0x54, 0x05,
  1345. 0xd8, 0x26, 0x26, 0xba, 0xba, 0x05, 0xb5, 0xe9, 0xe5, 0x76, 0xae, 0x25,
  1346. 0xdd, 0xfc, 0x10, 0x89, 0x5a, 0xa9, 0xee, 0x59, 0xc5, 0x79, 0x8b, 0xeb,
  1347. 0x1e, 0x2c, 0x61, 0xab, 0x0d, 0xd1, 0x10, 0x04, 0x91, 0x32, 0x77, 0x4a,
  1348. 0xa6, 0x64, 0x53, 0xda, 0x4c, 0xd7, 0x3a, 0x29, 0xd4, 0xf3, 0x82, 0x25,
  1349. 0x1d, 0x6f, 0x4a, 0x7f, 0xd3, 0x08, 0x3b, 0x42, 0x30, 0x10, 0xd8, 0xd0,
  1350. 0x97, 0x3a, 0xeb, 0x92, 0x63, 0xec, 0x93, 0x2b, 0x6f, 0x32, 0xd8, 0xcd,
  1351. 0x80, 0xd3, 0xc0, 0x4c, 0x03, 0xd5, 0xca, 0xbc, 0x8f, 0xc7, 0x43, 0x53,
  1352. 0x64, 0x66, 0x1c, 0x82, 0x2d, 0xfb, 0xff, 0x39, 0xba, 0xd6, 0x42, 0x62,
  1353. 0x02, 0x6f, 0x96, 0x36
  1354. };
  1355. static const unsigned char seed_data[] = {
  1356. 0x64, 0x46, 0x07, 0x32, 0x8d, 0x70, 0x9c, 0xb3, 0x8a, 0x35, 0xde, 0x62,
  1357. 0x00, 0xf2, 0x6d, 0x52, 0x37, 0x4d, 0xb3, 0x84, 0xe1, 0x9d, 0x41, 0x04,
  1358. 0xda, 0x7b, 0xdc, 0x0d, 0x8b, 0x5e, 0xe0, 0x84
  1359. };
  1360. const int gindex = 1;
  1361. const int pcounter = 53;
  1362. /*
  1363. * The keypair was generated using
  1364. * openssl genpkey -paramfile dsa_params.pem --pkeyopt pcounter:53 \
  1365. * -pkeyopt gindex:1 \
  1366. * -pkeyopt hexseed:644607328d709cb38a35de6200f26d -text
  1367. */
  1368. static const unsigned char priv_data[] = {
  1369. 0x00, 0x8f, 0xc5, 0x9e, 0xd0, 0xf7, 0x2a, 0x0b, 0x66, 0xf1, 0x32, 0x73,
  1370. 0xae, 0xf6, 0xd9, 0xd4, 0xdb, 0x2d, 0x96, 0x55, 0x89, 0xff, 0xef, 0xa8,
  1371. 0x5f, 0x47, 0x8f, 0xca, 0x02, 0x8a, 0xe1, 0x35, 0x90
  1372. };
  1373. static const unsigned char pub_data[] = {
  1374. 0x44, 0x19, 0xc9, 0x46, 0x45, 0x57, 0xc1, 0xa9, 0xd8, 0x30, 0x99, 0x29,
  1375. 0x6a, 0x4b, 0x63, 0x71, 0x69, 0x96, 0x35, 0x17, 0xb2, 0x62, 0x9b, 0x80,
  1376. 0x0a, 0x95, 0x9d, 0x6a, 0xc0, 0x32, 0x0d, 0x07, 0x5f, 0x19, 0x44, 0x02,
  1377. 0xf1, 0xbd, 0xce, 0xdf, 0x10, 0xf8, 0x02, 0x5d, 0x7d, 0x98, 0x8a, 0x73,
  1378. 0x89, 0x00, 0xb6, 0x24, 0xd6, 0x33, 0xe7, 0xcf, 0x8b, 0x49, 0x2a, 0xaf,
  1379. 0x13, 0x1c, 0xb2, 0x52, 0x15, 0xfd, 0x9b, 0xd5, 0x40, 0x4a, 0x1a, 0xda,
  1380. 0x29, 0x4c, 0x92, 0x7e, 0x66, 0x06, 0xdb, 0x61, 0x86, 0xac, 0xb5, 0xda,
  1381. 0x3c, 0x7d, 0x73, 0x7e, 0x54, 0x32, 0x68, 0xa5, 0x02, 0xbc, 0x59, 0x47,
  1382. 0x84, 0xd3, 0x87, 0x71, 0x5f, 0xeb, 0x43, 0x45, 0x24, 0xd3, 0xec, 0x08,
  1383. 0x52, 0xc2, 0x89, 0x2d, 0x9c, 0x1a, 0xcc, 0x91, 0x65, 0x5d, 0xa3, 0xa1,
  1384. 0x35, 0x31, 0x10, 0x1c, 0x3a, 0xa8, 0x4d, 0x18, 0xd5, 0x06, 0xaf, 0xb2,
  1385. 0xec, 0x5c, 0x89, 0x9e, 0x90, 0x86, 0x10, 0x01, 0xeb, 0x51, 0xd5, 0x1b,
  1386. 0x9c, 0xcb, 0x66, 0x07, 0x3f, 0xc4, 0x6e, 0x0a, 0x1b, 0x73, 0xa0, 0x4b,
  1387. 0x5f, 0x4d, 0xab, 0x35, 0x28, 0xfa, 0xda, 0x3a, 0x0c, 0x08, 0xe8, 0xf3,
  1388. 0xef, 0x42, 0x67, 0xbc, 0x21, 0xf2, 0xc2, 0xb8, 0xff, 0x1a, 0x81, 0x05,
  1389. 0x68, 0x73, 0x62, 0xdf, 0xd7, 0xab, 0x0f, 0x22, 0x89, 0x57, 0x96, 0xd4,
  1390. 0x93, 0xaf, 0xa1, 0x21, 0xa3, 0x48, 0xe9, 0xf0, 0x97, 0x47, 0xa0, 0x27,
  1391. 0xba, 0x87, 0xb8, 0x15, 0x5f, 0xff, 0x2c, 0x50, 0x41, 0xf1, 0x7e, 0xc6,
  1392. 0x81, 0xc4, 0x51, 0xf1, 0xfd, 0xd6, 0x86, 0xf7, 0x69, 0x97, 0xf1, 0x49,
  1393. 0xc9, 0xf9, 0xf4, 0x9b, 0xf4, 0xe8, 0x85, 0xa7, 0xbd, 0x36, 0x55, 0x4a,
  1394. 0x3d, 0xe8, 0x65, 0x09, 0x7b, 0xb7, 0x12, 0x64, 0xd2, 0x0a, 0x53, 0x60,
  1395. 0x48, 0xd1, 0x8a, 0xbd
  1396. };
  1397. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  1398. || !TEST_ptr(pub = BN_bin2bn(pub_data, sizeof(pub_data), NULL))
  1399. || !TEST_ptr(priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL))
  1400. || !TEST_ptr(p = BN_bin2bn(p_data, sizeof(p_data), NULL))
  1401. || !TEST_ptr(q = BN_bin2bn(q_data, sizeof(q_data), NULL))
  1402. || !TEST_ptr(g = BN_bin2bn(g_data, sizeof(g_data), NULL))
  1403. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
  1404. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
  1405. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
  1406. || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
  1407. OSSL_PKEY_PARAM_FFC_SEED,
  1408. seed_data,
  1409. sizeof(seed_data)))
  1410. || !TEST_true(OSSL_PARAM_BLD_push_int(bld, OSSL_PKEY_PARAM_FFC_GINDEX,
  1411. gindex))
  1412. || !TEST_true(OSSL_PARAM_BLD_push_int(bld,
  1413. OSSL_PKEY_PARAM_FFC_PCOUNTER,
  1414. pcounter))
  1415. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
  1416. pub))
  1417. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
  1418. priv))
  1419. || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)))
  1420. goto err;
  1421. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL)))
  1422. goto err;
  1423. if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
  1424. || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR,
  1425. fromdata_params), 1))
  1426. goto err;
  1427. while (dup_pk == NULL) {
  1428. ret = 0;
  1429. if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 2048)
  1430. || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 112)
  1431. || !TEST_int_eq(EVP_PKEY_get_size(pk), 2 + 2 * (3 + sizeof(q_data)))
  1432. || !TEST_false(EVP_PKEY_missing_parameters(pk)))
  1433. goto err;
  1434. if (!TEST_false(EVP_PKEY_get_utf8_string_param(pk,
  1435. OSSL_PKEY_PARAM_GROUP_NAME,
  1436. name_out,
  1437. sizeof(name_out),
  1438. &len))
  1439. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PUB_KEY,
  1440. &pub_out))
  1441. || !TEST_BN_eq(pub, pub_out)
  1442. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY,
  1443. &priv_out))
  1444. || !TEST_BN_eq(priv, priv_out)
  1445. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_P,
  1446. &p_out))
  1447. || !TEST_BN_eq(p, p_out)
  1448. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_Q,
  1449. &q_out))
  1450. || !TEST_BN_eq(q, q_out)
  1451. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_G,
  1452. &g_out))
  1453. || !TEST_BN_eq(g, g_out)
  1454. || !TEST_false(EVP_PKEY_get_bn_param(pk,
  1455. OSSL_PKEY_PARAM_FFC_COFACTOR,
  1456. &j_out))
  1457. || !TEST_ptr_null(j_out)
  1458. || !TEST_true(EVP_PKEY_get_octet_string_param(pk,
  1459. OSSL_PKEY_PARAM_FFC_SEED,
  1460. seed_out,
  1461. sizeof(seed_out),
  1462. &len))
  1463. || !TEST_true(EVP_PKEY_get_int_param(pk,
  1464. OSSL_PKEY_PARAM_FFC_GINDEX,
  1465. &gindex_out))
  1466. || !TEST_int_eq(gindex, gindex_out)
  1467. || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_H,
  1468. &hindex_out))
  1469. || !TEST_int_eq(hindex_out, 0)
  1470. || !TEST_true(EVP_PKEY_get_int_param(pk,
  1471. OSSL_PKEY_PARAM_FFC_PCOUNTER,
  1472. &pcounter_out))
  1473. || !TEST_int_eq(pcounter, pcounter_out))
  1474. goto err;
  1475. BN_free(p);
  1476. p = NULL;
  1477. BN_free(q);
  1478. q = NULL;
  1479. BN_free(g);
  1480. g = NULL;
  1481. BN_free(j_out);
  1482. j_out = NULL;
  1483. BN_free(pub_out);
  1484. pub_out = NULL;
  1485. BN_free(priv_out);
  1486. priv_out = NULL;
  1487. if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, "")))
  1488. goto err;
  1489. if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0)
  1490. || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0)
  1491. || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0)
  1492. || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0))
  1493. goto err;
  1494. EVP_PKEY_CTX_free(key_ctx);
  1495. key_ctx = NULL;
  1496. if (!TEST_ptr(copy_pk = EVP_PKEY_new())
  1497. || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk)))
  1498. goto err;
  1499. EVP_PKEY_free(copy_pk);
  1500. copy_pk = NULL;
  1501. ret = test_print_key_using_pem("DSA", pk)
  1502. && test_print_key_using_encoder("DSA", pk);
  1503. if (!ret || !TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
  1504. goto err;
  1505. ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
  1506. EVP_PKEY_free(pk);
  1507. pk = dup_pk;
  1508. if (!ret)
  1509. goto err;
  1510. }
  1511. err:
  1512. OSSL_PARAM_free(fromdata_params);
  1513. OSSL_PARAM_BLD_free(bld);
  1514. BN_free(p);
  1515. BN_free(q);
  1516. BN_free(g);
  1517. BN_free(pub);
  1518. BN_free(priv);
  1519. BN_free(p_out);
  1520. BN_free(q_out);
  1521. BN_free(g_out);
  1522. BN_free(pub_out);
  1523. BN_free(priv_out);
  1524. BN_free(j_out);
  1525. EVP_PKEY_free(pk);
  1526. EVP_PKEY_free(copy_pk);
  1527. EVP_PKEY_CTX_free(ctx);
  1528. EVP_PKEY_CTX_free(key_ctx);
  1529. return ret;
  1530. }
  1531. static int test_check_dsa(void)
  1532. {
  1533. int ret = 0;
  1534. EVP_PKEY_CTX *ctx = NULL;
  1535. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL))
  1536. || !TEST_int_le(EVP_PKEY_check(ctx), 0)
  1537. || !TEST_int_le(EVP_PKEY_public_check(ctx), 0)
  1538. || !TEST_int_le(EVP_PKEY_private_check(ctx), 0)
  1539. || !TEST_int_le(EVP_PKEY_pairwise_check(ctx), 0))
  1540. goto err;
  1541. ret = 1;
  1542. err:
  1543. EVP_PKEY_CTX_free(ctx);
  1544. return ret;
  1545. }
  1546. #endif /* OPENSSL_NO_DSA */
  1547. static OSSL_PARAM *do_construct_hkdf_params(char *digest, char *key,
  1548. size_t keylen, char *salt)
  1549. {
  1550. OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 5);
  1551. OSSL_PARAM *p = params;
  1552. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, digest, 0);
  1553. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
  1554. salt, strlen(salt));
  1555. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
  1556. (unsigned char *)key, keylen);
  1557. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MODE,
  1558. "EXTRACT_ONLY", 0);
  1559. *p = OSSL_PARAM_construct_end();
  1560. return params;
  1561. }
  1562. static int test_evp_pkey_ctx_dup_kdf(void)
  1563. {
  1564. int ret = 0;
  1565. size_t len = 0, dlen = 0;
  1566. EVP_PKEY_CTX *pctx = NULL, *dctx = NULL;
  1567. OSSL_PARAM *params = NULL;
  1568. if (!TEST_ptr(params = do_construct_hkdf_params("sha256", "secret", 6,
  1569. "salt")))
  1570. goto err;
  1571. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(NULL, "HKDF", NULL)))
  1572. goto err;
  1573. if (!TEST_int_eq(EVP_PKEY_derive_init_ex(pctx, params), 1))
  1574. goto err;
  1575. if (!TEST_ptr(dctx = EVP_PKEY_CTX_dup(pctx)))
  1576. goto err;
  1577. if (!TEST_int_eq(EVP_PKEY_derive(pctx, NULL, &len), 1)
  1578. || !TEST_size_t_eq(len, SHA256_DIGEST_LENGTH)
  1579. || !TEST_int_eq(EVP_PKEY_derive(dctx, NULL, &dlen), 1)
  1580. || !TEST_size_t_eq(dlen, SHA256_DIGEST_LENGTH))
  1581. goto err;
  1582. ret = 1;
  1583. err:
  1584. OPENSSL_free(params);
  1585. EVP_PKEY_CTX_free(dctx);
  1586. EVP_PKEY_CTX_free(pctx);
  1587. return ret;
  1588. }
  1589. int setup_tests(void)
  1590. {
  1591. if (!test_skip_common_options()) {
  1592. TEST_error("Error parsing test options\n");
  1593. return 0;
  1594. }
  1595. if (!TEST_ptr(datadir = test_get_argument(0)))
  1596. return 0;
  1597. ADD_TEST(test_evp_pkey_ctx_dup_kdf);
  1598. ADD_TEST(test_evp_pkey_get_bn_param_large);
  1599. ADD_TEST(test_fromdata_rsa);
  1600. #ifndef OPENSSL_NO_DH
  1601. ADD_TEST(test_fromdata_dh_fips186_4);
  1602. ADD_TEST(test_fromdata_dh_named_group);
  1603. #endif
  1604. #ifndef OPENSSL_NO_DSA
  1605. ADD_TEST(test_check_dsa);
  1606. ADD_TEST(test_fromdata_dsa_fips186_4);
  1607. #endif
  1608. #ifndef OPENSSL_NO_EC
  1609. ADD_ALL_TESTS(test_fromdata_ecx, 4 * 3);
  1610. ADD_TEST(test_fromdata_ec);
  1611. ADD_TEST(test_ec_dup_no_operation);
  1612. ADD_TEST(test_ec_dup_keygen_operation);
  1613. #endif
  1614. return 1;
  1615. }