Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Sep 29, 2024
1 parent ac659a5 commit 952025a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
25 changes: 23 additions & 2 deletions pkcs12/mac.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"crypto/sha256"
"crypto/sha512"
"crypto/x509/pkix"
"crypto/cipher"
"encoding/asn1"

"golang.org/x/crypto/md4"
Expand All @@ -19,8 +20,10 @@ import (
"github.com/deatil/go-cryptobin/kdf/gost_pbkdf2"
"github.com/deatil/go-cryptobin/hash/md2"
"github.com/deatil/go-cryptobin/hash/sm3"
"github.com/deatil/go-cryptobin/hash/gost/gost341194"
"github.com/deatil/go-cryptobin/hash/gost/gost34112012256"
"github.com/deatil/go-cryptobin/hash/gost/gost34112012512"
cipher_gost "github.com/deatil/go-cryptobin/cipher/gost"
)

// 可使用的 hash 方式
Expand All @@ -38,6 +41,7 @@ const (
SHA512_224
SHA512_256
SM3
GOST341194
GOST34112012256
GOST34112012512
)
Expand All @@ -60,6 +64,7 @@ var (
oidSHA512_256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 6}
oidSM3 = asn1.ObjectIdentifier{1, 2, 156, 10197, 1, 401}

oidGOST341194 = asn1.ObjectIdentifier{1, 2, 643, 2, 2, 10}
oidGOST34112012256 = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 2, 2}
oidGOST34112012512 = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 2, 3}
)
Expand Down Expand Up @@ -103,6 +108,8 @@ func hashByOID(oid asn1.ObjectIdentifier) (func() hash.Hash, int, error) {
return sha512.New512_256, 128, nil
case oid.Equal(oidSM3):
return sm3.New, 64, nil
case oid.Equal(oidGOST341194):
return newGOST341194Hash, 64, nil
case oid.Equal(oidGOST34112012256):
return gost34112012256.New, 64, nil
case oid.Equal(oidGOST34112012512):
Expand Down Expand Up @@ -138,6 +145,8 @@ func oidByHash(h Hash) (asn1.ObjectIdentifier, error) {
return oidSHA512_256, nil
case SM3:
return oidSM3, nil
case GOST341194:
return oidGOST341194, nil
case GOST34112012256:
return oidGOST34112012256, nil
case GOST34112012512:
Expand Down Expand Up @@ -212,7 +221,8 @@ func (this MacData) parseMacParam(password []byte) (h func() hash.Hash, key []by
}

switch {
case oid.Equal(oidGOST34112012256),
case oid.Equal(oidGOST341194),
oid.Equal(oidGOST34112012256),
oid.Equal(oidGOST34112012512):
pass, err := decodeBMPString(password)
if err != nil {
Expand Down Expand Up @@ -270,7 +280,8 @@ func (this MacOpts) Compute(message []byte, password []byte) (data MacKDFParamet

var key []byte
switch {
case alg.Equal(oidGOST34112012256),
case alg.Equal(oidGOST341194),
alg.Equal(oidGOST34112012256),
alg.Equal(oidGOST34112012512):
pass, err := decodeBMPString(password)
if err != nil {
Expand Down Expand Up @@ -300,3 +311,13 @@ func (this MacOpts) Compute(message []byte, password []byte) (data MacKDFParamet

return
}

func newGOST341194Hash() hash.Hash {
h := gost341194.New(func(key []byte) cipher.Block {
cip, _ := cipher_gost.NewCipher(key, cipher_gost.SboxGostR341194CryptoProParamSet)

return cip
})

return h
}
1 change: 1 addition & 0 deletions pkcs12/pkcs12_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ func Test_Encode(t *testing.T) {
test_Encode(t, newTestOpt(SHA512_224), "password-testkjjj", "testOpt SHA512_224")
test_Encode(t, newTestOpt(SHA512_256), "password-testkjjj", "testOpt SHA512_256")
test_Encode(t, newTestOpt(SM3), "password-testkjjj", "testOpt SM3")
test_Encode(t, newTestOpt(GOST341194), "password-testkjjj", "testOpt GOST341194")
test_Encode(t, newTestOpt(GOST34112012256), "password-testkjjj", "testOpt GOST34112012256")
test_Encode(t, newTestOpt(GOST34112012512), "password-testkjjj", "testOpt GOST34112012512")

Expand Down

0 comments on commit 952025a

Please sign in to comment.