From 116c4cf07862ff127054fca98b1fdac9451fb3a0 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Tue, 29 Jun 2021 13:39:27 +0300 Subject: [PATCH] Counter increment optimization --- src/pkt.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/pkt.go b/src/pkt.go index c2715e4..2049b63 100644 --- a/src/pkt.go +++ b/src/pkt.go @@ -128,6 +128,16 @@ func NewPkt(typ PktType, nice uint8, path []byte) (*Pkt, error) { return &pkt, nil } +func ctrIncr(b []byte) { + for i := len(b) - 1; i >= 0; i-- { + b[i]++ + if b[i] != 0 { + return + } + } + panic("counter overflow") +} + func aeadProcess( aead cipher.AEAD, nonce []byte, @@ -135,7 +145,6 @@ func aeadProcess( r io.Reader, w io.Writer, ) (int, error) { - var blkCtr uint64 ciphCtr := nonce[len(nonce)-8:] buf := make([]byte, EncBlkSize+aead.Overhead()) var toRead []byte @@ -159,8 +168,7 @@ func aeadProcess( } } readBytes += n - blkCtr++ - binary.BigEndian.PutUint64(ciphCtr, blkCtr) + ctrIncr(ciphCtr) if doEncrypt { toWrite = aead.Seal(buf[:0], nonce, buf[:n], nil) } else { -- 2.44.0