/*
GoVPN -- simple secure free software virtual private network daemon
-Copyright (C) 2014-2016 Sergey Matveev <stargrave@stargrave.org>
+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
package govpn
import (
+ "io"
+
+ "github.com/pkg/errors"
+
"cypherpunks.ru/govpn/aont"
"cypherpunks.ru/govpn/cnw"
)
-const (
- EnclessEnlargeSize = aont.HSize + aont.RSize*cnw.EnlargeFactor
-)
+// EnclessEnlargeSize is number of bytes overhead for each message
+const EnclessEnlargeSize = aont.HSize + aont.RSize*cnw.EnlargeFactor
-// Confidentiality preserving (but encryptionless) encoding.
+// EnclessEncode is encryptionless, but confidentiality preserving encoding.
//
// It uses Chaffing-and-Winnowing technology (it is neither
// encryption nor steganography) over All-Or-Nothing-Transformed data.
// nonce is 64-bit nonce. Output data will be EnclessEnlargeSize larger.
// It also consumes 64-bits of entropy.
-func EnclessEncode(authKey *[32]byte, nonce, in []byte) ([]byte, error) {
+func EnclessEncode(authKey *[32]byte, nonce *[16]byte, in []byte) ([]byte, error) {
r := new([aont.RSize]byte)
var err error
- if _, err = Rand.Read(r[:]); err != nil {
- return nil, err
+ if _, err = io.ReadFull(Rand, r[:]); err != nil {
+ 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, aonted[:aont.RSize]),
+ cnw.Chaff(authKey, nonce[8:], aonted[:aont.RSize]),
aonted[aont.RSize:]...,
)
SliceZero(aonted[:aont.RSize])
return out, nil
}
-// Decode EnclessEncode-ed data.
-func EnclessDecode(authKey *[32]byte, nonce, in []byte) ([]byte, error) {
+// EnclessDecode decodes EnclessEncode-ed data.
+func EnclessDecode(authKey *[32]byte, nonce *[16]byte, in []byte) ([]byte, error) {
var err error
winnowed, err := cnw.Winnow(
- authKey, nonce, in[:aont.RSize*cnw.EnlargeFactor],
+ 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
}