xref: /illumos-gate/usr/src/common/mpi/mpprime.c (revision db8f0877c245a884664489a7d8d1e4d8a34a8432)
1 /*
2  *  mpprime.c
3  *
4  *  Utilities for finding and working with prime and pseudo-prime
5  *  integers
6  *
7  * ***** BEGIN LICENSE BLOCK *****
8  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
9  *
10  * The contents of this file are subject to the Mozilla Public License Version
11  * 1.1 (the "License"); you may not use this file except in compliance with
12  * the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS" basis,
16  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17  * for the specific language governing rights and limitations under the
18  * License.
19  *
20  * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
21  *
22  * The Initial Developer of the Original Code is
23  * Michael J. Fromberger.
24  * Portions created by the Initial Developer are Copyright (C) 1997
25  * the Initial Developer. All Rights Reserved.
26  *
27  * Contributor(s):
28  *   Netscape Communications Corporation
29  *
30  * Alternatively, the contents of this file may be used under the terms of
31  * either the GNU General Public License Version 2 or later (the "GPL"), or
32  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
33  * in which case the provisions of the GPL or the LGPL are applicable instead
34  * of those above. If you wish to allow use of your version of this file only
35  * under the terms of either the GPL or the LGPL, and not to allow others to
36  * use your version of this file under the terms of the MPL, indicate your
37  * decision by deleting the provisions above and replace them with the notice
38  * and other provisions required by the GPL or the LGPL. If you do not delete
39  * the provisions above, a recipient may use your version of this file under
40  * the terms of any one of the MPL, the GPL or the LGPL.
41  *
42  * ***** END LICENSE BLOCK ***** */
43 /*
44  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
45  * Use is subject to license terms.
46  *
47  * Sun elects to use this software under the MPL license.
48  */
49 
50 #pragma ident	"%Z%%M%	%I%	%E% SMI"
51 
52 #include "mpi-priv.h"
53 #include "mpprime.h"
54 #include "mplogic.h"
55 #ifndef _KERNEL
56 #include <stdlib.h>
57 #include <string.h>
58 #else
59 #include <sys/random.h>
60 #endif
61 
62 #define SMALL_TABLE 0 /* determines size of hard-wired prime table */
63 
64 #ifndef _KERNEL
65 #define RANDOM() rand()
66 #else
67 #define RANDOM() foo_rand()
68 
69 static int
70 foo_rand()
71 {
72 	int r;
73 	random_get_pseudo_bytes((uchar_t *)&r, sizeof (r));
74 	return (r);
75 }
76 #endif
77 
78 /*
79   mpp_random(a)
80 
81   Assigns a random value to a.  This value is generated using the
82   standard C library's rand() function, so it should not be used for
83   cryptographic purposes, but it should be fine for primality testing,
84   since all we really care about there is good statistical properties.
85 
86   As many digits as a currently has are filled with random digits.
87  */
88 
89 mp_err  mpp_random(mp_int *a)
90 
91 {
92   mp_digit  next = 0;
93   unsigned int       ix, jx;
94 
95   ARGCHK(a != NULL, MP_BADARG);
96 
97   for(ix = 0; ix < USED(a); ix++) {
98     for(jx = 0; jx < sizeof(mp_digit); jx++) {
99       next = (next << CHAR_BIT) | (RANDOM() & UCHAR_MAX);
100     }
101     DIGIT(a, ix) = next;
102   }
103 
104   return MP_OKAY;
105 
106 } /* end mpp_random() */
107 
108 /* }}} */
109 
110 /* {{{ mpp_random_size(a, prec) */
111 
112 mp_err  mpp_random_size(mp_int *a, mp_size prec)
113 {
114   mp_err   res;
115 
116   ARGCHK(a != NULL && prec > 0, MP_BADARG);
117 
118   if((res = s_mp_pad(a, prec)) != MP_OKAY)
119     return res;
120 
121   return mpp_random(a);
122 
123 } /* end mpp_random_size() */
124