*: replace slice.Clean() with clear()

Signed-off-by: Roman Khimov <roman@nspcc.ru>
This commit is contained in:
Roman Khimov 2024-08-23 22:31:03 +03:00
parent d9ee31fb52
commit 963e22ea95
5 changed files with 10 additions and 31 deletions

View file

@ -7,7 +7,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/encoding/base58"
"github.com/nspcc-dev/neo-go/pkg/util/slice"
"golang.org/x/crypto/scrypt"
"golang.org/x/text/unicode/norm"
)
@ -53,15 +52,15 @@ func NEP2Encrypt(priv *PrivateKey, passphrase string, params ScryptParams) (s st
if err != nil {
return s, err
}
defer slice.Clean(derivedKey)
defer clear(derivedKey)
derivedKey1 := derivedKey[:32]
derivedKey2 := derivedKey[32:]
privBytes := priv.Bytes()
defer slice.Clean(privBytes)
defer clear(privBytes)
xr := xor(privBytes, derivedKey1)
defer slice.Clean(xr)
defer clear(xr)
encrypted, err := aesEncrypt(xr, derivedKey2)
if err != nil {
@ -99,7 +98,7 @@ func NEP2Decrypt(key, passphrase string, params ScryptParams) (*PrivateKey, erro
if err != nil {
return nil, err
}
defer slice.Clean(derivedKey)
defer clear(derivedKey)
derivedKey1 := derivedKey[:32]
derivedKey2 := derivedKey[32:]
@ -109,10 +108,10 @@ func NEP2Decrypt(key, passphrase string, params ScryptParams) (*PrivateKey, erro
if err != nil {
return nil, err
}
defer slice.Clean(decrypted)
defer clear(decrypted)
privBytes := xor(decrypted, derivedKey1)
defer slice.Clean(privBytes)
defer clear(privBytes)
// Rebuild the private key.
privKey, err := NewPrivateKeyFromBytes(privBytes)

View file

@ -13,7 +13,6 @@ import (
"github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/util/slice"
"github.com/nspcc-dev/rfc6979"
)
@ -49,7 +48,7 @@ func NewPrivateKeyFromHex(str string) (*PrivateKey, error) {
if err != nil {
return nil, err
}
defer slice.Clean(b)
defer clear(b)
return NewPrivateKeyFromBytes(b)
}
@ -111,7 +110,7 @@ func NewPrivateKeyFromWIF(wif string) (*PrivateKey, error) {
// https://en.bitcoin.it/wiki/Wallet_import_format
func (p *PrivateKey) WIF() string {
pb := p.Bytes()
defer slice.Clean(pb)
defer clear(pb)
w, err := WIFEncode(pb, WIFVersion, true)
// The only way WIFEncode() can fail is if we're to give it a key of
// wrong size, but we have a proper key here, aren't we?
@ -124,10 +123,7 @@ func (p *PrivateKey) WIF() string {
// Destroy wipes the contents of the private key from memory. Any operations
// with the key after call to Destroy have undefined behavior.
func (p *PrivateKey) Destroy() {
bits := p.D.Bits()
for i := range bits {
bits[i] = 0
}
clear(p.D.Bits())
}
// Address derives the public NEO address that is coupled with the private key, and

View file

@ -5,7 +5,6 @@ import (
"fmt"
"github.com/nspcc-dev/neo-go/pkg/encoding/base58"
"github.com/nspcc-dev/neo-go/pkg/util/slice"
)
const (
@ -54,7 +53,7 @@ func WIFDecode(wif string, version byte) (*WIF, error) {
if err != nil {
return nil, err
}
defer slice.Clean(b)
defer clear(b)
if version == 0x00 {
version = WIFVersion

View file

@ -21,10 +21,3 @@ func reverse(dst []byte, src []byte) {
dst[i], dst[j] = src[j], src[i]
}
}
// Clean wipes the data in b by filling it with zeros.
func Clean(b []byte) {
for i := range b {
b[i] = 0
}
}

View file

@ -50,11 +50,3 @@ func TestCopyReverse(t *testing.T) {
}
}
}
func TestClean(t *testing.T) {
for _, tc := range testCases[1:] { // Empty one will be equal.
cp := bytes.Clone(tc.arr)
Clean(cp)
require.NotEqual(t, tc.arr, cp)
}
}