]> Cypherpunks.ru repositories - gogost.git/commitdiff
Slightly more descriptive errors v4.2.0
authorSergey Matveev <stargrave@stargrave.org>
Fri, 18 Oct 2019 12:57:26 +0000 (15:57 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Fri, 18 Oct 2019 12:57:26 +0000 (15:57 +0300)
gost28147/mac.go
gost3410/curve.go
gost3410/private.go
gost3410/public.go
gost3410/vko2001.go
internal/gost34112012/hash.go
mgm/mode.go

index bcfd52c5ed5044d5eee3f08524e5d5bd588e498b..b49bc7d305f67f720f2cb0cfc2d37cc0bb3795c7 100644 (file)
@@ -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)
index 47f964b583a66bf9b735b73c671b0ca0e97873e3..b5fa052c9812867ca5e034700061a5ec147654be 100644 (file)
@@ -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)
index 045c8185b94458e7ad2c9be153bd37eeb99f04ed..231f45e552934c7bcee1f44a31a929d2a72ca336 100644 (file)
@@ -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
 }
index 08e1414000bf6e3d42710b5485e002d74b6e46c7..de2d858c604d793d559c5b11fcdab47fdcc04761 100644 (file)
@@ -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)
index ed49d8f00fbb423f4991dc650bad9a618bd38800..c950280bf7f6de78a5e4ee71e4aab7898488ca22 100644 (file)
@@ -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 {
index 5f6b707d87a05a001326d464693406e10aa60672..2c1b4b989448076194b81d06136ca6bda460ee66 100644 (file)
@@ -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])
index fdbfa4277fad5577b2fc1af15a2857e547a4099c..7857968abec8de3a4cf941bad9c28455d42e7d64 100644 (file)
@@ -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<<uint(blockSize*8/2) - 1),
@@ -242,7 +242,7 @@ func (mgm *MGM) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, err
        copy(mgm.icn, nonce)
        mgm.auth(mgm.sum, ct, additionalData)
        if !hmac.Equal(mgm.sum[:mgm.tagSize], ciphertext[len(ciphertext)-mgm.tagSize:]) {
-               return nil, errors.New("invalid authentication tag")
+               return nil, errors.New("gogost/mgm: invalid authentication tag")
        }
        mgm.crypt(out, ct)
        return ret, nil