Skip to main content
  • Home
  • login
  • Browse the archive

    swh mirror partner logo
swh logo
SoftwareHeritage
Software
Heritage
Mirror
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • 72a6b9e
  • /
  • recipes
  • /
  • 15-test_mp_rsa.t
Raw File
Permalinks

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge Iframe embedding
swh:1:cnt:2ab4e56f93510ea88aa1d69378f29ce11fb722d0
directory badge Iframe embedding
swh:1:dir:1d961ad0a8206855224851d31ff0ccf02a98cac8
15-test_mp_rsa.t
#! /usr/bin/env perl
# Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
# Copyright 2017 BaishanCloud. All rights reserved.
#
# Licensed under the Apache License 2.0 (the "License").  You may not use
# 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',
    },
    # 4 primes, 4096-bit
    {
        primes => '4',
        bits => '4096',
    },
    # 5 primes, 8192-bit
    {
        primes => '5',
        bits => '8192',
    },
);

plan tests => 1 + scalar(@test_param) * 5 * 2;

ok(run(test(["rsa_mp_test"])), "running rsa multi prime test");

my $cleartext = data_file("plain_text");

# genrsa
run_mp_tests(0);
# 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) {
            ok(run(app([ 'openssl', 'genpkey', '-out', "rsamptest-$name.pem",
                         '-algorithm', 'RSA',
                         '-pkeyopt', "rsa_keygen_primes:$primes",
                         '-pkeyopt', "rsa_keygen_bits:$bits"])),
               "genrsa $name");
            ok(run(app([ 'openssl', 'pkey', '-check',
                         '-in', "rsamptest-$name.pem", '-noout'])),
               "rsa -check $name");
            ok(run(app([ 'openssl', 'pkeyutl', '-inkey', "rsamptest-$name.pem",
                         '-encrypt', '-in', $cleartext,
                         '-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");
        } else {
            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");
            if (!disabled('deprecated-3.0')) {
                ok(run(app([ 'openssl', 'rsautl', '-inkey', "rsamptest-$name.pem",
                             '-encrypt', '-in', $cleartext,
                             '-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");
            } else {
                ok(run(app([ 'openssl', 'pkeyutl', '-inkey', "rsamptest-$name.pem",
                             '-encrypt', '-in', $cleartext,
                             '-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");
            }
        }
        ok(check_msg("rsamptest-$name.dec"), "rsa $name check result");
    }
}

sub check_msg {
    my $decrypted = shift;
    my $msg;
    my $dec;

    open(my $fh, "<", $cleartext) or return 0;
    binmode $fh;
    read($fh, $msg, 10240);
    close $fh;
    open($fh, "<", $decrypted ) or return 0;
    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;
}

ENEA — Copyright (C), ENEA. License: GNU AGPLv3+.
Legal notes  ::  JavaScript license information ::  Web API

back to top