-
Notifications
You must be signed in to change notification settings - Fork 22
/
ascon.go
203 lines (168 loc) · 5.14 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Package ascon implements the ASCON AEAD cipher.
//
// References:
//
// [ascon]: https://ascon.iaik.tugraz.at
//
package ascon
import (
"errors"
"runtime"
"strconv"
"crypto/subtle"
"crypto/cipher"
"encoding/binary"
"github.com/deatil/go-cryptobin/tool/alias"
)
const (
iv128 uint64 = 0x80400c0600000000 // Ascon-128
iv128a uint64 = 0x80800c0800000000 // Ascon-128a
)
var errOpen = errors.New("ascon: message authentication failed")
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
)
type ascon struct {
k0, k1 uint64
iv uint64
}
var _ cipher.AEAD = (*ascon)(nil)
// New128 creates a 128-bit ASCON-128 AEAD.
//
// ASCON-128 provides lower throughput but increased robustness
// against partial or full state recovery compared to ASCON-128a.
//
// Each unique key can encrypt a maximum 2^68 bytes (i.e., 2^64
// plaintext and associated data blocks). Nonces must never be
// reused with the same key. Violating either of these
// constraints compromises the security of the algorithm.
//
// There are no other constraints on the composition of the
// nonce. For example, the nonce can be a counter.
//
// Refer to ASCON's documentation for more information.
func New128(key []byte) (cipher.AEAD, error) {
if len(key) != KeySize {
return nil, errors.New("cryptobin/ascon: bad key length")
}
return &ascon{
k0: binary.BigEndian.Uint64(key[0:]),
k1: binary.BigEndian.Uint64(key[8:]),
iv: iv128,
}, nil
}
// New128a creates a 128-bit ASCON-128a AEAD.
//
// ASCON-128a provides higher throughput but reduced robustness
// against partial or full state recovery compared to ASCON-128.
//
// Each unique key can encrypt a maximum 2^68 bytes (i.e., 2^64
// plaintext and associated data blocks). Nonces must never be
// reused with the same key. Violating either of these
// constraints compromises the security of the algorithm.
//
// There are no other constraints on the composition of the
// nonce. For example, the nonce can be a counter.
//
// Refer to ASCON's documentation for more information.
func New128a(key []byte) (cipher.AEAD, error) {
if len(key) != KeySize {
return nil, errors.New("cryptobin/ascon: bad key length")
}
return &ascon{
k0: binary.BigEndian.Uint64(key[0:]),
k1: binary.BigEndian.Uint64(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 := binary.BigEndian.Uint64(nonce[0:])
n1 := binary.BigEndian.Uint64(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 := binary.BigEndian.Uint64(nonce[0:])
n1 := binary.BigEndian.Uint64(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
}