evp_pkey_provided_test.c 61 KB

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