|
|
1.1.1.2 root 1: /*
2: **
3: ** 80386 assembly primitives for RSA library
4: ** GNU gas syntax, tested with gcc v1.39
5: **
6: ** Compile with UNIT32 defined, MULTUNIT must be unsigned long
7: ** assemble this file with gcc -c (file should have .S suffix)
8: **
9: ** Written by Branko Lankester ([email protected]) 11/18/91
10: ** Last revised: 12/01/92
11: **
12: */
13:
1.1.1.3 ! root 14: #ifndef SYSV
1.1.1.2 root 15: #ifdef __STDC__
16: #define ENTRY(name) .align 4 ; .globl _##name ; _##name:
17: #else
18: #define ENTRY(name) .align 4 ; .globl _/**/name ; _/**/name:
19: #endif
1.1.1.3 ! root 20: #else /* SYSV */
1.1.1.2 root 21: #define ENTRY(name) .align 4 ; .globl name ; name:
22: #endif
23:
24: .text
25:
26: /*
27: ********************* set precision ********************
28: */
29: ENTRY (P_SETP)
30: pushl %ebp
31: movl %esp,%ebp
32: pushl %ebx
33: pushl %ecx
34: pushl %edx
35: movl 8(%ebp),%eax
36: addl $0x1f,%eax
37: shrl $5,%eax
38: movl %eax,%ebx
39: shrl $3,%eax
40: movl %eax,prec8
41: andl $7,%ebx
42:
43: movl $add_ref,%eax
44: movl %eax,%ecx
45: subl $add_1ref,%eax
46: mul %ebx
47: subl %eax,%ecx
48: movl %ecx,addp
49: movl $sub_ref,%ecx
50: subl %eax,%ecx
51: movl %ecx,subp
52:
53: movl $rot_ref,%eax
54: movl %eax,%ecx
55: subl $rot_1ref,%eax
56: mul %ebx
57: subl %eax,%ecx
58: movl %ecx,rotp
59:
60: movl $mul_ref,%eax
61: movl %eax,%ecx
62: subl $mul_1ref,%eax
63: mul %ebx
64: subl %eax,%ecx
65: movl %ecx,mulp
66:
67: popl %edx
68: popl %ecx
69: popl %ebx
70: leave
71: ret
72:
73:
74:
75: /*
76: ********************* mpi add with carry ********************
77: */
78:
79: #define ADDU lodsl ; adcl %eax,(%ebx,%esi)
80:
81: ENTRY (P_ADDC)
82: pushl %ebp
83: movl %esp,%ebp
84: pushl %ebx
85: pushl %ecx
86: pushl %esi
87: pushl %edi
88: movl 12(%ebp),%esi
89: movl 8(%ebp),%ebx
90: subl %esi,%ebx
91: subl $4,%ebx
92: cld
93: movl 16(%ebp),%eax
94: movl prec8,%ecx
95: orl %ecx,%ecx
96: rcrl $1,%eax /* set the carry flag */
97: jz add_units /* z-flag set by orl %ecx,%ecx */
98: add_8u:
99: ADDU ; ADDU ; ADDU ; ADDU ; ADDU ; ADDU ; ADDU ; ADDU
100: loop add_8u
101: add_units:
102: jmp *addp
103: ADDU ; ADDU ; ADDU ; ADDU ; ADDU ; ADDU
104: add_1ref: /* label to compute size of codes */
105: ADDU
106: add_ref:
107: rcll $1,%eax
108: andl $1,%eax
109:
110: popl %edi
111: popl %esi
112: popl %ecx
113: popl %ebx
114: leave
115: ret
116:
117:
118: /*
119: ********************* mpi subtract with borrow ********************
120: */
121:
122: #define SUBU lodsl ; sbbl %eax,(%ebx,%esi)
123:
124: ENTRY (P_SUBB)
125: pushl %ebp
126: movl %esp,%ebp
127: pushl %ebx
128: pushl %ecx
129: pushl %esi
130: pushl %edi
131: movl 12(%ebp),%esi
132: movl 8(%ebp),%ebx
133: subl %esi,%ebx
134: subl $4,%ebx
135: cld
136: movl 16(%ebp),%eax
137: movl prec8,%ecx
138: orl %ecx,%ecx
139: rcrl $1,%eax
140: jz sub_units
141: sub_8u:
142: SUBU ; SUBU ; SUBU ; SUBU ; SUBU ; SUBU ; SUBU ; SUBU
143: loop sub_8u
144: sub_units:
145: jmp *subp
146: SUBU ; SUBU ; SUBU ; SUBU ; SUBU ; SUBU ; SUBU
147: sub_ref:
148: rcll $1,%eax
149: andl $1,%eax
150:
151: popl %edi
152: popl %esi
153: popl %ecx
154: popl %ebx
155: leave
156: ret
157:
158:
159:
160: /*
161: ********************* mpi rotate left ********************
162: */
163:
164: #define ROTU rcll $1,(%ebx,%esi,4) ; incl %esi
165:
166: ENTRY (P_ROTL)
167: pushl %ebp
168: movl %esp,%ebp
169: pushl %ebx
170: pushl %ecx
171: pushl %esi
172: movl 8(%ebp),%ebx
173: movl 12(%ebp),%eax
174: xorl %esi,%esi
175: movl prec8,%ecx
176: orl %ecx,%ecx
177: rcrl $1,%eax /* set the carry flag */
178: jz rot_units /* z-flag set by orl %ecx,%ecx */
179: rot_8u:
180: rcll $1,(%ebx)
181: rcll $1,4(%ebx)
182: rcll $1,8(%ebx)
183: rcll $1,12(%ebx)
184: rcll $1,16(%ebx)
185: rcll $1,20(%ebx)
186: rcll $1,24(%ebx)
187: rcll $1,28(%ebx)
188: leal 32(%ebx),%ebx
189: loop rot_8u
190: rot_units:
191: jmp *rotp
192: ROTU ; ROTU ; ROTU ; ROTU ; ROTU ; ROTU ; ROTU
193: rot_1ref:
194: ROTU
195: rot_ref:
196: rcll $1,%eax
197: andl $1,%eax
198:
199: popl %esi
200: popl %ecx
201: popl %ebx
202: leave
203: ret
204:
205: /*
206: ********************* mpi multiply ********************
207: */
208: #define MULU \
209: lodsl ; \
210: mull %ebp ; \
211: addl %ebx,%eax ; \
212: adcl $0,%edx ; \
213: addl (%edi),%eax ; \
214: adcl $0,%edx ; \
215: movl %edx,%ebx ; \
216: stosl
217:
218: ENTRY (P_SMULA)
219: pushl %ebp
220: movl %esp,%ebp
221: pushl %ebx
222: pushl %ecx
223: pushl %edx
224: pushl %esi
225: pushl %edi
226:
227: xorl %ebx,%ebx
228: movl prec8,%ecx
229: movl 8(%ebp),%edi
230: movl 12(%ebp),%esi
231: movl 16(%ebp),%ebp
232: cld
233: orl %ecx,%ecx
234: jz mul_units
235: mul_8u:
236: MULU ; MULU ; MULU ; MULU ; MULU ; MULU ; MULU ; MULU
237: decl %ecx
238: jnz mul_8u /* offset too big for loop */
239: mul_units:
240: jmp *mulp
241: MULU ; MULU ; MULU ; MULU ; MULU ; MULU
242: mul_1ref:
243: MULU
244: mul_ref:
245: addl %ebx,(%edi)
246:
247: popl %edi
248: popl %esi
249: popl %edx
250: popl %ecx
251: popl %ebx
252: popl %ebp
253: ret
254:
255:
256: .lcomm _reciph,4
257: .lcomm _recipl,4
258: .lcomm _mshift,4
259:
260: ENTRY (p_setrecip)
261: movl 4(%esp),%eax
262: movl %eax,_reciph
263: movl 8(%esp),%eax
264: movl %eax,_recipl
265: movl 12(%esp),%eax
266: movl %eax,_mshift
267: ret
268:
269:
270: ENTRY (p_quo_digit)
271: pushl %ebp
272: pushl %ebx
273: pushl %esi
274: pushl %edi
275:
276: movl 20(%esp),%esi /* dividend */
277: movl -8(%esi),%eax /* dividend[-2] */
278: notl %eax
279: mull _reciph
280: addl _reciph,%eax
281: adcl $0,%edx
282: movl %eax,%ebx
283: movl %edx,%edi /* di:bx = q1 */
284:
285: movl -4(%esi),%eax /* dividend[-1] */
286: notl %eax
287: mull _recipl
288: incl %edx /* dx:ax = q2 */
289:
290: movl %edx,%ebp
291: andl %edi,%ebp
292: andl $1,%ebp /* bp = lsb_factor */
293:
294: addl %ebx,%eax
295: adcl %edx,%edi
296: rcrl $1,%edi /* di = MS word of q0 */
297:
298: movl -4(%esi),%eax /* dividend [-1] */
299: notl %eax
300: mull _reciph
301: movl %eax,%ebx
302: movl %edx,%ecx /* cx:bx = q1 */
303:
304: movl (%esi),%eax /* dividend[0] */
305: notl %eax
306: mull _recipl /* dx:ax = q2 */
307: xorl %ebx,%eax
308: andl %eax,%ebp /* lsb correction */
309: xorl %ebx,%eax /* restore ax */
310:
311: addl %ebx,%eax
312: adcl %ecx,%edx
313: rcrl $1,%edx
314: rcrl $1,%eax /* dx:ax = q */
315:
316: addl %edi,%eax /* + scaled q0 */
317: adcl $0,%edx
318: addl %ebp,%eax /* + lsb correction */
319: adcl $0,%edx /* q */
320:
321: shll $1,%eax
322: rcll $1,%edx
323: rcll $1,%eax
324: rcll $1,%edx
325: rcll $1,%eax
326: andl $3,%eax
327: movl %eax,%ecx
328: movl %edx,%ebx /* bx:cx = q >> 14 */
329:
330: movl (%esi),%eax /* dividend[0] */
331: notl %eax
332: mull _reciph
333: shll $1,%eax
334: rcll $1,%edx
335: addl %ebx,%eax
336: adcl %ecx,%edx /* q */
337:
338: movl _mshift,%ecx
339: cmpl $32,_mshift
1.1.1.3 ! root 340: je L2
! 341: #if 0
! 342: shrl %cl,%eax
! 343: movl %edx,%ebx
! 344: shrl %cl,%edx
! 345: negl %ecx
! 346: addl $32,%ecx
! 347: shll %cl,%ebx
! 348: addl %ebx,%eax /* dx:ax = q >> mshift */
! 349: #else
! 350: #ifdef SYSV
! 351: shrdl %edx,%eax
! 352: #else
1.1.1.2 root 353: shrdl %cl,%edx,%eax
1.1.1.3 ! root 354: #endif
1.1.1.2 root 355: shrl %cl,%edx
1.1.1.3 ! root 356: #endif
1.1.1.2 root 357:
358: orl %edx,%edx
1.1.1.3 ! root 359: je L1
1.1.1.2 root 360: movl $-1,%eax
1.1.1.3 ! root 361: jmp L1
! 362: L2:
1.1.1.2 root 363: xchgl %edx,%eax
1.1.1.3 ! root 364: L1:
1.1.1.2 root 365: popl %edi
366: popl %esi
367: popl %ebx
368: popl %ebp
369: ret
370:
371: .lcomm prec8,4
372: .lcomm addp,4
373: .lcomm subp,4
374: .lcomm rotp,4
375: .lcomm mulp,4
376:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.