/
keygen.go
65 lines (58 loc) · 1.84 KB
/
keygen.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
package spn
import (
"io"
"github.com/OpenWhiteBox/primitives/encoding"
"github.com/OpenWhiteBox/primitives/matrix"
)
func newAffineLayer(rand io.Reader) encoding.BlockAffine {
c := [16]byte{}
rand.Read(c[:])
return encoding.NewBlockAffine(matrix.GenerateRandom(rand, 128), c)
}
func newSBoxLayer(rand io.Reader) encoding.ConcatenatedBlock {
sbox := encoding.ConcatenatedBlock{}
for pos := 0; pos < 16; pos++ {
sbox[pos] = encoding.GenerateSBox(rand)
}
return sbox
}
// NewSPN generates a random SPN instance using the random source random (for example, crypto/rand.Reader), with the
// specified structure.
func NewSPN(rand io.Reader, structure Structure) Construction {
switch structure {
case AS:
return Construction(encoding.ComposedBlocks{
newSBoxLayer(rand), newAffineLayer(rand),
})
case SA:
return Construction(encoding.ComposedBlocks{
newAffineLayer(rand), newSBoxLayer(rand),
})
case ASA:
return Construction(encoding.ComposedBlocks{
newAffineLayer(rand), newSBoxLayer(rand), newAffineLayer(rand),
})
case SAS:
return Construction(encoding.ComposedBlocks{
newSBoxLayer(rand), newAffineLayer(rand), newSBoxLayer(rand),
})
case ASAS:
return Construction(encoding.ComposedBlocks{
newSBoxLayer(rand), newAffineLayer(rand), newSBoxLayer(rand), newAffineLayer(rand),
})
case SASA:
return Construction(encoding.ComposedBlocks{
newAffineLayer(rand), newSBoxLayer(rand), newAffineLayer(rand), newSBoxLayer(rand),
})
case ASASA:
return Construction(encoding.ComposedBlocks{
newAffineLayer(rand), newSBoxLayer(rand), newAffineLayer(rand), newSBoxLayer(rand), newAffineLayer(rand),
})
case SASAS:
return Construction(encoding.ComposedBlocks{
newSBoxLayer(rand), newAffineLayer(rand), newSBoxLayer(rand), newAffineLayer(rand), newSBoxLayer(rand),
})
default:
panic("Unknown SPN structure!")
}
}