d4547e5849695ab6f34b4ed222be00a08d9326f7
[openssl.git] / test / recipes / 15-test_genec.t
1 #! /usr/bin/env perl
2 # Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the Apache License 2.0 (the "License").  You may not use
5 # this file except in compliance with the License.  You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
8
9
10 use strict;
11 use warnings;
12
13 use File::Spec;
14 use OpenSSL::Test qw/:DEFAULT srctop_file/;
15 use OpenSSL::Test::Utils;
16
17 # 'supported' and 'unsupported' reflect the current state of things.  In
18 # Test::More terms, 'supported' works exactly like ok(run(whatever)), while
19 # 'unsupported' wraps that in a TODO: { } block.
20 #
21 # The first argument is the test name (this becomes the last argument to
22 # 'ok')
23 # The remaining argument are passed unchecked to 'run'.
24
25 # 1:    the result of app() or similar, i.e. something you can pass to
26 sub supported {
27     my $str = shift;
28
29     ok(run(@_), $str);
30 }
31
32 sub unsupported {
33     my $str = shift;
34  TODO: {
35         local $TODO = "Currently not supported";
36
37         ok(run(@_), $str);
38     }
39 }
40
41
42 setup("test_genec");
43
44 plan skip_all => "This test is unsupported in a no-ec build"
45     if disabled("ec");
46
47 my @prime_curves = qw(
48     secp112r1
49     secp112r2
50     secp128r1
51     secp128r2
52     secp160k1
53     secp160r1
54     secp160r2
55     secp192k1
56     secp224k1
57     secp224r1
58     secp256k1
59     secp384r1
60     secp521r1
61     prime192v1
62     prime192v2
63     prime192v3
64     prime239v1
65     prime239v2
66     prime239v3
67     prime256v1
68     wap-wsg-idm-ecid-wtls6
69     wap-wsg-idm-ecid-wtls7
70     wap-wsg-idm-ecid-wtls8
71     wap-wsg-idm-ecid-wtls9
72     wap-wsg-idm-ecid-wtls12
73     brainpoolP160r1
74     brainpoolP160t1
75     brainpoolP192r1
76     brainpoolP192t1
77     brainpoolP224r1
78     brainpoolP224t1
79     brainpoolP256r1
80     brainpoolP256t1
81     brainpoolP320r1
82     brainpoolP320t1
83     brainpoolP384r1
84     brainpoolP384t1
85     brainpoolP512r1
86     brainpoolP512t1
87 );
88
89 my @binary_curves = qw(
90     sect113r1
91     sect113r2
92     sect131r1
93     sect131r2
94     sect163k1
95     sect163r1
96     sect163r2
97     sect193r1
98     sect193r2
99     sect233k1
100     sect233r1
101     sect239k1
102     sect283k1
103     sect283r1
104     sect409k1
105     sect409r1
106     sect571k1
107     sect571r1
108     c2pnb163v1
109     c2pnb163v2
110     c2pnb163v3
111     c2pnb176v1
112     c2tnb191v1
113     c2tnb191v2
114     c2tnb191v3
115     c2pnb208w1
116     c2tnb239v1
117     c2tnb239v2
118     c2tnb239v3
119     c2pnb272w1
120     c2pnb304w1
121     c2tnb359v1
122     c2pnb368w1
123     c2tnb431r1
124     wap-wsg-idm-ecid-wtls1
125     wap-wsg-idm-ecid-wtls3
126     wap-wsg-idm-ecid-wtls4
127     wap-wsg-idm-ecid-wtls5
128     wap-wsg-idm-ecid-wtls10
129     wap-wsg-idm-ecid-wtls11
130     Oakley-EC2N-3
131     Oakley-EC2N-4
132 );
133
134 my @other_curves = ();
135 push(@other_curves, 'SM2')
136     if !disabled("sm2");
137
138 my @curve_aliases = qw(
139     P-192
140     P-224
141     P-256
142     P-384
143     P-521
144 );
145 push(@curve_aliases, qw(
146     B-163
147     B-233
148     B-283
149     B-409
150     B-571
151     K-163
152     K-233
153     K-283
154     K-409
155     K-571
156 )) if !disabled("ec2m");
157
158 my @curve_list = ();
159 push(@curve_list, @prime_curves);
160 push(@curve_list, @binary_curves)
161     if !disabled("ec2m");
162 push(@curve_list, @other_curves);
163 push(@curve_list, @curve_aliases);
164
165 my %params_encodings =
166     (
167      'named_curve'      => \&supported,
168      'explicit'         => \&unsupported
169     );
170
171 my @output_formats = ('PEM', 'DER');
172
173 plan tests => scalar(@curve_list) * scalar(keys %params_encodings)
174     * (1 + scalar(@output_formats)) # Try listed @output_formats and text output
175     + 1                             # Checking that with no curve it fails
176     + 1                             # Checking that with unknown curve it fails
177     ;
178
179 foreach my $curvename (@curve_list) {
180     foreach my $paramenc (sort keys %params_encodings) {
181         my $fn = $params_encodings{$paramenc};
182         $fn->("genpkey EC params ${curvename} with ec_param_enc:'${paramenc}' (text)",
183               app([ 'openssl', 'genpkey',
184                     '-algorithm', 'EC',
185                     '-pkeyopt', 'ec_paramgen_curve:'.$curvename,
186                     '-pkeyopt', 'ec_param_enc:'.$paramenc,
187                     '-text']));
188
189         foreach my $outform (@output_formats) {
190             my $outfile = "ecgen.${curvename}.${paramenc}." . lc $outform;
191             $fn->("genpkey EC params ${curvename} with ec_param_enc:'${paramenc}' (${outform})",
192                   app([ 'openssl', 'genpkey', '-genparam',
193                         '-algorithm', 'EC',
194                         '-pkeyopt', 'ec_paramgen_curve:'.$curvename,
195                         '-pkeyopt', 'ec_param_enc:'.$paramenc,
196                         '-outform', $outform,
197                         '-out', $outfile]));
198         }
199     }
200 }
201
202 ok(!run(app([ 'openssl', 'genpkey',
203               '-algorithm', 'EC'])),
204    "genpkey EC with no params should fail");
205
206 ok(!run(app([ 'openssl', 'genpkey',
207               '-algorithm', 'EC',
208               '-pkeyopt', 'ec_paramgen_curve:bogus_foobar_curve'])),
209    "genpkey EC with unknown curve name should fail");