• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2024 The BoringSSL Authors
2//
3// Permission to use, copy, modify, and/or distribute this software for any
4// purpose with or without fee is hereby granted, provided that the above
5// copyright notice and this permission notice appear in all copies.
6//
7// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15//go:build ignore
16
17// This trivial program is used to corrupt the FIPS module. This is done as
18// part of FIPS testing to show that the integrity check is effective.
19//
20// It finds the (sole) occurance of a given hex pattern in a file and flips the
21// first bit. The hex pattern is intended to be the output of running
22// `BORINGSSL_FIPS_SHOW_HASH=1 ninja bcm.o`, i.e. the integrity hash value of
23// the module. By flipping the first bit we ensure that the check will
24// mismatch.
25//
26// This is a simplier version of `break-hash.go` for when we're building with
27// BORINGSSL_FIPS_SHOW_HASH. (But we don't do that in all cases.)
28
29package main
30
31import (
32	"bytes"
33	"encoding/hex"
34	"fmt"
35	"io/ioutil"
36	"os"
37)
38
39func main() {
40	if len(os.Args) != 3 {
41		fmt.Fprintln(os.Stderr, "Usage: program <hex_string> <file_path>")
42		os.Exit(1)
43	}
44
45	hexString := os.Args[1]
46	filePath := os.Args[2]
47
48	// Decode hex string
49	searchBytes, err := hex.DecodeString(hexString)
50	if err != nil {
51		fmt.Fprintln(os.Stderr, "Error decoding hex string:", err)
52		os.Exit(1)
53	}
54
55	// Read file contents
56	content, err := ioutil.ReadFile(filePath)
57	if err != nil {
58		fmt.Fprintln(os.Stderr, "Error reading file:", err)
59		os.Exit(1)
60	}
61
62	// Search for the occurrence of the hex string
63	index := bytes.Index(content, searchBytes)
64	if index == -1 {
65		fmt.Fprintln(os.Stderr, "Hex string not found in the file")
66		os.Exit(1)
67	}
68
69	// Check for other occurrences
70	if bytes.Index(content[index+len(searchBytes):], searchBytes) != -1 {
71		fmt.Fprintln(os.Stderr, "Multiple occurrences of the hex string found")
72		os.Exit(1)
73	}
74
75	// Flip the first bit
76	content[index] ^= 0x80
77
78	// Write updated contents to stdout
79	os.Stdout.Write(content)
80}
81