1// Copyright 2017 syzkaller project authors. All rights reserved.
2// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
3
4package linux
5
6import (
7	"math/rand"
8
9	"github.com/google/syzkaller/prog"
10)
11
12func (arch *arch) generateSockaddrAlg(g *prog.Gen, typ0 prog.Type, old prog.Arg) (
13	arg prog.Arg, calls []*prog.Call) {
14	typ := typ0.(*prog.StructType)
15	family := g.GenerateArg(typ.Fields[0], &calls)
16	// There is very little point in generating feat/mask,
17	// because that can only fail otherwise correct bind.
18	feat := prog.MakeConstArg(typ.Fields[2], 0)
19	mask := prog.MakeConstArg(typ.Fields[3], 0)
20	if g.NOutOf(1, 1000) {
21		feat = g.GenerateArg(typ.Fields[2], &calls).(*prog.ConstArg)
22		mask = g.GenerateArg(typ.Fields[3], &calls).(*prog.ConstArg)
23	}
24	algType, algName := generateAlgName(g.Rand())
25	// Extend/truncate type/name to their fixed sizes.
26	algTypeData := fixedSizeData(algType, typ.Fields[1].Size())
27	algNameData := fixedSizeData(algName, typ.Fields[4].Size())
28	arg = prog.MakeGroupArg(typ, []prog.Arg{
29		family,
30		prog.MakeDataArg(typ.Fields[1], algTypeData),
31		feat,
32		mask,
33		prog.MakeDataArg(typ.Fields[4], algNameData),
34	})
35	return
36}
37
38func (arch *arch) generateAlgName(g *prog.Gen, typ prog.Type, old prog.Arg) (
39	arg prog.Arg, calls []*prog.Call) {
40	return generateAlgNameStruct(g, typ, allTypes[g.Rand().Intn(len(allTypes))].typ)
41}
42
43func (arch *arch) generateAlgAeadName(g *prog.Gen, typ prog.Type, old prog.Arg) (
44	arg prog.Arg, calls []*prog.Call) {
45	return generateAlgNameStruct(g, typ, ALG_AEAD)
46}
47
48func (arch *arch) generateAlgHashName(g *prog.Gen, typ prog.Type, old prog.Arg) (
49	arg prog.Arg, calls []*prog.Call) {
50	return generateAlgNameStruct(g, typ, ALG_HASH)
51}
52
53func (arch *arch) generateAlgBlkcipherhName(g *prog.Gen, typ prog.Type, old prog.Arg) (
54	arg prog.Arg, calls []*prog.Call) {
55	return generateAlgNameStruct(g, typ, ALG_BLKCIPHER)
56}
57
58func generateAlgNameStruct(g *prog.Gen, typ0 prog.Type, algTyp int) (arg prog.Arg, calls []*prog.Call) {
59	typ := typ0.(*prog.StructType)
60	algName := generateAlg(g.Rand(), algTyp)
61	algNameData := fixedSizeData(algName, typ.Fields[0].Size())
62	arg = prog.MakeGroupArg(typ, []prog.Arg{
63		prog.MakeDataArg(typ.Fields[0], algNameData),
64	})
65	return
66}
67
68func generateAlgName(rnd *rand.Rand) (string, string) {
69	typ := allTypes[rnd.Intn(len(allTypes))]
70	name := generateAlg(rnd, typ.typ)
71	return typ.name, name
72}
73
74func generateAlg(rnd *rand.Rand, typ int) string {
75	algs := allAlgs[typ]
76	alg := algs[rnd.Intn(len(algs))]
77	return generateAlgImpl(rnd, alg)
78}
79
80func generateAlgImpl(rnd *rand.Rand, alg algDesc) string {
81	args := ""
82	if len(alg.args) != 0 {
83		args += "("
84		for i, a := range alg.args {
85			if i != 0 {
86				args += ","
87			}
88			args += generateAlg(rnd, a)
89		}
90		args += ")"
91	}
92	return alg.name + args
93}
94
95func fixedSizeData(str string, sz uint64) []byte {
96	return append([]byte(str), make([]byte, sz)...)[:sz]
97}
98
99type algType struct {
100	name string
101	typ  int
102}
103
104type algDesc struct {
105	name string
106	args []int
107}
108
109const (
110	ALG_CIPHER = iota
111	ALG_BLKCIPHER
112	ALG_AEAD
113	ALG_HASH
114	ALG_RNG
115)
116
117var allTypes = []algType{
118	{"aead", ALG_AEAD},
119	{"skcipher", ALG_BLKCIPHER},
120	{"hash", ALG_HASH},
121	{"rng", ALG_RNG},
122}
123
124var allAlgs = map[int][]algDesc{
125	ALG_AEAD: {
126		// templates:
127		{"authencesn", []int{ALG_HASH, ALG_BLKCIPHER}},
128		{"authenc", []int{ALG_HASH, ALG_BLKCIPHER}},
129		{"rfc7539esp", []int{ALG_BLKCIPHER, ALG_HASH}},
130		{"rfc7539", []int{ALG_BLKCIPHER, ALG_HASH}},
131		{"rfc4543", []int{ALG_AEAD}},
132		{"rfc4106", []int{ALG_AEAD}},
133		{"pcrypt", []int{ALG_AEAD}},
134		{"rfc4309", []int{ALG_AEAD}},
135		{"gcm", []int{ALG_CIPHER}},
136		{"gcm_base", []int{ALG_BLKCIPHER, ALG_HASH}},
137		{"ccm", []int{ALG_CIPHER}},
138		{"ccm_base", []int{ALG_BLKCIPHER, ALG_HASH}},
139		{"echainiv", []int{ALG_AEAD}},
140		{"seqiv", []int{ALG_AEAD}},
141
142		// algorithms:
143		{"gcm(aes)", nil},
144		{"gcm_base(ctr(aes-aesni),ghash-generic)", nil},
145		{"generic-gcm-aesni", nil},
146		{"rfc4106(gcm(aes))", nil},
147		{"rfc4106-gcm-aesni", nil},
148		{"morus640", nil},
149		{"morus640-generic", nil},
150		{"morus640-sse2", nil},
151		{"morus1280", nil},
152		{"morus1280-generic", nil},
153		{"morus1280-sse2", nil},
154		{"morus1280-avx2", nil},
155		{"aegis128", nil},
156		{"aegis128-generic", nil},
157		{"aegis128-aesni", nil},
158		{"aegis128l", nil},
159		{"aegis128l-generic", nil},
160		{"aegis128l-aesni", nil},
161		{"aegis256", nil},
162		{"aegis256-generic", nil},
163		{"aegis256-aesni", nil},
164	},
165	ALG_BLKCIPHER: {
166		// templates:
167		{"pcbc", []int{ALG_CIPHER}},
168		{"cbc", []int{ALG_CIPHER}},
169		{"cfb", []int{ALG_CIPHER}},
170		{"xts", []int{ALG_CIPHER}},
171		{"ctr", []int{ALG_CIPHER}},
172		{"lrw", []int{ALG_CIPHER}},
173		{"ecb", []int{ALG_CIPHER}},
174		{"kw", []int{ALG_CIPHER}},
175		{"cts", []int{ALG_BLKCIPHER}},
176		{"fpu", []int{ALG_BLKCIPHER}},
177		{"xts", []int{ALG_BLKCIPHER}},
178		{"lrw", []int{ALG_BLKCIPHER}},
179		{"rfc3686", []int{ALG_BLKCIPHER}},
180		{"cryptd", []int{ALG_BLKCIPHER}},
181
182		// algorithms:
183		{"cbc(aes)", nil},
184		{"cbc(aes-aesni)", nil},
185		{"chacha20", nil},
186		{"chacha20-simd", nil},
187		{"pcbc(aes)", nil},
188		{"pcbc-aes-aesni", nil},
189		{"fpu(pcbc(aes))", nil},
190		{"fpu(pcbc(aes-aesni))", nil},
191		{"pcbc(aes-aesni)", nil},
192		{"xts(aes)", nil},
193		{"xts-aes-aesni", nil},
194		{"ctr(aes)", nil},
195		{"ctr-aes-aesni", nil},
196		{"cbc-aes-aesni", nil},
197		{"ecb(aes)", nil},
198		{"ecb-aes-aesni", nil},
199		{"chacha20-generic", nil},
200		{"xts(serpent)", nil},
201		{"xts-serpent-avx2", nil},
202		{"lrw(serpent)", nil},
203		{"lrw-serpent-avx2", nil},
204		{"ctr(serpent)", nil},
205		{"ctr-serpent-avx2", nil},
206		{"cbc(serpent)", nil},
207		{"cbc-serpent-avx2", nil},
208		{"ecb(serpent)", nil},
209		{"ecb-serpent-avx2", nil},
210		{"xts(camellia)", nil},
211		{"xts-camellia-aesni-avx2", nil},
212		{"lrw(camellia)", nil},
213		{"lrw-camellia-aesni-avx2", nil},
214		{"ctr(camellia)", nil},
215		{"ctr-camellia-aesni-avx2", nil},
216		{"cbc(camellia)", nil},
217		{"cbc-camellia-aesni-avx2", nil},
218		{"ecb(camellia)", nil},
219		{"ecb-camellia-aesni-avx2", nil},
220		{"xts-serpent-avx", nil},
221		{"lrw-serpent-avx", nil},
222		{"ctr-serpent-avx", nil},
223		{"cbc-serpent-avx", nil},
224		{"ecb-serpent-avx", nil},
225		{"xts(twofish)", nil},
226		{"xts-twofish-avx", nil},
227		{"lrw(twofish)", nil},
228		{"lrw-twofish-avx", nil},
229		{"ctr(twofish)", nil},
230		{"ctr-twofish-avx", nil},
231		{"cbc(twofish)", nil},
232		{"cbc-twofish-avx", nil},
233		{"ecb(twofish)", nil},
234		{"ecb-twofish-avx", nil},
235		{"xts(cast6)", nil},
236		{"xts-cast6-avx", nil},
237		{"lrw(cast6)", nil},
238		{"lrw-cast6-avx", nil},
239		{"ctr(cast6)", nil},
240		{"ctr-cast6-avx", nil},
241		{"cbc(cast6)", nil},
242		{"cbc-cast6-avx", nil},
243		{"ecb(cast6)", nil},
244		{"ecb-cast6-avx", nil},
245		{"ctr(cast5)", nil},
246		{"ctr-cast5-avx", nil},
247		{"cbc(cast5)", nil},
248		{"cbc-cast5-avx", nil},
249		{"ecb(cast5)", nil},
250		{"ecb-cast5-avx", nil},
251		{"xts-camellia-aesni", nil},
252		{"lrw-camellia-aesni", nil},
253		{"ctr-camellia-aesni", nil},
254		{"cbc-camellia-aesni", nil},
255		{"ecb-camellia-aesni", nil},
256		{"xts-serpent-sse2", nil},
257		{"lrw-serpent-sse2", nil},
258		{"ctr-serpent-sse2", nil},
259		{"cbc-serpent-sse2", nil},
260		{"ecb-serpent-sse2", nil},
261		{"ctr(aes-aesni)", nil},
262		{"salsa20", nil},
263		{"salsa20-generic", nil},
264		{"ecb(arc4)", nil},
265		{"ecb(arc4)-generic", nil},
266		{"ecb(cipher_null)", nil},
267		{"ecb-cipher_null", nil},
268		{"salsa20-asm", nil},
269		{"xts-twofish-3way", nil},
270		{"lrw-twofish-3way", nil},
271		{"ctr-twofish-3way", nil},
272		{"cbc-twofish-3way", nil},
273		{"ecb-twofish-3way", nil},
274		{"ctr(blowfish)", nil},
275		{"ctr-blowfish-asm", nil},
276		{"cbc(blowfish)", nil},
277		{"cbc-blowfish-asm", nil},
278		{"ecb(blowfish)", nil},
279		{"ecb-blowfish-asm", nil},
280		{"xts-camellia-asm", nil},
281		{"lrw-camellia-asm", nil},
282		{"ctr-camellia-asm", nil},
283		{"cbc-camellia-asm", nil},
284		{"ecb-camellia-asm", nil},
285		{"ctr(des3_ede)", nil},
286		{"ctr-des3_ede-asm", nil},
287		{"cbc(des3_ede)", nil},
288		{"cbc-des3_ede-asm", nil},
289		{"ecb(des3_ede)", nil},
290		{"ecb-des3_ede-asm", nil},
291	},
292	ALG_CIPHER: {
293		{"aes", nil},
294		{"aes-aesni", nil},
295		{"seed", nil},
296		{"seed-generic", nil},
297		{"anubis", nil},
298		{"anubis-generic", nil},
299		{"khazad", nil},
300		{"khazad-generic", nil},
301		{"xeta", nil},
302		{"xeta-generic", nil},
303		{"xtea", nil},
304		{"xtea-generic", nil},
305		{"tea", nil},
306		{"tea-generic", nil},
307		{"arc4", nil},
308		{"arc4-generic", nil},
309		{"cast6", nil},
310		{"cast6-generic", nil},
311		{"cast5", nil},
312		{"cast5-generic", nil},
313		{"camellia", nil},
314		{"camellia-generic", nil},
315		{"camellia-asm", nil},
316		{"tnepres", nil},
317		{"aes-fixed-time", nil},
318		{"aes-generic", nil},
319		{"tnepres-generic", nil},
320		{"serpent", nil},
321		{"serpent-generic", nil},
322		{"twofish", nil},
323		{"twofish-generic", nil},
324		{"twofish-asm", nil},
325		{"blowfish", nil},
326		{"blowfish-generic", nil},
327		{"blowfish-asm", nil},
328		{"fcrypt", nil},
329		{"fcrypt-generic", nil},
330		{"des3_ede", nil},
331		{"des3_ede-generic", nil},
332		{"des3_ede-asm", nil},
333		{"des", nil},
334		{"des-generic", nil},
335		{"cipher_null", nil},
336		{"cipher_null-generic", nil},
337		{"aes-asm", nil},
338	},
339	ALG_HASH: {
340		// templates:
341		{"cmac", []int{ALG_CIPHER}},
342		{"cbcmac", []int{ALG_CIPHER}},
343		{"xcbc", []int{ALG_CIPHER}},
344		{"vmac", []int{ALG_CIPHER}},
345		{"hmac", []int{ALG_HASH}},
346		{"mcryptd", []int{ALG_HASH}},
347		{"cryptd", []int{ALG_HASH}},
348
349		// algorithms:
350		{"sha512", nil},
351		{"sha512_mb", nil},
352		{"sha256", nil},
353		{"sha256_mb", nil},
354		{"sha1", nil},
355		{"sha1_mb", nil},
356		{"ghash", nil},
357		{"ghash-clmulni", nil},
358		{"md4", nil},
359		{"md4-generic", nil},
360		{"md5", nil},
361		{"md5-generic", nil},
362		{"ghash-generic", nil},
363		{"crct10dif", nil},
364		{"crct10dif-generic", nil},
365		{"crct10dif-pclmul", nil},
366		{"crc32", nil},
367		{"crc32-generic", nil},
368		{"crc32c", nil},
369		{"crc32c-generic", nil},
370		{"michael_mic", nil},
371		{"michael_mic-generic", nil},
372		{"poly1305", nil},
373		{"poly1305-generic", nil},
374		{"tgr128", nil},
375		{"tgr128-generic", nil},
376		{"tgr160", nil},
377		{"tgr160-generic", nil},
378		{"tgr192", nil},
379		{"tgr192-generic", nil},
380		{"wp256", nil},
381		{"wp256-generic", nil},
382		{"wp384", nil},
383		{"wp384-generic", nil},
384		{"wp512", nil},
385		{"wp512-generic", nil},
386		{"sm3", nil},
387		{"sm3-generic", nil},
388		{"sm4", nil},
389		{"sm4-generic", nil},
390		{"speck128", nil},
391		{"speck128-generic", nil},
392		{"speck64", nil},
393		{"speck64-generic", nil},
394		{"sha3-512", nil},
395		{"sha3-512-generic", nil},
396		{"sha3-384", nil},
397		{"sha3-384-generic", nil},
398		{"sha3-256", nil},
399		{"sha3-256-generic", nil},
400		{"sha3-224", nil},
401		{"sha3-224-generic", nil},
402		{"sha384", nil},
403		{"sha384-generic", nil},
404		{"sha512-generic", nil},
405		{"sha224", nil},
406		{"sha224-generic", nil},
407		{"sha256-generic", nil},
408		{"sha1-generic", nil},
409		{"rmd320", nil},
410		{"rmd320-generic", nil},
411		{"rmd256", nil},
412		{"rmd256-generic", nil},
413		{"rmd160", nil},
414		{"rmd160-generic", nil},
415		{"rmd128", nil},
416		{"rmd128-generic", nil},
417		{"digest_null", nil},
418		{"digest_null-generic", nil},
419		{"poly1305-simd", nil},
420		{"sha384-avx2", nil},
421		{"sha512-avx2", nil},
422		{"sha384-avx", nil},
423		{"sha512-avx", nil},
424		{"sha384-ssse3", nil},
425		{"sha512-ssse3", nil},
426		{"sha224-avx2", nil},
427		{"sha256-avx2", nil},
428		{"sha224-avx", nil},
429		{"sha256-avx", nil},
430		{"sha224-ssse3", nil},
431		{"sha256-ssse3", nil},
432		{"crc32-pclmul", nil},
433		{"sha1-avx2", nil},
434		{"sha1-avx", nil},
435		{"sha1-ssse3", nil},
436		{"crc32c-intel", nil},
437	},
438	ALG_RNG: {
439		{"stdrng", nil},
440		{"ansi_cprng", nil},
441		{"jitterentropy_rng", nil},
442		{"drbg_nopr_hmac_sha256", nil},
443		{"drbg_nopr_hmac_sha512", nil},
444		{"drbg_nopr_hmac_sha384", nil},
445		{"drbg_nopr_hmac_sha1", nil},
446		{"drbg_nopr_sha256", nil},
447		{"drbg_nopr_sha512", nil},
448		{"drbg_nopr_sha384", nil},
449		{"drbg_nopr_sha1", nil},
450		{"drbg_nopr_ctr_aes256", nil},
451		{"drbg_nopr_ctr_aes192", nil},
452		{"drbg_nopr_ctr_aes128", nil},
453		{"drbg_pr_hmac_sha256", nil},
454		{"drbg_pr_hmac_sha512", nil},
455		{"drbg_pr_hmac_sha384", nil},
456		{"drbg_pr_hmac_sha1", nil},
457		{"drbg_pr_sha256", nil},
458		{"drbg_pr_sha512", nil},
459		{"drbg_pr_sha384", nil},
460		{"drbg_pr_sha1", nil},
461		{"drbg_pr_ctr_aes256", nil},
462		{"drbg_pr_ctr_aes192", nil},
463		{"drbg_pr_ctr_aes128", nil},
464	},
465}
466