summaryrefslogtreecommitdiff
path: root/usr/src/lib/libresolv2/common/cylink/math.c
blob: 4c7b0e6a5eed2f5264a4cfb8921858470f57df4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
/*
 * Copyright (c) 1999 by Sun Microsystems, Inc.
 * All rights reserved.
 */

/*
 * Cylink Corporation © 1998
 * 
 * This software is licensed by Cylink to the Internet Software Consortium to
 * promote implementation of royalty free public key cryptography within IETF
 * standards.  Cylink wishes to expressly thank the contributions of Dr.
 * Martin Hellman, Whitfield Diffie, Ralph Merkle and Stanford University for
 * their contributions to Internet Security.  In accordance with the terms of
 * this license, ISC is authorized to distribute and sublicense this software
 * for the practice of IETF standards.  
 *
 * The software includes BigNum, written by Colin Plumb and licensed by Philip
 * R. Zimmermann for royalty free use and distribution with Cylink's
 * software.  Use of BigNum as a stand alone product or component is
 * specifically prohibited.
 *
 * Disclaimer of All Warranties. THIS SOFTWARE IS BEING PROVIDED "AS IS",
 * WITHOUT ANY EXPRESSED OR IMPLIED WARRANTY OF ANY KIND WHATSOEVER. IN
 * PARTICULAR, WITHOUT LIMITATION ON THE GENERALITY OF THE FOREGOING, CYLINK
 * MAKES NO REPRESENTATION OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
 * PURPOSE.
 *
 * Cylink or its representatives shall not be liable for tort, indirect,
 * special or consequential damages such as loss of profits or loss of
 * goodwill from the use or inability to use the software for any purpose or
 * for any reason whatsoever.
 *
 * EXPORT LAW: Export of the Foundations Suite may be subject to compliance
 * with the rules and regulations promulgated from time to time by the Bureau
 * of Export Administration, United States Department of Commerce, which
 * restrict the export and re-export of certain products and technical data.
 * If the export of the Foundations Suite is controlled under such rules and
 * regulations, then the Foundations Suite shall not be exported or
 * re-exported, directly or indirectly, (a) without all export or re-export
 * licenses and governmental approvals required by any applicable laws, or (b)
 * in violation of any applicable prohibition against the export or re-export
 * of any part of the Foundations Suite. All export licenses for software
 * containing the Foundations Suite are the sole responsibility of the licensee.
 */
 
/****************************************************************************
*  FILENAME: math.c   PRODUCT NAME: CRYPTOGRAPHIC TOOLKIT
*
*  FILE STATUS:
*
*  DESCRIPTION: Math Routines for the ToolKit 
*
*  PUBLIC FUNCTIONS:
*
*         int Sum_big (ord *X,
*                      ord *Y,
*                      ord *Z,
*                      u_int16_t len_X )
*
*         int Sub_big (ord *X,
*                      ord *Y,
*                      ord *Z,
*                      u_int16_t len_X )
*
*         void  Mul_big( ord *X, ord *Y,ord *XY,
*                        u_int16_t lx, u_int16_t ly)
*
*
*  PRIVATE FUNCTIONS:
*
*  REVISION  HISTORY:
*
*  14 Oct 94   GKL     Initial release
*  26 Oct 94   GKL     (alignment for big endian support )
*
****************************************************************************/

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/****************************************************************************
*  INCLUDE FILES
****************************************************************************/
/* bn files */
#include "port_before.h"
#include "bn.h"
/* system files */
#ifdef VXD
#include <vtoolsc.h>
#else
#include <stdlib.h>
#include <string.h>
#endif
/* program files */
#include "cylink.h"
#include "ctk_endian.h"
#include "toolkit.h"
#include "port_after.h"

/****************************************************************************
*   NAME: void BigNumInit( void )
*
*
*  DESCRIPTION:  Initialize BigNum
*
*  INPUTS:
*     PARAMETERS:
*  OUTPUT:
*     PARAMETERS:
*
*     RETURN:
*
*
*  REVISION HISTORY:
*
*  29 Sep 96        Initial release
*
****************************************************************************/

