S3 chunked uploads SigV4 hash chain diagram

When you upload an object to an S3-compatible bucket using standard Signature Version 4, you must compute the SHA-256 hash of the entire payload before sending the very first HTTP byte. That works cleanly for small files. But if you are streaming an 80 GB PostgreSQL dump directly from pg_dump, piping live video, or generating archives on the fly, buffering the whole stream into RAM or spilling it to local disk just to calculate a preliminary hash defeats the purpose of streaming.

AWS solved this by defining Content-Encoding: aws-chunked paired with the header x-amz-content-sha256: STREAMING-AWS4-HMAC-SHA256-PAYLOAD. Instead of hashing the full payload up front, you sign only the initial HTTP request headers to produce a seed signature. You then stream the data in successive chunks, where each chunk carries its own cryptographic signature over the wire. Crucially, these chunk signatures are chained together like an HMAC ratchet: each chunk's signature incorporates the signature of the previous chunk. Here is how that chain is calculated byte by byte, how it matches botocore, and what happens when chunks arrive out of order.

1. The request headers and the seed signature

A streaming chunked upload starts with four required headers that configure the signing protocol on the S3 endpoint:

  • x-amz-content-sha256: STREAMING-AWS4-HMAC-SHA256-PAYLOAD: Tells the storage gateway that the payload is not pre-hashed and will arrive in signed chunks.
  • Content-Encoding: aws-chunked: Informs HTTP decoders that the body contains chunk metadata headers and framing.
  • x-amz-decoded-content-length: The exact size in bytes of the unchunked, raw object payload (e.g. 66560).
  • Content-Length: The total wire length, which includes raw data bytes PLUS chunk size headers, signatures, and CRLF separators (e.g. 66824).

Before transmitting any body bytes, the client builds a standard SigV4 canonical request over the headers. In place of the usual payload hash, line 7 contains the literal ASCII string STREAMING-AWS4-HMAC-SHA256-PAYLOAD. Using AWS's standard test credentials (AKIAIOSFODNN7EXAMPLE with secret wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY) for a PUT to /examplebucket/chunkObject.txt on 2013-05-24, the canonical request is:

text
PUT
/examplebucket/chunkObject.txt

content-encoding:aws-chunked
content-length:66824
host:s3.amazonaws.com
x-amz-content-sha256:STREAMING-AWS4-HMAC-SHA256-PAYLOAD
x-amz-date:20130524T000000Z
x-amz-decoded-content-length:66560
x-amz-storage-class:REDUCED_REDUNDANCY

content-encoding;content-length;host;x-amz-content-sha256;x-amz-date;x-amz-decoded-content-length;x-amz-storage-class
STREAMING-AWS4-HMAC-SHA256-PAYLOAD

The SHA-256 hash of this canonical request is cee3fed04b70f867d036f722359b0b1f2f0e5dc0efadbc082b76c4c60e316455. Feeding this hash into the standard SigV4 string-to-sign produces the seed signature:

text
AWS4-HMAC-SHA256
20130524T000000Z
20130524/us-east-1/s3/aws4_request
cee3fed04b70f867d036f722359b0b1f2f0e5dc0efadbc082b76c4c60e316455

Computing HMAC-SHA256 with the derived kSigning key gives the seed signature: 4f232c4386841ef735655705268965c44a0e4690baa4adea153f7db9fa80a0a9. This value is sent in the initial HTTP Authorization header and serves as the cryptographic anchor for the entire chunk stream.

2. Wire framing and protocol overhead

Every chunk transmitted in aws-chunked format adheres to a strict framing structure. Each data chunk begins with the chunk size in hexadecimal, followed by ;chunk-signature=, the 64-character lowercase hexadecimal signature, and a \r\n delimiter. The raw chunk data follows, terminated by another \r\n. Once all data chunks are transmitted, a final 0-byte terminal chunk closes the stream:

http
<chunk-size-hex>;chunk-signature=<64-hex-signature>

<raw-chunk-bytes>

0;chunk-signature=<64-hex-signature>

Notice why Content-Length (66,824 bytes) is larger than x-amz-decoded-content-length (66,560 bytes). For a 65 KB file uploaded in two data chunks (64 KB and 1 KB) plus the zero-byte terminal chunk, the framing adds exactly 264 bytes of overhead:

SegmentRaw DataHeader BytesTrailing CRLFWire Bytes
Chunk 1 (64 KB)65,536 B88 B (10000;chunk-signature=...\r\n)2 B65,626 B
Chunk 2 (1 KB)1,024 B86 B (400;chunk-signature=...\r\n)2 B1,112 B
Chunk 3 (0 B terminal)0 B84 B (0;chunk-signature=...\r\n)2 B86 B
Total66,560 B258 B6 B66,824 B

