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