Debian Codegraph @

cipher/eax.go

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