15-test_mp_rsa.t 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #! /usr/bin/env perl
  2. # Copyright 2017-2021 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 * 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);
  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. if (!disabled('deprecated-3.0')) {
  69. ok(run(app([ 'openssl', 'rsautl', '-inkey', "rsamptest-$name.pem",
  70. '-encrypt', '-in', $cleartext,
  71. '-out', "rsamptest-$name.enc" ])),
  72. "rsa $name encrypt");
  73. ok(run(app([ 'openssl', 'rsautl', '-inkey', "rsamptest-$name.pem",
  74. '-decrypt', '-in', "rsamptest-$name.enc",
  75. '-out', "rsamptest-$name.dec" ])),
  76. "rsa $name decrypt");
  77. } else {
  78. ok(run(app([ 'openssl', 'pkeyutl', '-inkey', "rsamptest-$name.pem",
  79. '-encrypt', '-in', $cleartext,
  80. '-out', "rsamptest-$name.enc" ])),
  81. "rsa $name encrypt");
  82. ok(run(app([ 'openssl', 'pkeyutl', '-inkey', "rsamptest-$name.pem",
  83. '-decrypt', '-in', "rsamptest-$name.enc",
  84. '-out', "rsamptest-$name.dec" ])),
  85. "rsa $name decrypt");
  86. }
  87. }
  88. ok(check_msg("rsamptest-$name.dec"), "rsa $name check result");
  89. }
  90. }
  91. sub check_msg {
  92. my $decrypted = shift;
  93. my $msg;
  94. my $dec;
  95. open(my $fh, "<", $cleartext) or return 0;
  96. binmode $fh;
  97. read($fh, $msg, 10240);
  98. close $fh;
  99. open($fh, "<", $decrypted ) or return 0;
  100. binmode $fh;
  101. read($fh, $dec, 10240);
  102. close $fh;
  103. if ($msg ne $dec) {
  104. print STDERR "cleartext and decrypted are not the same";
  105. return 0;
  106. }
  107. return 1;
  108. }