-
Notifications
You must be signed in to change notification settings - Fork 3
/
types.go
202 lines (187 loc) · 4.64 KB
/
types.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package go_frodokem
import (
"crypto/rand"
"golang.org/x/crypto/sha3"
)
var variants = []FrodoKEM{
Frodo640AES(), Frodo640SHAKE(),
Frodo976AES(), Frodo976SHAKE(),
Frodo1344AES(), Frodo1344SHAKE(),
}
// Returns all the FrodoKEM variants supported as an array
func Variants() []FrodoKEM {
return variants
}
type FrodoKEM struct {
// error_distribution
name string
errDistribution []uint16
tChi []uint16
d int
q uint16
n int
nBar int
mBar int
b int
lenSeedA int
lenZ int
lenMu int
lenSeedSE int
lenS int
lenK int
lenPkh int
lenSS int
lenChi int
lenSkBytes int
lenPkBytes int
lenCtBytes int
shake func(msg []byte, digestLength int) []byte
gen func([]byte) [][]uint16
rng func([]byte)
}
// Returns a new FrodoKEM 640 AES variant (Generates 128 bits of secret)
func Frodo640AES() (f FrodoKEM) {
f = FrodoKEM{
name: "Frodo640AES",
errDistribution: []uint16{9288, 8720, 7216, 5264, 3384, 1918, 958, 422, 164, 56, 17, 4, 1},
d: 15,
q: 32768,
n: 640,
nBar: 8,
mBar: 8,
b: 2,
lenSeedA: 128,
lenZ: 128,
lenMu: 128,
lenSeedSE: 128,
lenS: 128,
lenK: 128,
lenPkh: 128,
lenSS: 128,
lenChi: 16,
lenSkBytes: 19888,
lenPkBytes: 9616,
lenCtBytes: 9720,
shake: shake128,
rng: cryptoRand,
}
f.tChi = cdfZeroCentredSymmetric(f.errDistribution)
f.gen = f.genAES128
return
}
// Returns a new FrodoKEM 640 SHAKE variant (Generates 128 bits of secret)
func Frodo640SHAKE() (f FrodoKEM) {
f = Frodo640AES()
f.name = "Frodo640Shake"
f.shake = shake128
f.gen = f.genSHAKE128
return
}
// Returns a new FrodoKEM 976 AES variant (Generates 192 bits of secret)
func Frodo976AES() (f FrodoKEM) {
f = FrodoKEM{
name: "Frodo976AES",
errDistribution: []uint16{11278, 10277, 7774, 4882, 2545, 1101, 396, 118, 29, 6, 1},
d: 16,
q: 0, // means no mod in 16 bits uint
n: 976,
nBar: 8,
mBar: 8,
b: 3,
lenSeedA: 128,
lenZ: 128,
lenMu: 192,
lenSeedSE: 192,
lenS: 192,
lenK: 192,
lenPkh: 192,
lenSS: 192,
lenChi: 16,
lenSkBytes: 31296,
lenPkBytes: 15632,
lenCtBytes: 15744,
shake: shake256,
rng: cryptoRand,
}
f.tChi = cdfZeroCentredSymmetric(f.errDistribution)
f.gen = f.genAES128
return
}
// Returns a new FrodoKEM 976 SHAKE variant (Generates 192 bits of secret)
func Frodo976SHAKE() (f FrodoKEM) {
f = Frodo976AES()
f.name = "Frodo976Shake"
f.gen = f.genSHAKE128
return
}
// Returns a new FrodoKEM 1344 AES variant (Generates 256 bits of secret)
func Frodo1344AES() (f FrodoKEM) {
f = FrodoKEM{
name: "Frodo1344AES",
errDistribution: []uint16{18286, 14320, 6876, 2023, 364, 40, 2},
d: 16,
q: 0,
n: 1344,
nBar: 8,
mBar: 8,
b: 4,
lenSeedA: 128,
lenZ: 128,
lenMu: 256,
lenSeedSE: 256,
lenS: 256,
lenK: 256,
lenPkh: 256,
lenSS: 256,
lenChi: 16,
lenSkBytes: 43088,
lenPkBytes: 21520,
lenCtBytes: 21632,
shake: shake256,
rng: cryptoRand,
}
f.tChi = cdfZeroCentredSymmetric(f.errDistribution)
f.gen = f.genAES128
return
}
// Returns a new FrodoKEM 1344 SHAKE variant (Generates 256 bits of secret)
func Frodo1344SHAKE() (f FrodoKEM) {
f = Frodo1344AES()
f.name = "Frodo1344Shake"
f.gen = f.genSHAKE128
return
}
func sumUint16s(arr []uint16) (r uint16) {
r = 0
for _, v := range arr {
r += v
}
return
}
func cdfZeroCentredSymmetric(chi []uint16) (tChi []uint16) {
tChi = make([]uint16, len(chi))
tChi[0] = (chi[0] / 2) - 1
for z := 1; z < len(chi); z++ {
tChi[z] = tChi[0] + sumUint16s(chi[1:z+1])
}
return
}
func cryptoRand(target []byte) {
n, err := rand.Read(target)
if err != nil {
panic(err)
}
if len(target) != n {
panic("could not generate enough randomness")
}
}
func shake128(msg []byte, size int) (hash []byte) {
hash = make([]byte, size)
sha3.ShakeSum128(hash, msg)
return
}
func shake256(msg []byte, size int) (hash []byte) {
hash = make([]byte, size)
sha3.ShakeSum256(hash, msg)
return
}