xref: /freebsd/crypto/openssl/test/evp_pkey_dparams_test.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert #include <stdio.h>
11*e0c4386eSCy Schubert #include <stdlib.h>
12*e0c4386eSCy Schubert #include <string.h>
13*e0c4386eSCy Schubert 
14*e0c4386eSCy Schubert #include "internal/nelem.h"
15*e0c4386eSCy Schubert #include <openssl/crypto.h>
16*e0c4386eSCy Schubert #include <openssl/bio.h>
17*e0c4386eSCy Schubert #include <openssl/bn.h>
18*e0c4386eSCy Schubert #include <openssl/rand.h>
19*e0c4386eSCy Schubert #include <openssl/err.h>
20*e0c4386eSCy Schubert #include <openssl/evp.h>
21*e0c4386eSCy Schubert #include <openssl/ec.h>
22*e0c4386eSCy Schubert #include "testutil.h"
23*e0c4386eSCy Schubert 
24*e0c4386eSCy Schubert #if defined(OPENSSL_NO_DH) && defined(OPENSSL_NO_DSA) && defined(OPENSSL_NO_EC)
25*e0c4386eSCy Schubert # define OPENSSL_NO_KEYPARAMS
26*e0c4386eSCy Schubert #endif
27*e0c4386eSCy Schubert 
28*e0c4386eSCy Schubert #ifndef OPENSSL_NO_KEYPARAMS
29*e0c4386eSCy Schubert 
30*e0c4386eSCy Schubert struct pubkey {
31*e0c4386eSCy Schubert     int bad;
32*e0c4386eSCy Schubert     const unsigned char *key_bin;
33*e0c4386eSCy Schubert     size_t key_bin_len;
34*e0c4386eSCy Schubert };
35*e0c4386eSCy Schubert 
36*e0c4386eSCy Schubert # ifndef OPENSSL_NO_DH
37*e0c4386eSCy Schubert static const unsigned char dhparam_bin[] = {
38*e0c4386eSCy Schubert 0x30,0x82,0x01,0x08,0x02,0x82,0x01,0x01,0x00,0xc0,0xd1,0x2e,0x14,0x18,0xbd,0x03,
39*e0c4386eSCy Schubert 0xfd,0x39,0xe1,0x99,0xf4,0x93,0x06,0x2d,0x49,0xc6,0xb5,0xb9,0xf0,0x91,0xcb,0x2f,
40*e0c4386eSCy Schubert 0x48,0x54,0x79,0x7d,0xc4,0x65,0x11,0x55,0xf7,0x99,0xde,0x42,0x83,0x84,0xc0,0xf8,
41*e0c4386eSCy Schubert 0x88,0x89,0xa0,0xff,0xff,0x7d,0xe8,0xef,0x9e,0xbc,0xf7,0x1d,0x70,0x6d,0x3a,0x33,
42*e0c4386eSCy Schubert 0x49,0x28,0xa1,0xa3,0xe1,0x41,0xc4,0x8b,0x91,0xf9,0xf2,0xb6,0xe2,0x77,0x79,0x38,
43*e0c4386eSCy Schubert 0x7d,0x21,0xb3,0xdf,0x79,0x9c,0x5e,0x65,0x16,0x00,0x16,0x82,0xb2,0x36,0x46,0x21,
44*e0c4386eSCy Schubert 0xac,0xaf,0x86,0xc7,0xe3,0x10,0x44,0x48,0xfb,0xbd,0xad,0x4e,0x11,0x73,0x4c,0x25,
45*e0c4386eSCy Schubert 0xb0,0x8c,0x1c,0x1e,0x8e,0x58,0x50,0x5e,0x43,0x89,0xe4,0xd9,0x34,0xf8,0x3b,0xcc,
46*e0c4386eSCy Schubert 0x36,0x2c,0x1b,0xb3,0xb2,0x77,0x0c,0xa5,0x96,0xc1,0x8a,0x38,0xd4,0xe3,0x9c,0x2a,
47*e0c4386eSCy Schubert 0xde,0x49,0x46,0xc7,0xd4,0xa2,0x47,0xc9,0x0a,0xbd,0x84,0xd4,0x1c,0xbc,0xb6,0x19,
48*e0c4386eSCy Schubert 0x04,0x94,0x64,0xfa,0x8a,0x11,0x9c,0x5f,0x4a,0x4c,0x0f,0x58,0x81,0x02,0xbf,0xcf,
49*e0c4386eSCy Schubert 0x87,0x27,0x2b,0xae,0x8e,0xe2,0x61,0x7a,0xdb,0xba,0x23,0x39,0x25,0x44,0xdc,0x22,
50*e0c4386eSCy Schubert 0x75,0xc3,0x28,0xd9,0x12,0x33,0x84,0x32,0xd4,0x5d,0xd9,0x77,0xf8,0x04,0x90,0x38,
51*e0c4386eSCy Schubert 0x0a,0xec,0x84,0x93,0x43,0xce,0xe7,0x07,0x42,0x7d,0x2d,0xe0,0x21,0x3b,0x19,0x22,
52*e0c4386eSCy Schubert 0xa7,0x8f,0x50,0x31,0xda,0xd0,0x0d,0xd3,0x0b,0xdb,0xad,0xed,0x94,0x92,0xff,0x83,
53*e0c4386eSCy Schubert 0x06,0x7f,0x7f,0xd7,0x7b,0x42,0x5b,0xba,0x93,0x7a,0xeb,0x43,0x5f,0xce,0x59,0x26,
54*e0c4386eSCy Schubert 0xe8,0x76,0xdc,0xee,0xe2,0xbe,0x36,0x7a,0x83,0x02,0x01,0x02
55*e0c4386eSCy Schubert };
56*e0c4386eSCy Schubert static const unsigned char dhkey_1[] = {
57*e0c4386eSCy Schubert     0x7a, 0x49, 0xcb, 0xc3, 0x25, 0x67, 0x7a, 0x61,
58*e0c4386eSCy Schubert     0xd0, 0x60, 0x81, 0x0f, 0xf6, 0xbd, 0x38, 0x82,
59*e0c4386eSCy Schubert     0xe7, 0x38, 0x8c, 0xe9, 0xd1, 0x04, 0x33, 0xbf,
60*e0c4386eSCy Schubert     0x8a, 0x03, 0x63, 0xb3, 0x05, 0x04, 0xb5, 0x1f,
61*e0c4386eSCy Schubert     0xba, 0x9f, 0x1a, 0x5f, 0x31, 0x3e, 0x96, 0x79,
62*e0c4386eSCy Schubert     0x88, 0x7d, 0x3f, 0x59, 0x6d, 0x3b, 0xf3, 0x2f,
63*e0c4386eSCy Schubert     0xf2, 0xa6, 0x43, 0x48, 0x64, 0x5a, 0x6a, 0x32,
64*e0c4386eSCy Schubert     0x1f, 0x24, 0x37, 0x62, 0x54, 0x3a, 0x7d, 0xab,
65*e0c4386eSCy Schubert     0x26, 0x77, 0x7c, 0xec, 0x57, 0x3c, 0xa4, 0xbd,
66*e0c4386eSCy Schubert     0x96, 0x9d, 0xaa, 0x3b, 0x0e, 0x9a, 0x55, 0x7e,
67*e0c4386eSCy Schubert     0x1d, 0xb4, 0x47, 0x5b, 0xea, 0x20, 0x3c, 0x6d,
68*e0c4386eSCy Schubert     0xbe, 0xd6, 0x70, 0x7d, 0xa8, 0x9e, 0x84, 0xb4,
69*e0c4386eSCy Schubert     0x03, 0x52, 0xf2, 0x08, 0x4c, 0x98, 0xd3, 0x4f,
70*e0c4386eSCy Schubert     0x58, 0xb3, 0xdf, 0xb4, 0xe6, 0xdc, 0x2c, 0x43,
71*e0c4386eSCy Schubert     0x55, 0xd1, 0xce, 0x2a, 0xb3, 0xfc, 0xe0, 0x29,
72*e0c4386eSCy Schubert     0x97, 0xd8, 0xd8, 0x62, 0xc6, 0x87, 0x0a, 0x1b,
73*e0c4386eSCy Schubert     0xfd, 0x72, 0x74, 0xe0, 0xa9, 0xfb, 0xfa, 0x91,
74*e0c4386eSCy Schubert     0xf2, 0xc1, 0x09, 0x93, 0xea, 0x63, 0xf6, 0x9a,
75*e0c4386eSCy Schubert     0x4b, 0xdf, 0x4e, 0xdf, 0x6b, 0xf9, 0xeb, 0xf6,
76*e0c4386eSCy Schubert     0x66, 0x3c, 0xfd, 0x6f, 0x68, 0xcb, 0xdb, 0x6e,
77*e0c4386eSCy Schubert     0x40, 0x65, 0xf7, 0xf2, 0x46, 0xe5, 0x0d, 0x9a,
78*e0c4386eSCy Schubert     0xd9, 0x6f, 0xcf, 0x28, 0x22, 0x8f, 0xca, 0x0b,
79*e0c4386eSCy Schubert     0x30, 0xa0, 0x9e, 0xa5, 0x13, 0xba, 0x72, 0x7f,
80*e0c4386eSCy Schubert     0x85, 0x3d, 0x02, 0x9c, 0x97, 0x8e, 0x6f, 0xea,
81*e0c4386eSCy Schubert     0x6d, 0x35, 0x4e, 0xd1, 0x78, 0x7d, 0x73, 0x60,
82*e0c4386eSCy Schubert     0x92, 0xa9, 0x12, 0xf4, 0x2a, 0xac, 0x17, 0x97,
83*e0c4386eSCy Schubert     0xf3, 0x7b, 0x79, 0x08, 0x69, 0xd1, 0x9e, 0xb5,
84*e0c4386eSCy Schubert     0xf8, 0x2a, 0x0a, 0x2b, 0x00, 0x7b, 0x16, 0x8d,
85*e0c4386eSCy Schubert     0x41, 0x82, 0x3a, 0x72, 0x58, 0x57, 0x80, 0x65,
86*e0c4386eSCy Schubert     0xae, 0x17, 0xbc, 0x3a, 0x5b, 0x7e, 0x5c, 0x2d,
87*e0c4386eSCy Schubert     0xae, 0xb2, 0xc2, 0x26, 0x20, 0x9a, 0xaa, 0x57,
88*e0c4386eSCy Schubert     0x4b, 0x7d, 0x43, 0x41, 0x96, 0x3f, 0xf0, 0x0d
89*e0c4386eSCy Schubert };
90*e0c4386eSCy Schubert /* smaller but still valid key */
91*e0c4386eSCy Schubert static const unsigned char dhkey_2[] = {
92*e0c4386eSCy Schubert     0x73, 0xb2, 0x22, 0x91, 0x27, 0xb9, 0x45, 0xb0,
93*e0c4386eSCy Schubert     0xfd, 0x17, 0x66, 0x79, 0x9b, 0x32, 0x71, 0x92,
94*e0c4386eSCy Schubert     0x97, 0x1d, 0x70, 0x02, 0x37, 0x70, 0x79, 0x63,
95*e0c4386eSCy Schubert     0xed, 0x11, 0x22, 0xe9, 0xe6, 0xf8, 0xeb, 0xd7,
96*e0c4386eSCy Schubert     0x90, 0x00, 0xe6, 0x5c, 0x47, 0x02, 0xfb, 0x13,
97*e0c4386eSCy Schubert     0xca, 0x29, 0x14, 0x1e, 0xf4, 0x61, 0x58, 0xf6,
98*e0c4386eSCy Schubert     0xaa, 0xbb, 0xcf, 0xa7, 0x82, 0x9a, 0x9e, 0x7c,
99*e0c4386eSCy Schubert     0x4a, 0x05, 0x42, 0xed, 0x55, 0xd8, 0x08, 0x37,
100*e0c4386eSCy Schubert     0x06, 0x49, 0x9b, 0xda, 0xb3, 0xb9, 0xc9, 0xc0,
101*e0c4386eSCy Schubert     0x56, 0x26, 0xda, 0x60, 0x1d, 0xbc, 0x06, 0x0b,
102*e0c4386eSCy Schubert     0xb0, 0x94, 0x4b, 0x4e, 0x95, 0xf9, 0xb4, 0x2f,
103*e0c4386eSCy Schubert     0x4e, 0xad, 0xf8, 0xab, 0x2d, 0x19, 0xa2, 0xe6,
104*e0c4386eSCy Schubert     0x6d, 0x11, 0xfd, 0x9b, 0x5a, 0x2a, 0xb0, 0x81,
105*e0c4386eSCy Schubert     0x42, 0x4d, 0x86, 0x76, 0xd5, 0x9e, 0xaf, 0xf9,
106*e0c4386eSCy Schubert     0x6f, 0x79, 0xab, 0x1d, 0xfe, 0xd8, 0xc8, 0xba,
107*e0c4386eSCy Schubert     0xb6, 0xce, 0x03, 0x61, 0x48, 0x53, 0xd8, 0x0b,
108*e0c4386eSCy Schubert     0x83, 0xf0, 0xb0, 0x46, 0xa0, 0xea, 0x46, 0x60,
109*e0c4386eSCy Schubert     0x7a, 0x39, 0x4e, 0x46, 0x6a, 0xbb, 0x07, 0x6c,
110*e0c4386eSCy Schubert     0x8c, 0x7d, 0xb7, 0x7d, 0x5b, 0xe5, 0x24, 0xa5,
111*e0c4386eSCy Schubert     0xab, 0x41, 0x8a, 0xc4, 0x63, 0xf9, 0xce, 0x20,
112*e0c4386eSCy Schubert     0x6f, 0x58, 0x4f, 0x0e, 0x42, 0x82, 0x9e, 0x17,
113*e0c4386eSCy Schubert     0x53, 0xa6, 0xd6, 0x42, 0x3e, 0x80, 0x66, 0x6f,
114*e0c4386eSCy Schubert     0x2a, 0x1c, 0x30, 0x08, 0x01, 0x99, 0x5a, 0x4f,
115*e0c4386eSCy Schubert     0x72, 0x16, 0xed, 0xb0, 0xd6, 0x8c, 0xf0, 0x7a,
116*e0c4386eSCy Schubert     0x33, 0x15, 0xc4, 0x95, 0x65, 0xba, 0x11, 0x37,
117*e0c4386eSCy Schubert     0xa0, 0xcc, 0xe7, 0x45, 0x65, 0x4f, 0x17, 0x0a,
118*e0c4386eSCy Schubert     0x2c, 0x62, 0xc0, 0x65, 0x3b, 0x65, 0x2a, 0x56,
119*e0c4386eSCy Schubert     0xf7, 0x29, 0x8a, 0x9b, 0x1b, 0xbb, 0x0c, 0x40,
120*e0c4386eSCy Schubert     0xcd, 0x66, 0x4b, 0x4f, 0x2f, 0xba, 0xdb, 0x59,
121*e0c4386eSCy Schubert     0x93, 0x6d, 0x34, 0xf3, 0x8d, 0xde, 0x68, 0x99,
122*e0c4386eSCy Schubert     0x78, 0xfc, 0xac, 0x95, 0xd9, 0xa3, 0x74, 0xe6,
123*e0c4386eSCy Schubert     0x24, 0x96, 0x98, 0x6f, 0x64, 0x71, 0x76
124*e0c4386eSCy Schubert };
125*e0c4386eSCy Schubert /* 1 is not a valid key */
126*e0c4386eSCy Schubert static const unsigned char dhkey_3[] = {
127*e0c4386eSCy Schubert     0x01
128*e0c4386eSCy Schubert };
129*e0c4386eSCy Schubert # endif
130*e0c4386eSCy Schubert 
131*e0c4386eSCy Schubert # ifndef OPENSSL_NO_DSA
132*e0c4386eSCy Schubert static const unsigned char dsaparam_bin[] = {
133*e0c4386eSCy Schubert 0x30,0x82,0x02,0x28,0x02,0x82,0x01,0x01,0x00,0xf2,0x85,0x01,0xa5,0xb9,0x56,0x65,
134*e0c4386eSCy Schubert 0x19,0xff,0x9a,0x7d,0xf9,0x90,0xd6,0xaa,0x73,0xac,0xf7,0x94,0xfa,0x8a,0x64,0x6d,
135*e0c4386eSCy Schubert 0xa0,0x01,0x42,0xe5,0x45,0xfc,0x53,0x72,0xb0,0x7c,0xe6,0x3b,0xfb,0x09,0x33,0x41,
136*e0c4386eSCy Schubert 0x27,0xbd,0x00,0xb5,0x18,0x87,0x62,0xa8,0x2b,0xfc,0xd0,0x52,0x4a,0x14,0x2d,0xaa,
137*e0c4386eSCy Schubert 0x36,0xc6,0xf3,0xa9,0xe3,0x90,0x1b,0x74,0xdf,0x0a,0x6d,0x33,0xba,0xf4,0x32,0x6d,
138*e0c4386eSCy Schubert 0xba,0x36,0x68,0x1d,0x83,0x36,0x50,0xc6,0x62,0xc0,0x40,0x67,0x0e,0xf6,0x22,0x00,
139*e0c4386eSCy Schubert 0x62,0x1b,0x76,0x72,0x62,0x5f,0xa0,0xdf,0x38,0xb1,0x1d,0x26,0x70,0x9b,0x84,0x64,
140*e0c4386eSCy Schubert 0xbb,0x16,0x15,0xc2,0x66,0xb9,0x97,0xd0,0x07,0xf1,0x4b,0x70,0x02,0x03,0xf1,0xd2,
141*e0c4386eSCy Schubert 0x03,0xdb,0x78,0x8b,0xb4,0xda,0x6f,0x3c,0xe2,0x31,0xa8,0x1c,0x99,0xea,0x9c,0x75,
142*e0c4386eSCy Schubert 0x28,0x96,0x82,0x16,0x77,0xac,0x79,0x32,0x61,0x87,0xec,0xb7,0xb4,0xc3,0xea,0x12,
143*e0c4386eSCy Schubert 0x62,0x1f,0x08,0xb8,0x16,0xab,0xcc,0xef,0x28,0xdf,0x06,0x07,0xbe,0xb0,0xdc,0x78,
144*e0c4386eSCy Schubert 0x83,0x8a,0x70,0x80,0x34,0xe6,0x91,0xe3,0xd3,0x92,0xd9,0xf4,0x56,0x53,0x52,0xb7,
145*e0c4386eSCy Schubert 0x35,0xf6,0x2a,0xec,0x4b,0xcb,0xa2,0x3c,0xc3,0x0c,0x94,0xa7,0x4e,0x1c,0x42,0x9c,
146*e0c4386eSCy Schubert 0x72,0x99,0x60,0x8c,0xfe,0xfb,0x60,0x57,0x75,0xf5,0x23,0x11,0x12,0xba,0x97,0xcd,
147*e0c4386eSCy Schubert 0xad,0x5a,0x0b,0xa6,0x1f,0x6a,0x48,0x2e,0x8d,0xda,0x95,0xc6,0x0e,0x14,0xde,0xf7,
148*e0c4386eSCy Schubert 0x22,0x55,0xa8,0x6b,0x25,0xdf,0xa2,0xab,0x33,0x65,0x56,0xfc,0x78,0x4f,0x62,0xdf,
149*e0c4386eSCy Schubert 0x48,0xdd,0xce,0x8b,0xe1,0x76,0xf4,0xf6,0x7f,0x02,0x1d,0x00,0xac,0xb0,0xb8,0x92,
150*e0c4386eSCy Schubert 0x3b,0x6b,0x61,0xcf,0x36,0x6d,0xf2,0x1e,0x5d,0xe0,0x7b,0xf5,0x73,0x48,0xa3,0x8b,
151*e0c4386eSCy Schubert 0x86,0x9e,0x88,0xce,0x40,0xf8,0x27,0x6d,0x02,0x82,0x01,0x00,0x77,0x6b,0x89,0xd6,
152*e0c4386eSCy Schubert 0x8f,0x3d,0xce,0x52,0x30,0x74,0xb2,0xa1,0x13,0x96,0xd5,0x92,0xf2,0xf1,0x6b,0x10,
153*e0c4386eSCy Schubert 0x31,0x0b,0xf3,0x69,0xaa,0xbf,0x4b,0x6c,0xcb,0x3f,0x6d,0x58,0x76,0x44,0x09,0xf9,
154*e0c4386eSCy Schubert 0x28,0xef,0xa0,0xe4,0x55,0x77,0x57,0xe0,0xfb,0xcc,0x9a,0x6a,0x2c,0x90,0xec,0x72,
155*e0c4386eSCy Schubert 0x24,0x0b,0x43,0xc5,0xbc,0x31,0xed,0x1a,0x46,0x2c,0x76,0x42,0x9e,0xc0,0x82,0xfc,
156*e0c4386eSCy Schubert 0xff,0xf9,0x7e,0xe2,0x1f,0x39,0xf3,0x3b,0xdb,0x27,0x36,0xe7,0xf5,0x3b,0xc2,0x23,
157*e0c4386eSCy Schubert 0xb6,0xd0,0xcf,0x5b,0x85,0x2e,0x1b,0x00,0x5b,0x31,0xaa,0x72,0x8f,0x37,0xee,0x56,
158*e0c4386eSCy Schubert 0x71,0xc4,0xfd,0x3c,0x8d,0xfa,0x5b,0xab,0xb1,0xa9,0x52,0x76,0xa0,0xe4,0xe3,0x78,
159*e0c4386eSCy Schubert 0x83,0x64,0x5d,0xd7,0x6c,0xec,0x9b,0x40,0x65,0xe2,0x0a,0x11,0x19,0x60,0xdd,0xce,
160*e0c4386eSCy Schubert 0x29,0x9f,0xc6,0x1d,0x0a,0xab,0x8e,0x59,0x25,0xc5,0x0b,0x9c,0x02,0x45,0xba,0x99,
161*e0c4386eSCy Schubert 0x74,0x22,0x1d,0xc1,0x57,0xca,0x50,0x8c,0x5e,0xdf,0xd8,0x5d,0x43,0xae,0x06,0x28,
162*e0c4386eSCy Schubert 0x29,0x82,0xf6,0x5a,0xa9,0x51,0xa2,0x04,0x1d,0xbf,0x88,0x15,0x98,0xce,0x8a,0xb4,
163*e0c4386eSCy Schubert 0x3b,0xe5,0x30,0x29,0xce,0x0c,0x9b,0xf8,0xdb,0xbf,0x06,0x9f,0xd0,0x59,0x18,0xd4,
164*e0c4386eSCy Schubert 0x0b,0x94,0xbf,0xe9,0x67,0x6b,0x9e,0xf0,0x72,0xc6,0xbf,0x79,0x8f,0x1e,0xa3,0x95,
165*e0c4386eSCy Schubert 0x24,0xe3,0xcb,0x58,0xb5,0x67,0xd3,0xae,0x79,0xb0,0x28,0x9c,0x9a,0xd0,0xa4,0xe7,
166*e0c4386eSCy Schubert 0x22,0x15,0xc1,0x8b,0x04,0xb9,0x8a,0xa8,0xb7,0x1b,0x62,0x44,0xc6,0xef,0x4b,0x74,
167*e0c4386eSCy Schubert 0xd0,0xfd,0xa9,0xb4,0x4e,0xdd,0x7d,0x38,0x60,0xd1,0x40,0xcd
168*e0c4386eSCy Schubert };
169*e0c4386eSCy Schubert # endif
170*e0c4386eSCy Schubert 
171*e0c4386eSCy Schubert # ifndef OPENSSL_NO_EC
172*e0c4386eSCy Schubert static const unsigned char ecparam_bin[] = {
173*e0c4386eSCy Schubert 0x06,0x08,0x2a,0x86,0x48,0xce,0x3d,0x03,0x01,0x07
174*e0c4386eSCy Schubert };
175*e0c4386eSCy Schubert static const unsigned char eckey_1[] = {
176*e0c4386eSCy Schubert     0x04, 0xc8, 0x65, 0x45, 0x63, 0x73, 0xe5, 0x0a,
177*e0c4386eSCy Schubert     0x61, 0x1d, 0xcf, 0x60, 0x76, 0x2c, 0xe7, 0x36,
178*e0c4386eSCy Schubert     0x0b, 0x76, 0xc2, 0x92, 0xfc, 0xa4, 0x56, 0xee,
179*e0c4386eSCy Schubert     0xc2, 0x62, 0x05, 0x00, 0x80, 0xe4, 0x4f, 0x07,
180*e0c4386eSCy Schubert     0x3b, 0xf4, 0x59, 0xb8, 0xc3, 0xb3, 0x1f, 0x77,
181*e0c4386eSCy Schubert     0x36, 0x16, 0x4c, 0x72, 0x2a, 0xc0, 0x89, 0x89,
182*e0c4386eSCy Schubert     0xd6, 0x16, 0x14, 0xee, 0x2f, 0x5a, 0xde, 0x9e,
183*e0c4386eSCy Schubert     0x83, 0xc5, 0x78, 0xd0, 0x0b, 0x69, 0xb4, 0xb9,
184*e0c4386eSCy Schubert     0xf1
185*e0c4386eSCy Schubert };
186*e0c4386eSCy Schubert /* a modified key */
187*e0c4386eSCy Schubert static const unsigned char eckey_2[] = {
188*e0c4386eSCy Schubert     0x04, 0xc8, 0x65, 0x45, 0x63, 0x73, 0xe5, 0x0a,
189*e0c4386eSCy Schubert     0x61, 0x1d, 0xcf, 0x60, 0x76, 0x2c, 0xe7, 0x36,
190*e0c4386eSCy Schubert     0x0b, 0x77, 0xc2, 0x92, 0xfc, 0xa4, 0x56, 0xee,
191*e0c4386eSCy Schubert     0xc2, 0x62, 0x05, 0x00, 0x80, 0xe4, 0x4f, 0x07,
192*e0c4386eSCy Schubert     0x3b, 0xf4, 0x59, 0xb8, 0xc3, 0xb3, 0x1f, 0x77,
193*e0c4386eSCy Schubert     0x36, 0x16, 0x4c, 0x72, 0x2a, 0xc0, 0x89, 0x89,
194*e0c4386eSCy Schubert     0xd6, 0x16, 0x14, 0xee, 0x2f, 0x5a, 0xde, 0x9e,
195*e0c4386eSCy Schubert     0x83, 0xc5, 0x78, 0xd0, 0x0b, 0x69, 0xb4, 0xb9,
196*e0c4386eSCy Schubert     0xf1
197*e0c4386eSCy Schubert };
198*e0c4386eSCy Schubert /* an added byte */
199*e0c4386eSCy Schubert static const unsigned char eckey_3[] = {
200*e0c4386eSCy Schubert     0x04, 0xc8, 0x65, 0x45, 0x63, 0x73, 0xe5, 0x0a,
201*e0c4386eSCy Schubert     0x61, 0x1d, 0xcf, 0x60, 0x76, 0x2c, 0xe7, 0x36,
202*e0c4386eSCy Schubert     0x0b, 0x76, 0xc2, 0x92, 0xfc, 0xa4, 0x56, 0xee,
203*e0c4386eSCy Schubert     0xc2, 0x62, 0x05, 0x00, 0x80, 0xe4, 0x4f, 0x07,
204*e0c4386eSCy Schubert     0x3b, 0xf4, 0x59, 0xb8, 0xc3, 0xb3, 0x1f, 0x77,
205*e0c4386eSCy Schubert     0x36, 0x16, 0x4c, 0x72, 0x2a, 0xc0, 0x89, 0x89,
206*e0c4386eSCy Schubert     0xd6, 0x16, 0x14, 0xee, 0x2f, 0x5a, 0xde, 0x9e,
207*e0c4386eSCy Schubert     0x83, 0xc5, 0x78, 0xd0, 0x0b, 0x69, 0xb4, 0xb9,
208*e0c4386eSCy Schubert     0xf1, 0xaa
209*e0c4386eSCy Schubert };
210*e0c4386eSCy Schubert # endif
211*e0c4386eSCy Schubert 
212*e0c4386eSCy Schubert #define NUM_KEYS 10
213*e0c4386eSCy Schubert 
214*e0c4386eSCy Schubert static const struct {
215*e0c4386eSCy Schubert     int type;
216*e0c4386eSCy Schubert     const unsigned char *param_bin;
217*e0c4386eSCy Schubert     size_t param_bin_len;
218*e0c4386eSCy Schubert     struct pubkey keys[NUM_KEYS];
219*e0c4386eSCy Schubert } pkey_params [] = {
220*e0c4386eSCy Schubert # ifndef OPENSSL_NO_DH
221*e0c4386eSCy Schubert     { EVP_PKEY_DH, dhparam_bin, sizeof(dhparam_bin),
222*e0c4386eSCy Schubert         { { 0, dhkey_1, sizeof(dhkey_1) },
223*e0c4386eSCy Schubert           { 0, dhkey_2, sizeof(dhkey_2) },
224*e0c4386eSCy Schubert           { 1, dhkey_3, sizeof(dhkey_3) },
225*e0c4386eSCy Schubert           { 1, dhkey_1, 0 },
226*e0c4386eSCy Schubert           { 1, dhparam_bin, sizeof(dhparam_bin) }
227*e0c4386eSCy Schubert         }
228*e0c4386eSCy Schubert     },
229*e0c4386eSCy Schubert # endif
230*e0c4386eSCy Schubert # ifndef OPENSSL_NO_DSA
231*e0c4386eSCy Schubert     { EVP_PKEY_DSA, dsaparam_bin, sizeof(dsaparam_bin) },
232*e0c4386eSCy Schubert # endif
233*e0c4386eSCy Schubert # ifndef OPENSSL_NO_EC
234*e0c4386eSCy Schubert     { EVP_PKEY_EC, ecparam_bin, sizeof(ecparam_bin),
235*e0c4386eSCy Schubert         { { 0, eckey_1, sizeof(eckey_1) },
236*e0c4386eSCy Schubert           { 1, eckey_2, sizeof(eckey_2) },
237*e0c4386eSCy Schubert           { 1, eckey_3, sizeof(eckey_3) },
238*e0c4386eSCy Schubert           { 1, eckey_1, 0 },
239*e0c4386eSCy Schubert           { 1, eckey_1, sizeof(eckey_1) - 1 }
240*e0c4386eSCy Schubert         }
241*e0c4386eSCy Schubert     }
242*e0c4386eSCy Schubert # endif
243*e0c4386eSCy Schubert };
244*e0c4386eSCy Schubert 
params_bio_test(int id)245*e0c4386eSCy Schubert static int params_bio_test(int id)
246*e0c4386eSCy Schubert {
247*e0c4386eSCy Schubert     int ret, out_len;
248*e0c4386eSCy Schubert     BIO *in = NULL, *out = NULL;
249*e0c4386eSCy Schubert     EVP_PKEY *in_key = NULL, *out_key = NULL;
250*e0c4386eSCy Schubert     unsigned char *out_bin;
251*e0c4386eSCy Schubert     int type = pkey_params[id].type;
252*e0c4386eSCy Schubert 
253*e0c4386eSCy Schubert     ret = TEST_ptr(in = BIO_new_mem_buf(pkey_params[id].param_bin,
254*e0c4386eSCy Schubert                                         (int)pkey_params[id].param_bin_len))
255*e0c4386eSCy Schubert         /* Load in pkey params from binary */
256*e0c4386eSCy Schubert         && TEST_ptr(d2i_KeyParams_bio(type, &in_key, in))
257*e0c4386eSCy Schubert         && TEST_ptr(out = BIO_new(BIO_s_mem()))
258*e0c4386eSCy Schubert         /* Save pkey params to binary */
259*e0c4386eSCy Schubert         && TEST_int_gt(i2d_KeyParams_bio(out, in_key), 0)
260*e0c4386eSCy Schubert         /* test the output binary is the expected value */
261*e0c4386eSCy Schubert         && TEST_int_gt(out_len = BIO_get_mem_data(out, &out_bin), 0)
262*e0c4386eSCy Schubert         && TEST_mem_eq(pkey_params[id].param_bin,
263*e0c4386eSCy Schubert                        (int)pkey_params[id].param_bin_len,
264*e0c4386eSCy Schubert                        out_bin, out_len);
265*e0c4386eSCy Schubert 
266*e0c4386eSCy Schubert     BIO_free(in);
267*e0c4386eSCy Schubert     BIO_free(out);
268*e0c4386eSCy Schubert     EVP_PKEY_free(in_key);
269*e0c4386eSCy Schubert     EVP_PKEY_free(out_key);
270*e0c4386eSCy Schubert     return ret;
271*e0c4386eSCy Schubert }
272*e0c4386eSCy Schubert 
set_enc_pubkey_test(int id)273*e0c4386eSCy Schubert static int set_enc_pubkey_test(int id)
274*e0c4386eSCy Schubert {
275*e0c4386eSCy Schubert     int ret, i;
276*e0c4386eSCy Schubert     BIO *in = NULL;
277*e0c4386eSCy Schubert     EVP_PKEY *in_key = NULL;
278*e0c4386eSCy Schubert     int type = pkey_params[id].type;
279*e0c4386eSCy Schubert     const struct pubkey *keys = pkey_params[id].keys;
280*e0c4386eSCy Schubert 
281*e0c4386eSCy Schubert     if (keys[0].key_bin == NULL)
282*e0c4386eSCy Schubert         return TEST_skip("Not applicable test");
283*e0c4386eSCy Schubert 
284*e0c4386eSCy Schubert     ret = TEST_ptr(in = BIO_new_mem_buf(pkey_params[id].param_bin,
285*e0c4386eSCy Schubert                                         (int)pkey_params[id].param_bin_len))
286*e0c4386eSCy Schubert         /* Load in pkey params from binary */
287*e0c4386eSCy Schubert         && TEST_ptr(d2i_KeyParams_bio(type, &in_key, in));
288*e0c4386eSCy Schubert 
289*e0c4386eSCy Schubert     for (i = 0; ret && i < NUM_KEYS && keys[i].key_bin != NULL; i++) {
290*e0c4386eSCy Schubert         if (keys[i].bad) {
291*e0c4386eSCy Schubert             ERR_set_mark();
292*e0c4386eSCy Schubert             ret = ret
293*e0c4386eSCy Schubert                 && TEST_int_le(EVP_PKEY_set1_encoded_public_key(in_key,
294*e0c4386eSCy Schubert                                                                 keys[i].key_bin,
295*e0c4386eSCy Schubert                                                                 keys[i].key_bin_len),
296*e0c4386eSCy Schubert                                0);
297*e0c4386eSCy Schubert             ERR_pop_to_mark();
298*e0c4386eSCy Schubert         } else {
299*e0c4386eSCy Schubert             ret = ret
300*e0c4386eSCy Schubert                 && TEST_int_gt(EVP_PKEY_set1_encoded_public_key(in_key,
301*e0c4386eSCy Schubert                                                                 keys[i].key_bin,
302*e0c4386eSCy Schubert                                                                 keys[i].key_bin_len),
303*e0c4386eSCy Schubert                                0);
304*e0c4386eSCy Schubert         }
305*e0c4386eSCy Schubert         if (!ret)
306*e0c4386eSCy Schubert             TEST_info("Test key index #%d", i);
307*e0c4386eSCy Schubert     }
308*e0c4386eSCy Schubert 
309*e0c4386eSCy Schubert     BIO_free(in);
310*e0c4386eSCy Schubert     EVP_PKEY_free(in_key);
311*e0c4386eSCy Schubert     return ret;
312*e0c4386eSCy Schubert }
313*e0c4386eSCy Schubert #endif
314*e0c4386eSCy Schubert 
setup_tests(void)315*e0c4386eSCy Schubert int setup_tests(void)
316*e0c4386eSCy Schubert {
317*e0c4386eSCy Schubert #ifdef OPENSSL_NO_KEYPARAMS
318*e0c4386eSCy Schubert     TEST_note("No DH/DSA/EC support");
319*e0c4386eSCy Schubert #else
320*e0c4386eSCy Schubert     ADD_ALL_TESTS(params_bio_test, OSSL_NELEM(pkey_params));
321*e0c4386eSCy Schubert     ADD_ALL_TESTS(set_enc_pubkey_test, OSSL_NELEM(pkey_params));
322*e0c4386eSCy Schubert #endif
323*e0c4386eSCy Schubert     return 1;
324*e0c4386eSCy Schubert }
325