void BigNumInit()
{
static int bignuminit = 0;
if(!bignuminit){
	bnInit();
	bignuminit = 1;
 }
}
/****************************************************************************
*   NAME: int Sum_big (ord *X,
*                      ord *Y,
*                      ord *Z,
*                      u_int16_t len_X )
*
*  DESCRIPTION:  Compute addition.
*
*  INPUTS:
*     PARAMETERS:
*           ord  *X        Pointer to first array
*           ord  *Y        Pointer to second array
*           int  len_X     Number of longs in X_l
*  OUTPUT:
*     PARAMETERS:
*           ord *Z         Pointer to result arrray
*
*     RETURN:
*            Carry bit
*
*  REVISION HISTORY:
*
*  24 Sep 94   KPZ     Initial release
*  14 Oct 94   GKL     Second version (big endian support)
*
****************************************************************************/

 int Sum_big (ord *X,
			  ord *Y,
			  ord *Z,
			  u_int16_t len_X )
{

struct BigNum src2,temp_bn;
ord *temp;
BigNumInit();

/*bnInit();
bnBegin(&src2);
bnBegin(&temp_bn);
*/
temp = (ord *) malloc((len_X*sizeof(ord)) + sizeof(ord));
temp_bn.size = len_X;
temp_bn.ptr = temp;
temp_bn.allocated = len_X + 1;

src2.ptr = Y;
src2.size = len_X;
src2.allocated = len_X;

memcpy(temp,X,len_X*sizeof(ord));
bnAdd(&temp_bn,&src2);
memcpy(Z,temp_bn.ptr,len_X*sizeof(ord));
/*bn package increments the size of dest by 1 if the carry bit is 1*/
free(temp);
if (temp_bn.size > len_X)
	return 1;
else
	return 0;
}

 int Sum (ord *X, ord *Y, u_int16_t len_X )
{

struct BigNum dest,src;
/*ord *temp;*/
BigNumInit();
#if 0
bnInit();
bnBegin(&src2);
bnBegin(&temp_bn);

temp = (ord *) malloc((len_X*sizeof(ord)) + sizeof(ord));
temp_bn.size = len_X;
temp_bn.ptr = temp;
temp_bn.allocated = len_X + 1;
#endif

dest.ptr = X;
dest.size = len_X-1;
dest.allocated = len_X;

src.ptr = Y;
src.size = len_X;
src.allocated = len_X;

/*memcpy(temp,X,len_X*sizeof(ord));*/
bnAdd(&dest,&src);
/*memcpy(Z,temp_bn.ptr,len_X*sizeof(ord));*/
/*bn package increments the size of dest by 1 if the carry bit is 1*/
/*free(temp);*/
if (dest.size > (u_int16_t)(len_X -1))
	return 1;
else
	return 0;
}


/****************************************************************************
*   NAME: int Sum_Q(ord *X,
*                      u_int16_t src,
*                      u_int16_t len_X )
*  DESCRIPTION:  Compute addition X += src.
*
*  INPUTS:
*     PARAMETERS:
*           ord  *X        Pointer to first array
*           u_int16_t  src    Second operand must be <65535
*           int  len_X     Number of ords in X_l
*  OUTPUT:
*     PARAMETERS:
*           ord *X         Pointer to result arrray
*
*     RETURN:
*            SUCCESS or -1
*
*  REVISION HISTORY:
*
*  21 Sep 96   AAB     Initial release
****************************************************************************/
 int Sum_Q(ord *X, u_int16_t src, u_int16_t len_X )
 {
  int status = SUCCESS;
  struct BigNum des;
  BigNumInit();
  /*bnInit();*/
  des.ptr = X;
  des.size = len_X;
  des.allocated = len_X;
  status = bnAddQ(&des, src);
  return status;
 }


/****************************************************************************
*  NAME:  int Sub_big (ord *X,
*                      ord *Y,
*                      ord *Z,
*                      u_int16_t len_X )
*
*
*  DESCRIPTION:  Compute subtraction.
*
*  INPUTS:
*     PARAMETERS:
*           ord   *X        Pointer to first array
*           ord   *Y        Pointer to second array
*           u_int16_t   len_X     Number of longs in X_l
*  OUTPUT:
*     PARAMETERS:
*           ord  *Z         Pointer to result arrray
*
*     RETURN:
*            Carry bit
*
*  REVISION HISTORY:
*
*  24 Sep 94   KPZ     Initial release
*  14 Oct 94   GKL     Second version (big endian support)
*
****************************************************************************/

