4a4ac3569d25b7e37fc842232797ca57d9566165
[oweals/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 Apache License 2.0 (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 => 31;
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, 4096-bit
33     {
34         primes => '4',
35         bits => '4096',
36     },
37     # 5 primes, 8192-bit
38     {
39         primes => '5',
40         bits => '8192',
41     },
42 );
43
44 # genrsa
45 run_mp_tests(0);
46 # evp
47 run_mp_tests(1);
48
49 sub run_mp_tests {
50     my $evp = shift;
51
52     foreach my $param (@test_param) {
53         my $primes = $param->{primes};
54         my $bits = $param->{bits};
55         my $name = ($evp ? "evp" : "") . "${bits}p${primes}";
56
57         if ($evp) {
58             ok(run(app([ 'openssl', 'genpkey', '-out', "rsamptest-$name.pem",
59                          '-algorithm', 'RSA',
60                          '-pkeyopt', "rsa_keygen_primes:$primes",
61                          '-pkeyopt', "rsa_keygen_bits:$bits"])),
62                "genrsa $name");
63         } else {
64             ok(run(app([ 'openssl', 'genrsa', '-out', "rsamptest-$name.pem",
65                          '-primes', $primes, $bits])),
66                "genrsa $name");
67         }
68
69         ok(run(app([ 'openssl', 'rsa', '-check', '-in', "rsamptest-$name.pem",
70                      '-noout'])),
71            "rsa -check $name");
72
73         if ($evp) {
74             ok(run(app([ 'openssl', 'pkeyutl', '-inkey', "rsamptest-$name.pem",
75                          '-encrypt', '-in', $cleartext,
76                          '-out', "rsamptest-$name.enc" ])),
77                "rsa $name encrypt");
78             ok(run(app([ 'openssl', 'pkeyutl', '-inkey', "rsamptest-$name.pem",
79                          '-decrypt', '-in', "rsamptest-$name.enc",
80                          '-out', "rsamptest-$name.dec" ])),
81                "rsa $name decrypt");
82         } else {
83             ok(run(app([ 'openssl', 'rsautl', '-inkey', "rsamptest-$name.pem",
84                          '-encrypt', '-in', $cleartext,
85                          '-out', "rsamptest-$name.enc" ])),
86                "rsa $name encrypt");
87             ok(run(app([ 'openssl', 'rsautl', '-inkey', "rsamptest-$name.pem",
88                          '-decrypt', '-in', "rsamptest-$name.enc",
89                          '-out', "rsamptest-$name.dec" ])),
90                "rsa $name decrypt");
91         }
92
93         ok(check_msg("rsamptest-$name.dec"), "rsa $name check result");
94     }
95 }
96
97 sub check_msg {
98     my $decrypted = shift;
99     my $msg;
100     my $dec;
101
102     open(my $fh, "<", $cleartext) or return 0;
103     binmode $fh;
104     read($fh, $msg, 10240);
105     close $fh;
106     open($fh, "<", $decrypted ) or return 0;
107     binmode $fh;
108     read($fh, $dec, 10240);
109     close $fh;
110
111     if ($msg ne $dec) {
112         print STDERR "cleartext and decrypted are not the same";
113         return 0;
114     }
115     return 1;
116 }