| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 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 |
| 29 | package main |
| 30 |
| 31 | import ( |
| 32 | "bytes" |
| 33 | "encoding/hex" |
| 34 | "fmt" |
| 35 | "os" |
| 36 | ) |
| 37 |
| 38 | func main() { |
| 39 | if len(os.Args) != 3 { |
| 40 | fmt.Fprintln(os.Stderr, "Usage: program <hex_string> <file_path>") |
| 41 | os.Exit(1) |
| 42 | } |
| 43 |
| 44 | hexString := os.Args[1] |
| 45 | filePath := os.Args[2] |
| 46 |
| 47 | // Decode hex string |
| 48 | searchBytes, err := hex.DecodeString(hexString) |
| 49 | if err != nil { |
| 50 | fmt.Fprintln(os.Stderr, "Error decoding hex string:", err) |
| 51 | os.Exit(1) |
| 52 | } |
| 53 |
| 54 | // Read file contents |
| 55 | content, err := os.ReadFile(filePath) |
| 56 | if err != nil { |
| 57 | fmt.Fprintln(os.Stderr, "Error reading file:", err) |
| 58 | os.Exit(1) |
| 59 | } |
| 60 |
| 61 | // Search for the occurrence of the hex string |
| 62 | index := bytes.Index(content, searchBytes) |
| 63 | if index == -1 { |
| 64 | fmt.Fprintln(os.Stderr, "Hex string not found in the file") |
| 65 | os.Exit(1) |
| 66 | } |
| 67 |
| 68 | // Check for other occurrences |
| 69 | if bytes.Index(content[index+len(searchBytes):], searchBytes) != -1 { |
| 70 | fmt.Fprintln(os.Stderr, "Multiple occurrences of the hex string found") |
| 71 | os.Exit(1) |
| 72 | } |
| 73 |
| 74 | // Flip the first bit |
| 75 | content[index] ^= 0x80 |
| 76 |
| 77 | // Write updated contents to stdout |
| 78 | os.Stdout.Write(content) |
| 79 | } |
| 80 |