From: Sergey Matveev Date: Wed, 5 Apr 2023 11:21:15 +0000 (+0300) Subject: Slightly refactored error messages X-Git-Tag: v5.10.0~5 X-Git-Url: http://www.git.cypherpunks.ru/?p=gogost.git;a=commitdiff_plain;h=c19a111af499e05273906e9d5f7c059ab38d6f02 Slightly refactored error messages --- diff --git a/gost28147/mac.go b/gost28147/mac.go index a5a0487..71e8039 100644 --- a/gost28147/mac.go +++ b/gost28147/mac.go @@ -16,7 +16,7 @@ package gost28147 import ( - "errors" + "fmt" ) var ( @@ -42,10 +42,10 @@ type MAC struct { // following ones are fed to Write function. func (c *Cipher) NewMAC(size int, iv []byte) (*MAC, error) { if size == 0 || size > 8 { - return nil, errors.New("gogost/gost28147: invalid tag size") + return nil, fmt.Errorf("gogost/gost28147: invalid tag size (0<%d<=8)", size) } if len(iv) != BlockSize { - return nil, errors.New("gogost/gost28147: len(iv) != 8") + return nil, fmt.Errorf("gogost/gost28147: len(iv)=%d != %d", len(iv), BlockSize) } m := MAC{c: c, size: size, iv: iv} n2, n1 := block2nvs(iv) diff --git a/gost3410/private.go b/gost3410/private.go index f51096f..7629563 100644 --- a/gost3410/private.go +++ b/gost3410/private.go @@ -31,7 +31,7 @@ type PrivateKey struct { func NewPrivateKey(c *Curve, raw []byte) (*PrivateKey, error) { pointSize := c.PointSize() if len(raw) != pointSize { - return nil, fmt.Errorf("gogost/gost3410: len(key) != %d", pointSize) + return nil, fmt.Errorf("gogost/gost3410: len(key)=%d != %d", len(raw), pointSize) } key := make([]byte, pointSize) for i := 0; i < len(key); i++ { @@ -47,7 +47,7 @@ func NewPrivateKey(c *Curve, raw []byte) (*PrivateKey, error) { func GenPrivateKey(c *Curve, rand io.Reader) (*PrivateKey, error) { raw := make([]byte, c.PointSize()) if _, err := io.ReadFull(rand, raw); err != nil { - return nil, err + return nil, fmt.Errorf("gogost/gost3410.GenPrivateKey: %w", err) } return NewPrivateKey(c, raw) } @@ -61,7 +61,7 @@ func (prv *PrivateKey) Raw() []byte { func (prv *PrivateKey) PublicKey() (*PublicKey, error) { x, y, err := prv.C.Exp(prv.Key, prv.C.X, prv.C.Y) if err != nil { - return nil, err + return nil, fmt.Errorf("gogost/gost3410.PrivateKey.PublicKey: %w", err) } return &PublicKey{prv.C, x, y}, nil } @@ -80,7 +80,7 @@ func (prv *PrivateKey) SignDigest(digest []byte, rand io.Reader) ([]byte, error) s := big.NewInt(0) Retry: if _, err = io.ReadFull(rand, kRaw); err != nil { - return nil, err + return nil, fmt.Errorf("gogost/gost3410.PrivateKey.SignDigest: %w", err) } k = bytes2big(kRaw) k.Mod(k, prv.C.Q) @@ -89,7 +89,7 @@ Retry: } r, _, err = prv.C.Exp(k, prv.C.X, prv.C.Y) if err != nil { - return nil, err + return nil, fmt.Errorf("gogost/gost3410.PrivateKey.SignDigest: %w", err) } r.Mod(r, prv.C.Q) if r.Cmp(zero) == 0 { diff --git a/gost3410/public.go b/gost3410/public.go index ea1906d..957293e 100644 --- a/gost3410/public.go +++ b/gost3410/public.go @@ -56,7 +56,7 @@ func (pub *PublicKey) Raw() []byte { func (pub *PublicKey) VerifyDigest(digest, signature []byte) (bool, error) { pointSize := pub.C.PointSize() if len(signature) != 2*pointSize { - return false, fmt.Errorf("gogost/gost3410: len(signature) != %d", 2*pointSize) + return false, fmt.Errorf("gogost/gost3410: len(signature)=%d != %d", len(signature), 2*pointSize) } s := bytes2big(signature[:pointSize]) r := bytes2big(signature[pointSize:]) diff --git a/gost3410/vko.go b/gost3410/vko.go index 88d0a26..c0afa99 100644 --- a/gost3410/vko.go +++ b/gost3410/vko.go @@ -16,19 +16,20 @@ package gost3410 import ( + "fmt" "math/big" ) func (prv *PrivateKey) KEK(pub *PublicKey, ukm *big.Int) ([]byte, error) { keyX, keyY, err := prv.C.Exp(prv.Key, pub.X, pub.Y) if err != nil { - return nil, err + return nil, fmt.Errorf("gogost/gost3410.PrivateKey.KEK: %w", err) } u := big.NewInt(0).Set(ukm).Mul(ukm, prv.C.Co) if u.Cmp(bigInt1) != 0 { keyX, keyY, err = prv.C.Exp(u, keyX, keyY) if err != nil { - return nil, err + return nil, fmt.Errorf("gogost/gost3410.PrivateKey.KEK: %w", err) } } pk := PublicKey{prv.C, keyX, keyY} diff --git a/gost3410/vko2001.go b/gost3410/vko2001.go index 6d8fbf6..fd944b1 100644 --- a/gost3410/vko2001.go +++ b/gost3410/vko2001.go @@ -17,6 +17,7 @@ package gost3410 import ( "errors" + "fmt" "math/big" "go.cypherpunks.ru/gogost/v5/gost28147" @@ -31,11 +32,11 @@ func (prv *PrivateKey) KEK2001(pub *PublicKey, ukm *big.Int) ([]byte, error) { } key, err := prv.KEK(pub, ukm) if err != nil { - return nil, err + return nil, fmt.Errorf("gogost/gost3410.PrivateKey.KEK2001: %w", err) } h := gost341194.New(&gost28147.SboxIdGostR341194CryptoProParamSet) if _, err = h.Write(key); err != nil { - return nil, err + return nil, fmt.Errorf("gogost/gost3410.PrivateKey.KEK2001: %w", err) } return h.Sum(key[:0]), nil } diff --git a/gost3410/vko2012.go b/gost3410/vko2012.go index b452768..0875e19 100644 --- a/gost3410/vko2012.go +++ b/gost3410/vko2012.go @@ -16,6 +16,7 @@ package gost3410 import ( + "fmt" "math/big" "go.cypherpunks.ru/gogost/v5/gost34112012256" @@ -27,11 +28,11 @@ import ( func (prv *PrivateKey) KEK2012256(pub *PublicKey, ukm *big.Int) ([]byte, error) { key, err := prv.KEK(pub, ukm) if err != nil { - return nil, err + return nil, fmt.Errorf("gogost/gost3410.PrivateKey.KEK2012256: %w", err) } h := gost34112012256.New() if _, err = h.Write(key); err != nil { - return nil, err + return nil, fmt.Errorf("gogost/gost3410.PrivateKey.KEK2012256: %w", err) } return h.Sum(key[:0]), nil } @@ -41,11 +42,11 @@ func (prv *PrivateKey) KEK2012256(pub *PublicKey, ukm *big.Int) ([]byte, error) func (prv *PrivateKey) KEK2012512(pub *PublicKey, ukm *big.Int) ([]byte, error) { key, err := prv.KEK(pub, ukm) if err != nil { - return nil, err + return nil, fmt.Errorf("gogost/gost3410.PrivateKey.KEK2012256: %w", err) } h := gost34112012512.New() if _, err = h.Write(key); err != nil { - return nil, err + return nil, fmt.Errorf("gogost/gost3410.PrivateKey.KEK2012256: %w", err) } return h.Sum(key[:0]), nil } diff --git a/internal/gost34112012/hash.go b/internal/gost34112012/hash.go index 6d56ca0..0e6d02a 100644 --- a/internal/gost34112012/hash.go +++ b/internal/gost34112012/hash.go @@ -452,7 +452,7 @@ func (h *Hash) MarshalBinary() (data []byte, err error) { func (h *Hash) UnmarshalBinary(data []byte) error { expectedLen := len(MarshaledName) + 1 + 8 + 2*BlockSize if len(data) < expectedLen { - return fmt.Errorf("gogost/internal/gost34112012: len(data) != %d", expectedLen) + return fmt.Errorf("gogost/internal/gost34112012: len(data)=%d != %d", len(data), expectedLen) } if !bytes.HasPrefix(data, []byte(MarshaledName)) { return errors.New("gogost/internal/gost34112012: no hash name prefix") diff --git a/mgm/mode.go b/mgm/mode.go index 3fdeaba..50abfd4 100644 --- a/mgm/mode.go +++ b/mgm/mode.go @@ -21,6 +21,7 @@ import ( "crypto/hmac" "encoding/binary" "errors" + "fmt" ) var InvalidTag = errors.New("gogost/mgm: invalid authentication tag") @@ -45,10 +46,10 @@ type MGM struct { func NewMGM(cipher cipher.Block, tagSize int) (cipher.AEAD, error) { blockSize := cipher.BlockSize() if !(blockSize == 8 || blockSize == 16) { - return nil, errors.New("gogost/mgm: only 64/128 blocksizes allowed") + return nil, errors.New("gogost/mgm: only {64|128} blocksizes allowed") } if tagSize < 4 || tagSize > blockSize { - return nil, errors.New("gogost/mgm: invalid tag size") + return nil, fmt.Errorf("gogost/mgm: invalid tag size (4<=%d<=%d)", tagSize, blockSize) } mgm := MGM{ MaxSize: uint64(1< mgm.MaxSize { panic("ciphertext is too big")