2
0

15-test_mp_rsa.t 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. 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. plan tests => 31;
  16. ok(run(test(["rsa_mp_test"])), "running rsa multi prime test");
  17. my $cleartext = data_file("plain_text");
  18. my @test_param = (
  19. # 3 primes, 2048-bit
  20. {
  21. primes => '3',
  22. bits => '2048',
  23. },
  24. # 4 primes, 4096-bit
  25. {
  26. primes => '4',
  27. bits => '4096',
  28. },
  29. # 5 primes, 8192-bit
  30. {
  31. primes => '5',
  32. bits => '8192',
  33. },
  34. );
  35. # genrsa
  36. run_mp_tests(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.pem',
  47. '-algorithm', 'RSA', '-pkeyopt', "rsa_keygen_primes:$primes",
  48. '-pkeyopt', "rsa_keygen_bits:$bits"])), "genrsa $name");
  49. } else {
  50. ok(run(app([ 'openssl', 'genrsa', '-out', 'rsamptest.pem',
  51. '-primes', $primes, $bits])), "genrsa $name");
  52. }
  53. ok(run(app([ 'openssl', 'rsa', '-check', '-in', 'rsamptest.pem',
  54. '-noout'])), "rsa -check $name");
  55. if ($evp) {
  56. ok(run(app([ 'openssl', 'pkeyutl', '-inkey', 'rsamptest.pem',
  57. '-encrypt', '-in', $cleartext,
  58. '-out', 'rsamptest.enc' ])), "rsa $name encrypt");
  59. ok(run(app([ 'openssl', 'pkeyutl', '-inkey', 'rsamptest.pem',
  60. '-decrypt', '-in', 'rsamptest.enc',
  61. '-out', 'rsamptest.dec' ])), "rsa $name decrypt");
  62. } else {
  63. ok(run(app([ 'openssl', 'rsautl', '-inkey', 'rsamptest.pem',
  64. '-encrypt', '-in', $cleartext,
  65. '-out', 'rsamptest.enc' ])), "rsa $name encrypt");
  66. ok(run(app([ 'openssl', 'rsautl', '-inkey', 'rsamptest.pem',
  67. '-decrypt', '-in', 'rsamptest.enc',
  68. '-out', 'rsamptest.dec' ])), "rsa $name decrypt");
  69. }
  70. ok(check_msg(), "rsa $name check result");
  71. # clean up temp files
  72. unlink 'rsamptest.pem';
  73. unlink 'rsamptest.enc';
  74. unlink 'rsamptest.dec';
  75. }
  76. }
  77. sub check_msg {
  78. my $msg;
  79. my $dec;
  80. open(my $fh, "<", $cleartext) or return 0;
  81. binmode $fh;
  82. read($fh, $msg, 10240);
  83. close $fh;
  84. open($fh, "<", "rsamptest.dec") or return 0;
  85. binmode $fh;
  86. read($fh, $dec, 10240);
  87. close $fh;
  88. if ($msg ne $dec) {
  89. print STDERR "cleartext and decrypted are not the same";
  90. return 0;
  91. }
  92. return 1;
  93. }