Skip to content

Commit

Permalink
修复 elgamal 名称错误问题
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Nov 1, 2024
1 parent 09659fe commit be89324
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 41 deletions.
6 changes: 3 additions & 3 deletions pubkey/elgamal/key_pkcs1.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (this PKCS1Key) ParsePublicKey(der []byte) (*PublicKey, error) {
!keyDer.ReadASN1Integer(publicKey.G) ||
!keyDer.ReadASN1Integer(q) ||
!keyDer.ReadASN1Integer(publicKey.Y) {
return nil, errors.New("cryptobin/elgamal: invalid EIGamal public key")
return nil, errors.New("cryptobin/elgamal: invalid ElGamal public key")
}

return publicKey, nil
Expand Down Expand Up @@ -154,11 +154,11 @@ func (this PKCS1Key) ParsePrivateKey(der []byte) (*PrivateKey, error) {
!keyDer.ReadASN1Integer(q) ||
!keyDer.ReadASN1Integer(privateKey.Y) ||
!keyDer.ReadASN1Integer(privateKey.X) {
return nil, errors.New("cryptobin/elgamal: invalid EIGamal private key")
return nil, errors.New("cryptobin/elgamal: invalid ElGamal private key")
}

if version != elgamalPrivKeyVersion {
return nil, fmt.Errorf("cryptobin/elgamal: unknown EIGamal private key version %d", version)
return nil, fmt.Errorf("cryptobin/elgamal: unknown ElGamal private key version %d", version)
}

return privateKey, nil
Expand Down
28 changes: 14 additions & 14 deletions pubkey/elgamal/key_pkcs8.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (

var (
// Unsure about this OID
// oidPublicKeyEIGamal = asn1.ObjectIdentifier{1, 3, 14, 7, 2, 1, 1}
// oidPublicKeyElGamal = asn1.ObjectIdentifier{1, 3, 14, 7, 2, 1, 1}
// oidMD2WithRSA = asn1.ObjectIdentifier{1, 3, 14, 7, 2, 3, 1}
// oidMD2WithElGamal = asn1.ObjectIdentifier{1, 3, 14, 7, 2, 3, 2}

// cryptlib public-key algorithm
oidPublicKeyEIGamal = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 3029, 1, 2, 1}
oidEIGamalWithSHA1 = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 3029, 1, 2, 1, 1}
oidEIGamalWithRIPEMD160 = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 3029, 1, 2, 1, 2}
oidPublicKeyElGamal = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 3029, 1, 2, 1}
oidElGamalWithSHA1 = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 3029, 1, 2, 1, 1}
oidElGamalWithRIPEMD160 = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 3029, 1, 2, 1, 2}
)

// elgamal Parameters
Expand Down Expand Up @@ -91,7 +91,7 @@ func (this PKCS8Key) MarshalPublicKey(key *PublicKey) ([]byte, error) {
return nil, errors.New("cryptobin/elgamal: failed to marshal algo param: " + err.Error())
}

publicKeyAlgorithm.Algorithm = oidPublicKeyEIGamal
publicKeyAlgorithm.Algorithm = oidPublicKeyElGamal
publicKeyAlgorithm.Parameters.FullBytes = paramBytes

var yInt cryptobyte.Builder
Expand Down Expand Up @@ -130,7 +130,7 @@ func (this PKCS8Key) ParsePublicKey(der []byte) (*PublicKey, error) {
return nil, asn1.SyntaxError{Msg: "trailing data"}
}

