2
0

dhtest.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /*
  2. * Copyright 1995-2023 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. /*
  10. * DH low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "internal/nelem.h"
  18. #include <openssl/crypto.h>
  19. #include <openssl/bio.h>
  20. #include <openssl/bn.h>
  21. #include <openssl/rand.h>
  22. #include <openssl/err.h>
  23. #include <openssl/obj_mac.h>
  24. #include <openssl/core_names.h>
  25. #include "testutil.h"
  26. #ifndef OPENSSL_NO_DH
  27. # include <openssl/dh.h>
  28. # include "crypto/bn_dh.h"
  29. # include "crypto/dh.h"
  30. static int cb(int p, int n, BN_GENCB *arg);
  31. static int dh_test(void)
  32. {
  33. DH *dh = NULL;
  34. BIGNUM *p = NULL, *q = NULL, *g = NULL;
  35. const BIGNUM *p2, *q2, *g2;
  36. BIGNUM *priv_key = NULL;
  37. const BIGNUM *pub_key2, *priv_key2;
  38. BN_GENCB *_cb = NULL;
  39. DH *a = NULL;
  40. DH *b = NULL;
  41. DH *c = NULL;
  42. const BIGNUM *ap = NULL, *ag = NULL, *apub_key = NULL;
  43. const BIGNUM *bpub_key = NULL, *bpriv_key = NULL;
  44. BIGNUM *bp = NULL, *bg = NULL, *cpriv_key = NULL;
  45. unsigned char *abuf = NULL;
  46. unsigned char *bbuf = NULL;
  47. unsigned char *cbuf = NULL;
  48. int i, alen, blen, clen, aout, bout, cout;
  49. int ret = 0;
  50. if (!TEST_ptr(dh = DH_new())
  51. || !TEST_ptr(p = BN_new())
  52. || !TEST_ptr(q = BN_new())
  53. || !TEST_ptr(g = BN_new())
  54. || !TEST_ptr(priv_key = BN_new()))
  55. goto err1;
  56. /*
  57. * I) basic tests
  58. */
  59. /* using a small predefined Sophie Germain DH group with generator 3 */
  60. if (!TEST_true(BN_set_word(p, 4079L))
  61. || !TEST_true(BN_set_word(q, 2039L))
  62. || !TEST_true(BN_set_word(g, 3L))
  63. || !TEST_true(DH_set0_pqg(dh, p, q, g)))
  64. goto err1;
  65. /* check fails, because p is way too small */
  66. if (!TEST_true(DH_check(dh, &i)))
  67. goto err2;
  68. i ^= DH_MODULUS_TOO_SMALL;
  69. if (!TEST_false(i & DH_CHECK_P_NOT_PRIME)
  70. || !TEST_false(i & DH_CHECK_P_NOT_SAFE_PRIME)
  71. || !TEST_false(i & DH_UNABLE_TO_CHECK_GENERATOR)
  72. || !TEST_false(i & DH_NOT_SUITABLE_GENERATOR)
  73. || !TEST_false(i & DH_CHECK_Q_NOT_PRIME)
  74. || !TEST_false(i & DH_CHECK_INVALID_Q_VALUE)
  75. || !TEST_false(i & DH_CHECK_INVALID_J_VALUE)
  76. || !TEST_false(i & DH_MODULUS_TOO_SMALL)
  77. || !TEST_false(i & DH_MODULUS_TOO_LARGE)
  78. || !TEST_false(i))
  79. goto err2;
  80. /* test the combined getter for p, q, and g */
  81. DH_get0_pqg(dh, &p2, &q2, &g2);
  82. if (!TEST_ptr_eq(p2, p)
  83. || !TEST_ptr_eq(q2, q)
  84. || !TEST_ptr_eq(g2, g))
  85. goto err2;
  86. /* test the simple getters for p, q, and g */
  87. if (!TEST_ptr_eq(DH_get0_p(dh), p2)
  88. || !TEST_ptr_eq(DH_get0_q(dh), q2)
  89. || !TEST_ptr_eq(DH_get0_g(dh), g2))
  90. goto err2;
  91. /* set the private key only*/
  92. if (!TEST_true(BN_set_word(priv_key, 1234L))
  93. || !TEST_true(DH_set0_key(dh, NULL, priv_key)))
  94. goto err2;
  95. /* test the combined getter for pub_key and priv_key */
  96. DH_get0_key(dh, &pub_key2, &priv_key2);
  97. if (!TEST_ptr_eq(pub_key2, NULL)
  98. || !TEST_ptr_eq(priv_key2, priv_key))
  99. goto err3;
  100. /* test the simple getters for pub_key and priv_key */
  101. if (!TEST_ptr_eq(DH_get0_pub_key(dh), pub_key2)
  102. || !TEST_ptr_eq(DH_get0_priv_key(dh), priv_key2))
  103. goto err3;
  104. /* now generate a key pair (expect failure since modulus is too small) */
  105. if (!TEST_false(DH_generate_key(dh)))
  106. goto err3;
  107. /* We'll have a stale error on the queue from the above test so clear it */
  108. ERR_clear_error();
  109. if (!TEST_ptr(BN_copy(q, p)) || !TEST_true(BN_add(q, q, BN_value_one())))
  110. goto err3;
  111. if (!TEST_true(DH_check(dh, &i)))
  112. goto err3;
  113. if (!TEST_true(i & DH_CHECK_INVALID_Q_VALUE)
  114. || !TEST_false(i & DH_CHECK_Q_NOT_PRIME))
  115. goto err3;
  116. /* Modulus of size: dh check max modulus bits + 1 */
  117. if (!TEST_true(BN_set_word(p, 1))
  118. || !TEST_true(BN_lshift(p, p, OPENSSL_DH_CHECK_MAX_MODULUS_BITS)))
  119. goto err3;
  120. /*
  121. * We expect no checks at all for an excessively large modulus
  122. */
  123. if (!TEST_false(DH_check(dh, &i)))
  124. goto err3;
  125. /* We'll have a stale error on the queue from the above test so clear it */
  126. ERR_clear_error();
  127. /*
  128. * II) key generation
  129. */
  130. /* generate a DH group ... */
  131. if (!TEST_ptr(_cb = BN_GENCB_new()))
  132. goto err3;
  133. BN_GENCB_set(_cb, &cb, NULL);
  134. if (!TEST_ptr(a = DH_new())
  135. || !TEST_true(DH_generate_parameters_ex(a, 512,
  136. DH_GENERATOR_5, _cb)))
  137. goto err3;
  138. /* ... and check whether it is valid */
  139. if (!TEST_true(DH_check(a, &i)))
  140. goto err3;
  141. if (!TEST_false(i & DH_CHECK_P_NOT_PRIME)
  142. || !TEST_false(i & DH_CHECK_P_NOT_SAFE_PRIME)
  143. || !TEST_false(i & DH_UNABLE_TO_CHECK_GENERATOR)
  144. || !TEST_false(i & DH_NOT_SUITABLE_GENERATOR)
  145. || !TEST_false(i & DH_CHECK_Q_NOT_PRIME)
  146. || !TEST_false(i & DH_CHECK_INVALID_Q_VALUE)
  147. || !TEST_false(i & DH_CHECK_INVALID_J_VALUE)
  148. || !TEST_false(i & DH_MODULUS_TOO_SMALL)
  149. || !TEST_false(i & DH_MODULUS_TOO_LARGE)
  150. || !TEST_false(i))
  151. goto err3;
  152. DH_get0_pqg(a, &ap, NULL, &ag);
  153. /* now create another copy of the DH group for the peer */
  154. if (!TEST_ptr(b = DH_new()))
  155. goto err3;
  156. if (!TEST_ptr(bp = BN_dup(ap))
  157. || !TEST_ptr(bg = BN_dup(ag))
  158. || !TEST_true(DH_set0_pqg(b, bp, NULL, bg)))
  159. goto err3;
  160. bp = bg = NULL;
  161. /*
  162. * III) simulate a key exchange
  163. */
  164. if (!DH_generate_key(a))
  165. goto err3;
  166. DH_get0_key(a, &apub_key, NULL);
  167. if (!DH_generate_key(b))
  168. goto err3;
  169. DH_get0_key(b, &bpub_key, &bpriv_key);
  170. /* Also test with a private-key-only copy of |b|. */
  171. if (!TEST_ptr(c = DHparams_dup(b))
  172. || !TEST_ptr(cpriv_key = BN_dup(bpriv_key))
  173. || !TEST_true(DH_set0_key(c, NULL, cpriv_key)))
  174. goto err3;
  175. cpriv_key = NULL;
  176. alen = DH_size(a);
  177. if (!TEST_ptr(abuf = OPENSSL_malloc(alen))
  178. || !TEST_true((aout = DH_compute_key(abuf, bpub_key, a)) != -1))
  179. goto err3;
  180. blen = DH_size(b);
  181. if (!TEST_ptr(bbuf = OPENSSL_malloc(blen))
  182. || !TEST_true((bout = DH_compute_key(bbuf, apub_key, b)) != -1))
  183. goto err3;
  184. clen = DH_size(c);
  185. if (!TEST_ptr(cbuf = OPENSSL_malloc(clen))
  186. || !TEST_true((cout = DH_compute_key(cbuf, apub_key, c)) != -1))
  187. goto err3;
  188. if (!TEST_true(aout >= 20)
  189. || !TEST_mem_eq(abuf, aout, bbuf, bout)
  190. || !TEST_mem_eq(abuf, aout, cbuf, cout))
  191. goto err3;
  192. ret = 1;
  193. goto success;
  194. err1:
  195. /* an error occurred before p,q,g were assigned to dh */
  196. BN_free(p);
  197. BN_free(q);
  198. BN_free(g);
  199. err2:
  200. /* an error occurred before priv_key was assigned to dh */
  201. BN_free(priv_key);
  202. err3:
  203. success:
  204. OPENSSL_free(abuf);
  205. OPENSSL_free(bbuf);
  206. OPENSSL_free(cbuf);
  207. DH_free(b);
  208. DH_free(a);
  209. DH_free(c);
  210. BN_free(bp);
  211. BN_free(bg);
  212. BN_free(cpriv_key);
  213. BN_GENCB_free(_cb);
  214. DH_free(dh);
  215. return ret;
  216. }
  217. static int cb(int p, int n, BN_GENCB *arg)
  218. {
  219. return 1;
  220. }
  221. static int dh_computekey_range_test(void)
  222. {
  223. int ret = 0, sz;
  224. DH *dh = NULL;
  225. BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub = NULL, *priv = NULL;
  226. unsigned char *buf = NULL;
  227. if (!TEST_ptr(p = BN_dup(&ossl_bignum_ffdhe2048_p))
  228. || !TEST_ptr(q = BN_dup(&ossl_bignum_ffdhe2048_q))
  229. || !TEST_ptr(g = BN_dup(&ossl_bignum_const_2))
  230. || !TEST_ptr(dh = DH_new())
  231. || !TEST_true(DH_set0_pqg(dh, p, q, g)))
  232. goto err;
  233. p = q = g = NULL;
  234. if (!TEST_int_gt(sz = DH_size(dh), 0)
  235. || !TEST_ptr(buf = OPENSSL_malloc(sz))
  236. || !TEST_ptr(pub = BN_new())
  237. || !TEST_ptr(priv = BN_new()))
  238. goto err;
  239. if (!TEST_true(BN_set_word(priv, 1))
  240. || !TEST_true(DH_set0_key(dh, NULL, priv)))
  241. goto err;
  242. priv = NULL;
  243. if (!TEST_true(BN_set_word(pub, 1)))
  244. goto err;
  245. /* Given z = pub ^ priv mod p */
  246. /* Test that z == 1 fails */
  247. if (!TEST_int_le(ossl_dh_compute_key(buf, pub, dh), 0))
  248. goto err;
  249. /* Test that z == 0 fails */
  250. if (!TEST_ptr(BN_copy(pub, DH_get0_p(dh)))
  251. || !TEST_int_le(ossl_dh_compute_key(buf, pub, dh), 0))
  252. goto err;
  253. /* Test that z == p - 1 fails */
  254. if (!TEST_true(BN_sub_word(pub, 1))
  255. || !TEST_int_le(ossl_dh_compute_key(buf, pub, dh), 0))
  256. goto err;
  257. /* Test that z == p - 2 passes */
  258. if (!TEST_true(BN_sub_word(pub, 1))
  259. || !TEST_int_eq(ossl_dh_compute_key(buf, pub, dh), sz))
  260. goto err;
  261. ret = 1;
  262. err:
  263. OPENSSL_free(buf);
  264. BN_free(priv);
  265. BN_free(pub);
  266. BN_free(g);
  267. BN_free(q);
  268. BN_free(p);
  269. DH_free(dh);
  270. return ret;
  271. }
  272. /* Test data from RFC 5114 */
  273. static const unsigned char dhtest_1024_160_xA[] = {
  274. 0xB9, 0xA3, 0xB3, 0xAE, 0x8F, 0xEF, 0xC1, 0xA2, 0x93, 0x04, 0x96, 0x50,
  275. 0x70, 0x86, 0xF8, 0x45, 0x5D, 0x48, 0x94, 0x3E
  276. };
  277. static const unsigned char dhtest_1024_160_yA[] = {
  278. 0x2A, 0x85, 0x3B, 0x3D, 0x92, 0x19, 0x75, 0x01, 0xB9, 0x01, 0x5B, 0x2D,
  279. 0xEB, 0x3E, 0xD8, 0x4F, 0x5E, 0x02, 0x1D, 0xCC, 0x3E, 0x52, 0xF1, 0x09,
  280. 0xD3, 0x27, 0x3D, 0x2B, 0x75, 0x21, 0x28, 0x1C, 0xBA, 0xBE, 0x0E, 0x76,
  281. 0xFF, 0x57, 0x27, 0xFA, 0x8A, 0xCC, 0xE2, 0x69, 0x56, 0xBA, 0x9A, 0x1F,
  282. 0xCA, 0x26, 0xF2, 0x02, 0x28, 0xD8, 0x69, 0x3F, 0xEB, 0x10, 0x84, 0x1D,
  283. 0x84, 0xA7, 0x36, 0x00, 0x54, 0xEC, 0xE5, 0xA7, 0xF5, 0xB7, 0xA6, 0x1A,
  284. 0xD3, 0xDF, 0xB3, 0xC6, 0x0D, 0x2E, 0x43, 0x10, 0x6D, 0x87, 0x27, 0xDA,
  285. 0x37, 0xDF, 0x9C, 0xCE, 0x95, 0xB4, 0x78, 0x75, 0x5D, 0x06, 0xBC, 0xEA,
  286. 0x8F, 0x9D, 0x45, 0x96, 0x5F, 0x75, 0xA5, 0xF3, 0xD1, 0xDF, 0x37, 0x01,
  287. 0x16, 0x5F, 0xC9, 0xE5, 0x0C, 0x42, 0x79, 0xCE, 0xB0, 0x7F, 0x98, 0x95,
  288. 0x40, 0xAE, 0x96, 0xD5, 0xD8, 0x8E, 0xD7, 0x76
  289. };
  290. static const unsigned char dhtest_1024_160_xB[] = {
  291. 0x93, 0x92, 0xC9, 0xF9, 0xEB, 0x6A, 0x7A, 0x6A, 0x90, 0x22, 0xF7, 0xD8,
  292. 0x3E, 0x72, 0x23, 0xC6, 0x83, 0x5B, 0xBD, 0xDA
  293. };
  294. static const unsigned char dhtest_1024_160_yB[] = {
  295. 0x71, 0x7A, 0x6C, 0xB0, 0x53, 0x37, 0x1F, 0xF4, 0xA3, 0xB9, 0x32, 0x94,
  296. 0x1C, 0x1E, 0x56, 0x63, 0xF8, 0x61, 0xA1, 0xD6, 0xAD, 0x34, 0xAE, 0x66,
  297. 0x57, 0x6D, 0xFB, 0x98, 0xF6, 0xC6, 0xCB, 0xF9, 0xDD, 0xD5, 0xA5, 0x6C,
  298. 0x78, 0x33, 0xF6, 0xBC, 0xFD, 0xFF, 0x09, 0x55, 0x82, 0xAD, 0x86, 0x8E,
  299. 0x44, 0x0E, 0x8D, 0x09, 0xFD, 0x76, 0x9E, 0x3C, 0xEC, 0xCD, 0xC3, 0xD3,
  300. 0xB1, 0xE4, 0xCF, 0xA0, 0x57, 0x77, 0x6C, 0xAA, 0xF9, 0x73, 0x9B, 0x6A,
  301. 0x9F, 0xEE, 0x8E, 0x74, 0x11, 0xF8, 0xD6, 0xDA, 0xC0, 0x9D, 0x6A, 0x4E,
  302. 0xDB, 0x46, 0xCC, 0x2B, 0x5D, 0x52, 0x03, 0x09, 0x0E, 0xAE, 0x61, 0x26,
  303. 0x31, 0x1E, 0x53, 0xFD, 0x2C, 0x14, 0xB5, 0x74, 0xE6, 0xA3, 0x10, 0x9A,
  304. 0x3D, 0xA1, 0xBE, 0x41, 0xBD, 0xCE, 0xAA, 0x18, 0x6F, 0x5C, 0xE0, 0x67,
  305. 0x16, 0xA2, 0xB6, 0xA0, 0x7B, 0x3C, 0x33, 0xFE
  306. };
  307. static const unsigned char dhtest_1024_160_Z[] = {
  308. 0x5C, 0x80, 0x4F, 0x45, 0x4D, 0x30, 0xD9, 0xC4, 0xDF, 0x85, 0x27, 0x1F,
  309. 0x93, 0x52, 0x8C, 0x91, 0xDF, 0x6B, 0x48, 0xAB, 0x5F, 0x80, 0xB3, 0xB5,
  310. 0x9C, 0xAA, 0xC1, 0xB2, 0x8F, 0x8A, 0xCB, 0xA9, 0xCD, 0x3E, 0x39, 0xF3,
  311. 0xCB, 0x61, 0x45, 0x25, 0xD9, 0x52, 0x1D, 0x2E, 0x64, 0x4C, 0x53, 0xB8,
  312. 0x07, 0xB8, 0x10, 0xF3, 0x40, 0x06, 0x2F, 0x25, 0x7D, 0x7D, 0x6F, 0xBF,
  313. 0xE8, 0xD5, 0xE8, 0xF0, 0x72, 0xE9, 0xB6, 0xE9, 0xAF, 0xDA, 0x94, 0x13,
  314. 0xEA, 0xFB, 0x2E, 0x8B, 0x06, 0x99, 0xB1, 0xFB, 0x5A, 0x0C, 0xAC, 0xED,
  315. 0xDE, 0xAE, 0xAD, 0x7E, 0x9C, 0xFB, 0xB3, 0x6A, 0xE2, 0xB4, 0x20, 0x83,
  316. 0x5B, 0xD8, 0x3A, 0x19, 0xFB, 0x0B, 0x5E, 0x96, 0xBF, 0x8F, 0xA4, 0xD0,
  317. 0x9E, 0x34, 0x55, 0x25, 0x16, 0x7E, 0xCD, 0x91, 0x55, 0x41, 0x6F, 0x46,
  318. 0xF4, 0x08, 0xED, 0x31, 0xB6, 0x3C, 0x6E, 0x6D
  319. };
  320. static const unsigned char dhtest_2048_224_xA[] = {
  321. 0x22, 0xE6, 0x26, 0x01, 0xDB, 0xFF, 0xD0, 0x67, 0x08, 0xA6, 0x80, 0xF7,
  322. 0x47, 0xF3, 0x61, 0xF7, 0x6D, 0x8F, 0x4F, 0x72, 0x1A, 0x05, 0x48, 0xE4,
  323. 0x83, 0x29, 0x4B, 0x0C
  324. };
  325. static const unsigned char dhtest_2048_224_yA[] = {
  326. 0x1B, 0x3A, 0x63, 0x45, 0x1B, 0xD8, 0x86, 0xE6, 0x99, 0xE6, 0x7B, 0x49,
  327. 0x4E, 0x28, 0x8B, 0xD7, 0xF8, 0xE0, 0xD3, 0x70, 0xBA, 0xDD, 0xA7, 0xA0,
  328. 0xEF, 0xD2, 0xFD, 0xE7, 0xD8, 0xF6, 0x61, 0x45, 0xCC, 0x9F, 0x28, 0x04,
  329. 0x19, 0x97, 0x5E, 0xB8, 0x08, 0x87, 0x7C, 0x8A, 0x4C, 0x0C, 0x8E, 0x0B,
  330. 0xD4, 0x8D, 0x4A, 0x54, 0x01, 0xEB, 0x1E, 0x87, 0x76, 0xBF, 0xEE, 0xE1,
  331. 0x34, 0xC0, 0x38, 0x31, 0xAC, 0x27, 0x3C, 0xD9, 0xD6, 0x35, 0xAB, 0x0C,
  332. 0xE0, 0x06, 0xA4, 0x2A, 0x88, 0x7E, 0x3F, 0x52, 0xFB, 0x87, 0x66, 0xB6,
  333. 0x50, 0xF3, 0x80, 0x78, 0xBC, 0x8E, 0xE8, 0x58, 0x0C, 0xEF, 0xE2, 0x43,
  334. 0x96, 0x8C, 0xFC, 0x4F, 0x8D, 0xC3, 0xDB, 0x08, 0x45, 0x54, 0x17, 0x1D,
  335. 0x41, 0xBF, 0x2E, 0x86, 0x1B, 0x7B, 0xB4, 0xD6, 0x9D, 0xD0, 0xE0, 0x1E,
  336. 0xA3, 0x87, 0xCB, 0xAA, 0x5C, 0xA6, 0x72, 0xAF, 0xCB, 0xE8, 0xBD, 0xB9,
  337. 0xD6, 0x2D, 0x4C, 0xE1, 0x5F, 0x17, 0xDD, 0x36, 0xF9, 0x1E, 0xD1, 0xEE,
  338. 0xDD, 0x65, 0xCA, 0x4A, 0x06, 0x45, 0x5C, 0xB9, 0x4C, 0xD4, 0x0A, 0x52,
  339. 0xEC, 0x36, 0x0E, 0x84, 0xB3, 0xC9, 0x26, 0xE2, 0x2C, 0x43, 0x80, 0xA3,
  340. 0xBF, 0x30, 0x9D, 0x56, 0x84, 0x97, 0x68, 0xB7, 0xF5, 0x2C, 0xFD, 0xF6,
  341. 0x55, 0xFD, 0x05, 0x3A, 0x7E, 0xF7, 0x06, 0x97, 0x9E, 0x7E, 0x58, 0x06,
  342. 0xB1, 0x7D, 0xFA, 0xE5, 0x3A, 0xD2, 0xA5, 0xBC, 0x56, 0x8E, 0xBB, 0x52,
  343. 0x9A, 0x7A, 0x61, 0xD6, 0x8D, 0x25, 0x6F, 0x8F, 0xC9, 0x7C, 0x07, 0x4A,
  344. 0x86, 0x1D, 0x82, 0x7E, 0x2E, 0xBC, 0x8C, 0x61, 0x34, 0x55, 0x31, 0x15,
  345. 0xB7, 0x0E, 0x71, 0x03, 0x92, 0x0A, 0xA1, 0x6D, 0x85, 0xE5, 0x2B, 0xCB,
  346. 0xAB, 0x8D, 0x78, 0x6A, 0x68, 0x17, 0x8F, 0xA8, 0xFF, 0x7C, 0x2F, 0x5C,
  347. 0x71, 0x64, 0x8D, 0x6F
  348. };
  349. static const unsigned char dhtest_2048_224_xB[] = {
  350. 0x4F, 0xF3, 0xBC, 0x96, 0xC7, 0xFC, 0x6A, 0x6D, 0x71, 0xD3, 0xB3, 0x63,
  351. 0x80, 0x0A, 0x7C, 0xDF, 0xEF, 0x6F, 0xC4, 0x1B, 0x44, 0x17, 0xEA, 0x15,
  352. 0x35, 0x3B, 0x75, 0x90
  353. };
  354. static const unsigned char dhtest_2048_224_yB[] = {
  355. 0x4D, 0xCE, 0xE9, 0x92, 0xA9, 0x76, 0x2A, 0x13, 0xF2, 0xF8, 0x38, 0x44,
  356. 0xAD, 0x3D, 0x77, 0xEE, 0x0E, 0x31, 0xC9, 0x71, 0x8B, 0x3D, 0xB6, 0xC2,
  357. 0x03, 0x5D, 0x39, 0x61, 0x18, 0x2C, 0x3E, 0x0B, 0xA2, 0x47, 0xEC, 0x41,
  358. 0x82, 0xD7, 0x60, 0xCD, 0x48, 0xD9, 0x95, 0x99, 0x97, 0x06, 0x22, 0xA1,
  359. 0x88, 0x1B, 0xBA, 0x2D, 0xC8, 0x22, 0x93, 0x9C, 0x78, 0xC3, 0x91, 0x2C,
  360. 0x66, 0x61, 0xFA, 0x54, 0x38, 0xB2, 0x07, 0x66, 0x22, 0x2B, 0x75, 0xE2,
  361. 0x4C, 0x2E, 0x3A, 0xD0, 0xC7, 0x28, 0x72, 0x36, 0x12, 0x95, 0x25, 0xEE,
  362. 0x15, 0xB5, 0xDD, 0x79, 0x98, 0xAA, 0x04, 0xC4, 0xA9, 0x69, 0x6C, 0xAC,
  363. 0xD7, 0x17, 0x20, 0x83, 0xA9, 0x7A, 0x81, 0x66, 0x4E, 0xAD, 0x2C, 0x47,
  364. 0x9E, 0x44, 0x4E, 0x4C, 0x06, 0x54, 0xCC, 0x19, 0xE2, 0x8D, 0x77, 0x03,
  365. 0xCE, 0xE8, 0xDA, 0xCD, 0x61, 0x26, 0xF5, 0xD6, 0x65, 0xEC, 0x52, 0xC6,
  366. 0x72, 0x55, 0xDB, 0x92, 0x01, 0x4B, 0x03, 0x7E, 0xB6, 0x21, 0xA2, 0xAC,
  367. 0x8E, 0x36, 0x5D, 0xE0, 0x71, 0xFF, 0xC1, 0x40, 0x0A, 0xCF, 0x07, 0x7A,
  368. 0x12, 0x91, 0x3D, 0xD8, 0xDE, 0x89, 0x47, 0x34, 0x37, 0xAB, 0x7B, 0xA3,
  369. 0x46, 0x74, 0x3C, 0x1B, 0x21, 0x5D, 0xD9, 0xC1, 0x21, 0x64, 0xA7, 0xE4,
  370. 0x05, 0x31, 0x18, 0xD1, 0x99, 0xBE, 0xC8, 0xEF, 0x6F, 0xC5, 0x61, 0x17,
  371. 0x0C, 0x84, 0xC8, 0x7D, 0x10, 0xEE, 0x9A, 0x67, 0x4A, 0x1F, 0xA8, 0xFF,
  372. 0xE1, 0x3B, 0xDF, 0xBA, 0x1D, 0x44, 0xDE, 0x48, 0x94, 0x6D, 0x68, 0xDC,
  373. 0x0C, 0xDD, 0x77, 0x76, 0x35, 0xA7, 0xAB, 0x5B, 0xFB, 0x1E, 0x4B, 0xB7,
  374. 0xB8, 0x56, 0xF9, 0x68, 0x27, 0x73, 0x4C, 0x18, 0x41, 0x38, 0xE9, 0x15,
  375. 0xD9, 0xC3, 0x00, 0x2E, 0xBC, 0xE5, 0x31, 0x20, 0x54, 0x6A, 0x7E, 0x20,
  376. 0x02, 0x14, 0x2B, 0x6C
  377. };
  378. static const unsigned char dhtest_2048_224_Z[] = {
  379. 0x34, 0xD9, 0xBD, 0xDC, 0x1B, 0x42, 0x17, 0x6C, 0x31, 0x3F, 0xEA, 0x03,
  380. 0x4C, 0x21, 0x03, 0x4D, 0x07, 0x4A, 0x63, 0x13, 0xBB, 0x4E, 0xCD, 0xB3,
  381. 0x70, 0x3F, 0xFF, 0x42, 0x45, 0x67, 0xA4, 0x6B, 0xDF, 0x75, 0x53, 0x0E,
  382. 0xDE, 0x0A, 0x9D, 0xA5, 0x22, 0x9D, 0xE7, 0xD7, 0x67, 0x32, 0x28, 0x6C,
  383. 0xBC, 0x0F, 0x91, 0xDA, 0x4C, 0x3C, 0x85, 0x2F, 0xC0, 0x99, 0xC6, 0x79,
  384. 0x53, 0x1D, 0x94, 0xC7, 0x8A, 0xB0, 0x3D, 0x9D, 0xEC, 0xB0, 0xA4, 0xE4,
  385. 0xCA, 0x8B, 0x2B, 0xB4, 0x59, 0x1C, 0x40, 0x21, 0xCF, 0x8C, 0xE3, 0xA2,
  386. 0x0A, 0x54, 0x1D, 0x33, 0x99, 0x40, 0x17, 0xD0, 0x20, 0x0A, 0xE2, 0xC9,
  387. 0x51, 0x6E, 0x2F, 0xF5, 0x14, 0x57, 0x79, 0x26, 0x9E, 0x86, 0x2B, 0x0F,
  388. 0xB4, 0x74, 0xA2, 0xD5, 0x6D, 0xC3, 0x1E, 0xD5, 0x69, 0xA7, 0x70, 0x0B,
  389. 0x4C, 0x4A, 0xB1, 0x6B, 0x22, 0xA4, 0x55, 0x13, 0x53, 0x1E, 0xF5, 0x23,
  390. 0xD7, 0x12, 0x12, 0x07, 0x7B, 0x5A, 0x16, 0x9B, 0xDE, 0xFF, 0xAD, 0x7A,
  391. 0xD9, 0x60, 0x82, 0x84, 0xC7, 0x79, 0x5B, 0x6D, 0x5A, 0x51, 0x83, 0xB8,
  392. 0x70, 0x66, 0xDE, 0x17, 0xD8, 0xD6, 0x71, 0xC9, 0xEB, 0xD8, 0xEC, 0x89,
  393. 0x54, 0x4D, 0x45, 0xEC, 0x06, 0x15, 0x93, 0xD4, 0x42, 0xC6, 0x2A, 0xB9,
  394. 0xCE, 0x3B, 0x1C, 0xB9, 0x94, 0x3A, 0x1D, 0x23, 0xA5, 0xEA, 0x3B, 0xCF,
  395. 0x21, 0xA0, 0x14, 0x71, 0xE6, 0x7E, 0x00, 0x3E, 0x7F, 0x8A, 0x69, 0xC7,
  396. 0x28, 0xBE, 0x49, 0x0B, 0x2F, 0xC8, 0x8C, 0xFE, 0xB9, 0x2D, 0xB6, 0xA2,
  397. 0x15, 0xE5, 0xD0, 0x3C, 0x17, 0xC4, 0x64, 0xC9, 0xAC, 0x1A, 0x46, 0xE2,
  398. 0x03, 0xE1, 0x3F, 0x95, 0x29, 0x95, 0xFB, 0x03, 0xC6, 0x9D, 0x3C, 0xC4,
  399. 0x7F, 0xCB, 0x51, 0x0B, 0x69, 0x98, 0xFF, 0xD3, 0xAA, 0x6D, 0xE7, 0x3C,
  400. 0xF9, 0xF6, 0x38, 0x69
  401. };
  402. static const unsigned char dhtest_2048_256_xA[] = {
  403. 0x08, 0x81, 0x38, 0x2C, 0xDB, 0x87, 0x66, 0x0C, 0x6D, 0xC1, 0x3E, 0x61,
  404. 0x49, 0x38, 0xD5, 0xB9, 0xC8, 0xB2, 0xF2, 0x48, 0x58, 0x1C, 0xC5, 0xE3,
  405. 0x1B, 0x35, 0x45, 0x43, 0x97, 0xFC, 0xE5, 0x0E
  406. };
  407. static const unsigned char dhtest_2048_256_yA[] = {
  408. 0x2E, 0x93, 0x80, 0xC8, 0x32, 0x3A, 0xF9, 0x75, 0x45, 0xBC, 0x49, 0x41,
  409. 0xDE, 0xB0, 0xEC, 0x37, 0x42, 0xC6, 0x2F, 0xE0, 0xEC, 0xE8, 0x24, 0xA6,
  410. 0xAB, 0xDB, 0xE6, 0x6C, 0x59, 0xBE, 0xE0, 0x24, 0x29, 0x11, 0xBF, 0xB9,
  411. 0x67, 0x23, 0x5C, 0xEB, 0xA3, 0x5A, 0xE1, 0x3E, 0x4E, 0xC7, 0x52, 0xBE,
  412. 0x63, 0x0B, 0x92, 0xDC, 0x4B, 0xDE, 0x28, 0x47, 0xA9, 0xC6, 0x2C, 0xB8,
  413. 0x15, 0x27, 0x45, 0x42, 0x1F, 0xB7, 0xEB, 0x60, 0xA6, 0x3C, 0x0F, 0xE9,
  414. 0x15, 0x9F, 0xCC, 0xE7, 0x26, 0xCE, 0x7C, 0xD8, 0x52, 0x3D, 0x74, 0x50,
  415. 0x66, 0x7E, 0xF8, 0x40, 0xE4, 0x91, 0x91, 0x21, 0xEB, 0x5F, 0x01, 0xC8,
  416. 0xC9, 0xB0, 0xD3, 0xD6, 0x48, 0xA9, 0x3B, 0xFB, 0x75, 0x68, 0x9E, 0x82,
  417. 0x44, 0xAC, 0x13, 0x4A, 0xF5, 0x44, 0x71, 0x1C, 0xE7, 0x9A, 0x02, 0xDC,
  418. 0xC3, 0x42, 0x26, 0x68, 0x47, 0x80, 0xDD, 0xDC, 0xB4, 0x98, 0x59, 0x41,
  419. 0x06, 0xC3, 0x7F, 0x5B, 0xC7, 0x98, 0x56, 0x48, 0x7A, 0xF5, 0xAB, 0x02,
  420. 0x2A, 0x2E, 0x5E, 0x42, 0xF0, 0x98, 0x97, 0xC1, 0xA8, 0x5A, 0x11, 0xEA,
  421. 0x02, 0x12, 0xAF, 0x04, 0xD9, 0xB4, 0xCE, 0xBC, 0x93, 0x7C, 0x3C, 0x1A,
  422. 0x3E, 0x15, 0xA8, 0xA0, 0x34, 0x2E, 0x33, 0x76, 0x15, 0xC8, 0x4E, 0x7F,
  423. 0xE3, 0xB8, 0xB9, 0xB8, 0x7F, 0xB1, 0xE7, 0x3A, 0x15, 0xAF, 0x12, 0xA3,
  424. 0x0D, 0x74, 0x6E, 0x06, 0xDF, 0xC3, 0x4F, 0x29, 0x0D, 0x79, 0x7C, 0xE5,
  425. 0x1A, 0xA1, 0x3A, 0xA7, 0x85, 0xBF, 0x66, 0x58, 0xAF, 0xF5, 0xE4, 0xB0,
  426. 0x93, 0x00, 0x3C, 0xBE, 0xAF, 0x66, 0x5B, 0x3C, 0x2E, 0x11, 0x3A, 0x3A,
  427. 0x4E, 0x90, 0x52, 0x69, 0x34, 0x1D, 0xC0, 0x71, 0x14, 0x26, 0x68, 0x5F,
  428. 0x4E, 0xF3, 0x7E, 0x86, 0x8A, 0x81, 0x26, 0xFF, 0x3F, 0x22, 0x79, 0xB5,
  429. 0x7C, 0xA6, 0x7E, 0x29
  430. };
  431. static const unsigned char dhtest_2048_256_xB[] = {
  432. 0x7D, 0x62, 0xA7, 0xE3, 0xEF, 0x36, 0xDE, 0x61, 0x7B, 0x13, 0xD1, 0xAF,
  433. 0xB8, 0x2C, 0x78, 0x0D, 0x83, 0xA2, 0x3B, 0xD4, 0xEE, 0x67, 0x05, 0x64,
  434. 0x51, 0x21, 0xF3, 0x71, 0xF5, 0x46, 0xA5, 0x3D
  435. };
  436. static const unsigned char dhtest_2048_256_yB[] = {
  437. 0x57, 0x5F, 0x03, 0x51, 0xBD, 0x2B, 0x1B, 0x81, 0x74, 0x48, 0xBD, 0xF8,
  438. 0x7A, 0x6C, 0x36, 0x2C, 0x1E, 0x28, 0x9D, 0x39, 0x03, 0xA3, 0x0B, 0x98,
  439. 0x32, 0xC5, 0x74, 0x1F, 0xA2, 0x50, 0x36, 0x3E, 0x7A, 0xCB, 0xC7, 0xF7,
  440. 0x7F, 0x3D, 0xAC, 0xBC, 0x1F, 0x13, 0x1A, 0xDD, 0x8E, 0x03, 0x36, 0x7E,
  441. 0xFF, 0x8F, 0xBB, 0xB3, 0xE1, 0xC5, 0x78, 0x44, 0x24, 0x80, 0x9B, 0x25,
  442. 0xAF, 0xE4, 0xD2, 0x26, 0x2A, 0x1A, 0x6F, 0xD2, 0xFA, 0xB6, 0x41, 0x05,
  443. 0xCA, 0x30, 0xA6, 0x74, 0xE0, 0x7F, 0x78, 0x09, 0x85, 0x20, 0x88, 0x63,
  444. 0x2F, 0xC0, 0x49, 0x23, 0x37, 0x91, 0xAD, 0x4E, 0xDD, 0x08, 0x3A, 0x97,
  445. 0x8B, 0x88, 0x3E, 0xE6, 0x18, 0xBC, 0x5E, 0x0D, 0xD0, 0x47, 0x41, 0x5F,
  446. 0x2D, 0x95, 0xE6, 0x83, 0xCF, 0x14, 0x82, 0x6B, 0x5F, 0xBE, 0x10, 0xD3,
  447. 0xCE, 0x41, 0xC6, 0xC1, 0x20, 0xC7, 0x8A, 0xB2, 0x00, 0x08, 0xC6, 0x98,
  448. 0xBF, 0x7F, 0x0B, 0xCA, 0xB9, 0xD7, 0xF4, 0x07, 0xBE, 0xD0, 0xF4, 0x3A,
  449. 0xFB, 0x29, 0x70, 0xF5, 0x7F, 0x8D, 0x12, 0x04, 0x39, 0x63, 0xE6, 0x6D,
  450. 0xDD, 0x32, 0x0D, 0x59, 0x9A, 0xD9, 0x93, 0x6C, 0x8F, 0x44, 0x13, 0x7C,
  451. 0x08, 0xB1, 0x80, 0xEC, 0x5E, 0x98, 0x5C, 0xEB, 0xE1, 0x86, 0xF3, 0xD5,
  452. 0x49, 0x67, 0x7E, 0x80, 0x60, 0x73, 0x31, 0xEE, 0x17, 0xAF, 0x33, 0x80,
  453. 0xA7, 0x25, 0xB0, 0x78, 0x23, 0x17, 0xD7, 0xDD, 0x43, 0xF5, 0x9D, 0x7A,
  454. 0xF9, 0x56, 0x8A, 0x9B, 0xB6, 0x3A, 0x84, 0xD3, 0x65, 0xF9, 0x22, 0x44,
  455. 0xED, 0x12, 0x09, 0x88, 0x21, 0x93, 0x02, 0xF4, 0x29, 0x24, 0xC7, 0xCA,
  456. 0x90, 0xB8, 0x9D, 0x24, 0xF7, 0x1B, 0x0A, 0xB6, 0x97, 0x82, 0x3D, 0x7D,
  457. 0xEB, 0x1A, 0xFF, 0x5B, 0x0E, 0x8E, 0x4A, 0x45, 0xD4, 0x9F, 0x7F, 0x53,
  458. 0x75, 0x7E, 0x19, 0x13
  459. };
  460. static const unsigned char dhtest_2048_256_Z[] = {
  461. 0x86, 0xC7, 0x0B, 0xF8, 0xD0, 0xBB, 0x81, 0xBB, 0x01, 0x07, 0x8A, 0x17,
  462. 0x21, 0x9C, 0xB7, 0xD2, 0x72, 0x03, 0xDB, 0x2A, 0x19, 0xC8, 0x77, 0xF1,
  463. 0xD1, 0xF1, 0x9F, 0xD7, 0xD7, 0x7E, 0xF2, 0x25, 0x46, 0xA6, 0x8F, 0x00,
  464. 0x5A, 0xD5, 0x2D, 0xC8, 0x45, 0x53, 0xB7, 0x8F, 0xC6, 0x03, 0x30, 0xBE,
  465. 0x51, 0xEA, 0x7C, 0x06, 0x72, 0xCA, 0xC1, 0x51, 0x5E, 0x4B, 0x35, 0xC0,
  466. 0x47, 0xB9, 0xA5, 0x51, 0xB8, 0x8F, 0x39, 0xDC, 0x26, 0xDA, 0x14, 0xA0,
  467. 0x9E, 0xF7, 0x47, 0x74, 0xD4, 0x7C, 0x76, 0x2D, 0xD1, 0x77, 0xF9, 0xED,
  468. 0x5B, 0xC2, 0xF1, 0x1E, 0x52, 0xC8, 0x79, 0xBD, 0x95, 0x09, 0x85, 0x04,
  469. 0xCD, 0x9E, 0xEC, 0xD8, 0xA8, 0xF9, 0xB3, 0xEF, 0xBD, 0x1F, 0x00, 0x8A,
  470. 0xC5, 0x85, 0x30, 0x97, 0xD9, 0xD1, 0x83, 0x7F, 0x2B, 0x18, 0xF7, 0x7C,
  471. 0xD7, 0xBE, 0x01, 0xAF, 0x80, 0xA7, 0xC7, 0xB5, 0xEA, 0x3C, 0xA5, 0x4C,
  472. 0xC0, 0x2D, 0x0C, 0x11, 0x6F, 0xEE, 0x3F, 0x95, 0xBB, 0x87, 0x39, 0x93,
  473. 0x85, 0x87, 0x5D, 0x7E, 0x86, 0x74, 0x7E, 0x67, 0x6E, 0x72, 0x89, 0x38,
  474. 0xAC, 0xBF, 0xF7, 0x09, 0x8E, 0x05, 0xBE, 0x4D, 0xCF, 0xB2, 0x40, 0x52,
  475. 0xB8, 0x3A, 0xEF, 0xFB, 0x14, 0x78, 0x3F, 0x02, 0x9A, 0xDB, 0xDE, 0x7F,
  476. 0x53, 0xFA, 0xE9, 0x20, 0x84, 0x22, 0x40, 0x90, 0xE0, 0x07, 0xCE, 0xE9,
  477. 0x4D, 0x4B, 0xF2, 0xBA, 0xCE, 0x9F, 0xFD, 0x4B, 0x57, 0xD2, 0xAF, 0x7C,
  478. 0x72, 0x4D, 0x0C, 0xAA, 0x19, 0xBF, 0x05, 0x01, 0xF6, 0xF1, 0x7B, 0x4A,
  479. 0xA1, 0x0F, 0x42, 0x5E, 0x3E, 0xA7, 0x60, 0x80, 0xB4, 0xB9, 0xD6, 0xB3,
  480. 0xCE, 0xFE, 0xA1, 0x15, 0xB2, 0xCE, 0xB8, 0x78, 0x9B, 0xB8, 0xA3, 0xB0,
  481. 0xEA, 0x87, 0xFE, 0xBE, 0x63, 0xB6, 0xC8, 0xF8, 0x46, 0xEC, 0x6D, 0xB0,
  482. 0xC2, 0x6C, 0x5D, 0x7C
  483. };
  484. typedef struct {
  485. DH *(*get_param) (void);
  486. const unsigned char *xA;
  487. size_t xA_len;
  488. const unsigned char *yA;
  489. size_t yA_len;
  490. const unsigned char *xB;
  491. size_t xB_len;
  492. const unsigned char *yB;
  493. size_t yB_len;
  494. const unsigned char *Z;
  495. size_t Z_len;
  496. } rfc5114_td;
  497. # define make_rfc5114_td(pre) { \
  498. DH_get_##pre, \
  499. dhtest_##pre##_xA, sizeof(dhtest_##pre##_xA), \
  500. dhtest_##pre##_yA, sizeof(dhtest_##pre##_yA), \
  501. dhtest_##pre##_xB, sizeof(dhtest_##pre##_xB), \
  502. dhtest_##pre##_yB, sizeof(dhtest_##pre##_yB), \
  503. dhtest_##pre##_Z, sizeof(dhtest_##pre##_Z) \
  504. }
  505. static const rfc5114_td rfctd[] = {
  506. make_rfc5114_td(1024_160),
  507. make_rfc5114_td(2048_224),
  508. make_rfc5114_td(2048_256)
  509. };
  510. static int rfc5114_test(void)
  511. {
  512. int i;
  513. DH *dhA = NULL;
  514. DH *dhB = NULL;
  515. unsigned char *Z1 = NULL;
  516. unsigned char *Z2 = NULL;
  517. int szA, szB;
  518. const rfc5114_td *td = NULL;
  519. BIGNUM *priv_key = NULL, *pub_key = NULL;
  520. const BIGNUM *pub_key_tmp;
  521. for (i = 0; i < (int)OSSL_NELEM(rfctd); i++) {
  522. td = rfctd + i;
  523. /* Set up DH structures setting key components */
  524. if (!TEST_ptr(dhA = td->get_param())
  525. || !TEST_ptr(dhB = td->get_param()))
  526. goto bad_err;
  527. if (!TEST_ptr(priv_key = BN_bin2bn(td->xA, td->xA_len, NULL))
  528. || !TEST_ptr(pub_key = BN_bin2bn(td->yA, td->yA_len, NULL))
  529. || !TEST_true(DH_set0_key(dhA, pub_key, priv_key)))
  530. goto bad_err;
  531. if (!TEST_ptr(priv_key = BN_bin2bn(td->xB, td->xB_len, NULL))
  532. || !TEST_ptr(pub_key = BN_bin2bn(td->yB, td->yB_len, NULL))
  533. || !TEST_true(DH_set0_key(dhB, pub_key, priv_key)))
  534. goto bad_err;
  535. priv_key = pub_key = NULL;
  536. if (!TEST_int_gt(szA = DH_size(dhA), 0)
  537. || !TEST_int_gt(szB = DH_size(dhB), 0)
  538. || !TEST_size_t_eq(td->Z_len, (size_t)szA)
  539. || !TEST_size_t_eq(td->Z_len, (size_t)szB))
  540. goto err;
  541. if (!TEST_ptr(Z1 = OPENSSL_malloc((size_t)szA))
  542. || !TEST_ptr(Z2 = OPENSSL_malloc((size_t)szB)))
  543. goto bad_err;
  544. /*
  545. * Work out shared secrets using both sides and compare with expected
  546. * values.
  547. */
  548. DH_get0_key(dhB, &pub_key_tmp, NULL);
  549. if (!TEST_int_ne(DH_compute_key(Z1, pub_key_tmp, dhA), -1))
  550. goto bad_err;
  551. DH_get0_key(dhA, &pub_key_tmp, NULL);
  552. if (!TEST_int_ne(DH_compute_key(Z2, pub_key_tmp, dhB), -1))
  553. goto bad_err;
  554. if (!TEST_mem_eq(Z1, td->Z_len, td->Z, td->Z_len)
  555. || !TEST_mem_eq(Z2, td->Z_len, td->Z, td->Z_len))
  556. goto err;
  557. DH_free(dhA);
  558. dhA = NULL;
  559. DH_free(dhB);
  560. dhB = NULL;
  561. OPENSSL_free(Z1);
  562. Z1 = NULL;
  563. OPENSSL_free(Z2);
  564. Z2 = NULL;
  565. }
  566. return 1;
  567. bad_err:
  568. DH_free(dhA);
  569. DH_free(dhB);
  570. BN_free(pub_key);
  571. BN_free(priv_key);
  572. OPENSSL_free(Z1);
  573. OPENSSL_free(Z2);
  574. TEST_error("Initialisation error RFC5114 set %d\n", i + 1);
  575. return 0;
  576. err:
  577. DH_free(dhA);
  578. DH_free(dhB);
  579. OPENSSL_free(Z1);
  580. OPENSSL_free(Z2);
  581. TEST_error("Test failed RFC5114 set %d\n", i + 1);
  582. return 0;
  583. }
  584. static int rfc7919_test(void)
  585. {
  586. DH *a = NULL, *b = NULL;
  587. const BIGNUM *apub_key = NULL, *bpub_key = NULL;
  588. unsigned char *abuf = NULL;
  589. unsigned char *bbuf = NULL;
  590. int i, alen, blen, aout, bout;
  591. int ret = 0;
  592. if (!TEST_ptr(a = DH_new_by_nid(NID_ffdhe2048)))
  593. goto err;
  594. if (!DH_check(a, &i))
  595. goto err;
  596. if (!TEST_false(i & DH_CHECK_P_NOT_PRIME)
  597. || !TEST_false(i & DH_CHECK_P_NOT_SAFE_PRIME)
  598. || !TEST_false(i & DH_UNABLE_TO_CHECK_GENERATOR)
  599. || !TEST_false(i & DH_NOT_SUITABLE_GENERATOR)
  600. || !TEST_false(i))
  601. goto err;
  602. if (!DH_generate_key(a))
  603. goto err;
  604. DH_get0_key(a, &apub_key, NULL);
  605. /* now create another copy of the DH group for the peer */
  606. if (!TEST_ptr(b = DH_new_by_nid(NID_ffdhe2048)))
  607. goto err;
  608. if (!DH_generate_key(b))
  609. goto err;
  610. DH_get0_key(b, &bpub_key, NULL);
  611. alen = DH_size(a);
  612. if (!TEST_int_gt(alen, 0) || !TEST_ptr(abuf = OPENSSL_malloc(alen))
  613. || !TEST_true((aout = DH_compute_key(abuf, bpub_key, a)) != -1))
  614. goto err;
  615. blen = DH_size(b);
  616. if (!TEST_int_gt(blen, 0) || !TEST_ptr(bbuf = OPENSSL_malloc(blen))
  617. || !TEST_true((bout = DH_compute_key(bbuf, apub_key, b)) != -1))
  618. goto err;
  619. if (!TEST_true(aout >= 20)
  620. || !TEST_mem_eq(abuf, aout, bbuf, bout))
  621. goto err;
  622. ret = 1;
  623. err:
  624. OPENSSL_free(abuf);
  625. OPENSSL_free(bbuf);
  626. DH_free(a);
  627. DH_free(b);
  628. return ret;
  629. }
  630. static int prime_groups[] = {
  631. NID_ffdhe2048,
  632. NID_ffdhe3072,
  633. NID_ffdhe4096,
  634. NID_ffdhe6144,
  635. NID_ffdhe8192,
  636. NID_modp_2048,
  637. NID_modp_3072,
  638. NID_modp_4096,
  639. NID_modp_6144,
  640. };
  641. static int dh_test_prime_groups(int index)
  642. {
  643. int ok = 0;
  644. DH *dh = NULL;
  645. const BIGNUM *p, *q, *g;
  646. if (!TEST_ptr(dh = DH_new_by_nid(prime_groups[index])))
  647. goto err;
  648. DH_get0_pqg(dh, &p, &q, &g);
  649. if (!TEST_ptr(p) || !TEST_ptr(q) || !TEST_ptr(g))
  650. goto err;
  651. if (!TEST_int_eq(DH_get_nid(dh), prime_groups[index]))
  652. goto err;
  653. /* Since q is set there is no need for the private length to be set */
  654. if (!TEST_int_eq((int)DH_get_length(dh), 0))
  655. goto err;
  656. ok = 1;
  657. err:
  658. DH_free(dh);
  659. return ok;
  660. }
  661. static int dh_rfc5114_fix_nid_test(void)
  662. {
  663. int ok = 0;
  664. EVP_PKEY_CTX *paramgen_ctx;
  665. /* Run the test. Success is any time the test does not cause a SIGSEGV interrupt */
  666. paramgen_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DHX, 0);
  667. if (!TEST_ptr(paramgen_ctx))
  668. goto err;
  669. if (!TEST_int_eq(EVP_PKEY_paramgen_init(paramgen_ctx), 1))
  670. goto err;
  671. /* Tested function is called here */
  672. if (!TEST_int_eq(EVP_PKEY_CTX_set_dhx_rfc5114(paramgen_ctx, 3), 1))
  673. goto err;
  674. /* Negative test */
  675. if (!TEST_int_eq(EVP_PKEY_CTX_set_dhx_rfc5114(paramgen_ctx, 99), 0))
  676. goto err;
  677. /* If we're still running then the test passed. */
  678. ok = 1;
  679. err:
  680. EVP_PKEY_CTX_free(paramgen_ctx);
  681. return ok;
  682. }
  683. static int dh_set_dh_nid_test(void)
  684. {
  685. int ok = 0;
  686. EVP_PKEY_CTX *paramgen_ctx;
  687. /* Run the test. Success is any time the test does not cause a SIGSEGV interrupt */
  688. paramgen_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, 0);
  689. if (!TEST_ptr(paramgen_ctx))
  690. goto err;
  691. if (!TEST_int_eq(EVP_PKEY_paramgen_init(paramgen_ctx), 1))
  692. goto err;
  693. /* Tested function is called here */
  694. if (!TEST_int_eq(EVP_PKEY_CTX_set_dh_nid(paramgen_ctx, NID_ffdhe2048), 1))
  695. goto err;
  696. /* Negative test */
  697. if (!TEST_int_eq(EVP_PKEY_CTX_set_dh_nid(paramgen_ctx, NID_secp521r1), 0))
  698. goto err;
  699. /* If we're still running then the test passed. */
  700. ok = 1;
  701. err:
  702. EVP_PKEY_CTX_free(paramgen_ctx);
  703. return ok;
  704. }
  705. static int dh_get_nid(void)
  706. {
  707. int ok = 0;
  708. const BIGNUM *p, *q, *g;
  709. BIGNUM *pcpy = NULL, *gcpy = NULL, *qcpy = NULL;
  710. DH *dh1 = DH_new_by_nid(NID_ffdhe2048);
  711. DH *dh2 = DH_new();
  712. if (!TEST_ptr(dh1)
  713. || !TEST_ptr(dh2))
  714. goto err;
  715. /* Set new DH parameters manually using a existing named group's p & g */
  716. DH_get0_pqg(dh1, &p, &q, &g);
  717. if (!TEST_ptr(p)
  718. || !TEST_ptr(q)
  719. || !TEST_ptr(g)
  720. || !TEST_ptr(pcpy = BN_dup(p))
  721. || !TEST_ptr(gcpy = BN_dup(g)))
  722. goto err;
  723. if (!TEST_true(DH_set0_pqg(dh2, pcpy, NULL, gcpy)))
  724. goto err;
  725. pcpy = gcpy = NULL;
  726. /* Test q is set if p and g are provided */
  727. if (!TEST_ptr(DH_get0_q(dh2)))
  728. goto err;
  729. /* Test that setting p & g manually returns that it is a named group */
  730. if (!TEST_int_eq(DH_get_nid(dh2), NID_ffdhe2048))
  731. goto err;
  732. /* Test that after changing g it is no longer a named group */
  733. if (!TEST_ptr(gcpy = BN_dup(BN_value_one())))
  734. goto err;
  735. if (!TEST_true(DH_set0_pqg(dh2, NULL, NULL, gcpy)))
  736. goto err;
  737. gcpy = NULL;
  738. if (!TEST_int_eq(DH_get_nid(dh2), NID_undef))
  739. goto err;
  740. /* Test that setting an incorrect q results in this not being a named group */
  741. if (!TEST_ptr(pcpy = BN_dup(p))
  742. || !TEST_ptr(qcpy = BN_dup(q))
  743. || !TEST_ptr(gcpy = BN_dup(g))
  744. || !TEST_int_eq(BN_add_word(qcpy, 2), 1)
  745. || !TEST_true(DH_set0_pqg(dh2, pcpy, qcpy, gcpy)))
  746. goto err;
  747. pcpy = qcpy = gcpy = NULL;
  748. if (!TEST_int_eq(DH_get_nid(dh2), NID_undef))
  749. goto err;
  750. ok = 1;
  751. err:
  752. BN_free(pcpy);
  753. BN_free(qcpy);
  754. BN_free(gcpy);
  755. DH_free(dh2);
  756. DH_free(dh1);
  757. return ok;
  758. }
  759. static const unsigned char dh_pub_der[] = {
  760. 0x30, 0x82, 0x02, 0x28, 0x30, 0x82, 0x01, 0x1b, 0x06, 0x09, 0x2a, 0x86,
  761. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x03, 0x01, 0x30, 0x82, 0x01, 0x0c, 0x02,
  762. 0x82, 0x01, 0x01, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  763. 0xc9, 0x0f, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b,
  764. 0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67, 0xcc, 0x74,
  765. 0x02, 0x0b, 0xbe, 0xa6, 0x3b, 0x13, 0x9b, 0x22, 0x51, 0x4a, 0x08, 0x79,
  766. 0x8e, 0x34, 0x04, 0xdd, 0xef, 0x95, 0x19, 0xb3, 0xcd, 0x3a, 0x43, 0x1b,
  767. 0x30, 0x2b, 0x0a, 0x6d, 0xf2, 0x5f, 0x14, 0x37, 0x4f, 0xe1, 0x35, 0x6d,
  768. 0x6d, 0x51, 0xc2, 0x45, 0xe4, 0x85, 0xb5, 0x76, 0x62, 0x5e, 0x7e, 0xc6,
  769. 0xf4, 0x4c, 0x42, 0xe9, 0xa6, 0x37, 0xed, 0x6b, 0x0b, 0xff, 0x5c, 0xb6,
  770. 0xf4, 0x06, 0xb7, 0xed, 0xee, 0x38, 0x6b, 0xfb, 0x5a, 0x89, 0x9f, 0xa5,
  771. 0xae, 0x9f, 0x24, 0x11, 0x7c, 0x4b, 0x1f, 0xe6, 0x49, 0x28, 0x66, 0x51,
  772. 0xec, 0xe4, 0x5b, 0x3d, 0xc2, 0x00, 0x7c, 0xb8, 0xa1, 0x63, 0xbf, 0x05,
  773. 0x98, 0xda, 0x48, 0x36, 0x1c, 0x55, 0xd3, 0x9a, 0x69, 0x16, 0x3f, 0xa8,
  774. 0xfd, 0x24, 0xcf, 0x5f, 0x83, 0x65, 0x5d, 0x23, 0xdc, 0xa3, 0xad, 0x96,
  775. 0x1c, 0x62, 0xf3, 0x56, 0x20, 0x85, 0x52, 0xbb, 0x9e, 0xd5, 0x29, 0x07,
  776. 0x70, 0x96, 0x96, 0x6d, 0x67, 0x0c, 0x35, 0x4e, 0x4a, 0xbc, 0x98, 0x04,
  777. 0xf1, 0x74, 0x6c, 0x08, 0xca, 0x18, 0x21, 0x7c, 0x32, 0x90, 0x5e, 0x46,
  778. 0x2e, 0x36, 0xce, 0x3b, 0xe3, 0x9e, 0x77, 0x2c, 0x18, 0x0e, 0x86, 0x03,
  779. 0x9b, 0x27, 0x83, 0xa2, 0xec, 0x07, 0xa2, 0x8f, 0xb5, 0xc5, 0x5d, 0xf0,
  780. 0x6f, 0x4c, 0x52, 0xc9, 0xde, 0x2b, 0xcb, 0xf6, 0x95, 0x58, 0x17, 0x18,
  781. 0x39, 0x95, 0x49, 0x7c, 0xea, 0x95, 0x6a, 0xe5, 0x15, 0xd2, 0x26, 0x18,
  782. 0x98, 0xfa, 0x05, 0x10, 0x15, 0x72, 0x8e, 0x5a, 0x8a, 0xac, 0xaa, 0x68,
  783. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x02, 0x01, 0x02, 0x02,
  784. 0x02, 0x04, 0x00, 0x03, 0x82, 0x01, 0x05, 0x00, 0x02, 0x82, 0x01, 0x00,
  785. 0x08, 0x87, 0x8a, 0x5f, 0x4f, 0x3b, 0xef, 0xe1, 0x77, 0x13, 0x3b, 0xd7,
  786. 0x58, 0x76, 0xc9, 0xeb, 0x7e, 0x2d, 0xcc, 0x7e, 0xed, 0xc5, 0xee, 0xf9,
  787. 0x2d, 0x55, 0xb0, 0xe2, 0x37, 0x8c, 0x51, 0x87, 0x6a, 0x8e, 0x0d, 0xb2,
  788. 0x08, 0xed, 0x4f, 0x88, 0x9b, 0x63, 0x19, 0x7a, 0x67, 0xa1, 0x61, 0xd8,
  789. 0x17, 0xa0, 0x2c, 0xdb, 0xc2, 0xfa, 0xb3, 0x4f, 0xe7, 0xcb, 0x16, 0xf2,
  790. 0xe7, 0xd0, 0x2c, 0xf8, 0xcc, 0x97, 0xd3, 0xe7, 0xae, 0xc2, 0x71, 0xd8,
  791. 0x2b, 0x12, 0x83, 0xe9, 0x5a, 0x45, 0xfe, 0x66, 0x5c, 0xa2, 0xb6, 0xce,
  792. 0x2f, 0x04, 0x05, 0xe7, 0xa7, 0xbc, 0xe5, 0x63, 0x1a, 0x93, 0x3d, 0x4d,
  793. 0xf4, 0x77, 0xdd, 0x2a, 0xc9, 0x51, 0x7b, 0xf5, 0x54, 0xa2, 0xab, 0x26,
  794. 0xee, 0x16, 0xd3, 0x83, 0x92, 0x85, 0x40, 0x67, 0xa3, 0xa9, 0x31, 0x16,
  795. 0x64, 0x45, 0x5a, 0x2a, 0x9d, 0xa8, 0x1a, 0x84, 0x2f, 0x59, 0x57, 0x6b,
  796. 0xbb, 0x51, 0x28, 0xbd, 0x91, 0x60, 0xd9, 0x8f, 0x54, 0x6a, 0xa0, 0x6b,
  797. 0xb2, 0xf6, 0x78, 0x79, 0xd2, 0x3a, 0x8f, 0xa6, 0x24, 0x7e, 0xe9, 0x6e,
  798. 0x66, 0x30, 0xed, 0xbf, 0x55, 0x71, 0x9c, 0x89, 0x81, 0xf0, 0xa7, 0xe7,
  799. 0x05, 0x87, 0x51, 0xc1, 0xff, 0xe5, 0xcf, 0x1f, 0x19, 0xe4, 0xeb, 0x7c,
  800. 0x1c, 0x1a, 0x58, 0xd5, 0x22, 0x3d, 0x31, 0x22, 0xc7, 0x8b, 0x60, 0xf5,
  801. 0xe8, 0x95, 0x73, 0xe0, 0x20, 0xe2, 0x4f, 0x03, 0x9e, 0x89, 0x34, 0x91,
  802. 0x5e, 0xda, 0x4f, 0x60, 0xff, 0xc9, 0x4f, 0x5a, 0x37, 0x1e, 0xb0, 0xed,
  803. 0x26, 0x4c, 0xa4, 0xc6, 0x26, 0xc9, 0xcc, 0xab, 0xd2, 0x1a, 0x3a, 0x82,
  804. 0x68, 0x03, 0x49, 0x8f, 0xb0, 0xb9, 0xc8, 0x48, 0x9d, 0xc7, 0xdf, 0x8b,
  805. 0x1c, 0xbf, 0xda, 0x89, 0x78, 0x6f, 0xd3, 0x62, 0xad, 0x35, 0xb9, 0xd3,
  806. 0x9b, 0xd0, 0x25, 0x65
  807. };
  808. /*
  809. * Load PKCS3 DH Parameters that contain an optional private value length.
  810. * Loading a named group should not overwrite the private value length field.
  811. */
  812. static int dh_load_pkcs3_namedgroup_privlen_test(void)
  813. {
  814. int ret, privlen = 0;
  815. EVP_PKEY *pkey = NULL;
  816. const unsigned char *p = dh_pub_der;
  817. ret = TEST_ptr(pkey = d2i_PUBKEY_ex(NULL, &p, sizeof(dh_pub_der),
  818. NULL, NULL))
  819. && TEST_true(EVP_PKEY_get_int_param(pkey, OSSL_PKEY_PARAM_DH_PRIV_LEN,
  820. &privlen))
  821. && TEST_int_eq(privlen, 1024);
  822. EVP_PKEY_free(pkey);
  823. return ret;
  824. }
  825. #endif
  826. int setup_tests(void)
  827. {
  828. #ifdef OPENSSL_NO_DH
  829. TEST_note("No DH support");
  830. #else
  831. ADD_TEST(dh_test);
  832. ADD_TEST(dh_computekey_range_test);
  833. ADD_TEST(rfc5114_test);
  834. ADD_TEST(rfc7919_test);
  835. ADD_ALL_TESTS(dh_test_prime_groups, OSSL_NELEM(prime_groups));
  836. ADD_TEST(dh_get_nid);
  837. ADD_TEST(dh_load_pkcs3_namedgroup_privlen_test);
  838. ADD_TEST(dh_rfc5114_fix_nid_test);
  839. ADD_TEST(dh_set_dh_nid_test);
  840. #endif
  841. return 1;
  842. }