The framing adds only 264 bytes of wire data for a 65 KB object—an overhead of just 0.39%. For large multi-megabyte streams, framing overhead drops to negligible fractions of a percent.

3. Walking the 3-chunk signature chain

Every chunk's signature is calculated from a standardized 6-line StringToSign. Unlike the initial request string-to-sign which starts with AWS4-HMAC-SHA256, chunk strings-to-sign start with AWS4-HMAC-SHA256-PAYLOAD:

text
AWS4-HMAC-SHA256-PAYLOAD
<DateTime>
<CredentialScope>
<PreviousSignature>
<HashedEmptyString>
<HashedCurrentChunkData>

Here is the exact progression across all three chunks:

Chunk 1: 65,536 bytes ('a' x 65,536)

For the first data chunk, <PreviousSignature> is the Seed Signature (4f232c43...). The SHA-256 hash of 65,536 ASCII 'a' characters is bf718b6f653bebc184e1479f1935b8da974d701b893afcf49e701f3e2f9f9c5a:

text
AWS4-HMAC-SHA256-PAYLOAD
20130524T000000Z
20130524/us-east-1/s3/aws4_request
4f232c4386841ef735655705268965c44a0e4690baa4adea153f7db9fa80a0a9
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
bf718b6f653bebc184e1479f1935b8da974d701b893afcf49e701f3e2f9f9c5a

Computing HMAC-SHA256 with kSigning yields Chunk 1 Signature: ad80c730a21e5b8d04586a2213dd63b9a0e99e0e2307b0ade35a65485a288648.

Chunk 2: 1,024 bytes ('a' x 1,024)

For the second chunk, <PreviousSignature> becomes the Chunk 1 Signature (ad80c730...). The SHA-256 hash of 1,024 'a's is 2edc986847e209b4016e141a6dc8716d3207350f416969382d431539bf292e4a:

text
AWS4-HMAC-SHA256-PAYLOAD
20130524T000000Z
20130524/us-east-1/s3/aws4_request
ad80c730a21e5b8d04586a2213dd63b9a0e99e0e2307b0ade35a65485a288648
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
2edc986847e209b4016e141a6dc8716d3207350f416969382d431539bf292e4a

Computing HMAC-SHA256 with kSigning yields Chunk 2 Signature: 0055627c9e194cb4542bae2aa5492e3c1575bbb81b612b7d234b86a503ef5497.

Chunk 3: 0 bytes (terminal completion)

The stream terminates with a 0-byte chunk. <PreviousSignature> is the Chunk 2 Signature (0055627c...). Because the chunk has zero data bytes, both line 5 and line 6 contain the empty-string SHA-256 hash (e3b0c442...):

text
AWS4-HMAC-SHA256-PAYLOAD
20130524T000000Z
20130524/us-east-1/s3/aws4_request
0055627c9e194cb4542bae2aa5492e3c1575bbb81b612b7d234b86a503ef5497
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

Computing HMAC-SHA256 with kSigning yields Chunk 3 Signature: b6c6ea8a5354eaf15b3cb7646744f4275b71ea724fed81ceb9323e279d449df9.

StepInput Previous SignatureCurrent Chunk HashComputed Signature
Seed (Headers)N/A (Canonical Request)cee3fed04b70f8...4f232c4386841e...
Chunk 1 (64 KB)4f232c4386841e... (Seed)bf718b6f653beb...ad80c730a21e5b...
Chunk 2 (1 KB)ad80c730a21e5b... (C1)2edc986847e209...0055627c9e194c...
Chunk 3 (0 B)0055627c9e194c... (C2)e3b0c44298fc1c... (empty)b6c6ea8a5354ea...

4. Failure modes: what happens when chunks are reordered

Suppose a network middlebox, HTTP/2 multiplexing bug, or proxy error delivers Chunk 2 before Chunk 1. How does the S3 server react?

The server maintains an internal cursor of the signature chain. When Chunk 2 arrives first over the connection, the server expects its <PreviousSignature> to equal the Seed Signature (4f232c43...). It reconstructs Chunk 2's String-to-Sign accordingly:

text
AWS4-HMAC-SHA256-PAYLOAD
20130524T000000Z
20130524/us-east-1/s3/aws4_request
4f232c4386841ef735655705268965c44a0e4690baa4adea153f7db9fa80a0a9
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
2edc986847e209b4016e141a6dc8716d3207350f416969382d431539bf292e4a

The server calculates HMAC-SHA256 over this string and obtains 4f9f4e93a9785ad19aaa4c6713fc20b87c8ea6da0e1aa2948b81e23256328f8f. It then compares this value to the signature sent in the wire header (0055627c9e194cb4542bae2aa5492e3c1575bbb81b612b7d234b86a503ef5497).

The signatures do not match. The server immediately closes the TCP socket and returns HTTP 403 SignatureDoesNotMatch. The upload fails before a single corrupted byte is written to storage.

