From 4593cdb30bd377c843fbcbd7e6afab49b8db4af5 Mon Sep 17 00:00:00 2001 From: Sun Yimin Date: Thu, 19 Jun 2025 13:31:43 +0800 Subject: [PATCH] all: golint --- cipher/hctr.go | 6 +++--- drbg/common.go | 6 +++--- internal/sm2ec/sm2p256_asm.go | 9 +++++---- internal/sm3/sm3block_amd64.go | 7 ++++--- internal/sm4/cbc_cipher_asm.go | 2 +- internal/sm4/gcm_cipher_asm.go | 2 +- internal/zuc/eia256.go | 4 ++-- mldsa/mldsa44.go | 6 +++--- pkcs/internal/md2/md2.go | 2 +- pkcs/internal/rc2/rc2.go | 32 ++++++++++++++++---------------- pkcs7/session.go | 9 +++++---- pkcs7/sign.go | 11 ++++------- slhdsa/fors.go | 2 +- sm2/example_test.go | 4 ++-- 14 files changed, 51 insertions(+), 51 deletions(-) diff --git a/cipher/hctr.go b/cipher/hctr.go index 66f10e0..7c9e6f5 100644 --- a/cipher/hctr.go +++ b/cipher/hctr.go @@ -100,7 +100,7 @@ var hctrReductionTable = []uint16{ 0xe100, 0xfd20, 0xd940, 0xc560, 0x9180, 0x8da0, 0xa9c0, 0xb5e0, } -// hctr represents a Varaible-Input-Length enciphering mode with a specific block cipher, +// hctr represents a Variable-Input-Length enciphering mode with a specific block cipher, // and specific tweak and a hash key. See // https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.470.5288 // GB/T 17964-2021 第11章 带泛杂凑函数的计数器工作模式 @@ -116,8 +116,8 @@ func (h *hctr) BlockSize() int { return blockSize } -// NewHCTR returns a [LengthPreservingMode] which encrypts/decrypts useing the given [Block] -// in HCTR mode. The lenght of tweak and hash key must be the same as the [Block]'s block size. +// NewHCTR returns a [LengthPreservingMode] which encrypts/decrypts using the given [Block] +// in HCTR mode. The length of tweak and hash key must be the same as the [Block]'s block size. func NewHCTR(cipher cipher.Block, tweak, hkey []byte) (LengthPreservingMode, error) { if len(tweak) != blockSize || len(hkey) != blockSize { return nil, errors.New("cipher: invalid tweak and/or hash key length") diff --git a/drbg/common.go b/drbg/common.go index 25a0dc5..0a207c5 100644 --- a/drbg/common.go +++ b/drbg/common.go @@ -69,7 +69,7 @@ func NewCtrDrbgPrng(cipherProvider func(key []byte) (cipher.Block, error), keyLe return nil, err } - // inital working state + // initial working state prng.impl, err = NewCtrDrbg(cipherProvider, keyLen, securityLevel, gm, entropyInput, nonce, personalization) if err != nil { return nil, err @@ -115,7 +115,7 @@ func NewHashDrbgPrng(newHash func() hash.Hash, entropySource io.Reader, security return nil, err } - // inital working state + // initial working state prng.impl, err = NewHashDrbg(newHash, securityLevel, gm, entropyInput, nonce, personalization) if err != nil { return nil, err @@ -158,7 +158,7 @@ func NewHmacDrbgPrng(newHash func() hash.Hash, entropySource io.Reader, security return nil, err } - // inital working state + // initial working state prng.impl, err = NewHmacDrbg(newHash, securityLevel, gm, entropyInput, nonce, personalization) if err != nil { return nil, err diff --git a/internal/sm2ec/sm2p256_asm.go b/internal/sm2ec/sm2p256_asm.go index 2863205..12d7b8c 100644 --- a/internal/sm2ec/sm2p256_asm.go +++ b/internal/sm2ec/sm2p256_asm.go @@ -890,13 +890,14 @@ func (p *SM2P256Point) p256ScalarMult(scalar *p256OrdElement) { p256PointDouble6TimesAsm(p, p) - if index >= 192 { + switch { + case index >= 192: wvalue = (scalar[3] >> (index & 63)) & 0x7f - } else if index >= 128 { + case index >= 128: wvalue = ((scalar[2] >> (index & 63)) + (scalar[3] << (64 - (index & 63)))) & 0x7f - } else if index >= 64 { + case index >= 64: wvalue = ((scalar[1] >> (index & 63)) + (scalar[2] << (64 - (index & 63)))) & 0x7f - } else { + default: wvalue = ((scalar[0] >> (index & 63)) + (scalar[1] << (64 - (index & 63)))) & 0x7f } diff --git a/internal/sm3/sm3block_amd64.go b/internal/sm3/sm3block_amd64.go index f21b7d1..062793c 100644 --- a/internal/sm3/sm3block_amd64.go +++ b/internal/sm3/sm3block_amd64.go @@ -18,11 +18,12 @@ func blockSIMD(dig *digest, p []byte) func blockAVX2(dig *digest, p []byte) func block(dig *digest, p []byte) { - if useAVX2 { + switch { + case useAVX2: blockAVX2(dig, p) - } else if useSSSE3 || useAVX { + case useSSSE3, useAVX: // useSSSE3 or useAVX blockSIMD(dig, p) - } else { + default: blockAMD64(dig, p) } } diff --git a/internal/sm4/cbc_cipher_asm.go b/internal/sm4/cbc_cipher_asm.go index d855670..2d583ac 100644 --- a/internal/sm4/cbc_cipher_asm.go +++ b/internal/sm4/cbc_cipher_asm.go @@ -83,5 +83,5 @@ func (x *cbc) SetIV(iv []byte) { if len(iv) != BlockSize { panic("cipher: incorrect length IV") } - copy(x.iv[:], iv) + copy(x.iv, iv) } diff --git a/internal/sm4/gcm_cipher_asm.go b/internal/sm4/gcm_cipher_asm.go index 0c73f3c..1c7f64b 100644 --- a/internal/sm4/gcm_cipher_asm.go +++ b/internal/sm4/gcm_cipher_asm.go @@ -269,7 +269,7 @@ func (g *gcm) counterCrypt(out, in []byte, counter *[gcmBlockSize]byte) { gcmInc32(counter) } g.cipher.EncryptBlocks(mask, counters) - subtle.XORBytes(out, in, mask[:]) + subtle.XORBytes(out, in, mask) out = out[g.cipher.blocksSize:] in = in[g.cipher.blocksSize:] } diff --git a/internal/zuc/eia256.go b/internal/zuc/eia256.go index b20d784..fa98a7b 100644 --- a/internal/zuc/eia256.go +++ b/internal/zuc/eia256.go @@ -226,7 +226,7 @@ func (m *ZUC256Mac) Finish(p []byte, nbits int) []byte { } digest := m.checkSum(nRemainBits, b) m.Reset() - return digest[:] + return digest } // Sum appends the current hash to in and returns the resulting slice. @@ -237,5 +237,5 @@ func (m *ZUC256Mac) Sum(in []byte) []byte { d0.t = make([]uint32, len(m.t)) copy(d0.t, m.t) hash := d0.checkSum(0, 0) - return append(in, hash[:]...) + return append(in, hash...) } diff --git a/mldsa/mldsa44.go b/mldsa/mldsa44.go index 6270bd6..eb8980a 100644 --- a/mldsa/mldsa44.go +++ b/mldsa/mldsa44.go @@ -52,7 +52,7 @@ const ( eta4 = 4 // private key range for ML-DSA-65 bitLenOfETA4 = 4 - lambda128 = 128 // collision strengh of c tilde for ML-DSA-44 + lambda128 = 128 // collision strength of c tilde for ML-DSA-44 lambda192 = 192 // collision strength of c tilde for ML-DSA-65 lambda256 = 256 // collision strength of c tilde for ML-DSA-87 @@ -501,7 +501,7 @@ func (sk *PrivateKey44) signInternal(seed, mu []byte) ([]byte, error) { r0NormThreshold := int(gamma2QMinus1Div88 - beta44) // rejection sampling loop - for kappa := 0; ; kappa = kappa + l44 { + for kappa := 0; ; kappa += l44 { // expand mask var ( y [l44]ringElement @@ -705,5 +705,5 @@ func (pk *PublicKey44) verifyInternal(sig, mu []byte) bool { var cTilde1 [lambda128 / 4]byte H.Read(cTilde1[:]) return subtle.ConstantTimeLessOrEq(int(gamma1TwoPower17-beta44), zNorm) == 0 && - subtle.ConstantTimeCompare(cTilde[:], cTilde1[:]) == 1 + subtle.ConstantTimeCompare(cTilde, cTilde1[:]) == 1 } diff --git a/pkcs/internal/md2/md2.go b/pkcs/internal/md2/md2.go index 5a98bbd..8392d1b 100644 --- a/pkcs/internal/md2/md2.go +++ b/pkcs/internal/md2/md2.go @@ -187,7 +187,7 @@ func block(dig *digest, p []byte) { X[k] ^= piSubst[t] t = X[k] } - t = t + byte(j) + t += byte(j) } // Save state diff --git a/pkcs/internal/rc2/rc2.go b/pkcs/internal/rc2/rc2.go index 89e85e7..fe6ff7f 100644 --- a/pkcs/internal/rc2/rc2.go +++ b/pkcs/internal/rc2/rc2.go @@ -140,10 +140,10 @@ func (c *rc2Cipher) Encrypt(dst, src []byte) { } // perform 1 mashing round - r0 = r0 + c.k[r3&63] - r1 = r1 + c.k[r0&63] - r2 = r2 + c.k[r1&63] - r3 = r3 + c.k[r2&63] + r0 += c.k[r3&63] + r1 += c.k[r0&63] + r2 += c.k[r1&63] + r3 += c.k[r2&63] // perform 6 mixing rounds for j <= 40 { @@ -169,10 +169,10 @@ func (c *rc2Cipher) Encrypt(dst, src []byte) { } // perform 1 mashing round - r0 = r0 + c.k[r3&63] - r1 = r1 + c.k[r0&63] - r2 = r2 + c.k[r1&63] - r3 = r3 + c.k[r2&63] + r0 += c.k[r3&63] + r1 += c.k[r0&63] + r2 += c.k[r1&63] + r3 += c.k[r2&63] // perform 5 mixing rounds for j <= 60 { @@ -244,10 +244,10 @@ func (c *rc2Cipher) Decrypt(dst, src []byte) { } // perform 1 r-mashing round - r3 = r3 - c.k[r2&63] - r2 = r2 - c.k[r1&63] - r1 = r1 - c.k[r0&63] - r0 = r0 - c.k[r3&63] + r3 -= c.k[r2&63] + r2 -= c.k[r1&63] + r1 -= c.k[r0&63] + r0 -= c.k[r3&63] // perform 6 r-mixing rounds for j >= 20 { @@ -273,10 +273,10 @@ func (c *rc2Cipher) Decrypt(dst, src []byte) { } // perform 1 r-mashing round - r3 = r3 - c.k[r2&63] - r2 = r2 - c.k[r1&63] - r1 = r1 - c.k[r0&63] - r0 = r0 - c.k[r3&63] + r3 -= c.k[r2&63] + r2 -= c.k[r1&63] + r1 -= c.k[r0&63] + r0 -= c.k[r3&63] // perform 5 r-mixing rounds for j >= 3 { diff --git a/pkcs7/session.go b/pkcs7/session.go index 3416fa4..14387cd 100644 --- a/pkcs7/session.go +++ b/pkcs7/session.go @@ -60,12 +60,12 @@ func (DefaultSession) EncryptdDataKey(key []byte, cert *smx509.Certificate, opts } func (DefaultSession) DecryptDataKey(key []byte, priv crypto.PrivateKey, cert *smx509.Certificate, opts any) ([]byte, error) { - switch pkey := priv.(type) { - case crypto.Decrypter: + if decrypter, ok := priv.(crypto.Decrypter); ok { // Generic case to handle anything that provides the crypto.Decrypter interface. encryptedKey := key var decrypterOpts crypto.DecrypterOpts - if _, ok := pkey.(*sm2.PrivateKey); ok { + + if _, isSM2Key := priv.(*sm2.PrivateKey); isSM2Key { if isLegacyCFCA, ok := opts.(bool); ok && isLegacyCFCA { encryptedKey = make([]byte, len(key)+1) encryptedKey[0] = 0x04 @@ -73,7 +73,8 @@ func (DefaultSession) DecryptDataKey(key []byte, priv crypto.PrivateKey, cert *s decrypterOpts = sm2.NewPlainDecrypterOpts(sm2.C1C2C3) } } - contentKey, err := pkey.Decrypt(rand.Reader, encryptedKey, decrypterOpts) + + contentKey, err := decrypter.Decrypt(rand.Reader, encryptedKey, decrypterOpts) if err != nil { return nil, err } diff --git a/pkcs7/sign.go b/pkcs7/sign.go index 61913a1..016eba4 100644 --- a/pkcs7/sign.go +++ b/pkcs7/sign.go @@ -436,13 +436,10 @@ func signData(data []byte, pkey crypto.PrivateKey, hasher crypto.Hash, isDigestP } else if len(hash) != sm3.Size { return nil, fmt.Errorf("pkcs7: invalid hash value for SM2 signature") } - switch realKey := key.(type) { - case *ecdsa.PrivateKey: - { - sm2Key := new(sm2.PrivateKey) - sm2Key.PrivateKey = *realKey - key = sm2Key - } + if ecdsaKey, ok := key.(*ecdsa.PrivateKey); ok { + sm2Key := new(sm2.PrivateKey) + sm2Key.PrivateKey = *ecdsaKey + key = sm2Key } } else { return nil, fmt.Errorf("pkcs7: unsupported hash function %v", hasher) diff --git a/slhdsa/fors.go b/slhdsa/fors.go index 14d29fa..0a9e733 100644 --- a/slhdsa/fors.go +++ b/slhdsa/fors.go @@ -63,7 +63,7 @@ func (pk *PublicKey) forsPkFromSig(md, signature []byte, adrs adrsOperations, ou for layer := range pk.params.a { adrs.setTreeHeight(layer + 1) if nodeID&1 == 0 { - treeIdx = treeIdx >> 1 + treeIdx >>= 1 adrs.setTreeIndex(treeIdx) pk.h.h(pk, adrs, rootPt, signature, rootPt) } else { diff --git a/sm2/example_test.go b/sm2/example_test.go index 0f8b9e9..bfe304a 100644 --- a/sm2/example_test.go +++ b/sm2/example_test.go @@ -121,7 +121,7 @@ func ExamplePrivateKey_Sign_withHash() { log.Fatalf("fail to new private key %v", err) } - // caluclate hash value + // calculate hash value h, err := sm2.NewHash(&testkey.PublicKey) if err != nil { log.Fatalf("fail to new hash %v", err) @@ -164,7 +164,7 @@ func ExampleVerifyASN1() { log.Fatalf("fail to new public key %v", err) } - // caluclate hash value + // calculate hash value data := []byte("ShangMi SM2 Sign Standard") h, err := sm2.NewHash(testkey) if err != nil {