]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/recipes/15-test_mp_rsa.t
Deprecate the low level RSA functions.
[thirdparty/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 my @test_param = (
21 # 3 primes, 2048-bit
22 {
23 primes => '3',
24 bits => '2048',
25 },
26 # 4 primes, 4096-bit
27 {
28 primes => '4',
29 bits => '4096',
30 },
31 # 5 primes, 8192-bit
32 {
33 primes => '5',
34 bits => '8192',
35 },
36 );
37
38 plan tests => 1 + scalar(@test_param) * 5 * (disabled('deprecated-3.0') ? 1 : 2);
39
40 ok(run(test(["rsa_mp_test"])), "running rsa multi prime test");
41
42 my $cleartext = data_file("plain_text");
43
44 # genrsa
45 run_mp_tests(0) if !disabled('deprecated-3.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 ok(run(app([ 'openssl', 'pkey', '-check',
64 '-in', "rsamptest-$name.pem", '-noout'])),
65 "rsa -check $name");
66 ok(run(app([ 'openssl', 'pkeyutl', '-inkey', "rsamptest-$name.pem",
67 '-encrypt', '-in', $cleartext,
68 '-out', "rsamptest-$name.enc" ])),
69 "rsa $name encrypt");
70 ok(run(app([ 'openssl', 'pkeyutl', '-inkey', "rsamptest-$name.pem",
71 '-decrypt', '-in', "rsamptest-$name.enc",
72 '-out', "rsamptest-$name.dec" ])),
73 "rsa $name decrypt");
74 } else {
75 ok(run(app([ 'openssl', 'genrsa', '-out', "rsamptest-$name.pem",
76 '-primes', $primes, $bits])), "genrsa $name");
77 ok(run(app([ 'openssl', 'rsa', '-check',
78 '-in', "rsamptest-$name.pem", '-noout'])),
79 "rsa -check $name");
80 ok(run(app([ 'openssl', 'rsautl', '-inkey', "rsamptest-$name.pem",
81 '-encrypt', '-in', $cleartext,
82 '-out', "rsamptest-$name.enc" ])),
83 "rsa $name encrypt");
84 ok(run(app([ 'openssl', 'rsautl', '-inkey', "rsamptest-$name.pem",
85 '-decrypt', '-in', "rsamptest-$name.enc",
86 '-out', "rsamptest-$name.dec" ])),
87 "rsa $name decrypt");
88 }
89 ok(check_msg("rsamptest-$name.dec"), "rsa $name check result");
90 }
91 }
92
93 sub check_msg {
94 my $decrypted = shift;
95 my $msg;
96 my $dec;
97
98 open(my $fh, "<", $cleartext) or return 0;
99 binmode $fh;
100 read($fh, $msg, 10240);
101 close $fh;
102 open($fh, "<", $decrypted ) or return 0;
103 binmode $fh;
104 read($fh, $dec, 10240);
105 close $fh;
106
107 if ($msg ne $dec) {
108 print STDERR "cleartext and decrypted are not the same";
109 return 0;
110 }
111 return 1;
112 }