-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.asm
729 lines (536 loc) · 16.4 KB
/
main.asm
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
.386
INCLUDE Irvine32.inc
.data ;Insert ur data here
;----- combine data -----
turn db 8 DUP(0),0 ; char array of size 8
;----- end combine data -----
;----- split data -----
splitOut db 8 DUP(0), 0
;----- end split data -----
;----- encrypt & decrypt data -----
sum dd 0
values dd 2 DUP(0)
v0 dd ?
v1 dd ?
delta dd 9e3779b9h ;constant
key dd 4 DUP(0)
;----- end encrypt & decrypt data -----
;----- TEA data -----
maxInput = 5000
printString db maxInput DUP(0),0
inputLen dd ?
rounds dd 0
teaIn db maxInput DUP(0),0 ;string of size multiples of 8
i dw 0
j db 0
teaOut db maxInput DUP(0),0
decision db 0
encryptedMsg db "Encrypted text", 0
decryptedMsg db "Decrypted text", 0
printIndex dd 0
;----- end TEA data -----
;----- main data -----
inputMsg db "Enter a string: ",0
inKeyMsg db "**Enter 4 keys**",10,13,0
enterKeyMsg db "Enter Key ",0
colon db ": ",0
msgBoxDecrypt db "Would you like to decrypt?",0
msgBoxRestart db "Would you like to enter another string?",0
msgBoxDecTitle db "Decrypt ?",0
msgBoxResTitle db "Restart ?",0
newSize dd ?
;----- end main data -----
;----- handleKey data -----
keyStr db 200 DUP(0)
validKey dd ?
invalidInputMsg db "Invalid Input, Try another key: ",0
;----- end handleKey data -----
.code ;Inser ur code here
;---------------- Start Main ----------------
main PROC
START:
;cout<<"Enter a String: "
lea edx, inputMsg
call writestring
;getline(cin,input)
lea edx, teaIn
mov ecx, maxInput
call readstring
call StrLength ; eax = teaIn.length()
mov inputLen, eax ;inputLen = teaIn.length()
;handling empty input
cmp inputLen,0
je START
;remove newline char from the string buffer
add eax , offset teaIn
mov BYTE PTR [eax+1], 0 ;[teaIn + inputLen + 1] = 00h
;cout<<"Enter a 4 keys: "
lea edx, inKeyMsg
call writestring
;for(int i = 0 ; i < 4 ; i++) cin>>key[i];
mov ecx, 4
lea ebx, key
mov i, 1
keyLoop:
;cout<<"enter key no#:";
lea edx, enterKeyMsg
call writestring
;cout<<"#: ";
movzx eax,i
call writeDec
lea edx, colon
call writestring
;cin>>key[i];
call handleKey
mov eax , validKey
mov [ebx], eax
add ebx, 4
inc i
LOOP keyLoop
mov decision, 0 ;decision = 0 to set TEA to encrypt
;teaOut = TEA(teaIn, key) >> encryption
call TEA
;make a msgbox asking would u like to decrypt?
lea edx, msgBoxDecrypt
lea ebx, msgBoxDecTitle
call MsgBoxAsk
;if yes call decrypt
;else
;make a msg box asking would u like to enter another message?
;if yes goto the start of main
;else goto end main
cmp eax , IDYES ;eax = 7 if user choose no, eax = 6 if yes /// IDYES = 6
ja skipDecryption
mov decision, 1 ;decision = 1 to set TEA to encrypt
; teaIn = teaOut //copy teaOut in teaIn
mov ecx, newSize
lea ebx, teaOut
lea edx, teaIn
copyLoop:
mov al, [ebx]
mov [edx], al
inc edx
inc ebx
LOOP copyLoop
;teaOut = TEA(teaIn, key) >> decryption
call TEA
skipDecryption:
lea edx, msgBoxRestart
lea ebx, msgBoxResTitle
call MsgBoxAsk
;if clicked yes goto start
call crlf
cmp eax , IDYES
je START
INVOKE ExitProcess, 0 ;end the program
main ENDP
;---------------- End Main ----------------
;-----------------------------------------------
;
; This proc handle exceptions for key
;
; It reads a string from the user and check if it contains a valid or invalid key
;
; Returns: a DWORD contains the valid key the user input
;---------------- Start handleKey ----------------
handleKey PROC USES ecx
keyLoop:
;Enter a key (string)
lea edx, keyStr
mov ecx, 200
call readstring
;loop on the string char by char
call strlength
mov ecx, eax
checkKey:
mov al, [edx]
;if char ==' ' skip isDigit (ignore spaces)
cmp al, ' '
je skipIsDigit
;else check if the char is a digit or not
call isdigit ;isdigit sets ZF=0 if char is invalid
jnz invalidKey
;repeat for next char
skipIsDigit:
inc edx
LOOP checkKey
;at the end of the loop the string contains spaces and digits only
;but we have to check if the digits is not greater than (2^32)-1
;(valid input) parse the valid key from the string
lea edx, keyStr
call strlength
mov ecx, eax
call parseDecimal32
;if CF == 1 then the entered key is blank or greater than (2^32)-1
jc invalidKey
;else //CF == 0 The entered key is valid
mov validKey, eax
jmp endProc
invalidKey:
lea edx ,invalidInputMsg
call writestring
jmp keyLoop
endProc:
ret
handleKey ENDP
;---------------- End handleKey ----------------
;-----------------------------------------------
;
; This proc works as a driver for all other procedures to prevent duplicated code
;
; It Splits the string into multiple blocks of 64-bits to allow TEA to operate on input of any size
; Encrypt or Decrypt every block (depending on a decision bit)
; Display the encrypted text or the decrypted text
;
; Recieves: a BYTE array (char array) contains user input
;
;---------------- Start TEA ----------------
TEA PROC
;calc no of rounds
call calcRounds
mov i ,0
cmp rounds , 0
;jump if below or equal
jbe skipTeaOuterLoop
;for(int i = 0 ; i < rounds ; i++)
teaOuterLoop:
mov j,0
;for(int j = 0 ; j < 8 ; j++)
mov ecx, 8 ; ecx = 8
turnLoop:
;turn[j] = teaIn[i*8 + j];
mov eax, 0
mov eax, 8
mul i ;eax = 8 * i
movzx edx, j
add eax , edx ;eax= (8*i + j)
lea edx, teaIn ;edx = offset teaIn
mov eax,[edx + eax] ;eax = teaIn[i*8 + j]
movzx ebx , j ; ebx = j
add ebx, offset turn ; ebx = turn[j]
mov [ebx] , al ;turn[j] = teaIn[i*8 + j]
INC j
LOOP turnLoop
;combine(turn, values);
call combine
;if(decision == 0) encrypt(values, key);
;else decrypt(values, key);
CMP decision, 0
je encryption
call decrypt
jmp endDecision
encryption:
call encrypt
endDecision:
;teaOut+=split(values);
call split ;return splitOut contains 8 chars
;teaOut += splitOut
mov esi, offset splitOut
mov edi, offset teaOut
mov eax, 8
mul i ;eax = 8 * i
add edi, eax
mov ecx, 8
cld
REP MOVSB
INC i
dec rounds
cmp rounds, 0
Jg teaOuterLoop
skipTeaOuterLoop:
;copy exactly input length char from teaOut to printString
mov ecx, inputLen
lea ebx, teaOut
lea edx, printString
copyTeaOut:
mov al, [ebx]
;if (char == '\0') replace with ' '
cmp al, 0
jne putChar
mov al, ' '
putChar:
mov [edx], al
inc ebx
inc edx
LOOP copyTeaOut
mov BYTE PTR [edx], 0
;erase everything in teaIn (user input)
mov ecx , newSize
lea ebx, teaIn
eraseTeaIn:
mov BYTE PTR [ebx], 0
inc ebx
LOOP eraseTeaIn
;if(decision == 0) cout<<"Encrypted: ";
;else cout<<"Decrypted: ";
CMP decision, 0
je printEncrypted
lea ebx, decryptedMsg
jmp endPrintDecision
printEncrypted:
lea ebx, encryptedMsg
endPrintDecision:
lea edx, printString
call msgBox
ret
TEA ENDP
;---------------- End TEA ----------------
;-----------------------------------------------
;
; This is helper proc called in TEA proc to calculate number of rounds
; rounds is the number we split string to
;
; Recieves: double word inputLen
;
; Returns: double word rounds
;
;---------------- Start calcRounds ----------------
calcRounds PROC
;calculating rounds
;rounds = inputLen/8;
mov edx , 0
mov eax, inputLen
mov ecx, 8
div ecx ;eax = quotient , edx = remainder
mov rounds ,eax ;round = quotient
;if(inputLen%8 != 0)
; rounds++;
CMP edx , 0 ; if remainder == 0 dont inc
jz noInc
INC rounds ;else inc
noInc:
; calculate new string size
mov eax, 8
mul rounds ;eax = round*8
mov newSize, eax
ret
calcRounds ENDP
;-----------------------------------------------
;
; This proc uses a 128-bit key to encrypt a 64-bit block in 32 cycles using TEA
;
; Recieves: a DWORD array of size 4 (128-bit key)
; a DWORD array of size 2 (64-bit block)
;
;---------------- Start encrypt ----------------
encrypt PROC
;uint32_t sum = 0
mov sum, 0
;v0 = values[0]
mov eax, [values]
mov v0, eax
;v1 = values[1]
mov eax, [values+4]
mov v1, eax
;for (i = 0; i < 32; i++)
;loop 32 times
mov ecx, 32
encryptLoop:
;sum += delta;
mov eax, delta
add sum ,eax
;v0 += ((v1 << 4) + key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + key[1])
;v1<<4
mov eax, v1
shl eax, 4
;(v1 << 4) + key[0])
add eax, [key] ; eax =(v1 << 4) + key[0])
;(v1 + sum)
mov ebx, v1
add ebx, sum ; ebx = (v1 + sum)
;(v1 << 4) + key[0]) ^ (v1 + sum) >>> eax ^ ebx
xor eax, ebx ; eax = (v1 << 4) + key[0]) ^ (v1 + sum)
;((v1 >> 5) + key[1])
mov ebx, v1
shr ebx, 5 ;ebx = (v1 >> 5)
add ebx, [key+4] ;ebx =(v1 >> 5) + key[1]
;eax = (v1 << 4) + key[0]) ^ (v1 + sum)
;ebx = (v1 >> 5) + key[1]
xor eax, ebx ; eax = ((v1 << 4) + key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + key[1])
add v0, eax ;v0 += ((v1 << 4) + key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + key[1])
;v1 += ((v0 << 4) + key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + key[3]);
;v0<<4
mov eax, v0
shl eax, 4
;(v0 << 4) + key[2])
add eax, [key+8] ; eax =(v0 << 4) + key[2])
;(v0 + sum)
mov ebx, v0
add ebx, sum ; ebx = (v0 + sum)
;(v0 << 4) + key[2]) ^ (v0 + sum) >>> eax ^ ebx
xor eax, ebx ; eax = (v0 << 4) + key[2]) ^ (v0 + sum)
;((v0 >> 5) + key[3])
mov ebx, v0
shr ebx, 5 ;ebx = (v0 >> 5)
add ebx, [key+12] ;ebx =(v0 >> 5) + key[3]
;eax = (v0 << 4) + key[2]) ^ (v0 + sum)
;ebx = (v0 >> 5) + key[3]
xor eax, ebx ; eax = ((v0 << 4) + key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + key[3])
add v1, eax ;v1 += ((v0 << 4) + key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + key[3])
loop encryptLoop
; values[0] = v0
mov eax, v0
mov [values] , eax
; values[1] = v1
mov eax, v1
mov [values+4] , eax
ret
encrypt ENDP
;---------------- End encrypt ----------------
;-----------------------------------------------
;
;
; This proc uses a 128-bit key to decrypt a 64-bit block in 32 cycles using TEA
;
; Recieves: a DWORD array of size 4 (128-bit key)
; a DWORD array of size 2 (64-bit block)
;
;---------------- Start decrypt ----------------
decrypt PROC
;v0 = values[0]
mov eax, [values]
mov v0, eax
;v1 = values[1]
mov eax, [values+4]
mov v1, eax
;sum = delta<<5
mov ebx, delta
shl ebx, 5
mov sum, ebx
;for (i = 0; i < 32; i++)
;loop 32 times
mov ecx, 32
encLoop:
;v1 -= ((v0 << 4) + key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + key[3]);
;v0<<4
mov eax, v0
shl eax, 4
;(v0 << 4) + key[2])
add eax, [key+8] ; eax =(v0 << 4) + key[2])
;(v0 + sum)
mov ebx, v0
add ebx, sum ; ebx = (v0 + sum)
;(v0 << 4) + key[2]) ^ (v0 + sum) >>> eax ^ ebx
xor eax, ebx ; eax = (v0 << 4) + key[2]) ^ (v0 + sum)
;((v0 >> 5) + key[3])
mov ebx, v0
shr ebx, 5 ;ebx = (v0 >> 5)
add ebx, [key+12] ;ebx =(v0 >> 5) + key[3]
;eax = (v0 << 4) + key[2]) ^ (v0 + sum)
;ebx = (v0 >> 5) + key[3]
xor eax, ebx ; eax = ((v0 << 4) + key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + key[3])
sub v1, eax ;v1 -= ((v0 << 4) + key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + key[3])
;v0 -= ((v1 << 4) + key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + key[1]);
;v1<<4
mov eax, v1
shl eax, 4
;(v1 << 4) + key[0])
add eax, [key] ; eax =(v1 << 4) + key[0])
;(v1 + sum)
mov ebx, v1
add ebx, sum ; ebx = (v1 + sum)
;(v1 << 4) + key[0]) ^ (v1 + sum) >>> eax ^ ebx
xor eax, ebx ; eax = (v1 << 4) + key[0]) ^ (v1 + sum)
;((v1 >> 5) + key[1])
mov ebx, v1
shr ebx, 5 ;ebx = (v1 >> 5)
add ebx, [key+4] ;ebx =(v1 >> 5) + key[1]
;eax = (v1 << 4) + key[0]) ^ (v1 + sum)
;ebx = (v1 >> 5) + key[1]
xor eax, ebx ; eax = ((v1 << 4) + key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + key[1])
sub v0, eax ;v0 -= ((v1 << 4) + key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + key[1])
;sum -= delta;
mov eax, delta
sub sum ,eax
loop encLoop
;values[0] = v0
mov eax, v0
mov [values] , eax
;values[1] = v1
mov eax, v1
mov [values+4] , eax
ret
decrypt ENDP
;---------------- End decrypt ----------------
;-----------------------------------------------
;
; This proc combines every 4 characters from a char array of size 8 into one 32-bit block
;
; Recieves: a BYTE array of size 8 (char array)
;
; Returns: a DWORD array of size 2 (every DWORD contains 4 characters)
;
;---------------- Start combine ----------------
combine PROC
;v[0] = s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
movzx edx, [turn+0]
mov eax, 0
mov al, [turn+1]
shl eax, 8
or edx, eax
mov eax, 0
mov al, [turn+2]
shl eax, 16
or edx, eax
mov eax, 0
mov al, [turn+3]
shl eax, 24
or edx, eax
mov [values+0], edx
;v[1] = s[4] | s[5] << 8 | s[6] << 16 | s[7] << 24;
movzx edx, [turn+4]
mov eax, 0
mov al, [turn+5]
shl eax, 8
or edx, eax
mov eax, 0
mov al, [turn+6]
shl eax, 16
or edx, eax
mov eax, 0
mov al, [turn+7]
shl eax, 24
or edx, eax
mov [values+4], edx
ret
combine ENDP
;---------------- End combine ----------------
;-----------------------------------------------
;
;
; This proc splits every 32-bit from a 64-bit block into 4 characters
;
; Recieves: a DWORD array of size 2 (64-bit block)
;
;
; Returns: a BYTE array (char array) contains the equivalent 8 characters that were stored in the 64-bit block
;
;----------------- Start split -----------------
split PROC
;splitOut[0] = first char in values[0] ;splitOut[1] = second char in values[0]
;splitOut[2] = third char in values[0] ;splitOut[3] = fourth char in values[0]
;-----------------------------------------------------------------------------
;splitOut[4] = first char in values[1] ;splitOut[5] = second char in values[1]
;splitOut[6] = third char in values[1] ;splitOut[7] = fourth char in values[1]
;splitting first 4 chars
mov eax, values[0]
lea ebx, splitOut ; ebx = offset splitOut
mov ecx, 2 ;counter of outerloop
splitOuterLoop:
mov edx, ecx ;storing the counter of outerloop in edx
mov ecx, 2 ;setting counter of inner loop
splitInnerLoop:
mov [ebx], al
inc ebx
mov [ebx], ah
inc ebx
shr eax, 16
LOOP splitInnerLoop
;splitting second 4 chars
mov eax, values[4]
mov ecx, edx ;putting the counter of outer loop back in ecx
LOOP splitOuterLoop
ret
split ENDP
;----------------- End split -----------------
END main