int Sub_big  (ord *X,
			  ord *Y,
			  ord *Z,
			  u_int16_t len_X )
{
/* carry is not returned in bn version */
struct BigNum dest, src;
int status;
ord *temp;
BigNumInit();
/*bnInit();
bnBegin(&dest);
bnBegin(&src);
*/
src.ptr = Y;
src.size = len_X;
src.allocated = len_X;

temp = (ord*)malloc(len_X*sizeof(ord));
dest.ptr = temp;
dest.size = len_X;
dest.allocated = len_X;
memcpy(dest.ptr,X,len_X*sizeof(ord));

status = bnSub(&dest,&src);
memcpy(Z,dest.ptr,len_X*sizeof(ord));
free(temp);
return status;
}

#if 0
/****************************************************************************
*  NAME:   void  Mul_big( ord  *X, ord *Y, ord *XY,
*                         u_int16_t lx, u_int16_t ly)
*
*
*
*  DESCRIPTION:  Compute a product.
*
*  INPUTS:
*     PARAMETERS:
*            ord  *X                 Pointer to first long array
*            ord  *Y                 Pointer to second long array
*            u_int16_t lx               Leftmost non zero element of first array
*            u_int16_t ly               Leftmost non zero element of second array
*  OUTPUT:
*     PARAMETERS:
*            ord  *XY              Pointer to result
*
*     RETURN:
*
*
*  REVISION HISTORY:
*
*  24 Sep 94   KPZ     Initial release
*  14 Oct 94   GKL     Second version (big endian support)
*  08 Sep 95   AAB     Comment out calloc and discard the elements_in_X,
*                  elements_in_Y
****************************************************************************/
void  Mul_big( ord  *X, ord *Y, ord *XY,
						 u_int16_t lx, u_int16_t ly )
{
struct BigNum dest, src1, src2;
BigNumInit();
/*bnInit();*/
bnBegin(&dest);
/*
bnBegin(&src1);
bnBegin(&src2);
*/
src1.size = lx + 1;
src1.ptr = X;
src1.allocated = lx + 1;

src2.ptr = Y;
src2.size = ly + 1;
src2.allocated = ly + 1;

dest.ptr = XY;
dest.size = lx + ly + 2;
dest.allocated = lx + ly + 2;

/* Call bn routine */
bnMul(&dest, &src1,&src2);
}

#endif
/****************************************************************************
*  NAME:   void  Mul_big_1( ord  X, ord *Y, ord *XY,
*                                 u_int16_t lx, u_int16_t ly )
*
*
*
*  DESCRIPTION:  Compute a product.
*
*  INPUTS:
*     PARAMETERS:
*            ord  X                  Number
*            ord  *Y                 Pointer to long array
*            u_int16_t ly               Leftmost non zero element of second array
*  OUTPUT:
*     PARAMETERS:
*            ord  *XY              Pointer to result
*
*     RETURN:
*
*
*  REVISION HISTORY:
*
*  08 Oct 95   AAB     Initial relaese
*
****************************************************************************/
void  Mul_big_1( ord  X, ord *Y, ord *XY,
				u_int16_t ly )
{
struct BigNum dest, src;
BigNumInit();
/*bnInit();
bnBegin(&dest);
bnBegin(&src);
*/
src.ptr = Y;
src.size = ly + 1;
src.allocated = ly + 1;

dest.ptr = XY;
dest.size = ly + 2;
dest.allocated = ly + 2;

bnMulQ(&dest, &src, (unsigned)X);

}

