evp_pkey_provided_test.c 49 KB

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