-
Notifications
You must be signed in to change notification settings - Fork 22
/
present.go
237 lines (188 loc) · 6.03 KB
/
present.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package present
import (
"strconv"
"crypto/cipher"
"github.com/deatil/go-cryptobin/tool/alias"
)
const BlockSize = 8
type KeySizeError int
func (k KeySizeError) Error() string {
return "cryptobin/present: invalid key size " + strconv.Itoa(int(k))
}
type presentCipher struct {
key [32]uint64
}
// NewCipher creates and returns a new cipher.Block.
func NewCipher(key []byte) (cipher.Block, error) {
keyLen := len(key)
// Check key length
if keyLen != 10 && keyLen != 16 {
return nil, KeySizeError(keyLen)
}
c := new(presentCipher)
c.expandKey(key)
return c, nil
}
func (this *presentCipher) BlockSize() int {
return BlockSize
}
func (this *presentCipher) Encrypt(dst, src []byte) {
if len(src) < BlockSize {
panic("cryptobin/present: input not full block")
}
if len(dst) < BlockSize {
panic("cryptobin/present: output not full block")
}
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
panic("cryptobin/present: invalid buffer overlap")
}
this.encryptBlock(dst, src)
}
func (this *presentCipher) Decrypt(dst, src []byte) {
if len(src) < BlockSize {
panic("cryptobin/present: input not full block")
}
if len(dst) < BlockSize {
panic("cryptobin/present: output not full block")
}
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
panic("cryptobin/present: invalid buffer overlap")
}
this.decryptBlock(dst, src)
}
func (this *presentCipher) expandKey(key []byte) {
var i uint
var t uint64
var kl uint64
var kh uint64
keyLen := len(key)
//PRESENT can take keys of either 80 or 128 bits
if keyLen == 10 {
//Copy the 80-bit key
kh = bytesToUint64(key)
kl = bytesToUint64(key[2:])
//Save the 64 leftmost bits of K
this.key[0] = (kh << 48) | (kl >> 16)
//Generate round keys
for i = 1; i <= 31; i++ {
//The key register is rotated by 61 bit positions to the left
t = kh & 0xFFFF
kh = (kl >> 3) & 0xFFFF
kl = (kl << 61) | (t << 45) | (kl >> 19)
//The left-most four bits are passed through the S-box
t = uint64(sbox[(kh >> 12) & 0x0F])
kh = (kh & 0x0FFF) | (t << 12)
//The round counter value i is XOR-ed with bits 19, 18, 17, 16, 15 of K
kl ^= uint64(i) << 15
//Save the 64 leftmost bits of K
this.key[i] = (kh << 48) | (kl >> 16)
}
} else {
//Copy the 128-bit key
kh = bytesToUint64(key)
kl = bytesToUint64(key[8:])
//Save the 64 leftmost bits of K
this.key[0] = kh
//Generate round keys
for i = 1; i <= 31; i++ {
//The key register is rotated by 61 bit positions to the left
t = kh
kh = (t << 61) | (kl >> 3)
kl = (kl << 61) | (t >> 3)
//The left-most eight bits are passed through two S-boxes
t = uint64(sbox[(kh >> 56) & 0x0F])
kh = (kh & 0xF0FFFFFFFFFFFFFF) | (t << 56)
t = uint64(sbox[(kh >> 60) & 0x0F])
kh = (kh & 0x0FFFFFFFFFFFFFFF) | (t << 60)
//The round counter value i is XOR-ed with bits 66, 65, 64, 63, 62 of K
kh ^= uint64(i) >> 2
kl ^= uint64(i) << 62
//Save the 64 leftmost bits of K
this.key[i] = kh
}
}
}
func (this *presentCipher) encryptBlock(output []byte, input []byte){
var i uint
var s uint64
var t uint64
var state uint64
//Copy the plaintext to the 64-bit state
state = bytesToUint64(input)
//Initial round key addition
state ^= this.key[0];
//The encryption consists of 31 rounds
for i = 1; i <= 31; i++ {
//Apply S-box and bit permutation
s = spbox[state & 0xFF]
t = spbox[(state >> 8) & 0xFF]
s |= ROL64(t, 2)
t = spbox[(state >> 16) & 0xFF]
s |= ROL64(t, 4)
t = spbox[(state >> 24) & 0xFF]
s |= ROL64(t, 6)
t = spbox[(state >> 32) & 0xFF]
s |= ROL64(t, 8)
t = spbox[(state >> 40) & 0xFF]
s |= ROL64(t, 10)
t = spbox[(state >> 48) & 0xFF]
s |= ROL64(t, 12)
t = spbox[(state >> 56) & 0xFF]
s |= ROL64(t, 14)
//Add round key
state = s ^ this.key[i]
}
//The final state is then copied to the output
stateBytes := uint64ToBytes(state)
copy(output, stateBytes[:])
}
func (this *presentCipher) decryptBlock(output []byte, input []byte) {
var i uint
var s uint64
var t uint64
var state uint64
//Copy the ciphertext to the 64-bit state
state = bytesToUint64(input)
//The decryption consists of 31 rounds
for i = 31; i > 0; i-- {
//Add round key
state ^= this.key[i]
//Apply inverse bit permutation
s = uint64(ipbox[state & 0xFF])
t = uint64(ipbox[(state >> 8) & 0xFF])
s |= ROL64(t, 32)
t = uint64(ipbox[(state >> 16) & 0xFF])
s |= ROL64(t, 1)
t = uint64(ipbox[(state >> 24) & 0xFF])
s |= ROL64(t, 33)
t = uint64(ipbox[(state >> 32) & 0xFF])
s |= ROL64(t, 2)
t = uint64(ipbox[(state >> 40) & 0xFF])
s |= ROL64(t, 34)
t = uint64(ipbox[(state >> 48) & 0xFF])
s |= ROL64(t, 3)
t = uint64(ipbox[(state >> 56) & 0xFF])
s |= ROL64(t, 35)
//Apply inverse S-box
state = uint64(isbox[s & 0xFF])
t = uint64(isbox[(s >> 8) & 0xFF])
state |= ROL64(t, 8)
t = uint64(isbox[(s >> 16) & 0xFF])
state |= ROL64(t, 16);
t = uint64(isbox[(s >> 24) & 0xFF])
state |= ROL64(t, 24)
t = uint64(isbox[(s >> 32) & 0xFF])
state |= ROL64(t, 32)
t = uint64(isbox[(s >> 40) & 0xFF])
state |= ROL64(t, 40)
t = uint64(isbox[(s >> 48) & 0xFF])
state |= ROL64(t, 48)
t = uint64(isbox[(s >> 56) & 0xFF])
state |= ROL64(t, 56)
}
//Final round key addition
state ^= this.key[0]
//The final state is then copied to the output
stateBytes := uint64ToBytes(state)
copy(output, stateBytes[:])
}