/****************************************************************************
*  NAME: int Mul( u_int16_t X_bytes,
*                 ord        *X,
*                 u_int16_t Y_bytes,
*                 ord       *Y,
*                 u_int16_t P_bytes,
*                 ord   *P,
*                 ord   *Z )
*
*  DESCRIPTION:  Compute a modulo product
*
*  INPUTS:
*      PARAMETERS:
*            ord   *X           Pointer to first operand
*            u_int16_t X_bytes     Number of bytes in X
*            ord   *Y           Pointer to second operand
*            u_int16_t Y_bytes     Number of bytes in Y
*            ord   *P           Pointer to modulo
*            u_int16_t P_bytes     Number of bytes in P
*
*  OUTPUT:
*      PARAMETERS:
*            ord   *Z           Pointer to result
*
*      RETURN:
*          SUCCESS              No errors
*          ERR_INPUT_LEN        Invalid length for input data (zero bytes)
*
*  REVISION HISTORY:
*
*  24 Sep 94   KPZ     Initial release
*  14 Oct 94   GKL     Second version (big endian support)
*
****************************************************************************/

int Mul( u_int16_t X_bytes,
			ord *X,
		 u_int16_t Y_bytes,
			ord *Y,
		 u_int16_t P_bytes,
		 ord *P,
		 ord *Z )

{
	int status = SUCCESS;       /*function return status*/
	u_int16_t X_longs;             /*number of longs in X*/
	u_int16_t Y_longs;             /*number of longs in Y*/
	ord *XY;                    /*pointer to product (temporary)*/


struct BigNum dest, src1,src2, mod;
BigNumInit();
/*bnInit();
bnBegin(&dest);
bnBegin(&src1);
bnBegin(&src2);
bnBegin(&mod);
*/

src1.size = X_bytes/sizeof(ord);
src1.ptr = X;
src1.allocated = X_bytes/sizeof(ord);

src2.size = Y_bytes/sizeof(ord);
src2.ptr = Y;
src2.allocated =Y_bytes/sizeof(ord);

mod.size = P_bytes/sizeof(ord);
mod.ptr = P;
mod.allocated = P_bytes/sizeof(ord);

	 if ( P_bytes == 0 || X_bytes == 0 || Y_bytes == 0 )
	{
		 status = ERR_INPUT_LEN;
		return status;
	}
	if ( (X_bytes % sizeof(ord) != 0) ||
		  (Y_bytes % sizeof(ord) != 0) ||
		 (P_bytes % sizeof(ord) != 0) )
	{
		 status = ERR_INPUT_LEN;
		return status;
	}
	X_longs = (u_int16_t) (X_bytes / sizeof(ord));
	Y_longs = (u_int16_t) (Y_bytes / sizeof(ord));
	XY = (ord *)calloc( X_longs +  Y_longs, sizeof(ord) );
	if( !XY  )
	{
		return ERR_ALLOC;
	}
dest.size = X_longs + Y_longs;
dest.ptr = XY;
dest.allocated = X_longs + Y_longs;

bnMul (&dest,&src1,&src2);

status = bnMod(&dest, &dest, &mod);
memcpy(Z, dest.ptr, P_bytes);
free( XY );
	return status;
}

/****************************************************************************
*  NAME: int Square( u_int16_t X_bytes,
*                         ord    *X,
*                     u_int16_t P_bytes,
*                        ord    *P,
*                     ord   *Z )
*
*  DESCRIPTION:  Compute a modulo square
*
*  INPUTS:
*      PARAMETERS:
*            ord   *X           Pointer to array to be squared
*            u_int16_t X_bytes     Number of bytes in X
*            ord   *P           Pointer to modulo
*            u_int16_t P_bytes     Number of bytes in P
*
*  OUTPUT:
*      PARAMETERS:
*            ord   *Z           Pointer to result
*
*      RETURN:
*          SUCCESS              No errors
*          ERR_INPUT_LEN        Invalid length for input data (zero bytes)
*
*  REVISION HISTORY:
*
*  1  Sep 95   AAB     Initial release
****************************************************************************/

int Square( u_int16_t X_bytes,
				ord *X,
				u_int16_t P_bytes,
				ord *P,
				ord *Z )

