Right now, KID and CTR follow different encoding/decoding rules in the header, and their ordering is crossed. KID is encoded within the header byte if the value is less than 8, and a bit is set if it overflows. CTR is always length-encoded. (This made some sense when there was a need for a bit to signal signature, but that feature was removed long ago.) The order of LEN and KLEN in the header byte is the reverse of the KID and CTR fields that they describe.
We should rearrange the header octet so that it does the same thing for both KID and CTR, and in the same order:
+---+------+---+------+--------+--------+
| K | KLEN | C | CLEN | KID... | CTR... |
+---+------+---+------+--------+--------+
Right now, KID and CTR follow different encoding/decoding rules in the header, and their ordering is crossed. KID is encoded within the header byte if the value is less than 8, and a bit is set if it overflows. CTR is always length-encoded. (This made some sense when there was a need for a bit to signal signature, but that feature was removed long ago.) The order of LEN and KLEN in the header byte is the reverse of the KID and CTR fields that they describe.
We should rearrange the header octet so that it does the same thing for both KID and CTR, and in the same order: