]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/aont/oaep.go
wrap errors
[govpn.git] / src / cypherpunks.ru / govpn / aont / oaep.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2017 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 // Package aont stands for All-Or-Nothing-Transform, based on OAEP.
20 //
21 // This package implements OAEP (Optimal Asymmetric Encryption Padding)
22 // (http://cseweb.ucsd.edu/~mihir/papers/oaep.html)
23 // used there as All-Or-Nothing-Transformation
24 // (http://theory.lcs.mit.edu/~cis/pubs/rivest/fusion.ps).
25 // We do not fix OAEP parts length, instead we add hash-based
26 // checksum like in SAEP+
27 // (http://crypto.stanford.edu/~dabo/abstracts/saep.html).
28 //
29 // AONT takes 128-bit random r, data M to be encoded and produce the
30 // package PKG:
31 //
32 //     PKG = P1 || P2
33 //      P1 = ChaCha20(key=r, nonce=0x00, 0x00) XOR (M || BLAKE2b(r || M))
34 //      P2 = BLAKE2b(P1) XOR r
35 package aont
36
37 import (
38         "crypto/subtle"
39
40         "chacha20"
41         "github.com/pkg/errors"
42         "golang.org/x/crypto/blake2b"
43 )
44
45 const (
46         // HSize size of generated hash output in terms of OAEP
47         HSize = 32
48         // RSize size of generated random output in terms of OAEP
49         RSize = 16
50
51         wrapBlake2bNew256 = "blake2b.New256"
52         wrapHashWrite     = "hash.Write"
53 )
54
55 var (
56         dummyNonce = new([16]byte)
57 )
58
59 // Encode the data, produce AONT package. Data size will be larger than
60 // the original one for 48 bytes.
61 func Encode(r *[RSize]byte, in []byte) ([]byte, error) {
62         out := make([]byte, len(in)+HSize+RSize)
63         copy(out, in)
64         h, err := blake2b.New256(nil)
65         if err != nil {
66                 return nil, errors.Wrap(err, wrapBlake2bNew256)
67         }
68         if _, err = h.Write(r[:]); err != nil {
69                 return nil, errors.Wrap(err, wrapHashWrite)
70         }
71         if _, err = h.Write(in); err != nil {
72                 return nil, errors.Wrap(err, wrapHashWrite)
73         }
74         copy(out[len(in):], h.Sum(nil))
75         chachaKey := new([32]byte)
76         copy(chachaKey[:], r[:])
77         chacha20.XORKeyStream(out, out, dummyNonce, chachaKey)
78         h.Reset()
79         if _, err = h.Write(out[:len(in)+32]); err != nil {
80                 return nil, errors.Wrap(err, wrapHashWrite)
81         }
82         for i, b := range h.Sum(nil)[:RSize] {
83                 out[len(in)+32+i] = b ^ r[i]
84         }
85         return out, nil
86 }
87
88 // Decode the data from AONT package. Data size will be smaller than the
89 // original one for 48 bytes.
90 func Decode(in []byte) ([]byte, error) {
91         if len(in) < HSize+RSize {
92                 return nil, errors.New("Too small input buffer")
93         }
94         h, err := blake2b.New256(nil)
95         if err != nil {
96                 return nil, errors.Wrap(err, wrapBlake2bNew256)
97         }
98         if _, err = h.Write(in[:len(in)-RSize]); err != nil {
99                 return nil, errors.Wrap(err, wrapHashWrite)
100         }
101         chachaKey := new([32]byte)
102         for i, b := range h.Sum(nil)[:RSize] {
103                 chachaKey[i] = b ^ in[len(in)-RSize+i]
104         }
105         h.Reset()
106         if _, err = h.Write(chachaKey[:RSize]); err != nil {
107                 return nil, errors.Wrap(err, wrapHashWrite)
108         }
109         out := make([]byte, len(in)-RSize)
110         chacha20.XORKeyStream(out, in[:len(in)-RSize], dummyNonce, chachaKey)
111         if _, err = h.Write(out[:len(out)-HSize]); err != nil {
112                 return nil, errors.Wrap(err, wrapHashWrite)
113         }
114         if subtle.ConstantTimeCompare(h.Sum(nil), out[len(out)-HSize:]) != 1 {
115                 return nil, errors.New("Invalid checksum")
116         }
117         return out[:len(out)-HSize], nil
118 }