mirror of
https://github.com/emmansun/gmsm.git
synced 2025-06-28 00:13:26 +08:00
all: golint
This commit is contained in:
parent
1e53c5f16d
commit
4593cdb30b
@ -100,7 +100,7 @@ var hctrReductionTable = []uint16{
|
|||||||
0xe100, 0xfd20, 0xd940, 0xc560, 0x9180, 0x8da0, 0xa9c0, 0xb5e0,
|
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
|
// and specific tweak and a hash key. See
|
||||||
// https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.470.5288
|
// https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.470.5288
|
||||||
// GB/T 17964-2021 第11章 带泛杂凑函数的计数器工作模式
|
// GB/T 17964-2021 第11章 带泛杂凑函数的计数器工作模式
|
||||||
@ -116,8 +116,8 @@ func (h *hctr) BlockSize() int {
|
|||||||
return blockSize
|
return blockSize
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewHCTR returns a [LengthPreservingMode] which encrypts/decrypts useing the given [Block]
|
// NewHCTR returns a [LengthPreservingMode] which encrypts/decrypts using the given [Block]
|
||||||
// in HCTR mode. The lenght of tweak and hash key must be the same as the [Block]'s block size.
|
// 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) {
|
func NewHCTR(cipher cipher.Block, tweak, hkey []byte) (LengthPreservingMode, error) {
|
||||||
if len(tweak) != blockSize || len(hkey) != blockSize {
|
if len(tweak) != blockSize || len(hkey) != blockSize {
|
||||||
return nil, errors.New("cipher: invalid tweak and/or hash key length")
|
return nil, errors.New("cipher: invalid tweak and/or hash key length")
|
||||||
|
@ -69,7 +69,7 @@ func NewCtrDrbgPrng(cipherProvider func(key []byte) (cipher.Block, error), keyLe
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// inital working state
|
// initial working state
|
||||||
prng.impl, err = NewCtrDrbg(cipherProvider, keyLen, securityLevel, gm, entropyInput, nonce, personalization)
|
prng.impl, err = NewCtrDrbg(cipherProvider, keyLen, securityLevel, gm, entropyInput, nonce, personalization)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -115,7 +115,7 @@ func NewHashDrbgPrng(newHash func() hash.Hash, entropySource io.Reader, security
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// inital working state
|
// initial working state
|
||||||
prng.impl, err = NewHashDrbg(newHash, securityLevel, gm, entropyInput, nonce, personalization)
|
prng.impl, err = NewHashDrbg(newHash, securityLevel, gm, entropyInput, nonce, personalization)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -158,7 +158,7 @@ func NewHmacDrbgPrng(newHash func() hash.Hash, entropySource io.Reader, security
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// inital working state
|
// initial working state
|
||||||
prng.impl, err = NewHmacDrbg(newHash, securityLevel, gm, entropyInput, nonce, personalization)
|
prng.impl, err = NewHmacDrbg(newHash, securityLevel, gm, entropyInput, nonce, personalization)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -890,13 +890,14 @@ func (p *SM2P256Point) p256ScalarMult(scalar *p256OrdElement) {
|
|||||||
|
|
||||||
p256PointDouble6TimesAsm(p, p)
|
p256PointDouble6TimesAsm(p, p)
|
||||||
|
|
||||||
if index >= 192 {
|
switch {
|
||||||
|
case index >= 192:
|
||||||
wvalue = (scalar[3] >> (index & 63)) & 0x7f
|
wvalue = (scalar[3] >> (index & 63)) & 0x7f
|
||||||
} else if index >= 128 {
|
case index >= 128:
|
||||||
wvalue = ((scalar[2] >> (index & 63)) + (scalar[3] << (64 - (index & 63)))) & 0x7f
|
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
|
wvalue = ((scalar[1] >> (index & 63)) + (scalar[2] << (64 - (index & 63)))) & 0x7f
|
||||||
} else {
|
default:
|
||||||
wvalue = ((scalar[0] >> (index & 63)) + (scalar[1] << (64 - (index & 63)))) & 0x7f
|
wvalue = ((scalar[0] >> (index & 63)) + (scalar[1] << (64 - (index & 63)))) & 0x7f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,11 +18,12 @@ func blockSIMD(dig *digest, p []byte)
|
|||||||
func blockAVX2(dig *digest, p []byte)
|
func blockAVX2(dig *digest, p []byte)
|
||||||
|
|
||||||
func block(dig *digest, p []byte) {
|
func block(dig *digest, p []byte) {
|
||||||
if useAVX2 {
|
switch {
|
||||||
|
case useAVX2:
|
||||||
blockAVX2(dig, p)
|
blockAVX2(dig, p)
|
||||||
} else if useSSSE3 || useAVX {
|
case useSSSE3, useAVX: // useSSSE3 or useAVX
|
||||||
blockSIMD(dig, p)
|
blockSIMD(dig, p)
|
||||||
} else {
|
default:
|
||||||
blockAMD64(dig, p)
|
blockAMD64(dig, p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,5 +83,5 @@ func (x *cbc) SetIV(iv []byte) {
|
|||||||
if len(iv) != BlockSize {
|
if len(iv) != BlockSize {
|
||||||
panic("cipher: incorrect length IV")
|
panic("cipher: incorrect length IV")
|
||||||
}
|
}
|
||||||
copy(x.iv[:], iv)
|
copy(x.iv, iv)
|
||||||
}
|
}
|
||||||
|
@ -269,7 +269,7 @@ func (g *gcm) counterCrypt(out, in []byte, counter *[gcmBlockSize]byte) {
|
|||||||
gcmInc32(counter)
|
gcmInc32(counter)
|
||||||
}
|
}
|
||||||
g.cipher.EncryptBlocks(mask, counters)
|
g.cipher.EncryptBlocks(mask, counters)
|
||||||
subtle.XORBytes(out, in, mask[:])
|
subtle.XORBytes(out, in, mask)
|
||||||
out = out[g.cipher.blocksSize:]
|
out = out[g.cipher.blocksSize:]
|
||||||
in = in[g.cipher.blocksSize:]
|
in = in[g.cipher.blocksSize:]
|
||||||
}
|
}
|
||||||
|
@ -226,7 +226,7 @@ func (m *ZUC256Mac) Finish(p []byte, nbits int) []byte {
|
|||||||
}
|
}
|
||||||
digest := m.checkSum(nRemainBits, b)
|
digest := m.checkSum(nRemainBits, b)
|
||||||
m.Reset()
|
m.Reset()
|
||||||
return digest[:]
|
return digest
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sum appends the current hash to in and returns the resulting slice.
|
// 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))
|
d0.t = make([]uint32, len(m.t))
|
||||||
copy(d0.t, m.t)
|
copy(d0.t, m.t)
|
||||||
hash := d0.checkSum(0, 0)
|
hash := d0.checkSum(0, 0)
|
||||||
return append(in, hash[:]...)
|
return append(in, hash...)
|
||||||
}
|
}
|
||||||
|
@ -52,7 +52,7 @@ const (
|
|||||||
eta4 = 4 // private key range for ML-DSA-65
|
eta4 = 4 // private key range for ML-DSA-65
|
||||||
bitLenOfETA4 = 4
|
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
|
lambda192 = 192 // collision strength of c tilde for ML-DSA-65
|
||||||
lambda256 = 256 // collision strength of c tilde for ML-DSA-87
|
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)
|
r0NormThreshold := int(gamma2QMinus1Div88 - beta44)
|
||||||
|
|
||||||
// rejection sampling loop
|
// rejection sampling loop
|
||||||
for kappa := 0; ; kappa = kappa + l44 {
|
for kappa := 0; ; kappa += l44 {
|
||||||
// expand mask
|
// expand mask
|
||||||
var (
|
var (
|
||||||
y [l44]ringElement
|
y [l44]ringElement
|
||||||
@ -705,5 +705,5 @@ func (pk *PublicKey44) verifyInternal(sig, mu []byte) bool {
|
|||||||
var cTilde1 [lambda128 / 4]byte
|
var cTilde1 [lambda128 / 4]byte
|
||||||
H.Read(cTilde1[:])
|
H.Read(cTilde1[:])
|
||||||
return subtle.ConstantTimeLessOrEq(int(gamma1TwoPower17-beta44), zNorm) == 0 &&
|
return subtle.ConstantTimeLessOrEq(int(gamma1TwoPower17-beta44), zNorm) == 0 &&
|
||||||
subtle.ConstantTimeCompare(cTilde[:], cTilde1[:]) == 1
|
subtle.ConstantTimeCompare(cTilde, cTilde1[:]) == 1
|
||||||
}
|
}
|
||||||
|
@ -187,7 +187,7 @@ func block(dig *digest, p []byte) {
|
|||||||
X[k] ^= piSubst[t]
|
X[k] ^= piSubst[t]
|
||||||
t = X[k]
|
t = X[k]
|
||||||
}
|
}
|
||||||
t = t + byte(j)
|
t += byte(j)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save state
|
// Save state
|
||||||
|
@ -140,10 +140,10 @@ func (c *rc2Cipher) Encrypt(dst, src []byte) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// perform 1 mashing round
|
// perform 1 mashing round
|
||||||
r0 = r0 + c.k[r3&63]
|
r0 += c.k[r3&63]
|
||||||
r1 = r1 + c.k[r0&63]
|
r1 += c.k[r0&63]
|
||||||
r2 = r2 + c.k[r1&63]
|
r2 += c.k[r1&63]
|
||||||
r3 = r3 + c.k[r2&63]
|
r3 += c.k[r2&63]
|
||||||
|
|
||||||
// perform 6 mixing rounds
|
// perform 6 mixing rounds
|
||||||
for j <= 40 {
|
for j <= 40 {
|
||||||
@ -169,10 +169,10 @@ func (c *rc2Cipher) Encrypt(dst, src []byte) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// perform 1 mashing round
|
// perform 1 mashing round
|
||||||
r0 = r0 + c.k[r3&63]
|
r0 += c.k[r3&63]
|
||||||
r1 = r1 + c.k[r0&63]
|
r1 += c.k[r0&63]
|
||||||
r2 = r2 + c.k[r1&63]
|
r2 += c.k[r1&63]
|
||||||
r3 = r3 + c.k[r2&63]
|
r3 += c.k[r2&63]
|
||||||
|
|
||||||
// perform 5 mixing rounds
|
// perform 5 mixing rounds
|
||||||
for j <= 60 {
|
for j <= 60 {
|
||||||
@ -244,10 +244,10 @@ func (c *rc2Cipher) Decrypt(dst, src []byte) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// perform 1 r-mashing round
|
// perform 1 r-mashing round
|
||||||
r3 = r3 - c.k[r2&63]
|
r3 -= c.k[r2&63]
|
||||||
r2 = r2 - c.k[r1&63]
|
r2 -= c.k[r1&63]
|
||||||
r1 = r1 - c.k[r0&63]
|
r1 -= c.k[r0&63]
|
||||||
r0 = r0 - c.k[r3&63]
|
r0 -= c.k[r3&63]
|
||||||
|
|
||||||
// perform 6 r-mixing rounds
|
// perform 6 r-mixing rounds
|
||||||
for j >= 20 {
|
for j >= 20 {
|
||||||
@ -273,10 +273,10 @@ func (c *rc2Cipher) Decrypt(dst, src []byte) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// perform 1 r-mashing round
|
// perform 1 r-mashing round
|
||||||
r3 = r3 - c.k[r2&63]
|
r3 -= c.k[r2&63]
|
||||||
r2 = r2 - c.k[r1&63]
|
r2 -= c.k[r1&63]
|
||||||
r1 = r1 - c.k[r0&63]
|
r1 -= c.k[r0&63]
|
||||||
r0 = r0 - c.k[r3&63]
|
r0 -= c.k[r3&63]
|
||||||
|
|
||||||
// perform 5 r-mixing rounds
|
// perform 5 r-mixing rounds
|
||||||
for j >= 3 {
|
for j >= 3 {
|
||||||
|
@ -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) {
|
func (DefaultSession) DecryptDataKey(key []byte, priv crypto.PrivateKey, cert *smx509.Certificate, opts any) ([]byte, error) {
|
||||||
switch pkey := priv.(type) {
|
if decrypter, ok := priv.(crypto.Decrypter); ok {
|
||||||
case crypto.Decrypter:
|
|
||||||
// Generic case to handle anything that provides the crypto.Decrypter interface.
|
// Generic case to handle anything that provides the crypto.Decrypter interface.
|
||||||
encryptedKey := key
|
encryptedKey := key
|
||||||
var decrypterOpts crypto.DecrypterOpts
|
var decrypterOpts crypto.DecrypterOpts
|
||||||
if _, ok := pkey.(*sm2.PrivateKey); ok {
|
|
||||||
|
if _, isSM2Key := priv.(*sm2.PrivateKey); isSM2Key {
|
||||||
if isLegacyCFCA, ok := opts.(bool); ok && isLegacyCFCA {
|
if isLegacyCFCA, ok := opts.(bool); ok && isLegacyCFCA {
|
||||||
encryptedKey = make([]byte, len(key)+1)
|
encryptedKey = make([]byte, len(key)+1)
|
||||||
encryptedKey[0] = 0x04
|
encryptedKey[0] = 0x04
|
||||||
@ -73,7 +73,8 @@ func (DefaultSession) DecryptDataKey(key []byte, priv crypto.PrivateKey, cert *s
|
|||||||
decrypterOpts = sm2.NewPlainDecrypterOpts(sm2.C1C2C3)
|
decrypterOpts = sm2.NewPlainDecrypterOpts(sm2.C1C2C3)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
contentKey, err := pkey.Decrypt(rand.Reader, encryptedKey, decrypterOpts)
|
|
||||||
|
contentKey, err := decrypter.Decrypt(rand.Reader, encryptedKey, decrypterOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -436,13 +436,10 @@ func signData(data []byte, pkey crypto.PrivateKey, hasher crypto.Hash, isDigestP
|
|||||||
} else if len(hash) != sm3.Size {
|
} else if len(hash) != sm3.Size {
|
||||||
return nil, fmt.Errorf("pkcs7: invalid hash value for SM2 signature")
|
return nil, fmt.Errorf("pkcs7: invalid hash value for SM2 signature")
|
||||||
}
|
}
|
||||||
switch realKey := key.(type) {
|
if ecdsaKey, ok := key.(*ecdsa.PrivateKey); ok {
|
||||||
case *ecdsa.PrivateKey:
|
sm2Key := new(sm2.PrivateKey)
|
||||||
{
|
sm2Key.PrivateKey = *ecdsaKey
|
||||||
sm2Key := new(sm2.PrivateKey)
|
key = sm2Key
|
||||||
sm2Key.PrivateKey = *realKey
|
|
||||||
key = sm2Key
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return nil, fmt.Errorf("pkcs7: unsupported hash function %v", hasher)
|
return nil, fmt.Errorf("pkcs7: unsupported hash function %v", hasher)
|
||||||
|
@ -63,7 +63,7 @@ func (pk *PublicKey) forsPkFromSig(md, signature []byte, adrs adrsOperations, ou
|
|||||||
for layer := range pk.params.a {
|
for layer := range pk.params.a {
|
||||||
adrs.setTreeHeight(layer + 1)
|
adrs.setTreeHeight(layer + 1)
|
||||||
if nodeID&1 == 0 {
|
if nodeID&1 == 0 {
|
||||||
treeIdx = treeIdx >> 1
|
treeIdx >>= 1
|
||||||
adrs.setTreeIndex(treeIdx)
|
adrs.setTreeIndex(treeIdx)
|
||||||
pk.h.h(pk, adrs, rootPt, signature, rootPt)
|
pk.h.h(pk, adrs, rootPt, signature, rootPt)
|
||||||
} else {
|
} else {
|
||||||
|
@ -121,7 +121,7 @@ func ExamplePrivateKey_Sign_withHash() {
|
|||||||
log.Fatalf("fail to new private key %v", err)
|
log.Fatalf("fail to new private key %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// caluclate hash value
|
// calculate hash value
|
||||||
h, err := sm2.NewHash(&testkey.PublicKey)
|
h, err := sm2.NewHash(&testkey.PublicKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("fail to new hash %v", err)
|
log.Fatalf("fail to new hash %v", err)
|
||||||
@ -164,7 +164,7 @@ func ExampleVerifyASN1() {
|
|||||||
log.Fatalf("fail to new public key %v", err)
|
log.Fatalf("fail to new public key %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// caluclate hash value
|
// calculate hash value
|
||||||
data := []byte("ShangMi SM2 Sign Standard")
|
data := []byte("ShangMi SM2 Sign Standard")
|
||||||
h, err := sm2.NewHash(testkey)
|
h, err := sm2.NewHash(testkey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user