]> Cypherpunks.ru repositories - govpn.git/commitdiff
wrap errors
authorBruno Clermont <bruno@robotinfra.com>
Wed, 8 Feb 2017 10:39:31 +0000 (18:39 +0800)
committerSergey Matveev <stargrave@stargrave.org>
Sat, 25 Feb 2017 08:23:45 +0000 (11:23 +0300)
src/cypherpunks.ru/govpn/aont/oaep.go
src/cypherpunks.ru/govpn/cnw/cnw.go
src/cypherpunks.ru/govpn/encless.go
src/cypherpunks.ru/govpn/tap_freebsd.go

index a2329a3dd28187a3dc6a83cd64de946cbada643b..7431060fa0a6b3bb1dabdd0a0ab12a75b8239269 100644 (file)
@@ -36,9 +36,9 @@ package aont
 
 import (
        "crypto/subtle"
-       "errors"
 
        "chacha20"
+       "github.com/pkg/errors"
        "golang.org/x/crypto/blake2b"
 )
 
@@ -47,6 +47,9 @@ const (
        HSize = 32
        // RSize size of generated random output in terms of OAEP
        RSize = 16
+
+       wrapBlake2bNew256 = "blake2b.New256"
+       wrapHashWrite     = "hash.Write"
 )
 
 var (
@@ -60,16 +63,22 @@ func Encode(r *[RSize]byte, in []byte) ([]byte, error) {
        copy(out, in)
        h, err := blake2b.New256(nil)
        if err != nil {
-               return nil, err
+               return nil, errors.Wrap(err, wrapBlake2bNew256)
+       }
+       if _, err = h.Write(r[:]); err != nil {
+               return nil, errors.Wrap(err, wrapHashWrite)
+       }
+       if _, err = h.Write(in); err != nil {
+               return nil, errors.Wrap(err, wrapHashWrite)
        }
-       h.Write(r[:])
-       h.Write(in)
        copy(out[len(in):], h.Sum(nil))
        chachaKey := new([32]byte)
        copy(chachaKey[:], r[:])
        chacha20.XORKeyStream(out, out, dummyNonce, chachaKey)
        h.Reset()
-       h.Write(out[:len(in)+32])
+       if _, err = h.Write(out[:len(in)+32]); err != nil {
+               return nil, errors.Wrap(err, wrapHashWrite)
+       }
        for i, b := range h.Sum(nil)[:RSize] {
                out[len(in)+32+i] = b ^ r[i]
        }
@@ -84,18 +93,24 @@ func Decode(in []byte) ([]byte, error) {
        }
        h, err := blake2b.New256(nil)
        if err != nil {
-               return nil, err
+               return nil, errors.Wrap(err, wrapBlake2bNew256)
+       }
+       if _, err = h.Write(in[:len(in)-RSize]); err != nil {
+               return nil, errors.Wrap(err, wrapHashWrite)
        }
-       h.Write(in[:len(in)-RSize])
        chachaKey := new([32]byte)
        for i, b := range h.Sum(nil)[:RSize] {
                chachaKey[i] = b ^ in[len(in)-RSize+i]
        }
        h.Reset()
-       h.Write(chachaKey[:RSize])
+       if _, err = h.Write(chachaKey[:RSize]); err != nil {
+               return nil, errors.Wrap(err, wrapHashWrite)
+       }
        out := make([]byte, len(in)-RSize)
        chacha20.XORKeyStream(out, in[:len(in)-RSize], dummyNonce, chachaKey)
-       h.Write(out[:len(out)-HSize])
+       if _, err = h.Write(out[:len(out)-HSize]); err != nil {
+               return nil, errors.Wrap(err, wrapHashWrite)
+       }
        if subtle.ConstantTimeCompare(h.Sum(nil), out[len(out)-HSize:]) != 1 {
                return nil, errors.New("Invalid checksum")
        }
index 7f031c31556c39ccba816e2d7b5409f2eb5f6343..fc4feb83a2650553db7d14a66f24c9f2f5b34033 100644 (file)
@@ -44,9 +44,9 @@ package cnw
 import (
        "crypto/subtle"
        "encoding/binary"
-       "errors"
 
        "chacha20"
+       "github.com/pkg/errors"
        "golang.org/x/crypto/poly1305"
 )
 
index c0998f25e53ae6a93d871fe2e782ca73660e5e54..2f73631f4e54c77adf38ccd679fa3ab32f7f4365 100644 (file)
@@ -1,6 +1,6 @@
 /*
 GoVPN -- simple secure free software virtual private network daemon
-Copyright (C) 2014-2017 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2014-2016 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -21,6 +21,8 @@ package govpn
 import (
        "io"
 
+       "github.com/pkg/errors"
+
        "cypherpunks.ru/govpn/aont"
        "cypherpunks.ru/govpn/cnw"
 )
@@ -38,11 +40,11 @@ func EnclessEncode(authKey *[32]byte, nonce *[16]byte, in []byte) ([]byte, error
        r := new([aont.RSize]byte)
        var err error
        if _, err = io.ReadFull(Rand, r[:]); err != nil {
-               return nil, err
+               return nil, errors.Wrapf(err, wrapIoReadFull, "Rand")
        }
        aonted, err := aont.Encode(r, in)
        if err != nil {
-               return nil, err
+               return nil, errors.Wrap(err, "aont.Encode")
        }
        out := append(
                cnw.Chaff(authKey, nonce[8:], aonted[:aont.RSize]),
@@ -59,14 +61,14 @@ func EnclessDecode(authKey *[32]byte, nonce *[16]byte, in []byte) ([]byte, error
                authKey, nonce[8:], in[:aont.RSize*cnw.EnlargeFactor],
        )
        if err != nil {
-               return nil, err
+               return nil, errors.Wrap(err, "cnw.Winnow")
        }
        out, err := aont.Decode(append(
                winnowed, in[aont.RSize*cnw.EnlargeFactor:]...,
        ))
        SliceZero(winnowed)
        if err != nil {
-               return nil, err
+               return nil, errors.Wrap(err, "aont.Decode")
        }
        return out, nil
 }
index 03d4554fce9a5973744684646a74645c5d09c772..15745df7964cbdd9f84257f9bc7c20e6ac0bcdc0 100644 (file)
@@ -3,6 +3,19 @@
 /*
 GoVPN -- simple secure free software virtual private network daemon
 Copyright (C) 2014-2017 Sergey Matveev <stargrave@stargrave.org>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 package govpn
@@ -11,8 +24,11 @@ import (
        "io"
        "os"
        "path"
+
+       "github.com/pkg/errors"
 )
 
-func newTAPer(ifaceName string) (io.ReadWriter, error) {
-       return os.OpenFile(path.Join("/dev/", ifaceName), os.O_RDWR, os.ModePerm)
+func newTAPer(ifaceName string) (io.ReadWriteCloser, error) {
+       output, err := os.OpenFile(path.Join("/dev/", ifaceName), os.O_RDWR, os.ModePerm)
+       return output, errors.Wrap(err, "os.OpenFile")
 }