evp_pkey_provided_test.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215
  1. /*
  2. * Copyright 2019-2024 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_ECX
  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. for (;;) {
  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 || dup_pk != NULL)
  364. break;
  365. if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
  366. goto err;
  367. ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
  368. EVP_PKEY_free(pk);
  369. pk = dup_pk;
  370. if (!ret)
  371. goto err;
  372. }
  373. err:
  374. /* for better diagnostics always compare key params */
  375. for (i = 0; fromdata_params[i].key != NULL; ++i) {
  376. if (!TEST_true(BN_set_word(bn_from, key_numbers[i]))
  377. || !TEST_true(EVP_PKEY_get_bn_param(pk, fromdata_params[i].key,
  378. &bn))
  379. || !TEST_BN_eq(bn, bn_from))
  380. ret = 0;
  381. }
  382. BN_free(bn_from);
  383. BN_free(bn);
  384. EVP_PKEY_free(pk);
  385. EVP_PKEY_free(copy_pk);
  386. EVP_PKEY_CTX_free(key_ctx);
  387. EVP_PKEY_CTX_free(ctx);
  388. return ret;
  389. }
  390. struct check_data {
  391. const char *pname;
  392. BIGNUM *comparebn;
  393. };
  394. static int do_fromdata_rsa_derive(OSSL_PARAM *fromdata_params,
  395. struct check_data check[],
  396. int expected_nbits, int expected_sbits,
  397. int expected_ksize)
  398. {
  399. const OSSL_PARAM *check_param = NULL;
  400. BIGNUM *check_bn = NULL;
  401. OSSL_PARAM *todata_params = NULL;
  402. EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
  403. EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL;
  404. int i;
  405. int ret = 0;
  406. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL))
  407. || !TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
  408. || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR,
  409. fromdata_params), 1))
  410. goto err;
  411. /*
  412. * get the generated key parameters back and validate that the
  413. * exponents/coeffs are correct
  414. */
  415. if (!TEST_int_eq(EVP_PKEY_todata(pk, EVP_PKEY_KEYPAIR, &todata_params), 1))
  416. goto err;
  417. for (i = 0; check[i].pname != NULL; i++) {
  418. if (!TEST_ptr(check_param = OSSL_PARAM_locate_const(todata_params,
  419. check[i].pname)))
  420. goto err;
  421. if (!TEST_int_eq(OSSL_PARAM_get_BN(check_param, &check_bn), 1))
  422. goto err;
  423. if (!TEST_BN_eq(check_bn, check[i].comparebn)) {
  424. TEST_info("Data mismatch for parameter %s", check[i].pname);
  425. goto err;
  426. }
  427. BN_free(check_bn);
  428. check_bn = NULL;
  429. }
  430. for (;;) {
  431. if (!TEST_int_eq(EVP_PKEY_get_bits(pk), expected_nbits)
  432. || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), expected_sbits)
  433. || !TEST_int_eq(EVP_PKEY_get_size(pk), expected_ksize)
  434. || !TEST_false(EVP_PKEY_missing_parameters(pk)))
  435. goto err;
  436. EVP_PKEY_CTX_free(key_ctx);
  437. if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, "")))
  438. goto err;
  439. if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0)
  440. || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0)
  441. || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0)
  442. || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0))
  443. goto err;
  444. /* EVP_PKEY_copy_parameters() should fail for RSA */
  445. if (!TEST_ptr(copy_pk = EVP_PKEY_new())
  446. || !TEST_false(EVP_PKEY_copy_parameters(copy_pk, pk)))
  447. goto err;
  448. EVP_PKEY_free(copy_pk);
  449. copy_pk = NULL;
  450. if (dup_pk != NULL)
  451. break;
  452. if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
  453. goto err;
  454. if (!TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1)) {
  455. EVP_PKEY_free(dup_pk);
  456. goto err;
  457. }
  458. EVP_PKEY_free(pk);
  459. pk = dup_pk;
  460. }
  461. ret = 1;
  462. err:
  463. BN_free(check_bn);
  464. EVP_PKEY_free(pk);
  465. EVP_PKEY_CTX_free(ctx);
  466. EVP_PKEY_CTX_free(key_ctx);
  467. OSSL_PARAM_free(fromdata_params);
  468. OSSL_PARAM_free(todata_params);
  469. return ret;
  470. }
  471. static int test_fromdata_rsa_derive_from_pq_sp800(void)
  472. {
  473. OSSL_PARAM_BLD *bld = NULL;
  474. BIGNUM *n = NULL, *e = NULL, *d = NULL, *p = NULL, *q = NULL;
  475. BIGNUM *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL;
  476. OSSL_PARAM *fromdata_params = NULL;
  477. struct check_data cdata[4];
  478. int ret = 0;
  479. /*
  480. * 512-bit RSA key, extracted from this command,
  481. * openssl genrsa 512 | openssl rsa -text
  482. * Note: When generating a key with EVP_PKEY_fromdata, and using
  483. * crt derivation, openssl requires a minimum of 512 bits of n data,
  484. * and 2048 bits in the FIPS case
  485. */
  486. static unsigned char n_data[] =
  487. {0x00, 0xc7, 0x06, 0xd8, 0x6b, 0x3c, 0x4f, 0xb7, 0x95, 0x42, 0x44, 0x90,
  488. 0xbd, 0xef, 0xf3, 0xc4, 0xb5, 0xa8, 0x55, 0x9e, 0x33, 0xa3, 0x04, 0x3a,
  489. 0x90, 0xe5, 0x13, 0xff, 0x87, 0x69, 0x15, 0xa4, 0x8a, 0x17, 0x10, 0xcc,
  490. 0xdf, 0xf9, 0xc5, 0x0f, 0xf1, 0x12, 0xff, 0x12, 0x11, 0xe5, 0x6b, 0x5c,
  491. 0x83, 0xd9, 0x43, 0xd1, 0x8a, 0x7e, 0xa6, 0x60, 0x07, 0x2e, 0xbb, 0x03,
  492. 0x17, 0x2d, 0xec, 0x17, 0x87};
  493. static unsigned char e_data[] = {0x01, 0x00, 0x01};
  494. static unsigned char d_data[] =
  495. {0x1e, 0x5e, 0x5d, 0x07, 0x7f, 0xdc, 0x6a, 0x16, 0xcc, 0x55, 0xca, 0x00,
  496. 0x31, 0x6c, 0xf0, 0xc7, 0x07, 0x38, 0x89, 0x3b, 0x37, 0xd4, 0x9d, 0x5b,
  497. 0x1e, 0x99, 0x3e, 0x94, 0x5a, 0xe4, 0x82, 0x86, 0x8a, 0x78, 0x34, 0x09,
  498. 0x37, 0xd5, 0xe7, 0xb4, 0xef, 0x5f, 0x83, 0x94, 0xff, 0xe5, 0x36, 0x79,
  499. 0x10, 0x0c, 0x38, 0xc5, 0x3a, 0x33, 0xa6, 0x7c, 0x3c, 0xcc, 0x98, 0xe0,
  500. 0xf5, 0xdb, 0xe6, 0x81};
  501. static unsigned char p_data[] =
  502. {0x00, 0xf6, 0x61, 0x38, 0x0e, 0x1f, 0x82, 0x7c, 0xb8, 0xba, 0x00, 0xd3,
  503. 0xac, 0xdc, 0x4e, 0x6b, 0x7e, 0xf7, 0x58, 0xf3, 0xd9, 0xd8, 0x21, 0xed,
  504. 0x54, 0xa3, 0x36, 0xd2, 0x2c, 0x5f, 0x06, 0x7d, 0xc5};
  505. static unsigned char q_data[] =
  506. {0x00, 0xce, 0xcc, 0x4a, 0xa5, 0x4f, 0xd6, 0x73, 0xd0, 0x20, 0xc3, 0x98,
  507. 0x64, 0x20, 0x9b, 0xc1, 0x23, 0xd8, 0x5c, 0x82, 0x4f, 0xe8, 0xa5, 0x32,
  508. 0xcd, 0x7e, 0x97, 0xb4, 0xde, 0xf6, 0x4c, 0x80, 0xdb};
  509. static unsigned char dmp1_data[] =
  510. {0x00, 0xd1, 0x07, 0xb6, 0x79, 0x34, 0xfe, 0x8e, 0x36, 0x63, 0x88, 0xa4,
  511. 0x0e, 0x3a, 0x73, 0x45, 0xfc, 0x58, 0x7a, 0x5d, 0x98, 0xeb, 0x28, 0x0d,
  512. 0xa5, 0x0b, 0x3c, 0x4d, 0xa0, 0x5b, 0x96, 0xb4, 0x49};
  513. static unsigned char dmq1_data[] =
  514. {0x5b, 0x47, 0x02, 0xdf, 0xaa, 0xb8, 0xae, 0x8f, 0xbc, 0x16, 0x79, 0x6a,
  515. 0x20, 0x96, 0x7f, 0x0e, 0x92, 0x4e, 0x6a, 0xda, 0x58, 0x86, 0xaa, 0x40,
  516. 0xd7, 0xd2, 0xa0, 0x6c, 0x15, 0x6c, 0xb9, 0x27};
  517. static unsigned char iqmp_data[] =
  518. {0x00, 0xa0, 0xd6, 0xf0, 0xe8, 0x17, 0x9e, 0xe7, 0xe6, 0x99, 0x12, 0xd6,
  519. 0xd9, 0x43, 0xcf, 0xed, 0x37, 0x29, 0xf5, 0x6c, 0x3e, 0xc1, 0x7f, 0x2e,
  520. 0x31, 0x3f, 0x64, 0x34, 0x66, 0x68, 0x5c, 0x22, 0x08};
  521. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  522. || !TEST_ptr(n = BN_bin2bn(n_data, sizeof(n_data), NULL))
  523. || !TEST_ptr(e = BN_bin2bn(e_data, sizeof(e_data), NULL))
  524. || !TEST_ptr(d = BN_bin2bn(d_data, sizeof(d_data), NULL))
  525. || !TEST_ptr(p = BN_bin2bn(p_data, sizeof(p_data), NULL))
  526. || !TEST_ptr(q = BN_bin2bn(q_data, sizeof(q_data), NULL))
  527. || !TEST_ptr(dmp1 = BN_bin2bn(dmp1_data, sizeof(dmp1_data), NULL))
  528. || !TEST_ptr(dmq1 = BN_bin2bn(dmq1_data, sizeof(dmq1_data), NULL))
  529. || !TEST_ptr(iqmp = BN_bin2bn(iqmp_data, sizeof(iqmp_data), NULL))
  530. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n))
  531. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e))
  532. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d))
  533. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_FACTOR1,
  534. p))
  535. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_FACTOR2,
  536. q))
  537. || !TEST_true(OSSL_PARAM_BLD_push_int(bld,
  538. OSSL_PKEY_PARAM_RSA_DERIVE_FROM_PQ, 1))
  539. || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)))
  540. goto err;
  541. cdata[0].pname = OSSL_PKEY_PARAM_RSA_EXPONENT1;
  542. cdata[0].comparebn = dmp1;
  543. cdata[1].pname = OSSL_PKEY_PARAM_RSA_EXPONENT2;
  544. cdata[1].comparebn = dmq1;
  545. cdata[2].pname = OSSL_PKEY_PARAM_RSA_COEFFICIENT1;
  546. cdata[2].comparebn = iqmp;
  547. cdata[3].pname = NULL;
  548. cdata[3].comparebn = NULL;
  549. ret = do_fromdata_rsa_derive(fromdata_params, cdata, 512, 56, 64);
  550. err:
  551. BN_free(n);
  552. BN_free(e);
  553. BN_free(d);
  554. BN_free(p);
  555. BN_free(q);
  556. BN_free(dmp1);
  557. BN_free(dmq1);
  558. BN_free(iqmp);
  559. OSSL_PARAM_BLD_free(bld);
  560. return ret;
  561. }
  562. static int test_fromdata_rsa_derive_from_pq_multiprime(void)
  563. {
  564. OSSL_PARAM_BLD *bld = NULL;
  565. BIGNUM *n = NULL, *e = NULL, *d = NULL;
  566. BIGNUM *p = NULL, *q = NULL, *p2 = NULL;
  567. BIGNUM *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL;
  568. BIGNUM *exp3 = NULL, *coeff2 = NULL;
  569. OSSL_PARAM *fromdata_params = NULL;
  570. struct check_data cdata[12];
  571. int ret = 0;
  572. /*
  573. * multiprime RSA key, extracted from this command,
  574. * openssl genrsa -primes 3 | openssl rsa -text
  575. * Note: When generating a key with EVP_PKEY_fromdata, and using
  576. * crt derivation, openssl requires a minimum of 512 bits of n data,
  577. * and 2048 bits in the FIPS case
  578. */
  579. static unsigned char n_data[] =
  580. {0x00, 0x95, 0x78, 0x21, 0xe0, 0xca, 0x94, 0x6c, 0x0b, 0x86, 0x2a, 0x01,
  581. 0xde, 0xd9, 0xab, 0xee, 0x88, 0x4a, 0x27, 0x4f, 0xcc, 0x5f, 0xf1, 0x71,
  582. 0xe1, 0x0b, 0xc3, 0xd1, 0x88, 0x76, 0xf0, 0x83, 0x03, 0x93, 0x7e, 0x39,
  583. 0xfa, 0x47, 0x89, 0x34, 0x27, 0x18, 0x19, 0x97, 0xfc, 0xd4, 0xfe, 0xe5,
  584. 0x8a, 0xa9, 0x11, 0x83, 0xb5, 0x15, 0x4a, 0x29, 0xa6, 0xa6, 0xd0, 0x6e,
  585. 0x0c, 0x7f, 0x61, 0x8f, 0x7e, 0x7c, 0xfb, 0xfc, 0x04, 0x8b, 0xca, 0x44,
  586. 0xf8, 0x59, 0x0b, 0x22, 0x6f, 0x3f, 0x92, 0x23, 0x98, 0xb5, 0xc8, 0xf7,
  587. 0xff, 0xf7, 0xac, 0x6b, 0x36, 0xb3, 0xaf, 0x39, 0xde, 0x66, 0x38, 0x51,
  588. 0x9f, 0xbe, 0xe2, 0xfc, 0xe4, 0x6f, 0x1a, 0x0f, 0x7a, 0xde, 0x7f, 0x0f,
  589. 0x4e, 0xbc, 0xed, 0xa2, 0x99, 0xc5, 0xd1, 0xbf, 0x8f, 0xba, 0x92, 0x91,
  590. 0xe4, 0x00, 0x91, 0xbb, 0x67, 0x36, 0x7d, 0x00, 0x50, 0xda, 0x28, 0x38,
  591. 0xdc, 0x9f, 0xfe, 0x3f, 0x24, 0x5a, 0x0d, 0xe1, 0x8d, 0xe9, 0x45, 0x2c,
  592. 0xd7, 0xf2, 0x67, 0x8c, 0x0c, 0x6e, 0xdb, 0xc8, 0x8b, 0x6b, 0x38, 0x30,
  593. 0x21, 0x94, 0xc0, 0xe3, 0xd7, 0xe0, 0x23, 0xd3, 0xd4, 0xfa, 0xdb, 0xb9,
  594. 0xfe, 0x1a, 0xcc, 0xc9, 0x79, 0x19, 0x35, 0x18, 0x42, 0x30, 0xc4, 0xb5,
  595. 0x92, 0x33, 0x1e, 0xd4, 0xc4, 0xc0, 0x9d, 0x55, 0x37, 0xd4, 0xef, 0x54,
  596. 0x71, 0x81, 0x09, 0x15, 0xdb, 0x11, 0x38, 0x6b, 0x35, 0x93, 0x11, 0xdc,
  597. 0xb1, 0x6c, 0xd6, 0xa4, 0x37, 0x84, 0xf3, 0xb2, 0x2f, 0x1b, 0xd6, 0x05,
  598. 0x9f, 0x0e, 0x5c, 0x98, 0x29, 0x2f, 0x95, 0xb6, 0x55, 0xbd, 0x24, 0x44,
  599. 0xc5, 0xc8, 0xa2, 0x76, 0x1e, 0xf8, 0x82, 0x8a, 0xdf, 0x34, 0x72, 0x7e,
  600. 0xdd, 0x65, 0x4b, 0xfc, 0x6c, 0x1c, 0x96, 0x70, 0xe2, 0x69, 0xb5, 0x12,
  601. 0x1b, 0x59, 0x67, 0x14, 0x9d};
  602. static unsigned char e_data[] = {0x01, 0x00, 0x01};
  603. static unsigned char d_data[] =
  604. {0x64, 0x57, 0x4d, 0x86, 0xf6, 0xf8, 0x44, 0xc0, 0x47, 0xc5, 0x13, 0x94,
  605. 0x63, 0x54, 0x84, 0xc1, 0x81, 0xe6, 0x7a, 0x2f, 0x9d, 0x89, 0x1d, 0x06,
  606. 0x13, 0x3b, 0xd6, 0x02, 0x62, 0xb6, 0x7b, 0x7d, 0x7f, 0x1a, 0x92, 0x19,
  607. 0x6e, 0xc4, 0xb0, 0xfa, 0x3d, 0xb7, 0x90, 0xcc, 0xee, 0xc0, 0x5f, 0xa0,
  608. 0x82, 0x77, 0x7b, 0x8f, 0xa9, 0x47, 0x2c, 0x46, 0xf0, 0x5d, 0xa4, 0x43,
  609. 0x47, 0x90, 0x5b, 0x20, 0x73, 0x0f, 0x46, 0xd4, 0x56, 0x73, 0xe7, 0x71,
  610. 0x41, 0x75, 0xb4, 0x1c, 0x32, 0xf5, 0x0c, 0x68, 0x8c, 0x40, 0xea, 0x1c,
  611. 0x30, 0x12, 0xa2, 0x65, 0x02, 0x27, 0x98, 0x4e, 0x0a, 0xbf, 0x2b, 0x72,
  612. 0xb2, 0x5c, 0xe3, 0xbe, 0x3e, 0xc7, 0xdb, 0x9b, 0xa2, 0x4a, 0x90, 0xc0,
  613. 0xa7, 0xb0, 0x00, 0xf1, 0x6a, 0xff, 0xa3, 0x77, 0xf7, 0x71, 0xa2, 0x41,
  614. 0xe9, 0x6e, 0x7c, 0x38, 0x24, 0x46, 0xd5, 0x5c, 0x49, 0x2a, 0xe6, 0xee,
  615. 0x27, 0x4b, 0x2e, 0x6f, 0x16, 0x54, 0x2d, 0x37, 0x36, 0x01, 0x39, 0x2b,
  616. 0x23, 0x4b, 0xb4, 0x65, 0x25, 0x4d, 0x7f, 0x72, 0x20, 0x7f, 0x5d, 0xec,
  617. 0x50, 0xba, 0xbb, 0xaa, 0x9c, 0x3c, 0x1d, 0xa1, 0x40, 0x2c, 0x6a, 0x8b,
  618. 0x5f, 0x2e, 0xe0, 0xa6, 0xf7, 0x9e, 0x03, 0xb5, 0x44, 0x5f, 0x74, 0xc7,
  619. 0x9f, 0x89, 0x2b, 0x71, 0x2f, 0x66, 0x9f, 0x03, 0x6c, 0x96, 0xd0, 0x23,
  620. 0x36, 0x4d, 0xa1, 0xf0, 0x82, 0xcc, 0x43, 0xe7, 0x08, 0x93, 0x40, 0x18,
  621. 0xc0, 0x39, 0x73, 0x83, 0xe2, 0xec, 0x9b, 0x81, 0x9d, 0x4c, 0x86, 0xaa,
  622. 0x59, 0xa8, 0x67, 0x1c, 0x80, 0xdc, 0x6f, 0x7f, 0x23, 0x6b, 0x7d, 0x2c,
  623. 0x56, 0x99, 0xa0, 0x89, 0x7e, 0xdb, 0x8b, 0x7a, 0xaa, 0x03, 0x8e, 0x8e,
  624. 0x8e, 0x3a, 0x58, 0xb4, 0x03, 0x6b, 0x65, 0xfa, 0x92, 0x0a, 0x96, 0x93,
  625. 0xa6, 0x07, 0x60, 0x01};
  626. static unsigned char p_data[] =
  627. {0x06, 0x55, 0x7f, 0xbd, 0xfd, 0xa8, 0x4c, 0x94, 0x5e, 0x10, 0x8a, 0x54,
  628. 0x37, 0xf3, 0x64, 0x37, 0x3a, 0xca, 0x18, 0x1b, 0xdd, 0x71, 0xa5, 0x94,
  629. 0xc9, 0x31, 0x59, 0xa5, 0x89, 0xe9, 0xc4, 0xba, 0x55, 0x90, 0x6d, 0x9c,
  630. 0xcc, 0x52, 0x5d, 0x44, 0xa8, 0xbc, 0x2b, 0x3b, 0x8c, 0xbd, 0x96, 0xfa,
  631. 0xcd, 0x54, 0x63, 0xe3, 0xc8, 0xfe, 0x5e, 0xc6, 0x73, 0x98, 0x14, 0x7a,
  632. 0x54, 0x0e, 0xe7, 0x75, 0x49, 0x93, 0x20, 0x33, 0x17, 0xa9, 0x34, 0xa8,
  633. 0xee, 0xaf, 0x3a, 0xcc, 0xf5, 0x69, 0xfc, 0x30, 0x1a, 0xdf, 0x49, 0x61,
  634. 0xa4, 0xd1};
  635. static unsigned char p2_data[] =
  636. {0x03, 0xe2, 0x41, 0x3d, 0xb1, 0xdd, 0xad, 0xd7, 0x3b, 0xf8, 0xab, 0x32,
  637. 0x27, 0x8b, 0xac, 0x95, 0xc0, 0x1a, 0x3f, 0x80, 0x8e, 0x21, 0xa9, 0xb8,
  638. 0xa2, 0xed, 0xcf, 0x97, 0x5c, 0x61, 0x10, 0x94, 0x1b, 0xd0, 0xbe, 0x88,
  639. 0xc2, 0xa7, 0x20, 0xe5, 0xa5, 0xc2, 0x7a, 0x7e, 0xf0, 0xd1, 0xe4, 0x13,
  640. 0x75, 0xb9, 0x62, 0x90, 0xf1, 0xc3, 0x5b, 0x8c, 0xe9, 0xa9, 0x5b, 0xb7,
  641. 0x6d, 0xdc, 0xcd, 0x12, 0xea, 0x97, 0x05, 0x04, 0x25, 0x2a, 0x93, 0xd1,
  642. 0x4e, 0x05, 0x1a, 0x50, 0xa2, 0x67, 0xb8, 0x4b, 0x09, 0x15, 0x65, 0x6c,
  643. 0x66, 0x2d};
  644. static unsigned char q_data[] =
  645. {0x06, 0x13, 0x74, 0x6e, 0xde, 0x7c, 0x33, 0xc2, 0xe7, 0x05, 0x2c, 0xeb,
  646. 0x25, 0x7d, 0x4a, 0x07, 0x7e, 0x03, 0xcf, 0x6a, 0x23, 0x36, 0x25, 0x23,
  647. 0xf6, 0x5d, 0xde, 0xa3, 0x0f, 0x82, 0xe6, 0x4b, 0xec, 0x39, 0xbf, 0x37,
  648. 0x1f, 0x4f, 0x56, 0x1e, 0xd8, 0x62, 0x32, 0x5c, 0xf5, 0x37, 0x75, 0x20,
  649. 0xe2, 0x7e, 0x56, 0x82, 0xc6, 0x35, 0xd3, 0x4d, 0xfa, 0x6c, 0xc3, 0x93,
  650. 0xf0, 0x60, 0x53, 0x78, 0x95, 0xee, 0xf9, 0x8b, 0x2c, 0xaf, 0xb1, 0x47,
  651. 0x5c, 0x29, 0x0d, 0x2a, 0x47, 0x7f, 0xd0, 0x7a, 0x4e, 0x26, 0x7b, 0x47,
  652. 0xfb, 0x61};
  653. static unsigned char dmp1_data[] =
  654. {0x01, 0x13, 0x3a, 0x1f, 0x91, 0x92, 0xa3, 0x8c, 0xfb, 0x7a, 0x6b, 0x40,
  655. 0x68, 0x4e, 0xd3, 0xcf, 0xdc, 0x16, 0xb9, 0x88, 0xe1, 0x49, 0x8d, 0x05,
  656. 0x78, 0x30, 0xfc, 0x3a, 0x70, 0xf2, 0x51, 0x06, 0x1f, 0xc7, 0xe8, 0x13,
  657. 0x19, 0x4b, 0x51, 0xb1, 0x79, 0xc2, 0x96, 0xc4, 0x00, 0xdb, 0x9d, 0x68,
  658. 0xec, 0xb9, 0x4a, 0x4b, 0x3b, 0xae, 0x91, 0x7f, 0xb5, 0xd7, 0x36, 0x82,
  659. 0x9d, 0x09, 0xfa, 0x97, 0x99, 0xe9, 0x73, 0x29, 0xb8, 0xf6, 0x6b, 0x8d,
  660. 0xd1, 0x15, 0xc5, 0x31, 0x4c, 0xe6, 0xb4, 0x7b, 0xa5, 0xd4, 0x08, 0xac,
  661. 0x9e, 0x41};
  662. static unsigned char dmq1_data[] =
  663. {0x05, 0xcd, 0x33, 0xc2, 0xdd, 0x3b, 0xb8, 0xec, 0xe4, 0x4c, 0x03, 0xcc,
  664. 0xef, 0xba, 0x07, 0x22, 0xca, 0x47, 0x77, 0x18, 0x40, 0x50, 0xe5, 0xfb,
  665. 0xc5, 0xb5, 0x71, 0xed, 0x3e, 0xd5, 0x5d, 0x72, 0xa7, 0x37, 0xa8, 0x86,
  666. 0x48, 0xa6, 0x27, 0x74, 0x42, 0x66, 0xd8, 0xf1, 0xfb, 0xcf, 0x1d, 0x4e,
  667. 0xee, 0x15, 0x76, 0x23, 0x5e, 0x81, 0x6c, 0xa7, 0x2b, 0x74, 0x08, 0xf7,
  668. 0x4c, 0x71, 0x9d, 0xa2, 0x29, 0x7f, 0xca, 0xd5, 0x02, 0x31, 0x2c, 0x54,
  669. 0x18, 0x02, 0xb6, 0xa8, 0x65, 0x26, 0xfc, 0xf8, 0x9b, 0x80, 0x90, 0xfc,
  670. 0x75, 0x61};
  671. static unsigned char iqmp_data[] =
  672. {0x05, 0x78, 0xf8, 0xdd, 0x1c, 0x6f, 0x3d, 0xaf, 0x53, 0x84, 0x32, 0xa9,
  673. 0x35, 0x52, 0xf3, 0xd0, 0x4d, 0xf8, 0x09, 0x85, 0x3d, 0x72, 0x20, 0x8b,
  674. 0x47, 0xba, 0xc8, 0xce, 0xac, 0xd9, 0x76, 0x90, 0x05, 0x88, 0x63, 0x8a,
  675. 0x10, 0x2b, 0xcd, 0xd3, 0xbe, 0x8c, 0x16, 0x60, 0x6a, 0xfd, 0xce, 0xc7,
  676. 0x9f, 0xfa, 0xbb, 0xe3, 0xa6, 0xde, 0xc2, 0x8f, 0x1d, 0x25, 0xdc, 0x41,
  677. 0xcb, 0xa4, 0xeb, 0x76, 0xc9, 0xdc, 0x8e, 0x49, 0x0e, 0xe4, 0x7c, 0xd2,
  678. 0xd5, 0x6e, 0x26, 0x3c, 0x0b, 0xd3, 0xc5, 0x20, 0x4e, 0x4b, 0xb6, 0xf7,
  679. 0xae, 0xef};
  680. static unsigned char exp3_data[] =
  681. {0x02, 0x7d, 0x16, 0x24, 0xfc, 0x35, 0xf9, 0xd0, 0xb3, 0x02, 0xf2, 0x5f,
  682. 0xde, 0xeb, 0x27, 0x19, 0x85, 0xd0, 0xcb, 0xe4, 0x0a, 0x2f, 0x13, 0xdb,
  683. 0xd5, 0xba, 0xe0, 0x8c, 0x32, 0x8b, 0x97, 0xdd, 0xef, 0xbc, 0xe0, 0x7a,
  684. 0x2d, 0x90, 0x7e, 0x09, 0xe9, 0x1f, 0x26, 0xf2, 0xf4, 0x48, 0xea, 0x06,
  685. 0x76, 0x26, 0xe6, 0x3b, 0xce, 0x4e, 0xc9, 0xf9, 0x0f, 0x38, 0x90, 0x26,
  686. 0x87, 0x65, 0x36, 0x9a, 0xea, 0x6a, 0xfe, 0xb1, 0xdb, 0x46, 0xdf, 0x14,
  687. 0xfd, 0x13, 0x53, 0xfb, 0x5b, 0x35, 0x6e, 0xe7, 0xd5, 0xd8, 0x39, 0xf7,
  688. 0x2d, 0xb9};
  689. static unsigned char coeff2_data[] =
  690. {0x01, 0xba, 0x66, 0x0a, 0xa2, 0x86, 0xc0, 0x57, 0x7f, 0x4e, 0x68, 0xb1,
  691. 0x86, 0x63, 0x23, 0x5b, 0x0e, 0xeb, 0x93, 0x42, 0xd1, 0xaa, 0x15, 0x13,
  692. 0xcc, 0x29, 0x71, 0x8a, 0xb0, 0xe0, 0xc9, 0x67, 0xde, 0x1a, 0x7c, 0x1a,
  693. 0xef, 0xa7, 0x08, 0x85, 0xb3, 0xae, 0x98, 0x99, 0xde, 0xaf, 0x09, 0x38,
  694. 0xfc, 0x46, 0x29, 0x5f, 0x4f, 0x7e, 0x01, 0x6c, 0x50, 0x13, 0x95, 0x91,
  695. 0x4c, 0x0f, 0x00, 0xba, 0xca, 0x40, 0xa3, 0xd0, 0x58, 0xb6, 0x62, 0x4c,
  696. 0xd1, 0xb6, 0xd3, 0x29, 0x5d, 0x82, 0xb3, 0x3d, 0x61, 0xbe, 0x5d, 0xf0,
  697. 0x4b, 0xf4};
  698. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  699. || !TEST_ptr(n = BN_bin2bn(n_data, sizeof(n_data), NULL))
  700. || !TEST_ptr(e = BN_bin2bn(e_data, sizeof(e_data), NULL))
  701. || !TEST_ptr(d = BN_bin2bn(d_data, sizeof(d_data), NULL))
  702. || !TEST_ptr(p = BN_bin2bn(p_data, sizeof(p_data), NULL))
  703. || !TEST_ptr(q = BN_bin2bn(q_data, sizeof(q_data), NULL))
  704. || !TEST_ptr(p2 = BN_bin2bn(p2_data, sizeof(p2_data), NULL))
  705. || !TEST_ptr(exp3 = BN_bin2bn(exp3_data, sizeof(exp3_data), NULL))
  706. || !TEST_ptr(coeff2 = BN_bin2bn(coeff2_data, sizeof(coeff2_data), NULL))
  707. || !TEST_ptr(dmp1 = BN_bin2bn(dmp1_data, sizeof(dmp1_data), NULL))
  708. || !TEST_ptr(dmq1 = BN_bin2bn(dmq1_data, sizeof(dmq1_data), NULL))
  709. || !TEST_ptr(iqmp = BN_bin2bn(iqmp_data, sizeof(iqmp_data), NULL))
  710. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n))
  711. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e))
  712. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d))
  713. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_FACTOR1,
  714. p))
  715. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_FACTOR2,
  716. q))
  717. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_FACTOR3,
  718. p2))
  719. || !TEST_true(OSSL_PARAM_BLD_push_int(bld,
  720. OSSL_PKEY_PARAM_RSA_DERIVE_FROM_PQ, 1))
  721. || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)))
  722. goto err;
  723. cdata[0].pname = OSSL_PKEY_PARAM_RSA_EXPONENT1;
  724. cdata[0].comparebn = dmp1;
  725. cdata[1].pname = OSSL_PKEY_PARAM_RSA_EXPONENT2;
  726. cdata[1].comparebn = dmq1;
  727. cdata[2].pname = OSSL_PKEY_PARAM_RSA_COEFFICIENT1;
  728. cdata[2].comparebn = iqmp;
  729. cdata[3].pname = OSSL_PKEY_PARAM_RSA_EXPONENT3;
  730. cdata[3].comparebn = exp3;
  731. cdata[4].pname = OSSL_PKEY_PARAM_RSA_COEFFICIENT2;
  732. cdata[4].comparebn = coeff2;
  733. cdata[5].pname = OSSL_PKEY_PARAM_RSA_N;
  734. cdata[5].comparebn = n;
  735. cdata[6].pname = OSSL_PKEY_PARAM_RSA_E;
  736. cdata[6].comparebn = e;
  737. cdata[7].pname = OSSL_PKEY_PARAM_RSA_D;
  738. cdata[7].comparebn = d;
  739. cdata[8].pname = OSSL_PKEY_PARAM_RSA_FACTOR1;
  740. cdata[8].comparebn = p;
  741. cdata[9].pname = OSSL_PKEY_PARAM_RSA_FACTOR2;
  742. cdata[9].comparebn = q;
  743. cdata[10].pname = OSSL_PKEY_PARAM_RSA_FACTOR3;
  744. cdata[10].comparebn = p2;
  745. cdata[11].pname = NULL;
  746. cdata[11].comparebn = NULL;
  747. ret = do_fromdata_rsa_derive(fromdata_params, cdata, 2048, 112, 256);
  748. err:
  749. BN_free(n);
  750. BN_free(e);
  751. BN_free(d);
  752. BN_free(p);
  753. BN_free(p2);
  754. BN_free(q);
  755. BN_free(dmp1);
  756. BN_free(dmq1);
  757. BN_free(iqmp);
  758. BN_free(exp3);
  759. BN_free(coeff2);
  760. OSSL_PARAM_BLD_free(bld);
  761. return ret;
  762. }
  763. static int test_evp_pkey_get_bn_param_large(void)
  764. {
  765. int ret = 0;
  766. EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
  767. EVP_PKEY *pk = NULL;
  768. OSSL_PARAM_BLD *bld = NULL;
  769. OSSL_PARAM *fromdata_params = NULL;
  770. BIGNUM *n = NULL, *e = NULL, *d = NULL, *n_out = NULL;
  771. /*
  772. * The buffer size chosen here for n_data larger than the buffer used
  773. * internally in EVP_PKEY_get_bn_param.
  774. */
  775. static unsigned char n_data[2050];
  776. static const unsigned char e_data[] = {
  777. 0x1, 0x00, 0x01
  778. };
  779. static const unsigned char d_data[] = {
  780. 0x99, 0x33, 0x13, 0x7b
  781. };
  782. /* N is a large buffer */
  783. memset(n_data, 0xCE, sizeof(n_data));
  784. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  785. || !TEST_ptr(n = BN_bin2bn(n_data, sizeof(n_data), NULL))
  786. || !TEST_ptr(e = BN_bin2bn(e_data, sizeof(e_data), NULL))
  787. || !TEST_ptr(d = BN_bin2bn(d_data, sizeof(d_data), NULL))
  788. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n))
  789. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e))
  790. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d))
  791. || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld))
  792. || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL))
  793. || !TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
  794. || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR,
  795. fromdata_params), 1)
  796. || !TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, ""))
  797. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_RSA_N, &n_out))
  798. || !TEST_BN_eq(n, n_out))
  799. goto err;
  800. ret = 1;
  801. err:
  802. BN_free(n_out);
  803. BN_free(n);
  804. BN_free(e);
  805. BN_free(d);
  806. EVP_PKEY_free(pk);
  807. EVP_PKEY_CTX_free(key_ctx);
  808. EVP_PKEY_CTX_free(ctx);
  809. OSSL_PARAM_free(fromdata_params);
  810. OSSL_PARAM_BLD_free(bld);
  811. return ret;
  812. }
  813. #ifndef OPENSSL_NO_DH
  814. static int test_fromdata_dh_named_group(void)
  815. {
  816. int ret = 0;
  817. int gindex = 0, pcounter = 0, hindex = 0;
  818. EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
  819. EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL;
  820. size_t len;
  821. BIGNUM *pub = NULL, *priv = NULL;
  822. BIGNUM *pub_out = NULL, *priv_out = NULL;
  823. BIGNUM *p = NULL, *q = NULL, *g = NULL, *j = NULL;
  824. OSSL_PARAM *fromdata_params = NULL;
  825. OSSL_PARAM_BLD *bld = NULL;
  826. char name_out[80];
  827. unsigned char seed_out[32];
  828. /*
  829. * DH key data was generated using the following:
  830. * openssl genpkey -algorithm DH -pkeyopt group:ffdhe2048
  831. * -pkeyopt priv_len:224 -text
  832. */
  833. static const unsigned char priv_data[] = {
  834. 0x88, 0x85, 0xe7, 0x9f, 0xee, 0x6d, 0xc5, 0x7c, 0x78, 0xaf, 0x63, 0x5d,
  835. 0x38, 0x2a, 0xd0, 0xed, 0x56, 0x4b, 0x47, 0x21, 0x2b, 0xfa, 0x55, 0xfa,
  836. 0x87, 0xe8, 0xa9, 0x7b,
  837. };
  838. static const unsigned char pub_data[] = {
  839. 0x00, 0xd6, 0x2d, 0x77, 0xe0, 0xd3, 0x7d, 0xf8, 0xeb, 0x98, 0x50, 0xa1,
  840. 0x82, 0x22, 0x65, 0xd5, 0xd9, 0xfe, 0xc9, 0x3f, 0xbe, 0x16, 0x83, 0xbd,
  841. 0x33, 0xe9, 0xc6, 0x93, 0xcf, 0x08, 0xaf, 0x83, 0xfa, 0x80, 0x8a, 0x6c,
  842. 0x64, 0xdf, 0x70, 0x64, 0xd5, 0x0a, 0x7c, 0x5a, 0x72, 0xda, 0x66, 0xe6,
  843. 0xf9, 0xf5, 0x31, 0x21, 0x92, 0xb0, 0x60, 0x1a, 0xb5, 0xd3, 0xf0, 0xa5,
  844. 0xfa, 0x48, 0x95, 0x2e, 0x38, 0xd9, 0xc5, 0xe6, 0xda, 0xfb, 0x6c, 0x03,
  845. 0x9d, 0x4b, 0x69, 0xb7, 0x95, 0xe4, 0x5c, 0xc0, 0x93, 0x4f, 0x48, 0xd9,
  846. 0x7e, 0x06, 0x22, 0xb2, 0xde, 0xf3, 0x79, 0x24, 0xed, 0xe1, 0xd1, 0x4a,
  847. 0x57, 0xf1, 0x40, 0x86, 0x70, 0x42, 0x25, 0xc5, 0x27, 0x68, 0xc9, 0xfa,
  848. 0xe5, 0x8e, 0x62, 0x7e, 0xff, 0x49, 0x6c, 0x5b, 0xb5, 0xba, 0xf9, 0xef,
  849. 0x9a, 0x1a, 0x10, 0xd4, 0x81, 0x53, 0xcf, 0x83, 0x04, 0x18, 0x1c, 0xe1,
  850. 0xdb, 0xe1, 0x65, 0xa9, 0x7f, 0xe1, 0x33, 0xeb, 0xc3, 0x4f, 0xe3, 0xb7,
  851. 0x22, 0xf7, 0x1c, 0x09, 0x4f, 0xed, 0xc6, 0x07, 0x8e, 0x78, 0x05, 0x8f,
  852. 0x7c, 0x96, 0xd9, 0x12, 0xe0, 0x81, 0x74, 0x1a, 0xe9, 0x13, 0xc0, 0x20,
  853. 0x82, 0x65, 0xbb, 0x42, 0x3b, 0xed, 0x08, 0x6a, 0x84, 0x4f, 0xea, 0x77,
  854. 0x14, 0x32, 0xf9, 0xed, 0xc2, 0x12, 0xd6, 0xc5, 0xc6, 0xb3, 0xe5, 0xf2,
  855. 0x6e, 0xf6, 0x16, 0x7f, 0x37, 0xde, 0xbc, 0x09, 0xc7, 0x06, 0x6b, 0x12,
  856. 0xbc, 0xad, 0x2d, 0x49, 0x25, 0xd5, 0xdc, 0xf4, 0x18, 0x14, 0xd2, 0xf0,
  857. 0xf1, 0x1d, 0x1f, 0x3a, 0xaa, 0x15, 0x55, 0xbb, 0x0d, 0x7f, 0xbe, 0x67,
  858. 0xa1, 0xa7, 0xf0, 0xaa, 0xb3, 0xfb, 0x41, 0x82, 0x39, 0x49, 0x93, 0xbc,
  859. 0xa8, 0xee, 0x72, 0x13, 0x45, 0x65, 0x15, 0x42, 0x17, 0xaa, 0xd8, 0xab,
  860. 0xcf, 0x33, 0x42, 0x83, 0x42
  861. };
  862. static const char group_name[] = "ffdhe2048";
  863. static const long priv_len = 224;
  864. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  865. || !TEST_ptr(pub = BN_bin2bn(pub_data, sizeof(pub_data), NULL))
  866. || !TEST_ptr(priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL))
  867. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
  868. OSSL_PKEY_PARAM_GROUP_NAME,
  869. group_name, 0))
  870. || !TEST_true(OSSL_PARAM_BLD_push_long(bld, OSSL_PKEY_PARAM_DH_PRIV_LEN,
  871. priv_len))
  872. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, pub))
  873. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv))
  874. || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)))
  875. goto err;
  876. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL)))
  877. goto err;
  878. if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
  879. || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR,
  880. fromdata_params), 1))
  881. goto err;
  882. /*
  883. * A few extra checks of EVP_PKEY_get_utf8_string_param() to see that
  884. * it behaves as expected with regards to string length and terminating
  885. * NUL byte.
  886. */
  887. if (!TEST_true(EVP_PKEY_get_utf8_string_param(pk,
  888. OSSL_PKEY_PARAM_GROUP_NAME,
  889. NULL, sizeof(name_out),
  890. &len))
  891. || !TEST_size_t_eq(len, sizeof(group_name) - 1)
  892. /* Just enough space to hold the group name and a terminating NUL */
  893. || !TEST_true(EVP_PKEY_get_utf8_string_param(pk,
  894. OSSL_PKEY_PARAM_GROUP_NAME,
  895. name_out,
  896. sizeof(group_name),
  897. &len))
  898. || !TEST_size_t_eq(len, sizeof(group_name) - 1)
  899. /* Too small buffer to hold the terminating NUL byte */
  900. || !TEST_false(EVP_PKEY_get_utf8_string_param(pk,
  901. OSSL_PKEY_PARAM_GROUP_NAME,
  902. name_out,
  903. sizeof(group_name) - 1,
  904. &len))
  905. /* Too small buffer to hold the whole group name, even! */
  906. || !TEST_false(EVP_PKEY_get_utf8_string_param(pk,
  907. OSSL_PKEY_PARAM_GROUP_NAME,
  908. name_out,
  909. sizeof(group_name) - 2,
  910. &len)))
  911. goto err;
  912. for (;;) {
  913. ret = 0;
  914. if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 2048)
  915. || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 112)
  916. || !TEST_int_eq(EVP_PKEY_get_size(pk), 256)
  917. || !TEST_false(EVP_PKEY_missing_parameters(pk)))
  918. goto err;
  919. if (!TEST_true(EVP_PKEY_get_utf8_string_param(pk,
  920. OSSL_PKEY_PARAM_GROUP_NAME,
  921. name_out,
  922. sizeof(name_out),
  923. &len))
  924. || !TEST_str_eq(name_out, group_name)
  925. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PUB_KEY,
  926. &pub_out))
  927. || !TEST_BN_eq(pub, pub_out)
  928. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY,
  929. &priv_out))
  930. || !TEST_BN_eq(priv, priv_out)
  931. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_P, &p))
  932. || !TEST_BN_eq(&ossl_bignum_ffdhe2048_p, p)
  933. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_Q, &q))
  934. || !TEST_ptr(q)
  935. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_G, &g))
  936. || !TEST_BN_eq(&ossl_bignum_const_2, g)
  937. || !TEST_false(EVP_PKEY_get_bn_param(pk,
  938. OSSL_PKEY_PARAM_FFC_COFACTOR,
  939. &j))
  940. || !TEST_ptr_null(j)
  941. || !TEST_false(EVP_PKEY_get_octet_string_param(pk,
  942. OSSL_PKEY_PARAM_FFC_SEED,
  943. seed_out,
  944. sizeof(seed_out),
  945. &len))
  946. || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_GINDEX,
  947. &gindex))
  948. || !TEST_int_eq(gindex, -1)
  949. || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_H,
  950. &hindex))
  951. || !TEST_int_eq(hindex, 0)
  952. || !TEST_true(EVP_PKEY_get_int_param(pk,
  953. OSSL_PKEY_PARAM_FFC_PCOUNTER,
  954. &pcounter))
  955. || !TEST_int_eq(pcounter, -1))
  956. goto err;
  957. BN_free(p);
  958. p = NULL;
  959. BN_free(q);
  960. q = NULL;
  961. BN_free(g);
  962. g = NULL;
  963. BN_free(j);
  964. j = NULL;
  965. BN_free(pub_out);
  966. pub_out = NULL;
  967. BN_free(priv_out);
  968. priv_out = NULL;
  969. if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, "")))
  970. goto err;
  971. if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0)
  972. || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0)
  973. || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0)
  974. || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0))
  975. goto err;
  976. EVP_PKEY_CTX_free(key_ctx);
  977. key_ctx = NULL;
  978. if (!TEST_ptr(copy_pk = EVP_PKEY_new())
  979. || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk)))
  980. goto err;
  981. EVP_PKEY_free(copy_pk);
  982. copy_pk = NULL;
  983. ret = test_print_key_using_pem("DH", pk)
  984. && test_print_key_using_encoder("DH", pk);
  985. if (!ret || dup_pk != NULL)
  986. break;
  987. if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
  988. goto err;
  989. ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
  990. EVP_PKEY_free(pk);
  991. pk = dup_pk;
  992. if (!ret)
  993. goto err;
  994. }
  995. err:
  996. BN_free(p);
  997. BN_free(q);
  998. BN_free(g);
  999. BN_free(j);
  1000. BN_free(pub);
  1001. BN_free(priv);
  1002. BN_free(pub_out);
  1003. BN_free(priv_out);
  1004. EVP_PKEY_free(copy_pk);
  1005. EVP_PKEY_free(pk);
  1006. EVP_PKEY_CTX_free(ctx);
  1007. EVP_PKEY_CTX_free(key_ctx);
  1008. OSSL_PARAM_free(fromdata_params);
  1009. OSSL_PARAM_BLD_free(bld);
  1010. return ret;
  1011. }
  1012. static int test_fromdata_dh_fips186_4(void)
  1013. {
  1014. int ret = 0;
  1015. int gindex = 0, pcounter = 0, hindex = 0;
  1016. EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
  1017. EVP_PKEY *pk = NULL, *dup_pk = NULL;
  1018. size_t len;
  1019. BIGNUM *pub = NULL, *priv = NULL;
  1020. BIGNUM *pub_out = NULL, *priv_out = NULL;
  1021. BIGNUM *p = NULL, *q = NULL, *g = NULL, *j = NULL;
  1022. OSSL_PARAM_BLD *bld = NULL;
  1023. OSSL_PARAM *fromdata_params = NULL;
  1024. char name_out[80];
  1025. unsigned char seed_out[32];
  1026. /*
  1027. * DH key data was generated using the following:
  1028. * openssl genpkey -algorithm DH
  1029. * -pkeyopt group:ffdhe2048 -pkeyopt priv_len:224 -text
  1030. */
  1031. static const unsigned char priv_data[] = {
  1032. 0x88, 0x85, 0xe7, 0x9f, 0xee, 0x6d, 0xc5, 0x7c, 0x78, 0xaf, 0x63, 0x5d,
  1033. 0x38, 0x2a, 0xd0, 0xed, 0x56, 0x4b, 0x47, 0x21, 0x2b, 0xfa, 0x55, 0xfa,
  1034. 0x87, 0xe8, 0xa9, 0x7b,
  1035. };
  1036. static const unsigned char pub_data[] = {
  1037. 0xd6, 0x2d, 0x77, 0xe0, 0xd3, 0x7d, 0xf8, 0xeb, 0x98, 0x50, 0xa1, 0x82,
  1038. 0x22, 0x65, 0xd5, 0xd9, 0xfe, 0xc9, 0x3f, 0xbe, 0x16, 0x83, 0xbd, 0x33,
  1039. 0xe9, 0xc6, 0x93, 0xcf, 0x08, 0xaf, 0x83, 0xfa, 0x80, 0x8a, 0x6c, 0x64,
  1040. 0xdf, 0x70, 0x64, 0xd5, 0x0a, 0x7c, 0x5a, 0x72, 0xda, 0x66, 0xe6, 0xf9,
  1041. 0xf5, 0x31, 0x21, 0x92, 0xb0, 0x60, 0x1a, 0xb5, 0xd3, 0xf0, 0xa5, 0xfa,
  1042. 0x48, 0x95, 0x2e, 0x38, 0xd9, 0xc5, 0xe6, 0xda, 0xfb, 0x6c, 0x03, 0x9d,
  1043. 0x4b, 0x69, 0xb7, 0x95, 0xe4, 0x5c, 0xc0, 0x93, 0x4f, 0x48, 0xd9, 0x7e,
  1044. 0x06, 0x22, 0xb2, 0xde, 0xf3, 0x79, 0x24, 0xed, 0xe1, 0xd1, 0x4a, 0x57,
  1045. 0xf1, 0x40, 0x86, 0x70, 0x42, 0x25, 0xc5, 0x27, 0x68, 0xc9, 0xfa, 0xe5,
  1046. 0x8e, 0x62, 0x7e, 0xff, 0x49, 0x6c, 0x5b, 0xb5, 0xba, 0xf9, 0xef, 0x9a,
  1047. 0x1a, 0x10, 0xd4, 0x81, 0x53, 0xcf, 0x83, 0x04, 0x18, 0x1c, 0xe1, 0xdb,
  1048. 0xe1, 0x65, 0xa9, 0x7f, 0xe1, 0x33, 0xeb, 0xc3, 0x4f, 0xe3, 0xb7, 0x22,
  1049. 0xf7, 0x1c, 0x09, 0x4f, 0xed, 0xc6, 0x07, 0x8e, 0x78, 0x05, 0x8f, 0x7c,
  1050. 0x96, 0xd9, 0x12, 0xe0, 0x81, 0x74, 0x1a, 0xe9, 0x13, 0xc0, 0x20, 0x82,
  1051. 0x65, 0xbb, 0x42, 0x3b, 0xed, 0x08, 0x6a, 0x84, 0x4f, 0xea, 0x77, 0x14,
  1052. 0x32, 0xf9, 0xed, 0xc2, 0x12, 0xd6, 0xc5, 0xc6, 0xb3, 0xe5, 0xf2, 0x6e,
  1053. 0xf6, 0x16, 0x7f, 0x37, 0xde, 0xbc, 0x09, 0xc7, 0x06, 0x6b, 0x12, 0xbc,
  1054. 0xad, 0x2d, 0x49, 0x25, 0xd5, 0xdc, 0xf4, 0x18, 0x14, 0xd2, 0xf0, 0xf1,
  1055. 0x1d, 0x1f, 0x3a, 0xaa, 0x15, 0x55, 0xbb, 0x0d, 0x7f, 0xbe, 0x67, 0xa1,
  1056. 0xa7, 0xf0, 0xaa, 0xb3, 0xfb, 0x41, 0x82, 0x39, 0x49, 0x93, 0xbc, 0xa8,
  1057. 0xee, 0x72, 0x13, 0x45, 0x65, 0x15, 0x42, 0x17, 0xaa, 0xd8, 0xab, 0xcf,
  1058. 0x33, 0x42, 0x83, 0x42
  1059. };
  1060. static const char group_name[] = "ffdhe2048";
  1061. static const long priv_len = 224;
  1062. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  1063. || !TEST_ptr(pub = BN_bin2bn(pub_data, sizeof(pub_data), NULL))
  1064. || !TEST_ptr(priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL))
  1065. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
  1066. OSSL_PKEY_PARAM_GROUP_NAME,
  1067. group_name, 0))
  1068. || !TEST_true(OSSL_PARAM_BLD_push_long(bld, OSSL_PKEY_PARAM_DH_PRIV_LEN,
  1069. priv_len))
  1070. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, pub))
  1071. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv))
  1072. || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)))
  1073. goto err;
  1074. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL)))
  1075. goto err;
  1076. if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
  1077. || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR,
  1078. fromdata_params), 1))
  1079. goto err;
  1080. for (;;) {
  1081. ret = 0;
  1082. if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 2048)
  1083. || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 112)
  1084. || !TEST_int_eq(EVP_PKEY_get_size(pk), 256)
  1085. || !TEST_false(EVP_PKEY_missing_parameters(pk)))
  1086. goto err;
  1087. if (!TEST_true(EVP_PKEY_get_utf8_string_param(pk,
  1088. OSSL_PKEY_PARAM_GROUP_NAME,
  1089. name_out,
  1090. sizeof(name_out),
  1091. &len))
  1092. || !TEST_str_eq(name_out, group_name)
  1093. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PUB_KEY,
  1094. &pub_out))
  1095. || !TEST_BN_eq(pub, pub_out)
  1096. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY,
  1097. &priv_out))
  1098. || !TEST_BN_eq(priv, priv_out)
  1099. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_P, &p))
  1100. || !TEST_BN_eq(&ossl_bignum_ffdhe2048_p, p)
  1101. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_Q, &q))
  1102. || !TEST_ptr(q)
  1103. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_G, &g))
  1104. || !TEST_BN_eq(&ossl_bignum_const_2, g)
  1105. || !TEST_false(EVP_PKEY_get_bn_param(pk,
  1106. OSSL_PKEY_PARAM_FFC_COFACTOR,
  1107. &j))
  1108. || !TEST_ptr_null(j)
  1109. || !TEST_false(EVP_PKEY_get_octet_string_param(pk,
  1110. OSSL_PKEY_PARAM_FFC_SEED,
  1111. seed_out,
  1112. sizeof(seed_out),
  1113. &len))
  1114. || !TEST_true(EVP_PKEY_get_int_param(pk,
  1115. OSSL_PKEY_PARAM_FFC_GINDEX,
  1116. &gindex))
  1117. || !TEST_int_eq(gindex, -1)
  1118. || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_H,
  1119. &hindex))
  1120. || !TEST_int_eq(hindex, 0)
  1121. || !TEST_true(EVP_PKEY_get_int_param(pk,
  1122. OSSL_PKEY_PARAM_FFC_PCOUNTER,
  1123. &pcounter))
  1124. || !TEST_int_eq(pcounter, -1))
  1125. goto err;
  1126. BN_free(p);
  1127. p = NULL;
  1128. BN_free(q);
  1129. q = NULL;
  1130. BN_free(g);
  1131. g = NULL;
  1132. BN_free(j);
  1133. j = NULL;
  1134. BN_free(pub_out);
  1135. pub_out = NULL;
  1136. BN_free(priv_out);
  1137. priv_out = NULL;
  1138. if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, "")))
  1139. goto err;
  1140. if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0)
  1141. || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0)
  1142. || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0)
  1143. || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0))
  1144. goto err;
  1145. EVP_PKEY_CTX_free(key_ctx);
  1146. key_ctx = NULL;
  1147. ret = test_print_key_using_pem("DH", pk)
  1148. && test_print_key_using_encoder("DH", pk);
  1149. if (!ret || dup_pk != NULL)
  1150. break;
  1151. if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
  1152. goto err;
  1153. ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
  1154. EVP_PKEY_free(pk);
  1155. pk = dup_pk;
  1156. if (!ret)
  1157. goto err;
  1158. }
  1159. err:
  1160. BN_free(p);
  1161. BN_free(q);
  1162. BN_free(g);
  1163. BN_free(j);
  1164. BN_free(pub);
  1165. BN_free(priv);
  1166. BN_free(pub_out);
  1167. BN_free(priv_out);
  1168. EVP_PKEY_free(pk);
  1169. EVP_PKEY_CTX_free(ctx);
  1170. EVP_PKEY_CTX_free(key_ctx);
  1171. OSSL_PARAM_free(fromdata_params);
  1172. OSSL_PARAM_BLD_free(bld);
  1173. return ret;
  1174. }
  1175. #endif
  1176. #ifndef OPENSSL_NO_EC
  1177. # ifndef OPENSSL_NO_ECX
  1178. /* Array indexes used in test_fromdata_ecx */
  1179. # define PRIV_KEY 0
  1180. # define PUB_KEY 1
  1181. # define X25519_IDX 0
  1182. # define X448_IDX 1
  1183. # define ED25519_IDX 2
  1184. # define ED448_IDX 3
  1185. /*
  1186. * tst uses indexes 0 ... (3 * 4 - 1)
  1187. * For the 4 ECX key types (X25519_IDX..ED448_IDX)
  1188. * 0..3 = public + private key.
  1189. * 4..7 = private key (This will generate the public key from the private key)
  1190. * 8..11 = public key
  1191. */
  1192. static int test_fromdata_ecx(int tst)
  1193. {
  1194. int ret = 0;
  1195. EVP_PKEY_CTX *ctx = NULL, *ctx2 = NULL;
  1196. EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL;
  1197. const char *alg = NULL;
  1198. size_t len;
  1199. unsigned char out_pub[ED448_KEYLEN];
  1200. unsigned char out_priv[ED448_KEYLEN];
  1201. OSSL_PARAM params[3] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
  1202. /* ED448_KEYLEN > X448_KEYLEN > X25519_KEYLEN == ED25519_KEYLEN */
  1203. static unsigned char key_numbers[4][2][ED448_KEYLEN] = {
  1204. /* X25519: Keys from RFC 7748 6.1 */
  1205. {
  1206. /* Private Key */
  1207. {
  1208. 0x77, 0x07, 0x6d, 0x0a, 0x73, 0x18, 0xa5, 0x7d, 0x3c, 0x16,
  1209. 0xc1, 0x72, 0x51, 0xb2, 0x66, 0x45, 0xdf, 0x4c, 0x2f, 0x87,
  1210. 0xeb, 0xc0, 0x99, 0x2a, 0xb1, 0x77, 0xfb, 0xa5, 0x1d, 0xb9,
  1211. 0x2c, 0x2a
  1212. },
  1213. /* Public Key */
  1214. {
  1215. 0x85, 0x20, 0xf0, 0x09, 0x89, 0x30, 0xa7, 0x54, 0x74, 0x8b,
  1216. 0x7d, 0xdc, 0xb4, 0x3e, 0xf7, 0x5a, 0x0d, 0xbf, 0x3a, 0x0d,
  1217. 0x26, 0x38, 0x1a, 0xf4, 0xeb, 0xa4, 0xa9, 0x8e, 0xaa, 0x9b,
  1218. 0x4e, 0x6a
  1219. }
  1220. },
  1221. /* X448: Keys from RFC 7748 6.2 */
  1222. {
  1223. /* Private Key */
  1224. {
  1225. 0x9a, 0x8f, 0x49, 0x25, 0xd1, 0x51, 0x9f, 0x57, 0x75, 0xcf,
  1226. 0x46, 0xb0, 0x4b, 0x58, 0x00, 0xd4, 0xee, 0x9e, 0xe8, 0xba,
  1227. 0xe8, 0xbc, 0x55, 0x65, 0xd4, 0x98, 0xc2, 0x8d, 0xd9, 0xc9,
  1228. 0xba, 0xf5, 0x74, 0xa9, 0x41, 0x97, 0x44, 0x89, 0x73, 0x91,
  1229. 0x00, 0x63, 0x82, 0xa6, 0xf1, 0x27, 0xab, 0x1d, 0x9a, 0xc2,
  1230. 0xd8, 0xc0, 0xa5, 0x98, 0x72, 0x6b
  1231. },
  1232. /* Public Key */
  1233. {
  1234. 0x9b, 0x08, 0xf7, 0xcc, 0x31, 0xb7, 0xe3, 0xe6, 0x7d, 0x22,
  1235. 0xd5, 0xae, 0xa1, 0x21, 0x07, 0x4a, 0x27, 0x3b, 0xd2, 0xb8,
  1236. 0x3d, 0xe0, 0x9c, 0x63, 0xfa, 0xa7, 0x3d, 0x2c, 0x22, 0xc5,
  1237. 0xd9, 0xbb, 0xc8, 0x36, 0x64, 0x72, 0x41, 0xd9, 0x53, 0xd4,
  1238. 0x0c, 0x5b, 0x12, 0xda, 0x88, 0x12, 0x0d, 0x53, 0x17, 0x7f,
  1239. 0x80, 0xe5, 0x32, 0xc4, 0x1f, 0xa0
  1240. }
  1241. },
  1242. /* ED25519: Keys from RFC 8032 */
  1243. {
  1244. /* Private Key */
  1245. {
  1246. 0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60, 0xba, 0x84,
  1247. 0x4a, 0xf4, 0x92, 0xec, 0x2c, 0xc4, 0x44, 0x49, 0xc5, 0x69,
  1248. 0x7b, 0x32, 0x69, 0x19, 0x70, 0x3b, 0xac, 0x03, 0x1c, 0xae,
  1249. 0x7f, 0x60
  1250. },
  1251. /* Public Key */
  1252. {
  1253. 0xd7, 0x5a, 0x98, 0x01, 0x82, 0xb1, 0x0a, 0xb7, 0xd5, 0x4b,
  1254. 0xfe, 0xd3, 0xc9, 0x64, 0x07, 0x3a, 0x0e, 0xe1, 0x72, 0xf3,
  1255. 0xda, 0xa6, 0x23, 0x25, 0xaf, 0x02, 0x1a, 0x68, 0xf7, 0x07,
  1256. 0x51, 0x1a
  1257. }
  1258. },
  1259. /* ED448: Keys from RFC 8032 */
  1260. {
  1261. /* Private Key */
  1262. {
  1263. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10, 0xd6, 0x32,
  1264. 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf, 0x6c, 0x92, 0x9f, 0x34,
  1265. 0xdd, 0xfa, 0x8c, 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3,
  1266. 0x48, 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  1267. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f, 0x03, 0x2e,
  1268. 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9, 0x5b
  1269. },
  1270. /* Public Key */
  1271. {
  1272. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7,
  1273. 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  1274. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9,
  1275. 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  1276. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa,
  1277. 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61, 0x80
  1278. }
  1279. }
  1280. };
  1281. OSSL_PARAM x25519_fromdata_params[] = {
  1282. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
  1283. key_numbers[X25519_IDX][PRIV_KEY],
  1284. X25519_KEYLEN),
  1285. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
  1286. key_numbers[X25519_IDX][PUB_KEY],
  1287. X25519_KEYLEN),
  1288. OSSL_PARAM_END
  1289. };
  1290. OSSL_PARAM x448_fromdata_params[] = {
  1291. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
  1292. key_numbers[X448_IDX][PRIV_KEY],
  1293. X448_KEYLEN),
  1294. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
  1295. key_numbers[X448_IDX][PUB_KEY],
  1296. X448_KEYLEN),
  1297. OSSL_PARAM_END
  1298. };
  1299. OSSL_PARAM ed25519_fromdata_params[] = {
  1300. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
  1301. key_numbers[ED25519_IDX][PRIV_KEY],
  1302. ED25519_KEYLEN),
  1303. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
  1304. key_numbers[ED25519_IDX][PUB_KEY],
  1305. ED25519_KEYLEN),
  1306. OSSL_PARAM_END
  1307. };
  1308. OSSL_PARAM ed448_fromdata_params[] = {
  1309. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
  1310. key_numbers[ED448_IDX][PRIV_KEY],
  1311. ED448_KEYLEN),
  1312. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
  1313. key_numbers[ED448_IDX][PUB_KEY],
  1314. ED448_KEYLEN),
  1315. OSSL_PARAM_END
  1316. };
  1317. OSSL_PARAM *fromdata_params = NULL;
  1318. int bits = 0, security_bits = 0, size = 0;
  1319. OSSL_PARAM *orig_fromdata_params = NULL;
  1320. switch (tst & 3) {
  1321. case X25519_IDX:
  1322. fromdata_params = x25519_fromdata_params;
  1323. bits = X25519_BITS;
  1324. security_bits = X25519_SECURITY_BITS;
  1325. size = X25519_KEYLEN;
  1326. alg = "X25519";
  1327. break;
  1328. case X448_IDX:
  1329. fromdata_params = x448_fromdata_params;
  1330. bits = X448_BITS;
  1331. security_bits = X448_SECURITY_BITS;
  1332. size = X448_KEYLEN;
  1333. alg = "X448";
  1334. break;
  1335. case ED25519_IDX:
  1336. fromdata_params = ed25519_fromdata_params;
  1337. bits = ED25519_BITS;
  1338. security_bits = ED25519_SECURITY_BITS;
  1339. size = ED25519_SIGSIZE;
  1340. alg = "ED25519";
  1341. break;
  1342. case ED448_IDX:
  1343. fromdata_params = ed448_fromdata_params;
  1344. bits = ED448_BITS;
  1345. security_bits = ED448_SECURITY_BITS;
  1346. size = ED448_SIGSIZE;
  1347. alg = "ED448";
  1348. break;
  1349. default:
  1350. goto err;
  1351. }
  1352. ctx = EVP_PKEY_CTX_new_from_name(NULL, alg, NULL);
  1353. if (!TEST_ptr(ctx))
  1354. goto err;
  1355. orig_fromdata_params = fromdata_params;
  1356. if (tst > 7) {
  1357. /* public key only */
  1358. fromdata_params++;
  1359. } else if (tst > 3) {
  1360. /* private key only */
  1361. params[0] = fromdata_params[0];
  1362. params[1] = fromdata_params[2];
  1363. fromdata_params = params;
  1364. }
  1365. if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
  1366. || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR,
  1367. fromdata_params), 1))
  1368. goto err;
  1369. for (;;) {
  1370. ret = 0;
  1371. if (!TEST_int_eq(EVP_PKEY_get_bits(pk), bits)
  1372. || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), security_bits)
  1373. || !TEST_int_eq(EVP_PKEY_get_size(pk), size)
  1374. || !TEST_false(EVP_PKEY_missing_parameters(pk)))
  1375. goto err;
  1376. if (!TEST_ptr(ctx2 = EVP_PKEY_CTX_new_from_pkey(NULL, pk, NULL)))
  1377. goto err;
  1378. if (tst <= 7) {
  1379. if (!TEST_int_gt(EVP_PKEY_check(ctx2), 0))
  1380. goto err;
  1381. if (!TEST_true(EVP_PKEY_get_octet_string_param(
  1382. pk, orig_fromdata_params[PRIV_KEY].key,
  1383. out_priv, sizeof(out_priv), &len))
  1384. || !TEST_mem_eq(out_priv, len,
  1385. orig_fromdata_params[PRIV_KEY].data,
  1386. orig_fromdata_params[PRIV_KEY].data_size)
  1387. || !TEST_true(EVP_PKEY_get_octet_string_param(
  1388. pk, orig_fromdata_params[PUB_KEY].key,
  1389. out_pub, sizeof(out_pub), &len))
  1390. || !TEST_mem_eq(out_pub, len,
  1391. orig_fromdata_params[PUB_KEY].data,
  1392. orig_fromdata_params[PUB_KEY].data_size))
  1393. goto err;
  1394. } else {
  1395. /* The private key check should fail if there is only a public key */
  1396. if (!TEST_int_gt(EVP_PKEY_public_check(ctx2), 0)
  1397. || !TEST_int_le(EVP_PKEY_private_check(ctx2), 0)
  1398. || !TEST_int_le(EVP_PKEY_check(ctx2), 0))
  1399. goto err;
  1400. }
  1401. EVP_PKEY_CTX_free(ctx2);
  1402. ctx2 = NULL;
  1403. if (!TEST_ptr(copy_pk = EVP_PKEY_new())
  1404. /* This should succeed because there are no parameters to copy */
  1405. || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk)))
  1406. goto err;
  1407. if (!TEST_ptr(ctx2 = EVP_PKEY_CTX_new_from_pkey(NULL, copy_pk, NULL))
  1408. /* This should fail because copy_pk has no pubkey */
  1409. || !TEST_int_le(EVP_PKEY_public_check(ctx2), 0))
  1410. goto err;
  1411. EVP_PKEY_CTX_free(ctx2);
  1412. ctx2 = NULL;
  1413. EVP_PKEY_free(copy_pk);
  1414. copy_pk = NULL;
  1415. if (tst > 7)
  1416. ret = test_print_key_using_encoder_public(alg, pk);
  1417. else
  1418. ret = test_print_key_using_pem(alg, pk)
  1419. && test_print_key_using_encoder(alg, pk);
  1420. if (!ret || dup_pk != NULL)
  1421. break;
  1422. if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
  1423. goto err;
  1424. ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
  1425. EVP_PKEY_free(pk);
  1426. pk = dup_pk;
  1427. if (!ret)
  1428. goto err;
  1429. }
  1430. err:
  1431. EVP_PKEY_free(pk);
  1432. EVP_PKEY_free(copy_pk);
  1433. EVP_PKEY_CTX_free(ctx);
  1434. EVP_PKEY_CTX_free(ctx2);
  1435. return ret;
  1436. }
  1437. # endif /* OPENSSL_NO_ECX */
  1438. static int test_fromdata_ec(void)
  1439. {
  1440. int ret = 0;
  1441. EVP_PKEY_CTX *ctx = NULL;
  1442. EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL;
  1443. OSSL_PARAM_BLD *bld = NULL;
  1444. BIGNUM *ec_priv_bn = NULL;
  1445. BIGNUM *bn_priv = NULL;
  1446. OSSL_PARAM *fromdata_params = NULL;
  1447. const char *alg = "EC";
  1448. const char *curve = "prime256v1";
  1449. const char bad_curve[] = "nonexistent-curve";
  1450. OSSL_PARAM nokey_params[2] = {
  1451. OSSL_PARAM_END,
  1452. OSSL_PARAM_END
  1453. };
  1454. /* UNCOMPRESSED FORMAT */
  1455. static const unsigned char ec_pub_keydata[] = {
  1456. POINT_CONVERSION_UNCOMPRESSED,
  1457. 0x1b, 0x93, 0x67, 0x55, 0x1c, 0x55, 0x9f, 0x63,
  1458. 0xd1, 0x22, 0xa4, 0xd8, 0xd1, 0x0a, 0x60, 0x6d,
  1459. 0x02, 0xa5, 0x77, 0x57, 0xc8, 0xa3, 0x47, 0x73,
  1460. 0x3a, 0x6a, 0x08, 0x28, 0x39, 0xbd, 0xc9, 0xd2,
  1461. 0x80, 0xec, 0xe9, 0xa7, 0x08, 0x29, 0x71, 0x2f,
  1462. 0xc9, 0x56, 0x82, 0xee, 0x9a, 0x85, 0x0f, 0x6d,
  1463. 0x7f, 0x59, 0x5f, 0x8c, 0xd1, 0x96, 0x0b, 0xdf,
  1464. 0x29, 0x3e, 0x49, 0x07, 0x88, 0x3f, 0x9a, 0x29
  1465. };
  1466. /* SAME BUT COMPRESSED FORMAT */
  1467. static const unsigned char ec_pub_keydata_compressed[] = {
  1468. POINT_CONVERSION_COMPRESSED+1,
  1469. 0x1b, 0x93, 0x67, 0x55, 0x1c, 0x55, 0x9f, 0x63,
  1470. 0xd1, 0x22, 0xa4, 0xd8, 0xd1, 0x0a, 0x60, 0x6d,
  1471. 0x02, 0xa5, 0x77, 0x57, 0xc8, 0xa3, 0x47, 0x73,
  1472. 0x3a, 0x6a, 0x08, 0x28, 0x39, 0xbd, 0xc9, 0xd2
  1473. };
  1474. static const unsigned char ec_priv_keydata[] = {
  1475. 0x33, 0xd0, 0x43, 0x83, 0xa9, 0x89, 0x56, 0x03,
  1476. 0xd2, 0xd7, 0xfe, 0x6b, 0x01, 0x6f, 0xe4, 0x59,
  1477. 0xcc, 0x0d, 0x9a, 0x24, 0x6c, 0x86, 0x1b, 0x2e,
  1478. 0xdc, 0x4b, 0x4d, 0x35, 0x43, 0xe1, 0x1b, 0xad
  1479. };
  1480. unsigned char out_pub[sizeof(ec_pub_keydata)];
  1481. char out_curve_name[80];
  1482. const OSSL_PARAM *gettable = NULL;
  1483. size_t len;
  1484. EC_GROUP *group = NULL;
  1485. BIGNUM *group_a = NULL;
  1486. BIGNUM *group_b = NULL;
  1487. BIGNUM *group_p = NULL;
  1488. BIGNUM *a = NULL;
  1489. BIGNUM *b = NULL;
  1490. BIGNUM *p = NULL;
  1491. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()))
  1492. goto err;
  1493. if (!TEST_ptr(ec_priv_bn = BN_bin2bn(ec_priv_keydata,
  1494. sizeof(ec_priv_keydata), NULL)))
  1495. goto err;
  1496. if (OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME,
  1497. curve, 0) <= 0)
  1498. goto err;
  1499. /*
  1500. * We intentionally provide the input point in compressed format,
  1501. * and avoid setting `OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT`.
  1502. *
  1503. * Later on we check what format is used when exporting the
  1504. * `OSSL_PKEY_PARAM_PUB_KEY` and expect to default to uncompressed
  1505. * format.
  1506. */
  1507. if (OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_PUB_KEY,
  1508. ec_pub_keydata_compressed,
  1509. sizeof(ec_pub_keydata_compressed)) <= 0)
  1510. goto err;
  1511. if (OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, ec_priv_bn) <= 0)
  1512. goto err;
  1513. if (!TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)))
  1514. goto err;
  1515. ctx = EVP_PKEY_CTX_new_from_name(NULL, alg, NULL);
  1516. if (!TEST_ptr(ctx))
  1517. goto err;
  1518. /* try importing parameters with bad curve first */
  1519. nokey_params[0] =
  1520. OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  1521. (char *)bad_curve, sizeof(bad_curve));
  1522. if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
  1523. || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEY_PARAMETERS,
  1524. nokey_params), 0)
  1525. || !TEST_ptr_null(pk))
  1526. goto err;
  1527. if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
  1528. || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR,
  1529. fromdata_params), 1))
  1530. goto err;
  1531. for (;;) {
  1532. ret = 0;
  1533. if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 256)
  1534. || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 128)
  1535. || !TEST_int_eq(EVP_PKEY_get_size(pk), 2 + 35 * 2)
  1536. || !TEST_false(EVP_PKEY_missing_parameters(pk)))
  1537. goto err;
  1538. if (!TEST_ptr(copy_pk = EVP_PKEY_new())
  1539. || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk)))
  1540. goto err;
  1541. EVP_PKEY_free(copy_pk);
  1542. copy_pk = NULL;
  1543. if (!TEST_ptr(gettable = EVP_PKEY_gettable_params(pk))
  1544. || !TEST_ptr(OSSL_PARAM_locate_const(gettable,
  1545. OSSL_PKEY_PARAM_GROUP_NAME))
  1546. || !TEST_ptr(OSSL_PARAM_locate_const(gettable,
  1547. OSSL_PKEY_PARAM_PUB_KEY))
  1548. || !TEST_ptr(OSSL_PARAM_locate_const(gettable,
  1549. OSSL_PKEY_PARAM_PRIV_KEY)))
  1550. goto err;
  1551. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(OBJ_sn2nid(curve)))
  1552. || !TEST_ptr(group_p = BN_new())
  1553. || !TEST_ptr(group_a = BN_new())
  1554. || !TEST_ptr(group_b = BN_new())
  1555. || !TEST_true(EC_GROUP_get_curve(group, group_p, group_a, group_b, NULL)))
  1556. goto err;
  1557. if (!TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_EC_A, &a))
  1558. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_EC_B, &b))
  1559. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_EC_P, &p)))
  1560. goto err;
  1561. if (!TEST_BN_eq(group_p, p) || !TEST_BN_eq(group_a, a)
  1562. || !TEST_BN_eq(group_b, b))
  1563. goto err;
  1564. EC_GROUP_free(group);
  1565. group = NULL;
  1566. BN_free(group_p);
  1567. group_p = NULL;
  1568. BN_free(group_a);
  1569. group_a = NULL;
  1570. BN_free(group_b);
  1571. group_b = NULL;
  1572. if (!EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME,
  1573. out_curve_name,
  1574. sizeof(out_curve_name),
  1575. &len)
  1576. || !TEST_str_eq(out_curve_name, curve)
  1577. || !EVP_PKEY_get_octet_string_param(pk, OSSL_PKEY_PARAM_PUB_KEY,
  1578. out_pub, sizeof(out_pub), &len)
  1579. /*
  1580. * Our providers use uncompressed format by default if
  1581. * `OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT` was not
  1582. * explicitly set, irrespective of the format used for the
  1583. * input point given as a param to create this key.
  1584. */
  1585. || !TEST_true(out_pub[0] == POINT_CONVERSION_UNCOMPRESSED)
  1586. || !TEST_mem_eq(out_pub + 1, len - 1,
  1587. ec_pub_keydata + 1, sizeof(ec_pub_keydata) - 1)
  1588. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY,
  1589. &bn_priv))
  1590. || !TEST_BN_eq(ec_priv_bn, bn_priv))
  1591. goto err;
  1592. BN_free(bn_priv);
  1593. bn_priv = NULL;
  1594. ret = test_print_key_using_pem(alg, pk)
  1595. && test_print_key_using_encoder(alg, pk);
  1596. if (!ret || dup_pk != NULL)
  1597. break;
  1598. if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
  1599. goto err;
  1600. ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
  1601. EVP_PKEY_free(pk);
  1602. pk = dup_pk;
  1603. if (!ret)
  1604. goto err;
  1605. }
  1606. err:
  1607. EC_GROUP_free(group);
  1608. BN_free(group_a);
  1609. BN_free(group_b);
  1610. BN_free(group_p);
  1611. BN_free(a);
  1612. BN_free(b);
  1613. BN_free(p);
  1614. BN_free(bn_priv);
  1615. BN_free(ec_priv_bn);
  1616. OSSL_PARAM_free(fromdata_params);
  1617. OSSL_PARAM_BLD_free(bld);
  1618. EVP_PKEY_free(pk);
  1619. EVP_PKEY_free(copy_pk);
  1620. EVP_PKEY_CTX_free(ctx);
  1621. return ret;
  1622. }
  1623. static int test_ec_dup_no_operation(void)
  1624. {
  1625. int ret = 0;
  1626. EVP_PKEY_CTX *pctx = NULL, *ctx = NULL, *kctx = NULL;
  1627. EVP_PKEY *param = NULL, *pkey = NULL;
  1628. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL))
  1629. || !TEST_int_gt(EVP_PKEY_paramgen_init(pctx), 0)
  1630. || !TEST_int_gt(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
  1631. NID_X9_62_prime256v1), 0)
  1632. || !TEST_int_gt(EVP_PKEY_paramgen(pctx, &param), 0)
  1633. || !TEST_ptr(param))
  1634. goto err;
  1635. EVP_PKEY_CTX_free(pctx);
  1636. pctx = NULL;
  1637. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(NULL, param, NULL))
  1638. || !TEST_ptr(kctx = EVP_PKEY_CTX_dup(ctx))
  1639. || !TEST_int_gt(EVP_PKEY_keygen_init(kctx), 0)
  1640. || !TEST_int_gt(EVP_PKEY_keygen(kctx, &pkey), 0))
  1641. goto err;
  1642. ret = 1;
  1643. err:
  1644. EVP_PKEY_free(pkey);
  1645. EVP_PKEY_free(param);
  1646. EVP_PKEY_CTX_free(ctx);
  1647. EVP_PKEY_CTX_free(kctx);
  1648. EVP_PKEY_CTX_free(pctx);
  1649. return ret;
  1650. }
  1651. /* Test that keygen doesn't support EVP_PKEY_CTX_dup */
  1652. static int test_ec_dup_keygen_operation(void)
  1653. {
  1654. int ret = 0;
  1655. EVP_PKEY_CTX *pctx = NULL, *ctx = NULL, *kctx = NULL;
  1656. EVP_PKEY *param = NULL, *pkey = NULL;
  1657. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL))
  1658. || !TEST_int_gt(EVP_PKEY_paramgen_init(pctx), 0)
  1659. || !TEST_int_gt(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
  1660. NID_X9_62_prime256v1), 0)
  1661. || !TEST_int_gt(EVP_PKEY_paramgen(pctx, &param), 0)
  1662. || !TEST_ptr(param))
  1663. goto err;
  1664. EVP_PKEY_CTX_free(pctx);
  1665. pctx = NULL;
  1666. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(NULL, param, NULL))
  1667. || !TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
  1668. || !TEST_ptr_null(kctx = EVP_PKEY_CTX_dup(ctx)))
  1669. goto err;
  1670. ret = 1;
  1671. err:
  1672. EVP_PKEY_free(pkey);
  1673. EVP_PKEY_free(param);
  1674. EVP_PKEY_CTX_free(ctx);
  1675. EVP_PKEY_CTX_free(kctx);
  1676. EVP_PKEY_CTX_free(pctx);
  1677. return ret;
  1678. }
  1679. #endif /* OPENSSL_NO_EC */
  1680. #ifndef OPENSSL_NO_DSA
  1681. static int test_fromdata_dsa_fips186_4(void)
  1682. {
  1683. int ret = 0;
  1684. EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
  1685. EVP_PKEY *pk = NULL, *copy_pk = NULL, *dup_pk = NULL;
  1686. BIGNUM *pub = NULL, *priv = NULL;
  1687. BIGNUM *p = NULL, *q = NULL, *g = NULL;
  1688. BIGNUM *pub_out = NULL, *priv_out = NULL;
  1689. BIGNUM *p_out = NULL, *q_out = NULL, *g_out = NULL, *j_out = NULL;
  1690. int gindex_out = 0, pcounter_out = 0, hindex_out = 0;
  1691. char name_out[80];
  1692. unsigned char seed_out[32];
  1693. size_t len;
  1694. OSSL_PARAM_BLD *bld = NULL;
  1695. OSSL_PARAM *fromdata_params = NULL;
  1696. /*
  1697. * DSA parameter data was generated using the following:
  1698. * openssl genpkey -genparam -algorithm DSA -pkeyopt pbits:2048 \
  1699. * -pkeyopt qbits:256 -pkeyopt type:0 \
  1700. * -pkeyopt gindex:1 -out dsa_params.pem -text
  1701. */
  1702. static const unsigned char p_data[] = {
  1703. 0x00, 0xa0, 0xb7, 0x02, 0xc4, 0xac, 0xa6, 0x42, 0xab, 0xf2, 0x34, 0x0b,
  1704. 0x22, 0x47, 0x1f, 0x33, 0xcf, 0xd5, 0x04, 0xe4, 0x3e, 0xec, 0xa1, 0x21,
  1705. 0xc8, 0x41, 0x2b, 0xef, 0xb8, 0x1f, 0x0b, 0x5b, 0x88, 0x8b, 0x67, 0xf8,
  1706. 0x68, 0x6d, 0x7c, 0x4d, 0x96, 0x5f, 0x3c, 0x66, 0xef, 0x58, 0x34, 0xd7,
  1707. 0xf6, 0xa2, 0x1b, 0xad, 0xc8, 0x12, 0x52, 0xb8, 0xe8, 0x2a, 0x63, 0xcc,
  1708. 0xea, 0xe7, 0x4e, 0xc8, 0x34, 0x4c, 0x58, 0x59, 0x0a, 0xc2, 0x4a, 0xe4,
  1709. 0xb4, 0x64, 0x20, 0xf4, 0xf6, 0x0a, 0xcf, 0x86, 0x01, 0x6c, 0x7f, 0x23,
  1710. 0x4a, 0x51, 0x07, 0x99, 0x42, 0x28, 0x7a, 0xff, 0x18, 0x67, 0x52, 0x64,
  1711. 0xf2, 0x9a, 0x62, 0x30, 0xc3, 0x00, 0xde, 0x23, 0xe9, 0x11, 0x95, 0x7e,
  1712. 0xd1, 0x3d, 0x8d, 0xb4, 0x0e, 0x9f, 0x9e, 0xb1, 0x30, 0x03, 0xf0, 0x73,
  1713. 0xa8, 0x40, 0x48, 0x42, 0x7b, 0x60, 0xa0, 0xc4, 0xf2, 0x3b, 0x2d, 0x0a,
  1714. 0x0c, 0xb8, 0x19, 0xfb, 0xb4, 0xf8, 0xe0, 0x2a, 0xc7, 0xf1, 0xc0, 0xc6,
  1715. 0x86, 0x14, 0x60, 0x12, 0x0f, 0xc0, 0xde, 0x4a, 0x67, 0xec, 0xc7, 0xde,
  1716. 0x76, 0x21, 0x1a, 0x55, 0x7f, 0x86, 0xc3, 0x97, 0x98, 0xce, 0xf5, 0xcd,
  1717. 0xf0, 0xe7, 0x12, 0xd6, 0x93, 0xee, 0x1b, 0x9b, 0x61, 0xef, 0x05, 0x8c,
  1718. 0x45, 0x46, 0xd9, 0x64, 0x6f, 0xbe, 0x27, 0xaa, 0x67, 0x01, 0xcc, 0x71,
  1719. 0xb1, 0x60, 0xce, 0x21, 0xd8, 0x51, 0x17, 0x27, 0x0d, 0x90, 0x3d, 0x18,
  1720. 0x7c, 0x87, 0x15, 0x8e, 0x48, 0x4c, 0x6c, 0xc5, 0x72, 0xeb, 0xb7, 0x56,
  1721. 0xf5, 0x6b, 0x60, 0x8f, 0xc2, 0xfd, 0x3f, 0x46, 0x5c, 0x00, 0x91, 0x85,
  1722. 0x79, 0x45, 0x5b, 0x1c, 0x82, 0xc4, 0x87, 0x50, 0x79, 0xba, 0xcc, 0x1c,
  1723. 0x32, 0x7e, 0x2e, 0xb8, 0x2e, 0xc5, 0x4e, 0xd1, 0x9b, 0xdb, 0x66, 0x79,
  1724. 0x7c, 0xfe, 0xaf, 0x6a, 0x05
  1725. };
  1726. static const unsigned char q_data[] = {
  1727. 0xa8, 0xcd, 0xf4, 0x33, 0x7b, 0x13, 0x0a, 0x24, 0xc1, 0xde, 0x4a, 0x04,
  1728. 0x7b, 0x4b, 0x71, 0x51, 0x32, 0xe9, 0x47, 0x74, 0xbd, 0x0c, 0x21, 0x40,
  1729. 0x84, 0x12, 0x0a, 0x17, 0x73, 0xdb, 0x29, 0xc7
  1730. };
  1731. static const unsigned char g_data[] = {
  1732. 0x6c, 0xc6, 0xa4, 0x3e, 0x61, 0x84, 0xc1, 0xff, 0x6f, 0x4a, 0x1a, 0x6b,
  1733. 0xb0, 0x24, 0x4b, 0xd2, 0x92, 0x5b, 0x29, 0x5c, 0x61, 0xb8, 0xc9, 0x2b,
  1734. 0xd6, 0xf7, 0x59, 0xfd, 0xd8, 0x70, 0x66, 0x77, 0xfc, 0xc1, 0xa4, 0xd4,
  1735. 0xb0, 0x1e, 0xd5, 0xbf, 0x59, 0x98, 0xb3, 0x66, 0x8b, 0xf4, 0x2e, 0xe6,
  1736. 0x12, 0x3e, 0xcc, 0xf8, 0x02, 0xb8, 0xc6, 0xc3, 0x47, 0xd2, 0xf5, 0xaa,
  1737. 0x0c, 0x5f, 0x51, 0xf5, 0xd0, 0x4c, 0x55, 0x3d, 0x07, 0x73, 0xa6, 0x57,
  1738. 0xce, 0x5a, 0xad, 0x42, 0x0c, 0x13, 0x0f, 0xe2, 0x31, 0x25, 0x8e, 0x72,
  1739. 0x12, 0x73, 0x10, 0xdb, 0x7f, 0x79, 0xeb, 0x59, 0xfc, 0xfe, 0xf7, 0x0c,
  1740. 0x1a, 0x81, 0x53, 0x96, 0x22, 0xb8, 0xe7, 0x58, 0xd8, 0x67, 0x80, 0x60,
  1741. 0xad, 0x8b, 0x55, 0x1c, 0x91, 0xf0, 0x72, 0x9a, 0x7e, 0xad, 0x37, 0xf1,
  1742. 0x77, 0x18, 0x96, 0x8a, 0x68, 0x70, 0xfc, 0x71, 0xa9, 0xa2, 0xe8, 0x35,
  1743. 0x27, 0x78, 0xf2, 0xef, 0x59, 0x36, 0x6d, 0x7c, 0xb6, 0x98, 0xd8, 0x1e,
  1744. 0xfa, 0x25, 0x73, 0x97, 0x45, 0x58, 0xe3, 0xae, 0xbd, 0x52, 0x54, 0x05,
  1745. 0xd8, 0x26, 0x26, 0xba, 0xba, 0x05, 0xb5, 0xe9, 0xe5, 0x76, 0xae, 0x25,
  1746. 0xdd, 0xfc, 0x10, 0x89, 0x5a, 0xa9, 0xee, 0x59, 0xc5, 0x79, 0x8b, 0xeb,
  1747. 0x1e, 0x2c, 0x61, 0xab, 0x0d, 0xd1, 0x10, 0x04, 0x91, 0x32, 0x77, 0x4a,
  1748. 0xa6, 0x64, 0x53, 0xda, 0x4c, 0xd7, 0x3a, 0x29, 0xd4, 0xf3, 0x82, 0x25,
  1749. 0x1d, 0x6f, 0x4a, 0x7f, 0xd3, 0x08, 0x3b, 0x42, 0x30, 0x10, 0xd8, 0xd0,
  1750. 0x97, 0x3a, 0xeb, 0x92, 0x63, 0xec, 0x93, 0x2b, 0x6f, 0x32, 0xd8, 0xcd,
  1751. 0x80, 0xd3, 0xc0, 0x4c, 0x03, 0xd5, 0xca, 0xbc, 0x8f, 0xc7, 0x43, 0x53,
  1752. 0x64, 0x66, 0x1c, 0x82, 0x2d, 0xfb, 0xff, 0x39, 0xba, 0xd6, 0x42, 0x62,
  1753. 0x02, 0x6f, 0x96, 0x36
  1754. };
  1755. static const unsigned char seed_data[] = {
  1756. 0x64, 0x46, 0x07, 0x32, 0x8d, 0x70, 0x9c, 0xb3, 0x8a, 0x35, 0xde, 0x62,
  1757. 0x00, 0xf2, 0x6d, 0x52, 0x37, 0x4d, 0xb3, 0x84, 0xe1, 0x9d, 0x41, 0x04,
  1758. 0xda, 0x7b, 0xdc, 0x0d, 0x8b, 0x5e, 0xe0, 0x84
  1759. };
  1760. const int gindex = 1;
  1761. const int pcounter = 53;
  1762. /*
  1763. * The keypair was generated using
  1764. * openssl genpkey -paramfile dsa_params.pem --pkeyopt pcounter:53 \
  1765. * -pkeyopt gindex:1 \
  1766. * -pkeyopt hexseed:644607328d709cb38a35de6200f26d -text
  1767. */
  1768. static const unsigned char priv_data[] = {
  1769. 0x00, 0x8f, 0xc5, 0x9e, 0xd0, 0xf7, 0x2a, 0x0b, 0x66, 0xf1, 0x32, 0x73,
  1770. 0xae, 0xf6, 0xd9, 0xd4, 0xdb, 0x2d, 0x96, 0x55, 0x89, 0xff, 0xef, 0xa8,
  1771. 0x5f, 0x47, 0x8f, 0xca, 0x02, 0x8a, 0xe1, 0x35, 0x90
  1772. };
  1773. static const unsigned char pub_data[] = {
  1774. 0x44, 0x19, 0xc9, 0x46, 0x45, 0x57, 0xc1, 0xa9, 0xd8, 0x30, 0x99, 0x29,
  1775. 0x6a, 0x4b, 0x63, 0x71, 0x69, 0x96, 0x35, 0x17, 0xb2, 0x62, 0x9b, 0x80,
  1776. 0x0a, 0x95, 0x9d, 0x6a, 0xc0, 0x32, 0x0d, 0x07, 0x5f, 0x19, 0x44, 0x02,
  1777. 0xf1, 0xbd, 0xce, 0xdf, 0x10, 0xf8, 0x02, 0x5d, 0x7d, 0x98, 0x8a, 0x73,
  1778. 0x89, 0x00, 0xb6, 0x24, 0xd6, 0x33, 0xe7, 0xcf, 0x8b, 0x49, 0x2a, 0xaf,
  1779. 0x13, 0x1c, 0xb2, 0x52, 0x15, 0xfd, 0x9b, 0xd5, 0x40, 0x4a, 0x1a, 0xda,
  1780. 0x29, 0x4c, 0x92, 0x7e, 0x66, 0x06, 0xdb, 0x61, 0x86, 0xac, 0xb5, 0xda,
  1781. 0x3c, 0x7d, 0x73, 0x7e, 0x54, 0x32, 0x68, 0xa5, 0x02, 0xbc, 0x59, 0x47,
  1782. 0x84, 0xd3, 0x87, 0x71, 0x5f, 0xeb, 0x43, 0x45, 0x24, 0xd3, 0xec, 0x08,
  1783. 0x52, 0xc2, 0x89, 0x2d, 0x9c, 0x1a, 0xcc, 0x91, 0x65, 0x5d, 0xa3, 0xa1,
  1784. 0x35, 0x31, 0x10, 0x1c, 0x3a, 0xa8, 0x4d, 0x18, 0xd5, 0x06, 0xaf, 0xb2,
  1785. 0xec, 0x5c, 0x89, 0x9e, 0x90, 0x86, 0x10, 0x01, 0xeb, 0x51, 0xd5, 0x1b,
  1786. 0x9c, 0xcb, 0x66, 0x07, 0x3f, 0xc4, 0x6e, 0x0a, 0x1b, 0x73, 0xa0, 0x4b,
  1787. 0x5f, 0x4d, 0xab, 0x35, 0x28, 0xfa, 0xda, 0x3a, 0x0c, 0x08, 0xe8, 0xf3,
  1788. 0xef, 0x42, 0x67, 0xbc, 0x21, 0xf2, 0xc2, 0xb8, 0xff, 0x1a, 0x81, 0x05,
  1789. 0x68, 0x73, 0x62, 0xdf, 0xd7, 0xab, 0x0f, 0x22, 0x89, 0x57, 0x96, 0xd4,
  1790. 0x93, 0xaf, 0xa1, 0x21, 0xa3, 0x48, 0xe9, 0xf0, 0x97, 0x47, 0xa0, 0x27,
  1791. 0xba, 0x87, 0xb8, 0x15, 0x5f, 0xff, 0x2c, 0x50, 0x41, 0xf1, 0x7e, 0xc6,
  1792. 0x81, 0xc4, 0x51, 0xf1, 0xfd, 0xd6, 0x86, 0xf7, 0x69, 0x97, 0xf1, 0x49,
  1793. 0xc9, 0xf9, 0xf4, 0x9b, 0xf4, 0xe8, 0x85, 0xa7, 0xbd, 0x36, 0x55, 0x4a,
  1794. 0x3d, 0xe8, 0x65, 0x09, 0x7b, 0xb7, 0x12, 0x64, 0xd2, 0x0a, 0x53, 0x60,
  1795. 0x48, 0xd1, 0x8a, 0xbd
  1796. };
  1797. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  1798. || !TEST_ptr(pub = BN_bin2bn(pub_data, sizeof(pub_data), NULL))
  1799. || !TEST_ptr(priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL))
  1800. || !TEST_ptr(p = BN_bin2bn(p_data, sizeof(p_data), NULL))
  1801. || !TEST_ptr(q = BN_bin2bn(q_data, sizeof(q_data), NULL))
  1802. || !TEST_ptr(g = BN_bin2bn(g_data, sizeof(g_data), NULL))
  1803. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
  1804. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
  1805. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
  1806. || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
  1807. OSSL_PKEY_PARAM_FFC_SEED,
  1808. seed_data,
  1809. sizeof(seed_data)))
  1810. || !TEST_true(OSSL_PARAM_BLD_push_int(bld, OSSL_PKEY_PARAM_FFC_GINDEX,
  1811. gindex))
  1812. || !TEST_true(OSSL_PARAM_BLD_push_int(bld,
  1813. OSSL_PKEY_PARAM_FFC_PCOUNTER,
  1814. pcounter))
  1815. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
  1816. pub))
  1817. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
  1818. priv))
  1819. || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)))
  1820. goto err;
  1821. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL)))
  1822. goto err;
  1823. if (!TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
  1824. || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pk, EVP_PKEY_KEYPAIR,
  1825. fromdata_params), 1))
  1826. goto err;
  1827. for (;;) {
  1828. ret = 0;
  1829. if (!TEST_int_eq(EVP_PKEY_get_bits(pk), 2048)
  1830. || !TEST_int_eq(EVP_PKEY_get_security_bits(pk), 112)
  1831. || !TEST_int_eq(EVP_PKEY_get_size(pk), 2 + 2 * (3 + sizeof(q_data)))
  1832. || !TEST_false(EVP_PKEY_missing_parameters(pk)))
  1833. goto err;
  1834. if (!TEST_false(EVP_PKEY_get_utf8_string_param(pk,
  1835. OSSL_PKEY_PARAM_GROUP_NAME,
  1836. name_out,
  1837. sizeof(name_out),
  1838. &len))
  1839. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PUB_KEY,
  1840. &pub_out))
  1841. || !TEST_BN_eq(pub, pub_out)
  1842. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY,
  1843. &priv_out))
  1844. || !TEST_BN_eq(priv, priv_out)
  1845. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_P,
  1846. &p_out))
  1847. || !TEST_BN_eq(p, p_out)
  1848. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_Q,
  1849. &q_out))
  1850. || !TEST_BN_eq(q, q_out)
  1851. || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_G,
  1852. &g_out))
  1853. || !TEST_BN_eq(g, g_out)
  1854. || !TEST_false(EVP_PKEY_get_bn_param(pk,
  1855. OSSL_PKEY_PARAM_FFC_COFACTOR,
  1856. &j_out))
  1857. || !TEST_ptr_null(j_out)
  1858. || !TEST_true(EVP_PKEY_get_octet_string_param(pk,
  1859. OSSL_PKEY_PARAM_FFC_SEED,
  1860. seed_out,
  1861. sizeof(seed_out),
  1862. &len))
  1863. || !TEST_true(EVP_PKEY_get_int_param(pk,
  1864. OSSL_PKEY_PARAM_FFC_GINDEX,
  1865. &gindex_out))
  1866. || !TEST_int_eq(gindex, gindex_out)
  1867. || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_H,
  1868. &hindex_out))
  1869. || !TEST_int_eq(hindex_out, 0)
  1870. || !TEST_true(EVP_PKEY_get_int_param(pk,
  1871. OSSL_PKEY_PARAM_FFC_PCOUNTER,
  1872. &pcounter_out))
  1873. || !TEST_int_eq(pcounter, pcounter_out))
  1874. goto err;
  1875. BN_free(p_out);
  1876. p_out = NULL;
  1877. BN_free(q_out);
  1878. q_out = NULL;
  1879. BN_free(g_out);
  1880. g_out = NULL;
  1881. BN_free(j_out);
  1882. j_out = NULL;
  1883. BN_free(pub_out);
  1884. pub_out = NULL;
  1885. BN_free(priv_out);
  1886. priv_out = NULL;
  1887. if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, "")))
  1888. goto err;
  1889. if (!TEST_int_gt(EVP_PKEY_check(key_ctx), 0)
  1890. || !TEST_int_gt(EVP_PKEY_public_check(key_ctx), 0)
  1891. || !TEST_int_gt(EVP_PKEY_private_check(key_ctx), 0)
  1892. || !TEST_int_gt(EVP_PKEY_pairwise_check(key_ctx), 0))
  1893. goto err;
  1894. EVP_PKEY_CTX_free(key_ctx);
  1895. key_ctx = NULL;
  1896. if (!TEST_ptr(copy_pk = EVP_PKEY_new())
  1897. || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk)))
  1898. goto err;
  1899. EVP_PKEY_free(copy_pk);
  1900. copy_pk = NULL;
  1901. ret = test_print_key_using_pem("DSA", pk)
  1902. && test_print_key_using_encoder("DSA", pk);
  1903. if (!ret || dup_pk != NULL)
  1904. break;
  1905. if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pk)))
  1906. goto err;
  1907. ret = ret && TEST_int_eq(EVP_PKEY_eq(pk, dup_pk), 1);
  1908. EVP_PKEY_free(pk);
  1909. pk = dup_pk;
  1910. if (!ret)
  1911. goto err;
  1912. }
  1913. err:
  1914. OSSL_PARAM_free(fromdata_params);
  1915. OSSL_PARAM_BLD_free(bld);
  1916. BN_free(p);
  1917. BN_free(q);
  1918. BN_free(g);
  1919. BN_free(pub);
  1920. BN_free(priv);
  1921. BN_free(p_out);
  1922. BN_free(q_out);
  1923. BN_free(g_out);
  1924. BN_free(pub_out);
  1925. BN_free(priv_out);
  1926. BN_free(j_out);
  1927. EVP_PKEY_free(pk);
  1928. EVP_PKEY_free(copy_pk);
  1929. EVP_PKEY_CTX_free(ctx);
  1930. EVP_PKEY_CTX_free(key_ctx);
  1931. return ret;
  1932. }
  1933. static int test_check_dsa(void)
  1934. {
  1935. int ret = 0;
  1936. EVP_PKEY_CTX *ctx = NULL;
  1937. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL))
  1938. || !TEST_int_le(EVP_PKEY_check(ctx), 0)
  1939. || !TEST_int_le(EVP_PKEY_public_check(ctx), 0)
  1940. || !TEST_int_le(EVP_PKEY_private_check(ctx), 0)
  1941. || !TEST_int_le(EVP_PKEY_pairwise_check(ctx), 0))
  1942. goto err;
  1943. ret = 1;
  1944. err:
  1945. EVP_PKEY_CTX_free(ctx);
  1946. return ret;
  1947. }
  1948. #endif /* OPENSSL_NO_DSA */
  1949. static OSSL_PARAM *do_construct_hkdf_params(char *digest, char *key,
  1950. size_t keylen, char *salt)
  1951. {
  1952. OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 5);
  1953. OSSL_PARAM *p = params;
  1954. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, digest, 0);
  1955. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
  1956. salt, strlen(salt));
  1957. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
  1958. (unsigned char *)key, keylen);
  1959. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MODE,
  1960. "EXTRACT_ONLY", 0);
  1961. *p = OSSL_PARAM_construct_end();
  1962. return params;
  1963. }
  1964. static int test_evp_pkey_ctx_dup_kdf(void)
  1965. {
  1966. int ret = 0;
  1967. size_t len = 0, dlen = 0;
  1968. EVP_PKEY_CTX *pctx = NULL, *dctx = NULL;
  1969. OSSL_PARAM *params = NULL;
  1970. if (!TEST_ptr(params = do_construct_hkdf_params("sha256", "secret", 6,
  1971. "salt")))
  1972. goto err;
  1973. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(NULL, "HKDF", NULL)))
  1974. goto err;
  1975. if (!TEST_int_eq(EVP_PKEY_derive_init_ex(pctx, params), 1))
  1976. goto err;
  1977. if (!TEST_ptr(dctx = EVP_PKEY_CTX_dup(pctx)))
  1978. goto err;
  1979. if (!TEST_int_eq(EVP_PKEY_derive(pctx, NULL, &len), 1)
  1980. || !TEST_size_t_eq(len, SHA256_DIGEST_LENGTH)
  1981. || !TEST_int_eq(EVP_PKEY_derive(dctx, NULL, &dlen), 1)
  1982. || !TEST_size_t_eq(dlen, SHA256_DIGEST_LENGTH))
  1983. goto err;
  1984. ret = 1;
  1985. err:
  1986. OPENSSL_free(params);
  1987. EVP_PKEY_CTX_free(dctx);
  1988. EVP_PKEY_CTX_free(pctx);
  1989. return ret;
  1990. }
  1991. int setup_tests(void)
  1992. {
  1993. if (!test_skip_common_options()) {
  1994. TEST_error("Error parsing test options\n");
  1995. return 0;
  1996. }
  1997. if (!TEST_ptr(datadir = test_get_argument(0)))
  1998. return 0;
  1999. ADD_TEST(test_evp_pkey_ctx_dup_kdf);
  2000. ADD_TEST(test_evp_pkey_get_bn_param_large);
  2001. ADD_TEST(test_fromdata_rsa);
  2002. ADD_TEST(test_fromdata_rsa_derive_from_pq_sp800);
  2003. ADD_TEST(test_fromdata_rsa_derive_from_pq_multiprime);
  2004. #ifndef OPENSSL_NO_DH
  2005. ADD_TEST(test_fromdata_dh_fips186_4);
  2006. ADD_TEST(test_fromdata_dh_named_group);
  2007. #endif
  2008. #ifndef OPENSSL_NO_DSA
  2009. ADD_TEST(test_check_dsa);
  2010. ADD_TEST(test_fromdata_dsa_fips186_4);
  2011. #endif
  2012. #ifndef OPENSSL_NO_EC
  2013. # ifndef OPENSSL_NO_ECX
  2014. ADD_ALL_TESTS(test_fromdata_ecx, 4 * 3);
  2015. # endif
  2016. ADD_TEST(test_fromdata_ec);
  2017. ADD_TEST(test_ec_dup_no_operation);
  2018. ADD_TEST(test_ec_dup_keygen_operation);
  2019. #endif
  2020. return 1;
  2021. }