]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
device: simplify copying counter to nonce
authorJosh Bleecher Snyder <josh@tailscale.com>
Tue, 15 Dec 2020 02:30:13 +0000 (18:30 -0800)
committerJason A. Donenfeld <Jason@zx2c4.com>
Tue, 15 Dec 2020 17:11:55 +0000 (18:11 +0100)
Since we already have it packed into a uint64
in a known byte order, write it back out again
the same byte order instead of copying byte by byte.

This should also generate more efficient code,
because the compiler can do a single uint64 write,
instead of eight bounds checks and eight byte writes.

Due to a missed optimization, it actually generates a mishmash
of smaller writes: 1 byte, 4 bytes, 2 bytes, 1 byte.
This is https://golang.org/issue/41663.
The code is still better than before, and will get better yet
once that compiler bug gets fixed.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
device/receive.go

index 839b735f60ff7f7fba6c8380a5caefe959190fef..852bd891e229a4fd13e5875f3504b7595d7b6b38 100644 (file)
@@ -268,22 +268,12 @@ func (device *Device) RoutineDecryption() {
                        counter := elem.packet[MessageTransportOffsetCounter:MessageTransportOffsetContent]
                        content := elem.packet[MessageTransportOffsetContent:]
 
-                       // expand nonce
-
-                       nonce[0x4] = counter[0x0]
-                       nonce[0x5] = counter[0x1]
-                       nonce[0x6] = counter[0x2]
-                       nonce[0x7] = counter[0x3]
-
-                       nonce[0x8] = counter[0x4]
-                       nonce[0x9] = counter[0x5]
-                       nonce[0xa] = counter[0x6]
-                       nonce[0xb] = counter[0x7]
-
                        // decrypt and release to consumer
 
                        var err error
                        elem.counter = binary.LittleEndian.Uint64(counter)
+                       // copy counter to nonce
+                       binary.LittleEndian.PutUint64(nonce[0x4:0xc], elem.counter)
                        elem.packet, err = elem.keypair.receive.Open(
                                content[:0],
                                nonce[:],