-
Notifications
You must be signed in to change notification settings - Fork 61
/
eea.go
59 lines (52 loc) · 1.61 KB
/
eea.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
package zuc
import (
"crypto/cipher"
"encoding/binary"
"github.com/emmansun/gmsm/internal/alias"
"github.com/emmansun/gmsm/internal/subtle"
)
const RoundWords = 32
// NewCipher create a stream cipher based on key and iv aguments.
func NewCipher(key, iv []byte) (cipher.Stream, error) {
return newZUCState(key, iv)
}
// NewEEACipher create a stream cipher based on key, count, bearer and direction arguments according specification.
func NewEEACipher(key []byte, count, bearer, direction uint32) (cipher.Stream, error) {
iv := make([]byte, 16)
binary.BigEndian.PutUint32(iv, count)
copy(iv[8:12], iv[:4])
iv[4] = byte(((bearer << 1) | (direction & 1)) << 2)
iv[12] = iv[4]
return newZUCState(key, iv)
}
func xorKeyStreamGeneric(c *zucState32, dst, src []byte) {
words := (len(src) + 3) / 4
rounds := words / RoundWords
var keyWords [RoundWords]uint32
var keyBytes [RoundWords * 4]byte
for i := 0; i < rounds; i++ {
c.genKeywords(keyWords[:])
for j := 0; j < RoundWords; j++ {
binary.BigEndian.PutUint32(keyBytes[j*4:], keyWords[j])
}
subtle.XORBytes(dst, src, keyBytes[:])
dst = dst[RoundWords*4:]
src = src[RoundWords*4:]
}
if rounds*RoundWords < words {
c.genKeywords(keyWords[:words-rounds*RoundWords])
for j := 0; j < words-rounds*RoundWords; j++ {
binary.BigEndian.PutUint32(keyBytes[j*4:], keyWords[j])
}
subtle.XORBytes(dst, src, keyBytes[:])
}
}
func (c *zucState32) XORKeyStream(dst, src []byte) {
if len(dst) < len(src) {
panic("zuc: output smaller than input")
}
if alias.InexactOverlap(dst[:len(src)], src) {
panic("zuc: invalid buffer overlap")
}
xorKeyStream(c, dst, src)
}