-
Notifications
You must be signed in to change notification settings - Fork 995
Add fips-hash-offline.sh #10749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
holtrop-wolfssl
wants to merge
1
commit into
wolfSSL:master
Choose a base branch
from
holtrop-wolfssl:add-fips-hash-offline-script
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+181
−0
Open
Add fips-hash-offline.sh #10749
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| #!/bin/bash | ||
|
|
||
| # fips-hash-offline.sh | ||
| # | ||
| # Copyright (C) 2006-2026 wolfSSL Inc. | ||
| # | ||
| # This file is part of wolfSSL. | ||
| # | ||
| # wolfSSL is free software; you can redistribute it and/or modify | ||
| # it under the terms of the GNU General Public License as published by | ||
| # the Free Software Foundation; either version 3 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # wolfSSL is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU General Public License | ||
| # along with this program; if not, write to the Free Software | ||
| # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA | ||
|
|
||
| # This script computes the wolfCrypt FIPS in-core integrity hash at compile | ||
| # time directly from an already linked binary, and patches the result into the | ||
| # verifyCore[] array inside that binary. | ||
| # | ||
| # This reproduces, at build time and against the final ELF image, exactly what | ||
| # DoInCoreCheck() in wolfcrypt/src/fips_test.c computes at run time: | ||
| # | ||
| # HMAC-SHA256( coreKey, | ||
| # .text bytes in [wolfCrypt_FIPS_first, wolfCrypt_FIPS_last) | ||
| # || | ||
| # .rodata bytes in [wolfCrypt_FIPS_ro_start, wolfCrypt_FIPS_ro_end) | ||
| # with the verifyCore[] bytes skipped ) | ||
| # | ||
| # Because verifyCore[] is excluded from the digest, overwriting it with the | ||
| # computed value does not change the digest, so a single pass over the linked | ||
| # binary is sufficient -- no rebuild and no scraping of the module's reported | ||
| # hash is required. | ||
| # | ||
| # Assumptions (match the optest user-space static FIPS build): | ||
| # - single fixed-address (ET_EXEC) image, not stripped | ||
| # - measured .text/.rodata bracketed by the wolfCrypt_FIPS_first/last and | ||
| # wolfCrypt_FIPS_ro_start/ro_end symbols | ||
| # - no text-segment canonicalizer and no relocation-table indirection in | ||
| # effect (i.e. the plain #else path of DoInCoreCheck()) | ||
| # | ||
| # Usage: fips-hash-offline.sh [path-to-binary] | ||
| # default binary: wolfcrypt/test/testwolfcrypt | ||
|
|
||
| set -u | ||
|
|
||
| BIN="${1:-wolfcrypt/test/testwolfcrypt}" | ||
|
|
||
| die() { echo "fips-hash-offline: error: $*" >&2; exit 1; } | ||
|
|
||
| [ -f "$BIN" ] || die "binary not found: $BIN" | ||
| command -v awk >/dev/null 2>&1 || die "awk not found" | ||
| command -v dd >/dev/null 2>&1 || die "dd not found" | ||
| command -v readelf >/dev/null 2>&1 || die "readelf not found" | ||
| command -v openssl >/dev/null 2>&1 || die "openssl not found" | ||
| command -v xxd >/dev/null 2>&1 || die "xxd not found" | ||
|
|
||
| # Symbol lookup from the ELF symbol table. | ||
| # readelf -s columns: Num: Value Size Type Bind Vis Ndx Name | ||
| # -> Value ($2) is hexadecimal, Size ($3) is decimal, Ndx ($7), Name ($8). | ||
| sym_val() { readelf -sW "$BIN" | awk -v n="$1" '$8==n && $7!="UND"{print $2; exit}'; } | ||
| sym_size() { readelf -sW "$BIN" | awk -v n="$1" '$8==n && $7!="UND"{print $3; exit}'; } | ||
|
|
||
| # Map a virtual address to a file offset using the containing PROGBITS section. | ||
| # This avoids assuming any particular load base or section padding. | ||
| vaddr_to_off() { | ||
| local v="$1" | ||
| local name type addr off size a o s | ||
| while read -r name type addr off size; do | ||
| [ "$type" = "PROGBITS" ] || continue | ||
| a=$((0x$addr)); o=$((0x$off)); s=$((0x$size)) | ||
| if [ "$v" -ge "$a" ] && [ "$v" -lt $((a + s)) ]; then | ||
| echo $((o + v - a)) | ||
| return 0 | ||
| fi | ||
| done < <(readelf -SW "$BIN" | sed 's/\[[ 0-9]*\]//' | awk '/PROGBITS/{print $1, $2, $3, $4, $5}') | ||
| return 1 | ||
| } | ||
|
|
||
| # Extract byte_count bytes starting at file_offset (both in bytes) to stdout. | ||
| # Uses GNU dd byte-granular skip/count with a large block size for speed. | ||
| extract() { | ||
| dd if="$BIN" bs=1M iflag=skip_bytes,count_bytes skip="$1" count="$2" 2>/dev/null | ||
| } | ||
|
|
||
| # --- gather the FIPS boundary and verifyCore/coreKey symbols --- | ||
| FIRST_H=$(sym_val wolfCrypt_FIPS_first) | ||
| LAST_H=$(sym_val wolfCrypt_FIPS_last) | ||
| ROSTART_H=$(sym_val wolfCrypt_FIPS_ro_start) | ||
| ROEND_H=$(sym_val wolfCrypt_FIPS_ro_end) | ||
| VC_H=$(sym_val verifyCore) | ||
| VCSZ=$(sym_size verifyCore) | ||
| KEY_H=$(sym_val coreKey) | ||
| KEYSZ=$(sym_size coreKey) | ||
|
|
||
| [ -n "$FIRST_H" ] && [ -n "$LAST_H" ] && [ -n "$ROSTART_H" ] && [ -n "$ROEND_H" ] \ | ||
| && [ -n "$VC_H" ] && [ -n "$VCSZ" ] && [ -n "$KEY_H" ] && [ -n "$KEYSZ" ] \ | ||
| || die "missing FIPS boundary symbols (is $BIN a non-stripped static FIPS build?)" | ||
|
|
||
| first=$((0x$FIRST_H)) | ||
| last=$((0x$LAST_H)) | ||
| rostart=$((0x$ROSTART_H)) | ||
| roend=$((0x$ROEND_H)) | ||
| vc=$((0x$VC_H)) | ||
| keyaddr=$((0x$KEY_H)) | ||
|
|
||
| [ "$last" -gt "$first" ] || die "wolfCrypt_FIPS_last <= wolfCrypt_FIPS_first" | ||
| [ "$roend" -gt "$rostart" ] || die "wolfCrypt_FIPS_ro_end <= wolfCrypt_FIPS_ro_start" | ||
|
|
||
| # Select the digest from the size of verifyCore[] (= digest_bytes*2 + 1). | ||
| digest_bytes=$(( (VCSZ - 1) / 2 )) | ||
| case "$digest_bytes" in | ||
| 32) ALG=sha256 ;; | ||
| 48) ALG=sha384 ;; | ||
| *) die "unexpected verifyCore size ($VCSZ); cannot determine digest" ;; | ||
| esac | ||
|
|
||
| # Read the HMAC key (coreKey) as ASCII hex straight out of the binary. | ||
| keyoff=$(vaddr_to_off "$keyaddr") || die "cannot map coreKey address to file offset" | ||
| KEYHEX=$(extract "$keyoff" $((KEYSZ - 1))) | ||
| [ "${#KEYHEX}" -eq $((digest_bytes * 2)) ] || die "coreKey length mismatch in binary" | ||
|
|
||
| # File offsets for the measured regions. | ||
| codeoff=$(vaddr_to_off "$first") || die "cannot map wolfCrypt_FIPS_first" | ||
| roff=$(vaddr_to_off "$rostart") || die "cannot map wolfCrypt_FIPS_ro_start" | ||
|
|
||
| vc_in_ro=0 | ||
| if [ "$vc" -ge "$rostart" ] && [ "$vc" -lt "$roend" ]; then | ||
| vc_in_ro=1 | ||
| aoff=$(vaddr_to_off $((vc + VCSZ))) || die "cannot map verifyCore tail" | ||
| fi | ||
|
|
||
| # Compute the digest, streaming the measured bytes in the same order and with | ||
| # the same verifyCore exclusion as DoInCoreCheck(). | ||
| NEWHASH=$( | ||
| { | ||
| extract "$codeoff" $((last - first)) | ||
| if [ "$vc_in_ro" -eq 1 ]; then | ||
| extract "$roff" $((vc - rostart)) | ||
| extract "$aoff" $((roend - vc - VCSZ)) | ||
| else | ||
| extract "$roff" $((roend - rostart)) | ||
| fi | ||
|
holtrop-wolfssl marked this conversation as resolved.
|
||
| } | openssl dgst -"$ALG" -mac HMAC -macopt hexkey:"$KEYHEX" -binary \ | ||
| | xxd -p -c 1000 | tr -d '\n' | tr 'a-f' 'A-F' | ||
| ) | ||
| [ "${#NEWHASH}" -eq $((digest_bytes * 2)) ] || die "hash computation failed" | ||
|
|
||
| # Overwrite the first digest_bytes*2 ASCII characters of verifyCore[] in place. | ||
| # The trailing NUL terminator (byte digest_bytes*2) is left untouched. | ||
| vcoff=$(vaddr_to_off "$vc") || die "cannot map verifyCore address to file offset" | ||
| printf '%s' "$NEWHASH" | dd of="$BIN" bs=1M oflag=seek_bytes conv=notrunc seek="$vcoff" 2>/dev/null \ | ||
| || die "failed to write verifyCore" | ||
|
|
||
| # Confirm the patch landed. | ||
| CHECK=$(extract "$vcoff" $((digest_bytes * 2))) | ||
| [ "$CHECK" = "$NEWHASH" ] || die "verifyCore patch verification failed" | ||
|
|
||
| ALG_UC=$(echo "$ALG" | tr 'a-z' 'A-Z') | ||
| echo "fips-hash-offline: patched $BIN" | ||
| echo " algorithm : HMAC-$ALG_UC" | ||
| echo " code range : [0x$FIRST_H, 0x$LAST_H) ($((last - first)) bytes)" | ||
| echo " ro range : [0x$ROSTART_H, 0x$ROEND_H) (verifyCore $([ "$vc_in_ro" -eq 1 ] && echo excluded || echo 'not in range'))" | ||
| echo " hash : $NEWHASH" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.