xref: /openssl/test/recipes/15-test_mp_rsa.t (revision da1c088f)
1#! /usr/bin/env perl
2# Copyright 2017-2023 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
11use strict;
12use warnings;
13
14use File::Spec;
15use OpenSSL::Test qw/:DEFAULT data_file/;
16use OpenSSL::Test::Utils;
17
18setup("test_mp_rsa");
19
20my @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
38plan tests => 2 + scalar(@test_param) * 5 * 2;
39
40ok(run(test(["rsa_mp_test"])), "running rsa multi prime test");
41
42ok(run(app(['openssl', 'pkey', '-noout', '-check', '-in',
43            data_file('rsamplcm.pem')])), "checking lcm in key check");
44
45my $cleartext = data_file("plain_text");
46
47# genrsa
48run_mp_tests(0);
49# evp
50run_mp_tests(1);
51
52sub run_mp_tests {
53    my $evp = shift;
54
55    foreach my $param (@test_param) {
56        my $primes = $param->{primes};
57        my $bits = $param->{bits};
58        my $name = ($evp ? "evp" : "") . "${bits}p${primes}";
59
60        if ($evp) {
61            ok(run(app([ 'openssl', 'genpkey', '-out', "rsamptest-$name.pem",
62                         '-algorithm', 'RSA',
63                         '-pkeyopt', "rsa_keygen_primes:$primes",
64                         '-pkeyopt', "rsa_keygen_bits:$bits"])),
65               "genrsa $name");
66            ok(run(app([ 'openssl', 'pkey', '-check',
67                         '-in', "rsamptest-$name.pem", '-noout'])),
68               "rsa -check $name");
69            ok(run(app([ 'openssl', 'pkeyutl', '-inkey', "rsamptest-$name.pem",
70                         '-encrypt', '-in', $cleartext,
71                         '-out', "rsamptest-$name.enc" ])),
72               "rsa $name encrypt");
73            ok(run(app([ 'openssl', 'pkeyutl', '-inkey', "rsamptest-$name.pem",
74                         '-decrypt', '-in', "rsamptest-$name.enc",
75                         '-out', "rsamptest-$name.dec" ])),
76               "rsa $name decrypt");
77        } else {
78            ok(run(app([ 'openssl', 'genrsa', '-out', "rsamptest-$name.pem",
79                         '-primes', $primes, $bits])), "genrsa $name");
80            ok(run(app([ 'openssl', 'rsa', '-check',
81                         '-in', "rsamptest-$name.pem", '-noout'])),
82               "rsa -check $name");
83            if (!disabled('deprecated-3.0')) {
84                ok(run(app([ 'openssl', 'rsautl', '-inkey', "rsamptest-$name.pem",
85                             '-encrypt', '-in', $cleartext,
86                             '-out', "rsamptest-$name.enc" ])),
87                   "rsa $name encrypt");
88                ok(run(app([ 'openssl', 'rsautl', '-inkey', "rsamptest-$name.pem",
89                             '-decrypt', '-in', "rsamptest-$name.enc",
90                             '-out', "rsamptest-$name.dec" ])),
91                   "rsa $name decrypt");
92            } else {
93                ok(run(app([ 'openssl', 'pkeyutl', '-inkey', "rsamptest-$name.pem",
94                             '-encrypt', '-in', $cleartext,
95                             '-out', "rsamptest-$name.enc" ])),
96                   "rsa $name encrypt");
97                ok(run(app([ 'openssl', 'pkeyutl', '-inkey', "rsamptest-$name.pem",
98                             '-decrypt', '-in', "rsamptest-$name.enc",
99                             '-out', "rsamptest-$name.dec" ])),
100                   "rsa $name decrypt");
101            }
102        }
103        ok(check_msg("rsamptest-$name.dec"), "rsa $name check result");
104    }
105}
106
107sub check_msg {
108    my $decrypted = shift;
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, "<", $decrypted ) 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}
127