{
	 int status = SUCCESS;       /*function return status*/

ord *XY;
struct BigNum dest, src, mod;
BigNumInit();
/*bnInit();
bnBegin(&dest);
bnBegin(&src);
bnBegin(&mod);
*/
	if ( P_bytes == 0 || X_bytes == 0 )
	{
		 status = ERR_INPUT_LEN;
		return status;
	 }
	if ( (X_bytes % sizeof(ord) != 0) ||
		  (P_bytes % sizeof(ord) != 0) )
	 {
		 status = ERR_INPUT_LEN;
		return status;
	 }
	XY = (ord *)malloc( 2*X_bytes );
	 if( !XY )
	 {
		 return ERR_ALLOC;
	 }

src.size = X_bytes/sizeof(ord);
src.ptr = X;
src.allocated = X_bytes/sizeof(ord);

dest.size = 2*X_bytes/sizeof(ord);
dest.ptr = XY;
dest.allocated = 2*X_bytes/sizeof(ord);

mod.size = P_bytes/sizeof(ord);
mod.ptr = P;
mod.allocated = P_bytes/sizeof(ord);

status = bnSquare(&dest, &src);
status = bnMod(&dest, &dest, &mod);
memcpy(Z, dest.ptr, P_bytes);
free(XY);
return status;
}


/****************************************************************************
*  NAME: int PartReduct( u_int16_t X_bytes,
*                        ord  *X,
*                        u_int16_t P_bytes,
*                        ord  *P,
*                        ord *Z )
*
*  DESCRIPTION:  Compute a modulo
*
*  INPUTS:
*      PARAMETERS:
*            ord   *X              Pointer to array
*            u_int16_t X_bytes        Number of bytes in X
*            ord   *P              Pointer to modulo
*            u_int16_t P_bytes        Number of bytes in P
*
*  OUTPUT:
*      PARAMETERS:
*            ord   *Z              Pointer to result
*
*      RETURN:
*          SUCCESS             No errors
*          ERR_INPUT_LEN       Invalid length for input data (zero bytes)
*  REVISION HISTORY:
*
*  24 Sep 94   KPZ     Initial release
*  14 Oct 94   GKL     Second version (big endian support)
*
****************************************************************************/

int PartReduct( u_int16_t X_bytes,
	  ord *X,
	  u_int16_t P_bytes,
	  ord   *P,
	  ord   *Z )
{
	 int status = SUCCESS;       /*function return status */


struct BigNum dest, /*src,*/ d;
ord *temp;
BigNumInit();
/*bnInit();
bnBegin(&dest);
bnBegin(&src);
bnBegin(&d);

src.size = X_bytes/sizeof(ord);
src.ptr = X;
src.allocated = X_bytes/sizeof(ord);
*/
d.size = P_bytes/sizeof(ord);
d.ptr = P;
d.allocated = P_bytes/sizeof(ord);

temp = (ord*)malloc(X_bytes);
dest.size = X_bytes/sizeof(ord);
dest.ptr = temp;
dest.allocated = X_bytes/sizeof(ord);
memcpy(dest.ptr, X, X_bytes);

status = bnMod(&dest, &dest, &d);

memcpy(Z, dest.ptr, P_bytes);
free(temp);

return status;

}

/****************************************************************************
*  NAME: int Expo( u_int16_t X_bytes,
*                  ord    *X,
*                  u_int16_t Y_bytes,
*                  ord    *Y,
*                  u_int16_t P_bytes,
*                  ord    *P,
*                  ord    *Z,
*                  YIELD_context *yield_cont )
*
*  DESCRIPTION:  Compute a modulo exponent
*
*  INPUTS:
*      PARAMETERS:
*            ord   *X           Pointer to base array
*            u_int16_t X_bytes     Number of bytes in base
*            ord   *Y           Pointer to exponent array
*            u_int16_t Y_bytes     Number of bytes in exponent
*            ord   *P           Pointer to modulo
*            u_int16_t P_bytes     Number of bytes in  P
*            YIELD_context *yield_cont  Pointer to yield_cont structure (NULL if not used)
*
*  OUTPUT:
*      PARAMETERS:
*            ord   *Z            Pointer to result
*
*  RETURN:
*          SUCCESS               No errors
*          ERR_INPUT_LEN         Invalid length for input data(zero bytes)
*
*  REVISION HISTORY:
*
*  24 Sep 94   KPZ     Initial release
*  14 Oct 94   GKL     Second version (big endian support)
*  08 Dec 94   GKL     Added YIELD_context
*  01 Sep 95           Fast exponentation algorithm
****************************************************************************/

