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