Byte swapping
Eric S. Raymond
esr at thyrsus.com
Thu Jan 31 15:12:21 UTC 2019
Hal Murray via devel <devel at ntpsec.org>:
> The Internet is big endian. Intel is little endian. When I say byte swap
> below, I'm assuming they turn into no-ops on a big endian system.
>
> We have to byte swap integers and floats. We don't swap arrays of bytes --
> text or binary blobs like cookies.
>
> On transmit, we have to do the byte swapping before the
> authentication/encryption.
> On receive, we have to do the authentication/decryption before the byte
> swapping.
This is why one of my items under keep-you-guys-supplied-with tools is
to write a pair of functions that marshel/unmarshal between an
extension-fields ADT and the wire representation. If *you* guys have
to worry about when to do byte-swapping, I'm not doing my part of the
job right. (Well, either that or Ian fscked up the unit tests.
Neither is very likely.)
More generally, my doctrine for avoiding SNAFUs in situations like this
is to be careful about layering and aware of what your invariants are at
every level. A few days ago I was insufficiently rigorous about this and
messed up in a minoe way. It's a lesson that tends to need an occasional
refresh.
> The RefID is the only ugly case I know about. If you define it as a string,
> we don't swap it near the wire but we have to swap IP Addresses (integers) as
> we store into that slot or load from it. If you define it as an integer, we
> swap it near the wire, you can load/store IP Addresses directly, but you have
> to swap text when you load/store them.
>
> Crypto gets interesting. I think what's going on is that the implementation
> has to understand byte swapping. If the RFC for the algorithm is described in
> bytes, the obvious implementation just works. If the algorithm is described
> in words but the API uses bytes, then the implementation has to think in byte
> swapped terms. For example, if the algorithm is xor data words with
> 0x11223344, the actual implementation would xor with byte-swap(0x11223344).
This matches my understanding.
> I think a cleaned up version of the above should be captured someplace --
> after people check it.
Agreed.
--
<a href="http://www.catb.org/~esr/">Eric S. Raymond</a>
My work is funded by the Internet Civil Engineering Institute: https://icei.org
Please visit their site and donate: the civilization you save might be your own.
More information about the devel
mailing list