aws-lc-fips-sys 0.13.8

AWS-LC is a general-purpose cryptographic library maintained by the AWS Cryptography team for AWS and their customers. This is the FIPS validated version of AWS-LC.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package main

import (
	"bytes"
	"crypto/hmac"
	"crypto/sha256"
	"debug/elf"
	"encoding/binary"
	"encoding/hex"
	"flag"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"strings"
)

const usageTemplate = `
Compare the fipsmodule hash of two shared libraries or two binaries statically linked AWS-LC:

%s <fileLeft> <fileRight>

Print the fipsmodule hash of a shared library or a binary statically linked AWS-LC.
Options:
-verify used to verify that the contents of the module match the recorded hash.
-extract used to extract the BCM .text and .rodata sections explicitly for inspection. This will dump in binary and hex.

%s [-verify] [-extract] <file>

`

const (
	fipsModuleHashSymbol      = "BORINGSSL_bcm_text_hash"
	fipsModuleTextSection     = "BORINGSSL_bcm_text"
	fipsModuleReadOnlySection = "BORINGSSL_bcm_rodata"
)

var (
	usageStr string
	verify   bool
	extract  bool
)

func init() {
	programName := filepath.Base(os.Args[0])
	usageStr = fmt.Sprintf(usageTemplate, programName, programName)
	flag.BoolVar(&verify, "verify", false, "Verify the recorded fipsmodule hash of a shared library or a binary statically linked AWS-LC to it's contents")
	flag.BoolVar(&extract, "extract", false, "Extract the BCM sections to output files")
	flag.CommandLine.Usage = func() {
		_, _ = fmt.Fprint(os.Stderr, usageStr)
		// Exit handled by flag package
	}
}

func printErrorAndExit(message string, args ...interface{}) {
	_, _ = fmt.Fprintf(os.Stderr, message, args...)
	os.Exit(1)
}

func printMessage(message string, args ...interface{}) {
	_, _ = fmt.Fprintf(os.Stdout, message, args...)
}

func main() {
	flag.Parse()

	switch {
	case len(flag.Args()) == 1:
		printModuleHash(flag.Arg(0))
	case len(flag.Args()) == 2:
		diffModules(flag.Arg(0), flag.Arg(1))
	default:
		printErrorAndExit(usageStr)
	}
}

func printModuleHash(filepath string) {
	module, err := loadFIPSModule(filepath)
	if err != nil {
		printErrorAndExit("Error: %v\n", err)
	}
	defer module.Close()

	hashBytes, err := getFIPSModuleIntegrityHash(&module)
	if err != nil {
		printErrorAndExit("Error: %v\n", err)
	}

	hexStr := hex.EncodeToString(hashBytes)
	printMessage("%s\n", hexStr)

	if verify {
		computed, err := computeFIPSModuleIntegrityHash(&module)
		if err != nil {
			printErrorAndExit("Error Verifying Integrity Hash: %v\n", err)
		}
		if bytes.Equal(hashBytes, computed) {
			printMessage("Integrity Hash: VERIFIED\n")
		} else {
			printErrorAndExit("Integrity Hash: MISMATCH\nComputed: %v\n", hex.EncodeToString(computed))
		}
	}

	if extract {
		textBinary, textHex, rodataBinary, rodataHex, err := extractModuleSections(&module)
		if err != nil {
			printErrorAndExit("Error: %v\n", err)
		}
		printMessage("Extracted %s: %s\n", fipsModuleTextSection, textBinary)
		printMessage("Extracted %s: %s\n", fipsModuleTextSection, textHex)
		if rodataBinary != "" && rodataHex != "" {
			printMessage("Extracted %s: %s\n", fipsModuleReadOnlySection, rodataBinary)
			printMessage("Extracted %s: %s\n", fipsModuleReadOnlySection, rodataHex)
		}
	}
}

func diffModules(left, right string) {
	leftModule, err := loadFIPSModule(left)
	if err != nil {
		printErrorAndExit("Error: %v\n", err)
	}
	defer leftModule.Close()

	leftHashBytes, err := getFIPSModuleIntegrityHash(&leftModule)
	if err != nil {
		printErrorAndExit("Error: %v\n", err)
	}

	rightModule, err := loadFIPSModule(right)
	if err != nil {
		printErrorAndExit("Error: %v\n", err)
	}
	defer rightModule.Close()

	rightHashBytes, err := getFIPSModuleIntegrityHash(&rightModule)
	if err != nil {
		printErrorAndExit("Error: %v\n", err)
	}

	leftHexStr := hex.EncodeToString(leftHashBytes)
	rightHexStr := hex.EncodeToString(rightHashBytes)

	if leftHexStr != rightHexStr {
		printErrorAndExit("MISMATCH: (%s != %s)\n", leftHexStr, rightHexStr)
	}

	printMessage("MATCH: (%s)\n", leftHexStr)
}

type FIPSModule struct {
	file        *elf.File
	hash        *elf.Symbol
	textStart   *elf.Symbol
	textEnd     *elf.Symbol
	rodataStart *elf.Symbol
	rodataEnd   *elf.Symbol
}

func (m *FIPSModule) Close() error {
	m.hash = nil
	m.textStart = nil
	m.textEnd = nil
	m.rodataStart = nil
	m.rodataEnd = nil
	return m.file.Close()
}

func (m *FIPSModule) HasROData() bool {
	return m.rodataStart != nil && m.rodataEnd != nil
}

func loadFIPSModule(filepath string) (FIPSModule, error) {
	file, err := elf.Open(filepath)
	if err != nil {
		return FIPSModule{}, fmt.Errorf("failed to open file: %w", err)
	}

	symbols, err := file.Symbols()
	if err != nil {
		return FIPSModule{}, fmt.Errorf("error reading symbols: %w", err)
	}

	var hashSymbol, textStart, textEnd, rodataStart, rodataEnd *elf.Symbol

	for _, symbol := range symbols {
		symbol := symbol
		if symbol.Name == fipsModuleHashSymbol {
			hashSymbol = &symbol
		} else if strings.HasPrefix(symbol.Name, fipsModuleTextSection) {
			if strings.HasSuffix(symbol.Name, "_start") {
				textStart = &symbol
			} else if strings.HasSuffix(symbol.Name, "_end") {
				textEnd = &symbol
			} else {
				return FIPSModule{}, fmt.Errorf("unexpected symbol name: %s", symbol.Name)
			}
		} else if strings.HasPrefix(symbol.Name, fipsModuleReadOnlySection) {
			if strings.HasSuffix(symbol.Name, "_start") {
				rodataStart = &symbol
			} else if strings.HasSuffix(symbol.Name, "_end") {
				rodataEnd = &symbol
			} else {
				return FIPSModule{}, fmt.Errorf("unexpected symbol name: %s", symbol.Name)
			}
		}
	}

	if hashSymbol == nil {
		return FIPSModule{}, fmt.Errorf("failed to find fipsmodule hash symbol")
	}

	// We should always find the the .text section start and end markers. The .rodata section markers will only
	// be present for shared library builds.
	if textStart == nil || textEnd == nil {
		return FIPSModule{}, fmt.Errorf("failed to find fipsmodule .text start and end markers")
	}

	if (rodataStart != nil) != (rodataEnd != nil) {
		return FIPSModule{}, fmt.Errorf("inconsistent fipsmodule .rodata start and end markers")
	}

	return FIPSModule{
		file:        file,
		hash:        hashSymbol,
		textStart:   textStart,
		textEnd:     textEnd,
		rodataStart: rodataStart,
		rodataEnd:   rodataEnd,
	}, nil
}

func getFIPSModuleIntegrityHash(module *FIPSModule) ([]byte, error) {
	hashSymbolSection := module.file.Sections[module.hash.Section]
	if hashSymbolSection == nil {
		return nil, fmt.Errorf("fipsmodule hash symbol's section not found")
	}

	hashSymbolOffset := module.hash.Value - hashSymbolSection.Addr
	const expectedSize = 32
	if module.hash.Size != expectedSize {
		return nil, fmt.Errorf("the fipsmodule hash symbol does not match the expected size, expect %v got %v", expectedSize, module.hash.Size)
	}

	hashSymbolSectionData, err := hashSymbolSection.Data()
	if err != nil {
		return nil, fmt.Errorf("error reading section containing fipsmodule hash symbol: %w", err)
	}

	if int(hashSymbolOffset+module.hash.Size) > len(hashSymbolSectionData) {
		return nil, fmt.Errorf("the fipsmodule hash extends out of bounds of the section?")
	}

	return hashSymbolSectionData[hashSymbolOffset : hashSymbolOffset+module.hash.Size], nil
}

func computeFIPSModuleIntegrityHash(module *FIPSModule) ([]byte, error) {
	length, subReader, err := getSubSectionReader(module.file, module.textStart, module.textEnd)
	if err != nil {
		return nil, fmt.Errorf("failed to extract .text section: %w", err)
	}

	var lengthBytes [8]byte
	var zeroKey [64]byte
	hmacHash := hmac.New(sha256.New, zeroKey[:])
	binary.LittleEndian.PutUint64(lengthBytes[:], uint64(length))
	if _, err := hmacHash.Write(lengthBytes[:]); err != nil {
		return nil, err
	}
	if _, err := io.Copy(hmacHash, subReader); err != nil {
		return nil, err
	}

	if module.HasROData() {
		length, subReader, err := getSubSectionReader(module.file, module.rodataStart, module.rodataEnd)
		if err != nil {
			return nil, fmt.Errorf("failed to extract .rodata section: %w", err)
		}
		binary.LittleEndian.PutUint64(lengthBytes[:], uint64(length))
		if _, err := hmacHash.Write(lengthBytes[:]); err != nil {
			return nil, err
		}
		if _, err := io.Copy(hmacHash, subReader); err != nil {
			return nil, err
		}
	}

	return hmacHash.Sum(nil), nil
}

