Debian Codegraph @

hc256/hc256.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 hc256 implements the stream cipher
5 // HC-256 designed by Hongjun Wu.
6 package hc256 // import "github.com/enceve/crypto/hc256"
7
8 import "crypto/cipher"
9
10 const (
11 mod1024 uint32 = 0x3FF
12 mod2048 uint32 = 0x7FF
13 )
14
15 // NewCipher returns a new cipher.Stream implementing the
16 // HC-256 cipher with the given key and nonce.
17 func NewCipher(nonce, key *[32]byte) cipher.Stream {
18 c := new(streamCipher)
19 initialize(nonce, key, &(c.p), &(c.q))
20 return c
21 }
22
23 type streamCipher struct {
24 p, q [1024]uint32
25 ctr uint32
26 keyStream [4]byte
27 off int
28 }
29
30 func initialize(nonce, key *[32]byte, p, q *[1024]uint32) {
31 var tmp [2560]uint32
32
33 tmp[0] = uint32(key[0]) | (uint32(key[1]) << 8) | (uint32(key[2]) << 16) | (uint32(key[3]) << 24)
34 tmp[1] = uint32(key[4]) | (uint32(key[5]) << 8) | (uint32(key[6]) << 16) | (uint32(key[7]) << 24)
35 tmp[2] = uint32(key[8]) | (uint32(key[9]) << 8) | (uint32(key[10]) << 16) | (uint32(key[11]) << 24)
36 tmp[3] = uint32(key[12]) | (uint32(key[13]) << 8) | (uint32(key[14]) << 16) | (uint32(key[15]) << 24)
37 tmp[4] = uint32(key[16]) | (uint32(key[17]) << 8) | (uint32(key[18]) << 16) | (uint32(key[19]) << 24)
38 tmp[5] = uint32(key[20]) | (uint32(key[21]) << 8) | (uint32(key[22]) << 16) | (uint32(key[23]) << 24)
39 tmp[6] = uint32(key[24]) | (uint32(key[25]) << 8) | (uint32(key[26]) << 16) | (uint32(key[27]) << 24)
40 tmp[7] = uint32(key[28]) | (uint32(key[29]) << 8) | (uint32(key[30]) << 16) | (uint32(key[31]) << 24)
41
42 tmp[8] = uint32(nonce[0]) | (uint32(nonce[1]) << 8) | (uint32(nonce[2]) << 16) | (uint32(nonce[3]) << 24)
43 tmp[9] = uint32(nonce[4]) | (uint32(nonce[5]) << 8) | (uint32(nonce[6]) << 16) | (uint32(nonce[7]) << 24)
44 tmp[10] = uint32(nonce[8]) | (uint32(nonce[9]) << 8) | (uint32(nonce[10]) << 16) | (uint32(nonce[11]) << 24)
45 tmp[11] = uint32(nonce[12]) | (uint32(nonce[13]) << 8) | (uint32(nonce[14]) << 16) | (uint32(nonce[15]) << 24)
46 tmp[12] = uint32(nonce[16]) | (uint32(nonce[17]) << 8) | (uint32(nonce[18]) << 16) | (uint32(nonce[19]) << 24)
47 tmp[13] = uint32(nonce[20]) | (uint32(nonce[21]) << 8) | (uint32(nonce[22]) << 16) | (uint32(nonce[23]) << 24)
48 tmp[14] = uint32(nonce[24]) | (uint32(nonce[25]) << 8) | (uint32(nonce[26]) << 16) | (uint32(nonce[27]) << 24)
49 tmp[15] = uint32(nonce[28]) | (uint32(nonce[29]) << 8) | (uint32(nonce[30]) << 16) | (uint32(nonce[31]) << 24)
50
51 // expand key and nonce with the f1 and f2 functions
52 // (2.2 https://eprint.iacr.org/2004/092.pdf)
53 var f2, f1 uint32
54 for i := 16; i < 2560; i++ {
55 f1, f2 = tmp[i-15], tmp[i-2]
56 f1 = ((f1 >> 7) | (f1 << 25)) ^ ((f1 >> 18) | (f1 << 14)) ^ (f1 >> 3)
57 f2 = ((f2 >> 17) | (f2 << 15)) ^ ((f2 >> 19) | (f2 << 13)) ^ (f2 >> 10)
58 tmp[i] = f1 + f2 + tmp[i-7] + tmp[i-16] + uint32(i)
59 }
60 copy(p[:], tmp[512:(512+1024)])
61 copy(q[:], tmp[1536:(1536+1024)])
62
63 // do 4096 iterations for initialization
64 var ctr uint32
65 for i := 0; i < 4096; i++ {
66 genKeyStream(&ctr, p, q)
67 }
68 }
69
70 func genKeyStream(counter *uint32, p, q *[1024]uint32) uint32 {
71 var r, t0, t1, t2, t3 uint32
72 ctr := *counter
73
74 j := ctr & mod1024
75 if ctr < 1024 {
76 t0 = p[(j-3)&mod1024]
77 t1 = p[(j-1023)&mod1024]
78 t2 = t0 ^ t1
79
80 t0 = (t0 >> 10) | (t0 << 22)
81 t1 = (t1 >> 23) | (t1 << 9)
82 t3 = t0 ^ t1
83 t0 = p[(j-10)&mod1024]
84 t1 = q[t2&mod1024]
85
86 p[j] += t0 + t1 + t3
87
88 t3 = p[(j-12)&mod1024]
89 t0 = 256 + ((t3 >> 8) & 0xff)
90 t1 = 512 + ((t3 >> 16) & 0xff)
91 t2 = 768 + ((t3 >> 24) & 0xff)
92 t3 = t3 & 0xff
93
94 r = q[t3] + q[t0] + q[t1] + q[t2] ^ p[j]
95 } else {
96 t0 = q[(j-3)&mod1024]
97 t1 = q[(j-1023)&mod1024]
98 t2 = t0 ^ t1
99
100 t0 = (t0 >> 10) | (t0 << 22)
101 t1 = (t1 >> 23) | (t1 << 9)
102 t3 = t0 ^ t1
103 t0 = q[(j-10)&mod1024]
104 t1 = p[t2&mod1024]
105
106 q[j] += t0 + t1 + t3
107
108 t3 = q[(j-12)&mod1024]
109 t0 = 256 + ((t3 >> 8) & 0xff)
110 t1 = 512 + ((t3 >> 16) & 0xff)
111 t2 = 768 + ((t3 >> 24) & 0xff)
112 t3 = t3 & 0xff
113
114 r = p[t3] + p[t0] + p[t1] + p[t2] ^ q[j]
115 }
116
117 *counter = (ctr + 1) & mod2048
118 return r
119 }