Skip to content

Commit

Permalink
pkcs7: align pkix.AlgorithmIdentifier Null Parameters with others
Browse files Browse the repository at this point in the history
  • Loading branch information
emmansun authored Oct 30, 2024
1 parent 559da49 commit 8e2f6c1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 26 deletions.
16 changes: 8 additions & 8 deletions pkcs/kdf_pbkdf2.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,35 +74,35 @@ func newPRFParamFromHash(h Hash) (pkix.AlgorithmIdentifier, error) {
case SHA1:
return pkix.AlgorithmIdentifier{
Algorithm: oidHMACWithSHA1,
Parameters: asn1.RawValue{Tag: asn1.TagNull}}, nil
Parameters: asn1.NullRawValue}, nil
case SHA224:
return pkix.AlgorithmIdentifier{
Algorithm: oidHMACWithSHA224,
Parameters: asn1.RawValue{Tag: asn1.TagNull}}, nil
Parameters: asn1.NullRawValue}, nil
case SHA256:
return pkix.AlgorithmIdentifier{
Algorithm: oidHMACWithSHA256,
Parameters: asn1.RawValue{Tag: asn1.TagNull}}, nil
Parameters: asn1.NullRawValue}, nil
case SHA384:
return pkix.AlgorithmIdentifier{
Algorithm: oidHMACWithSHA384,
Parameters: asn1.RawValue{Tag: asn1.TagNull}}, nil
Parameters: asn1.NullRawValue}, nil
case SHA512:
return pkix.AlgorithmIdentifier{
Algorithm: oidHMACWithSHA512,
Parameters: asn1.RawValue{Tag: asn1.TagNull}}, nil
Parameters: asn1.NullRawValue}, nil
case SHA512_224:
return pkix.AlgorithmIdentifier{
Algorithm: oidHMACWithSHA512_224,
Parameters: asn1.RawValue{Tag: asn1.TagNull}}, nil
Parameters: asn1.NullRawValue}, nil
case SHA512_256:
return pkix.AlgorithmIdentifier{
Algorithm: oidHMACWithSHA512_256,
Parameters: asn1.RawValue{Tag: asn1.TagNull}}, nil
Parameters: asn1.NullRawValue}, nil
case SM3:
return pkix.AlgorithmIdentifier{
Algorithm: oidHMACWithSM3,
Parameters: asn1.RawValue{Tag: asn1.TagNull}}, nil
Parameters: asn1.NullRawValue}, nil

}
return pkix.AlgorithmIdentifier{}, errors.New("pbes/pbkdf2: unsupported hash function")
Expand Down
3 changes: 2 additions & 1 deletion pkcs7/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ func (ed *EnvelopedData) AddRecipient(cert *smx509.Certificate, version int, enc
Version: version,
IssuerAndSerialNumber: ias,
KeyEncryptionAlgorithm: pkix.AlgorithmIdentifier{
Algorithm: keyEncryptionAlgorithm,
Algorithm: keyEncryptionAlgorithm,
Parameters: asn1.NullRawValue,
},
EncryptedKey: encrypted,
}
Expand Down
12 changes: 6 additions & 6 deletions pkcs7/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (sd *SignedData) AddSignerChain(ee *smx509.Certificate, pkey crypto.Private
ias.IssuerName = asn1.RawValue{FullBytes: parents[0].RawSubject}
}
sd.sd.DigestAlgorithmIdentifiers = append(sd.sd.DigestAlgorithmIdentifiers,
pkix.AlgorithmIdentifier{Algorithm: sd.digestOid},
pkix.AlgorithmIdentifier{Algorithm: sd.digestOid, Parameters: asn1.NullRawValue},
)
hasher, err := getHashForOID(sd.digestOid)
if err != nil {
Expand Down Expand Up @@ -192,8 +192,8 @@ func (sd *SignedData) AddSignerChain(ee *smx509.Certificate, pkey crypto.Private
}
signer := signerInfo{
AuthenticatedAttributes: finalAttrs,
DigestAlgorithm: pkix.AlgorithmIdentifier{Algorithm: sd.digestOid},
DigestEncryptionAlgorithm: pkix.AlgorithmIdentifier{Algorithm: encryptionOid},
DigestAlgorithm: pkix.AlgorithmIdentifier{Algorithm: sd.digestOid, Parameters: asn1.NullRawValue},
DigestEncryptionAlgorithm: pkix.AlgorithmIdentifier{Algorithm: encryptionOid, Parameters: asn1.NullRawValue},
IssuerAndSerialNumber: ias,
EncryptedDigest: signature,
Version: 1,
Expand Down Expand Up @@ -231,7 +231,7 @@ func newHash(hasher crypto.Hash, hashOid asn1.ObjectIdentifier) hash.Hash {
// applications.
func (sd *SignedData) SignWithoutAttr(ee *smx509.Certificate, pkey crypto.PrivateKey, config SignerInfoConfig) error {
var signature []byte
sd.sd.DigestAlgorithmIdentifiers = append(sd.sd.DigestAlgorithmIdentifiers, pkix.AlgorithmIdentifier{Algorithm: sd.digestOid})
sd.sd.DigestAlgorithmIdentifiers = append(sd.sd.DigestAlgorithmIdentifiers, pkix.AlgorithmIdentifier{Algorithm: sd.digestOid, Parameters: asn1.NullRawValue})
hasher, err := getHashForOID(sd.digestOid)
if err != nil {
return err
Expand Down Expand Up @@ -265,8 +265,8 @@ func (sd *SignedData) SignWithoutAttr(ee *smx509.Certificate, pkey crypto.Privat
return err
}
signer := signerInfo{
DigestAlgorithm: pkix.AlgorithmIdentifier{Algorithm: sd.digestOid},
DigestEncryptionAlgorithm: pkix.AlgorithmIdentifier{Algorithm: sd.encryptionOid},
DigestAlgorithm: pkix.AlgorithmIdentifier{Algorithm: sd.digestOid, Parameters: asn1.NullRawValue},
DigestEncryptionAlgorithm: pkix.AlgorithmIdentifier{Algorithm: sd.encryptionOid, Parameters: asn1.NullRawValue},
IssuerAndSerialNumber: ias,
EncryptedDigest: signature,
Version: 1,
Expand Down
9 changes: 5 additions & 4 deletions pkcs7/sign_enveloped.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (saed *SignedAndEnvelopedData) AddSignerChain(ee *smx509.Certificate, pkey
ias.IssuerName = asn1.RawValue{FullBytes: parents[0].RawSubject}
}
saed.sed.DigestAlgorithmIdentifiers = append(saed.sed.DigestAlgorithmIdentifiers,
pkix.AlgorithmIdentifier{Algorithm: saed.digestOid},
pkix.AlgorithmIdentifier{Algorithm: saed.digestOid, Parameters: asn1.NullRawValue},
)
hasher, err := getHashForOID(saed.digestOid)
if err != nil {
Expand Down Expand Up @@ -250,8 +250,8 @@ func (saed *SignedAndEnvelopedData) AddSignerChain(ee *smx509.Certificate, pkey
return err
}
signer := signerInfo{
DigestAlgorithm: pkix.AlgorithmIdentifier{Algorithm: saed.digestOid},
DigestEncryptionAlgorithm: pkix.AlgorithmIdentifier{Algorithm: signatureOid},
DigestAlgorithm: pkix.AlgorithmIdentifier{Algorithm: saed.digestOid, Parameters: asn1.NullRawValue},
DigestEncryptionAlgorithm: pkix.AlgorithmIdentifier{Algorithm: signatureOid, Parameters: asn1.NullRawValue},
IssuerAndSerialNumber: ias,
EncryptedDigest: signature,
Version: 1,
Expand Down Expand Up @@ -287,7 +287,8 @@ func (saed *SignedAndEnvelopedData) AddRecipient(recipient *smx509.Certificate)
Version: 1,
IssuerAndSerialNumber: ias,
KeyEncryptionAlgorithm: pkix.AlgorithmIdentifier{
Algorithm: keyEncryptionAlgorithm,
Algorithm: keyEncryptionAlgorithm,
Parameters: asn1.NullRawValue,
},
EncryptedKey: encryptedKey,
}
Expand Down
7 changes: 0 additions & 7 deletions sm4/gcm_cipher_asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,6 @@ func (g *gcm) Open(dst, nonce, ciphertext, data []byte) ([]byte, error) {
return ret, nil
}

// reverseBits reverses the order of the bits of 4-bit number in i.
func reverseBits(i int) int {
i = ((i << 2) & 0xc) | ((i >> 2) & 0x3)
i = ((i << 1) & 0xa) | ((i >> 1) & 0x5)
return i
}

// gcmAdd adds two elements of GF(2¹²⁸) and returns the sum.
func gcmAdd(x, y *gcmFieldElement) gcmFieldElement {
// Addition in a characteristic 2 field is just XOR.
Expand Down

1 comment on commit 8e2f6c1

@emmansun
Copy link
Owner Author

@emmansun emmansun commented on 8e2f6c1 Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

尽管Parameters 是Optional,不填也不影响正确性,但实践中很多把它置成asn1.RawValue{Tag: asn1.TagNull}

Please sign in to comment.