2
0

20-test_dgst.t 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #! /usr/bin/env perl
  2. # Copyright 2017-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 File::Spec;
  11. use File::Basename;
  12. use OpenSSL::Test qw/:DEFAULT with srctop_file/;
  13. use OpenSSL::Test::Utils;
  14. setup("test_dgst");
  15. plan tests => 6;
  16. sub tsignverify {
  17. my $testtext = shift;
  18. my $privkey = shift;
  19. my $pubkey = shift;
  20. my $data_to_sign = srctop_file('test', 'data.txt');
  21. my $other_data = srctop_file('test', 'data2.txt');
  22. my $sigfile = basename($privkey, '.pem') . '.sig';
  23. plan tests => 4;
  24. ok(run(app(['openssl', 'dgst', '-sign', $privkey,
  25. '-out', $sigfile,
  26. $data_to_sign])),
  27. $testtext.": Generating signature");
  28. ok(run(app(['openssl', 'dgst', '-prverify', $privkey,
  29. '-signature', $sigfile,
  30. $data_to_sign])),
  31. $testtext.": Verify signature with private key");
  32. ok(run(app(['openssl', 'dgst', '-verify', $pubkey,
  33. '-signature', $sigfile,
  34. $data_to_sign])),
  35. $testtext.": Verify signature with public key");
  36. ok(!run(app(['openssl', 'dgst', '-verify', $pubkey,
  37. '-signature', $sigfile,
  38. $other_data])),
  39. $testtext.": Expect failure verifying mismatching data");
  40. }
  41. SKIP: {
  42. skip "RSA is not supported by this OpenSSL build", 1
  43. if disabled("rsa");
  44. subtest "RSA signature generation and verification with `dgst` CLI" => sub {
  45. tsignverify("RSA",
  46. srctop_file("test","testrsa.pem"),
  47. srctop_file("test","testrsapub.pem"));
  48. };
  49. }
  50. SKIP: {
  51. skip "DSA is not supported by this OpenSSL build", 1
  52. if disabled("dsa");
  53. subtest "DSA signature generation and verification with `dgst` CLI" => sub {
  54. tsignverify("DSA",
  55. srctop_file("test","testdsa.pem"),
  56. srctop_file("test","testdsapub.pem"));
  57. };
  58. }
  59. SKIP: {
  60. skip "ECDSA is not supported by this OpenSSL build", 1
  61. if disabled("ec");
  62. subtest "ECDSA signature generation and verification with `dgst` CLI" => sub {
  63. tsignverify("ECDSA",
  64. srctop_file("test","testec-p256.pem"),
  65. srctop_file("test","testecpub-p256.pem"));
  66. };
  67. }
  68. SKIP: {
  69. skip "EdDSA is not supported by this OpenSSL build", 2
  70. if disabled("ec");
  71. skip "EdDSA is not supported with `dgst` CLI", 2;
  72. subtest "Ed25519 signature generation and verification with `dgst` CLI" => sub {
  73. tsignverify("Ed25519",
  74. srctop_file("test","tested25519.pem"),
  75. srctop_file("test","tested25519pub.pem"));
  76. };
  77. subtest "Ed448 signature generation and verification with `dgst` CLI" => sub {
  78. tsignverify("Ed448",
  79. srctop_file("test","tested448.pem"),
  80. srctop_file("test","tested448pub.pem"));
  81. };
  82. }
  83. subtest "HMAC generation with `dgst` CLI" => sub {
  84. plan tests => 2;
  85. my $testdata = srctop_file('test', 'data.txt');
  86. #HMAC the data twice to check consistency
  87. my @hmacdata = run(app(['openssl', 'dgst', '-sha256', '-hmac', '123456',
  88. $testdata, $testdata]), capture => 1);
  89. chomp(@hmacdata);
  90. my $expected = qr/HMAC-SHA256\([^\)]*data.txt\)= 6f12484129c4a761747f13d8234a1ff0e074adb34e9e9bf3a155c391b97b9a7c/;
  91. ok($hmacdata[0] =~ $expected, "HMAC: Check HMAC value is as expected ($hmacdata[0]) vs ($expected)");
  92. ok($hmacdata[1] =~ $expected,
  93. "HMAC: Check second HMAC value is consistent with the first ($hmacdata[1]) vs ($expected)");
  94. };