15-test_mp_rsa.t 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #! /usr/bin/env perl
  2. # Copyright 2017-2020 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. use strict;
  10. use warnings;
  11. use File::Spec;
  12. use OpenSSL::Test qw/:DEFAULT data_file/;
  13. use OpenSSL::Test::Utils;
  14. setup("test_mp_rsa");
  15. my @test_param = (
  16. # 3 primes, 2048-bit
  17. {
  18. primes => '3',
  19. bits => '2048',
  20. },
  21. # 4 primes, 4096-bit
  22. {
  23. primes => '4',
  24. bits => '4096',
  25. },
  26. # 5 primes, 8192-bit
  27. {
  28. primes => '5',
  29. bits => '8192',
  30. },
  31. );
  32. plan tests => 1 + scalar(@test_param) * 5 * (disabled('deprecated-3.0') ? 1 : 2);
  33. ok(run(test(["rsa_mp_test"])), "running rsa multi prime test");
  34. my $cleartext = data_file("plain_text");
  35. # genrsa
  36. run_mp_tests(0) if !disabled('deprecated-3.0');
  37. # evp
  38. run_mp_tests(1);
  39. sub run_mp_tests {
  40. my $evp = shift;
  41. foreach my $param (@test_param) {
  42. my $primes = $param->{primes};
  43. my $bits = $param->{bits};
  44. my $name = ($evp ? "evp" : "") . "${bits}p${primes}";
  45. if ($evp) {
  46. ok(run(app([ 'openssl', 'genpkey', '-out', "rsamptest-$name.pem",
  47. '-algorithm', 'RSA',
  48. '-pkeyopt', "rsa_keygen_primes:$primes",
  49. '-pkeyopt', "rsa_keygen_bits:$bits"])),
  50. "genrsa $name");
  51. ok(run(app([ 'openssl', 'pkey', '-check',
  52. '-in', "rsamptest-$name.pem", '-noout'])),
  53. "rsa -check $name");
  54. ok(run(app([ 'openssl', 'pkeyutl', '-inkey', "rsamptest-$name.pem",
  55. '-encrypt', '-in', $cleartext,
  56. '-out', "rsamptest-$name.enc" ])),
  57. "rsa $name encrypt");
  58. ok(run(app([ 'openssl', 'pkeyutl', '-inkey', "rsamptest-$name.pem",
  59. '-decrypt', '-in', "rsamptest-$name.enc",
  60. '-out', "rsamptest-$name.dec" ])),
  61. "rsa $name decrypt");
  62. } else {
  63. ok(run(app([ 'openssl', 'genrsa', '-out', "rsamptest-$name.pem",
  64. '-primes', $primes, $bits])), "genrsa $name");
  65. ok(run(app([ 'openssl', 'rsa', '-check',
  66. '-in', "rsamptest-$name.pem", '-noout'])),
  67. "rsa -check $name");
  68. ok(run(app([ 'openssl', 'rsautl', '-inkey', "rsamptest-$name.pem",
  69. '-encrypt', '-in', $cleartext,
  70. '-out', "rsamptest-$name.enc" ])),
  71. "rsa $name encrypt");
  72. ok(run(app([ 'openssl', 'rsautl', '-inkey', "rsamptest-$name.pem",
  73. '-decrypt', '-in', "rsamptest-$name.enc",
  74. '-out', "rsamptest-$name.dec" ])),
  75. "rsa $name decrypt");
  76. }
  77. ok(check_msg("rsamptest-$name.dec"), "rsa $name check result");
  78. }
  79. }
  80. sub check_msg {
  81. my $decrypted = shift;
  82. my $msg;
  83. my $dec;
  84. open(my $fh, "<", $cleartext) or return 0;
  85. binmode $fh;
  86. read($fh, $msg, 10240);
  87. close $fh;
  88. open($fh, "<", $decrypted ) or return 0;
  89. binmode $fh;
  90. read($fh, $dec, 10240);
  91. close $fh;
  92. if ($msg ne $dec) {
  93. print STDERR "cleartext and decrypted are not the same";
  94. return 0;
  95. }
  96. return 1;
  97. }