forked from pbergman/go-smb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbc_mac.go
54 lines (45 loc) · 773 Bytes
/
cbc_mac.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
package ccm
import (
"crypto/cipher"
)
// CBC-MAC implementation
type mac struct {
ci []byte
p int
c cipher.Block
}
func newMAC(c cipher.Block) *mac {
return &mac{
c: c,
ci: make([]byte, c.BlockSize()),
}
}
func (m *mac) Reset() {
for i := range m.ci {
m.ci[i] = 0
}
m.p = 0
}
func (m *mac) Write(p []byte) (n int, err error) {
for _, c := range p {
if m.p >= len(m.ci) {
m.c.Encrypt(m.ci, m.ci)
m.p = 0
}
m.ci[m.p] ^= c
m.p++
}
return len(p), nil
}
// PadZero emulates zero byte padding.
func (m *mac) PadZero() {
if m.p != 0 {
m.c.Encrypt(m.ci, m.ci)
m.p = 0
}
}
func (m *mac) Sum(in []byte) []byte {
return append(in, m.ci...)
}
func (m *mac) Size() int { return len(m.ci) }
func (m *mac) BlockSize() int { return 16 }