/
ascon.go
179 lines (140 loc) · 3.91 KB
/
ascon.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
// Package ascon implements the ASCON AEAD cipher.
package ascon
import (
"errors"
"runtime"
"strconv"
"crypto/subtle"
"crypto/cipher"
"github.com/deatil/go-cryptobin/tool/alias"
)
const (
iv128 uint64 = 0x80400c0600000000 // Ascon-128
iv128a uint64 = 0x80800c0800000000 // Ascon-128a
)
const (
// BlockSize128a is the size in bytes of an ASCON-128a block.
BlockSize128a = 16
// BlockSize128 is the size in bytes of an ASCON-128 block.
BlockSize128 = 8
// KeySize is the size in bytes of ASCON-128 and ASCON-128a
// keys.
KeySize = 16
// NonceSize is the size in bytes of ASCON-128 and ASCON-128a
// nonces.
NonceSize = 16
// TagSize is the size in bytes of ASCON-128 and ASCON-128a
// authenticators.
TagSize = 16
)
var errOpen = errors.New("cryptobin/ascon: message authentication failed")
type ascon struct {
k0, k1 uint64
iv uint64
}
//
// References:
//
// [ascon]: https://ascon.iaik.tugraz.at
//
// NewCipher creates a 128-bit ASCON-128 AEAD.
func NewCipher(key []byte) (cipher.AEAD, error) {
if len(key) != KeySize {
return nil, errors.New("cryptobin/ascon: bad key length")
}
return &ascon{
k0: getu64(key[0:]),
k1: getu64(key[8:]),
iv: iv128,
}, nil
}
// NewCiphera creates a 128-bit ASCON-128a AEAD.
func NewCiphera(key []byte) (cipher.AEAD, error) {
if len(key) != KeySize {
return nil, errors.New("cryptobin/ascon: bad key length")
}
return &ascon{
k0: getu64(key[0:]),
k1: getu64(key[8:]),
iv: iv128a,
}, nil
}
func (a *ascon) NonceSize() int {
return NonceSize
}
func (a *ascon) Overhead() int {
return TagSize
}
func (a *ascon) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
if len(nonce) != NonceSize {
panic("cryptobin/ascon: incorrect nonce length: " + strconv.Itoa(len(nonce)))
}
n0 := getu64(nonce[0:])
n1 := getu64(nonce[8:])
var s state
s.init(a.iv, a.k0, a.k1, n0, n1)
if a.iv == iv128a {
s.additionalData128a(additionalData)
} else {
s.additionalData128(additionalData)
}
ret, out := alias.SliceForAppend(dst, len(plaintext)+TagSize)
if alias.InexactOverlap(out, plaintext) {
panic("cryptobin/ascon: invalid buffer overlap")
}
if a.iv == iv128a {
s.encrypt128a(out[:len(plaintext)], plaintext)
} else {
s.encrypt128(out[:len(plaintext)], plaintext)
}
if a.iv == iv128a {
s.finalize128a(a.k0, a.k1)
} else {
s.finalize128(a.k0, a.k1)
}
s.tag(out[len(out)-TagSize:])
return ret
}
func (a *ascon) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
if len(nonce) != NonceSize {
panic("cryptobin/ascon: incorrect nonce length: " + strconv.Itoa(len(nonce)))
}
if len(ciphertext) < TagSize {
return nil, errOpen
}
tag := ciphertext[len(ciphertext)-TagSize:]
ciphertext = ciphertext[:len(ciphertext)-TagSize]
n0 := getu64(nonce[0:])
n1 := getu64(nonce[8:])
var s state
s.init(a.iv, a.k0, a.k1, n0, n1)
if a.iv == iv128a {
s.additionalData128a(additionalData)
} else {
s.additionalData128(additionalData)
}
ret, out := alias.SliceForAppend(dst, len(ciphertext))
if alias.InexactOverlap(out, ciphertext) {
panic("cryptobin/ascon: invalid buffer overlap")
}
if a.iv == iv128a {
s.decrypt128a(out, ciphertext)
} else {
s.decrypt128(out, ciphertext)
}
if a.iv == iv128a {
s.finalize128a(a.k0, a.k1)
} else {
s.finalize128(a.k0, a.k1)
}
expectedTag := make([]byte, TagSize)
s.tag(expectedTag)
if subtle.ConstantTimeCompare(expectedTag, tag) != 1 {
for i := range out {
out[i] = 0
}
runtime.KeepAlive(out)
return nil, errOpen
}
return ret, nil
}