evp_pkey_provided_test.c 50 KB

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