]> Cypherpunks.ru repositories - gogost.git/commitdiff
Split 34.11-2012 on two different modules, add corresponding Size constant
authorSergey Matveev <stargrave@stargrave.org>
Sun, 20 Nov 2016 13:32:31 +0000 (16:32 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sun, 20 Nov 2016 13:32:31 +0000 (16:32 +0300)
common.mk
src/cypherpunks.ru/gogost/cmd/gogost-streebog/main.go [moved from src/cypherpunks.ru/gogost/gost34112012/cmd/gogost-streebog/main.go with 80% similarity]
src/cypherpunks.ru/gogost/gost34112012256/hash.go [new file with mode: 0644]
src/cypherpunks.ru/gogost/gost34112012512/hash.go [new file with mode: 0644]
src/cypherpunks.ru/gogost/internal/gost34112012/hash.go [moved from src/cypherpunks.ru/gogost/gost34112012/hash.go with 98% similarity]
src/cypherpunks.ru/gogost/internal/gost34112012/hash_test.go [moved from src/cypherpunks.ru/gogost/gost34112012/hash_test.go with 97% similarity]

index 5d59e850bb60113c635711f9da9e5f9bf0951aa9..d86b24ca093ac586fb9c111e48fb9d1d42370f1a 100644 (file)
--- 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/...
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 b6b9ff4f93f119770f36d59b3d94ca1be7720016..2817a2ac64d00e4cbd49ecb0ce3ff96e945c818c 100644 (file)
@@ -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 (file)
index 0000000..45da245
--- /dev/null
@@ -0,0 +1,32 @@
+// GoGOST -- Pure Go GOST cryptographic functions library
+// Copyright (C) 2015-2016 Sergey Matveev <stargrave@stargrave.org>
+//
+// 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 <http://www.gnu.org/licenses/>.
+
+// 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 (file)
index 0000000..e28f951
--- /dev/null
@@ -0,0 +1,32 @@
+// GoGOST -- Pure Go GOST cryptographic functions library
+// Copyright (C) 2015-2016 Sergey Matveev <stargrave@stargrave.org>
+//
+// 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 <http://www.gnu.org/licenses/>.
+
+// 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)
+}
similarity index 98%
rename from src/cypherpunks.ru/gogost/gost34112012/hash.go
rename to src/cypherpunks.ru/gogost/internal/gost34112012/hash.go
index 8f8c77e90ed9ea8b445c80b5215c38d1b5474c54..5a8227cd309adc10112888bdd28a539974257bed 100644 (file)
@@ -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[:]...)
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 e22db5fa33d478bb4f80787e47940ad4d1f7940d..82b99c244be07ace60d3c75fe1eb09636bf1bfb0 100644 (file)
@@ -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()