rsa/rsa_lib.c: make RSA_security_bits multi-prime aware.
[openssl.git] / test / recipes / 15-test_mp_rsa.t
1 #! /usr/bin/env perl
2 # Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3 # Copyright 2017 BaishanCloud. All rights reserved.
4 #
5 # Licensed under the OpenSSL license (the "License").  You may not use
6 # this file except in compliance with the License.  You can obtain a copy
7 # in the file LICENSE in the source distribution or at
8 # https://www.openssl.org/source/license.html
9
10
11 use strict;
12 use warnings;
13
14 use File::Spec;
15 use OpenSSL::Test qw/:DEFAULT data_file/;
16 use OpenSSL::Test::Utils;
17
18 setup("test_mp_rsa");
19
20 plan tests => 61;
21
22 ok(run(test(["rsa_mp_test"])), "running rsa multi prime test");
23
24 my $cleartext = data_file("plain_text");
25
26 my @test_param = (
27     # 3 primes, 2048-bit
28     {
29         primes => '3',
30         bits => '2048',
31     },
32     # 4 primes, 2048-bit
33     {
34         primes => '4',
35         bits => '2048',
36     },
37     # 8 primes, 2048-bit
38     {
39         primes => '8',
40         bits => '2048',
41     },
42     # 15 primes, 2048-bit
43     {
44         primes => '15',
45         bits => '2048',
46     },
47     # 8 primes, 15360-bit (3 & 4 primes for 15360 bit is too long to gen a key)
48     {
49         primes => '8',
50         bits => '15360',
51     },
52     # 15 primes, 15360-bit
53     {
54         primes => '15',
55         bits => '15360',
56     },
57 );
58
59 # genrsa
60 run_mp_tests(0);
61 # evp
62 run_mp_tests(1);
63
64 sub run_mp_tests {
65     my $evp = shift;
66
67     foreach my $param (@test_param) {
68         my $primes = $param->{primes};
69         my $bits = $param->{bits};
70         my $name = ($evp ? "evp" : "") . "${bits}p${primes}";
71
72         if ($evp) {
73             ok(run(app([ 'openssl', 'genpkey', '-out', 'rsamptest.pem',
74                          '-algorithm', 'RSA', '-pkeyopt', "rsa_keygen_primes:$primes",
75                          '-pkeyopt', "rsa_keygen_bits:$bits"])), "genrsa $name");
76         } else {
77             ok(run(app([ 'openssl', 'genrsa', '-out', 'rsamptest.pem',
78                          '-primes', $primes, $bits])), "genrsa $name");
79         }
80
81         ok(run(app([ 'openssl', 'rsa', '-check', '-in', 'rsamptest.pem',
82                      '-noout'])), "rsa -check $name");
83         if ($evp) {
84             ok(run(app([ 'openssl', 'pkeyutl', '-inkey', 'rsamptest.pem',
85                          '-encrypt', '-in', $cleartext,
86                          '-out', 'rsamptest.enc' ])), "rsa $name encrypt");
87             ok(run(app([ 'openssl', 'pkeyutl', '-inkey', 'rsamptest.pem',
88                          '-decrypt', '-in', 'rsamptest.enc',
89                          '-out', 'rsamptest.dec' ])), "rsa $name decrypt");
90         } else {
91             ok(run(app([ 'openssl', 'rsautl', '-inkey', 'rsamptest.pem',
92                          '-encrypt', '-in', $cleartext,
93                          '-out', 'rsamptest.enc' ])), "rsa $name encrypt");
94             ok(run(app([ 'openssl', 'rsautl', '-inkey', 'rsamptest.pem',
95                          '-decrypt', '-in', 'rsamptest.enc',
96                          '-out', 'rsamptest.dec' ])), "rsa $name decrypt");
97         }
98
99         ok(check_msg(), "rsa $name check result");
100
101         # clean up temp files
102         unlink 'rsamptest.pem';
103         unlink 'rsamptest.enc';
104         unlink 'rsamptest.dec';
105     }
106 }
107
108 sub check_msg {
109     my $msg;
110     my $dec;
111
112     open(my $fh, "<", $cleartext) or return 0;
113     binmode $fh;
114     read($fh, $msg, 10240);
115     close $fh;
116     open($fh, "<", "rsamptest.dec") or return 0;
117     binmode $fh;
118     read($fh, $dec, 10240);
119     close $fh;
120
121     if ($msg ne $dec) {
122         print STDERR "cleartext and decrypted are not the same";
123         return 0;
124     }
125     return 1;
126 }