/
eax.go
181 lines (153 loc) · 4.32 KB
/
eax.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Use of this source code is governed by a license
// that can be found in the LICENSE file.
// Package cipher implements additional block cipher modes
// that can be wrapped around low-level block cipher implementations.
// For standard block cipher modes see: https://golang.org/pkg/crypto/cipher
package cipher
import (
"crypto/cipher"
"crypto/subtle"
"errors"
"hash"
"github.com/enceve/crypto"
"github.com/enceve/crypto/cmac"
)
const (
nTag = 0x0 // The nonce tag constant
hTag = 0x1 // The additional data tag constant
cTag = 0x2 // The ciphertext tag constant
)
// The EAX cipher
type eaxCipher struct {
blockCipher cipher.Block
ctr, block []byte
mac hash.Hash
size int
}
// NewEAX returns a cipher.AEAD wrapping the cipher.Block.
// EAX is a two pass-scheme AEAD cipher with provable security.
// For authentication EAX uses CMac (OMAC1).
// The tagsize argument specifies the number of bytes of the auth. tag
// and must be between 1 and the block size of the cipher.
// This function returns a non-nil error if the given block cipher
// is not supported by CMac (see crypto/cmac for details)
func NewEAX(c cipher.Block, tagsize int) (cipher.AEAD, error) {
m, err := cmac.New(c)
if err != nil {
return nil, err
}
if tagsize < 1 || tagsize > c.BlockSize() {
return nil, errors.New("tagSize must between 1 and BlockSize() of the given cipher")
}
return &eaxCipher{
blockCipher: c,
mac: m,
ctr: make([]byte, c.BlockSize()),
block: make([]byte, c.BlockSize()),
size: tagsize,
}, nil
}
func (c *eaxCipher) NonceSize() int { return c.blockCipher.BlockSize() }
func (c *eaxCipher) Overhead() int { return c.size }
func (c *eaxCipher) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
if n := len(nonce); n != c.blockCipher.BlockSize() {
panic(crypto.NonceSizeError(n))
}
if len(dst) < len(plaintext) {
panic("dst buffer to small")
}
tag := make([]byte, c.mac.BlockSize())
// process nonce
tag[len(tag)-1] = nTag
c.mac.Write(tag)
c.mac.Write(nonce)
authNonce := c.mac.Sum(nil)
c.mac.Reset()
// process additional data
tag[len(tag)-1] = hTag
c.mac.Write(tag)
c.mac.Write(additionalData)
authData := c.mac.Sum(nil)
c.mac.Reset()
// encrypt
n := len(plaintext)
copy(c.ctr, authNonce) // set the ctr-mode nonce
c.ctrCrypt(dst, plaintext)
// process ciphertext
tag[len(tag)-1] = cTag
c.mac.Write(tag)
c.mac.Write(dst[:n])
tag = c.mac.Sum(tag[:0])
c.mac.Reset()
for i := range tag {
tag[i] ^= authData[i] ^ authNonce[i]
}
return append(dst[:n], tag[:c.size]...)
}
func (c *eaxCipher) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
if n := len(nonce); n != c.blockCipher.BlockSize() {
return nil, crypto.NonceSizeError(n)
}
if len(ciphertext) < c.size {
return nil, crypto.AuthenticationError{}
}
if len(dst) < len(ciphertext)-c.mac.Size() {
panic("dst buffer to small")
}
hash := ciphertext[len(ciphertext)-c.size:]
ciphertext = ciphertext[:len(ciphertext)-c.size]
tag := make([]byte, c.mac.BlockSize())
// process nonce
tag[len(tag)-1] = nTag
c.mac.Write(tag)
c.mac.Write(nonce)
authNonce := c.mac.Sum(nil)
c.mac.Reset()
// process additional data
tag[len(tag)-1] = hTag
c.mac.Write(tag)
c.mac.Write(additionalData)
authData := c.mac.Sum(nil)
c.mac.Reset()
// process ciphertext
tag[len(tag)-1] = cTag
c.mac.Write(tag)
c.mac.Write(ciphertext)
tag = c.mac.Sum(tag[:0])
c.mac.Reset()
for i := range tag {
tag[i] ^= authData[i] ^ authNonce[i]
}
if subtle.ConstantTimeCompare(tag[:c.size], hash) != 1 {
return nil, crypto.AuthenticationError{}
}
// decrypt
n := len(ciphertext)
copy(c.ctr, authNonce) // set the ctr-mode nonce
c.ctrCrypt(dst, ciphertext)
return dst[:n], nil
}
// ctrCrypt encrypts the bytes in src with the CTR mode and writes
// the ciphertext into dst
func (c *eaxCipher) ctrCrypt(dst, src []byte) {
length := len(src)
bs := c.blockCipher.BlockSize()
n := length & (^(length - bs))
for i := 0; i < n; i += bs {
j := i + bs
c.blockCipher.Encrypt(c.block, c.ctr)
crypto.XOR(dst[i:j], src[i:j], c.block)
// Increment counter
for k := len(c.ctr) - 1; k >= 0; k-- {
c.ctr[k]++
if c.ctr[k] != 0 {
break
}
}
}
if n < length {
c.blockCipher.Encrypt(c.block, c.ctr)
crypto.XOR(dst[n:], src[n:], c.block)
}
// no reset of ctr needed - Seal or Open does this for us
}