| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 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 |
| 21 |
hTag = 0x1 |
| 22 |
cTag = 0x2 |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
type eaxCipher struct { |
| 27 |
blockCipher cipher.Block |
| 28 |
ctr, block []byte |
| 29 |
mac hash.Hash |
| 30 |
size int |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 86 |
n := len(plaintext) |
| 87 |
copy(c.ctr, authNonce) |
| 88 |
c.ctrCrypt(dst, plaintext) |
| 89 |
|
| 90 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 149 |
n := len(ciphertext) |
| 150 |
copy(c.ctr, authNonce) |
| 151 |
c.ctrCrypt(dst, ciphertext) |
| 152 |
|
| 153 |
return dst[:n], nil |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 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 |
|
| 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 |
|
| 181 |
} |