Definition
Serialization is the conversion of structured values, such as the fields of a record, into a sequence of bytes according to a fixed set of rules called a format, so that the values can be stored or sent and rebuilt by any program that follows the same format. The format states the order of the fields, the width of each, how each value is written, including the byte order §4.21 of numbers longer than one byte, and how the end of each field is found. The reverse step is deserialization §4.29.
The idea
Inside one program, a record is a pattern of bytes in memory whose layout the compiler chose. It contains padding between fields, numbers in the processor’s byte order, and pointers, which are memory addresses that mean nothing in another process. Another machine, another compiler or the next version of the same program may lay out the same record differently. Copying the memory image to a file or a network therefore ties the data to one compiler and one processor, and it carries stray padding bytes along. Serialization removes that tie by defining the bytes themselves: the format, not any machine’s memory, says what each byte means.
A format works like a paper form with numbered boxes of fixed sizes. Anyone who knows the form can fill it in or read it, whatever their handwriting. A good form also leaves no doubt where one box ends and the next begins.
That last property matters most for security. A message authentication code §5.7 (MAC), a short value computed from the message bytes with a secret key, protects bytes, not meanings. If two different messages can produce the same bytes, a valid MAC cannot say which of the two was meant. A format used with authentication §1.6 must therefore be unambiguous: one byte sequence must be the encoding of at most one message.
How it works
A format decides the following about its fields.
The position of each field in the order of fields.
Each field’s width: a fixed number of bytes, or a variable width with a stated maximum.
Each field’s representation. For an integer this means signed or unsigned and its endianness, the order of its bytes; big-endian order, the usual one in network formats, writes the most significant byte first.
How a variable-length field ends: by a length field written in front of it (length-prefix framing §4.30), by a type-length-value §4.31 layout, which writes a type code and a length before each field, or by a terminating byte that must then be escaped wherever it occurs inside the data.
Which fields, such as a version or a type code, decide how the rest of the message is read.
An encoder, the code that serializes, works in three steps.
Check that each value fits its field, and refuse a value that does not; never cut it silently to the field’s width.
Write the fields in format order into an output buffer. Take the bytes of a number with shifts and masks, such as
(v >> 8) & 0xFFfor the second-lowest byte ofv. Shifts act on the value, not on its layout in memory, so the same code produces the same bytes on every processor.Report the exact number of bytes written.
A pointer is never written as an address. The encoder writes the data the pointer refers to, or an index into a table that both sides share.
Formats come in two broad kinds. A binary format writes numbers as bytes: the fixed layout of the worked example below, ASN.1 (Abstract Syntax Notation One) values written with DER (Distinguished Encoding Rules), and CBOR (Concise Binary Object Representation) are examples. A text format, such as JSON §12.26 (JavaScript Object Notation), writes numbers as characters; it must still fix the character encoding, the number syntax and the treatment of repeated keys.
Worked example
Let a format define a message of exactly 5 bytes: byte 0 is a tag, an unsigned 8-bit number, and bytes 1 to 4 are a count, an unsigned 32-bit number in big-endian order. Encode tag 1 and count 258. The tag becomes the byte 01. For the count, , so in hexadecimal it is 0x00000102. The encoder computes the four bytes from the most significant down: (258 >> 24) & 0xFF is 0, (258 >> 16) & 0xFF is 0, (258 >> 8) & 0xFF is 1 and 258 & 0xFF is 2. The message is 01 00 00 01 02 on every processor. Copying the memory of the C structure struct { uint8_t tag; uint32_t count; } instead gives 8 bytes on a common little-endian machine, 01 ?? ?? ?? 02 01 00 00, where ?? marks a padding byte of unspecified value.
Now an ambiguous format. A second format writes a user number and an amount as decimal digits and simply joins them. The pair (user 1, amount 23) becomes "1" followed by "23", which is "123". The pair (user 12, amount 3) becomes "12" followed by "3", which is also "123". The bytes no longer say which pair was meant. A server that computes a MAC over "123" for user 12’s request has produced a tag that is equally valid for user 1 asking for 23, and a receiver that splits the digits after the first one acts on that second request. The MAC is valid; the format lost the boundary.
Two repairs make this format unambiguous. With fixed widths of exactly 2 digits per field, for values 0 to 99, (1, 23) becomes "0123" and (12, 3) becomes "1203". With a length and a colon before each field, (1, 23) becomes "1:1" followed by "2:23", that is "1:12:23", and (12, 3) becomes "2:121:3". A reader of "1:12:23" reads the length 1, takes the 1 character "1", then reads the length 2 and takes "23": only one split is possible.
Stated precisely
Let be the set of messages a format admits, let be the encoder, which maps a message to a byte string, and let be the parser, which maps a byte string to a message or to “reject”. A format for authenticated messages should meet four conditions.
Round trip: for every in .
Injectivity: implies , so different messages never share an encoding. Round trip implies injectivity, because gives .
Canonical parsing: if , then . The parser accepts only the one encoding of each message, so a byte string and the message it carries determine each other.
Prefix-free encoding §15.38, needed whenever encodings are joined end to end: no is a proper beginning of a different , so a reader can find where each encoding ends without looking past it.
Injectivity of each field does not carry over to their concatenation. Decimal digits encode each single number injectively, yet the joined pair in the example is not injective. The joined encoding is injective when every field encoding except the last is both injective and prefix-free, as fixed widths and length prefixes are. Then exactly one encoding of the first field can begin the byte string, which fixes the first field; the same argument then applies to the rest of the string.
A round-trip test alone does not show that the bytes match the format. An encoder and a parser that share the same mistake, such as the wrong byte order, pass every round trip. Test vectors, byte strings written out in the format’s specification together with the values they encode, catch that mistake.
Limits and common mistakes
A memory image is not a format. Copying a structure’s memory carries padding, the processor’s byte order and the compiler’s type sizes, and it can leak whatever old data the padding held, as the entry on struct padding and packing §4.27 shows.
Many formats allow several encodings of one message. In JSON, object keys may come in any order, spaces may be added, and the number one may be written 1, 1.0 or 1e0. The JSON standard, RFC 8259, says only that keys should be unique, and it records that implementations disagree about repeated keys [29]. Serializing the parsed values again therefore need not reproduce the received bytes. Either verify a MAC over the bytes as received, or define a canonical encoding, one fixed byte string per message, such as DER for ASN.1, deterministically encoded CBOR or canonical JSON §12.28, and reject every other form.
Joining fields without separators breaks authentication in practice. The first version of Amazon’s scheme for signing requests to its web services computed an HMAC §5.8 (hash-based message authentication code) over the request’s parameter names and values joined without separators, so two different requests could share one valid signature. The repair, as in the worked example, was an encoding in which every boundary is visible.
Serialization fixes meaning, not permission. An unambiguous encoding can still describe a request that is too large, one the sender may not make, or an old message sent again in a replay attack §1.26. Those checks belong to deserialization and to the protocol.
Questions answered
Is the authenticated byte sequence exactly the one the receiver parses?
The authenticated byte sequence is exactly the parsed one only when the design makes it so; nothing makes the two sequences the same automatically. In the safe design the receiver verifies the message authentication code (MAC), the keyed tag that protects the message, over the exact bytes it received, keeps those bytes where nothing can change them, and parses those same bytes with a parser that accepts only one reading of them. The authenticated sequence and the parsed sequence are then identical, and their meaning is fixed. The two come apart, and an attacker gains room, in five situations.
A field that steers parsing, such as a length, a version or a type code, lies outside the authenticated range.
The format is ambiguous, so the tag fixes the bytes but not their meaning, as when the joined digits
"123"can mean the pair (1, 23) or the pair (12, 3).Two components parse the same bytes differently; for example, one takes the first of two repeated fields and the other takes the last.
The bytes are read again after verification from a buffer that another part of the system changed in between, a time-of-check to time-of-use §9.26 (TOCTOU) error.
The verifier checks a re-encoding of the parsed values while the receiver acts on the original bytes, or the reverse.
A sound alternative is to authenticate a canonical encoding, one fixed byte string per message, and to have the parser reject every other form.
Related entries: Authentication coverage (§5.41), Deserialization (§4.29), Endianness (§4.21), Struct padding and packing (§4.27), Prefix-free encoding (§15.38). Further reading: [29].