]> Cypherpunks.ru repositories - govpn.git/blob - src/govpn/aont/oaep.go
cea5833fa906cd9dd28ab0ac145424a39fea5b86
[govpn.git] / src / govpn / aont / oaep.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2016 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 // 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 lengths, 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 = HKDF(BLAKE2b, r) XOR (M || BLAKE2b(r || M)) ||
34 //      P2 = BLAKE2b(P1) XOR r
35 package aont
36
37 import (
38         "crypto/subtle"
39         "errors"
40
41         "github.com/dchest/blake2b"
42         "golang.org/x/crypto/hkdf"
43 )
44
45 const (
46         HSize = 32
47         RSize = 16
48 )
49
50 // Encode the data, produce AONT package. Data size will be larger than
51 // the original one for 48 bytes.
52 func Encode(r *[RSize]byte, in []byte) ([]byte, error) {
53         out := make([]byte, len(in)+HSize+RSize)
54         hr := hkdf.New(blake2b.New512, r[:], nil, nil)
55         if _, err := hr.Read(out[:len(in)+HSize]); err != nil {
56                 return nil, err
57         }
58         var i int
59         for i = 0; i < len(in); i++ {
60                 out[i] ^= in[i]
61         }
62         h := blake2b.New256()
63         h.Write(r[:])
64         h.Write(in)
65         for _, b := range h.Sum(nil) {
66                 out[i] ^= b
67                 i++
68         }
69         h.Reset()
70         h.Write(out[:i])
71         for _, b := range h.Sum(nil)[:RSize] {
72                 out[i] = b ^ r[i-len(in)-HSize]
73                 i++
74         }
75         return out, nil
76 }
77
78 // Decode the data from AONT package. Data size will be smaller than the
79 // original one for 48 bytes.
80 func Decode(in []byte) ([]byte, error) {
81         if len(in) < HSize+RSize {
82                 return nil, errors.New("Too small input buffer")
83         }
84         h := blake2b.New256()
85         h.Write(in[:len(in)-RSize])
86         out := make([]byte, len(in)-RSize)
87         for i, b := range h.Sum(nil)[:RSize] {
88                 out[i] = b ^ in[len(in)-RSize+i]
89         }
90         h.Reset()
91         h.Write(out[:RSize])
92         hr := hkdf.New(blake2b.New512, out[:RSize], nil, nil)
93         if _, err := hr.Read(out); err != nil {
94                 return nil, err
95         }
96         for i := 0; i < len(out); i++ {
97                 out[i] ^= in[i]
98         }
99         h.Write(out[:len(out)-HSize])
100         if subtle.ConstantTimeCompare(h.Sum(nil), out[len(out)-HSize:]) != 1 {
101                 return nil, errors.New("Invalid checksum")
102         }
103         return out[:len(out)-HSize], nil
104 }