-
Notifications
You must be signed in to change notification settings - Fork 3
/
key128.go
60 lines (47 loc) · 1023 Bytes
/
key128.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
package present
// key128 implements the PRESENT key schedule for 128-bit keys.
type key128 struct {
A, B uint64
}
func (k *key128) copy() key {
cpy := *k
return &cpy
}
func (k *key128) rotate() {
w := k.A & 0xfffffffffffffff8
x := k.A & 0x7
y := k.B & 0xfffffffffffffff8
z := k.B & 0x7
k.A = (x << 61) + (y >> 3)
k.B = (z << 61) + (w >> 3)
}
func (k *key128) sBox() {
x := (k.A >> 60) & 0xF
y := (k.A >> 56) & 0xF
p := sBox[x]
q := sBox[y]
a := uint64(p) << 60
b := uint64(q) << 56
c := k.A & 0x00ffffffffffffff
k.A = a + b + c
}
func (k *key128) xor(ctr uint64) {
w := (k.A & 0x7) << 2
x := (k.B >> 62) & 0x3
y := w + x
z := y ^ ctr
p := (z >> 2) & 0x7
q := (z & 0x3) << 62
r := k.A & 0xfffffffffffffff8
s := k.B & 0x3fffffffffffffff
k.A = p + r
k.B = q + s
}
func (k *key128) roundKey() uint64 {
return k.A
}
// newKey128 returns a new 128-bit PRESENT key register from the provided key bytes.
func newKey128(key []byte) *key128 {
A, B := decompose(key)
return &key128{A, B}
}