-
Notifications
You must be signed in to change notification settings - Fork 1
/
aes_siv_block.go
70 lines (59 loc) · 1.71 KB
/
aes_siv_block.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
package siv
import (
"crypto/aes"
"crypto/cipher"
"errors"
"github.com/aead/cmac"
)
var (
//ErrAesSIVinitKeySize indicates that the key supplied to create an AesSIVBlockPair is in unsupported
ErrAesSIVinitKeySize = errors.New("Aes SIV Init error: Key size is unsupported")
//ErrAesSIVinitGeneric indicates that an error occurred initiating an AesSIVBlockPair
ErrAesSIVinitGeneric = errors.New("AES SIV Init error: Initializing AES SIV block failed")
)
type aesSIVBlockPair struct {
Cmacblock cipher.Block
Ctrblock cipher.Block
}
//NewAesSIVBlockPair function takes a 256, 384 or 512 bit key and returns an AesSIVBlockPair.
func NewAesSIVBlockPair(key []byte) (*aesSIVBlockPair, error) {
switch len(key) {
default:
return nil, ErrAesSIVinitKeySize
case 32, 48, 64:
break
}
cmac, ciphererr := aes.NewCipher(key[:((len(key)) / 2)])
if ciphererr != nil {
return nil, ErrAesSIVinitGeneric
}
ctr, ciphererr := aes.NewCipher(key[((len(key)) / 2):])
if ciphererr != nil {
return nil, ErrAesSIVinitGeneric
}
return &aesSIVBlockPair{
Cmacblock: cmac,
Ctrblock: ctr,
}, nil
}
func (aespair *aesSIVBlockPair) CMACBlockSize() int {
return aespair.Cmacblock.BlockSize()
}
func (aespair *aesSIVBlockPair) CTRBlockSize() int {
return aespair.Ctrblock.BlockSize()
}
func (aespair *aesSIVBlockPair) Cmac(src []byte) ([]byte, error) {
return cmac.Sum(src, aespair.Cmacblock, aespair.CMACBlockSize())
}
func (aespair *aesSIVBlockPair) Ctr(dst, iv, src []byte) []byte {
stream := cipher.NewCTR(aespair.Ctrblock, iv)
stream.XORKeyStream(dst, src)
return dst
}
var _ sivAble = (*aesSIVBlockPair)(nil)
func (aespair *aesSIVBlockPair) newSIV() (SIV, error) {
siv := &aesSiv{
sivBlockPair: aespair,
}
return siv, nil
}