xref: /illumos-gate/usr/src/common/mpi/mpprime.c (revision c40a6cd785e883b3f052b122c332e21174fc1871)
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 #include "mpi-priv.h"
51 #include "mpprime.h"
52 #include "mplogic.h"
53 #ifndef _KERNEL
54 #include <stdlib.h>
55 #include <string.h>
56 #else
57 #include <sys/random.h>
58 #endif
59 
60 #define SMALL_TABLE 0 /* determines size of hard-wired prime table */
61 
62 #ifndef _KERNEL
63 #define RANDOM() rand()
64 #else
65 #define RANDOM() foo_rand()
66 
67 static int
foo_rand()68 foo_rand()
69 {
70 	int r;
71 	random_get_pseudo_bytes((uchar_t *)&r, sizeof (r));
72 	return (r);
73 }
74 #endif
75 
76 /*
77   mpp_random(a)
78 
79   Assigns a random value to a.  This value is generated using the
80   standard C library's rand() function, so it should not be used for
81   cryptographic purposes, but it should be fine for primality testing,
82   since all we really care about there is good statistical properties.
83 
84   As many digits as a currently has are filled with random digits.
85  */
86 
mpp_random(mp_int * a)87 mp_err  mpp_random(mp_int *a)
88 
89 {
90   mp_digit  next = 0;
91   unsigned int       ix, jx;
92 
93   ARGCHK(a != NULL, MP_BADARG);
94 
95   for(ix = 0; ix < USED(a); ix++) {
96     for(jx = 0; jx < sizeof(mp_digit); jx++) {
97       next = (next << CHAR_BIT) | (RANDOM() & UCHAR_MAX);
98     }
99     DIGIT(a, ix) = next;
100   }
101 
102   return MP_OKAY;
103 
104 } /* end mpp_random() */
105 
106 /* }}} */
107 
108 /* {{{ mpp_random_size(a, prec) */
109 
mpp_random_size(mp_int * a,mp_size prec)110 mp_err  mpp_random_size(mp_int *a, mp_size prec)
111 {
112   mp_err   res;
113 
114   ARGCHK(a != NULL && prec > 0, MP_BADARG);
115 
116   if((res = s_mp_pad(a, prec)) != MP_OKAY)
117     return res;
118 
119   return mpp_random(a);
120 
121 } /* end mpp_random_size() */
122