Subject: [TLS] Length of a variable-length vector: Could it be an odd multiple?

From: Jong-Shian Wu <[email-masked]>
Date: Thu, 14 Jan 2016 04:44:30 +0800

I have a question about the even-vs-odd restrictions on the length of
a valid variable-length vector defined in TLS specification after
reading the section 4.3 of RFC 5246 [1] which states that: "The length
of an encoded vector must be an even multiple of the length of a
single element (for example, a 17-byte vector of uint16 would be
illegal)."

Does it also means that an 18-byte vector of uint16 would be illegal?
(18 is an odd multiple of 2, not an *even* multiple of 2)

To put it differently, given the following definitions for Foo and Bar:

   opaque Foo[2];
   Foo Bar<4..8>;

Is it correct to say that there does not exist any valid 6-byte value
of type Bar?  (6 is not an even multiple of 2)

[1]: https://tools.ietf.org/html/rfc5246#section-4.3

---

From: Benjamin Kaduk <[email-masked]>
Date: Wed, 13 Jan 2016 14:53:26 -0600

> single element (for example, a 17-byte vector of uint16 would be

It means "whole-number" as opposed to fractional, i.e., there should not
be unused "junk bytes" at the end.

-Ben

---

From: Jong-Shian Wu <[email-masked]>
Date: Thu, 14 Jan 2016 05:02:53 +0800

> be unused "junk bytes" at the end.

Ah, so that is not about even vs odd numbers at all.  Thank you very
much for the quick clarification!

---

From: "Hodges, Jeff" <[email-masked]>
Date: Wed, 20 Jan 2016 18:47:12 +0000

On 1/13/16, 12:53 PM, "Benjamin Kaduk" <[email-masked]> wrote:
>> reading the section 4.3 of RFC 5246 [1] which states that:

>>"The length of an encoded vector must be an even multiple of the length
>>of a single element (for example, a 17-byte vector of uint16 would be
>> illegal)."
>>
>

In case it's helpful, here's a suggested re-write of that quoted sentence
above..

  The length of an encoded variable-length vector must be an
  exact multiple of the length of a single element. For example,
  an encoded 17-byte vector of uint16 would be illegal, and an
  encoded variable-length vector of four 32 byte elements,
  having a ceiling of 2^16-1, will be 130 bytes long overall
  (2 byte length field followed by 128 bytes of data).

..assuming I'm correctly understanding variable-length vectors :)

HTH,

=JeffH

---

From: Benjamin Kaduk <[email-masked]>
Date: Wed, 20 Jan 2016 13:01:56 -0600

>   (2 byte length field followed by 128 bytes of data).

Wouldn't the ceiling more properly be 2^16-4 in that case?

-Ben

---

From: "Hodges, Jeff" <[email-masked]>
Date: Wed, 20 Jan 2016 19:20:51 +0000

>Wouldn't the ceiling more properly be 2^16-4 in that case?

hm, I'm not sure -- what would be the rationale?  The exact multiple
criteria?  but 2^16 / 32 = 2048

i do have further questions regarding variable-length vectors, and how
they are specified, that subsequent discussion will hopefully tease out.

thanks, 

=JeffH

---

From: Ilari Liusvaara <[email-masked]>
Date: Fri, 22 Jan 2016 18:22:42 +0200

> ..assuming I'm correctly understanding variable-length vectors :)

The length of length is inferred from the upper limit:
- 1-255 bytes => 1 Byte
- 256-65535 bytes => 2 Bytes
- 65536-16777215 bytes => 3 bytes
- 16777216-2^32-1 bytes => 4 bytes (not used anywhere)
... And so on.

So yes, 4x32 byte elements in vector with upper limit of 2^16-1 (or
2^16-32 rounded) is 130 bytes total: 2 byte length and 4*32=128 byte
value.

But I think that all lengths above 255 are either specified to be one
byte shy of power of 255 or rounding thereof to integral multiple of
elements (the only exception I offhand remember is session IDs in
TLS 1.0-1.2, which have maximum length of 32).

Then there are vectors with variable length elements. Those can be
created by putting in a vector or discriminant/case in the element
type (the latter can easily lead to interop problems later).

-Ilari