- pkt = ciph.Seal(buf[:4], nonce, buf[4:4+n], []byte{sid})
+
+ incr(nonce[len(nonce)-3:])
+ copy(buf, nonce[len(nonce)-4:])
+ ciph, err = chacha20.NewUnauthenticatedCipher(keyOur, nonce)
+ if err != nil {
+ log.Fatal(err)
+ }
+ clear(macKey[:])
+ ciph.XORKeyStream(macKey[:], macKey[:])
+ ciph.SetCounter(1)
+ ciph.XORKeyStream(buf[4:4+n], buf[4:4+n])
+ mac = poly1305.New(&macKey)
+ if _, err = mac.Write(buf[4 : 4+n]); err != nil {
+ log.Fatal(err)
+ }
+ mac.Sum(tag[:0])
+ copy(buf[4+n:], tag[:TagLen])
+ pkt = buf[:4+n+TagLen]
+