algoEq := pki.Algorithm.Algorithm.Equal(oidPublicKeyEIGamal)
algoEq := pki.Algorithm.Algorithm.Equal(oidPublicKeyElGamal)
if !algoEq {
return nil, errors.New("cryptobin/elgamal: unknown public key algorithm")
}
Expand All @@ -142,7 +142,7 @@ func (this PKCS8Key) ParsePublicKey(der []byte) (*PublicKey, error) {

y := new(big.Int)
if !yDer.ReadASN1Integer(y) {
return nil, errors.New("cryptobin/elgamal: invalid EIGamal public key")
return nil, errors.New("cryptobin/elgamal: invalid ElGamal public key")
}

pub := &PublicKey{
Expand All @@ -155,13 +155,13 @@ func (this PKCS8Key) ParsePublicKey(der []byte) (*PublicKey, error) {
if !paramsDer.ReadASN1(&paramsDer, cryptobyte_asn1.SEQUENCE) ||
!paramsDer.ReadASN1Integer(pub.P) ||
!paramsDer.ReadASN1Integer(pub.G) {
return nil, errors.New("cryptobin/elgamal: invalid EIGamal public key")
return nil, errors.New("cryptobin/elgamal: invalid ElGamal public key")
}

if pub.Y.Sign() <= 0 ||
pub.G.Sign() <= 0 ||
pub.P.Sign() <= 0 {
return nil, errors.New("cryptobin/elgamal: zero or negative EIGamal parameter")
return nil, errors.New("cryptobin/elgamal: zero or negative ElGamal parameter")
}

return pub, nil
Expand Down Expand Up @@ -194,7 +194,7 @@ func (this PKCS8Key) MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
}

privKey.Algo = pkix.AlgorithmIdentifier{
Algorithm: oidPublicKeyEIGamal,
Algorithm: oidPublicKeyElGamal,
Parameters: asn1.RawValue{
FullBytes: paramBytes,
},
Expand Down Expand Up @@ -226,15 +226,15 @@ func (this PKCS8Key) ParsePrivateKey(der []byte) (key *PrivateKey, err error) {
return nil, err
}

if !privKey.Algo.Algorithm.Equal(oidPublicKeyEIGamal) {
if !privKey.Algo.Algorithm.Equal(oidPublicKeyElGamal) {
return nil, fmt.Errorf("cryptobin/elgamal: PKCS#8 wrapping contained private key with unknown algorithm: %v", privKey.Algo.Algorithm)
}

xDer := cryptobyte.String(string(privKey.PrivateKey))

x := new(big.Int)
if !xDer.ReadASN1Integer(x) {
return nil, errors.New("cryptobin/elgamal: invalid EIGamal public key")
return nil, errors.New("cryptobin/elgamal: invalid ElGamal public key")
}

priv := &PrivateKey{
Expand All @@ -251,15 +251,15 @@ func (this PKCS8Key) ParsePrivateKey(der []byte) (key *PrivateKey, err error) {
if !paramsDer.ReadASN1(&paramsDer, cryptobyte_asn1.SEQUENCE) ||
!paramsDer.ReadASN1Integer(priv.P) ||
!paramsDer.ReadASN1Integer(priv.G) {
return nil, errors.New("cryptobin/elgamal: invalid EIGamal private key")
return nil, errors.New("cryptobin/elgamal: invalid ElGamal private key")
}

// 算出 Y 值
priv.Y.Exp(priv.G, priv.X, priv.P)

if priv.Y.Sign() <= 0 || priv.G.Sign() <= 0 ||
priv.P.Sign() <= 0 || priv.X.Sign() <= 0 {
return nil, errors.New("cryptobin/elgamal: zero or negative EIGamal parameter")
return nil, errors.New("cryptobin/elgamal: zero or negative ElGamal parameter")
}

return priv, nil
Expand Down
40 changes: 20 additions & 20 deletions x509/x509.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,15 @@ func parsePublicKey(algo PublicKeyAlgorithm, keyData *publicKeyInfo) (any, error
}

return pub, nil
case EIGamal:
case ElGamal:
keyBytes, err := asn1.Marshal(*keyData)
if err != nil {
return nil, errors.New("x509: failed to unmarshal EIGamal publickey")
return nil, errors.New("x509: failed to unmarshal ElGamal publickey")
}

pub, err := elgamal.ParsePKCS8PublicKey(keyBytes)
if err != nil {
return nil, errors.New("x509: failed to unmarshal EIGamal publickey")
return nil, errors.New("x509: failed to unmarshal ElGamal publickey")
}

return pub, nil
Expand Down Expand Up @@ -361,8 +361,8 @@ const (
GOST3410WithGOST34112001
GOST3410WithGOST34112012256
GOST3410WithGOST34112012512
EIGamalWithSHA1
EIGamalWithRIPEMD160
ElGamalWithSHA1
ElGamalWithRIPEMD160
)

func (algo SignatureAlgorithm) isRSAPSS() bool {
Expand Down Expand Up @@ -398,8 +398,8 @@ var algoName = [...]string{
GOST3410WithGOST34112001: "GOST3410-GOST34112001",
GOST3410WithGOST34112012256: "GOST3410-GOST34112012256",
GOST3410WithGOST34112012512: "GOST3410-GOST34112012512",
EIGamalWithSHA1: "EIGamal-SHA1",
EIGamalWithRIPEMD160: "EIGamal-RIPEMD160",
ElGamalWithSHA1: "ElGamal-SHA1",
ElGamalWithRIPEMD160: "ElGamal-RIPEMD160",
}

func (algo SignatureAlgorithm) String() string {
Expand All @@ -419,7 +419,7 @@ const (
Ed25519
SM2
GOST3410
EIGamal
ElGamal
)

// OIDs for signature algorithms
Expand Down Expand Up @@ -497,8 +497,8 @@ var (
oidSignatureGOST3410WithGOST34112012256 = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 3, 2}
oidSignatureGOST3410WithGOST34112012512 = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 3, 3}

oidSignatureEIGamalWithSHA1 = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 3029, 1, 2, 1, 1}
oidSignatureEIGamalWithRIPEMD160 = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 3029, 1, 2, 1, 2}
oidSignatureElGamalWithSHA1 = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 3029, 1, 2, 1, 1}
oidSignatureElGamalWithRIPEMD160 = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 3029, 1, 2, 1, 2}

oidSM3 = asn1.ObjectIdentifier{1, 2, 156, 10197, 1, 401, 1}
oidSHA256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 1}
Expand Down Expand Up @@ -554,8 +554,8 @@ var signatureAlgorithmDetails = []struct {
{GOST3410WithGOST34112012256, oidSignatureGOST3410WithGOST34112012256, GOST3410, GOST34112012256},
{GOST3410WithGOST34112012512, oidSignatureGOST3410WithGOST34112012512, GOST3410, GOST34112012512},

{EIGamalWithSHA1, oidSignatureEIGamalWithSHA1, EIGamal, SHA1},
{EIGamalWithRIPEMD160, oidSignatureEIGamalWithRIPEMD160, EIGamal, RIPEMD160},
{ElGamalWithSHA1, oidSignatureElGamalWithSHA1, ElGamal, SHA1},
{ElGamalWithRIPEMD160, oidSignatureElGamalWithRIPEMD160, ElGamal, RIPEMD160},
}

// pssParameters reflects the parameters in an AlgorithmIdentifier that
Expand Down Expand Up @@ -694,7 +694,7 @@ var (
oidGost2012PublicKey256 = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 1, 1}
oidGost2012PublicKey512 = asn1.ObjectIdentifier{1, 2, 643, 7, 1, 1, 1, 2}

oidPublicKeyEIGamal = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 3029, 1, 2, 1}
oidPublicKeyElGamal = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 3029, 1, 2, 1}
)

