summaryrefslogtreecommitdiff
path: root/usr/src/lib/libresolv2/common/cylink/ctk_prime.c
blob: 45b576eb3fb35bac7ca84722789320abe7bf91fd (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
/*
 * 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:  prime.c   PRODUCT NAME: CRYPTOGRAPHIC TOOLKIT
*
*  FILE STATUS:
*
*  DESCRIPTION:  Cryptographic Toolkit Internal Functions File
*               Prime Number functions
*  PRIVATE FUNCTIONS:
*
*               int VerPrime( u_int16_t P_bytes, ord *P,
*                             u_int16_t k, ord *RVAL,
*                             YIELD_context *yield_cont )
*               int GenPrime( u_int16_t P_bytes, ord *P,
*                             u_int16_t k, ord *RVAL,
*                             YIELD_context *yield_cont )
*       Copyright (c) Cylink Corporation 1996. All rights reserved.
*
*  REVISION  HISTORY:
*
*
****************************************************************************/

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

/****************************************************************************
*  INCLUDE FILES
****************************************************************************/

#include "port_before.h"
#include <sys/types.h>
#include "cylink.h"
#include "ctk_endian.h"

/* system files */
#ifdef VXD
#include <vtoolsc.h>
#else
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#endif

/* program files */
#include "cylink.h"
#include "ctk_endian.h"
#include "toolkit.h"
#include "cencrint.h"
#include "port_after.h"

/****************************************************************************
*  PRIVATE FUNCTIONS DEFINITIONS
****************************************************************************/
/****************************************************************************
*  NAME: int VerPrime( u_int16_t P_bytes,
*                      ord    *P,
*                      u_int16_t k,
*                      ord    *RVAL,
*                                         YIELD_context *yield_cont )
*
*  DESCRIPTION: Verify Pseudo Prime number
*
*  INPUTS:
*      PARAMETERS:
*            u_int16_t P_bytes     Number of bytes in array
*            u_int16_t k           Number of testing
*            ord    *RVAL       Pointer to random number generator value
*            YIELD_context *yield_cont  Pointer to yield_cont structure (NULL if not used)
*  OUTPUT:
*      PARAMETERS:
*            ord   *P           Pointer to prime number
*            ord   *RVAL        Pointer to updated value
*
*      RETURN:
*          SUCCESS              No errors
*          ERR_INPUT_LEN        Invalid length for input data(zero bytes)
*          ERR_PRIME            Number is not prime
*  REVISION HISTORY:
*
*  24 Sep 94   KPZ     Initial release
*  14 Oct 94   GKL     Second version (big endian support)
*  08 Dec 94   GKL    Added YIELD_context
*
****************************************************************************/

 int VerPrime( u_int16_t P_bytes,
						 ord    *P,
					  u_int16_t k,
					   ord    *RVAL,
						   YIELD_context *yield_cont ) /*TKL00601*/

{
	int status = SUCCESS;      /*function return status*/
   ord *b, *n, *c, *pp;       /*pointers to working buffers */
	 ord *m, *buf, *P_buf;
   u_int16_t max_bytes;          /*number of maximum bytes*/
  u_int16_t P_longs;            /*number of longs in Y*/
	 u_int16_t i, j, s, k_b;       /*counters*/
 u_int16_t exit ;              /*flag for exit*/
/* product of prime numbers from 3 to 379 (512-bit number) */
#ifdef ORD_32
	ord b1[DSS_LENGTH_MIN/sizeof(ord)]={
		  0x2e30c48fL, 0x0decece9L, 0xbada8f98L, 0x9f7ecb29L,
             0xa4a11de4L, 0x6ef04659L, 0xcbc38405L, 0x233db117L,
             0x03e81187L, 0xc1b631a2L, 0x238bfb99L, 0x077ec3baL,
             0xc5d5f09fL, 0xb0813c28L, 0x7646fa6eL, 0x106aa9fbL };
#else
	ord b1[DSS_LENGTH_MIN/sizeof(ord)]={
              0xc48f, 0x2e30, 0xece9, 0x0dec, 0x8f98, 0xbada, 0xcb29, 0x9f7e,
         0x1de4, 0xa4a1, 0x4659, 0x6ef0, 0x8405, 0xcbc3, 0xb117, 0x233d,
         0x1187, 0x03e8, 0x31a2, 0xc1b6, 0xfb99, 0x238b, 0xc3ba, 0x077e,
		 0xf09f, 0xc5d5, 0x3c28, 0xb081, 0xfa6e, 0x7646, 0xa9fb, 0x106a };
#endif
	if ( P_bytes % sizeof(ord) != 0 )                /* not multiple 4 (32 bit) */
    {
          status = ERR_INPUT_LEN;          /* invalid length for input data */
			return status;
    }
	if ( P_bytes <= DSS_LENGTH_MIN )
    {
			max_bytes = DSS_LENGTH_MIN;
	}
	else
    {
		   max_bytes = P_bytes;
    }
	buf = (ord *)calloc( max_bytes / sizeof(ord), sizeof(ord) );
	P_buf = (ord *)calloc( max_bytes / sizeof(ord), sizeof(ord));
	if( !buf || !P_buf )
    {
 if ( buf )
              free( buf );
    if( P_buf )
			 free( P_buf );
  return ERR_ALLOC;
	}
	memcpy( buf, b1, DSS_LENGTH_MIN );
	memcpy( P_buf, P, P_bytes  );

	if ( (P_buf[0] & 0x1) == 0 )
    {
#ifdef DEBUG1
         printf ("\n P is not pseudoprime");
#endif
			   status = ERR_PRIME;
             free ( buf );
		   free( P_buf );
          return status;
    }
	P_longs = (u_int16_t) (P_bytes / sizeof(ord));
	b = (ord *)calloc( P_longs, sizeof(ord) );
	m = (ord *)malloc( P_longs * sizeof(ord) );
	n = (ord *)calloc( P_longs, sizeof(ord) );
	c = (ord *)calloc( P_longs, sizeof(ord) );
	pp = (ord *)malloc( P_longs * sizeof(ord) );
	if( !b || !m || !n || !c || !pp )
    {
	  if( b )
         free( b );
      if( m )
         free( m );
      if( n )
         free( n );
      if( c )
         free( c );
      if( pp )
				free ( pp );
	}
	memcpy( m, P, P_bytes );
	memcpy( pp, P, P_bytes );
    /* Compute great common divisor(gcd) */
	if ( SteinGCD( P_buf, buf, (u_int16_t)(max_bytes / sizeof(ord)) ) == 0 )
  {
        pp[0] = pp[0] - 1;                     /* Initialized work buffer */
        m[0] = m[0] - 1;
           s = (u_int16_t) RShiftMostBit( m, (u_int16_t)(P_bytes / sizeof(ord)) ); /* Right shift by number of*/
        exit = 0;                               /* zero bits at rigth    */
		k_b = 0;
        while( k_b != k )
		{
                     MyGenRand( 4, b, RVAL );    /* generate random number */
            if ( SteinGCD( P_buf, b , (u_int16_t)(P_bytes / sizeof(ord)) ) ) /* check gcd */
            {
#ifdef DEBUG1
							 printf ("\n P is not pseudoprime");
#endif
							   status = ERR_PRIME;
                             break;
			}
            k_b++;                          /* increment counter */
		  if ( ( status = Expo ( 4, b, P_bytes, m,
                                P_bytes, P, c ) ) != SUCCESS )    /* c=b^m mod(P) */ /*TKL00601*/
                    {
                               free( b );
                              free( m );
                              free( n );
                              free( c );
                              free( pp );
                             free (buf );
							free( P_buf );
                          return status;
				  }
                       if ( c[0] == 1 )    /* if c==1 number is pseudo prime */
						{
                               for ( i = 1; i < P_bytes / sizeof(ord); i++ )
                {
                    if ( c[i] != 0 )
                    {
                                         break;
                                  }
							   }
                               if ( i == P_bytes / sizeof(ord) )
							   {
                                       if (yield_cont)  /*TKL00601*/
#ifdef VXD
                                         if ( VXD_Yield (yield_cont->yield_proc) )
#else
                                          if ( yield_cont->yield_proc(0xFFFF) )
#endif

											 {
													   status = ERR_CANCEL;
                                                    free( b );
													  free( m );
                                                      free( n );
                                                      free( c );
                                                      free( pp );
                                                     free( P_buf );
                                                  free (buf );
                                                    return status;
                                          }
#ifdef DEBUG1
								  printf ("\n P is a pseudoprime %d",k_b);
#endif
								  if ( k_b == k )
								 {
                                               break;
								  }
                               }
            }
                  else
            {
                for ( j = 1; j <= s; j++ )
				{
                    for ( i = 0; i < P_bytes / sizeof(ord); i++ )    /* if c==pp number is pseudo prime */
					{
                                             if ( c[i] != pp[i] )
                        {
                                           break;
                                          }
                                       }
                                       if ( i == P_bytes / sizeof(ord) )
                                       {
                                               if (yield_cont) /*TKL00601*/
#ifdef VXD
                                          if ( VXD_Yield (yield_cont->yield_proc) )
#else
                                                  if ( yield_cont->yield_proc(0xFFFF) )
#endif

                                                     {
                                                               status = ERR_CANCEL;
                                                            free( b );
                                                              free( m );
                                                              free( n );
															  free( c );
                                                              free( pp );
															 free( P_buf );
                                                          free (buf );
                                                            return status;
                                                  }
#ifdef DEBUG1
                                          printf ("\n P is a pseudoprime %d",k_b);
#endif
                                          break;
                                  }
									   if ( j == s )
                                   {
#ifdef DEBUG1
                                          printf ("\n P is not pseudoprime");
#endif
                                               status = ERR_PRIME;
                                             exit = 1;
                                               break;
                                  }
                                       else
                                    {
											   if ( (status = Square(P_bytes, c, /*P_bytes,
																					c,*/ P_bytes, P, c) )
																   != SUCCESS )     /* c=c^2mod(p) */
                                              {
                                                       free( b );
                                                      free( m );
                                                      free( n );
                                              free( c );
                                              free( pp );
                                             free( P_buf );
                                          free (buf );
											return status;
                                          }
									   }
                }
                      }
            if ( exit == 1 )            /* Exit */
            {
                                break;
                  }
               }
       }
	   else
    {
			   if (yield_cont)  /*TKL00601*/
#ifdef VXD
         if ( VXD_Yield (yield_cont->yield_proc) )
#else
          if ( yield_cont->yield_proc(0xFFFF) )
#endif

                     {
                               status = ERR_CANCEL;
							free( b );
                              free( m );
							  free( n );
                              free( c );
                              free( pp );
                             free( P_buf );
                          free (buf );
                            return status;
                  }
#ifdef DEBUG1
          printf ("\n P is not pseudoprime");
#endif
			   status = ERR_PRIME;
	 }
	   free( b );
	  free( m );
	  free( n );
	  free( c );
	  free( pp );
	 free( P_buf );
  free (buf );
	return status;
}

/****************************************************************************
*  NAME: int GenPrime( u_int16_t P_bytes,
*                      ord    *P,
*                      u_int16_t k,
*                      ord    *RVAL,
*                                           YIELD_context *yield_cont )
*
*  DESCRIPTION: Generate Random Pseudo Prime number
*
*  INPUTS:
*      PARAMETERS:
*            u_int16_t P_bytes     Number of bytes in array
*            u_int16_t k           Number of testing
*            ord    *RVAL       Pointer to random number generator value
*            YIELD_context *yield_cont  Pointer to yield_cont structure (NULL if not used)
*  OUTPUT:
*      PARAMETERS:
*            ord   *P           Pointer to prime number
*            ord   *RVAL        Pointer to updated value
*
*      RETURN:
*          SUCCESS              No errors
*          ERR_INPUT_LEN        Invalid length for input data(zero bytes)
*          ERR_PRIME            Number is not prime
*  REVISION HISTORY:
*
*  24 Sep 94   KPZ     Initial release
*  14 Oct 94   GKL     Second version (big endian support)
*  08 Dec 94   GKL     Added YIELD_context
*
****************************************************************************/

 int GenPrime( u_int16_t P_bytes,
					   ord    *P,
                      u_int16_t k,
                       ord    *RVAL,
                           YIELD_context *yield_cont ) /*TKL00601*/
{
    int status = SUCCESS;      /* function return status */
 if ( P_bytes % sizeof(ord) != 0 )    /* not multiple 4 (32 bit) */
      {
               status = ERR_INPUT_LEN;          /* invalid length for input data */
			return status;
  }
	   do
      {
               MyGenRand( P_bytes, P, RVAL );    /* generate random number */
          P[0] |= 1;
              P[(P_bytes/sizeof(ord))-1] |= ((ord)1 << (BITS_COUNT-1));
               status = VerPrime( P_bytes, P, k, RVAL, yield_cont); /*TKL00601*/
       } while ((status != SUCCESS) && (status != ERR_CANCEL)); /*TKL00601*/
   return status;
}