summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn
diff options
context:
space:
mode:
authorcvs2svn <admin@example.com>1998-10-05 20:13:17 +0000
committercvs2svn <admin@example.com>1998-10-05 20:13:17 +0000
commite82f18fab47b698d93971f576f962a3068132912 (patch)
tree681519717892864935c3d0533cf171098afa649a /src/lib/libcrypto/bn
parent536c76cbb863bab152f19842ab88772c01e922c7 (diff)
downloadopenbsd-SSLeay_0_9_0b.tar.gz
openbsd-SSLeay_0_9_0b.tar.bz2
openbsd-SSLeay_0_9_0b.zip
This commit was manufactured by cvs2git to create tag 'SSLeay_0_9_0b'.SSLeay_0_9_0b
Diffstat (limited to 'src/lib/libcrypto/bn')
-rw-r--r--src/lib/libcrypto/bn/Makefile.ssl133
-rw-r--r--src/lib/libcrypto/bn/asm/bn-win32.asm689
-rw-r--r--src/lib/libcrypto/bn/asm/bn86unix.cpp752
-rw-r--r--src/lib/libcrypto/bn/asm/sparc.s462
-rw-r--r--src/lib/libcrypto/bn/asm/x86w16.asm297
-rw-r--r--src/lib/libcrypto/bn/asm/x86w32.asm362
-rw-r--r--src/lib/libcrypto/bn/bn.err27
-rw-r--r--src/lib/libcrypto/bn/bn.org502
-rw-r--r--src/lib/libcrypto/bn/bn_m.c169
-rw-r--r--src/lib/libcrypto/bn/bn_mulw.c366
-rw-r--r--src/lib/libcrypto/bn/bn_sub.c180
11 files changed, 3939 insertions, 0 deletions
diff --git a/src/lib/libcrypto/bn/Makefile.ssl b/src/lib/libcrypto/bn/Makefile.ssl
new file mode 100644
index 0000000000..9809d26cbc
--- /dev/null
+++ b/src/lib/libcrypto/bn/Makefile.ssl
@@ -0,0 +1,133 @@
1#
2# SSLeay/crypto/bn/Makefile
3#
4
5DIR= bn
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16BN_MULW= bn_mulw.o
17# or use
18#BN_MULW= bn86-elf.o
19
20CFLAGS= $(INCLUDES) $(CFLAG)
21
22ERR=bn
23ERRC=bn_err
24GENERAL=Makefile
25TEST=bntest.c exptest.c
26APPS=
27
28LIB=$(TOP)/libcrypto.a
29LIBSRC= bn_add.c bn_div.c bn_exp.c bn_lib.c bn_mod.c bn_mul.c \
30 bn_print.c bn_rand.c bn_shift.c bn_sub.c bn_word.c bn_blind.c \
31 bn_gcd.c bn_prime.c $(ERRC).c bn_sqr.c bn_mulw.c bn_recp.c bn_mont.c \
32 bn_mpi.c
33
34LIBOBJ= bn_add.o bn_div.o bn_exp.o bn_lib.o bn_mod.o bn_mul.o \
35 bn_print.o bn_rand.o bn_shift.o bn_sub.o bn_word.o bn_blind.o \
36 bn_gcd.o bn_prime.o $(ERRC).o bn_sqr.o $(BN_MULW) bn_recp.o bn_mont.o \
37 bn_mpi.o
38
39
40SRC= $(LIBSRC)
41
42EXHEADER= bn.h
43HEADER= bn_lcl.h bn_prime.h $(EXHEADER)
44
45ALL= $(GENERAL) $(SRC) $(HEADER)
46
47top:
48 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
49
50all: lib
51
52knuth: bn_knuth.c
53 cc -pg -I.. -I../../include bn_knuth.c -o knuth $(LIB) #../../../libefence.a
54
55knuth.fast: bn_knuth.c
56 cc -pg -fast -I.. -I../../include bn_knuth.c -o knuth $(LIB) #../../../libefence.a
57
58
59lib: $(LIBOBJ)
60 $(AR) $(LIB) $(LIBOBJ)
61 sh $(TOP)/util/ranlib.sh $(LIB)
62 @touch lib
63
64# elf
65asm/bn86-elf.o: asm/bn86unix.cpp
66 $(CPP) -DELF asm/bn86unix.cpp | as -o asm/bn86-elf.o
67
68# solaris
69asm/bn86-sol.o: asm/bn86unix.cpp
70 $(CC) -E -DSOL asm/bn86unix.cpp | sed 's/^#.*//' > asm/bn86-sol.s
71 as -o asm/bn86-sol.o asm/bn86-sol.s
72 rm -f asm/bn86-sol.s
73
74# a.out
75asm/bn86-out.o: asm/bn86unix.cpp
76 $(CPP) -DOUT asm/bn86unix.cpp | as -o asm/bn86-out.o
77
78# bsdi
79asm/bn86bsdi.o: asm/bn86unix.cpp
80 $(CPP) -DBSDI asm/bn86unix.cpp | as -o asm/bn86bsdi.o
81
82asm/bn86unix.cpp:
83 (cd asm; perl bn-586.pl cpp >bn86unix.cpp )
84
85files:
86 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
87
88links:
89 /bin/rm -f Makefile
90 $(TOP)/util/point.sh Makefile.ssl Makefile ;
91 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
92 $(TOP)/util/mklink.sh ../../test $(TEST)
93 $(TOP)/util/mklink.sh ../../apps $(APPS)
94
95install:
96 @for i in $(EXHEADER) ; \
97 do \
98 (cp $$i $(INSTALLTOP)/include/$$i; \
99 chmod 644 $(INSTALLTOP)/include/$$i ); \
100 done;
101
102exptest:
103 /bin/rm -f exptest
104 gcc -I../../include -g2 -ggdb -o exptest exptest.c ../../libcrypto.a
105
106div:
107 /bin/rm -f a.out
108 gcc -I.. -g div.c ../../libcrypto.a
109
110tags:
111 ctags $(SRC)
112
113tests:
114
115lint:
116 lint -DLINT $(INCLUDES) $(SRC)>fluff
117
118depend:
119 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
120
121dclean:
122 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
123 mv -f Makefile.new $(MAKEFILE)
124
125clean:
126 /bin/rm -f *.o */*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff bn_mulw.s
127
128errors:
129 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).org # special case .org
130 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
131 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
132
133# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/bn/asm/bn-win32.asm b/src/lib/libcrypto/bn/asm/bn-win32.asm
new file mode 100644
index 0000000000..017ea462b0
--- /dev/null
+++ b/src/lib/libcrypto/bn/asm/bn-win32.asm
@@ -0,0 +1,689 @@
1 ; Don't even think of reading this code
2 ; It was automatically generated by bn-586.pl
3 ; Which is a perl program used to generate the x86 assember for
4 ; any of elf, a.out, BSDI,Win32, or Solaris
5 ; eric <eay@cryptsoft.com>
6 ;
7 TITLE bn-586.asm
8 .386
9.model FLAT
10_TEXT SEGMENT
11PUBLIC _bn_mul_add_words
12
13_bn_mul_add_words PROC NEAR
14 push ebp
15 push ebx
16 push esi
17 push edi
18 ;
19 xor esi, esi
20 mov edi, DWORD PTR 20[esp]
21 mov ecx, DWORD PTR 28[esp]
22 mov ebx, DWORD PTR 24[esp]
23 and ecx, 4294967288
24 mov ebp, DWORD PTR 32[esp]
25 push ecx
26 jz $L000maw_finish
27L001maw_loop:
28 mov DWORD PTR [esp],ecx
29 ; Round 0
30 mov eax, DWORD PTR [ebx]
31 mul ebp
32 add eax, esi
33 mov esi, DWORD PTR [edi]
34 adc edx, 0
35 add eax, esi
36 adc edx, 0
37 mov DWORD PTR [edi],eax
38 mov esi, edx
39 ; Round 4
40 mov eax, DWORD PTR 4[ebx]
41 mul ebp
42 add eax, esi
43 mov esi, DWORD PTR 4[edi]
44 adc edx, 0
45 add eax, esi
46 adc edx, 0
47 mov DWORD PTR 4[edi],eax
48 mov esi, edx
49 ; Round 8
50 mov eax, DWORD PTR 8[ebx]
51 mul ebp
52 add eax, esi
53 mov esi, DWORD PTR 8[edi]
54 adc edx, 0
55 add eax, esi
56 adc edx, 0
57 mov DWORD PTR 8[edi],eax
58 mov esi, edx
59 ; Round 12
60 mov eax, DWORD PTR 12[ebx]
61 mul ebp
62 add eax, esi
63 mov esi, DWORD PTR 12[edi]
64 adc edx, 0
65 add eax, esi
66 adc edx, 0
67 mov DWORD PTR 12[edi],eax
68 mov esi, edx
69 ; Round 16
70 mov eax, DWORD PTR 16[ebx]
71 mul ebp
72 add eax, esi
73 mov esi, DWORD PTR 16[edi]
74 adc edx, 0
75 add eax, esi
76 adc edx, 0
77 mov DWORD PTR 16[edi],eax
78 mov esi, edx
79 ; Round 20
80 mov eax, DWORD PTR 20[ebx]
81 mul ebp
82 add eax, esi
83 mov esi, DWORD PTR 20[edi]
84 adc edx, 0
85 add eax, esi
86 adc edx, 0
87 mov DWORD PTR 20[edi],eax
88 mov esi, edx
89 ; Round 24
90 mov eax, DWORD PTR 24[ebx]
91 mul ebp
92 add eax, esi
93 mov esi, DWORD PTR 24[edi]
94 adc edx, 0
95 add eax, esi
96 adc edx, 0
97 mov DWORD PTR 24[edi],eax
98 mov esi, edx
99 ; Round 28
100 mov eax, DWORD PTR 28[ebx]
101 mul ebp
102 add eax, esi
103 mov esi, DWORD PTR 28[edi]
104 adc edx, 0
105 add eax, esi
106 adc edx, 0
107 mov DWORD PTR 28[edi],eax
108 mov esi, edx
109 ;
110 mov ecx, DWORD PTR [esp]
111 add ebx, 32
112 add edi, 32
113 sub ecx, 8
114 jnz L001maw_loop
115$L000maw_finish:
116 mov ecx, DWORD PTR 32[esp]
117 and ecx, 7
118 jnz $L002maw_finish2
119 jmp $L003maw_end
120$L002maw_finish2:
121 ; Tail Round 0
122 mov eax, DWORD PTR [ebx]
123 mul ebp
124 add eax, esi
125 mov esi, DWORD PTR [edi]
126 adc edx, 0
127 add eax, esi
128 adc edx, 0
129 dec ecx
130 mov DWORD PTR [edi],eax
131 mov esi, edx
132 jz $L003maw_end
133 ; Tail Round 1
134 mov eax, DWORD PTR 4[ebx]
135 mul ebp
136 add eax, esi
137 mov esi, DWORD PTR 4[edi]
138 adc edx, 0
139 add eax, esi
140 adc edx, 0
141 dec ecx
142 mov DWORD PTR 4[edi],eax
143 mov esi, edx
144 jz $L003maw_end
145 ; Tail Round 2
146 mov eax, DWORD PTR 8[ebx]
147 mul ebp
148 add eax, esi
149 mov esi, DWORD PTR 8[edi]
150 adc edx, 0
151 add eax, esi
152 adc edx, 0
153 dec ecx
154 mov DWORD PTR 8[edi],eax
155 mov esi, edx
156 jz $L003maw_end
157 ; Tail Round 3
158 mov eax, DWORD PTR 12[ebx]
159 mul ebp
160 add eax, esi
161 mov esi, DWORD PTR 12[edi]
162 adc edx, 0
163 add eax, esi
164 adc edx, 0
165 dec ecx
166 mov DWORD PTR 12[edi],eax
167 mov esi, edx
168 jz $L003maw_end
169 ; Tail Round 4
170 mov eax, DWORD PTR 16[ebx]
171 mul ebp
172 add eax, esi
173 mov esi, DWORD PTR 16[edi]
174 adc edx, 0
175 add eax, esi
176 adc edx, 0
177 dec ecx
178 mov DWORD PTR 16[edi],eax
179 mov esi, edx
180 jz $L003maw_end
181 ; Tail Round 5
182 mov eax, DWORD PTR 20[ebx]
183 mul ebp
184 add eax, esi
185 mov esi, DWORD PTR 20[edi]
186 adc edx, 0
187 add eax, esi
188 adc edx, 0
189 dec ecx
190 mov DWORD PTR 20[edi],eax
191 mov esi, edx
192 jz $L003maw_end
193 ; Tail Round 6
194 mov eax, DWORD PTR 24[ebx]
195 mul ebp
196 add eax, esi
197 mov esi, DWORD PTR 24[edi]
198 adc edx, 0
199 add eax, esi
200 adc edx, 0
201 mov DWORD PTR 24[edi],eax
202 mov esi, edx
203$L003maw_end:
204 mov eax, esi
205 pop ecx
206 pop edi
207 pop esi
208 pop ebx
209 pop ebp
210 ret
211_bn_mul_add_words ENDP
212_TEXT ENDS
213_TEXT SEGMENT
214PUBLIC _bn_mul_words
215
216_bn_mul_words PROC NEAR
217 push ebp
218 push ebx
219 push esi
220 push edi
221 ;
222 xor esi, esi
223 mov edi, DWORD PTR 20[esp]
224 mov ebx, DWORD PTR 24[esp]
225 mov ebp, DWORD PTR 28[esp]
226 mov ecx, DWORD PTR 32[esp]
227 and ebp, 4294967288
228 jz $L004mw_finish
229L005mw_loop:
230 ; Round 0
231 mov eax, DWORD PTR [ebx]
232 mul ecx
233 add eax, esi
234 adc edx, 0
235 mov DWORD PTR [edi],eax
236 mov esi, edx
237 ; Round 4
238 mov eax, DWORD PTR 4[ebx]
239 mul ecx
240 add eax, esi
241 adc edx, 0
242 mov DWORD PTR 4[edi],eax
243 mov esi, edx
244 ; Round 8
245 mov eax, DWORD PTR 8[ebx]
246 mul ecx
247 add eax, esi
248 adc edx, 0
249 mov DWORD PTR 8[edi],eax
250 mov esi, edx
251 ; Round 12
252 mov eax, DWORD PTR 12[ebx]
253 mul ecx
254 add eax, esi
255 adc edx, 0
256 mov DWORD PTR 12[edi],eax
257 mov esi, edx
258 ; Round 16
259 mov eax, DWORD PTR 16[ebx]
260 mul ecx
261 add eax, esi
262 adc edx, 0
263 mov DWORD PTR 16[edi],eax
264 mov esi, edx
265 ; Round 20
266 mov eax, DWORD PTR 20[ebx]
267 mul ecx
268 add eax, esi
269 adc edx, 0
270 mov DWORD PTR 20[edi],eax
271 mov esi, edx
272 ; Round 24
273 mov eax, DWORD PTR 24[ebx]
274 mul ecx
275 add eax, esi
276 adc edx, 0
277 mov DWORD PTR 24[edi],eax
278 mov esi, edx
279 ; Round 28
280 mov eax, DWORD PTR 28[ebx]
281 mul ecx
282 add eax, esi
283 adc edx, 0
284 mov DWORD PTR 28[edi],eax
285 mov esi, edx
286 ;
287 add ebx, 32
288 add edi, 32
289 sub ebp, 8
290 jz $L004mw_finish
291 jmp L005mw_loop
292$L004mw_finish:
293 mov ebp, DWORD PTR 28[esp]
294 and ebp, 7
295 jnz $L006mw_finish2
296 jmp $L007mw_end
297$L006mw_finish2:
298 ; Tail Round 0
299 mov eax, DWORD PTR [ebx]
300 mul ecx
301 add eax, esi
302 adc edx, 0
303 mov DWORD PTR [edi],eax
304 mov esi, edx
305 dec ebp
306 jz $L007mw_end
307 ; Tail Round 1
308 mov eax, DWORD PTR 4[ebx]
309 mul ecx
310 add eax, esi
311 adc edx, 0
312 mov DWORD PTR 4[edi],eax
313 mov esi, edx
314 dec ebp
315 jz $L007mw_end
316 ; Tail Round 2
317 mov eax, DWORD PTR 8[ebx]
318 mul ecx
319 add eax, esi
320 adc edx, 0
321 mov DWORD PTR 8[edi],eax
322 mov esi, edx
323 dec ebp
324 jz $L007mw_end
325 ; Tail Round 3
326 mov eax, DWORD PTR 12[ebx]
327 mul ecx
328 add eax, esi
329 adc edx, 0
330 mov DWORD PTR 12[edi],eax
331 mov esi, edx
332 dec ebp
333 jz $L007mw_end
334 ; Tail Round 4
335 mov eax, DWORD PTR 16[ebx]
336 mul ecx
337 add eax, esi
338 adc edx, 0
339 mov DWORD PTR 16[edi],eax
340 mov esi, edx
341 dec ebp
342 jz $L007mw_end
343 ; Tail Round 5
344 mov eax, DWORD PTR 20[ebx]
345 mul ecx
346 add eax, esi
347 adc edx, 0
348 mov DWORD PTR 20[edi],eax
349 mov esi, edx
350 dec ebp
351 jz $L007mw_end
352 ; Tail Round 6
353 mov eax, DWORD PTR 24[ebx]
354 mul ecx
355 add eax, esi
356 adc edx, 0
357 mov DWORD PTR 24[edi],eax
358 mov esi, edx
359$L007mw_end:
360 mov eax, esi
361 pop edi
362 pop esi
363 pop ebx
364 pop ebp
365 ret
366_bn_mul_words ENDP
367_TEXT ENDS
368_TEXT SEGMENT
369PUBLIC _bn_sqr_words
370
371_bn_sqr_words PROC NEAR
372 push ebp
373 push ebx
374 push esi
375 push edi
376 ;
377 mov esi, DWORD PTR 20[esp]
378 mov edi, DWORD PTR 24[esp]
379 mov ebx, DWORD PTR 28[esp]
380 and ebx, 4294967288
381 jz $L008sw_finish
382L009sw_loop:
383 ; Round 0
384 mov eax, DWORD PTR [edi]
385 mul eax
386 mov DWORD PTR [esi],eax
387 mov DWORD PTR 4[esi],edx
388 ; Round 4
389 mov eax, DWORD PTR 4[edi]
390 mul eax
391 mov DWORD PTR 8[esi],eax
392 mov DWORD PTR 12[esi],edx
393 ; Round 8
394 mov eax, DWORD PTR 8[edi]
395 mul eax
396 mov DWORD PTR 16[esi],eax
397 mov DWORD PTR 20[esi],edx
398 ; Round 12
399 mov eax, DWORD PTR 12[edi]
400 mul eax
401 mov DWORD PTR 24[esi],eax
402 mov DWORD PTR 28[esi],edx
403 ; Round 16
404 mov eax, DWORD PTR 16[edi]
405 mul eax
406 mov DWORD PTR 32[esi],eax
407 mov DWORD PTR 36[esi],edx
408 ; Round 20
409 mov eax, DWORD PTR 20[edi]
410 mul eax
411 mov DWORD PTR 40[esi],eax
412 mov DWORD PTR 44[esi],edx
413 ; Round 24
414 mov eax, DWORD PTR 24[edi]
415 mul eax
416 mov DWORD PTR 48[esi],eax
417 mov DWORD PTR 52[esi],edx
418 ; Round 28
419 mov eax, DWORD PTR 28[edi]
420 mul eax
421 mov DWORD PTR 56[esi],eax
422 mov DWORD PTR 60[esi],edx
423 ;
424 add edi, 32
425 add esi, 64
426 sub ebx, 8
427 jnz L009sw_loop
428$L008sw_finish:
429 mov ebx, DWORD PTR 28[esp]
430 and ebx, 7
431 jz $L010sw_end
432 ; Tail Round 0
433 mov eax, DWORD PTR [edi]
434 mul eax
435 mov DWORD PTR [esi],eax
436 dec ebx
437 mov DWORD PTR 4[esi],edx
438 jz $L010sw_end
439 ; Tail Round 1
440 mov eax, DWORD PTR 4[edi]
441 mul eax
442 mov DWORD PTR 8[esi],eax
443 dec ebx
444 mov DWORD PTR 12[esi],edx
445 jz $L010sw_end
446 ; Tail Round 2
447 mov eax, DWORD PTR 8[edi]
448 mul eax
449 mov DWORD PTR 16[esi],eax
450 dec ebx
451 mov DWORD PTR 20[esi],edx
452 jz $L010sw_end
453 ; Tail Round 3
454 mov eax, DWORD PTR 12[edi]
455 mul eax
456 mov DWORD PTR 24[esi],eax
457 dec ebx
458 mov DWORD PTR 28[esi],edx
459 jz $L010sw_end
460 ; Tail Round 4
461 mov eax, DWORD PTR 16[edi]
462 mul eax
463 mov DWORD PTR 32[esi],eax
464 dec ebx
465 mov DWORD PTR 36[esi],edx
466 jz $L010sw_end
467 ; Tail Round 5
468 mov eax, DWORD PTR 20[edi]
469 mul eax
470 mov DWORD PTR 40[esi],eax
471 dec ebx
472 mov DWORD PTR 44[esi],edx
473 jz $L010sw_end
474 ; Tail Round 6
475 mov eax, DWORD PTR 24[edi]
476 mul eax
477 mov DWORD PTR 48[esi],eax
478 mov DWORD PTR 52[esi],edx
479$L010sw_end:
480 pop edi
481 pop esi
482 pop ebx
483 pop ebp
484 ret
485_bn_sqr_words ENDP
486_TEXT ENDS
487_TEXT SEGMENT
488PUBLIC _bn_div64
489
490_bn_div64 PROC NEAR
491 push ebp
492 push ebx
493 push esi
494 push edi
495 mov edx, DWORD PTR 20[esp]
496 mov eax, DWORD PTR 24[esp]
497 mov ebx, DWORD PTR 28[esp]
498 div ebx
499 pop edi
500 pop esi
501 pop ebx
502 pop ebp
503 ret
504_bn_div64 ENDP
505_TEXT ENDS
506_TEXT SEGMENT
507PUBLIC _bn_add_words
508
509_bn_add_words PROC NEAR
510 push ebp
511 push ebx
512 push esi
513 push edi
514 ;
515 mov ebx, DWORD PTR 20[esp]
516 mov esi, DWORD PTR 24[esp]
517 mov edi, DWORD PTR 28[esp]
518 mov ebp, DWORD PTR 32[esp]
519 xor eax, eax
520 and ebp, 4294967288
521 jz $L011aw_finish
522L012aw_loop:
523 ; Round 0
524 mov ecx, DWORD PTR [esi]
525 mov edx, DWORD PTR [edi]
526 add ecx, eax
527 mov eax, 0
528 adc eax, eax
529 add ecx, edx
530 adc eax, 0
531 mov DWORD PTR [ebx],ecx
532 ; Round 1
533 mov ecx, DWORD PTR 4[esi]
534 mov edx, DWORD PTR 4[edi]
535 add ecx, eax
536 mov eax, 0
537 adc eax, eax
538 add ecx, edx
539 adc eax, 0
540 mov DWORD PTR 4[ebx],ecx
541 ; Round 2
542 mov ecx, DWORD PTR 8[esi]
543 mov edx, DWORD PTR 8[edi]
544 add ecx, eax
545 mov eax, 0
546 adc eax, eax
547 add ecx, edx
548 adc eax, 0
549 mov DWORD PTR 8[ebx],ecx
550 ; Round 3
551 mov ecx, DWORD PTR 12[esi]
552 mov edx, DWORD PTR 12[edi]
553 add ecx, eax
554 mov eax, 0
555 adc eax, eax
556 add ecx, edx
557 adc eax, 0
558 mov DWORD PTR 12[ebx],ecx
559 ; Round 4
560 mov ecx, DWORD PTR 16[esi]
561 mov edx, DWORD PTR 16[edi]
562 add ecx, eax
563 mov eax, 0
564 adc eax, eax
565 add ecx, edx
566 adc eax, 0
567 mov DWORD PTR 16[ebx],ecx
568 ; Round 5
569 mov ecx, DWORD PTR 20[esi]
570 mov edx, DWORD PTR 20[edi]
571 add ecx, eax
572 mov eax, 0
573 adc eax, eax
574 add ecx, edx
575 adc eax, 0
576 mov DWORD PTR 20[ebx],ecx
577 ; Round 6
578 mov ecx, DWORD PTR 24[esi]
579 mov edx, DWORD PTR 24[edi]
580 add ecx, eax
581 mov eax, 0
582 adc eax, eax
583 add ecx, edx
584 adc eax, 0
585 mov DWORD PTR 24[ebx],ecx
586 ; Round 7
587 mov ecx, DWORD PTR 28[esi]
588 mov edx, DWORD PTR 28[edi]
589 add ecx, eax
590 mov eax, 0
591 adc eax, eax
592 add ecx, edx
593 adc eax, 0
594 mov DWORD PTR 28[ebx],ecx
595 ;
596 add esi, 32
597 add edi, 32
598 add ebx, 32
599 sub ebp, 8
600 jnz L012aw_loop
601$L011aw_finish:
602 mov ebp, DWORD PTR 32[esp]
603 and ebp, 7
604 jz $L013aw_end
605 ; Tail Round 0
606 mov ecx, DWORD PTR [esi]
607 mov edx, DWORD PTR [edi]
608 add ecx, eax
609 mov eax, 0
610 adc eax, eax
611 add ecx, edx
612 adc eax, 0
613 dec ebp
614 mov DWORD PTR [ebx],ecx
615 jz $L013aw_end
616 ; Tail Round 1
617 mov ecx, DWORD PTR 4[esi]
618 mov edx, DWORD PTR 4[edi]
619 add ecx, eax
620 mov eax, 0
621 adc eax, eax
622 add ecx, edx
623 adc eax, 0
624 dec ebp
625 mov DWORD PTR 4[ebx],ecx
626 jz $L013aw_end
627 ; Tail Round 2
628 mov ecx, DWORD PTR 8[esi]
629 mov edx, DWORD PTR 8[edi]
630 add ecx, eax
631 mov eax, 0
632 adc eax, eax
633 add ecx, edx
634 adc eax, 0
635 dec ebp
636 mov DWORD PTR 8[ebx],ecx
637 jz $L013aw_end
638 ; Tail Round 3
639 mov ecx, DWORD PTR 12[esi]
640 mov edx, DWORD PTR 12[edi]
641 add ecx, eax
642 mov eax, 0
643 adc eax, eax
644 add ecx, edx
645 adc eax, 0
646 dec ebp
647 mov DWORD PTR 12[ebx],ecx
648 jz $L013aw_end
649 ; Tail Round 4
650 mov ecx, DWORD PTR 16[esi]
651 mov edx, DWORD PTR 16[edi]
652 add ecx, eax
653 mov eax, 0
654 adc eax, eax
655 add ecx, edx
656 adc eax, 0
657 dec ebp
658 mov DWORD PTR 16[ebx],ecx
659 jz $L013aw_end
660 ; Tail Round 5
661 mov ecx, DWORD PTR 20[esi]
662 mov edx, DWORD PTR 20[edi]
663 add ecx, eax
664 mov eax, 0
665 adc eax, eax
666 add ecx, edx
667 adc eax, 0
668 dec ebp
669 mov DWORD PTR 20[ebx],ecx
670 jz $L013aw_end
671 ; Tail Round 6
672 mov ecx, DWORD PTR 24[esi]
673 mov edx, DWORD PTR 24[edi]
674 add ecx, eax
675 mov eax, 0
676 adc eax, eax
677 add ecx, edx
678 adc eax, 0
679 mov DWORD PTR 24[ebx],ecx
680$L013aw_end:
681 mov eax, eax
682 pop edi
683 pop esi
684 pop ebx
685 pop ebp
686 ret
687_bn_add_words ENDP
688_TEXT ENDS
689END
diff --git a/src/lib/libcrypto/bn/asm/bn86unix.cpp b/src/lib/libcrypto/bn/asm/bn86unix.cpp
new file mode 100644
index 0000000000..64702201ea
--- /dev/null
+++ b/src/lib/libcrypto/bn/asm/bn86unix.cpp
@@ -0,0 +1,752 @@
1/* Run the C pre-processor over this file with one of the following defined
2 * ELF - elf object files,
3 * OUT - a.out object files,
4 * BSDI - BSDI style a.out object files
5 * SOL - Solaris style elf
6 */
7
8#define TYPE(a,b) .type a,b
9#define SIZE(a,b) .size a,b
10
11#if defined(OUT) || defined(BSDI)
12#define bn_mul_add_words _bn_mul_add_words
13#define bn_mul_words _bn_mul_words
14#define bn_sqr_words _bn_sqr_words
15#define bn_div64 _bn_div64
16#define bn_add_words _bn_add_words
17
18#endif
19
20#ifdef OUT
21#define OK 1
22#define ALIGN 4
23#endif
24
25#ifdef BSDI
26#define OK 1
27#define ALIGN 4
28#undef SIZE
29#undef TYPE
30#define SIZE(a,b)
31#define TYPE(a,b)
32#endif
33
34#if defined(ELF) || defined(SOL)
35#define OK 1
36#define ALIGN 16
37#endif
38
39#ifndef OK
40You need to define one of
41ELF - elf systems - linux-elf, NetBSD and DG-UX
42OUT - a.out systems - linux-a.out and FreeBSD
43SOL - solaris systems, which are elf with strange comment lines
44BSDI - a.out with a very primative version of as.
45#endif
46
47/* Let the Assembler begin :-) */
48 /* Don't even think of reading this code */
49 /* It was automatically generated by bn-586.pl */
50 /* Which is a perl program used to generate the x86 assember for */
51 /* any of elf, a.out, BSDI,Win32, or Solaris */
52 /* eric <eay@cryptsoft.com> */
53
54 .file "bn-586.s"
55 .version "01.01"
56gcc2_compiled.:
57.text
58 .align ALIGN
59.globl bn_mul_add_words
60 TYPE(bn_mul_add_words,@function)
61bn_mul_add_words:
62 pushl %ebp
63 pushl %ebx
64 pushl %esi
65 pushl %edi
66
67
68 xorl %esi, %esi
69 movl 20(%esp), %edi
70 movl 28(%esp), %ecx
71 movl 24(%esp), %ebx
72 andl $4294967288, %ecx
73 movl 32(%esp), %ebp
74 pushl %ecx
75 jz .L000maw_finish
76.L001maw_loop:
77 movl %ecx, (%esp)
78 /* Round 0 */
79 movl (%ebx), %eax
80 mull %ebp
81 addl %esi, %eax
82 movl (%edi), %esi
83 adcl $0, %edx
84 addl %esi, %eax
85 adcl $0, %edx
86 movl %eax, (%edi)
87 movl %edx, %esi
88 /* Round 4 */
89 movl 4(%ebx), %eax
90 mull %ebp
91 addl %esi, %eax
92 movl 4(%edi), %esi
93 adcl $0, %edx
94 addl %esi, %eax
95 adcl $0, %edx
96 movl %eax, 4(%edi)
97 movl %edx, %esi
98 /* Round 8 */
99 movl 8(%ebx), %eax
100 mull %ebp
101 addl %esi, %eax
102 movl 8(%edi), %esi
103 adcl $0, %edx
104 addl %esi, %eax
105 adcl $0, %edx
106 movl %eax, 8(%edi)
107 movl %edx, %esi
108 /* Round 12 */
109 movl 12(%ebx), %eax
110 mull %ebp
111 addl %esi, %eax
112 movl 12(%edi), %esi
113 adcl $0, %edx
114 addl %esi, %eax
115 adcl $0, %edx
116 movl %eax, 12(%edi)
117 movl %edx, %esi
118 /* Round 16 */
119 movl 16(%ebx), %eax
120 mull %ebp
121 addl %esi, %eax
122 movl 16(%edi), %esi
123 adcl $0, %edx
124 addl %esi, %eax
125 adcl $0, %edx
126 movl %eax, 16(%edi)
127 movl %edx, %esi
128 /* Round 20 */
129 movl 20(%ebx), %eax
130 mull %ebp
131 addl %esi, %eax
132 movl 20(%edi), %esi
133 adcl $0, %edx
134 addl %esi, %eax
135 adcl $0, %edx
136 movl %eax, 20(%edi)
137 movl %edx, %esi
138 /* Round 24 */
139 movl 24(%ebx), %eax
140 mull %ebp
141 addl %esi, %eax
142 movl 24(%edi), %esi
143 adcl $0, %edx
144 addl %esi, %eax
145 adcl $0, %edx
146 movl %eax, 24(%edi)
147 movl %edx, %esi
148 /* Round 28 */
149 movl 28(%ebx), %eax
150 mull %ebp
151 addl %esi, %eax
152 movl 28(%edi), %esi
153 adcl $0, %edx
154 addl %esi, %eax
155 adcl $0, %edx
156 movl %eax, 28(%edi)
157 movl %edx, %esi
158
159 movl (%esp), %ecx
160 addl $32, %ebx
161 addl $32, %edi
162 subl $8, %ecx
163 jnz .L001maw_loop
164.L000maw_finish:
165 movl 32(%esp), %ecx
166 andl $7, %ecx
167 jnz .L002maw_finish2
168 jmp .L003maw_end
169.align ALIGN
170.L002maw_finish2:
171 /* Tail Round 0 */
172 movl (%ebx), %eax
173 mull %ebp
174 addl %esi, %eax
175 movl (%edi), %esi
176 adcl $0, %edx
177 addl %esi, %eax
178 adcl $0, %edx
179 decl %ecx
180 movl %eax, (%edi)
181 movl %edx, %esi
182 jz .L003maw_end
183 /* Tail Round 1 */
184 movl 4(%ebx), %eax
185 mull %ebp
186 addl %esi, %eax
187 movl 4(%edi), %esi
188 adcl $0, %edx
189 addl %esi, %eax
190 adcl $0, %edx
191 decl %ecx
192 movl %eax, 4(%edi)
193 movl %edx, %esi
194 jz .L003maw_end
195 /* Tail Round 2 */
196 movl 8(%ebx), %eax
197 mull %ebp
198 addl %esi, %eax
199 movl 8(%edi), %esi
200 adcl $0, %edx
201 addl %esi, %eax
202 adcl $0, %edx
203 decl %ecx
204 movl %eax, 8(%edi)
205 movl %edx, %esi
206 jz .L003maw_end
207 /* Tail Round 3 */
208 movl 12(%ebx), %eax
209 mull %ebp
210 addl %esi, %eax
211 movl 12(%edi), %esi
212 adcl $0, %edx
213 addl %esi, %eax
214 adcl $0, %edx
215 decl %ecx
216 movl %eax, 12(%edi)
217 movl %edx, %esi
218 jz .L003maw_end
219 /* Tail Round 4 */
220 movl 16(%ebx), %eax
221 mull %ebp
222 addl %esi, %eax
223 movl 16(%edi), %esi
224 adcl $0, %edx
225 addl %esi, %eax
226 adcl $0, %edx
227 decl %ecx
228 movl %eax, 16(%edi)
229 movl %edx, %esi
230 jz .L003maw_end
231 /* Tail Round 5 */
232 movl 20(%ebx), %eax
233 mull %ebp
234 addl %esi, %eax
235 movl 20(%edi), %esi
236 adcl $0, %edx
237 addl %esi, %eax
238 adcl $0, %edx
239 decl %ecx
240 movl %eax, 20(%edi)
241 movl %edx, %esi
242 jz .L003maw_end
243 /* Tail Round 6 */
244 movl 24(%ebx), %eax
245 mull %ebp
246 addl %esi, %eax
247 movl 24(%edi), %esi
248 adcl $0, %edx
249 addl %esi, %eax
250 adcl $0, %edx
251 movl %eax, 24(%edi)
252 movl %edx, %esi
253.L003maw_end:
254 movl %esi, %eax
255 popl %ecx
256 popl %edi
257 popl %esi
258 popl %ebx
259 popl %ebp
260 ret
261.bn_mul_add_words_end:
262 SIZE(bn_mul_add_words,.bn_mul_add_words_end-bn_mul_add_words)
263.ident "bn_mul_add_words"
264.text
265 .align ALIGN
266.globl bn_mul_words
267 TYPE(bn_mul_words,@function)
268bn_mul_words:
269 pushl %ebp
270 pushl %ebx
271 pushl %esi
272 pushl %edi
273
274
275 xorl %esi, %esi
276 movl 20(%esp), %edi
277 movl 24(%esp), %ebx
278 movl 28(%esp), %ebp
279 movl 32(%esp), %ecx
280 andl $4294967288, %ebp
281 jz .L004mw_finish
282.L005mw_loop:
283 /* Round 0 */
284 movl (%ebx), %eax
285 mull %ecx
286 addl %esi, %eax
287 adcl $0, %edx
288 movl %eax, (%edi)
289 movl %edx, %esi
290 /* Round 4 */
291 movl 4(%ebx), %eax
292 mull %ecx
293 addl %esi, %eax
294 adcl $0, %edx
295 movl %eax, 4(%edi)
296 movl %edx, %esi
297 /* Round 8 */
298 movl 8(%ebx), %eax
299 mull %ecx
300 addl %esi, %eax
301 adcl $0, %edx
302 movl %eax, 8(%edi)
303 movl %edx, %esi
304 /* Round 12 */
305 movl 12(%ebx), %eax
306 mull %ecx
307 addl %esi, %eax
308 adcl $0, %edx
309 movl %eax, 12(%edi)
310 movl %edx, %esi
311 /* Round 16 */
312 movl 16(%ebx), %eax
313 mull %ecx
314 addl %esi, %eax
315 adcl $0, %edx
316 movl %eax, 16(%edi)
317 movl %edx, %esi
318 /* Round 20 */
319 movl 20(%ebx), %eax
320 mull %ecx
321 addl %esi, %eax
322 adcl $0, %edx
323 movl %eax, 20(%edi)
324 movl %edx, %esi
325 /* Round 24 */
326 movl 24(%ebx), %eax
327 mull %ecx
328 addl %esi, %eax
329 adcl $0, %edx
330 movl %eax, 24(%edi)
331 movl %edx, %esi
332 /* Round 28 */
333 movl 28(%ebx), %eax
334 mull %ecx
335 addl %esi, %eax
336 adcl $0, %edx
337 movl %eax, 28(%edi)
338 movl %edx, %esi
339
340 addl $32, %ebx
341 addl $32, %edi
342 subl $8, %ebp
343 jz .L004mw_finish
344 jmp .L005mw_loop
345.L004mw_finish:
346 movl 28(%esp), %ebp
347 andl $7, %ebp
348 jnz .L006mw_finish2
349 jmp .L007mw_end
350.align ALIGN
351.L006mw_finish2:
352 /* Tail Round 0 */
353 movl (%ebx), %eax
354 mull %ecx
355 addl %esi, %eax
356 adcl $0, %edx
357 movl %eax, (%edi)
358 movl %edx, %esi
359 decl %ebp
360 jz .L007mw_end
361 /* Tail Round 1 */
362 movl 4(%ebx), %eax
363 mull %ecx
364 addl %esi, %eax
365 adcl $0, %edx
366 movl %eax, 4(%edi)
367 movl %edx, %esi
368 decl %ebp
369 jz .L007mw_end
370 /* Tail Round 2 */
371 movl 8(%ebx), %eax
372 mull %ecx
373 addl %esi, %eax
374 adcl $0, %edx
375 movl %eax, 8(%edi)
376 movl %edx, %esi
377 decl %ebp
378 jz .L007mw_end
379 /* Tail Round 3 */
380 movl 12(%ebx), %eax
381 mull %ecx
382 addl %esi, %eax
383 adcl $0, %edx
384 movl %eax, 12(%edi)
385 movl %edx, %esi
386 decl %ebp
387 jz .L007mw_end
388 /* Tail Round 4 */
389 movl 16(%ebx), %eax
390 mull %ecx
391 addl %esi, %eax
392 adcl $0, %edx
393 movl %eax, 16(%edi)
394 movl %edx, %esi
395 decl %ebp
396 jz .L007mw_end
397 /* Tail Round 5 */
398 movl 20(%ebx), %eax
399 mull %ecx
400 addl %esi, %eax
401 adcl $0, %edx
402 movl %eax, 20(%edi)
403 movl %edx, %esi
404 decl %ebp
405 jz .L007mw_end
406 /* Tail Round 6 */
407 movl 24(%ebx), %eax
408 mull %ecx
409 addl %esi, %eax
410 adcl $0, %edx
411 movl %eax, 24(%edi)
412 movl %edx, %esi
413.L007mw_end:
414 movl %esi, %eax
415 popl %edi
416 popl %esi
417 popl %ebx
418 popl %ebp
419 ret
420.bn_mul_words_end:
421 SIZE(bn_mul_words,.bn_mul_words_end-bn_mul_words)
422.ident "bn_mul_words"
423.text
424 .align ALIGN
425.globl bn_sqr_words
426 TYPE(bn_sqr_words,@function)
427bn_sqr_words:
428 pushl %ebp
429 pushl %ebx
430 pushl %esi
431 pushl %edi
432
433
434 movl 20(%esp), %esi
435 movl 24(%esp), %edi
436 movl 28(%esp), %ebx
437 andl $4294967288, %ebx
438 jz .L008sw_finish
439.L009sw_loop:
440 /* Round 0 */
441 movl (%edi), %eax
442 mull %eax
443 movl %eax, (%esi)
444 movl %edx, 4(%esi)
445 /* Round 4 */
446 movl 4(%edi), %eax
447 mull %eax
448 movl %eax, 8(%esi)
449 movl %edx, 12(%esi)
450 /* Round 8 */
451 movl 8(%edi), %eax
452 mull %eax
453 movl %eax, 16(%esi)
454 movl %edx, 20(%esi)
455 /* Round 12 */
456 movl 12(%edi), %eax
457 mull %eax
458 movl %eax, 24(%esi)
459 movl %edx, 28(%esi)
460 /* Round 16 */
461 movl 16(%edi), %eax
462 mull %eax
463 movl %eax, 32(%esi)
464 movl %edx, 36(%esi)
465 /* Round 20 */
466 movl 20(%edi), %eax
467 mull %eax
468 movl %eax, 40(%esi)
469 movl %edx, 44(%esi)
470 /* Round 24 */
471 movl 24(%edi), %eax
472 mull %eax
473 movl %eax, 48(%esi)
474 movl %edx, 52(%esi)
475 /* Round 28 */
476 movl 28(%edi), %eax
477 mull %eax
478 movl %eax, 56(%esi)
479 movl %edx, 60(%esi)
480
481 addl $32, %edi
482 addl $64, %esi
483 subl $8, %ebx
484 jnz .L009sw_loop
485.L008sw_finish:
486 movl 28(%esp), %ebx
487 andl $7, %ebx
488 jz .L010sw_end
489 /* Tail Round 0 */
490 movl (%edi), %eax
491 mull %eax
492 movl %eax, (%esi)
493 decl %ebx
494 movl %edx, 4(%esi)
495 jz .L010sw_end
496 /* Tail Round 1 */
497 movl 4(%edi), %eax
498 mull %eax
499 movl %eax, 8(%esi)
500 decl %ebx
501 movl %edx, 12(%esi)
502 jz .L010sw_end
503 /* Tail Round 2 */
504 movl 8(%edi), %eax
505 mull %eax
506 movl %eax, 16(%esi)
507 decl %ebx
508 movl %edx, 20(%esi)
509 jz .L010sw_end
510 /* Tail Round 3 */
511 movl 12(%edi), %eax
512 mull %eax
513 movl %eax, 24(%esi)
514 decl %ebx
515 movl %edx, 28(%esi)
516 jz .L010sw_end
517 /* Tail Round 4 */
518 movl 16(%edi), %eax
519 mull %eax
520 movl %eax, 32(%esi)
521 decl %ebx
522 movl %edx, 36(%esi)
523 jz .L010sw_end
524 /* Tail Round 5 */
525 movl 20(%edi), %eax
526 mull %eax
527 movl %eax, 40(%esi)
528 decl %ebx
529 movl %edx, 44(%esi)
530 jz .L010sw_end
531 /* Tail Round 6 */
532 movl 24(%edi), %eax
533 mull %eax
534 movl %eax, 48(%esi)
535 movl %edx, 52(%esi)
536.L010sw_end:
537 popl %edi
538 popl %esi
539 popl %ebx
540 popl %ebp
541 ret
542.bn_sqr_words_end:
543 SIZE(bn_sqr_words,.bn_sqr_words_end-bn_sqr_words)
544.ident "bn_sqr_words"
545.text
546 .align ALIGN
547.globl bn_div64
548 TYPE(bn_div64,@function)
549bn_div64:
550 pushl %ebp
551 pushl %ebx
552 pushl %esi
553 pushl %edi
554
555 movl 20(%esp), %edx
556 movl 24(%esp), %eax
557 movl 28(%esp), %ebx
558 divl %ebx
559 popl %edi
560 popl %esi
561 popl %ebx
562 popl %ebp
563 ret
564.bn_div64_end:
565 SIZE(bn_div64,.bn_div64_end-bn_div64)
566.ident "bn_div64"
567.text
568 .align ALIGN
569.globl bn_add_words
570 TYPE(bn_add_words,@function)
571bn_add_words:
572 pushl %ebp
573 pushl %ebx
574 pushl %esi
575 pushl %edi
576
577
578 movl 20(%esp), %ebx
579 movl 24(%esp), %esi
580 movl 28(%esp), %edi
581 movl 32(%esp), %ebp
582 xorl %eax, %eax
583 andl $4294967288, %ebp
584 jz .L011aw_finish
585.L012aw_loop:
586 /* Round 0 */
587 movl (%esi), %ecx
588 movl (%edi), %edx
589 addl %eax, %ecx
590 movl $0, %eax
591 adcl %eax, %eax
592 addl %edx, %ecx
593 adcl $0, %eax
594 movl %ecx, (%ebx)
595 /* Round 1 */
596 movl 4(%esi), %ecx
597 movl 4(%edi), %edx
598 addl %eax, %ecx
599 movl $0, %eax
600 adcl %eax, %eax
601 addl %edx, %ecx
602 adcl $0, %eax
603 movl %ecx, 4(%ebx)
604 /* Round 2 */
605 movl 8(%esi), %ecx
606 movl 8(%edi), %edx
607 addl %eax, %ecx
608 movl $0, %eax
609 adcl %eax, %eax
610 addl %edx, %ecx
611 adcl $0, %eax
612 movl %ecx, 8(%ebx)
613 /* Round 3 */
614 movl 12(%esi), %ecx
615 movl 12(%edi), %edx
616 addl %eax, %ecx
617 movl $0, %eax
618 adcl %eax, %eax
619 addl %edx, %ecx
620 adcl $0, %eax
621 movl %ecx, 12(%ebx)
622 /* Round 4 */
623 movl 16(%esi), %ecx
624 movl 16(%edi), %edx
625 addl %eax, %ecx
626 movl $0, %eax
627 adcl %eax, %eax
628 addl %edx, %ecx
629 adcl $0, %eax
630 movl %ecx, 16(%ebx)
631 /* Round 5 */
632 movl 20(%esi), %ecx
633 movl 20(%edi), %edx
634 addl %eax, %ecx
635 movl $0, %eax
636 adcl %eax, %eax
637 addl %edx, %ecx
638 adcl $0, %eax
639 movl %ecx, 20(%ebx)
640 /* Round 6 */
641 movl 24(%esi), %ecx
642 movl 24(%edi), %edx
643 addl %eax, %ecx
644 movl $0, %eax
645 adcl %eax, %eax
646 addl %edx, %ecx
647 adcl $0, %eax
648 movl %ecx, 24(%ebx)
649 /* Round 7 */
650 movl 28(%esi), %ecx
651 movl 28(%edi), %edx
652 addl %eax, %ecx
653 movl $0, %eax
654 adcl %eax, %eax
655 addl %edx, %ecx
656 adcl $0, %eax
657 movl %ecx, 28(%ebx)
658
659 addl $32, %esi
660 addl $32, %edi
661 addl $32, %ebx
662 subl $8, %ebp
663 jnz .L012aw_loop
664.L011aw_finish:
665 movl 32(%esp), %ebp
666 andl $7, %ebp
667 jz .L013aw_end
668 /* Tail Round 0 */
669 movl (%esi), %ecx
670 movl (%edi), %edx
671 addl %eax, %ecx
672 movl $0, %eax
673 adcl %eax, %eax
674 addl %edx, %ecx
675 adcl $0, %eax
676 decl %ebp
677 movl %ecx, (%ebx)
678 jz .L013aw_end
679 /* Tail Round 1 */
680 movl 4(%esi), %ecx
681 movl 4(%edi), %edx
682 addl %eax, %ecx
683 movl $0, %eax
684 adcl %eax, %eax
685 addl %edx, %ecx
686 adcl $0, %eax
687 decl %ebp
688 movl %ecx, 4(%ebx)
689 jz .L013aw_end
690 /* Tail Round 2 */
691 movl 8(%esi), %ecx
692 movl 8(%edi), %edx
693 addl %eax, %ecx
694 movl $0, %eax
695 adcl %eax, %eax
696 addl %edx, %ecx
697 adcl $0, %eax
698 decl %ebp
699 movl %ecx, 8(%ebx)
700 jz .L013aw_end
701 /* Tail Round 3 */
702 movl 12(%esi), %ecx
703 movl 12(%edi), %edx
704 addl %eax, %ecx
705 movl $0, %eax
706 adcl %eax, %eax
707 addl %edx, %ecx
708 adcl $0, %eax
709 decl %ebp
710 movl %ecx, 12(%ebx)
711 jz .L013aw_end
712 /* Tail Round 4 */
713 movl 16(%esi), %ecx
714 movl 16(%edi), %edx
715 addl %eax, %ecx
716 movl $0, %eax
717 adcl %eax, %eax
718 addl %edx, %ecx
719 adcl $0, %eax
720 decl %ebp
721 movl %ecx, 16(%ebx)
722 jz .L013aw_end
723 /* Tail Round 5 */
724 movl 20(%esi), %ecx
725 movl 20(%edi), %edx
726 addl %eax, %ecx
727 movl $0, %eax
728 adcl %eax, %eax
729 addl %edx, %ecx
730 adcl $0, %eax
731 decl %ebp
732 movl %ecx, 20(%ebx)
733 jz .L013aw_end
734 /* Tail Round 6 */
735 movl 24(%esi), %ecx
736 movl 24(%edi), %edx
737 addl %eax, %ecx
738 movl $0, %eax
739 adcl %eax, %eax
740 addl %edx, %ecx
741 adcl $0, %eax
742 movl %ecx, 24(%ebx)
743.L013aw_end:
744 movl %eax, %eax
745 popl %edi
746 popl %esi
747 popl %ebx
748 popl %ebp
749 ret
750.bn_add_words_end:
751 SIZE(bn_add_words,.bn_add_words_end-bn_add_words)
752.ident "bn_add_words"
diff --git a/src/lib/libcrypto/bn/asm/sparc.s b/src/lib/libcrypto/bn/asm/sparc.s
new file mode 100644
index 0000000000..f9e533caa8
--- /dev/null
+++ b/src/lib/libcrypto/bn/asm/sparc.s
@@ -0,0 +1,462 @@
1 .file "bn_mulw.c"
2gcc2_compiled.:
3.section ".text"
4 .align 4
5 .global bn_mul_add_words
6 .type bn_mul_add_words,#function
7 .proc 016
8bn_mul_add_words:
9 !#PROLOGUE# 0
10 save %sp,-112,%sp
11 !#PROLOGUE# 1
12 mov %i0,%o0
13 mov %i1,%o2
14 mov %i2,%g1
15 mov %i3,%o1
16 mov 0,%i4
17 add %o0,12,%g4
18 add %o2,12,%o7
19.LL2:
20 mov %i4,%i3
21 mov 0,%i2
22 ld [%o0],%g2
23 mov %g2,%i1
24 ld [%o2],%g2
25 mov 0,%i0
26 umul %o1,%g2,%g3
27 rd %y,%g2
28 addcc %g3,%i1,%g3
29 addx %g2,%i0,%g2
30 addcc %g3,%i3,%g3
31 addx %g2,%i2,%g2
32 st %g3,[%o0]
33 mov %g2,%i5
34 mov 0,%i4
35 addcc %g1,-1,%g1
36 be .LL3
37 mov %i5,%i4
38 mov %i4,%i3
39 mov 0,%i2
40 ld [%g4-8],%g2
41 mov %g2,%i1
42 ld [%o7-8],%g2
43 mov 0,%i0
44 umul %o1,%g2,%g3
45 rd %y,%g2
46 addcc %g3,%i1,%g3
47 addx %g2,%i0,%g2
48 addcc %g3,%i3,%g3
49 addx %g2,%i2,%g2
50 st %g3,[%g4-8]
51 mov %g2,%i5
52 mov 0,%i4
53 addcc %g1,-1,%g1
54 be .LL3
55 mov %i5,%i4
56 mov %i4,%i3
57 mov 0,%i2
58 ld [%g4-4],%g2
59 mov %g2,%i1
60 ld [%o7-4],%g2
61 mov 0,%i0
62 umul %o1,%g2,%g3
63 rd %y,%g2
64 addcc %g3,%i1,%g3
65 addx %g2,%i0,%g2
66 addcc %g3,%i3,%g3
67 addx %g2,%i2,%g2
68 st %g3,[%g4-4]
69 mov %g2,%i5
70 mov 0,%i4
71 addcc %g1,-1,%g1
72 be .LL3
73 mov %i5,%i4
74 mov %i4,%i3
75 mov 0,%i2
76 ld [%g4],%g2
77 mov %g2,%i1
78 ld [%o7],%g2
79 mov 0,%i0
80 umul %o1,%g2,%g3
81 rd %y,%g2
82 addcc %g3,%i1,%g3
83 addx %g2,%i0,%g2
84 addcc %g3,%i3,%g3
85 addx %g2,%i2,%g2
86 st %g3,[%g4]
87 mov %g2,%i5
88 mov 0,%i4
89 addcc %g1,-1,%g1
90 be .LL3
91 mov %i5,%i4
92 add %o7,16,%o7
93 add %o2,16,%o2
94 add %g4,16,%g4
95 b .LL2
96 add %o0,16,%o0
97.LL3:
98 ret
99 restore %g0,%i4,%o0
100.LLfe1:
101 .size bn_mul_add_words,.LLfe1-bn_mul_add_words
102 .align 4
103 .global bn_mul_words
104 .type bn_mul_words,#function
105 .proc 016
106bn_mul_words:
107 !#PROLOGUE# 0
108 save %sp,-112,%sp
109 !#PROLOGUE# 1
110 mov %i0,%o7
111 mov %i1,%o0
112 mov %i2,%i4
113 mov %i3,%g4
114 mov 0,%i0
115 add %o7,12,%g1
116 add %o0,12,%i5
117.LL18:
118 mov %i0,%g3
119 mov 0,%g2
120 ld [%o0],%i2
121 umul %g4,%i2,%i3
122 rd %y,%i2
123 addcc %i3,%g3,%i3
124 addx %i2,%g2,%i2
125 st %i3,[%o7]
126 mov %i2,%i1
127 mov 0,%i0
128 addcc %i4,-1,%i4
129 be .LL19
130 mov %i1,%i0
131 mov %i0,%g3
132 mov 0,%g2
133 ld [%i5-8],%i2
134 umul %g4,%i2,%i3
135 rd %y,%i2
136 addcc %i3,%g3,%i3
137 addx %i2,%g2,%i2
138 st %i3,[%g1-8]
139 mov %i2,%i1
140 mov 0,%i0
141 addcc %i4,-1,%i4
142 be .LL19
143 mov %i1,%i0
144 mov %i0,%g3
145 mov 0,%g2
146 ld [%i5-4],%i2
147 umul %g4,%i2,%i3
148 rd %y,%i2
149 addcc %i3,%g3,%i3
150 addx %i2,%g2,%i2
151 st %i3,[%g1-4]
152 mov %i2,%i1
153 mov 0,%i0
154 addcc %i4,-1,%i4
155 be .LL19
156 mov %i1,%i0
157 mov %i0,%g3
158 mov 0,%g2
159 ld [%i5],%i2
160 umul %g4,%i2,%i3
161 rd %y,%i2
162 addcc %i3,%g3,%i3
163 addx %i2,%g2,%i2
164 st %i3,[%g1]
165 mov %i2,%i1
166 mov 0,%i0
167 addcc %i4,-1,%i4
168 be .LL19
169 mov %i1,%i0
170 add %i5,16,%i5
171 add %o0,16,%o0
172 add %g1,16,%g1
173 b .LL18
174 add %o7,16,%o7
175.LL19:
176 ret
177 restore
178.LLfe2:
179 .size bn_mul_words,.LLfe2-bn_mul_words
180 .align 4
181 .global bn_sqr_words
182 .type bn_sqr_words,#function
183 .proc 020
184bn_sqr_words:
185 !#PROLOGUE# 0
186 !#PROLOGUE# 1
187 mov %o0,%g4
188 add %g4,28,%o3
189 add %o1,12,%g1
190.LL34:
191 ld [%o1],%o0
192 addcc %o2,-1,%o2
193 umul %o0,%o0,%o5
194 rd %y,%o4
195 st %o5,[%g4]
196 mov %o4,%g3
197 mov 0,%g2
198 be .LL35
199 st %g3,[%o3-24]
200 ld [%g1-8],%o0
201 addcc %o2,-1,%o2
202 umul %o0,%o0,%o5
203 rd %y,%o4
204 st %o5,[%o3-20]
205 mov %o4,%g3
206 mov 0,%g2
207 be .LL35
208 st %g3,[%o3-16]
209 ld [%g1-4],%o0
210 addcc %o2,-1,%o2
211 umul %o0,%o0,%o5
212 rd %y,%o4
213 st %o5,[%o3-12]
214 mov %o4,%g3
215 mov 0,%g2
216 be .LL35
217 st %g3,[%o3-8]
218 ld [%g1],%o0
219 addcc %o2,-1,%o2
220 umul %o0,%o0,%o5
221 rd %y,%o4
222 st %o5,[%o3-4]
223 mov %o4,%g3
224 mov 0,%g2
225 be .LL35
226 st %g3,[%o3]
227 add %g1,16,%g1
228 add %o1,16,%o1
229 add %o3,32,%o3
230 b .LL34
231 add %g4,32,%g4
232.LL35:
233 retl
234 nop
235.LLfe3:
236 .size bn_sqr_words,.LLfe3-bn_sqr_words
237 .align 4
238 .global bn_add_words
239 .type bn_add_words,#function
240 .proc 016
241bn_add_words:
242 !#PROLOGUE# 0
243 save %sp,-112,%sp
244 !#PROLOGUE# 1
245 mov %i0,%o2
246 mov %i1,%o3
247 mov %i2,%o4
248 mov %i3,%i5
249 mov 0,%o0
250 mov 0,%o1
251 add %o2,12,%o7
252 add %o4,12,%g4
253 b .LL42
254 add %o3,12,%g1
255.LL45:
256 add %i5,-1,%i5
257 mov %i4,%g3
258 ld [%g4-8],%i4
259 mov 0,%g2
260 mov %i4,%i1
261 mov 0,%i0
262 addcc %g3,%i1,%g3
263 addx %g2,%i0,%g2
264 addcc %o1,%g3,%o1
265 addx %o0,%g2,%o0
266 st %o1,[%o7-8]
267 mov %o0,%i3
268 mov 0,%i2
269 mov %i2,%o0
270 mov %i3,%o1
271 cmp %i5,0
272 ble .LL43
273 add %i5,-1,%i5
274 ld [%g1-4],%i4
275 mov %i4,%g3
276 ld [%g4-4],%i4
277 mov 0,%g2
278 mov %i4,%i1
279 mov 0,%i0
280 addcc %g3,%i1,%g3
281 addx %g2,%i0,%g2
282 addcc %o1,%g3,%o1
283 addx %o0,%g2,%o0
284 st %o1,[%o7-4]
285 mov %o0,%i3
286 mov 0,%i2
287 mov %i2,%o0
288 mov %i3,%o1
289 cmp %i5,0
290 ble .LL43
291 add %i5,-1,%i5
292 ld [%g1],%i4
293 mov %i4,%g3
294 ld [%g4],%i4
295 mov 0,%g2
296 mov %i4,%i1
297 mov 0,%i0
298 addcc %g3,%i1,%g3
299 addx %g2,%i0,%g2
300 addcc %o1,%g3,%o1
301 addx %o0,%g2,%o0
302 st %o1,[%o7]
303 mov %o0,%i3
304 mov 0,%i2
305 mov %i2,%o0
306 mov %i3,%o1
307 cmp %i5,0
308 ble .LL43
309 add %g1,16,%g1
310 add %o3,16,%o3
311 add %g4,16,%g4
312 add %o4,16,%o4
313 add %o7,16,%o7
314 add %o2,16,%o2
315.LL42:
316 ld [%o3],%i4
317 add %i5,-1,%i5
318 mov %i4,%g3
319 ld [%o4],%i4
320 mov 0,%g2
321 mov %i4,%i1
322 mov 0,%i0
323 addcc %g3,%i1,%g3
324 addx %g2,%i0,%g2
325 addcc %o1,%g3,%o1
326 addx %o0,%g2,%o0
327 st %o1,[%o2]
328 mov %o0,%i3
329 mov 0,%i2
330 mov %i2,%o0
331 mov %i3,%o1
332 cmp %i5,0
333 bg,a .LL45
334 ld [%g1-8],%i4
335.LL43:
336 ret
337 restore %g0,%o1,%o0
338.LLfe4:
339 .size bn_add_words,.LLfe4-bn_add_words
340.section ".rodata"
341 .align 8
342.LLC0:
343 .asciz "Division would overflow (%d)\n"
344.section ".text"
345 .align 4
346 .global bn_div64
347 .type bn_div64,#function
348 .proc 016
349bn_div64:
350 !#PROLOGUE# 0
351 save %sp,-112,%sp
352 !#PROLOGUE# 1
353 mov 0,%l1
354 cmp %i2,0
355 bne .LL51
356 mov 2,%l0
357 b .LL68
358 mov -1,%i0
359.LL51:
360 call BN_num_bits_word,0
361 mov %i2,%o0
362 mov %o0,%o2
363 cmp %o2,32
364 be .LL52
365 mov 1,%o0
366 sll %o0,%o2,%o0
367 cmp %i0,%o0
368 bleu .LL69
369 mov 32,%o0
370 sethi %hi(__iob+32),%o0
371 or %o0,%lo(__iob+32),%o0
372 sethi %hi(.LLC0),%o1
373 call fprintf,0
374 or %o1,%lo(.LLC0),%o1
375 call abort,0
376 nop
377.LL52:
378 mov 32,%o0
379.LL69:
380 cmp %i0,%i2
381 blu .LL53
382 sub %o0,%o2,%o2
383 sub %i0,%i2,%i0
384.LL53:
385 cmp %o2,0
386 be .LL54
387 sll %i0,%o2,%o1
388 sll %i2,%o2,%i2
389 sub %o0,%o2,%o0
390 srl %i1,%o0,%o0
391 or %o1,%o0,%i0
392 sll %i1,%o2,%i1
393.LL54:
394 srl %i2,16,%g2
395 sethi %hi(65535),%o0
396 or %o0,%lo(65535),%o1
397 and %i2,%o1,%g3
398 mov %o0,%g4
399 sethi %hi(-65536),%o7
400 mov %o1,%g1
401.LL55:
402 srl %i0,16,%o0
403 cmp %o0,%g2
404 be .LL59
405 or %g4,%lo(65535),%o3
406 wr %g0,%g0,%y
407 nop
408 nop
409 nop
410 udiv %i0,%g2,%o3
411.LL59:
412 and %i1,%o7,%o0
413 srl %o0,16,%o5
414 smul %o3,%g3,%o4
415 smul %o3,%g2,%o2
416.LL60:
417 sub %i0,%o2,%o1
418 andcc %o1,%o7,%g0
419 bne .LL61
420 sll %o1,16,%o0
421 add %o0,%o5,%o0
422 cmp %o4,%o0
423 bleu .LL61
424 sub %o4,%g3,%o4
425 sub %o2,%g2,%o2
426 b .LL60
427 add %o3,-1,%o3
428.LL61:
429 smul %o3,%g2,%o2
430 smul %o3,%g3,%o0
431 srl %o0,16,%o1
432 sll %o0,16,%o0
433 and %o0,%o7,%o0
434 cmp %i1,%o0
435 bgeu .LL65
436 add %o2,%o1,%o2
437 add %o2,1,%o2
438.LL65:
439 cmp %i0,%o2
440 bgeu .LL66
441 sub %i1,%o0,%i1
442 add %i0,%i2,%i0
443 add %o3,-1,%o3
444.LL66:
445 addcc %l0,-1,%l0
446 be .LL56
447 sub %i0,%o2,%i0
448 sll %o3,16,%l1
449 sll %i0,16,%o0
450 srl %i1,16,%o1
451 or %o0,%o1,%i0
452 and %i1,%g1,%o0
453 b .LL55
454 sll %o0,16,%i1
455.LL56:
456 or %l1,%o3,%i0
457.LL68:
458 ret
459 restore
460.LLfe5:
461 .size bn_div64,.LLfe5-bn_div64
462 .ident "GCC: (GNU) 2.7.2.3"
diff --git a/src/lib/libcrypto/bn/asm/x86w16.asm b/src/lib/libcrypto/bn/asm/x86w16.asm
new file mode 100644
index 0000000000..74a933a8cd
--- /dev/null
+++ b/src/lib/libcrypto/bn/asm/x86w16.asm
@@ -0,0 +1,297 @@
1; Static Name Aliases
2;
3 TITLE bn_mulw.c
4 .8087
5F_TEXT SEGMENT WORD PUBLIC 'CODE'
6F_TEXT ENDS
7_DATA SEGMENT WORD PUBLIC 'DATA'
8_DATA ENDS
9CONST SEGMENT WORD PUBLIC 'CONST'
10CONST ENDS
11_BSS SEGMENT WORD PUBLIC 'BSS'
12_BSS ENDS
13DGROUP GROUP CONST, _BSS, _DATA
14 ASSUME DS: DGROUP, SS: DGROUP
15F_TEXT SEGMENT
16 ASSUME CS: F_TEXT
17 PUBLIC _bn_mul_add_words
18_bn_mul_add_words PROC FAR
19; Line 58
20 push bp
21 push bx
22 push si
23 push di
24 push ds
25 push es
26 mov bp,sp
27; w = 26
28; num = 24
29; ap = 20
30; rp = 16
31 xor si,si ;c=0;
32 mov di,WORD PTR [bp+16] ; load r
33 mov ds,WORD PTR [bp+18] ; load r
34 mov bx,WORD PTR [bp+20] ; load a
35 mov es,WORD PTR [bp+22] ; load a
36 mov cx,WORD PTR [bp+26] ; load w
37 mov bp,WORD PTR [bp+24] ; load num
38
39 shr bp,1 ; div count by 4 and do groups of 4
40 shr bp,1
41 je $L555
42
43$L546:
44 mov ax,cx
45 mul WORD PTR es:[bx] ; w* *a
46 add ax,WORD PTR ds:[di] ; + *r
47 adc dx,0
48 adc ax,si
49 adc dx,0
50 mov WORD PTR ds:[di],ax
51 mov si,dx
52 ;
53 mov ax,cx
54 mul WORD PTR es:[bx+2] ; w* *a
55 add ax,WORD PTR ds:[di+2] ; + *r
56 adc dx,0
57 adc ax,si
58 adc dx,0
59 mov WORD PTR ds:[di+2],ax
60 mov si,dx
61 ;
62 mov ax,cx
63 mul WORD PTR es:[bx+4] ; w* *a
64 add ax,WORD PTR ds:[di+4] ; + *r
65 adc dx,0
66 adc ax,si
67 adc dx,0
68 mov WORD PTR ds:[di+4],ax
69 mov si,dx
70 ;
71 mov ax,cx
72 mul WORD PTR es:[bx+6] ; w* *a
73 add ax,WORD PTR ds:[di+6] ; + *r
74 adc dx,0
75 adc ax,si
76 adc dx,0
77 mov WORD PTR ds:[di+6],ax
78 mov si,dx
79 ;
80 add bx,8
81 add di,8
82 ;
83 dec bp
84 je $L555
85 jmp $L546
86;
87;
88$L555:
89 mov bp,sp
90 mov bp,WORD PTR [bp+24] ; load num
91 and bp,3
92 dec bp
93 js $L547
94
95 mov ax,cx
96 mul WORD PTR es:[bx] ; w* *a
97 add ax,WORD PTR ds:[di] ; + *r
98 adc dx,0
99 adc ax,si
100 adc dx,0
101 mov WORD PTR ds:[di],ax
102 mov si,dx
103 dec bp
104 js $L547 ; Note that we are now testing for -1
105 ;
106 mov ax,cx
107 mul WORD PTR es:[bx+2] ; w* *a
108 add ax,WORD PTR ds:[di+2] ; + *r
109 adc dx,0
110 adc ax,si
111 adc dx,0
112 mov WORD PTR ds:[di+2],ax
113 mov si,dx
114 dec bp
115 js $L547
116 ;
117 mov ax,cx
118 mul WORD PTR es:[bx+4] ; w* *a
119 add ax,WORD PTR ds:[di+4] ; + *r
120 adc dx,0
121 adc ax,si
122 adc dx,0
123 mov WORD PTR ds:[di+4],ax
124 mov si,dx
125$L547:
126 mov ax,si
127 pop es
128 pop ds
129 pop di
130 pop si
131 pop bx
132 pop bp
133 ret
134 nop
135
136_bn_mul_add_words ENDP
137 PUBLIC _bn_mul_words
138_bn_mul_words PROC FAR
139; Line 76
140 push bp
141 push bx
142 push si
143 push di
144 push ds
145 push es
146 xor si,si
147 mov bp,sp
148 mov di,WORD PTR [bp+16] ; r
149 mov ds,WORD PTR [bp+18]
150 mov bx,WORD PTR [bp+20] ; a
151 mov es,WORD PTR [bp+22]
152 mov cx,WORD PTR [bp+26] ; w
153 mov bp,WORD PTR [bp+24] ; num
154$FC743:
155 mov ax,cx
156 mul WORD PTR es:[bx]
157 add ax,si
158 adc dx,0
159 mov WORD PTR ds:[di],ax
160 mov si,dx
161 dec bp
162 je $L764
163 ;
164 mov ax,cx
165 mul WORD PTR es:[bx+2]
166 add ax,si
167 adc dx,0
168 mov WORD PTR ds:[di+2],ax
169 mov si,dx
170 dec bp
171 je $L764
172 ;
173 mov ax,cx
174 mul WORD PTR es:[bx+4]
175 add ax,si
176 adc dx,0
177 mov WORD PTR ds:[di+4],ax
178 mov si,dx
179 dec bp
180 je $L764
181 ;
182 mov ax,cx
183 mul WORD PTR es:[bx+6]
184 add ax,si
185 adc dx,0
186 mov WORD PTR ds:[di+6],ax
187 mov si,dx
188 dec bp
189 je $L764
190 ;
191 add bx,8
192 add di,8
193 jmp $FC743
194 nop
195$L764:
196 mov ax,si
197 pop es
198 pop ds
199 pop di
200 pop si
201 pop bx
202 pop bp
203 ret
204 nop
205_bn_mul_words ENDP
206 PUBLIC _bn_sqr_words
207_bn_sqr_words PROC FAR
208; Line 92
209 push bp
210 push bx
211 push si
212 push di
213 push ds
214 push es
215 mov bp,sp
216 mov si,WORD PTR [bp+16]
217 mov ds,WORD PTR [bp+18]
218 mov di,WORD PTR [bp+20]
219 mov es,WORD PTR [bp+22]
220 mov bx,WORD PTR [bp+24]
221
222 mov bp,bx ; save a memory lookup later
223 shr bx,1 ; div count by 4 and do groups of 4
224 shr bx,1
225 je $L666
226
227$L765:
228 mov ax,WORD PTR es:[di]
229 mul ax
230 mov WORD PTR ds:[si],ax
231 mov WORD PTR ds:[si+2],dx
232 ;
233 mov ax,WORD PTR es:[di+2]
234 mul ax
235 mov WORD PTR ds:[si+4],ax
236 mov WORD PTR ds:[si+6],dx
237 ;
238 mov ax,WORD PTR es:[di+4]
239 mul ax
240 mov WORD PTR ds:[si+8],ax
241 mov WORD PTR ds:[si+10],dx
242 ;
243 mov ax,WORD PTR es:[di+6]
244 mul ax
245 mov WORD PTR ds:[si+12],ax
246 mov WORD PTR ds:[si+14],dx
247 ;
248 add di,8
249 add si,16
250 dec bx
251 je $L666
252 jmp $L765
253$L666:
254 and bp,3
255 dec bp ; The copied value of bx (num)
256 js $L645
257 ;
258 mov ax,WORD PTR es:[di]
259 mul ax
260 mov WORD PTR ds:[si],ax
261 mov WORD PTR ds:[si+2],dx
262 dec bp
263 js $L645
264 ;
265 mov ax,WORD PTR es:[di+2]
266 mul ax
267 mov WORD PTR ds:[si+4],ax
268 mov WORD PTR ds:[si+6],dx
269 dec bp
270 js $L645
271 ;
272 mov ax,WORD PTR es:[di+4]
273 mul ax
274 mov WORD PTR ds:[si+8],ax
275 mov WORD PTR ds:[si+10],dx
276$L645:
277 pop es
278 pop ds
279 pop di
280 pop si
281 pop bx
282 pop bp
283 ret
284
285_bn_sqr_words ENDP
286 PUBLIC _bn_div64
287_bn_div64 PROC FAR
288 push bp
289 mov bp,sp
290 mov dx, WORD PTR [bp+6]
291 mov ax, WORD PTR [bp+8]
292 div WORD PTR [bp+10]
293 pop bp
294 ret
295_bn_div64 ENDP
296F_TEXT ENDS
297END
diff --git a/src/lib/libcrypto/bn/asm/x86w32.asm b/src/lib/libcrypto/bn/asm/x86w32.asm
new file mode 100644
index 0000000000..fc6f917714
--- /dev/null
+++ b/src/lib/libcrypto/bn/asm/x86w32.asm
@@ -0,0 +1,362 @@
1; Static Name Aliases
2;
3 TITLE bn_mulw.c
4 .386
5F_TEXT SEGMENT WORD USE16 PUBLIC 'CODE'
6F_TEXT ENDS
7_DATA SEGMENT WORD USE16 PUBLIC 'DATA'
8_DATA ENDS
9CONST SEGMENT WORD USE16 PUBLIC 'CONST'
10CONST ENDS
11_BSS SEGMENT WORD USE16 PUBLIC 'BSS'
12_BSS ENDS
13DGROUP GROUP CONST, _BSS, _DATA
14 ASSUME DS: DGROUP, SS: DGROUP
15F_TEXT SEGMENT
16 ASSUME CS: F_TEXT
17 PUBLIC _bn_mul_add_words
18_bn_mul_add_words PROC FAR
19; Line 58
20 push bp
21 push bx
22 push esi
23 push di
24 push ds
25 push es
26 mov bp,sp
27; w = 28
28; num = 26
29; ap = 22
30; rp = 18
31 xor esi,esi ;c=0;
32 mov di,WORD PTR [bp+18] ; load r
33 mov ds,WORD PTR [bp+20] ; load r
34 mov bx,WORD PTR [bp+22] ; load a
35 mov es,WORD PTR [bp+24] ; load a
36 mov ecx,DWORD PTR [bp+28] ; load w
37 mov bp,WORD PTR [bp+26] ; load num
38 shr bp,1 ; div count by 4 and do groups of 4
39 shr bp,1
40 je $L555
41
42$L546:
43 mov eax,ecx
44 mul DWORD PTR es:[bx] ; w* *a
45 add eax,DWORD PTR ds:[di] ; + *r
46 adc edx,0
47 adc eax,esi
48 adc edx,0
49 mov DWORD PTR ds:[di],eax
50 mov esi,edx
51 ;
52 mov eax,ecx
53 mul DWORD PTR es:[bx+4] ; w* *a
54 add eax,DWORD PTR ds:[di+4] ; + *r
55 adc edx,0
56 adc eax,esi
57 adc edx,0
58 mov DWORD PTR ds:[di+4],eax
59 mov esi,edx
60 ;
61 mov eax,ecx
62 mul DWORD PTR es:[bx+8] ; w* *a
63 add eax,DWORD PTR ds:[di+8] ; + *r
64 adc edx,0
65 adc eax,esi
66 adc edx,0
67 mov DWORD PTR ds:[di+8],eax
68 mov esi,edx
69 ;
70 mov eax,ecx
71 mul DWORD PTR es:[bx+12] ; w* *a
72 add eax,DWORD PTR ds:[di+12] ; + *r
73 adc edx,0
74 adc eax,esi
75 adc edx,0
76 mov DWORD PTR ds:[di+12],eax
77 mov esi,edx
78 ;
79 add bx,16
80 add di,16
81 ;
82 dec bp
83 je $L555
84 jmp $L546
85;
86;
87$L555:
88 mov bp,sp
89 mov bp,WORD PTR [bp+26] ; load num
90 and bp,3
91 dec bp
92 js $L547
93
94 mov eax,ecx
95 mul DWORD PTR es:[bx] ; w* *a
96 add eax,DWORD PTR ds:[di] ; + *r
97 adc edx,0
98 adc eax,esi
99 adc edx,0
100 mov DWORD PTR ds:[di],eax
101 mov esi,edx
102 dec bp
103 js $L547 ; Note that we are now testing for -1
104 ;
105 mov eax,ecx
106 mul DWORD PTR es:[bx+4] ; w* *a
107 add eax,DWORD PTR ds:[di+4] ; + *r
108 adc edx,0
109 adc eax,esi
110 adc edx,0
111 mov DWORD PTR ds:[di+4],eax
112 mov esi,edx
113 dec bp
114 js $L547
115 ;
116 mov eax,ecx
117 mul DWORD PTR es:[bx+8] ; w* *a
118 add eax,DWORD PTR ds:[di+8] ; + *r
119 adc edx,0
120 adc eax,esi
121 adc edx,0
122 mov DWORD PTR ds:[di+8],eax
123 mov esi,edx
124$L547:
125 mov eax,esi
126 mov edx,esi
127 shr edx,16
128 pop es
129 pop ds
130 pop di
131 pop esi
132 pop bx
133 pop bp
134 ret
135 nop
136_bn_mul_add_words ENDP
137
138 PUBLIC _bn_mul_words
139_bn_mul_words PROC FAR
140; Line 76
141 push bp
142 push bx
143 push esi
144 push di
145 push ds
146 push es
147 xor esi,esi
148 mov bp,sp
149 mov di,WORD PTR [bp+18] ; r
150 mov ds,WORD PTR [bp+20]
151 mov bx,WORD PTR [bp+22] ; a
152 mov es,WORD PTR [bp+24]
153 mov ecx,DWORD PTR [bp+28] ; w
154 mov bp,WORD PTR [bp+26] ; num
155
156$FC743:
157 mov eax,ecx
158 mul DWORD PTR es:[bx]
159 add eax,esi
160 adc edx,0
161 mov DWORD PTR ds:[di],eax
162 mov esi,edx
163 dec bp
164 je $L764
165 ;
166 mov eax,ecx
167 mul DWORD PTR es:[bx+4]
168 add eax,esi
169 adc edx,0
170 mov DWORD PTR ds:[di+4],eax
171 mov esi,edx
172 dec bp
173 je $L764
174 ;
175 mov eax,ecx
176 mul DWORD PTR es:[bx+8]
177 add eax,esi
178 adc edx,0
179 mov DWORD PTR ds:[di+8],eax
180 mov esi,edx
181 dec bp
182 je $L764
183 ;
184 mov eax,ecx
185 mul DWORD PTR es:[bx+12]
186 add eax,esi
187 adc edx,0
188 mov DWORD PTR ds:[di+12],eax
189 mov esi,edx
190 dec bp
191 je $L764
192 ;
193 add bx,16
194 add di,16
195 jmp $FC743
196 nop
197$L764:
198 mov eax,esi
199 mov edx,esi
200 shr edx,16
201 pop es
202 pop ds
203 pop di
204 pop esi
205 pop bx
206 pop bp
207 ret
208 nop
209_bn_mul_words ENDP
210 PUBLIC _bn_sqr_words
211_bn_sqr_words PROC FAR
212; Line 92
213 push bp
214 push bx
215 push si
216 push di
217 push ds
218 push es
219 mov bp,sp
220 mov si,WORD PTR [bp+16]
221 mov ds,WORD PTR [bp+18]
222 mov di,WORD PTR [bp+20]
223 mov es,WORD PTR [bp+22]
224 mov bx,WORD PTR [bp+24]
225
226 mov bp,bx ; save a memory lookup later
227 shr bx,1 ; div count by 4 and do groups of 4
228 shr bx,1
229 je $L666
230
231$L765:
232 mov eax,DWORD PTR es:[di]
233 mul eax
234 mov DWORD PTR ds:[si],eax
235 mov DWORD PTR ds:[si+4],edx
236 ;
237 mov eax,DWORD PTR es:[di+4]
238 mul eax
239 mov DWORD PTR ds:[si+8],eax
240 mov DWORD PTR ds:[si+12],edx
241 ;
242 mov eax,DWORD PTR es:[di+8]
243 mul eax
244 mov DWORD PTR ds:[si+16],eax
245 mov DWORD PTR ds:[si+20],edx
246 ;
247 mov eax,DWORD PTR es:[di+12]
248 mul eax
249 mov DWORD PTR ds:[si+24],eax
250 mov DWORD PTR ds:[si+28],edx
251 ;
252 add di,16
253 add si,32
254 dec bx
255 je $L666
256 jmp $L765
257$L666:
258 and bp,3
259 dec bp ; The copied value of bx (num)
260 js $L645
261 ;
262 mov eax,DWORD PTR es:[di]
263 mul eax
264 mov DWORD PTR ds:[si],eax
265 mov DWORD PTR ds:[si+4],edx
266 dec bp
267 js $L645
268 ;
269 mov eax,DWORD PTR es:[di+4]
270 mul eax
271 mov DWORD PTR ds:[si+8],eax
272 mov DWORD PTR ds:[si+12],edx
273 dec bp
274 js $L645
275 ;
276 mov eax,DWORD PTR es:[di+8]
277 mul eax
278 mov DWORD PTR ds:[si+16],eax
279 mov DWORD PTR ds:[si+20],edx
280$L645:
281 pop es
282 pop ds
283 pop di
284 pop si
285 pop bx
286 pop bp
287 ret
288_bn_sqr_words ENDP
289
290 PUBLIC _bn_div64
291_bn_div64 PROC FAR
292 push bp
293 mov bp,sp
294 mov edx, DWORD PTR [bp+6]
295 mov eax, DWORD PTR [bp+10]
296 div DWORD PTR [bp+14]
297 mov edx,eax
298 shr edx,16
299 pop bp
300 ret
301_bn_div64 ENDP
302
303 PUBLIC _bn_add_words
304_bn_add_words PROC FAR
305; Line 58
306 push bp
307 push bx
308 push esi
309 push di
310 push ds
311 push es
312 mov bp,sp
313; w = 28
314; num = 26
315; ap = 22
316; rp = 18
317 xor esi,esi ;c=0;
318 mov si,WORD PTR [bp+22] ; load a
319 mov es,WORD PTR [bp+24] ; load a
320 mov di,WORD PTR [bp+26] ; load b
321 mov ds,WORD PTR [bp+28] ; load b
322
323 mov dx,WORD PTR [bp+30] ; load num
324 dec dx
325 js $L547
326 xor ecx,ecx
327
328$L5477:
329 xor ebx,ebx
330 mov eax,DWORD PTR es:[si] ; *a
331 add eax,ecx
332 adc ebx,0
333 add si,4 ; a++
334 add eax,DWORD PTR ds:[di] ; + *b
335 mov ecx,ebx
336 adc ecx,0
337 add di,4
338 mov bx,WORD PTR [bp+18]
339 mov ds,WORD PTR [bp+20]
340 mov DWORD PTR ds:[bx],eax
341 add bx,4
342 mov ds,WORD PTR [bp+28]
343 mov WORD PTR [bp+18],bx
344 dec dx
345 js $L547 ; Note that we are now testing for -1
346 jmp $L5477
347 ;
348$L547:
349 mov eax,ecx
350 mov edx,ecx
351 shr edx,16
352 pop es
353 pop ds
354 pop di
355 pop esi
356 pop bx
357 pop bp
358 ret
359 nop
360_bn_add_words ENDP
361F_TEXT ENDS
362END
diff --git a/src/lib/libcrypto/bn/bn.err b/src/lib/libcrypto/bn/bn.err
new file mode 100644
index 0000000000..7ccc247c41
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn.err
@@ -0,0 +1,27 @@
1/* Error codes for the BN functions. */
2
3/* Function codes. */
4#define BN_F_BN_BLINDING_CONVERT 100
5#define BN_F_BN_BLINDING_INVERT 101
6#define BN_F_BN_BLINDING_NEW 102
7#define BN_F_BN_BLINDING_UPDATE 103
8#define BN_F_BN_BN2DEC 104
9#define BN_F_BN_BN2HEX 105
10#define BN_F_BN_CTX_NEW 106
11#define BN_F_BN_DIV 107
12#define BN_F_BN_EXPAND2 108
13#define BN_F_BN_MOD_EXP_MONT 109
14#define BN_F_BN_MOD_INVERSE 110
15#define BN_F_BN_MOD_MUL_RECIPROCAL 111
16#define BN_F_BN_MPI2BN 112
17#define BN_F_BN_NEW 113
18#define BN_F_BN_RAND 114
19
20/* Reason codes. */
21#define BN_R_BAD_RECIPROCAL 100
22#define BN_R_CALLED_WITH_EVEN_MODULUS 101
23#define BN_R_DIV_BY_ZERO 102
24#define BN_R_ENCODING_ERROR 103
25#define BN_R_INVALID_LENGTH 104
26#define BN_R_NOT_INITALISED 105
27#define BN_R_NO_INVERSE 106
diff --git a/src/lib/libcrypto/bn/bn.org b/src/lib/libcrypto/bn/bn.org
new file mode 100644
index 0000000000..66dde285d6
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn.org
@@ -0,0 +1,502 @@
1/* crypto/bn/bn.org */
2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
60 *
61 * Always modify bn.org since bn.h is automatically generated from
62 * it during SSLeay configuration.
63 *
64 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
65 */
66
67#ifndef HEADER_BN_H
68#define HEADER_BN_H
69
70#ifdef __cplusplus
71extern "C" {
72#endif
73
74#undef BN_LLONG
75
76#ifdef WIN32
77#define BN_LLONG /* This comment stops Configure mutilating things */
78#endif
79
80#define RECP_MUL_MOD
81#define MONT_MUL_MOD
82
83/* This next option uses the C libraries (2 word)/(1 word) function.
84 * If it is not defined, I use my C version (which is slower).
85 * The reason for this flag is that when the particular C compiler
86 * library routine is used, and the library is linked with a different
87 * compiler, the library is missing. This mostly happens when the
88 * library is built with gcc and then linked using nornal cc. This would
89 * be a common occurance because gcc normally produces code that is
90 * 2 times faster than system compilers for the big number stuff.
91 * For machines with only one compiler (or shared libraries), this should
92 * be on. Again this in only really a problem on machines
93 * using "long long's", are 32bit, and are not using my assember code. */
94#if defined(MSDOS) || defined(WINDOWS) || defined(linux)
95#define BN_DIV2W
96#endif
97
98/* Only one for the following should be defined */
99/* The prime number generation stuff may not work when
100 * EIGHT_BIT but I don't care since I've only used this mode
101 * for debuging the bignum libraries */
102#undef SIXTY_FOUR_BIT_LONG
103#undef SIXTY_FOUR_BIT
104#define THIRTY_TWO_BIT
105#undef SIXTEEN_BIT
106#undef EIGHT_BIT
107
108/* assuming long is 64bit - this is the DEC Alpha
109 * unsigned long long is only 64 bits :-(, don't define
110 * BN_LLONG for the DEC Alpha */
111#ifdef SIXTY_FOUR_BIT_LONG
112#define BN_ULLONG unsigned long long
113#define BN_ULONG unsigned long
114#define BN_LONG long
115#define BN_BITS 128
116#define BN_BYTES 8
117#define BN_BITS2 64
118#define BN_BITS4 32
119#define BN_MASK2 (0xffffffffffffffffL)
120#define BN_MASK2l (0xffffffffL)
121#define BN_MASK2h (0xffffffff00000000L)
122#define BN_MASK2h1 (0xffffffff80000000L)
123#define BN_TBIT (0x8000000000000000L)
124#define BN_DEC_CONV (10000000000000000000L)
125#define BN_DEC_FMT1 "%lu"
126#define BN_DEC_FMT2 "%019lu"
127#define BN_DEC_NUM 19
128#endif
129
130#ifdef SIXTY_FOUR_BIT
131#undef BN_LLONG
132/* #define BN_ULLONG unsigned long long */
133#define BN_ULONG unsigned long long
134#define BN_LONG long long
135#define BN_BITS 128
136#define BN_BYTES 8
137#define BN_BITS2 64
138#define BN_BITS4 32
139#define BN_MASK2 (0xffffffffffffffffLL)
140#define BN_MASK2l (0xffffffffL)
141#define BN_MASK2h (0xffffffff00000000LL)
142#define BN_MASK2h1 (0xffffffff80000000LL)
143#define BN_TBIT (0x8000000000000000LL)
144#define BN_DEC_CONV (10000000000000000000L)
145#define BN_DEC_FMT1 "%lu"
146#define BN_DEC_FMT2 "%019lu"
147#define BN_DEC_NUM 19
148#endif
149
150#ifdef THIRTY_TWO_BIT
151#ifdef WIN32
152#define BN_ULLONG unsigned _int64
153#else
154#define BN_ULLONG unsigned long long
155#endif
156#define BN_ULONG unsigned long
157#define BN_LONG long
158#define BN_BITS 64
159#define BN_BYTES 4
160#define BN_BITS2 32
161#define BN_BITS4 16
162#define BN_MASK2 (0xffffffffL)
163#define BN_MASK2l (0xffff)
164#define BN_MASK2h1 (0xffff8000L)
165#define BN_MASK2h (0xffff0000L)
166#define BN_TBIT (0x80000000L)
167#define BN_DEC_CONV (1000000000L)
168#define BN_DEC_FMT1 "%lu"
169#define BN_DEC_FMT2 "%09lu"
170#define BN_DEC_NUM 9
171#endif
172
173#ifdef SIXTEEN_BIT
174#ifndef BN_DIV2W
175#define BN_DIV2W
176#endif
177#define BN_ULLONG unsigned long
178#define BN_ULONG unsigned short
179#define BN_LONG short
180#define BN_BITS 32
181#define BN_BYTES 2
182#define BN_BITS2 16
183#define BN_BITS4 8
184#define BN_MASK2 (0xffff)
185#define BN_MASK2l (0xff)
186#define BN_MASK2h1 (0xff80)
187#define BN_MASK2h (0xff00)
188#define BN_TBIT (0x8000)
189#define BN_DEC_CONV (100000)
190#define BN_DEC_FMT1 "%u"
191#define BN_DEC_FMT2 "%05u"
192#define BN_DEC_NUM 5
193#endif
194
195#ifdef EIGHT_BIT
196#ifndef BN_DIV2W
197#define BN_DIV2W
198#endif
199#define BN_ULLONG unsigned short
200#define BN_ULONG unsigned char
201#define BN_LONG char
202#define BN_BITS 16
203#define BN_BYTES 1
204#define BN_BITS2 8
205#define BN_BITS4 4
206#define BN_MASK2 (0xff)
207#define BN_MASK2l (0xf)
208#define BN_MASK2h1 (0xf8)
209#define BN_MASK2h (0xf0)
210#define BN_TBIT (0x80)
211#define BN_DEC_CONV (100)
212#define BN_DEC_FMT1 "%u"
213#define BN_DEC_FMT2 "%02u"
214#define BN_DEC_NUM 2
215#endif
216
217#define BN_DEFAULT_BITS 1280
218
219#ifdef BIGNUM
220#undef BIGNUM
221#endif
222
223typedef struct bignum_st
224 {
225 BN_ULONG *d; /* Pointer to an array of 'BN_BITS2' bit chunks. */
226 int top; /* Index of last used d +1. */
227 /* The next are internal book keeping for bn_expand. */
228 int max; /* Size of the d array. */
229 int neg; /* one if the number is negative */
230 } BIGNUM;
231
232/* Used for temp variables */
233#define BN_CTX_NUM 12
234typedef struct bignum_ctx
235 {
236 int tos;
237 BIGNUM *bn[BN_CTX_NUM+1];
238 } BN_CTX;
239
240typedef struct bn_blinding_st
241 {
242 int init;
243 BIGNUM *A;
244 BIGNUM *Ai;
245 BIGNUM *mod; /* just a reference */
246 } BN_BLINDING;
247
248/* Used for montgomery multiplication */
249typedef struct bn_mont_ctx_st
250 {
251 int ri; /* number of bits in R */
252 BIGNUM *RR; /* used to convert to montgomery form */
253 BIGNUM *N; /* The modulus */
254 BIGNUM *Ni; /* The inverse of N */
255 BN_ULONG n0; /* word form of inverse, normally only one of
256 * Ni or n0 is defined */
257 } BN_MONT_CTX;
258
259#define BN_to_montgomery(r,a,mont,ctx) BN_mod_mul_montgomery(\
260 r,a,(mont)->RR,(mont),ctx)
261
262#define BN_prime_checks (5)
263
264#define BN_num_bytes(a) ((BN_num_bits(a)+7)/8)
265#define BN_is_word(a,w) (((a)->top == 1) && ((a)->d[0] == (BN_ULONG)(w)))
266#define BN_is_zero(a) (((a)->top <= 1) && ((a)->d[0] == (BN_ULONG)0))
267#define BN_is_one(a) (BN_is_word((a),1))
268#define BN_is_odd(a) ((a)->d[0] & 1)
269#define BN_one(a) (BN_set_word((a),1))
270#define BN_zero(a) (BN_set_word((a),0))
271
272#define BN_ascii2bn(a) BN_hex2bn(a)
273#define BN_bn2ascii(a) BN_bn2hex(a)
274
275#define bn_fix_top(a) \
276 { \
277 BN_ULONG *fix_top_l; \
278 for (fix_top_l= &((a)->d[(a)->top-1]); (a)->top > 0; (a)->top--) \
279 if (*(fix_top_l--)) break; \
280 }
281
282#define bn_expand(n,b) ((((b)/BN_BITS2) <= (n)->max)?\
283 (n):bn_expand2((n),(b)/BN_BITS2))
284#define bn_wexpand(n,b) (((b) <= (n)->max)?(n):bn_expand2((n),(b)))
285
286
287#ifndef NOPROTO
288BIGNUM *BN_value_one(void);
289char * BN_options(void);
290BN_CTX *BN_CTX_new(void);
291void BN_CTX_free(BN_CTX *c);
292int BN_rand(BIGNUM *rnd, int bits, int top,int bottom);
293int BN_num_bits(BIGNUM *a);
294int BN_num_bits_word(BN_ULONG);
295BIGNUM *BN_new(void);
296void BN_clear_free(BIGNUM *a);
297BIGNUM *BN_copy(BIGNUM *a, BIGNUM *b);
298BIGNUM *BN_bin2bn(unsigned char *s,int len,BIGNUM *ret);
299int BN_bn2bin(BIGNUM *a, unsigned char *to);
300BIGNUM *BN_mpi2bn(unsigned char *s,int len,BIGNUM *ret);
301int BN_bn2mpi(BIGNUM *a, unsigned char *to);
302int BN_sub(BIGNUM *r, BIGNUM *a, BIGNUM *b);
303void bn_qsub(BIGNUM *r, BIGNUM *a, BIGNUM *b);
304void bn_qadd(BIGNUM *r, BIGNUM *a, BIGNUM *b);
305int BN_add(BIGNUM *r, BIGNUM *a, BIGNUM *b);
306int BN_mod(BIGNUM *rem, BIGNUM *m, BIGNUM *d, BN_CTX *ctx);
307int BN_div(BIGNUM *dv, BIGNUM *rem, BIGNUM *m, BIGNUM *d, BN_CTX *ctx);
308int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b);
309int BN_sqr(BIGNUM *r, BIGNUM *a,BN_CTX *ctx);
310BN_ULONG BN_mod_word(BIGNUM *a, unsigned long w);
311BN_ULONG BN_div_word(BIGNUM *a, unsigned long w);
312int BN_mul_word(BIGNUM *a, unsigned long w);
313int BN_add_word(BIGNUM *a, unsigned long w);
314int BN_sub_word(BIGNUM *a, unsigned long w);
315int BN_set_word(BIGNUM *a, unsigned long w);
316unsigned long BN_get_word(BIGNUM *a);
317int BN_cmp(BIGNUM *a, BIGNUM *b);
318void BN_free(BIGNUM *a);
319int BN_is_bit_set(BIGNUM *a, int n);
320int BN_lshift(BIGNUM *r, BIGNUM *a, int n);
321int BN_lshift1(BIGNUM *r, BIGNUM *a);
322int BN_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p,BN_CTX *ctx);
323int BN_mod_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BIGNUM *m,BN_CTX *ctx);
324int BN_mod_exp_mont(BIGNUM *r, BIGNUM *a, BIGNUM *p, BIGNUM *m,BN_CTX *ctx,
325 BN_MONT_CTX *m_ctx);
326int BN_mod_exp_recp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BIGNUM *m,BN_CTX *ctx);
327int BN_mod_exp_simple(BIGNUM *r, BIGNUM *a, BIGNUM *p,
328 BIGNUM *m,BN_CTX *ctx);
329int BN_mask_bits(BIGNUM *a,int n);
330int BN_mod_mul_reciprocal(BIGNUM *r, BIGNUM *x, BIGNUM *y, BIGNUM *m,
331 BIGNUM *i, int nb, BN_CTX *ctx);
332int BN_mod_mul(BIGNUM *ret, BIGNUM *a, BIGNUM *b, BIGNUM *m,
333 BN_CTX *ctx);
334#ifndef WIN16
335int BN_print_fp(FILE *fp, BIGNUM *a);
336#endif
337#ifdef HEADER_BIO_H
338int BN_print(BIO *fp, BIGNUM *a);
339#else
340int BN_print(char *fp, BIGNUM *a);
341#endif
342int BN_reciprocal(BIGNUM *r, BIGNUM *m, BN_CTX *ctx);
343int BN_rshift(BIGNUM *r, BIGNUM *a, int n);
344int BN_rshift1(BIGNUM *r, BIGNUM *a);
345void BN_clear(BIGNUM *a);
346BIGNUM *bn_expand2(BIGNUM *b, int bits);
347BIGNUM *BN_dup(BIGNUM *a);
348int BN_ucmp(BIGNUM *a, BIGNUM *b);
349int BN_set_bit(BIGNUM *a, int n);
350int BN_clear_bit(BIGNUM *a, int n);
351char * BN_bn2hex(BIGNUM *a);
352char * BN_bn2dec(BIGNUM *a);
353int BN_hex2bn(BIGNUM **a,char *str);
354int BN_dec2bn(BIGNUM **a,char *str);
355int BN_gcd(BIGNUM *r,BIGNUM *in_a,BIGNUM *in_b,BN_CTX *ctx);
356BIGNUM *BN_mod_inverse(BIGNUM *a, BIGNUM *n,BN_CTX *ctx);
357BIGNUM *BN_generate_prime(int bits,int strong,BIGNUM *add,
358 BIGNUM *rem,void (*callback)(int,int,char *),char *cb_arg);
359int BN_is_prime(BIGNUM *p,int nchecks,void (*callback)(int,int,char *),
360 BN_CTX *ctx,char *cb_arg);
361void ERR_load_BN_strings(void );
362
363BN_ULONG bn_mul_add_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w);
364BN_ULONG bn_mul_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w);
365void bn_sqr_words(BN_ULONG *rp, BN_ULONG *ap, int num);
366BN_ULONG bn_div64(BN_ULONG h, BN_ULONG l, BN_ULONG d);
367BN_ULONG bn_add_words(BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,int num);
368
369BN_MONT_CTX *BN_MONT_CTX_new(void );
370int BN_mod_mul_montgomery(BIGNUM *r,BIGNUM *a,BIGNUM *b,BN_MONT_CTX *mont,
371 BN_CTX *ctx);
372int BN_from_montgomery(BIGNUM *r,BIGNUM *a,BN_MONT_CTX *mont,BN_CTX *ctx);
373void BN_MONT_CTX_free(BN_MONT_CTX *mont);
374int BN_MONT_CTX_set(BN_MONT_CTX *mont,BIGNUM *modulus,BN_CTX *ctx);
375
376BN_BLINDING *BN_BLINDING_new(BIGNUM *A,BIGNUM *Ai,BIGNUM *mod);
377void BN_BLINDING_free(BN_BLINDING *b);
378int BN_BLINDING_update(BN_BLINDING *b,BN_CTX *ctx);
379int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *r, BN_CTX *ctx);
380int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
381
382#else
383
384BIGNUM *BN_value_one();
385char * BN_options();
386BN_CTX *BN_CTX_new();
387void BN_CTX_free();
388int BN_rand();
389int BN_num_bits();
390int BN_num_bits_word();
391BIGNUM *BN_new();
392void BN_clear_free();
393BIGNUM *BN_copy();
394BIGNUM *BN_bin2bn();
395int BN_bn2bin();
396BIGNUM *BN_mpi2bn();
397int BN_bn2mpi();
398int BN_sub();
399void bn_qsub();
400void bn_qadd();
401int BN_add();
402int BN_mod();
403int BN_div();
404int BN_mul();
405int BN_sqr();
406BN_ULONG BN_mod_word();
407BN_ULONG BN_div_word();
408int BN_add_word();
409int BN_sub_word();
410int BN_mul_word();
411int BN_set_word();
412unsigned long BN_get_word();
413int BN_cmp();
414void BN_free();
415int BN_is_bit_set();
416int BN_lshift();
417int BN_lshift1();
418int BN_exp();
419int BN_mod_exp();
420int BN_mod_exp_mont();
421int BN_mod_exp_recp();
422int BN_mod_exp_simple();
423int BN_mask_bits();
424int BN_mod_mul_reciprocal();
425int BN_mod_mul();
426#ifndef WIN16
427int BN_print_fp();
428#endif
429int BN_print();
430int BN_reciprocal();
431int BN_rshift();
432int BN_rshift1();
433void BN_clear();
434BIGNUM *bn_expand2();
435BIGNUM *BN_dup();
436int BN_ucmp();
437int BN_set_bit();
438int BN_clear_bit();
439char * BN_bn2hex();
440char * BN_bn2dec();
441int BN_hex2bn();
442int BN_dec2bn();
443int BN_gcd();
444BIGNUM *BN_mod_inverse();
445BIGNUM *BN_generate_prime();
446int BN_is_prime();
447void ERR_load_BN_strings();
448
449BN_ULONG bn_mul_add_words();
450BN_ULONG bn_mul_words();
451void bn_sqr_words();
452BN_ULONG bn_div64();
453BN_ULONG bn_add_words();
454
455int BN_mod_mul_montgomery();
456int BN_from_montgomery();
457BN_MONT_CTX *BN_MONT_CTX_new();
458void BN_MONT_CTX_free();
459int BN_MONT_CTX_set();
460
461BN_BLINDING *BN_BLINDING_new();
462void BN_BLINDING_free();
463int BN_BLINDING_update();
464int BN_BLINDING_convert();
465int BN_BLINDING_invert();
466
467#endif
468
469/* BEGIN ERROR CODES */
470/* Error codes for the BN functions. */
471
472/* Function codes. */
473#define BN_F_BN_BLINDING_CONVERT 100
474#define BN_F_BN_BLINDING_INVERT 101
475#define BN_F_BN_BLINDING_NEW 102
476#define BN_F_BN_BLINDING_UPDATE 103
477#define BN_F_BN_BN2DEC 104
478#define BN_F_BN_BN2HEX 105
479#define BN_F_BN_CTX_NEW 106
480#define BN_F_BN_DIV 107
481#define BN_F_BN_EXPAND2 108
482#define BN_F_BN_MOD_EXP_MONT 109
483#define BN_F_BN_MOD_INVERSE 110
484#define BN_F_BN_MOD_MUL_RECIPROCAL 111
485#define BN_F_BN_MPI2BN 112
486#define BN_F_BN_NEW 113
487#define BN_F_BN_RAND 114
488
489/* Reason codes. */
490#define BN_R_BAD_RECIPROCAL 100
491#define BN_R_CALLED_WITH_EVEN_MODULUS 101
492#define BN_R_DIV_BY_ZERO 102
493#define BN_R_ENCODING_ERROR 103
494#define BN_R_INVALID_LENGTH 104
495#define BN_R_NOT_INITALISED 105
496#define BN_R_NO_INVERSE 106
497
498#ifdef __cplusplus
499}
500#endif
501#endif
502
diff --git a/src/lib/libcrypto/bn/bn_m.c b/src/lib/libcrypto/bn/bn_m.c
new file mode 100644
index 0000000000..5166daaeec
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_m.c
@@ -0,0 +1,169 @@
1/* crypto/bn/bn_m.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn_lcl.h"
62#include "stack.h"
63
64int limit=16;
65
66typedef struct bn_pool_st
67 {
68 int used;
69 int tos;
70 STACK *sk;
71 } BN_POOL;
72
73BIGNUM *BN_POOL_push(bp)
74BN_POOL *bp;
75 {
76 BIGNUM *ret;
77
78 if (bp->used >= bp->tos)
79 {
80 ret=BN_new();
81 sk_push(bp->sk,(char *)ret);
82 bp->tos++;
83 bp->used++;
84 }
85 else
86 {
87 ret=(BIGNUM *)sk_value(bp->sk,bp->used);
88 bp->used++;
89 }
90 return(ret);
91 }
92
93void BN_POOL_pop(bp,num)
94BN_POOL *bp;
95int num;
96 {
97 bp->used-=num;
98 }
99
100int BN_m(r,a,b)
101BIGNUM *r,*a,*b;
102 {
103 static BN_POOL bp;
104 static init=1;
105
106 if (init)
107 {
108 bp.used=0;
109 bp.tos=0;
110 bp.sk=sk_new_null();
111 init=0;
112 }
113 return(BN_mm(r,a,b,&bp));
114 }
115
116/* r must be different to a and b */
117int BN_mm(m, A, B, bp)
118BIGNUM *m,*A,*B;
119BN_POOL *bp;
120 {
121 int i,num;
122 int an,bn;
123 BIGNUM *a,*b,*c,*d,*ac,*bd;
124
125 an=A->top;
126 bn=B->top;
127 if ((an <= limit) || (bn <= limit))
128 {
129 return(BN_mul(m,A,B));
130 }
131
132 a=BN_POOL_push(bp);
133 b=BN_POOL_push(bp);
134 c=BN_POOL_push(bp);
135 d=BN_POOL_push(bp);
136 ac=BN_POOL_push(bp);
137 bd=BN_POOL_push(bp);
138
139 num=(an <= bn)?an:bn;
140 num=1<<(BN_num_bits_word(num-1)-1);
141
142 /* Are going to now chop things into 'num' word chunks. */
143 num*=BN_BITS2;
144
145 BN_copy(a,A);
146 BN_mask_bits(a,num);
147 BN_rshift(b,A,num);
148
149 BN_copy(c,B);
150 BN_mask_bits(c,num);
151 BN_rshift(d,B,num);
152
153 BN_sub(ac ,b,a);
154 BN_sub(bd,c,d);
155 BN_mm(m,ac,bd,bp);
156 BN_mm(ac,a,c,bp);
157 BN_mm(bd,b,d,bp);
158
159 BN_add(m,m,ac);
160 BN_add(m,m,bd);
161 BN_lshift(m,m,num);
162 BN_lshift(bd,bd,num*2);
163
164 BN_add(m,m,ac);
165 BN_add(m,m,bd);
166 BN_POOL_pop(bp,6);
167 return(1);
168 }
169
diff --git a/src/lib/libcrypto/bn/bn_mulw.c b/src/lib/libcrypto/bn/bn_mulw.c
new file mode 100644
index 0000000000..abfc7e4d6c
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_mulw.c
@@ -0,0 +1,366 @@
1/* crypto/bn/bn_mulw.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn_lcl.h"
62
63#ifdef BN_LLONG
64
65BN_ULONG bn_mul_add_words(rp,ap,num,w)
66BN_ULONG *rp,*ap;
67int num;
68BN_ULONG w;
69 {
70 BN_ULONG c1=0;
71
72 for (;;)
73 {
74 mul_add(rp[0],ap[0],w,c1);
75 if (--num == 0) break;
76 mul_add(rp[1],ap[1],w,c1);
77 if (--num == 0) break;
78 mul_add(rp[2],ap[2],w,c1);
79 if (--num == 0) break;
80 mul_add(rp[3],ap[3],w,c1);
81 if (--num == 0) break;
82 ap+=4;
83 rp+=4;
84 }
85
86 return(c1);
87 }
88
89BN_ULONG bn_mul_words(rp,ap,num,w)
90BN_ULONG *rp,*ap;
91int num;
92BN_ULONG w;
93 {
94 BN_ULONG c1=0;
95
96 for (;;)
97 {
98 mul(rp[0],ap[0],w,c1);
99 if (--num == 0) break;
100 mul(rp[1],ap[1],w,c1);
101 if (--num == 0) break;
102 mul(rp[2],ap[2],w,c1);
103 if (--num == 0) break;
104 mul(rp[3],ap[3],w,c1);
105 if (--num == 0) break;
106 ap+=4;
107 rp+=4;
108 }
109 return(c1);
110 }
111
112void bn_sqr_words(r,a,n)
113BN_ULONG *r,*a;
114int n;
115 {
116 for (;;)
117 {
118 BN_ULLONG t;
119
120 t=(BN_ULLONG)(a[0])*(a[0]);
121 r[0]=Lw(t); r[1]=Hw(t);
122 if (--n == 0) break;
123
124 t=(BN_ULLONG)(a[1])*(a[1]);
125 r[2]=Lw(t); r[3]=Hw(t);
126 if (--n == 0) break;
127
128 t=(BN_ULLONG)(a[2])*(a[2]);
129 r[4]=Lw(t); r[5]=Hw(t);
130 if (--n == 0) break;
131
132 t=(BN_ULLONG)(a[3])*(a[3]);
133 r[6]=Lw(t); r[7]=Hw(t);
134 if (--n == 0) break;
135
136 a+=4;
137 r+=8;
138 }
139 }
140
141BN_ULONG bn_add_words(r,a,b,n)
142BN_ULONG *r,*a,*b;
143int n;
144 {
145 BN_ULLONG ll;
146
147 ll=0;
148 for (;;)
149 {
150 ll+= (BN_ULLONG)a[0]+b[0];
151 r[0]=(BN_ULONG)ll&BN_MASK2;
152 ll>>=BN_BITS2;
153 if (--n <= 0) break;
154
155 ll+= (BN_ULLONG)a[1]+b[1];
156 r[1]=(BN_ULONG)ll&BN_MASK2;
157 ll>>=BN_BITS2;
158 if (--n <= 0) break;
159
160 ll+= (BN_ULLONG)a[2]+b[2];
161 r[2]=(BN_ULONG)ll&BN_MASK2;
162 ll>>=BN_BITS2;
163 if (--n <= 0) break;
164
165 ll+= (BN_ULLONG)a[3]+b[3];
166 r[3]=(BN_ULONG)ll&BN_MASK2;
167 ll>>=BN_BITS2;
168 if (--n <= 0) break;
169
170 a+=4;
171 b+=4;
172 r+=4;
173 }
174 return(ll&BN_MASK2);
175 }
176
177#else
178
179BN_ULONG bn_mul_add_words(rp,ap,num,w)
180BN_ULONG *rp,*ap;
181int num;
182BN_ULONG w;
183 {
184 BN_ULONG c=0;
185 BN_ULONG bl,bh;
186
187 bl=LBITS(w);
188 bh=HBITS(w);
189
190 for (;;)
191 {
192 mul_add(rp[0],ap[0],bl,bh,c);
193 if (--num == 0) break;
194 mul_add(rp[1],ap[1],bl,bh,c);
195 if (--num == 0) break;
196 mul_add(rp[2],ap[2],bl,bh,c);
197 if (--num == 0) break;
198 mul_add(rp[3],ap[3],bl,bh,c);
199 if (--num == 0) break;
200 ap+=4;
201 rp+=4;
202 }
203 return(c);
204 }
205
206BN_ULONG bn_mul_words(rp,ap,num,w)
207BN_ULONG *rp,*ap;
208int num;
209BN_ULONG w;
210 {
211 BN_ULONG carry=0;
212 BN_ULONG bl,bh;
213
214 bl=LBITS(w);
215 bh=HBITS(w);
216
217 for (;;)
218 {
219 mul(rp[0],ap[0],bl,bh,carry);
220 if (--num == 0) break;
221 mul(rp[1],ap[1],bl,bh,carry);
222 if (--num == 0) break;
223 mul(rp[2],ap[2],bl,bh,carry);
224 if (--num == 0) break;
225 mul(rp[3],ap[3],bl,bh,carry);
226 if (--num == 0) break;
227 ap+=4;
228 rp+=4;
229 }
230 return(carry);
231 }
232
233void bn_sqr_words(r,a,n)
234BN_ULONG *r,*a;
235int n;
236 {
237 for (;;)
238 {
239 sqr64(r[0],r[1],a[0]);
240 if (--n == 0) break;
241
242 sqr64(r[2],r[3],a[1]);
243 if (--n == 0) break;
244
245 sqr64(r[4],r[5],a[2]);
246 if (--n == 0) break;
247
248 sqr64(r[6],r[7],a[3]);
249 if (--n == 0) break;
250
251 a+=4;
252 r+=8;
253 }
254 }
255
256BN_ULONG bn_add_words(r,a,b,n)
257BN_ULONG *r,*a,*b;
258int n;
259 {
260 BN_ULONG t1,t2;
261 int carry,i;
262
263 carry=0;
264 for (i=0; i<n; i++)
265 {
266 t1= *(a++);
267 t2= *(b++);
268 if (carry)
269 {
270 carry=(t2 >= ((~t1)&BN_MASK2));
271 t2=(t1+t2+1)&BN_MASK2;
272 }
273 else
274 {
275 t2=(t1+t2)&BN_MASK2;
276 carry=(t2<t1);
277 }
278 *(r++)=t2;
279 }
280 return(carry);
281 }
282
283#endif
284
285#if defined(BN_LLONG) && defined(BN_DIV2W)
286
287BN_ULONG bn_div64(h,l,d)
288BN_ULONG h,l,d;
289 {
290 return((BN_ULONG)(((((BN_ULLONG)h)<<BN_BITS2)|l)/(BN_ULLONG)d));
291 }
292
293#else
294
295/* Divide h-l by d and return the result. */
296/* I need to test this some more :-( */
297BN_ULONG bn_div64(h,l,d)
298BN_ULONG h,l,d;
299 {
300 BN_ULONG dh,dl,q,ret=0,th,tl,t;
301 int i,count=2;
302
303 if (d == 0) return(BN_MASK2);
304
305 i=BN_num_bits_word(d);
306 if ((i != BN_BITS2) && (h > (BN_ULONG)1<<i))
307 {
308#if !defined(NO_STDIO) && !defined(WIN16)
309 fprintf(stderr,"Division would overflow (%d)\n",i);
310#endif
311 abort();
312 }
313 i=BN_BITS2-i;
314 if (h >= d) h-=d;
315
316 if (i)
317 {
318 d<<=i;
319 h=(h<<i)|(l>>(BN_BITS2-i));
320 l<<=i;
321 }
322 dh=(d&BN_MASK2h)>>BN_BITS4;
323 dl=(d&BN_MASK2l);
324 for (;;)
325 {
326 if ((h>>BN_BITS4) == dh)
327 q=BN_MASK2l;
328 else
329 q=h/dh;
330
331 for (;;)
332 {
333 t=(h-q*dh);
334 if ((t&BN_MASK2h) ||
335 ((dl*q) <= (
336 (t<<BN_BITS4)+
337 ((l&BN_MASK2h)>>BN_BITS4))))
338 break;
339 q--;
340 }
341 th=q*dh;
342 tl=q*dl;
343 t=(tl>>BN_BITS4);
344 tl=(tl<<BN_BITS4)&BN_MASK2h;
345 th+=t;
346
347 if (l < tl) th++;
348 l-=tl;
349 if (h < th)
350 {
351 h+=d;
352 q--;
353 }
354 h-=th;
355
356 if (--count == 0) break;
357
358 ret=q<<BN_BITS4;
359 h=((h<<BN_BITS4)|(l>>BN_BITS4))&BN_MASK2;
360 l=(l&BN_MASK2l)<<BN_BITS4;
361 }
362 ret|=q;
363 return(ret);
364 }
365#endif
366
diff --git a/src/lib/libcrypto/bn/bn_sub.c b/src/lib/libcrypto/bn/bn_sub.c
new file mode 100644
index 0000000000..bba80f8afb
--- /dev/null
+++ b/src/lib/libcrypto/bn/bn_sub.c
@@ -0,0 +1,180 @@
1/* crypto/bn/bn_sub.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "bn_lcl.h"
62
63/* unsigned subtraction of b from a, a must be larger than b. */
64void bn_qsub(r, a, b)
65BIGNUM *r;
66BIGNUM *a;
67BIGNUM *b;
68 {
69 int max,min;
70 register BN_ULONG t1,t2,*ap,*bp,*rp;
71 int i,carry;
72#if defined(IRIX_CC_BUG) && !defined(LINT)
73 int dummy;
74#endif
75
76 max=a->top;
77 min=b->top;
78 ap=a->d;
79 bp=b->d;
80 rp=r->d;
81
82 carry=0;
83 for (i=0; i<min; i++)
84 {
85 t1= *(ap++);
86 t2= *(bp++);
87 if (carry)
88 {
89 carry=(t1 <= t2);
90 t1=(t1-t2-1)&BN_MASK2;
91 }
92 else
93 {
94 carry=(t1 < t2);
95 t1=(t1-t2)&BN_MASK2;
96 }
97#if defined(IRIX_CC_BUG) && !defined(LINT)
98 dummy=t1;
99#endif
100 *(rp++)=t1&BN_MASK2;
101 }
102 if (carry) /* subtracted */
103 {
104 while (i < max)
105 {
106 i++;
107 t1= *(ap++);
108 t2=(t1-1)&BN_MASK2;
109 *(rp++)=t2;
110 if (t1 > t2) break;
111 }
112 }
113#if 0
114 memcpy(rp,ap,sizeof(*rp)*(max-i));
115#else
116 for (; i<max; i++)
117 *(rp++)= *(ap++);
118#endif
119
120 r->top=max;
121 bn_fix_top(r);
122 }
123
124int BN_sub(r, a, b)
125BIGNUM *r;
126BIGNUM *a;
127BIGNUM *b;
128 {
129 int max,i;
130 int add=0,neg=0;
131 BIGNUM *tmp;
132
133 /* a - b a-b
134 * a - -b a+b
135 * -a - b -(a+b)
136 * -a - -b b-a
137 */
138 if (a->neg)
139 {
140 if (b->neg)
141 { tmp=a; a=b; b=tmp; }
142 else
143 { add=1; neg=1; }
144 }
145 else
146 {
147 if (b->neg) { add=1; neg=0; }
148 }
149
150 if (add)
151 {
152 /* As a fast max size, do a a->top | b->top */
153 i=(a->top | b->top)+1;
154 if (bn_wexpand(r,i) == NULL)
155 return(0);
156 if (i)
157 bn_qadd(r,a,b);
158 else
159 bn_qadd(r,b,a);
160 r->neg=neg;
161 return(1);
162 }
163
164 /* We are actually doing a - b :-) */
165
166 max=(a->top > b->top)?a->top:b->top;
167 if (bn_wexpand(r,max) == NULL) return(0);
168 if (BN_ucmp(a,b) < 0)
169 {
170 bn_qsub(r,b,a);
171 r->neg=1;
172 }
173 else
174 {
175 bn_qsub(r,a,b);
176 r->neg=0;
177 }
178 return(1);
179 }
180