int Expo( u_int16_t X_bytes, ord    *X,
		 u_int16_t Y_bytes, ord    *Y,
		 u_int16_t P_bytes, ord    *P,
		 ord   *Z )
{

int status = SUCCESS;     /*function return status*/

struct BigNum dest, n, exp, mod;
BigNumInit();
#if 0
/*bnInit();*/
bnBegin(&dest);
bnBegin(&n);
bnBegin(&exp);
bnBegin(&mod);
#endif

n.size = X_bytes/sizeof(ord);
n.ptr = X;
n.allocated = X_bytes/sizeof(ord);

exp.ptr = Y;
exp.size = Y_bytes/sizeof(ord);
exp.allocated = Y_bytes/sizeof(ord);

mod.ptr = P;
mod.size = P_bytes/sizeof(ord);
mod.allocated = P_bytes/sizeof(ord);

dest.ptr = Z;
dest.size = P_bytes/sizeof(ord);
dest.allocated = P_bytes/sizeof(ord);

/* Call bn routine */

status = bnExpMod(&dest, &n,
				  &exp, &mod);

return status;
}


/****************************************************************************
*  NAME: int DoubleExpo( u_int16_t X1_bytes,
*                  ord    *X1,
*                  u_int16_t Y1_bytes,
*                  ord    *Y1,
*  					 u_int16_t X2_bytes,
*                  ord    *X2,
*                  u_int16_t Y2_bytes,
*                  ord    *Y2,
*                  u_int16_t P_bytes,
*                  ord    *P,
*                  ord    *Z)
*
*  DESCRIPTION:  Compute a modulo exponent
*
*  INPUTS:
*      PARAMETERS:
*            ord   *X1           Pointer to first base array
*            u_int16_t X1_bytes     Number of bytes in first base
*            ord   *Y1           Pointer to first exponent array
*            u_int16_t Y1_bytes     Number of bytes in first exponent
*            ord   *X2           Pointer to second base array
*            u_int16_t X2_bytes     Number of bytes in second base
*            ord   *Y2           Pointer to second exponent array
*            u_int16_t Y2_bytes     Number of bytes in second exponent            ord   *P           Pointer to modulo
*            ord   *P           Pointer to modulo
*            u_int16_t P_bytes     Number of bytes in
*
*  OUTPUT:
*      PARAMETERS:
*            ord   *Z            Pointer to result
*
*  RETURN:
*          SUCCESS               No errors
*          ERR_INPUT_LEN         Invalid length for input data(zero bytes)
*
*  REVISION HISTORY:
*
*  21 Aug 96   AAB     Initial release
****************************************************************************/


int DoubleExpo( u_int16_t X1_bytes,ord    *X1,
					 u_int16_t Y1_bytes,ord    *Y1,
					 u_int16_t X2_bytes,ord    *X2,
					 u_int16_t Y2_bytes,ord    *Y2,
					 u_int16_t P_bytes,ord    *P,
										 ord    *Z)
{
int status = SUCCESS;     /*function return status*/
struct BigNum res, n1, e1, n2, e2, mod;
BigNumInit();

n1.size = X1_bytes/sizeof(ord);
n1.ptr = X1;
n1.allocated = X1_bytes/sizeof(ord);

e1.size = Y1_bytes/sizeof(ord);
e1.ptr = Y1;
e1.allocated = Y1_bytes/sizeof(ord);

n2.size = X2_bytes/sizeof(ord);
n2.ptr = X2;
n2.allocated = X2_bytes/sizeof(ord);

e2.size = Y2_bytes/sizeof(ord);
e2.ptr = Y2;
e2.allocated = Y2_bytes/sizeof(ord);

mod.ptr = P;
mod.size = P_bytes/sizeof(ord);
mod.allocated = P_bytes/sizeof(ord);

res.ptr = Z;
res.size = P_bytes/sizeof(ord);
res.allocated = P_bytes/sizeof(ord);
status = bnDoubleExpMod(&res, &n1, &e1, &n2, &e2, &mod);
return status;
}

