Debian Codegraph @

hc128/hc128.go

raw symbols SCIP
1 // Use of this source code is governed by a license
2 // that can be found in the LICENSE file.
3
4 // Package hc128 implements the stream cipher
5 // HC-128 from the eSTREAM portfolio (software)
6 // designed by Hongjun Wu.
7 package hc128 // import "github.com/enceve/crypto/hc128"
8
9 import "crypto/cipher"
10
11 const (
12 mod512 uint32 = 0x1FF
13 mod1024 uint32 = 0x3FF
14 )
15
16 // NewCipher returns a new cipher.Stream implementing the
17 // HC-128 cipher with the given key and nonce.
18 func NewCipher(nonce, key *[16]byte) cipher.Stream {
19 c := new(streamCipher)
20 initialize(nonce, key, &(c.p), &(c.q))
21 return c
22 }
23
24 type streamCipher struct {
25 p, q [512]uint32
26 ctr uint32
27 keyStream [4]byte
28 off int
29 }
30
31 func initialize(nonce, key *[16]byte, p, q *[512]uint32) {
32 var tmp [1280]uint32
33
34 tmp[0] = uint32(key[0]) | (uint32(key[1]) << 8) | (uint32(key[2]) << 16) | (uint32(key[3]) << 24)
35 tmp[1] = uint32(key[4]) | (uint32(key[5]) << 8) | (uint32(key[6]) << 16) | (uint32(key[7]) << 24)
36 tmp[2] = uint32(key[8]) | (uint32(key[9]) << 8) | (uint32(key[10]) << 16) | (uint32(key[11]) << 24)
37 tmp[3] = uint32(key[12]) | (uint32(key[13]) << 8) | (uint32(key[14]) << 16) | (uint32(key[15]) << 24)
38 copy(tmp[4:8], tmp[0:4])
39
40 tmp[8] = uint32(nonce[0]) | (uint32(nonce[1]) << 8) | (uint32(nonce[2]) << 16) | (uint32(nonce[3]) << 24)
41 tmp[9] = uint32(nonce[4]) | (uint32(nonce[5]) << 8) | (uint32(nonce[6]) << 16) | (uint32(nonce[7]) << 24)
42 tmp[10] = uint32(nonce[8]) | (uint32(nonce[9]) << 8) | (uint32(nonce[10]) << 16) | (uint32(nonce[11]) << 24)
43 tmp[11] = uint32(nonce[12]) | (uint32(nonce[13]) << 8) | (uint32(nonce[14]) << 16) | (uint32(nonce[15]) << 24)
44 copy(tmp[12:16], tmp[8:12])
45
46 // expand key and nonce with the f1 and f2 functions
47 // (2.2 http://www.ecrypt.eu.org/stream/p3ciphers/hc/hc128_p3.pdf)
48 var f2, f1 uint32
49 for i := 16; i < 1280; i++ {
50 f1, f2 = tmp[i-15], tmp[i-2]
51 f1 = ((f1 >> 7) | (f1 << 25)) ^ ((f1 >> 18) | (f1 << 14)) ^ (f1 >> 3)
52 f2 = ((f2 >> 17) | (f2 << 15)) ^ ((f2 >> 19) | (f2 << 13)) ^ (f2 >> 10)
53 tmp[i] = f1 + f2 + tmp[i-7] + tmp[i-16] + uint32(i)
54 }
55 copy(p[:], tmp[256:(256+512)])
56 copy(q[:], tmp[768:(768+512)])
57
58 // do 1024 iterations for initialization
59 var ctr uint32
60 for i := range p {
61 p[i] = genKeyStream(&ctr, p, q)
62 }
63 for i := range q {
64 q[i] = genKeyStream(&ctr, p, q)
65 }
66 }
67
68 func genKeyStream(counter *uint32, p, q *[512]uint32) uint32 {
69 var r, t0, t1, t2, t3 uint32
70 ctr := *counter
71
72 j := ctr & mod512
73 if ctr < 512 {
74 t0 = p[(j-3)&mod512]
75 t1 = p[(j-10)&mod512]
76 t2 = p[(j-511)&mod512]
77 t3 = p[(j-12)&mod512]
78
79 t0 = ((t0 >> 10) | (t0 << 22))
80 t1 = ((t1 >> 8) | (t1 << 24))
81 t2 = ((t2 >> 23) | (t2 << 9))
82 p[j] += (t0 ^ t2) + t1
83
84 t0 = t3 & 0xff
85 t1 = 256 + (t3>>16)&0xff
86 r = (q[t0] + q[t1]) ^ p[j]
87 } else {
88 t0 = q[(j-3)&mod512]
89 t1 = q[(j-10)&mod512]
90 t2 = q[(j-511)&mod512]
91 t3 = q[(j-12)&mod512]
92
93 t0 = ((t0 << 10) | (t0 >> 22))
94 t1 = ((t1 << 8) | (t1 >> 24))
95 t2 = ((t2 << 23) | (t2 >> 9))
96 q[j] += (t0 ^ t2) + t1
97
98 t0 = t3 & 0xff
99 t1 = 256 + (t3>>16)&0xff
100 r = p[t0] + p[t1] ^ q[j]
101 }
102
103 *counter = (ctr + 1) & mod1024
104 return r
105 }