-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.asm
600 lines (502 loc) · 13.7 KB
/
code.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
TITLE RSA-FROM-NCU(RSA.asm)
INCLUDE Irvine32.inc
INCLUDE macros.inc
main EQU start@0
;-------------------------------------------------------------------------------------
; RSAlgorithm - encrypte and decrypte function |
; power - other useful function |
; readInteger - easy to print message and read an integer |
; printMessage - easy to print message and an integer |
; printlnMessage - easy to print message with change line |
; setup - rsa generating function |
; coprimeTest - setup function's public key test |
; generatePrivateKey - setup function's generate a private key to decrypte message |
;-------------------------------------------------------------------------------------
RSAlgorithm PROTO, Mt:DWORD, E:DWORD, N:DWORD
power PROTO , a:DWORD, n:DWORD
readInteger PROTO, msg:PTR BYTE, target:PTR DWORD
printMessage PROTO, msg:PTR BYTE, value:DWORD
printlnMessage PROTO, msg:PTR BYTE, value:DWORD
setup PROTO, P:PTR DWORD, Q:PTR DWORD, N:PTR DWORD, PN:PTR DWORD, E:PTR DWORD, D:PTR DWORD
coprimeTest PROTO, num:DWORD, N:DWORD, PN:DWORD
generatePrivateKey PROTO, E:DWORD, N:DWORD, D:PTR DWORD
readFileSync PROTO, bufPTR:PTR DWORD, bufSize:DWORD
encrypteBuffer PROTO, bufPTR:PTR DWORD, bufSize:DWORD, target:PTR DWORD
.data
; primeNumber - can be used prime number list
primeNumber DWORD 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191
whitespace BYTE " ", 0
readPrimeP BYTE "Enter P ( P must be a random prime number ): ", 0
readPrimeQ BYTE "Enter Q ( Q must be a random prime number ): ", 0
readPubKey BYTE "Enter E(Public key), 1 < E < Φ(n): ", 0
readNumMsg BYTE "Enter an Integer: ", 0
useableMsg BYTE "Useable number:", 0
pemDKeyMsg BYTE "Your private key: ", 0
plaintextMsg BYTE "Your Plaintext: ", 9, 0
ciphertextMsg BYTE "Your Ciphertext: ", 9, 0
showModMsg BYTE "Modulus N(mod N): ", 0
showPhiMsg BYTE "Totient N(Φ(N)): ", 0
match_msg BYTE 10h,"matches",0
Ppri DWORD 0 ; prime number P
Qpri DWORD 0 ; prime number Q
Nmod DWORD 0 ; N is P*Q
PhiN DWORD 0 ; Φ(N) = (p-1)(q-1)
PubE DWORD 0 ; Public key E
PemD DWORD 0 ; Private key D
OMsg DWORD 0 ; Plaintext Message
CMsg DWORD 0 ; Ciphertext Message
Ctmp DWORD 1 ; temp memory (RSA Calculation)
BUFFER_SIZE = 500000
buffer BYTE BUFFER_SIZE DUP(?) ; plaintext's buffer
EBUFFER_SIZE = 2000000
Ebuffer BYTE EBUFFER_SIZE DUP(?) ; encrypte's buffer
filename BYTE 80 DUP(0)
fileHandle HANDLE ?
fileSize DWORD 0
mode BYTE 0
.code
readInteger PROC USES eax edx edi, msg:PTR BYTE, target:PTR DWORD
; copy target address to edi then copy the value to target address
mov edi, target
;print message
mov edx, msg
call WriteString
; read value then write into the [target]
call ReadDec
mov [edi], eax
ret
readInteger ENDP
printMessage PROC USES eax edx, msg:PTR BYTE, value:DWORD
; write message
mov edx, msg
call WriteString
; write integer
mov eax, value
call WriteDec
ret
printMessage ENDP
printlnMessage PROC USES eax edx, msg:PTR BYTE, value:DWORD
; call printMessage
INVOKE printMessage, msg, value
; change line
call Crlf
ret
printlnMessage ENDP
setup PROC USES eax ebx ecx edx, P:PTR DWORD, Q:PTR DWORD, N:PTR DWORD, PN:PTR DWORD, E:PTR DWORD, D:PTR DWORD
; --------------------
; print all useable P and Q then P can not be equal Q
call Crlf
call Crlf
lea edi, primeNumber
call Crlf
mov edx, OFFSET useableMsg
call WriteString
printAllUseableNumber:
mov ecx, [edi]
cmp ecx, 181
jg printAllUseableNumberEnd
INVOKE printMessage, OFFSET whitespace, ecx
add edi, 4
jmp printAllUseableNumber
printAllUseableNumberEnd:
call Crlf
call Crlf
; --------------------
; --------------------
; read P and Q
INVOKE readInteger, OFFSET readPrimeP, OFFSET Ppri
INVOKE readInteger, OFFSET readPrimeQ, OFFSET Qpri
; --------------------
; --------------------
; calculate modules N
mov eax, Ppri
mul Qpri
mov Nmod, eax
; --------------------
; --------------------
; calculate Totient N
mov eax, Ppri
mov ebx, Qpri
dec eax
dec ebx
mul ebx
mov PhiN, eax
; --------------------
; --------------------
; Print N and Phi(N)
INVOKE printlnMessage, OFFSET showModMsg, Nmod
INVOKE printlnMessage, OFFSET showPhiMsg, PhiN
; --------------------
; --------------------
; generate useable Public key (number e)
lea edi, primeNumber
call Crlf
mov edx, OFFSET useableMsg
call WriteString
publicCoprimeTest:
; 1 < ecx < Phi(N)
mov ecx, [edi]
cmp ecx, 181
jg publicCoprimeTestEnd
cmp ecx, PhiN
jge publicCoprimeTestEnd
INVOKE coprimeTest, ecx, Nmod, PhiN
; call dumpRegs
cmp eax, 6666
je dontPrintE
INVOKE printMessage, OFFSET whitespace, ecx
dontPrintE:
add edi, 4
jmp publicCoprimeTest
publicCoprimeTestEnd:
call Crlf
call Crlf
; --------------------
; --------------------
; read public key ( E ) then generate private key
INVOKE readInteger, OFFSET readPubKey, OFFSET PubE
INVOKE generatePrivateKey, PubE, Nmod, OFFSET PemD
INVOKE printlnMessage, OFFSET pemDKeyMsg, PemD
; --------------------
ret
setup ENDP
coprimeTest PROC, num:DWORD, N:DWORD, PN:DWORD
; --------------------
; test num is coprime with N or not
mov eax, N
CDQ
div num
cmp edx, 0
je returnNCP ; not coprime with N return 6666
; --------------------
; --------------------
; test num is coprime with PhiN or not
mov eax, PhiN
CDQ
div num
cmp edx, 0
je returnNCP ; not coprime with PhiN
jmp return
; --------------------
returnNCP:
mov eax, 6666
return:
ret
coprimeTest ENDP
generatePrivateKey PROC, E:DWORD, N:DWORD, D:PTR DWORD
; --------------------
; setup first private
xor eax,eax
mov ebx, 2
; --------------------
; --------------------
; Start calculate private key
cal:
inc ebx
mov eax, PubE
mul ebx
div PhiN
cmp edx, 1
jne cal
endCal:
; --------------------
; --------------------
; if e*d !≡ 1 (mod N) then calculate an new key
cmp ebx, PubE
je cal
mov PemD, ebx
; --------------------
ret
generatePrivateKey ENDP
readFileSync PROC USES ecx edx esi, bufPTR:PTR DWORD, bufSize:DWORD
mWrite "Enter an input filename: "
mov edx,OFFSET filename
mov ecx,SIZEOF filename
call ReadString
mov edx,OFFSET filename
call OpenInputFile
mov fileHandle,eax
cmp eax,INVALID_HANDLE_VALUE
jne file_ok
mWrite <"Cannot open file",0dh,0ah>
mov eax, 1 ; can not open file
jmp quit
file_ok:
mov edx,bufPTR
mov ecx,BUFFER_SIZE
call ReadFromFile
jnc check_buffer_size
mWrite "Error reading file. "
mov eax, 2 ; failed to read file
call WriteWindowsMsg
jmp close_file
check_buffer_size:
cmp eax,BUFFER_SIZE
jb buf_size_ok
mWrite <"Error: Buffer too small for the file",0dh,0ah>
mov eax, 3 ; Buffer too small for the file
jmp quit
buf_size_ok:
mov ebx, eax
mov esi, bufPTR
add esi, eax
push eax
mov al, 0
mov [esi], al ;mov bufPTR[eax],0
pop eax
; mWrite "File size: "
; call WriteDec
; call Crlf
; mWrite <"Buffer:",0dh,0ah,0dh,0ah>
; mov edx,bufPTR
; call WriteString
; call Crlf
close_file:
mov eax,fileHandle
call CloseFile
mov eax, 0 ; no error
quit:
ret
readFileSync ENDP
writeFileSync PROC, bufPTR:PTR DWORD, bufSize:DWORD
mWrite "Enter an output filename: "
mov edx,OFFSET filename
mov ecx,SIZEOF filename
call ReadString
mov edx,OFFSET filename
call CreateOutputFile
mov fileHandle, eax
cmp eax, INVALID_HANDLE_VALUE ; error found?
jne file_ok ; no: skip
mWrite <"Cannot create file",0dh,0ah,0>
; mov edx,OFFSET str1 ; display error
; call WriteString
jmp quit
file_ok:
mov eax,fileHandle
mov edx,bufPTR
mov ecx,bufSize
call WriteToFile
; mov bytesWritten,eax ; save return value
call CloseFile
; Display the return value.
mWrite <"file write into ">
mov edx, OFFSET filename
call WriteString
call Crlf
; mov eax,bytesWritten
; mov eax, bufSize
; call WriteDec
; call Crlf
quit:
ret
writeFileSync ENDP
RSAlgorithm PROC USES ecx edx, M:DWORD, d:DWORD, N:DWORD
;https://github.com/tomorrow-have-advanced-calculus/algorithm/blob/master/loop.js
;int ctmp = 1
mov Ctmp, 1
while_1:
mov eax, M ; eax = m = c
mov ecx, 1 ; ecx = s = 1
while_m_lessthen_n:
cmp eax, N
jae while_m_lessthen_n_End
cmp ecx, d
jne dont_return_m_mod_n
; return m % n
cdq
div N
mov eax, edx
mul Ctmp
cdq
div N
mov eax, edx
jmp return
dont_return_m_mod_n:
mul M
inc ecx
jmp while_m_lessthen_n
while_m_lessthen_n_End:
push eax ; backup m
; a = d%s, b = d/s
mov eax, d
xor edx, edx
cdq
idiv ecx ; b = eax, a = edx
mov d, eax ; d = b
; mov eax, edx; eax = a, edx = b, then eax = power(M, eax)
INVOKE power, M, edx ; eax = power(M, a)
mul Ctmp ; eax = eax*Ctmp
cdq
idiv N ; edx = eax % n
mov Ctmp, edx ; storage Ctmp => Ctmp = ( power(c, a)*Ctmp )%n
pop eax ; eax = m
cdq
idiv n ; edx = m % n
mov M, edx ; c = m%n
jmp while_1
return:
ret
RSAlgorithm ENDP
power PROC USES ecx, a:DWORD, n:DWORD
mov eax, 1
mov ecx, n
cmp ecx, 0
je returnVal
l:
mul a
loop l
returnVal:
ret
power ENDP
encrypteBuffer PROC USES eax ecx esi edi, bufPTR:PTR DWORD, bufSize:DWORD, target:PTR DWORD
mov esi, bufPTR
mov ecx, bufSize
mov edi, target
encrypteLoop:
xor eax, eax
mov al, [esi]
INVOKE RSAlgorithm, eax, PubE, Nmod
call WriteDec
call Crlf
push ecx
mov ecx, 4
writeIntoTheBuffer:
mov [edi], al
shr eax, 8
inc edi
loop writeIntoTheBuffer
inc esi
pop ecx
loop encrypteLoop
ret
encrypteBuffer ENDP
decrypteBuffer PROC USES eax ecx esi edi, bufPTR:PTR DWORD, bufSize:DWORD, target:PTR DWORD
mov eax, bufSize
mov ecx, 4
CDQ
idiv ecx
xchg eax, ecx
mov esi, bufPTR
mov edi, target
solveBuf:
mov eax, [esi]
push eax
INVOKE RSAlgorithm, eax, PemD, Nmod
call WriteDec
call Crlf
mov [edi], al
inc edi
add esi, 4
loop solveBuf
ret
decrypteBuffer ENDP
main PROC
mWrite <" _________________menu_________________", 0dh, 0ah>
mWrite <"| |", 0dh, 0ah>
mWrite <"| 1. Encrypte file |", 0dh, 0ah>
mWrite <"| 2. Decrypte file |", 0dh, 0ah>
mWrite <"| 3. use Public key encrypte file |",0dh, 0ah>
mWrite <"| 4. use Private key decrypte file |", 0dh, 0ah>
mWrite <"| 5. en/decrypte singal message |", 0dh, 0ah>
mWrite <"|______________________________________|", 0dh, 0ah>
mWrite <"| <any key>. Quit Program |", 0dh, 0ah>
mWrite <"|______________________________________|", 0dh, 0ah>
mWrite <0dh, 0ah>
;mWrite <"---------menu---------", 0dh, 0ah,
; "1. Encrypte file", 0dh, 0ah,
; "2. Decrypte file", 0dh, 0ah,
; "-. Quit Program", 0dh, 0ah,0
;>
call ReadChar
mov mode, al
cmp al, 49
je setupKey
cmp al, 50
je setupKey
cmp al, 51
je readPublicKey
cmp al, 52
je readPrivateKey
cmp al, 53
je setupKey
mov eax, 404
jmp quitWithError
setupKey:
INVOKE setup, OFFSET Ppri, OFFSET Qpri, OFFSET Nmod, OFFSET PhiN, OFFSET PubE, OFFSET PemD
call Crlf
mov al, mode
cmp al, 49
je encrypte
cmp al, 50
je decrypte
cmp al, 53
je crypteSingalMsg
jmp quitWithError
readPublicKey:
mWrite <"Enter Public key (E): ">
call ReadDec
mov PubE, eax
mWrite <"Enter Asymmetric key (N): ">
call ReadDec
mov Nmod, eax
jmp encrypte
readPrivateKey:
mWrite <"Enter Private key (D): ">
call ReadDec
mov PemD, eax
mWrite <"Enter Asymmetric key (N): ">
call ReadDec
mov Nmod, eax
jmp decrypte
encrypte:
INVOKE readFileSync, OFFSET buffer, BUFFER_SIZE
cmp ebx, 0
je quitWithError
mWrite <"Buffer:",0dh,0ah,"--------------------",0dh,0ah>
mov edx,OFFSET buffer
call WriteString
mWrite <0dh,0ah,"--------------------",0dh,0ah,0dh,0ah>
mWrite <"Ciphertext:",0dh,0ah,"--------------------",0dh,0ah>
INVOKE encrypteBuffer, OFFSET buffer, ebx,OFFSET Ebuffer
mWrite <0dh,0ah,"--------------------",0dh,0ah,0dh,0ah>
;Write Ciphertext into an file
mov eax, 4
xchg eax, ebx
mul ebx
INVOKE writeFileSync, OFFSET Ebuffer, eax
jmp quit
decrypte:
INVOKE readFileSync, OFFSET Ebuffer, EBUFFER_SIZE
cmp ebx, 0
je quitWithError
mWrite <"Ciphertext:",0dh,0ah,"--------------------",0dh,0ah>
mov edx,OFFSET Ebuffer
call WriteString
mWrite <0dh,0ah,"--------------------",0dh,0ah,0dh,0ah>
mWrite <"Plaintext:",0dh,0ah,"--------------------",0dh,0ah>
INVOKE decrypteBuffer, OFFSET Ebuffer, ebx,OFFSET buffer
mWrite <0dh,0ah,"--------------------",0dh,0ah,0dh,0ah>
mov eax, 4
xchg eax, ebx
CDQ
idiv ebx
INVOKE writeFileSync, OFFSET buffer, eax
jmp quit
crypteSingalMsg:
mWrite <"Message: ">
call ReadDec
INVOKE RSAlgorithm, eax, PubE, Nmod
mWrite "Result: "
call WriteDec
call Crlf
jmp quit
quit:
exit
quitWithError:
mWrite <0dh, 0ah, "Error reading file, error code: ">
call WriteDec
mWrite <0dh, 0ah, 0dh, 0ah>
jmp quit
main ENDP
END main