2017-04-04 22:21:43 +01:00
|
|
|
// Copyright (c) 2017, Google Inc.
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
2017-04-12 21:16:10 +01:00
|
|
|
// inject-hash parses an archive containing a file object file. It finds a FIPS
|
|
|
|
// module inside that object, calculates its hash and replaces the default hash
|
|
|
|
// value in the object with the calculated value.
|
2017-04-04 22:21:43 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-04-12 21:16:10 +01:00
|
|
|
"crypto/hmac"
|
2017-05-08 20:42:15 +01:00
|
|
|
"crypto/sha512"
|
2017-04-12 21:16:10 +01:00
|
|
|
"debug/elf"
|
2017-04-04 22:21:43 +01:00
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2017-04-12 21:16:10 +01:00
|
|
|
"io"
|
2017-04-04 22:21:43 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2018-09-13 22:37:28 +01:00
|
|
|
|
2018-09-17 23:40:24 +01:00
|
|
|
"boringssl.googlesource.com/boringssl/util/ar"
|
2018-09-13 22:37:28 +01:00
|
|
|
"boringssl.googlesource.com/boringssl/util/fipstools/fipscommon"
|
2017-04-04 22:21:43 +01:00
|
|
|
)
|
|
|
|
|
2017-04-15 00:08:55 +01:00
|
|
|
func do(outPath, oInput string, arInput string) error {
|
|
|
|
var objectBytes []byte
|
|
|
|
if len(arInput) > 0 {
|
|
|
|
if len(oInput) > 0 {
|
|
|
|
return fmt.Errorf("-in-archive and -in-object are mutually exclusive")
|
|
|
|
}
|
2017-04-04 22:21:43 +01:00
|
|
|
|
2017-04-15 00:08:55 +01:00
|
|
|
arFile, err := os.Open(arInput)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer arFile.Close()
|
2017-04-04 22:21:43 +01:00
|
|
|
|
2018-09-17 23:40:24 +01:00
|
|
|
ar, err := ar.ParseAR(arFile)
|
2017-04-15 00:08:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-04 22:21:43 +01:00
|
|
|
|
2017-04-15 00:08:55 +01:00
|
|
|
if len(ar) != 1 {
|
|
|
|
return fmt.Errorf("expected one file in archive, but found %d", len(ar))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, contents := range ar {
|
|
|
|
objectBytes = contents
|
|
|
|
}
|
|
|
|
} else if len(oInput) > 0 {
|
|
|
|
var err error
|
|
|
|
if objectBytes, err = ioutil.ReadFile(oInput); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("exactly one of -in-archive or -in-object is required")
|
2017-04-04 22:21:43 +01:00
|
|
|
}
|
2017-04-12 21:16:10 +01:00
|
|
|
|
|
|
|
object, err := elf.NewFile(bytes.NewReader(objectBytes))
|
2017-04-04 22:21:43 +01:00
|
|
|
if err != nil {
|
2017-04-12 21:16:10 +01:00
|
|
|
return errors.New("failed to parse object: " + err.Error())
|
2017-04-04 22:21:43 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 21:16:10 +01:00
|
|
|
// Find the .text section.
|
|
|
|
|
|
|
|
var textSection *elf.Section
|
|
|
|
var textSectionIndex elf.SectionIndex
|
|
|
|
for i, section := range object.Sections {
|
|
|
|
if section.Name == ".text" {
|
|
|
|
textSectionIndex = elf.SectionIndex(i)
|
|
|
|
textSection = section
|
|
|
|
break
|
|
|
|
}
|
2017-04-04 22:21:43 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 21:16:10 +01:00
|
|
|
if textSection == nil {
|
|
|
|
return errors.New("failed to find .text section in object")
|
2017-04-04 22:21:43 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 21:16:10 +01:00
|
|
|
// Find the starting and ending symbols for the module.
|
|
|
|
|
|
|
|
var startSeen, endSeen bool
|
|
|
|
var start, end uint64
|
|
|
|
|
|
|
|
symbols, err := object.Symbols()
|
2017-04-07 01:29:10 +01:00
|
|
|
if err != nil {
|
2017-04-12 21:16:10 +01:00
|
|
|
return errors.New("failed to parse symbols: " + err.Error())
|
2017-04-07 01:29:10 +01:00
|
|
|
}
|
2017-04-04 22:21:43 +01:00
|
|
|
|
2017-04-12 21:16:10 +01:00
|
|
|
for _, symbol := range symbols {
|
|
|
|
if symbol.Section != textSectionIndex {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch symbol.Name {
|
|
|
|
case "BORINGSSL_bcm_text_start":
|
|
|
|
if startSeen {
|
|
|
|
return errors.New("duplicate start symbol found")
|
|
|
|
}
|
|
|
|
startSeen = true
|
|
|
|
start = symbol.Value
|
|
|
|
case "BORINGSSL_bcm_text_end":
|
|
|
|
if endSeen {
|
|
|
|
return errors.New("duplicate end symbol found")
|
|
|
|
}
|
|
|
|
endSeen = true
|
|
|
|
end = symbol.Value
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
2017-04-04 22:21:43 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 21:16:10 +01:00
|
|
|
if !startSeen || !endSeen {
|
|
|
|
return errors.New("could not find module boundaries in object")
|
|
|
|
}
|
|
|
|
|
|
|
|
if max := textSection.Size; start > max || start > end || end > max {
|
|
|
|
return fmt.Errorf("invalid module boundaries: start: %x, end: %x, max: %x", start, end, max)
|
2017-04-04 22:21:43 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 21:16:10 +01:00
|
|
|
// Extract the module from the .text section and hash it.
|
|
|
|
|
|
|
|
text := textSection.Open()
|
|
|
|
if _, err := text.Seek(int64(start), 0); err != nil {
|
|
|
|
return errors.New("failed to seek to module start in .text: " + err.Error())
|
|
|
|
}
|
|
|
|
moduleText := make([]byte, end-start)
|
|
|
|
if _, err := io.ReadFull(text, moduleText); err != nil {
|
|
|
|
return errors.New("failed to read .text: " + err.Error())
|
|
|
|
}
|
|
|
|
|
2017-05-08 20:42:15 +01:00
|
|
|
var zeroKey [64]byte
|
|
|
|
mac := hmac.New(sha512.New, zeroKey[:])
|
2017-04-12 21:16:10 +01:00
|
|
|
mac.Write(moduleText)
|
|
|
|
calculated := mac.Sum(nil)
|
|
|
|
|
|
|
|
// Replace the default hash value in the object with the calculated
|
|
|
|
// value and write it out.
|
|
|
|
|
2018-09-13 22:37:28 +01:00
|
|
|
offset := bytes.Index(objectBytes, fipscommon.UninitHashValue[:])
|
2017-04-04 22:21:43 +01:00
|
|
|
if offset < 0 {
|
|
|
|
return errors.New("did not find uninitialised hash value in object file")
|
|
|
|
}
|
|
|
|
|
2018-09-13 22:37:28 +01:00
|
|
|
if bytes.Index(objectBytes[offset+1:], fipscommon.UninitHashValue[:]) >= 0 {
|
2017-04-04 22:21:43 +01:00
|
|
|
return errors.New("found two occurrences of uninitialised hash value in object file")
|
|
|
|
}
|
|
|
|
|
2017-04-12 21:16:10 +01:00
|
|
|
copy(objectBytes[offset:], calculated)
|
2017-04-04 22:21:43 +01:00
|
|
|
|
2017-04-12 21:16:10 +01:00
|
|
|
return ioutil.WriteFile(outPath, objectBytes, 0644)
|
2017-04-04 22:21:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2017-04-15 00:08:55 +01:00
|
|
|
arInput := flag.String("in-archive", "", "Path to a .a file")
|
|
|
|
oInput := flag.String("in-object", "", "Path to a .o file")
|
2017-04-04 22:21:43 +01:00
|
|
|
outPath := flag.String("o", "", "Path to output object")
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
2017-04-15 00:08:55 +01:00
|
|
|
if err := do(*outPath, *oInput, *arInput); err != nil {
|
2017-04-04 22:21:43 +01:00
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|