-
Notifications
You must be signed in to change notification settings - Fork 1
/
s2v.go
48 lines (43 loc) · 1.06 KB
/
s2v.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
package siv
import (
"errors"
)
var (
zero = []byte{0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00}
one = []byte{0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01}
)
var (
//ErrAesSIVs2v indicates that the AesSIVs2v routine failed
ErrAesSIVs2v = errors.New("AES SIV s2v error: s2v routine failed")
)
func s2v(sivpair sivBlockPair, plaintext []byte, additionalData ...[]byte) ([]byte, error) {
if len(plaintext) == 0 && len(additionalData) == 0 {
return sivpair.Cmac(one)
}
d, cmacErr := sivpair.Cmac(zero)
if cmacErr != nil {
return nil, ErrAesSIVs2v
}
for _, ad := range additionalData {
mac, macErr := sivpair.Cmac(ad)
if macErr != nil {
return nil, ErrAesSIVs2v
}
xor(d, dbl(d), mac)
}
var t []byte
if len(plaintext) >= sivpair.CMACBlockSize() {
t = make([]byte, len(plaintext))
xorend(t, plaintext, d)
} else {
t = make([]byte, sivpair.CMACBlockSize())
xor(t, dbl(d), pad(plaintext, sivpair.CMACBlockSize()))
}
return sivpair.Cmac(t)
}