/****************************************************************************
*  NAME: int Inverse( u_int16_t X_bytes,
*                     ord    *X,
*                     u_int16_t P_bytes,
*                     ord    *P,
*                     ord    *Z )
*
*
*
*
*  DESCRIPTION:  Compute a modulo inverse element
*
*  INPUTS:
*      PARAMETERS:
*            ord   *X           Pointer to array
*            u_int16_t X_bytes     Number of bytes in array
*            ord   *P           Pointer to modulo
*            u_int16_t P_bytes     Number of bytes in  P
*
*  OUTPUT:
*      PARAMETERS:
*            ord   *Z           Pointer to result
*
*      RETURN:
*          SUCCESS              No errors
*          ERR_INPUT_LEN        Invalid length for input data(zero bytes)
*          ERR_INPUT_VALUE  Invalid input value
*
*  REVISION HISTORY:
*
*  24 Sep 94   KPZ     Initial release
*  14 Oct 94   GKL     Second version (big endian support)
*  08 Nov 94   GKL     Added input parameters check
*  01 Sep 95           Improve fuction
****************************************************************************/

int Inverse( u_int16_t X_bytes,
			  ord    *X,
			  u_int16_t P_bytes,
			 ord    *P,
				ord    *Z )
{
int status = SUCCESS;   /* function return status */

struct BigNum dest, src, mod;
BigNumInit();
/*bnInit();
bnBegin(&dest);
bnBegin(&src);
bnBegin(&mod);
*/
src.size = X_bytes/sizeof(ord);
src.ptr = X;
src.allocated = X_bytes/sizeof(ord);

mod.ptr = P;
mod.size = P_bytes/sizeof(ord);
mod.allocated = P_bytes/sizeof(ord);

dest.ptr = Z;
dest.size = (P_bytes/sizeof(ord))  ;
dest.allocated = (P_bytes/sizeof(ord)) + 1;
status = bnInv(&dest,&src,&mod);
return status;
}


/****************************************************************************
*  NAME:     void Add( ord    *X,
*                      ord    *Y,
*                      u_int16_t P_len,
*                      ord    *P,
*                      ord    *Z )

*
*  DESCRIPTION:  Compute modulo addition
*
*  INPUTS:
*          PARAMETERS:
*                         ord   *X              Pointer to first operand
*                         ord   *Y              Pointer to second operand
*                         u_int16_t P_len  Length of modulo
*                         ord   *P              Pointer to modulo
*  OUTPUT:
*             ord   *Z          Pointer to result
*          RETURN:
*
*  REVISION HISTORY:
*
*  24 sep 94    KPZ             Initial release
*  10 Oct 94    KPZ     Fixed bugs
*  14 Oct 94    GKL     Second version (big endian support)
*
****************************************************************************/
 /*
 int Add( ord *X,
		  ord *Y,
	  u_int16_t P_len,
		  ord *P,
			ord *Z )
{
	int status = SUCCESS;
	ord *temp;
	struct BigNum dest, src, mod;

bnInit();
bnBegin(&dest);
bnBegin(&src);
bnBegin(&mod);

temp = (ord*)malloc(P_len + sizeof(ord));
memcpy(temp, X, P_len);

dest.size = P_len/sizeof(ord);
dest.ptr = temp;
dest.allocated = P_len/sizeof(ord) + 1;

src.ptr = Y;
src.size = P_len/sizeof(ord);
src.allocated = P_len/sizeof(ord);

mod.ptr = P;
mod.size = P_len/sizeof(ord);
mod.allocated = P_len/sizeof(ord);

status = bnAdd(&dest,&src);
status = bnMod(&dest,&dest,&mod);
memcpy(Z,temp,P_len);
free(temp);
return status;
}
 */
 int Add( ord *X,
		  ord *Y,
	  u_int16_t P_len,
		  ord *P)
{
	int status = SUCCESS;
/*	ord *temp;*/
	struct BigNum dest, src, mod;

BigNumInit();
/*bnInit();
bnBegin(&dest);
bnBegin(&src);
bnBegin(&mod);
*/
/*
temp = (ord*)malloc(P_len + sizeof(ord));
memcpy(temp, X, P_len);
*/
dest.size = P_len/sizeof(ord);
/*dest.ptr = temp;*/
dest.ptr = X;
dest.allocated = P_len/sizeof(ord) + 1;

src.ptr = Y;
src.size = P_len/sizeof(ord);
src.allocated = P_len/sizeof(ord);

mod.ptr = P;
mod.size = P_len/sizeof(ord);
mod.allocated = P_len/sizeof(ord);

status = bnAdd(&dest,&src);
status = bnMod(&dest,&dest,&mod);
/*
memcpy(Z,temp,P_len);
free(temp);
*/
return status;
}