Even if an attacker intentionally recalculated Chunk 2's signature against the seed signature to make it valid (4f9f4e93...), the stored file content would be completely scrambled (1 KB followed by 64 KB). The terminal chunk and object-level checksums would then fail. The hash chain guarantees both tamper-evidence and strict sequential ordering.

5. SDK realities: botocore, Go, and trailing checksums

If chunked signing is cryptographically elegant, why don't all SDKs use it for every streaming upload? The answer is CPU overhead and language runtimes:

  • Python botocore defaults to UNSIGNED-PAYLOAD: In Python's botocore/auth.py (lines 502 & 533), body signing is deliberately disabled for streaming inputs. Computing HMAC-SHA256 chunk-by-chunk in pure Python under the GIL creates substantial CPU bottlenecks on gigabit pipes. Botocore replaces the content hash with UNSIGNED-PAYLOAD over TLS.
  • Go and Rust AWS SDKs support aws-chunked natively: Compiled languages with SIMD-accelerated SHA-256 instructions compute chunk HMACs with minimal latency, making aws-chunked viable for high-throughput pipes.
  • Trailing Checksums (Modern S3 Standard): Modern AWS SDKs increasingly favor trailing checksums (STREAMING-UNSIGNED-PAYLOAD-TRAILER with x-amz-trailer: x-amz-checksum-crc32c). The client streams the body without per-chunk HMAC overhead, then sends an end-of-stream CRC32C or SHA-256 trailer. S3 verifies object integrity upon stream completion without requiring upfront buffering.
ModeRAM BufferingCPU CostIntegrity Guarantee
Standard SigV4Full payload bufferedOne full SHA-256 passFull payload signed in header
aws-chunked SigV4One chunk (e.g. 64 KB)Per-chunk SHA-256 + HMACCryptographic hash chain; aborts on corrupt chunk
UNSIGNED-PAYLOADZero (direct stream)None (TLS crypto only)TLS transport integrity only
Trailing CRC32CZero (direct stream)Hardware-accelerated CRC32CEnd-to-end checksum verified at EOF

6. Reproducing the chain in Python

You can verify every hash in this chain yourself with standard Python libraries. The script below computes the seed signature, builds the 3-chunk hash chain, asserts that all values match the AWS test vectors, and simulates the failure when Chunk 2 is received out of order:

python
import hmac
import hashlib

def sign(key: bytes, msg: str) -> bytes:
    return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()

def get_signing_key(secret: str, date: str, region: str, service: str) -> bytes:
    k_date = sign(("AWS4" + secret).encode("utf-8"), date)
    k_region = sign(k_date, region)
    k_service = sign(k_region, service)
    return sign(k_service, "aws4_request")

secret_key = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
k_signing = get_signing_key(secret_key, "20130524", "us-east-1", "s3")
date_time = "20130524T000000Z"
scope = "20130524/us-east-1/s3/aws4_request"
empty_hash = hashlib.sha256(b"").hexdigest()

# 1. Seed signature from canonical request
seed_sig = "4f232c4386841ef735655705268965c44a0e4690baa4adea153f7db9fa80a0a9"

def make_chunk_sig(prev_sig: str, chunk_data: bytes) -> tuple[str, str]:
    chunk_hash = hashlib.sha256(chunk_data).hexdigest()
    sts = f"AWS4-HMAC-SHA256-PAYLOAD\n{date_time}\n{scope}\n{prev_sig}\n{empty_hash}\n{chunk_hash}"
    sig = hmac.new(k_signing, sts.encode("utf-8"), hashlib.sha256).hexdigest()
    return chunk_hash, sig

# Chunk 1: 65,536 bytes of letter 'a'
h1, sig1 = make_chunk_sig(seed_sig, b"a" * 65536)
assert sig1 == "ad80c730a21e5b8d04586a2213dd63b9a0e99e0e2307b0ade35a65485a288648"

# Chunk 2: 1,024 bytes of letter 'a'
h2, sig2 = make_chunk_sig(sig1, b"a" * 1024)
assert sig2 == "0055627c9e194cb4542bae2aa5492e3c1575bbb81b612b7d234b86a503ef5497"

# Chunk 3: 0 bytes terminal chunk
h3, sig3 = make_chunk_sig(sig2, b"")
assert sig3 == "b6c6ea8a5354eaf15b3cb7646744f4275b71ea724fed81ceb9323e279d449df9"

# Reordering check: if Chunk 2 arrives first, the server validates it against seed_sig
_, reordered_c2_sig = make_chunk_sig(seed_sig, b"a" * 1024)
print(f"Reordered Chunk 2 signature: {reordered_c2_sig}")
print(f"Original Chunk 2 signature:  {sig2}")
assert reordered_c2_sig != sig2
print("Validation succeeded: Out-of-order chunk triggers SignatureDoesNotMatch!")