gogost.go | 2 +- internal/gost34112012/hash.go | 81 +++++++++++++++++++++++++++-------------------------- news.texi | 4 ++++ diff --git a/gogost.go b/gogost.go index 6c12d834d9c7b4f83b6fa400e5ff6dfd423394fc2413623590fe28a97a90161c..4a87144b0cfc78e480201f1f6e14405e0b212ab5d776a7714aebb56517c8c90f 100644 --- a/gogost.go +++ b/gogost.go @@ -1,4 +1,4 @@ // Pure Go GOST cryptographic functions library. package gogost -const Version = "5.3.0" +const Version = "5.4.0" diff --git a/internal/gost34112012/hash.go b/internal/gost34112012/hash.go index 93bb454a2f34564cde63e80ba84bf38f4debd37070a5a7516ee1f28af2756f41..576ed5802892828332660f65918b4a071bf0f93f0b9d8fbcb8c56ac0bbd23391 100644 --- a/internal/gost34112012/hash.go +++ b/internal/gost34112012/hash.go @@ -288,16 +288,18 @@ } } type Hash struct { - size int - buf []byte - n uint64 - hsh []byte - chk []byte - tmp []byte - psBuf []byte - eBuf []byte - gBuf []byte - addBuf []byte + size int + buf []byte + n uint64 + hsh []byte + chk []byte + tmp []byte + psBuf []byte + eMsgBuf []byte + eKBuf []byte + eXorBuf []byte + gBuf []byte + addBuf []byte } // Create new hash object with specified size digest size. @@ -306,14 +308,16 @@ if size != 32 && size != 64 { panic("size must be either 32 or 64") } h := Hash{ - size: size, - hsh: make([]byte, BlockSize), - chk: make([]byte, BlockSize), - tmp: make([]byte, BlockSize), - psBuf: make([]byte, BlockSize), - eBuf: make([]byte, BlockSize), - gBuf: make([]byte, BlockSize), - addBuf: make([]byte, BlockSize), + size: size, + hsh: make([]byte, BlockSize), + chk: make([]byte, BlockSize), + tmp: make([]byte, BlockSize), + psBuf: make([]byte, BlockSize), + eMsgBuf: make([]byte, BlockSize), + eKBuf: make([]byte, BlockSize), + eXorBuf: make([]byte, BlockSize), + gBuf: make([]byte, BlockSize), + addBuf: make([]byte, BlockSize), } h.Reset() return &h @@ -344,8 +348,8 @@ func (h *Hash) Write(data []byte) (int, error) { h.buf = append(h.buf, data...) for len(h.buf) >= BlockSize { copy(h.tmp, h.buf[:BlockSize]) - h.hsh = h.g(h.n, h.hsh, h.tmp) - h.chk = h.add512bit(h.chk, h.tmp) + copy(h.hsh, h.g(h.n, h.hsh, h.tmp)) + copy(h.chk, h.add512bit(h.chk, h.tmp)) h.n += BlockSize * 8 h.buf = h.buf[BlockSize:] } @@ -354,13 +358,14 @@ } func (h *Hash) Sum(in []byte) []byte { buf := make([]byte, BlockSize) + hsh := make([]byte, BlockSize) copy(h.tmp, buf) copy(buf, h.buf) buf[len(h.buf)] = 1 - hsh := h.g(h.n, h.hsh, buf) + copy(hsh, h.g(h.n, h.hsh, buf)) binary.LittleEndian.PutUint64(h.tmp, h.n+uint64(len(h.buf))*8) - hsh = h.g(0, hsh, h.tmp) - hsh = h.g(0, hsh, h.add512bit(h.chk, buf)) + copy(hsh, h.g(0, hsh, h.tmp)) + copy(hsh, h.g(0, hsh, h.add512bit(h.chk, buf))) if h.size == 32 { return append(in, hsh[BlockSize/2:]...) } @@ -377,27 +382,25 @@ return h.addBuf } func (h *Hash) g(n uint64, hsh, data []byte) []byte { - r := make([]byte, BlockSize) - copy(r, hsh) - r[0] ^= byte((n >> 0) & 0xFF) - r[1] ^= byte((n >> 8) & 0xFF) - r[2] ^= byte((n >> 16) & 0xFF) - r[3] ^= byte((n >> 24) & 0xFF) - r[4] ^= byte((n >> 32) & 0xFF) - r[5] ^= byte((n >> 40) & 0xFF) - r[6] ^= byte((n >> 48) & 0xFF) - r[7] ^= byte((n >> 56) & 0xFF) - return blockXor(h.gBuf, blockXor(h.gBuf, h.e(l(r, h.ps(r)), data), hsh), data) + out := h.gBuf + copy(out, hsh) + out[0] ^= byte((n >> 0) & 0xFF) + out[1] ^= byte((n >> 8) & 0xFF) + out[2] ^= byte((n >> 16) & 0xFF) + out[3] ^= byte((n >> 24) & 0xFF) + out[4] ^= byte((n >> 32) & 0xFF) + out[5] ^= byte((n >> 40) & 0xFF) + out[6] ^= byte((n >> 48) & 0xFF) + out[7] ^= byte((n >> 56) & 0xFF) + return blockXor(out, blockXor(out, h.e(l(out, h.ps(out)), data), hsh), data) } func (h *Hash) e(k, msg []byte) []byte { - msgBuf := make([]byte, BlockSize) - kBuf := make([]byte, BlockSize) for i := 0; i < 12; i++ { - msg = l(msgBuf, h.ps(blockXor(h.eBuf, k, msg))) - k = l(kBuf, h.ps(blockXor(h.eBuf, k, c[i][:]))) + msg = l(h.eMsgBuf, h.ps(blockXor(h.eXorBuf, k, msg))) + k = l(h.eKBuf, h.ps(blockXor(h.eXorBuf, k, c[i][:]))) } - return blockXor(h.eBuf, k, msg) + return blockXor(h.eXorBuf, k, msg) } func blockXor(dst, x, y []byte) []byte { diff --git a/news.texi b/news.texi index f137b2b0b833df233e7d26d362ce2cb79e0abaff230704e5c5a8f8a19ccd0424..64e191ecf95b0f0bcac89940cc9c3f79f80c3e8cd1d753ff4ea86dbfdd0465f2 100644 --- a/news.texi +++ b/news.texi @@ -3,6 +3,10 @@ @unnumbered News @table @strong +@anchor{Release 5.4.0} +@item 5.4.0 + Even slightly less allocations in Streebog. + @anchor{Release 5.3.0} @item 5.3.0 ~16x speedup of Streebog, ~15x speedup of Kuznechik.