From: Sergey Matveev Date: Sun, 20 Nov 2016 13:32:31 +0000 (+0300) Subject: Split 34.11-2012 on two different modules, add corresponding Size constant X-Git-Tag: 2.0~9 X-Git-Url: http://www.git.cypherpunks.ru/?p=gogost.git;a=commitdiff_plain;h=5733007c9d8416d7aec16324b788156168d2e7a6 Split 34.11-2012 on two different modules, add corresponding Size constant --- diff --git a/common.mk b/common.mk index 5d59e85..d86b24c 100644 --- a/common.mk +++ b/common.mk @@ -1,7 +1,7 @@ LDFLAGS = -X cypherpunks.ru/gogost.Version=$(VERSION) gogost-streebog: - GOPATH=$(GOPATH) go build -ldflags "$(LDFLAGS)" cypherpunks.ru/gogost/gost34112012/cmd/gogost-streebog + GOPATH=$(GOPATH) go build -ldflags "$(LDFLAGS)" cypherpunks.ru/gogost/cmd/gogost-streebog bench: GOPATH=$(GOPATH) go test -benchmem -bench . cypherpunks.ru/gogost/... diff --git a/src/cypherpunks.ru/gogost/gost34112012/cmd/gogost-streebog/main.go b/src/cypherpunks.ru/gogost/cmd/gogost-streebog/main.go similarity index 80% rename from src/cypherpunks.ru/gogost/gost34112012/cmd/gogost-streebog/main.go rename to src/cypherpunks.ru/gogost/cmd/gogost-streebog/main.go index b6b9ff4..2817a2a 100644 --- a/src/cypherpunks.ru/gogost/gost34112012/cmd/gogost-streebog/main.go +++ b/src/cypherpunks.ru/gogost/cmd/gogost-streebog/main.go @@ -21,15 +21,17 @@ import ( "encoding/hex" "flag" "fmt" + "hash" "io" "os" "cypherpunks.ru/gogost" - "cypherpunks.ru/gogost/gost34112012" + "cypherpunks.ru/gogost/gost34112012256" + "cypherpunks.ru/gogost/gost34112012512" ) var ( - digestSize = flag.Int("size", 256, "Digest size (either 256 or 512)") + digestSize = flag.Int("size", 256, "Digest size in bits (either 256 or 512)") version = flag.Bool("version", false, "Print version information") ) @@ -39,7 +41,12 @@ func main() { fmt.Println(gogost.Version) return } - h := gost34112012.New(*digestSize) + var h hash.Hash + if *digestSize == 256 { + h = gost34112012256.New() + } else { + h = gost34112012512.New() + } io.Copy(h, os.Stdin) fmt.Println(hex.EncodeToString(h.Sum(nil))) } diff --git a/src/cypherpunks.ru/gogost/gost34112012256/hash.go b/src/cypherpunks.ru/gogost/gost34112012256/hash.go new file mode 100644 index 0000000..45da245 --- /dev/null +++ b/src/cypherpunks.ru/gogost/gost34112012256/hash.go @@ -0,0 +1,32 @@ +// GoGOST -- Pure Go GOST cryptographic functions library +// Copyright (C) 2015-2016 Sergey Matveev +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +// GOST R 34.11-2012 256-bit hash function. +// RFC 6986. +package gost34112012256 + +import ( + "cypherpunks.ru/gogost/internal/gost34112012" +) + +const ( + BlockSize = gost34112012.BlockSize + Size = 32 +) + +func New() *gost34112012.Hash { + return gost34112012.New(32) +} diff --git a/src/cypherpunks.ru/gogost/gost34112012512/hash.go b/src/cypherpunks.ru/gogost/gost34112012512/hash.go new file mode 100644 index 0000000..e28f951 --- /dev/null +++ b/src/cypherpunks.ru/gogost/gost34112012512/hash.go @@ -0,0 +1,32 @@ +// GoGOST -- Pure Go GOST cryptographic functions library +// Copyright (C) 2015-2016 Sergey Matveev +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +// GOST R 34.11-2012 512-bit hash function. +// RFC 6986. +package gost34112012512 + +import ( + "cypherpunks.ru/gogost/internal/gost34112012" +) + +const ( + BlockSize = gost34112012.BlockSize + Size = 64 +) + +func New() *gost34112012.Hash { + return gost34112012.New(64) +} diff --git a/src/cypherpunks.ru/gogost/gost34112012/hash.go b/src/cypherpunks.ru/gogost/internal/gost34112012/hash.go similarity index 98% rename from src/cypherpunks.ru/gogost/gost34112012/hash.go rename to src/cypherpunks.ru/gogost/internal/gost34112012/hash.go index 8f8c77e..5a8227c 100644 --- a/src/cypherpunks.ru/gogost/gost34112012/hash.go +++ b/src/cypherpunks.ru/gogost/internal/gost34112012/hash.go @@ -24,7 +24,6 @@ import ( const ( BlockSize = 64 - Size = 64 ) var ( @@ -279,10 +278,9 @@ type Hash struct { } // Create new hash object with specified size digest size. -// Size is either 512 or 256 (bits). func New(size int) *Hash { - if size != 256 && size != 512 { - panic("size must be either 256 or 512") + if size != 32 && size != 64 { + panic("size must be either 32 or 64") } h := Hash{ size: size, @@ -299,7 +297,7 @@ func (h *Hash) Reset() { h.buf = nil for i := 0; i < BlockSize; i++ { h.chk[i] = 0 - if h.size == 256 { + if h.size == 32 { h.hsh[i] = 1 } else { h.hsh[i] = 0 @@ -312,7 +310,7 @@ func (h *Hash) BlockSize() int { } func (h *Hash) Size() int { - return h.size / 8 + return h.size } func (h *Hash) Write(data []byte) (int, error) { @@ -336,7 +334,7 @@ func (h *Hash) Sum(in []byte) []byte { binary.LittleEndian.PutUint64(h.tmp[:], h.n+uint64(len(h.buf))*8) hsh = g(0, hsh, h.tmp) hsh = g(0, hsh, add512bit(h.chk, buf)) - if h.size == 256 { + if h.size == 32 { return append(in, hsh[BlockSize/2:]...) } return append(in, hsh[:]...) diff --git a/src/cypherpunks.ru/gogost/gost34112012/hash_test.go b/src/cypherpunks.ru/gogost/internal/gost34112012/hash_test.go similarity index 97% rename from src/cypherpunks.ru/gogost/gost34112012/hash_test.go rename to src/cypherpunks.ru/gogost/internal/gost34112012/hash_test.go index e22db5f..82b99c2 100644 --- a/src/cypherpunks.ru/gogost/gost34112012/hash_test.go +++ b/src/cypherpunks.ru/gogost/internal/gost34112012/hash_test.go @@ -25,13 +25,13 @@ import ( ) func TestHashInterface(t *testing.T) { - h := New(512) + h := New(64) var _ hash.Hash = h } func TestVectors(t *testing.T) { - h512 := New(512) - h256 := New(256) + h512 := New(64) + h256 := New(32) // First vector m := []byte{ @@ -106,7 +106,7 @@ func TestVectors(t *testing.T) { } func TestBlocksized(t *testing.T) { - h := New(512) + h := New(64) m := make([]byte, BlockSize) for i := 0; i < BlockSize; i++ { m[i] = byte(i) @@ -127,7 +127,7 @@ func TestBlocksized(t *testing.T) { } func TestBehaviour(t *testing.T) { - h := New(512) + h := New(64) // Sum does not change the state hsh1 := h.Sum(nil) if bytes.Compare(h.Sum(nil), hsh1) != 0 { @@ -146,7 +146,7 @@ func TestBehaviour(t *testing.T) { } func TestRandom(t *testing.T) { - h := New(512) + h := New(64) f := func(data []byte) bool { h.Reset() h.Write(data) @@ -164,7 +164,7 @@ func TestRandom(t *testing.T) { } func BenchmarkHash(b *testing.B) { - h := New(512) + h := New(64) src := make([]byte, BlockSize+1) rand.Read(src) b.ResetTimer()