]> Cypherpunks repositories - gogost.git/commitdiff
Fixed workability with Go 1.24
authorSergey Matveev <stargrave@stargrave.org>
Wed, 12 Feb 2025 08:15:23 +0000 (11:15 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Wed, 12 Feb 2025 08:19:23 +0000 (11:19 +0300)
go.mod
go.sum
gogost.go
gost34112012256/hkdf_test.go
gost34112012512/pbkdf2_test.go
gost341194/pbkdf2_test.go
mgm/mode.go
news.texi

diff --git a/go.mod b/go.mod
index 1d360920778d5b6b2ec339767cfb0c4573aa1377..d5e0c45679fbd5365073dc52b2e69f4e6de1c4c2 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -1,5 +1,3 @@
 module go.cypherpunks.su/gogost/v6
 
-go 1.23
-
-require golang.org/x/crypto v0.32.0
+go 1.24
diff --git a/go.sum b/go.sum
index 2f9c45b4dea01c7bd92180040ae12f1be7941183..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -1,2 +0,0 @@
-golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
-golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
index 9ebbf785e035bc6b16f2dd4c0cc2d02537755325..39185e04b9bc124181c279a6234cfb1e706ee9ad 100644 (file)
--- a/gogost.go
+++ b/gogost.go
@@ -1,4 +1,4 @@
 // Pure Go GOST cryptographic functions library.
 package gogost
 
-const Version = "6.0.2"
+const Version = "6.1.0"
index 94e47d59000a2d48a8afc27cb92569b3ed31e13c..106f63b6f3e2ba2c8ba8a2207480d8c7aa6feb6d 100644 (file)
@@ -19,7 +19,7 @@ import (
        "bytes"
        "testing"
 
-       "golang.org/x/crypto/hkdf"
+       "crypto/hkdf"
 )
 
 func TestHKDF(t *testing.T) {
@@ -33,7 +33,10 @@ func TestHKDF(t *testing.T) {
                        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                        0x08, 0x09, 0x0a, 0x0b, 0x0c,
                }
-               prk := hkdf.Extract(New, ikm, salt)
+               prk, err := hkdf.Extract(New, ikm, salt)
+               if err != nil {
+                       t.Fatal(err)
+               }
                if !bytes.Equal(prk, []byte{
                        0x94, 0x7d, 0x3d, 0x99, 0x2e, 0xdc, 0x44, 0xe3,
                        0x7b, 0x32, 0x72, 0x40, 0xf1, 0x5e, 0x62, 0x91,
@@ -43,8 +46,8 @@ func TestHKDF(t *testing.T) {
                        t.Fatal("PRK differs")
                }
                info := []byte{0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9}
-               okm := make([]byte, 42)
-               if _, err := hkdf.Expand(New, prk, info).Read(okm); err != nil {
+               okm, err := hkdf.Expand(New, prk, string(info), 42)
+               if err != nil {
                        t.Fatal("HKDF-Expand failed")
                }
                if !bytes.Equal(okm, []byte{
@@ -83,7 +86,10 @@ func TestHKDF(t *testing.T) {
                        0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
                        0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
                }
-               prk := hkdf.Extract(New, ikm, salt)
+               prk, err := hkdf.Extract(New, ikm, salt)
+               if err != nil {
+                       t.Fatal(err)
+               }
                if !bytes.Equal(prk, []byte{
                        0xa2, 0xff, 0xcd, 0x64, 0x44, 0x45, 0xef, 0x94,
                        0x7e, 0x7e, 0xc9, 0xa8, 0x26, 0x89, 0x3d, 0x9a,
@@ -104,8 +110,8 @@ func TestHKDF(t *testing.T) {
                        0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
                        0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
                }
-               okm := make([]byte, 82)
-               if _, err := hkdf.Expand(New, prk, info).Read(okm); err != nil {
+               okm, err := hkdf.Expand(New, prk, string(info), 82)
+               if err != nil {
                        t.Fatal("HKDF-Expand failed")
                }
                if !bytes.Equal(okm, []byte{
@@ -131,7 +137,10 @@ func TestHKDF(t *testing.T) {
                        0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
                }
                salt := []byte{}
-               prk := hkdf.Extract(New, ikm, salt)
+               prk, err := hkdf.Extract(New, ikm, salt)
+               if err != nil {
+                       t.Fatal(err)
+               }
                if !bytes.Equal(prk, []byte{
                        0x1f, 0xfc, 0x6d, 0x2b, 0xf9, 0x59, 0x4d, 0xfd,
                        0xf7, 0x75, 0xc2, 0xc1, 0x8f, 0xa3, 0xae, 0xcc,
@@ -141,8 +150,8 @@ func TestHKDF(t *testing.T) {
                        t.Fatal("PRK differs")
                }
                info := []byte{}
-               okm := make([]byte, 42)
-               if _, err := hkdf.Expand(New, prk, info).Read(okm); err != nil {
+               okm, err := hkdf.Expand(New, prk, string(info), 42)
+               if err != nil {
                        t.Fatal("HKDF-Expand failed")
                }
                if !bytes.Equal(okm, []byte{
index 3f00e7794c4159b7dea5b00605d7116787b03c67..741def4f9f45ca62194eba7ff95d674f88e8eb33 100644 (file)
@@ -19,19 +19,17 @@ import (
        "bytes"
        "testing"
 
-       "golang.org/x/crypto/pbkdf2"
+       "crypto/pbkdf2"
 )
 
 // Test vectors from http://tc26.ru/.../R_50.1.111-2016.pdf
 
 func Test1(t *testing.T) {
-       if !bytes.Equal(pbkdf2.Key(
-               []byte("password"),
-               []byte("salt"),
-               1,
-               64,
-               New,
-       ), []byte{
+       key, err := pbkdf2.Key(New, "password", []byte("salt"), 1, 64)
+       if err != nil {
+               t.Fatal(err)
+       }
+       if !bytes.Equal(key, []byte{
                0x64, 0x77, 0x0a, 0xf7, 0xf7, 0x48, 0xc3, 0xb1,
                0xc9, 0xac, 0x83, 0x1d, 0xbc, 0xfd, 0x85, 0xc2,
                0x61, 0x11, 0xb3, 0x0a, 0x8a, 0x65, 0x7d, 0xdc,
@@ -46,13 +44,11 @@ func Test1(t *testing.T) {
 }
 
 func Test2(t *testing.T) {
-       if !bytes.Equal(pbkdf2.Key(
-               []byte("password"),
-               []byte("salt"),
-               2,
-               64,
-               New,
-       ), []byte{
+       key, err := pbkdf2.Key(New, "password", []byte("salt"), 2, 64)
+       if err != nil {
+               t.Fatal(err)
+       }
+       if !bytes.Equal(key, []byte{
                0x5a, 0x58, 0x5b, 0xaf, 0xdf, 0xbb, 0x6e, 0x88,
                0x30, 0xd6, 0xd6, 0x8a, 0xa3, 0xb4, 0x3a, 0xc0,
                0x0d, 0x2e, 0x4a, 0xeb, 0xce, 0x01, 0xc9, 0xb3,
@@ -67,13 +63,11 @@ func Test2(t *testing.T) {
 }
 
 func Test4096(t *testing.T) {
-       if !bytes.Equal(pbkdf2.Key(
-               []byte("password"),
-               []byte("salt"),
-               4096,
-               64,
-               New,
-       ), []byte{
+       key, err := pbkdf2.Key(New, "password", []byte("salt"), 4096, 64)
+       if err != nil {
+               t.Fatal(err)
+       }
+       if !bytes.Equal(key, []byte{
                0xe5, 0x2d, 0xeb, 0x9a, 0x2d, 0x2a, 0xaf, 0xf4,
                0xe2, 0xac, 0x9d, 0x47, 0xa4, 0x1f, 0x34, 0xc2,
                0x03, 0x76, 0x59, 0x1c, 0x67, 0x80, 0x7f, 0x04,
@@ -88,13 +82,11 @@ func Test4096(t *testing.T) {
 }
 
 func TestWithZero(t *testing.T) {
-       if !bytes.Equal(pbkdf2.Key(
-               []byte("pass\x00word"),
-               []byte("sa\x00lt"),
-               4096,
-               64,
-               New,
-       ), []byte{
+       key, err := pbkdf2.Key(New, "pass\x00word", []byte("sa\x00lt"), 4096, 64)
+       if err != nil {
+               t.Fatal(err)
+       }
+       if !bytes.Equal(key, []byte{
                0x50, 0xdf, 0x06, 0x28, 0x85, 0xb6, 0x98, 0x01,
                0xa3, 0xc1, 0x02, 0x48, 0xeb, 0x0a, 0x27, 0xab,
                0x6e, 0x52, 0x2f, 0xfe, 0xb2, 0x0c, 0x99, 0x1c,
@@ -109,13 +101,17 @@ func TestWithZero(t *testing.T) {
 }
 
 func Test100(t *testing.T) {
-       if !bytes.Equal(pbkdf2.Key(
-               []byte("passwordPASSWORDpassword"),
+       key, err := pbkdf2.Key(
+               New,
+               "passwordPASSWORDpassword",
                []byte("saltSALTsaltSALTsaltSALTsaltSALTsalt"),
                4096,
                100,
-               New,
-       ), []byte{
+       )
+       if err != nil {
+               t.Fatal(err)
+       }
+       if !bytes.Equal(key, []byte{
                0xb2, 0xd8, 0xf1, 0x24, 0x5f, 0xc4, 0xd2, 0x92,
                0x74, 0x80, 0x20, 0x57, 0xe4, 0xb5, 0x4e, 0x0a,
                0x07, 0x53, 0xaa, 0x22, 0xfc, 0x53, 0x76, 0x0b,
@@ -136,13 +132,17 @@ func Test100(t *testing.T) {
 
 func Test16777216(t *testing.T) {
        t.Skip("takes too long")
-       if !bytes.Equal(pbkdf2.Key(
-               []byte("password"),
+       key, err := pbkdf2.Key(
+               New,
+               "password",
                []byte("salt"),
                16777216,
                64,
-               New,
-       ), []byte{
+       )
+       if err != nil {
+               t.Fatal(err)
+       }
+       if !bytes.Equal(key, []byte{
                0x49, 0xe4, 0x84, 0x3b, 0xba, 0x76, 0xe3, 0x00,
                0xaf, 0xe2, 0x4c, 0x4d, 0x23, 0xdc, 0x73, 0x92,
                0xde, 0xf1, 0x2f, 0x2c, 0x0e, 0x24, 0x41, 0x72,
index 23371e088bd9b1d816854a10ba5fb6ada5380c6c..39a747f85da01870fdc9b0409fc02d92f9dd2f82 100644 (file)
@@ -20,8 +20,9 @@ import (
        "hash"
        "testing"
 
+       "crypto/pbkdf2"
+
        "go.cypherpunks.su/gogost/v6/gost28147"
-       "golang.org/x/crypto/pbkdf2"
 )
 
 func PBKDF2Hash() hash.Hash {
@@ -30,106 +31,100 @@ func PBKDF2Hash() hash.Hash {
 
 // Test vectors for PBKDF2 taken from
 // http://tc26.ru/methods/containers_v1/Addition_to_PKCS5_v1_0.pdf test vectors
-func TestPBKDF2Vectors(t *testing.T) {
-       t.Run("1", func(t *testing.T) {
-               if !bytes.Equal(pbkdf2.Key(
-                       []byte("password"),
-                       []byte("salt"),
-                       1,
-                       32,
-                       PBKDF2Hash,
-               ), []byte{
-                       0x73, 0x14, 0xe7, 0xc0, 0x4f, 0xb2, 0xe6, 0x62,
-                       0xc5, 0x43, 0x67, 0x42, 0x53, 0xf6, 0x8b, 0xd0,
-                       0xb7, 0x34, 0x45, 0xd0, 0x7f, 0x24, 0x1b, 0xed,
-                       0x87, 0x28, 0x82, 0xda, 0x21, 0x66, 0x2d, 0x58,
-               }) {
-                       t.FailNow()
-               }
-       })
 
-       t.Run("2", func(t *testing.T) {
-               if !bytes.Equal(pbkdf2.Key(
-                       []byte("password"),
-                       []byte("salt"),
-                       2,
-                       32,
-                       PBKDF2Hash,
-               ), []byte{
-                       0x99, 0x0d, 0xfa, 0x2b, 0xd9, 0x65, 0x63, 0x9b,
-                       0xa4, 0x8b, 0x07, 0xb7, 0x92, 0x77, 0x5d, 0xf7,
-                       0x9f, 0x2d, 0xb3, 0x4f, 0xef, 0x25, 0xf2, 0x74,
-                       0x37, 0x88, 0x72, 0xfe, 0xd7, 0xed, 0x1b, 0xb3,
-               }) {
-                       t.FailNow()
-               }
-       })
+func TestPBKDF2Vectors1(t *testing.T) {
+       key, err := pbkdf2.Key(PBKDF2Hash, "password", []byte("salt"), 1, 32)
+       if err != nil {
+               t.Fatal(err)
+       }
+       if !bytes.Equal(key, []byte{
+               0x73, 0x14, 0xe7, 0xc0, 0x4f, 0xb2, 0xe6, 0x62,
+               0xc5, 0x43, 0x67, 0x42, 0x53, 0xf6, 0x8b, 0xd0,
+               0xb7, 0x34, 0x45, 0xd0, 0x7f, 0x24, 0x1b, 0xed,
+               0x87, 0x28, 0x82, 0xda, 0x21, 0x66, 0x2d, 0x58,
+       }) {
+               t.FailNow()
+       }
+}
 
-       t.Run("4096", func(t *testing.T) {
-               if !bytes.Equal(pbkdf2.Key(
-                       []byte("password"),
-                       []byte("salt"),
-                       4096,
-                       32,
-                       PBKDF2Hash,
-               ), []byte{
-                       0x1f, 0x18, 0x29, 0xa9, 0x4b, 0xdf, 0xf5, 0xbe,
-                       0x10, 0xd0, 0xae, 0xb3, 0x6a, 0xf4, 0x98, 0xe7,
-                       0xa9, 0x74, 0x67, 0xf3, 0xb3, 0x11, 0x16, 0xa5,
-                       0xa7, 0xc1, 0xaf, 0xff, 0x9d, 0xea, 0xda, 0xfe,
-               }) {
-                       t.FailNow()
-               }
-       })
+func TestPBKDF2Vectors2(t *testing.T) {
+       key, err := pbkdf2.Key(PBKDF2Hash, "password", []byte("salt"), 2, 32)
+       if err != nil {
+               t.Fatal(err)
+       }
+       if !bytes.Equal(key, []byte{
+               0x99, 0x0d, 0xfa, 0x2b, 0xd9, 0x65, 0x63, 0x9b,
+               0xa4, 0x8b, 0x07, 0xb7, 0x92, 0x77, 0x5d, 0xf7,
+               0x9f, 0x2d, 0xb3, 0x4f, 0xef, 0x25, 0xf2, 0x74,
+               0x37, 0x88, 0x72, 0xfe, 0xd7, 0xed, 0x1b, 0xb3,
+       }) {
+               t.FailNow()
+       }
+}
 
-       // t.Run("16777216", func(t *testing.T) {
-       //      if !bytes.Equal(pbkdf2.Key(
-       //              []byte("password"),
-       //              []byte("salt"),
-       //              16777216,
-       //              32,
-       //              PBKDF2Hash,
-       //      ), []byte{
-       //              0xa5, 0x7a, 0xe5, 0xa6, 0x08, 0x83, 0x96, 0xd1,
-       //              0x20, 0x85, 0x0c, 0x5c, 0x09, 0xde, 0x0a, 0x52,
-       //              0x51, 0x00, 0x93, 0x8a, 0x59, 0xb1, 0xb5, 0xc3,
-       //              0xf7, 0x81, 0x09, 0x10, 0xd0, 0x5f, 0xcd, 0x97,
-       //      }) {
-       //              t.FailNow()
-       //      }
-       // })
+func TestPBKDF2Vectors4096(t *testing.T) {
+       key, err := pbkdf2.Key(PBKDF2Hash, "password", []byte("salt"), 4096, 32)
+       if err != nil {
+               t.Fatal(err)
+       }
+       if !bytes.Equal(key, []byte{
+               0x1f, 0x18, 0x29, 0xa9, 0x4b, 0xdf, 0xf5, 0xbe,
+               0x10, 0xd0, 0xae, 0xb3, 0x6a, 0xf4, 0x98, 0xe7,
+               0xa9, 0x74, 0x67, 0xf3, 0xb3, 0x11, 0x16, 0xa5,
+               0xa7, 0xc1, 0xaf, 0xff, 0x9d, 0xea, 0xda, 0xfe,
+       }) {
+               t.FailNow()
+       }
+}
 
-       t.Run("many", func(t *testing.T) {
-               if !bytes.Equal(pbkdf2.Key(
-                       []byte("passwordPASSWORDpassword"),
-                       []byte("saltSALTsaltSALTsaltSALTsaltSALTsalt"),
-                       4096,
-                       40,
-                       PBKDF2Hash,
-               ), []byte{
-                       0x78, 0x83, 0x58, 0xc6, 0x9c, 0xb2, 0xdb, 0xe2,
-                       0x51, 0xa7, 0xbb, 0x17, 0xd5, 0xf4, 0x24, 0x1f,
-                       0x26, 0x5a, 0x79, 0x2a, 0x35, 0xbe, 0xcd, 0xe8,
-                       0xd5, 0x6f, 0x32, 0x6b, 0x49, 0xc8, 0x50, 0x47,
-                       0xb7, 0x63, 0x8a, 0xcb, 0x47, 0x64, 0xb1, 0xfd,
-               }) {
-                       t.FailNow()
-               }
-       })
+func TestPBKDF2Vectors16777216(t *testing.T) {
+       t.Skip("takes too long")
+       key, err := pbkdf2.Key(PBKDF2Hash, "password", []byte("salt"), 16777216, 32)
+       if err != nil {
+               t.Fatal(err)
+       }
+       if !bytes.Equal(key, []byte{
+               0xa5, 0x7a, 0xe5, 0xa6, 0x08, 0x83, 0x96, 0xd1,
+               0x20, 0x85, 0x0c, 0x5c, 0x09, 0xde, 0x0a, 0x52,
+               0x51, 0x00, 0x93, 0x8a, 0x59, 0xb1, 0xb5, 0xc3,
+               0xf7, 0x81, 0x09, 0x10, 0xd0, 0x5f, 0xcd, 0x97,
+       }) {
+               t.FailNow()
+       }
+}
+
+func TestPBKDF2VectorsMany(t *testing.T) {
+       key, err := pbkdf2.Key(
+               PBKDF2Hash,
+               "passwordPASSWORDpassword",
+               []byte("saltSALTsaltSALTsaltSALTsaltSALTsalt"),
+               4096,
+               40,
+       )
+       if err != nil {
+               t.Fatal(err)
+       }
+       if !bytes.Equal(key, []byte{
+               0x78, 0x83, 0x58, 0xc6, 0x9c, 0xb2, 0xdb, 0xe2,
+               0x51, 0xa7, 0xbb, 0x17, 0xd5, 0xf4, 0x24, 0x1f,
+               0x26, 0x5a, 0x79, 0x2a, 0x35, 0xbe, 0xcd, 0xe8,
+               0xd5, 0x6f, 0x32, 0x6b, 0x49, 0xc8, 0x50, 0x47,
+               0xb7, 0x63, 0x8a, 0xcb, 0x47, 0x64, 0xb1, 0xfd,
+       }) {
+               t.FailNow()
+       }
+}
 
-       t.Run("zero byte", func(t *testing.T) {
-               if !bytes.Equal(pbkdf2.Key(
-                       []byte("pass\x00word"),
-                       []byte("sa\x00lt"),
-                       4096,
-                       20,
-                       PBKDF2Hash,
-               ), []byte{
-                       0x43, 0xe0, 0x6c, 0x55, 0x90, 0xb0, 0x8c, 0x02,
-                       0x25, 0x24, 0x23, 0x73, 0x12, 0x7e, 0xdf, 0x9c,
-                       0x8e, 0x9c, 0x32, 0x91,
-               }) {
-                       t.FailNow()
-               }
-       })
+func TestPBKDF2VectorsZeroByte(t *testing.T) {
+       key, err := pbkdf2.Key(PBKDF2Hash, "pass\x00word", []byte("sa\x00lt"), 4096, 20)
+       if err != nil {
+               t.Fatal(err)
+       }
+       if !bytes.Equal(key, []byte{
+               0x43, 0xe0, 0x6c, 0x55, 0x90, 0xb0, 0x8c, 0x02,
+               0x25, 0x24, 0x23, 0x73, 0x12, 0x7e, 0xdf, 0x9c,
+               0x8e, 0x9c, 0x32, 0x91,
+       }) {
+               t.FailNow()
+       }
 }
index 60b39ecd2b48e23dbb7be760f31231a76afb359e..73f0c330021339eb24d45881c18648017ac91093 100644 (file)
@@ -173,8 +173,11 @@ func (mgm *MGM) crypt(out, in []byte) {
        mgm.cipher.Encrypt(mgm.bufP, mgm.icn) // Y_1 = E_K(0 || ICN)
        for len(in) >= mgm.BlockSize {
                mgm.cipher.Encrypt(mgm.bufC, mgm.bufP) // E_K(Y_i)
-               subtle.XORBytes(out, mgm.bufC, in)     // C_i = P_i (xor) E_K(Y_i)
-               incr(mgm.bufP[mgm.BlockSize/2:])       // Y_i = incr_r(Y_{i-1})
+               // subtle.XORBytes panic because of partial buffer overlapping
+               for i := 0; i < mgm.BlockSize; i++ {   // C_i = P_i (xor) E_K(Y_i)
+                       out[i] = mgm.bufC[i] ^ in[i]
+               }
+               incr(mgm.bufP[mgm.BlockSize/2:]) // Y_i = incr_r(Y_{i-1})
                out = out[mgm.BlockSize:]
                in = in[mgm.BlockSize:]
        }
index f14793fd6bd26e190351f7080ec712abc016a2bf..24732cb8cd4fe8385deaa434c7fb295c61358688 100644 (file)
--- a/news.texi
+++ b/news.texi
@@ -3,6 +3,14 @@
 
 @table @strong
 
+@anchor{Release 6.1.0}
+@item 6.1.0
+    @itemize
+    @item Fixed workability with Go 1.24's @code{subtle.XORBytes}
+    @item Go 1.24 is required now because of
+        @code{crypto/hkdf} and @code{crypto/pbkdf2}
+    @end itemize
+
 @anchor{Release 6.0.2}
 @item 6.0.2
 Minor trivial changes.