From 0da4d634ac5368d024489baf4bdd5d422b84dd84 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Fri, 18 Oct 2019 15:57:26 +0300 Subject: [PATCH] Slightly more descriptive errors --- gost28147/mac.go | 4 ++-- gost3410/curve.go | 4 ++-- gost3410/private.go | 5 +++-- gost3410/public.go | 11 +++++++---- gost3410/vko2001.go | 2 +- internal/gost34112012/hash.go | 8 +++++--- mgm/mode.go | 6 +++--- 7 files changed, 23 insertions(+), 17 deletions(-) diff --git a/gost28147/mac.go b/gost28147/mac.go index bcfd52c..b49bc7d 100644 --- a/gost28147/mac.go +++ b/gost28147/mac.go @@ -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("Invalid tag size") + return nil, errors.New("gogost/gost28147: invalid tag size") } if len(iv) != BlockSize { - return nil, errors.New("iv length is not equal to blocksize") + return nil, errors.New("gogost/gost28147: len(iv) != 8") } m := MAC{c: c, size: size, iv: iv} n2, n1 := block2nvs(iv) diff --git a/gost3410/curve.go b/gost3410/curve.go index 47f964b..b5fa052 100644 --- a/gost3410/curve.go +++ b/gost3410/curve.go @@ -79,7 +79,7 @@ func NewCurve(p, q, a, b, x, y, e, d *big.Int) (*Curve, error) { r2.Mod(r2, c.P) c.pos(r2) if r1.Cmp(r2) != 0 { - return nil, errors.New("Invalid curve parameters") + return nil, errors.New("gogost/gost3410: invalid curve parameters") } if e != nil && d != nil { c.E = e @@ -131,7 +131,7 @@ func (c *Curve) add(p1x, p1y, p2x, p2y *big.Int) { func (c *Curve) Exp(degree, xS, yS *big.Int) (*big.Int, *big.Int, error) { if degree.Cmp(zero) == 0 { - return nil, nil, errors.New("Bad degree value") + return nil, nil, errors.New("gogost/gost3410: zero degree value") } dg := big.NewInt(0).Sub(degree, bigInt1) tx := big.NewInt(0).Set(xS) diff --git a/gost3410/private.go b/gost3410/private.go index 045c818..231f45e 100644 --- a/gost3410/private.go +++ b/gost3410/private.go @@ -18,6 +18,7 @@ package gost3410 import ( "crypto" "errors" + "fmt" "io" "math/big" ) @@ -30,7 +31,7 @@ type PrivateKey struct { func NewPrivateKey(curve *Curve, mode Mode, raw []byte) (*PrivateKey, error) { if len(raw) != int(mode) { - return nil, errors.New("Invalid private key length") + return nil, fmt.Errorf("gogost/gost3410: len(key) != %d", mode) } key := make([]byte, int(mode)) for i := 0; i < len(key); i++ { @@ -38,7 +39,7 @@ func NewPrivateKey(curve *Curve, mode Mode, raw []byte) (*PrivateKey, error) { } k := bytes2big(key) if k.Cmp(zero) == 0 { - return nil, errors.New("Zero private key") + return nil, errors.New("gogost/gost3410: zero private key") } return &PrivateKey{curve, mode, k}, nil } diff --git a/gost3410/public.go b/gost3410/public.go index 08e1414..de2d858 100644 --- a/gost3410/public.go +++ b/gost3410/public.go @@ -16,7 +16,7 @@ package gost3410 import ( - "errors" + "fmt" "math/big" ) @@ -30,7 +30,7 @@ type PublicKey struct { func NewPublicKey(curve *Curve, mode Mode, raw []byte) (*PublicKey, error) { key := make([]byte, 2*int(mode)) if len(raw) != len(key) { - return nil, errors.New("Invalid public key length") + return nil, fmt.Errorf("gogost/gost3410: len(key) != %d", len(key)) } for i := 0; i < len(key); i++ { key[i] = raw[len(raw)-i-1] @@ -54,11 +54,14 @@ func (pub *PublicKey) Raw() []byte { func (pub *PublicKey) VerifyDigest(digest, signature []byte) (bool, error) { if len(signature) != 2*int(pub.Mode) { - return false, errors.New("Invalid signature length") + return false, fmt.Errorf("gogost/gost3410: len(signature) != %d", 2*int(pub.Mode)) } s := bytes2big(signature[:pub.Mode]) r := bytes2big(signature[pub.Mode:]) - if r.Cmp(zero) <= 0 || r.Cmp(pub.C.Q) >= 0 || s.Cmp(zero) <= 0 || s.Cmp(pub.C.Q) >= 0 { + if r.Cmp(zero) <= 0 || + r.Cmp(pub.C.Q) >= 0 || + s.Cmp(zero) <= 0 || + s.Cmp(pub.C.Q) >= 0 { return false, nil } e := bytes2big(digest) diff --git a/gost3410/vko2001.go b/gost3410/vko2001.go index ed49d8f..c950280 100644 --- a/gost3410/vko2001.go +++ b/gost3410/vko2001.go @@ -27,7 +27,7 @@ import ( // UKM is user keying material, also called VKO-factor. func (prv *PrivateKey) KEK2001(pub *PublicKey, ukm *big.Int) ([]byte, error) { if prv.Mode != Mode2001 { - return nil, errors.New("KEK2001 can not be used in Mode2012") + return nil, errors.New("gogost/gost3410: KEK2001 can not be used in Mode2012") } key, err := prv.KEK(pub, ukm) if err != nil { diff --git a/internal/gost34112012/hash.go b/internal/gost34112012/hash.go index 5f6b707..2c1b4b9 100644 --- a/internal/gost34112012/hash.go +++ b/internal/gost34112012/hash.go @@ -21,6 +21,7 @@ import ( "bytes" "encoding/binary" "errors" + "fmt" ) const ( @@ -426,11 +427,12 @@ func (h *Hash) MarshalBinary() (data []byte, err error) { } func (h *Hash) UnmarshalBinary(data []byte) error { - if len(data) < len(MarshaledName)+1+8+3*BlockSize { - return errors.New("too short data") + expectedLen := len(MarshaledName) + 1 + 8 + 3*BlockSize + if len(data) < expectedLen { + return fmt.Errorf("gogost/internal/gost34112012: len(data) != %d", expectedLen) } if !bytes.HasPrefix(data, []byte(MarshaledName)) { - return errors.New("no hash name prefix") + return errors.New("gogost/internal/gost34112012: no hash name prefix") } idx := len(MarshaledName) h.size = int(data[idx]) diff --git a/mgm/mode.go b/mgm/mode.go index fdbfa42..7857968 100644 --- a/mgm/mode.go +++ b/mgm/mode.go @@ -61,10 +61,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("MGM supports only 64/128 blocksizes") + return nil, errors.New("gogost/mgm: only 64/128 blocksizes allowed") } if tagSize < 4 || tagSize > blockSize { - return nil, errors.New("invalid tag size") + return nil, errors.New("gogost/mgm: invalid tag size") } mgm := MGM{ maxSize: uint64(1<