Update RSA keygen to use sp800-56b by default
[openssl.git] / test / recipes / 15-test_genrsa.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 setup("test_genrsa");
18
19 plan tests => 12;
20
21 # We want to know that an absurdly small number of bits isn't support
22 if (disabled("deprecated-3.0")) {
23     is(run(app([ 'openssl', 'genpkey', '-out', 'genrsatest.pem',
24                  '-algorithm', 'RSA', '-pkeyopt', 'rsa_keygen_bits:8',
25                  '-pkeyopt', 'rsa_keygen_pubexp:3'])),
26                0, "genrsa -3 8");
27 } else {
28     is(run(app([ 'openssl', 'genrsa', '-3', '-out', 'genrsatest.pem', '8'])),
29                0, "genrsa -3 8");
30 }
31
32 # Depending on the shared library, we might have different lower limits.
33 # Let's find it!  This is a simple binary search
34 # ------------------------------------------------------------
35 # NOTE: $good may need an update in the future
36 # ------------------------------------------------------------
37 note "Looking for lowest amount of bits";
38 my $bad = 3;                    # Log2 of number of bits (2 << 3  == 8)
39 my $good = 11;                  # Log2 of number of bits (2 << 11 == 2048)
40 my $fin;
41 while ($good > $bad + 1) {
42     my $checked = int(($good + $bad + 1) / 2);
43     my $bits = 2 ** $checked;
44     if (disabled("deprecated-3.0")) {
45         $fin = run(app([ 'openssl', 'genpkey', '-out', 'genrsatest.pem',
46                          '-algorithm', 'RSA', '-pkeyopt', 'rsa_keygen_pubexp:65537',
47                          '-pkeyopt', "rsa_keygen_bits:$bits",
48                        ], stderr => undef));
49     } else {
50         $fin = run(app([ 'openssl', 'genrsa', '-3', '-out', 'genrsatest.pem',
51                          $bits
52                        ], stderr => undef));
53     }
54     if ($fin) {
55         note 2 ** $checked, " bits is good";
56         $good = $checked;
57     } else {
58         note 2 ** $checked, " bits is bad";
59         $bad = $checked;
60     }
61 }
62 $good++ if $good == $bad;
63 $good = 2 ** $good;
64 note "Found lowest allowed amount of bits to be $good";
65
66 ok(run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
67              '-pkeyopt', 'rsa_keygen_pubexp:65537',
68              '-pkeyopt', "rsa_keygen_bits:$good",
69              '-out', 'genrsatest.pem' ])),
70    "genpkey -3 $good");
71 ok(run(app([ 'openssl', 'pkey', '-check', '-in', 'genrsatest.pem', '-noout' ])),
72    "pkey -check");
73 ok(run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
74              '-pkeyopt', 'rsa_keygen_pubexp:65537',
75              '-pkeyopt', "rsa_keygen_bits:$good",
76              '-out', 'genrsatest.pem' ])),
77    "genpkey -f4 $good");
78
79 ok(run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
80              '-pkeyopt', 'rsa_keygen_bits:2048',
81              '-out', 'genrsatest2048.pem' ])),
82    "genpkey 2048 bits");
83 ok(run(app([ 'openssl', 'pkey', '-check', '-in', 'genrsatest2048.pem', '-noout' ])),
84    "pkey -check");
85
86 ok(!run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
87              '-pkeyopt', 'hexe:02',
88              '-out', 'genrsatest.pem' ])),
89    "genpkey with a bad public exponent should fail");
90 ok(!run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
91              '-pkeyopt', 'e:65538',
92              '-out', 'genrsatest.pem' ])),
93    "genpkey with a even public exponent should fail");
94
95
96  SKIP: {
97     skip "Skipping rsa command line test", 4 if disabled("deprecated-3.0");
98
99     ok(run(app([ 'openssl', 'genrsa', '-3', '-out', 'genrsatest.pem', $good ])),
100        "genrsa -3 $good");
101     ok(run(app([ 'openssl', 'rsa', '-check', '-in', 'genrsatest.pem', '-noout' ])),
102        "rsa -check");
103     ok(run(app([ 'openssl', 'genrsa', '-f4', '-out', 'genrsatest.pem', $good ])),
104        "genrsa -f4 $good");
105     ok(run(app([ 'openssl', 'rsa', '-check', '-in', 'genrsatest.pem', '-noout' ])),
106        "rsa -check");
107 }