15-test_rsaoaep.t 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #! /usr/bin/env perl
  2. # Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. use strict;
  9. use warnings;
  10. use OpenSSL::Test qw(:DEFAULT data_file bldtop_dir srctop_file srctop_dir bldtop_file);
  11. use OpenSSL::Test::Utils;
  12. use File::Compare qw/compare_text/;
  13. BEGIN {
  14. setup("test_rsaoaep");
  15. }
  16. use lib srctop_dir('Configurations');
  17. use lib bldtop_dir('.');
  18. use platform;
  19. my $no_fips = disabled('fips') || ($ENV{NO_FIPS} // 0);
  20. my $no_check = disabled('fips-securitychecks');
  21. plan tests =>
  22. ($no_fips ? 0 : 1 + ($no_check ? 0 : 1)) # FIPS install test
  23. + 9;
  24. my @prov = ( );
  25. my $provconf = srctop_file("test", "fips-and-base.cnf");
  26. my $provpath = bldtop_dir("providers");
  27. my $msg_file = data_file("plain_text");
  28. my $enc1_file = "enc1.bin";
  29. my $enc2_file = "enc2.bin";
  30. my $enc3_file = "enc3.bin";
  31. my $dec1_file = "dec1.txt";
  32. my $dec2_file = "dec2.txt";
  33. my $dec3_file = "dec3.txt";
  34. my $key_file = srctop_file("test", "testrsa2048.pem");
  35. my $small_key_file = srctop_file("test", "testrsa.pem");
  36. unless ($no_fips) {
  37. @prov = ( "-provider-path", $provpath, "-config", $provconf );
  38. my $infile = bldtop_file('providers', platform->dso('fips'));
  39. ok(run(app(['openssl', 'fipsinstall',
  40. '-out', bldtop_file('providers', 'fipsmodule.cnf'),
  41. '-module', $infile])),
  42. "fipsinstall");
  43. $ENV{OPENSSL_TEST_LIBCTX} = "1";
  44. unless ($no_check) {
  45. ok(!run(app(['openssl', 'pkeyutl',
  46. @prov,
  47. '-encrypt',
  48. '-in', $msg_file,
  49. '-inkey', $small_key_file,
  50. '-pkeyopt', 'pad-mode:oaep',
  51. '-pkeyopt', 'oaep-label:123',
  52. '-pkeyopt', 'digest:sha1',
  53. '-pkeyopt', 'mgf1-digest:sha1',
  54. '-out', $enc1_file])),
  55. "RSA OAEP Encryption with a key smaller than 2048 in fips mode should fail");
  56. }
  57. }
  58. ok(run(app(['openssl', 'pkeyutl',
  59. @prov,
  60. '-encrypt',
  61. '-in', $msg_file,
  62. '-inkey', $key_file,
  63. '-pkeyopt', 'pad-mode:oaep',
  64. '-pkeyopt', 'oaep-label:123',
  65. '-pkeyopt', 'digest:sha1',
  66. '-pkeyopt', 'mgf1-digest:sha1',
  67. '-out', $enc1_file])),
  68. "RSA OAEP Encryption");
  69. ok(!run(app(['openssl', 'pkeyutl',
  70. @prov,
  71. '-encrypt',
  72. '-in', $key_file,
  73. '-inkey', $key_file,
  74. '-pkeyopt', 'pad-mode:oaep',
  75. '-pkeyopt', 'oaep-label:123',
  76. '-pkeyopt', 'digest:sha256',
  77. '-pkeyopt', 'mgf1-digest:sha1'])),
  78. "RSA OAEP Encryption should fail if the message is larger than the rsa modulus");
  79. ok(run(app(['openssl', 'pkeyutl',
  80. @prov,
  81. '-decrypt',
  82. '-inkey', $key_file,
  83. '-pkeyopt', 'pad-mode:oaep',
  84. '-pkeyopt', 'oaep-label:123',
  85. '-pkeyopt', 'digest:sha1',
  86. '-pkeyopt', 'mgf1-digest:sha1',
  87. '-in', $enc1_file,
  88. '-out', $dec1_file]))
  89. && compare_text($dec1_file, $msg_file) == 0,
  90. "RSA OAEP Decryption");
  91. ok(!run(app(['openssl', 'pkeyutl',
  92. @prov,
  93. '-decrypt',
  94. '-inkey', $key_file,
  95. '-pkeyopt', 'pad-mode:oaep',
  96. '-pkeyopt', 'oaep-label:123',
  97. '-pkeyopt', 'digest:sha256',
  98. '-pkeyopt', 'mgf1-digest:sha224',
  99. '-in', $enc1_file])),
  100. "Incorrect digest for RSA OAEP Decryption");
  101. ok(!run(app(['openssl', 'pkeyutl',
  102. @prov,
  103. '-decrypt',
  104. '-inkey', $key_file,
  105. '-pkeyopt', 'pad-mode:oaep',
  106. '-pkeyopt', 'oaep-label:123',
  107. '-pkeyopt', 'digest:sha1',
  108. '-pkeyopt', 'mgf1-digest:sha224',
  109. '-in', $enc1_file])),
  110. "Incorrect mgf1-digest for RSA OAEP Decryption");
  111. ok(run(app(['openssl', 'pkeyutl',
  112. @prov,
  113. '-encrypt',
  114. '-in', $msg_file,
  115. '-inkey', $key_file,
  116. '-pkeyopt', 'pad-mode:oaep',
  117. '-pkeyopt', 'oaep-label:123',
  118. '-pkeyopt', 'digest:sha1',
  119. '-pkeyopt', 'mgf1-digest:sha1',
  120. '-out', $enc2_file]))
  121. && compare_text($enc2_file, $enc1_file) != 0,
  122. "RSA OAEP Encryption should generate different encrypted data");
  123. ok(run(app(['openssl', 'pkeyutl',
  124. @prov,
  125. '-decrypt',
  126. '-inkey', $key_file,
  127. '-pkeyopt', 'pad-mode:oaep',
  128. '-pkeyopt', 'oaep-label:123',
  129. '-in', $enc2_file,
  130. '-out', $dec2_file]))
  131. && compare_text($dec2_file, $msg_file) == 0,
  132. "RSA OAEP Decryption with default digests");
  133. ok(run(app(['openssl', 'pkeyutl',
  134. @prov,
  135. '-encrypt',
  136. '-in', $msg_file,
  137. '-inkey', $key_file,
  138. '-pkeyopt', 'pad-mode:oaep',
  139. '-pkeyopt', 'oaep-label:123',
  140. '-out', $enc3_file])),
  141. "RSA OAEP Encryption with default digests");
  142. ok(run(app(['openssl', 'pkeyutl',
  143. @prov,
  144. '-decrypt',
  145. '-inkey', $key_file,
  146. '-pkeyopt', 'pad-mode:oaep',
  147. '-pkeyopt', 'oaep-label:123',
  148. '-pkeyopt', 'digest:sha1',
  149. '-pkeyopt', 'mgf1-digest:sha1',
  150. '-in', $enc3_file,
  151. '-out', $dec3_file]))
  152. && compare_text($dec3_file, $msg_file) == 0,
  153. "RSA OAEP Decryption with explicit default digests");