How ASN.1 Encodes Lengths
In ASN.1 (Abstract Syntax Notation One), lengths are encoded as part of the TLV (Type-Length-Value) structure, where each element consists of:
- Type: Identifies the data type of the element.
- Length: Specifies the number of bytes that make up the element’s content.
- Value: Contains the actual data or content.
The Length field in ASN.1 encoding is designed to allow both short and long forms to handle lengths of various sizes.
1. Short Form Length Encoding
If the length of the content is 127 bytes or less, the length is encoded in one byte:
- The most significant bit (MSB) of this byte is 0.
- The remaining 7 bits specify the length (from 0 to 127).
For example:
- A length of “10” would be represented by a single byte: “0x0A”.
- A length of “100” would be represented by a single byte: “0x64”.
2. Long Form Length Encoding
If the content is 128 bytes or more, the length is encoded in multiple bytes:
- The first byte has its MSB set to 1, indicating long-form encoding.
- The lower 7 bits of the first byte specify the number of additional bytes used to represent the length.
- The following bytes represent the length in big-endian format.
Examples of Long Form Encoding
- For a length of “128”:
- The first byte is “0x81” (MSB is 1, indicating long-form, and 1 additional byte follows).
- The next byte is “0x80”, giving a total length of “128”.
- Encoding: “0x81 0x80”.
- For a length of “300”:
- The first byte is “0x82” (MSB is 1, indicating long-form, and 2 additional bytes follow).
- The following two bytes are “0x01 0x2C”, representing “300” in hexadecimal.
- Encoding: “0x82 0x01 0x2C”.
Special Case: Indefinite Length Encoding
ASN.1 also supports an indefinite length encoding, typically used in constructed types (e.g., SEQUENCE or SET):
- The length byte is set to “0x80”, indicating that the length is undefined.
- The content ends with a special end-of-content marker (two consecutive zero bytes: “0x00 0x00”).
Indefinite length encoding is less common and primarily used when the exact length of the data is unknown in advance, such as in streaming.
Summary of Length Encoding in ASN.1
- Short Form: One byte if length is ≤ 127, with MSB set to 0.
- Long Form: Multiple bytes if length is ≥ 128, with MSB of the first byte set to 1, followed by additional bytes specifying the length.
- Indefinite Length: “0x80” for indefinite length, ending with “0x00 0x00” marker.
This flexible length encoding system in ASN.1 allows it to handle a wide range of content sizes efficiently while maintaining compatibility across systems.