mk-fipsmodule-cnf.pl 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #! /usr/bin/env perl
  2. # Copyright 2021 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 Getopt::Long;
  9. my $activate = 1;
  10. my $conditional_errors = 1;
  11. my $security_checks = 1;
  12. my $mac_key;
  13. my $module_name;
  14. my $section_name = "fips_sect";
  15. GetOptions("key=s" => \$mac_key,
  16. "module=s" => \$module_name,
  17. "section_name=s" => \$section_name)
  18. or die "Error when getting command line arguments";
  19. my $mac_keylen = length($mac_key);
  20. use Digest::SHA qw(hmac_sha256_hex);
  21. my $module_size = [ stat($module_name) ]->[7];
  22. open my $fh, "<:raw", $module_name or die "Trying to open $module_name: $!";
  23. read $fh, my $data, $module_size or die "Trying to read $module_name: $!";
  24. close $fh;
  25. # Calculate HMAC-SHA256 in hex, and split it into a list of two character
  26. # chunks, and join the chunks with colons.
  27. my @module_mac
  28. = ( uc(hmac_sha256_hex($data, pack("H$mac_keylen", $mac_key))) =~ m/../g );
  29. my $module_mac = join(':', @module_mac);
  30. print <<_____;
  31. [$section_name]
  32. activate = $activate
  33. conditional-errors = $conditional_errors
  34. security-checks = $security_checks
  35. module-mac = $module_mac
  36. _____