2017-08-02 02:19:43 +08:00
|
|
|
#! /usr/bin/env perl
|
|
|
|
# Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
|
|
|
|
# Copyright 2017 BaishanCloud. All rights reserved.
|
|
|
|
#
|
2018-12-06 20:05:25 +08:00
|
|
|
# Licensed under the Apache License 2.0 (the "License"). You may not use
|
2017-08-02 02:19:43 +08:00
|
|
|
# this file except in compliance with the License. You can obtain a copy
|
|
|
|
# in the file LICENSE in the source distribution or at
|
|
|
|
# https://www.openssl.org/source/license.html
|
|
|
|
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use File::Spec;
|
|
|
|
use OpenSSL::Test qw/:DEFAULT data_file/;
|
|
|
|
use OpenSSL::Test::Utils;
|
|
|
|
|
|
|
|
setup("test_mp_rsa");
|
|
|
|
|
|
|
|
my @test_param = (
|
|
|
|
# 3 primes, 2048-bit
|
|
|
|
{
|
|
|
|
primes => '3',
|
|
|
|
bits => '2048',
|
|
|
|
},
|
2017-11-25 05:45:45 +08:00
|
|
|
# 4 primes, 4096-bit
|
2017-08-02 02:19:43 +08:00
|
|
|
{
|
|
|
|
primes => '4',
|
2017-11-25 05:45:45 +08:00
|
|
|
bits => '4096',
|
2017-08-02 02:19:43 +08:00
|
|
|
},
|
2017-11-25 05:45:45 +08:00
|
|
|
# 5 primes, 8192-bit
|
2017-08-02 02:19:43 +08:00
|
|
|
{
|
2017-11-25 05:45:45 +08:00
|
|
|
primes => '5',
|
|
|
|
bits => '8192',
|
2017-08-02 02:19:43 +08:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-02-12 13:03:51 +08:00
|
|
|
plan tests => 1 + scalar(@test_param) * 5 * (disabled('deprecated-3.0') ? 1 : 2);
|
|
|
|
|
|
|
|
ok(run(test(["rsa_mp_test"])), "running rsa multi prime test");
|
|
|
|
|
|
|
|
my $cleartext = data_file("plain_text");
|
|
|
|
|
2017-08-02 02:19:43 +08:00
|
|
|
# genrsa
|
2020-02-12 13:03:51 +08:00
|
|
|
run_mp_tests(0) if !disabled('deprecated-3.0');
|
2017-08-02 02:19:43 +08:00
|
|
|
# evp
|
|
|
|
run_mp_tests(1);
|
|
|
|
|
|
|
|
sub run_mp_tests {
|
|
|
|
my $evp = shift;
|
|
|
|
|
|
|
|
foreach my $param (@test_param) {
|
|
|
|
my $primes = $param->{primes};
|
|
|
|
my $bits = $param->{bits};
|
|
|
|
my $name = ($evp ? "evp" : "") . "${bits}p${primes}";
|
|
|
|
|
|
|
|
if ($evp) {
|
2020-02-13 03:29:04 +08:00
|
|
|
ok(run(app([ 'openssl', 'genpkey', '-out', "rsamptest-$name.pem",
|
|
|
|
'-algorithm', 'RSA',
|
|
|
|
'-pkeyopt', "rsa_keygen_primes:$primes",
|
|
|
|
'-pkeyopt', "rsa_keygen_bits:$bits"])),
|
|
|
|
"genrsa $name");
|
2020-02-12 13:03:51 +08:00
|
|
|
ok(run(app([ 'openssl', 'pkey', '-check',
|
|
|
|
'-in', "rsamptest-$name.pem", '-noout'])),
|
|
|
|
"rsa -check $name");
|
2020-02-13 03:29:04 +08:00
|
|
|
ok(run(app([ 'openssl', 'pkeyutl', '-inkey', "rsamptest-$name.pem",
|
2017-08-02 02:19:43 +08:00
|
|
|
'-encrypt', '-in', $cleartext,
|
2020-02-13 03:29:04 +08:00
|
|
|
'-out', "rsamptest-$name.enc" ])),
|
|
|
|
"rsa $name encrypt");
|
|
|
|
ok(run(app([ 'openssl', 'pkeyutl', '-inkey', "rsamptest-$name.pem",
|
|
|
|
'-decrypt', '-in', "rsamptest-$name.enc",
|
|
|
|
'-out', "rsamptest-$name.dec" ])),
|
|
|
|
"rsa $name decrypt");
|
2017-08-02 02:19:43 +08:00
|
|
|
} else {
|
2020-02-12 13:03:51 +08:00
|
|
|
ok(run(app([ 'openssl', 'genrsa', '-out', "rsamptest-$name.pem",
|
|
|
|
'-primes', $primes, $bits])), "genrsa $name");
|
|
|
|
ok(run(app([ 'openssl', 'rsa', '-check',
|
|
|
|
'-in', "rsamptest-$name.pem", '-noout'])),
|
|
|
|
"rsa -check $name");
|
2020-02-13 03:29:04 +08:00
|
|
|
ok(run(app([ 'openssl', 'rsautl', '-inkey', "rsamptest-$name.pem",
|
2017-08-02 02:19:43 +08:00
|
|
|
'-encrypt', '-in', $cleartext,
|
2020-02-13 03:29:04 +08:00
|
|
|
'-out', "rsamptest-$name.enc" ])),
|
|
|
|
"rsa $name encrypt");
|
|
|
|
ok(run(app([ 'openssl', 'rsautl', '-inkey', "rsamptest-$name.pem",
|
|
|
|
'-decrypt', '-in', "rsamptest-$name.enc",
|
|
|
|
'-out', "rsamptest-$name.dec" ])),
|
|
|
|
"rsa $name decrypt");
|
2017-08-02 02:19:43 +08:00
|
|
|
}
|
2020-02-13 03:29:04 +08:00
|
|
|
ok(check_msg("rsamptest-$name.dec"), "rsa $name check result");
|
2017-08-02 02:19:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub check_msg {
|
2020-02-13 03:29:04 +08:00
|
|
|
my $decrypted = shift;
|
2017-08-02 02:19:43 +08:00
|
|
|
my $msg;
|
|
|
|
my $dec;
|
|
|
|
|
|
|
|
open(my $fh, "<", $cleartext) or return 0;
|
|
|
|
binmode $fh;
|
|
|
|
read($fh, $msg, 10240);
|
|
|
|
close $fh;
|
2020-02-13 03:29:04 +08:00
|
|
|
open($fh, "<", $decrypted ) or return 0;
|
2017-08-02 02:19:43 +08:00
|
|
|
binmode $fh;
|
|
|
|
read($fh, $dec, 10240);
|
|
|
|
close $fh;
|
|
|
|
|
|
|
|
if ($msg ne $dec) {
|
|
|
|
print STDERR "cleartext and decrypted are not the same";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|