Rev 485 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
223 | Ghost | 1 | ;testing speed of RSA coding (256 bit) |
2 | ;for program CPUID (WildWest) |
||
3 | ;author: halyavin |
||
4 | |||
5 | ;structure of long number: |
||
6 | ;rd 1 : length of number |
||
7 | ;rd 39: digits of number |
||
8 | ;length of zero number is equal to 1. |
||
9 | |||
10 | number_digits=39 |
||
11 | number_size=4*(number_digits+1) |
||
12 | |||
13 | ;calling convention: |
||
14 | ;procedures can change eax,ecx,edx |
||
15 | |||
16 | init_rsatest: |
||
17 | ;to do: add initialization which depends on processor. |
||
18 | ret |
||
19 | |||
20 | |||
21 | set_zero: |
||
22 | ;eax - address of number |
||
23 | ;[eax]:=0 |
||
24 | ;result: eax isn't changed |
||
25 | xor ecx,ecx |
||
26 | xor edx,edx |
||
27 | inc edx |
||
28 | mov dword [eax],edx |
||
29 | mov edx,39 |
||
30 | add eax,4 |
||
31 | .loop: |
||
32 | mov dword [eax],ecx |
||
33 | add eax,4 |
||
34 | dec edx |
||
35 | jnz .loop |
||
36 | sub eax,160 ;size of number |
||
37 | ret |
||
38 | |||
39 | ladd: |
||
40 | ;eax - first number |
||
41 | ;edx - second number |
||
42 | ;[eax]:=[eax]+[edx] |
||
43 | pushad |
||
44 | mov ecx,dword [eax] |
||
45 | mov esi,dword [edx] |
||
46 | mov ebp,eax |
||
47 | cmp ecx,esi |
||
48 | jg .l1 |
||
49 | mov ecx,esi |
||
50 | .l1: |
||
51 | add eax,4 |
||
52 | add edx,4 |
||
53 | xor ebx,ebx |
||
54 | xor esi,esi |
||
55 | mov edi,ecx |
||
56 | .l2: |
||
57 | add ebx,dword [eax] |
||
58 | adc ebx,dword [edx] |
||
59 | adc esi,esi |
||
60 | mov dword [eax],ebx |
||
61 | mov ebx,esi |
||
62 | add eax,4 |
||
63 | xor esi,esi |
||
64 | add edx,4 |
||
65 | dec ecx |
||
66 | jnz .l2 |
||
67 | test ebx,ebx |
||
68 | jz .lend |
||
69 | inc dword [eax] |
||
70 | inc edi |
||
71 | .lend: |
||
72 | mov eax,ebp |
||
73 | mov dword [eax],edi |
||
74 | popad |
||
75 | ret |
||
76 | |||
77 | lsub: |
||
78 | ;eax - first number |
||
79 | ;edx - second number |
||
80 | ;[eax]=[eax]-[edx] |
||
81 | ;[eax]>=[edx] |
||
82 | pushad |
||
83 | mov ecx,dword [eax] |
||
84 | mov edi,ecx |
||
85 | mov ebp,eax |
||
86 | add eax,4 |
||
87 | add edx,4 |
||
88 | xor ebx,ebx |
||
89 | xor esi,esi |
||
90 | .l1: |
||
91 | mov esi,dword [eax] |
||
92 | sub esi,ebx |
||
93 | sbb esi,dword [edx] |
||
94 | mov dword [eax],esi |
||
95 | sbb esi,esi |
||
96 | mov ebx,esi |
||
97 | neg ebx |
||
98 | add eax,4 |
||
99 | add edx,4 |
||
100 | dec ecx |
||
101 | jnz .l1 |
||
102 | dec edi |
||
103 | jz .lend |
||
104 | sub eax,4 |
||
105 | .l2: |
||
106 | mov ebx,dword [eax] |
||
107 | test ebx,ebx |
||
108 | jnz .lend |
||
109 | sub eax,4 |
||
110 | dec edi |
||
111 | jnz .l2 |
||
112 | .lend: |
||
113 | inc edi |
||
114 | mov eax,ebp |
||
115 | mov [eax],edi |
||
116 | popad |
||
117 | ret |
||
118 | |||
119 | lcmp: |
||
120 | ;eax - address of number |
||
121 | ;edx - address of number |
||
122 | ;result: ecx=1 if eax>edx |
||
123 | ; ecx=0 if eax=edx |
||
124 | ; ecx=-1 if eax |
||
125 | push ebx |
||
126 | mov ebx,dword [eax] |
||
127 | cmp ebx,dword [edx] |
||
128 | jg .l1 |
||
129 | jl .l2 |
||
130 | push esi |
||
131 | push eax |
||
132 | push edx |
||
133 | lea eax,[eax+4*ebx] |
||
134 | lea edx,[edx+4*ebx] |
||
135 | .l3: |
||
136 | mov esi,dword [eax] |
||
137 | cmp esi,dword [edx] |
||
138 | ja .l4 |
||
139 | jb .l5 |
||
140 | sub eax,4 |
||
141 | sub edx,4 |
||
142 | dec ebx |
||
143 | jnz .l3 |
||
144 | xor ecx,ecx |
||
145 | .lend: |
||
146 | pop edx |
||
147 | pop eax |
||
148 | pop esi |
||
149 | pop ebx |
||
150 | ret |
||
151 | .l4: |
||
152 | mov ecx,1 |
||
153 | jmp .lend |
||
154 | .l5: |
||
155 | mov ecx,-1 |
||
156 | jmp .lend |
||
157 | .l1: |
||
158 | mov ecx,1 |
||
159 | pop ebx |
||
160 | ret |
||
161 | .l2: |
||
162 | mov ecx,-1 |
||
163 | pop ebx |
||
164 | ret |
||
165 | |||
166 | lshr: |
||
167 | ;eax - number |
||
168 | ;edx - shift |
||
169 | ;[eax]=[eax] shr edx |
||
170 | cmp edx,32 |
||
171 | jl .general |
||
172 | mov ecx,edx |
||
173 | shr ecx,5 |
||
174 | and edx,31 |
||
175 | push esi |
||
176 | mov esi,ecx |
||
177 | cmp ecx,dword [eax] |
||
178 | jng .norm |
||
179 | xor esi,esi |
||
180 | mov ecx,dword [eax] |
||
181 | .norm: |
||
182 | push edi |
||
183 | push ebp |
||
184 | mov ebp,eax |
||
185 | add eax,4 |
||
186 | .loop1: |
||
187 | test esi,esi |
||
188 | jz .endloop1 |
||
189 | mov edi,dword [eax+4*ecx] |
||
190 | mov dword [eax],edi |
||
191 | add eax,4 |
||
192 | dec esi |
||
193 | jmp .loop1 |
||
194 | .endloop1: |
||
195 | mov eax,ebp |
||
196 | mov esi,dword [eax] |
||
197 | lea eax,[eax+4*esi] |
||
198 | xor edi,edi |
||
199 | mov esi,ecx |
||
200 | .loop2: |
||
201 | mov dword [eax],edi |
||
202 | sub eax,4 |
||
203 | dec ecx |
||
204 | jnz .loop2 |
||
205 | mov eax,ebp |
||
206 | sub dword [eax],esi |
||
207 | jz .len0 |
||
208 | .lend1: |
||
209 | pop ebp |
||
210 | pop edi |
||
211 | pop esi |
||
212 | jmp .general |
||
213 | |||
214 | .len0: |
||
215 | inc dword [eax] |
||
216 | jmp .lend1 |
||
217 | |||
218 | .general: |
||
219 | test edx,edx |
||
220 | jz .lend |
||
221 | push ebx |
||
222 | push esi |
||
223 | push edi |
||
224 | push ebp |
||
225 | push eax |
||
226 | mov ebx,dword [eax] |
||
227 | lea eax,[eax+4*ebx] |
||
228 | mov ecx,edx |
||
229 | sub ecx,32 |
||
230 | neg ecx |
||
231 | mov esi,1 |
||
232 | shl esi,cl |
||
233 | dec esi |
||
234 | mov ecx,edx |
||
235 | xor ebp,ebp |
||
236 | .loop3: |
||
237 | mov edi,dword [eax] |
||
238 | ror edi,cl |
||
239 | mov edx,edi |
||
240 | and edx,esi |
||
241 | or edx,ebp |
||
242 | mov dword [eax],edx |
||
243 | mov edx,esi |
||
244 | not edx |
||
245 | and edi,edx |
||
246 | mov ebp,edi |
||
247 | sub eax,4 |
||
248 | dec ebx |
||
249 | jnz .loop3 |
||
250 | pop eax |
||
251 | mov ebx,dword [eax] |
||
252 | mov ecx,dword [eax+4*ebx] |
||
253 | dec ebx |
||
254 | jz .norm1 |
||
255 | test ecx,ecx |
||
256 | jnz .norm1 |
||
257 | dec dword [eax] |
||
258 | .norm1: |
||
259 | pop ebp |
||
260 | pop edi |
||
261 | pop esi |
||
262 | pop ebx |
||
263 | .lend: |
||
264 | ret |
||
265 | |||
266 | lshl: |
||
267 | ;eax - number |
||
268 | ;edx - shift |
||
269 | ;[eax]:=[eax] shl edx |
||
270 | cmp edx,32 |
||
271 | jl .general |
||
272 | cmp dword [eax],1 |
||
273 | jnz .l1 |
||
274 | cmp dword [eax+4],0 |
||
275 | jnz .l1 |
||
276 | ret |
||
277 | .l1: |
||
278 | push esi |
||
279 | push ebx |
||
280 | mov ecx,edx |
||
281 | shr ecx,5 |
||
282 | and edx,31 |
||
283 | mov ebx,dword [eax] |
||
284 | add dword [eax],ecx |
||
285 | lea eax,[eax+4*ebx] |
||
286 | .loop1: |
||
287 | mov esi,dword [eax] |
||
288 | mov dword [eax+4*ecx],esi |
||
289 | sub eax,4 |
||
290 | dec ebx |
||
291 | jnz .loop1 |
||
292 | xor esi,esi |
||
293 | lea eax,[eax+4*ecx] |
||
294 | .loop2: |
||
295 | mov dword [eax],esi |
||
296 | sub eax,4 |
||
297 | dec ecx |
||
298 | jnz .loop2 |
||
299 | pop ebx |
||
300 | pop esi |
||
301 | jmp .general |
||
302 | |||
303 | .general: |
||
304 | test edx,edx |
||
305 | jz .lend |
||
306 | pushad |
||
307 | mov ecx,edx |
||
308 | mov ebx,1 |
||
309 | shl ebx,cl |
||
310 | dec ebx |
||
311 | mov edx,dword [eax] |
||
312 | xor esi,esi |
||
313 | lea eax,[eax+4*edx] |
||
314 | .loop3: |
||
315 | mov edi,dword [eax] |
||
316 | rol edi,cl |
||
317 | mov ebp,edi |
||
318 | and ebp,ebx |
||
319 | add esi,ebp |
||
320 | mov dword [eax+4],esi |
||
321 | mov ebp,ebx |
||
322 | not ebp |
||
323 | and edi,ebp |
||
324 | mov esi,edi |
||
325 | sub eax,4 |
||
326 | dec edx |
||
327 | jnz .loop3 |
||
328 | mov dword [eax+4],esi |
||
329 | mov edx,dword [eax] |
||
330 | cmp dword [eax+4*edx+4],0 |
||
331 | jz .l2 |
||
332 | inc dword [eax] |
||
333 | .l2: |
||
334 | popad |
||
335 | .lend: |
||
336 | ret |
||
337 | |||
338 | lwrite_bit: |
||
339 | ;eax - address of number |
||
340 | ;edx - number of bit |
||
341 | ;set bit to 1. First bit has number 0. |
||
342 | mov ecx,edx |
||
343 | shr edx,5 |
||
344 | inc edx |
||
345 | cmp dword [eax],edx |
||
346 | jg .norm |
||
347 | mov dword [eax],edx |
||
348 | .norm: |
||
349 | push ebx |
||
350 | mov ebx,1 |
||
351 | shl ebx,cl |
||
352 | or dword [eax+4*edx],ebx |
||
353 | pop ebx |
||
354 | ret |
||
355 | |||
356 | lcopy: |
||
357 | ;eax - address of first number |
||
358 | ;edx - address of second number |
||
359 | ;[eax]:=[edx] |
||
360 | push ebx |
||
361 | push esi |
||
362 | push eax |
||
363 | push edx |
||
364 | push dword [eax] |
||
365 | mov ebx,dword [edx] |
||
366 | mov ecx,ebx |
||
367 | add edx,4 |
||
368 | add eax,4 |
||
369 | .loop1: |
||
370 | mov esi,dword [edx] |
||
371 | mov dword [eax],esi |
||
372 | add eax,4 |
||
373 | add edx,4 |
||
374 | dec ecx |
||
375 | jnz .loop1 |
||
376 | pop ecx |
||
377 | sub ecx,ebx |
||
378 | jng .l1 |
||
379 | xor esi,esi |
||
380 | .loop2: |
||
381 | mov dword [eax],esi |
||
382 | add eax,4 |
||
383 | dec ecx |
||
384 | jnz .loop2 |
||
385 | .l1: |
||
386 | pop edx |
||
387 | pop eax |
||
388 | mov dword [eax],ebx |
||
389 | pop esi |
||
390 | pop ebx |
||
391 | ret |
||
392 | |||
393 | lmul: |
||
394 | ;eax - first number |
||
395 | ;edx - second number |
||
396 | ;ecx - third number |
||
397 | ;[eax]:=[ecx]*[edx] |
||
398 | push ecx |
||
399 | push edx |
||
400 | call set_zero |
||
401 | pop edx |
||
402 | pop ecx |
||
403 | pushad |
||
404 | sub esp,16 ; local variables |
||
405 | mov ebp,edx |
||
406 | mov ebx,dword [edx] |
||
407 | mov dword [esp],ebx |
||
408 | mov ebx,dword [ecx] |
||
409 | mov dword [esp+4],ebx |
||
410 | mov ebx,eax |
||
411 | add ebp,4 |
||
412 | add ecx,4 |
||
413 | add ebx,4 |
||
414 | xor esi,esi |
||
415 | mov dword [esp+8],esi |
||
416 | .loop1: |
||
417 | xor esi,esi |
||
418 | mov dword [esp+12],esi |
||
419 | .loop2: |
||
420 | mov eax,dword [esp+12] |
||
421 | mov eax,dword [ecx+4*eax] |
||
422 | mov edi,dword [esp+8] |
||
423 | mov edi,dword [ebp+4*edi] |
||
424 | mul edi |
||
425 | add eax,esi |
||
426 | adc edx,0 |
||
427 | mov edi,dword [esp+12] |
||
428 | add edi,dword [esp+8] |
||
429 | add eax,dword [ebx+4*edi] |
||
430 | adc edx,0 |
||
431 | mov dword [ebx+4*edi],eax |
||
432 | mov esi,edx |
||
433 | inc dword [esp+12] |
||
434 | mov eax,dword [esp+4] |
||
435 | cmp dword [esp+12],eax |
||
436 | jnz .loop2 |
||
437 | test esi,esi |
||
438 | jz .loop1end |
||
439 | mov eax,dword [esp+12] |
||
440 | add eax,dword [esp+8] |
||
441 | mov [ebx+4*eax],esi |
||
442 | .loop1end: |
||
443 | inc dword [esp+8] |
||
444 | mov eax,dword [esp] |
||
445 | cmp dword [esp+8],eax |
||
446 | jnz .loop1 |
||
447 | add esp,16 |
||
448 | sub ebx,4 |
||
449 | mov ecx,39 |
||
450 | xor edx,edx |
||
451 | .loop3: |
||
452 | cmp dword [ebx+4*ecx],edx |
||
453 | jnz .loop3end |
||
454 | dec ecx |
||
455 | jnz .loop3 |
||
456 | .loop3end: |
||
457 | test ecx,ecx |
||
458 | jnz .l1 |
||
459 | inc ecx |
||
460 | .l1: |
||
461 | mov dword [ebx],ecx |
||
462 | popad |
||
463 | ret |
||
464 | |||
465 | lmod: |
||
466 | ;eax - address of first number |
||
467 | ;edx - address of second number |
||
468 | ;[eax]:=[eax] mod [edx] |
||
469 | mov ecx,dword [eax] |
||
470 | sub ecx,dword [edx] |
||
471 | jl .lend |
||
472 | pushad |
||
473 | inc ecx |
||
474 | mov ebp,eax |
||
475 | mov eax,edx |
||
476 | mov edi,eax |
||
477 | shl ecx,5 |
||
478 | mov edx,ecx |
||
479 | mov esi,ecx |
||
480 | call lshl |
||
481 | .loop: |
||
482 | mov eax,ebp |
||
483 | mov edx,edi |
||
484 | call lcmp |
||
485 | test ecx,ecx |
||
486 | jl .l1 |
||
487 | call lsub |
||
488 | .l1: |
||
489 | dec esi |
||
490 | js .endloop |
||
491 | mov eax,edx |
||
492 | mov edx,1 |
||
493 | call lshr |
||
494 | jmp .loop |
||
495 | .endloop: |
||
496 | popad |
||
497 | .lend: |
||
498 | ret |
||
499 | |||
500 | rsacode: |
||
501 | ;eax - address of message |
||
502 | ;edx - open key |
||
503 | ;ecx - address of module |
||
504 | ;esi - temparary number1 |
||
505 | ;edi - temparary number2 |
||
506 | ;esi - [eax]^edx mod (ecx) |
||
507 | ;[eax] is changed. |
||
508 | push eax |
||
509 | push edx |
||
510 | push ecx |
||
511 | xchg eax,esi |
||
512 | call set_zero |
||
513 | mov dword [eax+4],1 |
||
514 | mov esi,eax |
||
515 | mov eax,edi |
||
516 | call set_zero |
||
517 | .loop: |
||
518 | mov ecx,dword [esp+4] |
||
519 | test ecx,1 |
||
520 | jz .l1 |
||
521 | mov eax,edi |
||
522 | mov edx,esi |
||
523 | mov ecx,dword [esp+8] |
||
524 | call lmul |
||
525 | mov edx,dword [esp] |
||
526 | call lmod |
||
527 | mov eax,esi |
||
528 | mov edx,edi |
||
529 | call lcopy |
||
530 | .l1: |
||
531 | shr dword [esp+4],1 |
||
532 | jz .endloop |
||
533 | mov edx,dword [esp+8] |
||
534 | mov ecx,edx |
||
535 | mov eax,edi |
||
536 | call lmul |
||
537 | mov edx,dword [esp] |
||
538 | call lmod |
||
539 | mov eax,dword [esp+8] |
||
540 | mov edx,edi |
||
541 | call lcopy |
||
542 | jmp .loop |
||
543 | .endloop: |
||
544 | pop ecx |
||
545 | pop edx |
||
546 | pop eax |
||
547 | ret |
||
548 | |||
549 | rsa_test: |
||
550 | mov eax,num1 |
||
551 | mov dword [eax],16 |
||
552 | mov ecx,1 |
||
553 | mov edx,16 |
||
554 | .loop: |
||
555 | add eax,4 |
||
556 | mov dword [eax],ecx |
||
557 | dec edx |
||
558 | jnz .loop |
||
559 | mov eax,num1 |
||
560 | mov ebx,[iter] |
||
561 | mov dword [eax+4],ebx |
||
562 | inc dword [iter] |
||
563 | mov edx,[openkey] |
||
564 | mov ecx,module |
||
565 | mov esi,num2 |
||
566 | mov edi,num3 |
||
567 | call rsacode |
||
568 | |||
569 | ret |
||
570 | |||
571 | init_test: |
||
572 | xor eax,eax |
||
573 | mov [iter],eax |
||
574 | mov [openkey],10007 |
||
575 | mov eax,num2 |
||
576 | call set_zero |
||
577 | mov eax,num3 |
||
578 | call set_zero |
||
579 | ret |
||
580 | |||
581 | module: |
||
582 | ;module=p*q. |
||
583 | ;where |
||
584 | ;p= |
||
585 | ;dd 9 |
||
586 | ;dd 147,2,3,4,5,6,7,8,9 |
||
587 | ;times 30 dd 0 |
||
588 | ;q= |
||
589 | ;dd 9 |
||
590 | ;dd 163,4,5,6,7,8,9,10,11 |
||
591 | ;times 30 dd 0 |
||
592 | dd 17 |
||
593 | dd 23961,914,1232,1556,1887,2226,2574,2932,3301,288,308,314,305,280,238,178,99 |
||
594 | times 22 dd 0 |
||
595 | ;in decimal |
||
596 | ;p=1042128803351525332193283695592003066548124262686459610611886292768993621049491 |
||
597 | ;q=1273712981880077616387281148672409277231717442781838063285512054053473668300963 |
||
598 | ;n=1327372985619988354987062708438042005329282516404896732667039640816200186465366322016844458439816997285872910403676793109807015096535910981266920474905959833 |