func getPublicKeyAlgorithmFromOID(oid asn1.ObjectIdentifier) PublicKeyAlgorithm {
Expand All @@ -711,8 +711,8 @@ func getPublicKeyAlgorithmFromOID(oid asn1.ObjectIdentifier) PublicKeyAlgorithm
oid.Equal(oidGost2012PublicKey256),
oid.Equal(oidGost2012PublicKey512):
return GOST3410
case oid.Equal(oidPublicKeyEIGamal):
return EIGamal
case oid.Equal(oidPublicKeyElGamal):
return ElGamal
}

return UnknownPublicKeyAlgorithm
Expand Down Expand Up @@ -1090,7 +1090,7 @@ func checkSignature(algo SignatureAlgorithm, signed, signature []byte, publicKey
var hashType Hash
switch algo {
case SHA1WithRSA, DSAWithSHA1, ECDSAWithSHA1,
SM2WithSHA1, EIGamalWithSHA1:
SM2WithSHA1, ElGamalWithSHA1:
hashType = SHA1
case SHA256WithRSA, SHA256WithRSAPSS, DSAWithSHA256,
ECDSAWithSHA256, SM2WithSHA256:
Expand All @@ -1111,7 +1111,7 @@ func checkSignature(algo SignatureAlgorithm, signed, signature []byte, publicKey
hashType = GOST34112012256
case GOST3410WithGOST34112012512:
hashType = GOST34112012512
case EIGamalWithRIPEMD160:
case ElGamalWithRIPEMD160:
hashType = RIPEMD160
default:
return ErrUnsupportedAlgorithm
Expand Down Expand Up @@ -2044,15 +2044,15 @@ func signingParamsForPublicKey(pub any, requestedSigAlgo SignatureAlgorithm) (ha
}

case *elgamal.PublicKey:
pubType = EIGamal
pubType = ElGamal
hashFunc = SHA1
sigAlgo.Algorithm = oidSignatureEIGamalWithSHA1
sigAlgo.Algorithm = oidSignatureElGamalWithSHA1
sigAlgo.Parameters = asn1.RawValue{
Tag: 5,
}

default:
err = errors.New("x509: only RSA, SM2, GOST3410, EIGamal and ECDSA keys supported")
err = errors.New("x509: only RSA, SM2, GOST3410, ElGamal and ECDSA keys supported")
}

if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions x509/x509_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ func Test_ELGamal(t *testing.T) {
CommonName: "test.example.com",
Organization: []string{"Test"},
},
SignatureAlgorithm: EIGamalWithSHA1,
SignatureAlgorithm: ElGamalWithSHA1,
}

reqPem, err := CreateCertificateRequest(rand.Reader, &templateReq, privKey)
Expand Down Expand Up @@ -1023,7 +1023,7 @@ func Test_ELGamal(t *testing.T) {
NotBefore: time.Now(),
NotAfter: time.Date(2028, time.October, 10, 12, 1, 1, 1, time.UTC),

SignatureAlgorithm: EIGamalWithSHA1,
SignatureAlgorithm: ElGamalWithSHA1,

SubjectKeyId: []byte{1, 2, 3, 4},
KeyUsage: KeyUsageCertSign,
Expand Down Expand Up @@ -1117,7 +1117,7 @@ func Test_ELGamal2(t *testing.T) {
CommonName: "test.example.com",
Organization: []string{"Test"},
},
SignatureAlgorithm: EIGamalWithRIPEMD160,
SignatureAlgorithm: ElGamalWithRIPEMD160,
}

reqPem, err := CreateCertificateRequest(rand.Reader, &templateReq, privKey)
Expand Down Expand Up @@ -1164,7 +1164,7 @@ func Test_ELGamal2(t *testing.T) {
NotBefore: time.Now(),
NotAfter: time.Date(2028, time.October, 10, 12, 1, 1, 1, time.UTC),

SignatureAlgorithm: EIGamalWithRIPEMD160,
SignatureAlgorithm: ElGamalWithRIPEMD160,

SubjectKeyId: []byte{1, 2, 3, 4},
KeyUsage: KeyUsageCertSign,
Expand Down

0 comments on commit be89324

Please sign in to comment.