Skip to content

Commit

Permalink
support kcdsa validation parameters in kx509
Browse files Browse the repository at this point in the history
  • Loading branch information
RyuaNerin committed Jan 25, 2024
1 parent 3c17380 commit 6c2c1ed
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
10 changes: 9 additions & 1 deletion kcdsa/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kcdsa

import (
"crypto"
"crypto/subtle"
"math/big"

"github.com/RyuaNerin/go-krypto/internal"
Expand Down Expand Up @@ -56,8 +57,15 @@ func (pub *PublicKey) Equal(x crypto.PublicKey) bool {
}

// Equal reports whether p, q, g and sizes have the same value.
func (params *Parameters) Equal(xx Parameters) bool {
func (params Parameters) Equal(xx Parameters) bool {
return internal.BigIntEqual(params.P, xx.P) &&
internal.BigIntEqual(params.Q, xx.Q) &&
internal.BigIntEqual(params.G, xx.G)
}

// Equal reports whether p, q, g and sizes have the same value.
func (params *TTAKParameters) Equal(xx TTAKParameters) bool {
return internal.BigIntEqual(params.J, xx.J) &&
subtle.ConstantTimeEq(int32(params.Count), int32(xx.Count)) == 1 &&
subtle.ConstantTimeCompare(params.Seed, xx.Seed) == 1
}
8 changes: 8 additions & 0 deletions kx509/pkcs8.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ func ParsePKCS8PrivateKey(der []byte) (key interface{}, err error) {
Q: params.Q,
G: params.G,
// TODO: Read KCDSA Parameters J, Seed, Count
TTAKParams: kcdsa.TTAKParameters{
J: params.J,
Seed: params.Seed,
Count: params.Count,
},
},
},
}
Expand Down Expand Up @@ -115,6 +120,9 @@ func marshalPKCS8PrivateKeyKCDSA(privKey *pkcs8, k *kcdsa.PrivateKey) error {
Q: k.Q,
G: k.G,
// TODO: Read KCDSA Parameters J, Seed, Count
J: k.TTAKParams.J,
Seed: k.TTAKParams.Seed,
Count: k.TTAKParams.Count,
})
if err != nil {
return errors.New("kx509: invalid paramerter")
Expand Down
31 changes: 31 additions & 0 deletions kx509/pkcs8_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,35 @@ func TestMarshalAndParsePKCS8PrivateKey(t *testing.T) {
}
}
})
t.Run("KCDSA-TTAK", func(t *testing.T) {
for _, size := range sizeList {
var p1 kcdsa.PrivateKey

_ = kcdsa.GenerateParametersTTAK(&p1.Parameters, rand.Reader, size)
_ = kcdsa.GenerateKey(&p1, rand.Reader)

der, err := MarshalPKCS8PrivateKey(&p1)
if err != nil {
t.Error(err)
return
}

p2r, err := ParsePKCS8PrivateKey(der)
if err != nil {
t.Error(err)
return
}

p2, ok := p2r.(*kcdsa.PrivateKey)
if !ok {
t.Error("type error")
return
}

if !p1.Equal(p2) || !p1.TTAKParams.Equal(p2.TTAKParams) {
t.Error("not equals!")
return
}
}
})
}
18 changes: 18 additions & 0 deletions kx509/pkix.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ func marshalPublicKey(pub interface{}) (publicKeyBytes []byte, publicKeyAlgorith
Q: pub.Q,
G: pub.G,
// TODO: Read KCDSA Parameters J, Seed, Count
J: pub.TTAKParams.J,
Seed: pub.TTAKParams.Seed,
Count: pub.TTAKParams.Count,
})
if err != nil {
return nil, pkix.AlgorithmIdentifier{}, errors.New("kx509: invalid paramerter")
Expand Down Expand Up @@ -195,6 +198,21 @@ func parsePublicKey(keyData *publicKeyInfo) (interface{}, error) {
pub.Parameters.Q.Sign() <= 0 || pub.Parameters.G.Sign() <= 0 {
return nil, errors.New("kx509: zero or negative KCDSA parameter")
}

// TODO: Read KCDSA Parameters J, Seed, Count
J := new(big.Int)
seed := make([]byte, 32)
var count int
if paramsDer.ReadASN1Integer(J) &&
paramsDer.ReadASN1Bytes(&seed, cryptobyte_asn1.OCTET_STRING) &&
paramsDer.ReadASN1Integer(&count) {
pub.Parameters.TTAKParams = kcdsa.TTAKParameters{
J: J,
Seed: seed,
Count: count,
}
}

return pub, nil

default:
Expand Down
33 changes: 33 additions & 0 deletions kx509/pkix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,37 @@ func TestMarshalAndParsePKIXPublicKey(t *testing.T) {
}
}
})
t.Run("KCDSA-TTAK", func(t *testing.T) {
for _, size := range sizeList {
var p1p kcdsa.PrivateKey

_ = kcdsa.GenerateParametersTTAK(&p1p.Parameters, rand.Reader, size)
_ = kcdsa.GenerateKey(&p1p, rand.Reader)

p1 := &p1p.PublicKey

der, err := MarshalPKIXPublicKey(p1)
if err != nil {
t.Error(err)
return
}

p2r, err := ParsePKIXPublicKey(der)
if err != nil {
t.Error(err)
return
}

p2, ok := p2r.(*kcdsa.PublicKey)
if !ok {
t.Error("type error")
return
}

if !p1.Equal(p2) || !p1.TTAKParams.Equal(p2.TTAKParams) {
t.Error("not equals!")
return
}
}
})
}

0 comments on commit 6c2c1ed

Please sign in to comment.