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 +++--- diff --git a/gost28147/mac.go b/gost28147/mac.go index b79fbab55749a5e39d3dfd66098a3fdf61156768edf072aac8f54661ce1b01ef..aa03c0d8cc54f81a5e4ba77b0cf66e56b4b31aa89ea5ed836c1d640a8653a55d 100644 --- a/gost28147/mac.go +++ b/gost28147/mac.go @@ -42,10 +42,10 @@ // iv must be the first block of the authenticated data, second and // 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 21f10bc291c593ff57083ac938bc0b238fbaca9373566765edf0214e6af410fe..8f8ae9434b3a20c3eedf9ca40e2533846ab2becd1c2f968a6211ebe5b9daf438 100644 --- a/gost3410/curve.go +++ b/gost3410/curve.go @@ -79,7 +79,7 @@ r2.Add(r2, c.B) 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) 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 99b497085cd2d7dfa4b8ad0ecf80ba95e49334e281101cfc18b108856575c9aa..c281f2240f0cd8c64a685b37b9ff6bacd6c57b09400bd92726af52b2023544d8 100644 --- a/gost3410/private.go +++ b/gost3410/private.go @@ -18,6 +18,7 @@ import ( "crypto" "errors" + "fmt" "io" "math/big" ) @@ -30,7 +31,7 @@ } 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 @@ key[i] = raw[len(raw)-i-1] } 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 227653a2b62295789318b8f55fc3f9e58cdbbe479ad13c3509d104c3054e866c..19a6b8c5389db17b172b470bd52bcb1aed4b02b1a6fe622f0d47246dc1bef64e 100644 --- a/gost3410/public.go +++ b/gost3410/public.go @@ -16,7 +16,7 @@ package gost3410 import ( - "errors" + "fmt" "math/big" ) @@ -30,7 +30,7 @@ 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) 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 4eb437762b118cdcdd02eb66937ca211c9d0e8e5e4b1adfe052106c64e40d57d..686445cd010309ad1f0da4759fc14d7e89273713cdfa8f98fb89c1fda0977a83 100644 --- a/gost3410/vko2001.go +++ b/gost3410/vko2001.go @@ -27,7 +27,7 @@ // RFC 4357 VKO GOST R 34.10-2001 key agreement function. // 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 7373bdbdee4575ae1b2ed347359593c8e8437872c7e7d923bfa7aa7f7ae6e3ce..e05e70e301b53834f5176a1c4efa85a4bcf3cecd5bb9edc249f0f2d7d314cabc 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 @@ return } 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 f4e649d1aa715ee8ec5ac91770e0df7fc790c567387beb7ce74e43aafb836cb3..7f4fddc216a3955b14611b5e97213acee3d3eb982f5769099b39c543862d3645 100644 --- a/mgm/mode.go +++ b/mgm/mode.go @@ -61,10 +61,10 @@ 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<