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