/****************************************************************************
*  NAME:     int SteinGCD( ord *m,
*                          ord *b
*                          u_int16_t len )
*
*  DESCRIPTION:  Compute great common divisor
*
*  INPUTS:
*          PARAMETERS:
*           ord *m           Pointer to first number
*           ord *b           Pointer to second number
*           u_int16_t len       Number of elements in number
*  OUTPUT:
*
*  RETURN:
*           TRUE                   if gcd != 1
*           FALSE                                  if gcd == 1
*  REVISION HISTORY:
*
*
*  24 Sep 94    KPZ     Initial release
*  14 Oct 94    GKL     Second version (big endian support)
*  01 Sep 95    AAB     Speed up
*
****************************************************************************/


/* test if GCD equal 1 */
int  SteinGCD ( ord  *m,
		  ord  *n,
				u_int16_t len )
{

int status;
struct BigNum dest, a, b;
ord *temp;
BigNumInit();
/*bnInit();
bnBegin(&dest);
bnBegin(&a);
bnBegin(&b);
*/
a.size = len;
a.ptr = m;
a.allocated = len;

b.size = len;
b.ptr = n;
b.allocated = len;

temp = (ord*)malloc((len+1)*sizeof(ord));
dest.size = len;
dest.ptr = temp;
dest.allocated = len+1;

status = bnGcd(&dest, &a, &b);

if (*(ord *)(dest.ptr) == 0x01 && dest.size == 1)
 status = 0;
else
 status = 1;

free(temp);

return status;

}


/****************************************************************************
*  NAME: int DivRem( u_int16_t X_bytes,
*                    ord    *X,
*                    u_int16_t P_bytes,
*                    ord    *P,
*                    ord    *Z,
*                    ord    *D)
*
*  DESCRIPTION:  Compute a modulo and quotient
*
*  INPUTS:
*          PARAMETERS:
*                    ord   *X              Pointer to array
*                    u_int16_t X_bytes        Number of bytes in X
*                    ord   *P              Pointer to modulo
*                    u_int16_t P_bytes        Number of bytes in  P
*
*  OUTPUT:
*          PARAMETERS:
*            ord   *Z              Pointer to result
*            ord   *D                      Pointer to quotient
*          RETURN:
*                  SUCCESS             No errors
*          ERR_INPUT_LEN       Invalid length for input data (zero bytes)
*  REVISION HISTORY:
*
*  24 Sep 94   KPZ              Initial release
*  10 Oct 94   KPZ      Fixed bugs
*  14 Oct 94   GKL      Second version (big endian support)
*
****************************************************************************/

int DivRem( u_int16_t X_bytes,
		 ord    *X,
	  u_int16_t P_bytes,
		 ord    *P,
	  ord    *Z,
	  ord    *D)
{
	int status = SUCCESS;       /* function return status */

struct BigNum q, r, /*n,*/ d;
ord *temp;
BigNumInit();
/*bnInit();
bnBegin(&q);
bnBegin(&r);
bnBegin(&n);
bnBegin(&d);

n.size = X_bytes/sizeof(ord);
n.ptr = X;
n.allocated = X_bytes/sizeof(ord);
*/
d.size = P_bytes/sizeof(ord);
d.ptr = P;
d.allocated = P_bytes/sizeof(ord);

q.size = (X_bytes/sizeof(ord)) - (P_bytes/sizeof(ord)) + 1;
q.ptr = D;
q.allocated = (X_bytes/sizeof(ord)) - (P_bytes/sizeof(ord)) + 1;

temp = (ord *)malloc(X_bytes);
r.size = X_bytes/sizeof(ord);
r.ptr = temp;
r.allocated = X_bytes/sizeof(ord);
memcpy(r.ptr, X, X_bytes);

status = bnDivMod(&q, &r, &r, &d);

memcpy(Z, r.ptr, P_bytes);
free(temp);

return status;

}