1e1051a39Sopenharmony_ci#! /usr/bin/env perl
2e1051a39Sopenharmony_ci# Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci#
4e1051a39Sopenharmony_ci# Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci# this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci# in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci# https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci
9e1051a39Sopenharmony_ciuse Getopt::Long;
10e1051a39Sopenharmony_ci
11e1051a39Sopenharmony_cimy $activate = 1;
12e1051a39Sopenharmony_cimy $conditional_errors = 1;
13e1051a39Sopenharmony_cimy $security_checks = 1;
14e1051a39Sopenharmony_cimy $mac_key;
15e1051a39Sopenharmony_cimy $module_name;
16e1051a39Sopenharmony_cimy $section_name = "fips_sect";
17e1051a39Sopenharmony_ci
18e1051a39Sopenharmony_ciGetOptions("key=s"              => \$mac_key,
19e1051a39Sopenharmony_ci           "module=s"           => \$module_name,
20e1051a39Sopenharmony_ci           "section_name=s"     => \$section_name)
21e1051a39Sopenharmony_ci    or die "Error when getting command line arguments";
22e1051a39Sopenharmony_ci
23e1051a39Sopenharmony_cimy $mac_keylen = length($mac_key);
24e1051a39Sopenharmony_ci
25e1051a39Sopenharmony_ciuse Digest::SHA qw(hmac_sha256_hex);
26e1051a39Sopenharmony_cimy $module_size = [ stat($module_name) ]->[7];
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_ciopen my $fh, "<:raw", $module_name or die "Trying to open $module_name: $!";
29e1051a39Sopenharmony_ciread $fh, my $data, $module_size or die "Trying to read $module_name: $!";
30e1051a39Sopenharmony_ciclose $fh;
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_ci# Calculate HMAC-SHA256 in hex, and split it into a list of two character
33e1051a39Sopenharmony_ci# chunks, and join the chunks with colons.
34e1051a39Sopenharmony_cimy @module_mac
35e1051a39Sopenharmony_ci    = ( uc(hmac_sha256_hex($data, pack("H$mac_keylen", $mac_key))) =~ m/../g );
36e1051a39Sopenharmony_cimy $module_mac = join(':', @module_mac);
37e1051a39Sopenharmony_ci
38e1051a39Sopenharmony_ciprint <<_____;
39e1051a39Sopenharmony_ci[$section_name]
40e1051a39Sopenharmony_ciactivate = $activate
41e1051a39Sopenharmony_ciconditional-errors = $conditional_errors
42e1051a39Sopenharmony_cisecurity-checks = $security_checks
43e1051a39Sopenharmony_cimodule-mac = $module_mac
44e1051a39Sopenharmony_ci_____
45