-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeck.s
260 lines (190 loc) · 4.55 KB
/
speck.s
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
.text
.global main
ROR:
@address of 64-bit number to be rotated is in r0
@we need r4. so save it in stack
sub sp, sp, #4
str r4, [sp]
mov r4, r0 @moving address of the number to r4
@loading the 64-bit number to 2 32-bit registers
ldr r0, [r4, #4]
ldr r1, [r4]
lsr r2, r0, #8
lsl r3, r1, #24
orr r12, r2, r3 @taking the first rotated part to r12
lsr r2, r1, #8
lsl r3, r0, #24
orr r1, r2, r3 @taking the second rotated part
@updating the number with rotation
str r12, [r4, #4]
str r1, [r4]
@loading r4 back from stack
ldr r4, [sp]
add sp, sp, #4
mov pc, lr
ROL:
@address of 64-bit number to be rotated is in r0
@we need r4. so save it in stack
sub sp, sp, #4
str r4, [sp]
mov r4, r0 @moving address of number to r4
@loading the 64-bit number to 2 32-bit registers
ldr r0, [r4, #4]
ldr r1, [r4]
lsl r2, r0, #3
lsr r3, r1, #29
orr r12, r2, r3 @taking the first rotated part to r12
lsl r2, r1, #3
lsr r3, r0, #29
orr r1, r2, r3 @taking the second rotated part
@updating the number with rotation
str r12, [r4, #4]
str r1, [r4]
@loading r4 back from stack
ldr r4, [sp]
add sp, sp, #4
mov pc, lr
R:
@starting addresses of 64-bit numbers x, y are in r0, r1. value of k(64-bit)
@is in r2(32-bit) and r3(32-bit) (i.e.&x, &y, k)
@saving return address and r4-r7 in stack
@since we call other functions and use r4-r7 inside the function
sub sp, sp, #20
str lr, [sp, #16]
str r4, [sp, #12]
str r5, [sp, #8]
str r6, [sp, #4]
str r7, [sp, #0]
@step 1: ROR x
@saving r0, r1, r2, r3 in stack before calling ROR
sub sp, sp, #16
str r0, [sp, #12]
str r1, [sp, #8]
str r2, [sp, #4]
str r3, [sp, #0]
bl ROR
@restoring r0, r1, r2, r3 from stack
ldr r0, [sp, #12]
ldr r1, [sp, #8]
ldr r2, [sp, #4]
ldr r3, [sp, #0]
add sp, sp, #16
@step 2: x = x + y
@loading x to r4, r5; loading y to r6, r7
ldr r4, [r0, #4]
ldr r5, [r0]
ldr r6, [r1, #4]
ldr r7, [r1]
@adding 32-bit parts to get 64-bit addition
adds r5, r5, r7
adc r6, r4, r6
@step 3: x = x ^ k
eor r5, r5, r3
eor r6, r6, r2
@updating x in the stack
str r5, [r0]
str r6, [r0, #4]
@step 4: ROL y
@saving r0, r1, r2, r3 in stack before calling ROL
sub sp, sp, #16
str r0, [sp, #12]
str r1, [sp, #8]
str r2, [sp, #4]
str r3, [sp, #0]
mov r0, r1
bl ROL
@restoring r0, r1, r2, r3 from stack
ldr r0, [sp, #12]
ldr r1, [sp, #8]
ldr r2, [sp, #4]
ldr r3, [sp, #0]
add sp, sp, #16
@step 5: y = y ^ x
@loading y to r4, r5; loading x to r6, r7
ldr r4, [r0, #4]
ldr r5, [r0]
ldr r6, [r1, #4]
ldr r7, [r1]
eor r4, r4, r6
eor r5, r5, r7
@updating y in the stack
str r5, [r1]
str r4, [r1, #4]
@loading actual return address and r4-r7 from stack and returning
ldr lr, [sp, #16]
ldr r4, [sp, #12]
ldr r5, [sp, #8]
ldr r6, [sp, #4]
ldr r7, [sp, #0]
add sp, sp, #20
mov pc, lr
main:
@ preserving return address
sub sp, sp, #4
str lr, [sp]
@to read 4 64-bit numbers to stack
sub sp, sp, #32
@prompt user to enter the key
ldr r0, =format1
bl printf
@reading the key
ldr r0, =format2
add r1, sp, #24 @a = key[1] will be read to [sp, #24]
add r2, sp, #16 @b = key[0] will be read to [sp, #16]
bl scanf
@prompt user to enter plain text
ldr r0, =format3
bl printf
@reading the plain text
ldr r0, =format2
add r1, sp, #8 @x = pt[1] will be read to [sp, #8]
add r2, sp, #0 @y = pt[0] will be read to [sp, #0]
bl scanf
@------------------encryption-------------------------
@calling R(&x, &y, b)
add r0, sp, #8 @&x
mov r1, sp @&y
ldr r2, [sp, #20] @b - first part (32-bit)
ldr r3, [sp, #16] @b - second part (32-bit)
bl R
mov r4, #0 @i = 0
loop:
cmp r4, #31 @if i < 31, continue; else, exit.
bge exit
@calling R(&a, &b, i)
add r0, sp, #24 @&a
add r1, sp, #16 @&b
mov r2, #0 @0 - first part (32-bit)
mov r3, r4 @i - second part (32-bit)
bl R
@calling R(&x, &y, b)
add r0, sp, #8 @&x
mov r1, sp @&y
ldr r2, [sp, #20] @b - first part (32-bit)
ldr r3, [sp, #16] @b - second part (32-bit)
bl R
add r4, r4, #1 @i++
b loop
exit:
@printing out the cipher text
ldr r0, =format4
ldr r1, [sp, #12]
ldr r2, [sp, #8]
bl printf
ldr r0, =format5
ldr r1, [sp, #4]
ldr r2, [sp, #0]
bl printf
@-----------------------------------------------------
@releasing stack used for 4 64-bit numbers
add sp, sp, #32
@popping return address and return
ldr lr, [sp]
add sp, sp, #4
mov pc, lr
.data
format1: .asciz "Enter the key:\n"
format2: .asciz "%llx %llx"
format3: .asciz "Enter the plain text:\n"
format4: .asciz "Cipher text is:\n%x%x "
format5: .asciz "%x%x\n"