func extractModuleSections(module *FIPSModule) (textBinary, textHex, rodataBinary, rodataHex string, err error) {
	textBinary, err = dumpSubSectionBinaryToFile(module.file, fipsModuleTextSection, module.textStart, module.textEnd)
	if err != nil {
		return "", "", "", "", fmt.Errorf("failed to dump %s binary to file: %w", fipsModuleTextSection, err)
	}

	textHex, err = dumpSubSectionHexToFile(module.file, fipsModuleTextSection, module.textStart, module.textEnd)
	if err != nil {
		return "", "", "", "", fmt.Errorf("failed to dump %s hex to file: %w", fipsModuleTextSection, err)
	}

	if module.HasROData() {
		rodataBinary, err = dumpSubSectionBinaryToFile(module.file, fipsModuleReadOnlySection, module.rodataStart, module.rodataEnd)
		if err != nil {
			return "", "", "", "", fmt.Errorf("failed to dump %s binary to file: %w", fipsModuleReadOnlySection, err)
		}

		rodataHex, err = dumpSubSectionHexToFile(module.file, fipsModuleReadOnlySection, module.rodataStart, module.rodataEnd)
		if err != nil {
			return "", "", "", "", fmt.Errorf("failed to dump %s hex to file: %w", fipsModuleReadOnlySection, err)
		}
	}

	return textBinary, textHex, rodataBinary, rodataHex, nil
}

func dumpSubSectionBinaryToFile(file *elf.File, name string, start, end *elf.Symbol) (string, error) {
	_, subReader, err := getSubSectionReader(file, start, end)
	if err != nil {
		return "", fmt.Errorf("failed to extract %s section: %w", name, err)
	}
	fileName, err := writeContentsToTempFile(subReader, name+".bin")
	if err != nil {
		return "", fmt.Errorf("failed to write %s binary to file: %w", name, err)
	}
	return fileName, nil
}

func dumpSubSectionHexToFile(file *elf.File, name string, start, end *elf.Symbol) (string, error) {
	_, subReader, err := getSubSectionReader(file, start, end)
	if err != nil {
		return "", fmt.Errorf("failed to extract %s section: %w", name, err)
	}

	tempFile, err := getTempFile(name + ".hex")
	if err != nil {
		return "", err
	}

	hexDumper := hex.Dumper(tempFile)
	if _, err := io.Copy(hexDumper, subReader); err != nil {
		return "", fmt.Errorf("failed to write %s hex to file: %w", name, err)
	}
	if err := hexDumper.Close(); err != nil {
		return "", fmt.Errorf("failed to close hex dumper: %w", err)
	}

	if err := tempFile.Close(); err != nil {
		return "", fmt.Errorf("failed to close file: %w", err)
	}

	return tempFile.Name(), nil
}

func getSubSectionReader(file *elf.File, start, end *elf.Symbol) (int64, io.Reader, error) {
	symbolSection := file.Sections[start.Section]
	if symbolSection == nil {
		return 0, nil, fmt.Errorf("section not found")
	}

	if start.Section != end.Section {
		return 0, nil, fmt.Errorf("start and end symbols are in different sections")
	}

	startOffset := start.Value - symbolSection.Addr
	if start.Size != 0 {
		return 0, nil, fmt.Errorf("start symbol does not match the expected size, expect 0 got %v", start.Size)
	}

	endOffset := end.Value - symbolSection.Addr
	if end.Size != 0 {
		return 0, nil, fmt.Errorf("end symbol does not match the expected size, expect 0 got %v", end.Size)
	}

	sectionReader := symbolSection.Open()
	if _, err := sectionReader.Seek(int64(startOffset), 0); err != nil {
		return 0, nil, err
	}

	length := int64(endOffset - startOffset)

	return length, io.LimitReader(sectionReader, length), nil
}

func writeContentsToTempFile(reader io.Reader, filename string) (string, error) {
	file, err := getTempFile(filename)
	if err != nil {
		return "", err
	}
	if _, err := io.Copy(file, reader); err != nil {
		return "", fmt.Errorf("failed to write file: %w", err)
	}
	if err := file.Close(); err != nil {
		return "", fmt.Errorf("failed to close file: %w", err)
	}
	return file.Name(), nil
}

func getTempFile(filename string) (*os.File, error) {
	file, err := os.CreateTemp("", fmt.Sprintf("*.%s", filename))
	if err != nil {
		return nil, fmt.Errorf("failed to create file: %w", err)
	}
	return file, nil
}