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 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 20plan tests => 31; 21 22ok(run(test(["rsa_mp_test"])), "running rsa multi prime test"); 23 24my $cleartext = data_file("plain_text"); 25 26my @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 45run_mp_tests(0); 46# evp 47run_mp_tests(1); 48 49sub 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.pem', 59 '-algorithm', 'RSA', '-pkeyopt', "rsa_keygen_primes:$primes", 60 '-pkeyopt', "rsa_keygen_bits:$bits"])), "genrsa $name"); 61 } else { 62 ok(run(app([ 'openssl', 'genrsa', '-out', 'rsamptest.pem', 63 '-primes', $primes, $bits])), "genrsa $name"); 64 } 65 66 ok(run(app([ 'openssl', 'rsa', '-check', '-in', 'rsamptest.pem', 67 '-noout'])), "rsa -check $name"); 68 if ($evp) { 69 ok(run(app([ 'openssl', 'pkeyutl', '-inkey', 'rsamptest.pem', 70 '-encrypt', '-in', $cleartext, 71 '-out', 'rsamptest.enc' ])), "rsa $name encrypt"); 72 ok(run(app([ 'openssl', 'pkeyutl', '-inkey', 'rsamptest.pem', 73 '-decrypt', '-in', 'rsamptest.enc', 74 '-out', 'rsamptest.dec' ])), "rsa $name decrypt"); 75 } else { 76 ok(run(app([ 'openssl', 'rsautl', '-inkey', 'rsamptest.pem', 77 '-encrypt', '-in', $cleartext, 78 '-out', 'rsamptest.enc' ])), "rsa $name encrypt"); 79 ok(run(app([ 'openssl', 'rsautl', '-inkey', 'rsamptest.pem', 80 '-decrypt', '-in', 'rsamptest.enc', 81 '-out', 'rsamptest.dec' ])), "rsa $name decrypt"); 82 } 83 84 ok(check_msg(), "rsa $name check result"); 85 86 # clean up temp files 87 unlink 'rsamptest.pem'; 88 unlink 'rsamptest.enc'; 89 unlink 'rsamptest.dec'; 90 } 91} 92 93sub check_msg { 94 my $msg; 95 my $dec; 96 97 open(my $fh, "<", $cleartext) or return 0; 98 binmode $fh; 99 read($fh, $msg, 10240); 100 close $fh; 101 open($fh, "<", "rsamptest.dec") or return 0; 102 binmode $fh; 103 read($fh, $dec, 10240); 104 close $fh; 105 106 if ($msg ne $dec) { 107 print STDERR "cleartext and decrypted are not the same"; 108 return 0; 109 } 110 return 1; 111} 112