20-test_rand_config.t 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #! /usr/bin/env perl
  2. # Copyright 2019-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;
  11. use OpenSSL::Test::Utils;
  12. setup("test_rand_config");
  13. my @rand_tests = (
  14. { drbg => 'HASH-DRBG',
  15. digest => 'SHA2-512/256',
  16. properties => '',
  17. expected => ["HASH-DRBG", "digest: 'SHA2-512/256'"],
  18. desc => 'HASH-DRBG SHA2-512/256' },
  19. { drbg => 'HASH-DRBG',
  20. digest => 'SHA3-256',
  21. properties => '',
  22. expected => ["HASH-DRBG", "digest: 'SHA3-512'"],
  23. desc => 'HASH-DRBG SHA3/512' },
  24. { drbg => 'HMAC-DRBG',
  25. digest => 'SHA3-256',
  26. properties => '',
  27. expected => ["HMAC-DRBG", "mac: HMAC", "digest: 'SHA3-256'"],
  28. desc => 'HMAC-DRBG SHA3/256' },
  29. { cipher => 'AES-128-CTR',
  30. expected => ["CTR-DRBG", "cipher: 'AES-128-CTR'"],
  31. desc => 'CTR-DRBG AES-128 no DRBG' },
  32. { expected => ["CTR-DRBG", "cipher: 'AES-256-CTR'"],
  33. desc => 'CTR-DRBG AES-256 defaults' },
  34. );
  35. my @aria_tests = (
  36. { drbg => 'CTR-DRBG',
  37. cipher => 'ARIA-128-CTR',
  38. properties => '',
  39. expected => ["CTR-DRBG", "cipher: 'ARIA-128-CTR'"],
  40. desc => 'CTR-DRBG ARIA-128' },
  41. { drbg => 'CTR-DRBG',
  42. cipher => 'ARIA-128-CTR',
  43. properties => '',
  44. expected => ["CTR-DRBG", "cipher: 'ARIA-128-CTR'"],
  45. desc => 'CTR-DRBG ARIA-256' },
  46. );
  47. push @rand_tests, @aria_tests unless disabled("aria");
  48. plan tests => scalar @rand_tests;
  49. my $contents =<<'CONFIGEND';
  50. openssl_conf = openssl_init
  51. [openssl_init]
  52. random = random_section
  53. [random_section]
  54. CONFIGEND
  55. foreach (@rand_tests) {
  56. my $tmpfile = 'rand_config.cfg';
  57. open(my $cfg, '>', $tmpfile) or die "Could not open file";
  58. print $cfg $contents;
  59. if ($_->{drbg}) {
  60. print $cfg "random = $_->{drbg}\n";
  61. }
  62. if ($_->{cipher}) {
  63. print $cfg "cipher = $_->{cipher}\n";
  64. }
  65. if ($_->{digest}) {
  66. print $cfg "digest = $_->{digest}\n"
  67. }
  68. close $cfg;
  69. $ENV{OPENSSL_CONF} = $tmpfile;
  70. ok(comparelines($_->{expected}), $_->{desc});
  71. }
  72. # Check that the stdout output contains the expected values.
  73. sub comparelines {
  74. my @lines = run(app(["openssl", "list", "--random-instances"]),
  75. capture => 1);
  76. foreach (@_) {
  77. if ( !grep( /$_/, @lines ) ) {
  78. print "Cannot find: $_\n";
  79. return 0;
  80. }
  81. }
  82. return 1;
  83. }